
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...... Thanks! Song

On 12-Oct-2001, Song Li
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
participants (2)
-
Fergus Henderson
-
Song Li