[Haskell-cafe] Basic question concerning data constructors
dave at zednenem.com
Sun Dec 30 15:19:48 EST 2007
On Dec 30, 2007 9:24 AM, Joost Behrends <webmaster at h-labahn.de> wrote:
> A similar point: The tutorials teach, that "=" has a similar meaning than
> "=" in
> mathematics. But there is a big difference: it is not reflexive. The
> the right side is the definition of the left. Thus "x=y" has still some
> kind of
> temporality, which mathematics doesn't have. Wadler himself describes
> of lazily computed equations as "dataflows" somewhere.
The "=" in the data declaration syntax is different from the "=" in value
and type declarations.
type A = B
means that "A" can be used wherever "B" can be used.
data A = B
means that "B" constructs a value of type "A". The "=" acts more like the
"::=" in a BNF grammar. It is *not* a claim that A equals B, since A is a
type and B is a data constructor. Furthermore, types and data constructors
have disjoint namespaces, hence the common idiom of using the same name for
the type and the constructor when the type has only one constructor.
There is an alternative syntax for data declarations in recent versions of
GHC. Using it, you would write:
data A where
B :: A
This defines a type A, and a constructor B which has type A.
data ClockTime where TOD :: Integer -> Integer -> ClockTime
This defines a type ClockTime, and a constructor TOD which takes two
Integers and constructs a ClockTime.
data Pair :: * -> * -> * where
Pair :: a -> b -> Pair a b
This defines a type constructor Pair, which takes two types and constructs a
new type, and a constructor, also called Pair, which, for arbitrary types a
and b, takes a value of type a and a value of type b and constructs a value
of type Pair a b.
Dave Menendez <dave at zednenem.com>
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe