[Haskell-cafe] Newbie Question on type constructors

Ben Rudiak-Gould Benjamin.Rudiak-Gould at cl.cam.ac.uk
Mon Nov 1 11:28:25 EST 2004


Brian Beckman wrote:

 >data Shape = Circle Float
 >           | Square Float
 >
 >I read this something along the lines of "'Shape' is a type constructor,
 >for use in other type-defining expressions, and 'Circle' and 'Sqare' are
 >its two data constructors, which should be used like functions of type
 >'Float -> Shape'".  Indeed, typing "Circle" at the Hugs prompt reveals
 >that Haskell has a "function" named "Circle" with type "Float -> Shape."
 >
 >However, I don't know of other circumstances where (1) I can declare
 >functions with capitalized names (Hugs groans about syntax errors if I
 >attempt the following:
 >
 >Circle2 :: Float -> Shape
 >Circle2 =  Circle
 >
 >And (2) where the argument-types of a function can be declared on the
 >function's right-hand side.

I remember being confused in a similar way by data constructors when I 
learned Haskell. You might find it easier to think of "Circle" and 
"Square" as part of the name of a value. "Circle 1.2" is one of the 
values in the type Shape, for example; it's not a function call which 
returns the value, it just *is* the value. Circle by itself doesn't 
really mean anything -- it's not a value of any type -- and Haskell 
could have been designed to make it a syntax error. But for convenience 
Haskell's designers decided to treat it as though it meant (\x -> Circle x).

-- Ben



More information about the Haskell-Cafe mailing list