a newbie's question
Fergus Henderson
fjh@cs.mu.oz.au
Mon, 15 Oct 2001 12:12:14 +1000
On 12-Oct-2001, Song Li <lisong@iastate.edu> wrote:
> Hi, there,
> Could anybody explain what does this type defination mean :
>
> data xxx a = xxx (a ->b)
>
> looks xxx can use itself as constructor(like tree) but change the
> type......
Interpreted literally, that declaration would actually be a syntax error;
but I'll assume that your are using "xxx" and "b" as meta-variables,
with "xxx" being replaced by a name starting with a capital letter,
and with "b" being replaced by a type. For example, let's assume
that "xxx" is "Foo" and "b" is "Integer".
data Foo a = Foo (a -> Integer)
This defines a type constructor named "Foo" of kind "* -> *".
It also defines a data constructor named "Foo"
of type "(a -> Integer) -> Foo a".
The type constructor and the data constructor happen to
share the same name, but they are separate entities.
In other words, the semantics is pretty much just the same
as it would be if you wrote
data Foo a = MkFoo (a -> Integer)
except that rather than using "Foo" for the name of the
type constructor and "MkFoo" for the name of the data
constructor, the original declaration overloads the name
"Foo" to mean both the type constructor and the data
constructor. For uses of the overloaded name "Foo",
the Haskell compiler will infer from the context which is
intended.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "... it seems to me that 15 years of
The University of Melbourne | email is plenty for one lifetime."
WWW: <http://www.cs.mu.oz.au/~fjh> | -- Prof. Donald E. Knuth