1 Type constructor

illustrates how to define a data type with type constructors (and data constructors at the same time). The type constructor is named

Tree

, but a tree of what? Of any specific type

a

, be it

Integer

,

MaybeString

, or even

Tree b

, in which case it will be a tree of tree of

b

. The data type is polymorphic (and

a

is a type variable that is to be substituted by a specific type). So when used, the values will have types like

Tree Int

or

Tree (Tree Boolean)

.

2 Data constructor

A data constructor groups values together and tags alternatives in an algebraic data type,

data Tree a = Tip | Node a (Tree a)(Tree a)

where there are two data constructors,

Tip

and

Node

. Any value that belongs to the type

Tree a

(I'm happy leaving the type parameter unspecified) will be a constructed by either

Tip

or

Node

.

Tip

is a constructor alright, but it groups no value whatsoever, that is, it's a nullary constructor. There can be only one value that will have this constructor, also conveniently denoted

Tip

. So nullary constructors contain no data apart from its name! For example, the

Bool

data type is defined to be

dataBool= True | False

and for all practical purposes you can just think of them as constants belonging to a type.

On the other hand,

Node

contains other data. The types of those data are its parameters. The first one has type

a

, so it's just a value of the parameter type

a

. This one is the value the tree node holds in it. The remaining two are the branches. Each of them have type

Tree a

, naturally.

2.1 Note

Data constructors are not types! They denote values. It would be illegal to write

Node a (Node a)(Node a)

there, because the type is

Tree

, not

Node

.

3 Deconstructing data constructors

All a data constructor does is holding values together. But you want to separate them if you want to use them. This is done via pattern matching,

depth Tip =0
depth (Node _ l r)=1+max(depth l)(depth r)

So, the depth of a tip is zero. The depth of a node depends on its branches, but not it's content. See how the constructor in the left hand side names its parts? we don't need the content so we don't name it (using

_

). The left branch is named

l

, the right

r

, allowing us to use these values in the right hand side.

4 Notes

You can declare a constructor (for both type and data) to be infix, and this can make your code a lot more readable.

Tuples are a built in feature of the syntax but are plain old algebraic data types! They have only one constructor though. Having the same name as their types (don't freak out, it's just a matter of convenience, as the type constructors and the data constructors have separate namespaces). So,

(4, True)

is really a value of the form

(,)4 True

having the type

(,)IntBool

, which, too, is written conveniently as

(Int,Bool)

to make it more readable. Incidentally, the empty tuple type

()

with its only value

()

is used throughout, and is called unit.

You can, in fact, name the values grouped together, using the record syntax,