[Haskell-cafe] Newbie Question on type constructors
Paul Hudak
paul.hudak at yale.edu
Mon Nov 1 11:44:24 EST 2004
Oh, I disagree with this point of view. Circle is certainly a value,
i.e. a full-fledged function, as Brian Beckman correctly surmised. The
Haskell designers did not decide "for convenience" that Circle is the
same as \x -> Circle x. Rather, that's a fundamental law (the eta law,
to be exact) of the lambda calculus, on which Haskell is based.
The real reason that the Haskell designers chose to have constructors
begin with a capital letter is to make pattern-matching clearer. For
example, if one writes (\Foo -> 42) it is clear that Foo is a
constructor, and this function will be an error when applied to anything
but Foo, whereas (\foo -> 42) will match anything. If the namespaces
were not separated in this way, then you wouldn't know whether "foo" was
a constructor or a formal parameter, without looking at your whole
program -- including imports -- to track down all "data" declarations.
I hope this helps,
-Paul
Ben Rudiak-Gould wrote:
> 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
--
Professor Paul Hudak
Chair, Dept of Computer Science Office: (203) 432-1235
Yale University FAX: (203) 432-0593
P.O. Box 208285 email: paul.hudak at yale.edu
New Haven, CT 06520-8285 WWW: www.cs.yale.edu/~hudak
More information about the Haskell-Cafe
mailing list