I’m a newcomer to Haskell and am currently going through Real World Haskell. The book says the type constructor is used only in the type signature while the value constructor is used in actual code. It also gives an example of a declaration to show that the names for both are independent of each other. Why are two constructors needed in the first place, if only one of them is used in actual code? Since we would not use the type constructor in actual code, what purpose does the type constructor serve?
Share
Maybe the names are a little bit misleading. A type constructor represents the name of the type you’re declaring. They’re called like that because they build types, not values: indeed, being (possibly) parameterized on type variables they define a family of types. They act something like C++’s templates and Java’s generics. In
data MyType a b = Constr a b, MyType is a type constructor that takes two typesaandbto build a new type(MyType a b).A value constructor is the only part you would call a “constructor” in other (object oriented) languages, because you need it in order to build values for that type. So, in the previous example, if you take the value constructor
Constr :: a -> b -> MyType a b, you may build a valueConstr "abc" 'd' :: MyType [Char] Char.