newtype and type synonyms

type gives a second name

type Name = String introduces a synonym: the compiler replaces it with the type it names everywhere, so a Name and a String are the same type and every function that takes one takes the other. The value of a synonym is documentation: it gives the reader a name for what the string means and puts it in the signature.

type Name = String type Phone = String label :: Name -> Phone -> String label n p = n ++ ": " ++ p

The signature reads well and the compiler learns nothing from it. That is enough for many programs, and it is free: no constructor, no conversion, no wrapping at run time.

The mistake a synonym cannot catch

Because a synonym is the same type, the arguments can be swapped and the program still compiles. The mistake shows up in the output, not in the build, which is exactly what a type is supposed to prevent:

module Main (main) where type Name = String type Phone = String label :: Name -> Phone -> String label n p = n ++ ": " ++ p main :: IO () main = do putStrLn (label "Ada" "555-0101") putStrLn (label "555-0101" "Ada")

ghc -Wall -O1 -o synonyms synonyms.hs
./synonyms

Ada: 555-0101
555-0101: Ada

The second line is wrong and the build is clean with -Wall. A code review catches this one; a longer program with two string parameters and a helper in between often does not.

newtype makes a distinct type

newtype Meters = Meters Double declares a new type with one constructor and one field. Unlike data, the wrapper has no runtime cost: GHC represents a Meters exactly as a Double, and the conversion happens only in the pattern match and the construction. What it adds is a type the compiler will not confuse with any other.

module Main (main) where newtype Meters = Meters Double deriving (Show) newtype Seconds = Seconds Double deriving (Show) addMeters :: Meters -> Meters -> Meters addMeters (Meters a) (Meters b) = Meters (a + b) speed :: Meters -> Seconds -> Double speed (Meters m) (Seconds s) = m / s main :: IO () main = do print (Meters 3.0) print (addMeters (Meters 1.5) (Meters 2.5)) print (speed (Meters 100.0) (Seconds 9.58))

ghc -Wall -O1 -o newtype newtype.hs
./newtype

Meters 3.0
Meters 4.0
10.438413361169102

Now speed (Seconds 9.58) (Meters 100.0) does not compile, and neither does adding a Seconds to a Meters. The price is the pattern match in every function that needs the number, which is why newtypes are used at the boundary of a module rather than inside every calculation: convert once, work with plain numbers inside, wrap the result.

The division prints 10.438413361169102 rather than 10.44 because it is a Double: the record and the newtype add safety, not precision.

deriving through a newtype

A newtype can derive the classes of its field, and the instance it gets works on the wrapped value. That is how a small numeric type gets Eq, Ord, Enum and Show without writing any of them:

module Main (main) where newtype Count = Count Int deriving (Show, Eq, Ord, Enum) main :: IO () main = do print (Count 2) print (Count 2 == Count 2) print (Count 2 < Count 3) print (countTo 3) countTo :: Int -> [Count] countTo n = [Count 1 .. Count n]

ghc -Wall -O1 -o counted counted.hs
./counted

Count 2
True
True
[Count 1,Count 2,Count 3]

The derived Show prints the constructor, which is the debugging format of the last lesson. The derived Enum is what makes the range work: the compiler translates the enumeration into operations on the wrapped Int. When the wrapping should be invisible even to Show, the lesson on instances shows GeneralizedNewtypeDeriving and manual instances.

Exercises

  1. Add type Email = String and a function that takes a name and an email, then swap the arguments and check that the program still builds.
  2. Write newtype Celsius = Celsius Double and a function that converts it to Fahrenheit.
  3. Try to add a Celsius to a Meters and read the type error.
  4. Derive Eq for Meters and compare two values.
  5. Add Bounded to Count and print maxBound.
  6. Explain in two sentences when a synonym is enough and when a newtype is worth the pattern match.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does type Name = String create?

    Answer

    Another name for String, which the compiler replaces

    A synonym is only a name; the types are identical and interchangeable.

  2. What does newtype Meters = Meters Double create?

    Answer

    A distinct type with one constructor and one field

    newtype declares a new type; the compiler still represents it as the field type at run time.

  3. Can label :: Name -> Phone -> String be called with the arguments swapped, when both are type synonyms of String?

    Answer

    Yes, and the build stays clean

    Synonyms are the same type, so the compiler sees String -> String and accepts any order.

  4. What is the runtime cost of a newtype wrapper?

    Answer

    None: the value is represented as its field

    GHC erases the wrapper; only construction and pattern matching are visible in the source.

  5. What does addMeters (Meters 1.5) (Meters 2.5) return?

    Answer

    Meters 4.0

    The two doubles are added and wrapped again, and print shows the constructor.

  6. What does deriving (Show, Eq, Ord) on a newtype give you?

    Answer

    Instances that work on the wrapped value

    The derived instances delegate to the field type, so the newtype compares and prints like the value it wraps.

  7. What does this program print? module Main (main) where newtype Count = Count Int deriving (Show, Eq) main :: IO () main = print (Count 2 == Count 2)

    Answer

    True

    The derived Eq compares the wrapped Ints, which are equal.

  8. Why does speed (Seconds 9.58) (Meters 100.0) fail to compile?

    Answer

    Seconds and Meters are different types, and the function takes them in the other order

    The newtype makes the two arguments distinct, so the swap is a type error.

  9. Where is a newtype usually unwrapped?

    Answer

    At the boundary of the module, once per value

    The point is safety at the interface; inside, plain numbers are simpler.

  10. What does [Count 1 .. Count n] need to work?

    Answer

    An Enum instance, which deriving provides

    A range is written with enumFromTo, so the type needs Enum.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev