* For this post, I’m doing a bit of an experiment. Haskell includes a “literate” syntax mode. In literate mode, and text that doesn’t start with a “>” sign is considered a comment. So this entire post can be copied and used as a source file; just save it with a name ending in `”.lhs”`. If this doesn’t work properly, please post something in the comments to let me know. It’s more work for me to write the posts this way, so if it’s not working properly, I’d rather not waste the effort. I’ve tested it in both Hugs and ghci, and everything works, but who knows what will happen after a pass through MovableType!*

Like most modern programming languages, Haskell has excellent support for building user-defined data types. In fact, even though Haskell is very much *not* object-oriented, most Haskell programs end up being centered around the design and implementation of data structures.

So today, I’m going to start looking at how you implement data types in Haskell. What I’m

going to do is start by showing you how to implement a simple binary search tree. I’ll start with

a very simple version, and then build on that.

The most common form of user-defined data type in Haskell is a *constructed* data type.

Constructed types are types with one or more *variants*, each of which has an associated

*constructor function*. The names of constructor functions must be unique to the type. Let’s start

with a very simple version of a binary search tree containing integers. We’ll build it so that there

are empty nodes and non-empty nodes. Every non-empty node of the tree has an integer value, a left

child, and a right child. A declaration for that would look like the following:

>data SimpleIntBST = SIBNode Integer SimpleIntBST SimpleIntBST > | SIBEmpty

What this says is: we’re defining a data type named `SimpleIntBST`

. There are two kinds of `SimpleIntBST`

values. The first kind is an `SIBNode`

, which contains an integer value, a left child, and a right child. The other kind is an `SIBEmpty`

, which contains no values. `SIBNode`

and `SIBEmpty`

are the names of the *constructor functions* that are used to create values. To create an

`SIBNode`

, you call it like a normal function with parameters of the correct type: for example, to create an `SIBNode`

containing the value “4” with empty left and right children, you’d write:

SIBNode 4 SIBEmpty SIBEmpty

Let’s try that in GHCI:

*Main> SIBEmpty <interactive>:1:0: No instance for (Show SimpleIntBST) arising from use of `print' at <interactive>:1:0-7 Possible fix: add an instance declaration for (Show SimpleIntBST) In the expression: print it In a 'do' expression: print it

What’s wrong? The interpreter tried to print out the value returned by the expression.

The interpreter outputs values using the `print`

function. In general in Haskell, when we

have a problem like this, we look at types. What’s the type of the `print`

function?

*Main> :type print print :: (Show a) => a -> IO () *Main>

The parameter to `print`

must be a value of some type that is an instance of the `Show`

class. But we didn’t make our binary tree an instance of `Show`

. Fortunately, since `Show`

is such a fundamental interface, Haskell provides a nifty shorthand to make the compiler *generate* the code necessary to provide an implementation of whatever is needed for `Show`

, called a *deriving* clause. Let’s try again, this time with a deriving clause. We’ll call the new version “IntBST” rather than “SimpleIntBST”, because treating

this post as a literate file, we can’t re-declare “SimpleIntBST”.

>data IntBST = IBNode Integer IntBST IntBST > | IBEmpty deriving (Show)

Now, loading that into GHC, we’ll see:

*Main> IBNode 4 IBEmpty IBEmpty IBNode 4 IBEmpty IBEmpty *Main>

Which is exactly what we want: our binary search tree is now printable.

A data structure like this isn’t useful without some operations to work with it. So let’s

go ahead and do some implementing! We’ll start with an insert operator:

>ibstInsert :: IntBST -> Integer -> IntBST >ibstInsert (IBNode i left right) v > | i > v = IBNode i (ibstInsert left v) right > | otherwise = IBNode i left (ibstInsert right v) >ibstInsert IBEmpty v = (IBNode v IBEmpty IBEmpty)

So let’s look at that a bit. It starts off with an explicit type declaration. It’s not strictly

necessary, but it’s a good practice to put it there. Then we get to the real definition, which is

where things get interesting. I’m sure you noticed that there was something a bit strange about the

type declaration for `IntBST`

. In most non-functional languages, you define a new data type as some kind of a *record* or *structure*, which contains a list of *named* fields; when you use the structure, you access its fields by derefencing an instance of the data structure using the name of a field. But in our Haskell binary search tree, the values belonging to an instance of the data structure *don’t have names*. Like most functional languages, Haskell uses *pattern matching* to access the pieces of a data structure.

A *pattern* looks like a value expression, except that it may contain some *unbound* variables. The pattern can also be followed by a *guard expression* which

is a boolean expression using variables bound in the pattern. If the guard is included, it’s separated from the patterns by a `|`

character, which is generally read as “such that”.

So the first declaration line of `ibstInsert`

has two patterns: `(IBNode i left right)`

, and `v`

. It’s followed by a guard. So that full declaration line can be read as “ibstInsert of an IntBST matching ‘`(IBNode i left right)`

‘ and a value matching ‘`v`

‘ *such that* ‘`i > v`

‘ returns ‘`(IBNode i left right)`

‘.

As a shorthand, if you have a guard expression on an indented line following a pattern

with a guard, the pattern is reused with the second guard. So the second line

starting with a “`|`

” is used if the first guard fails. As a guard expression,

“`otherwise`

” can only be used as the *last* guard for a pattern, and

it’s always true.

So the first case of the declaration of `ibstInsert`

, in full, does the

following:

- If the tree parameter is an
`IBNode`

, then bind “i” to the value

element of the node, “left” to the left subtree, and “right” to the right subtree; and

bind the variable “v” to the second parameter, which is the value to be inserted to the tree. - If the value in the tree node is larger than the value being inserted, then return a tree that’s the same as this, except that the value is inserted into the
*left*subtree. - Otherwise, return a tree like this one, except that the new value is inserted

into the*right*subtree.

The second case says that if the node is an empty, then return a new non-empty node with

the new value as its value, and with empty left and right children.

If you look at the code now that you know how to read it, one thing that should hopefully be rather striking about it is just how much it looks like the explanatory pseudo-code that you’d

find in an algorithms textbook. It’s very straightforward code, written in a very easy to read style. (As a brief aside, this is an example of one of the things that really attracts me to Haskell programming. Code in Haskell tends to very naturally decompose into very small definitions that

look and read like explanatory code from a textbook.)

So let’s look at a quick example of using this to build a tree.

>treeone = ibstInsert (ibstInsert (ibstInsert (ibstInsert (ibstInsert IBEmpty 3) 5) 2) 17) 10 >treetwo = (ibstInsert (ibstInsert (ibstInsert (ibstInsert treeone 4) 6) 1) 9)

That gives us two trees, one of which contains a subset of the values in the other. We can ask ghc to show us the values of them:

*Main> treeone IBNode 3 (IBNode 2 IBEmpty IBEmpty) (IBNode 5 IBEmpty (IBNode 17 (IBNode 10 IBEmpty IBEmpty) IBEmpty)) *Main*gt; treetwo IBNode 3 (IBNode 2 (IBNode 1 IBEmpty IBEmpty) IBEmpty) (IBNode 5 (IBNode 4 IBEmpty IBEmpty) (IBNode 17 (IBNode 10 (IBNode 6 IBEmpty (IBNode 9 IBEmpty IBEmpty)) IBEmpty) IBEmpty)) *Main>

The other thing that we want to be able to do with a binary search tree is find nodes in it. Since our current BST only contains integers, we’ll just write a function that returns a boolean value indicating whether or not a particular value is in the tree.

> >ibstSearch :: IntBST -> Integer -> Bool >ibstSearch (IBNode k left right) v > | k == v = True > | k > v = ibstSearch left v > | otherwise = ibstSearch right v >ibstSearch IBEmpty _ = False >

This one should be pretty easy to read without much explanation. To be pedantic, I’ll walk through it quickly. If it’s a non-empty node, and the value in the node equals the value being searched for, return true. If the value in the node is bigger, then return the result of searching the left subtree; otherwise, return the result of searching the right subtree. If it’s an empty node, then the value isn’t there, so return false.

A binary search tree isn’t particularly useful if it can only hold integers. So as a first step

towards making it more real, we’d like to be able to store not just integers in the tree,

but any value for which the necessary comparisons (`>`

and `==`

) are

defined. There’s a typeclass for that, called “`Ord`

“, for *Ordered*. So now let’s just rewrite `IntBST`

as a generic BST using typeclasses:

>data (Ord a) => GenBST a = GBNode a (GenBST a) (GenBST a) > | GBEmpty deriving (Show)

The variable on the *left* of the equal in the data type definition is

a type parameter. The type-class constraint preceding the name of the

new datatype means that the type parameter “`a`

” can *only* be

instantiated by a type that is an instance of the type-class “`Ord`

“. The

method implementations will barely change:

>gbstInsert :: (Ord a) => GenBST a -> a -> GenBST a >gbstInsert (GBNode i left right) v > | i > v = GBNode i (gbstInsert left v) right > | otherwise = GBNode i left (gbstInsert right v) >gbstInsert GBEmpty v = (GBNode v GBEmpty GBEmpty) > >gbstSearch :: (Ord a) => GenBST a -> a -> Bool >gbstSearch (GBNode k left right) v > | k == v = True > | k > v = gbstSearch left v > | otherwise = gbstSearch right v >gbstSearch GBEmpty _ = False

With that implementation of `gbstInsert`

, we can pretty much reuse the code to

generate a sample instance:

>gtreeone = gbstInsert (gbstInsert (gbstInsert (gbstInsert (gbstInsert GBEmpty 3) 5) 2) 17) 10 >gtreetwo = (gbstInsert (gbstInsert (gbstInsert (gbstInsert gtreeone 4) 6) 1) 9)

Look what ghci says about those:

*Main*gt; gtreetwo GBNode 3 (GBNode 2 (GBNode 1 GBEmpty GBEmpty) GBEmpty) (GBNode 5 (GBNode 4 GBEmpty GBEmpty) (GBNode 17 (GBNode 10 (GBNode 6 GBEmpty (GBNode 9 GBEmpty GBEmpty)) GBEmpty) GBEmpty)) *Main> :type gtreetwo gtreetwo :: GenBST Integer *Main> gbstSearch gtreetwo 17 True *Main> gbstSearch gtreetwo 18 False *Main> gbstSearch gtreetwo 1 True *Main> gbstSearch gtreeone 1 False *Main>

We can use the new generic tree in *exactly* the same way that we used the integer

specific one. We don’t need to declare types of expressions that use it. The compiler

will happily infer the type parameter, and just make everything work. But now we’re not

restricted to integers:

*Main> gbstInsert (gbstInsert (gbstInsert GBEmpty "hi") "hello") "Salutations" GBNode "hi" (GBNode "hello" (GBNode "Salutations" GBEmpty GBEmpty) GBEmpty) GBEmpty *Main> gbstInsert (gbstInsert (gbstInsert (gbstInsert GBEmpty "hi") "hello") "S alutations") "Greetings" GBNode "hi" (GBNode "hello" (GBNode "Salutations" (GBNode "Greetings" GBEmpty GBEmpty) GBEmpty) GBEmpty) GBEmpty *Main>

I think that’s enough for one day. Next time, we’ll take our binary search tree, and look at using it for key/value pairs, and maintaining tree balance.

Josiah CarlsonThere’s just something about Haskell syntax that doesn’t rub me the right way. Maybe it’s the lack of commas or something (I’m a Python guy). It could even be that I’m learned Scheme and SML first, so the lack of punctuation feels odd. I don’t know.

When taking the time to hack together equivalent Python code, I found it somewhat difficult to match your use of the empty node object. Either I had to use None and a couple ifs/ternary operators (for about 18 lines using a terse style), or for the full-on functional variant like the above, I needed to use two classes (one of which is a singleton), which ended up being 24 terse lines (or 17 using ternary operators). Then again, I suppose Python isn’t a language for direct use in a functional style, so I shouldn’t be terribly surprised.

Interesting in any case.

PseudonymAll of those gbstInserts are looking a bit unwieldy. But thankfully, there’s an easy fix.

If the type of insert was this instead:

> gbstInsert :: (Ord a) => a -> GenBST a -> GenBST a

Then you could just do this:

foldr gbstInsert GBEmpty [4,6,1,9]

This is why all of the standard container types, like Data.Map and Data.Set use that ordering. Basically, you give “insert” the same ordering that the standard list cons has.

mattiastPseudonym:

“All of those gbstInserts are looking a bit unwieldy. But thankfully, there’s an easy fix.”

You don’t need that fix: you just have to use foldl instead of foldr.

MasklinnWell even though it’s not how it’s usually done, even without changing the current code we could use foldl to initialize the trees, like this:

I guess he user neither because Mark tries to write a serie for beginner and hasn’t introduced folds yet.

PseudonymQuite right, you can use foldl. Incidentally, people who have done some Haskell may wonder if one is more stack-efficient than the other. For structures like binary trees, the answer is actually quite complex due to lazy evaluation. We might discuss that later, if it seems appropriate.

Incidentally, I didn’t think that the stuff in my comment was an omission from the article. Just extra info for the curious. 🙂 However, it

istrue that, as a convention, insert-type functions are almost always written in a way that they work like foldr.ChrisI find the type declaration syntax hard to get used to; for example:

f :: (Num a) => a -> a

really means that

f :: a -> a (for some type “a” in class Num)

It’s confusing IMO that “(Num a) =>” is stuck in the middle there. “=>” has low precedence to the compiler, but it doesn’t have low precedence to your *eye* until you look at lots of these declarations. I’d prefer a syntax something like:

f :: a -> a where Num a

I wonder if there’s some mathematical notation that this => is approximating, that I’m not familiar with.

JustinMark,

This series continues to be a nice read. Thanks for trying out the “literate” style – I liked being able to paste the post into a file and start interacting with it right away.

PseudonymChris, yes, => is a kind of implication.

You can read it as saying that if Num a is true, then f has type a -> a. This is analogous to the Curry-Howard interpretaion of function types as implications. Operationally, it’s actually a function type: The “type dictionary” (or, in C++ parlance, the “vtable”) for Num a gets passed as a separate argument to f.

mgsloanYeah, literate style is pretty awesome for tutorials and such 🙂

“We’ll call the new version “IntBST” rather than “SimpleIntBST”, because treating this post as a literate file, we can’t re-declare “SimpleIntBST”.” – You could leave the > s off the first one, if you really want to redefine it with the same name.

Something I find interesting in regards to trees (and other structures!) is that if you find yourself manually inlining traversal into your functions, you can just create traversal functions which yield a list. I’m not sure about list modification under this model, however i’m sure you could do it with a monad 🙂

Brandon Bickfordmy favorite haskell tree definition:

data Tree a = Tree {value :: a, branches :: [Tree a]} deriving (Ord, Eq, Show)

Matthias BenkardRegarding => and implication: When you realise that you can even use the universal quantifier (called »forall« in Haskell) in type signatures, the syntax becomes quite a bit more intuitive.