I had this idea a few weeks ago, of merging the syntactical features of algebraic data types with the implementation freedom of abstract data types. Coming to think about it, I had this idea in 1988, when I created an equational “mathematics” system on top of Prolog.

Most people having dealt with declarative languages have encountered pattern matching, and, specifically, pattern matching against constructors for algebraic data types. The canonical example – to which even Erlang developer have access, being in a land without user-defined types – is lists, which have the following two constructors:

`[H|T]`

nil

The first one creates a list out of a head and another list and second one creates the empty list. The syntax differs a bit between languages, and most languages have the syntactic sugar `[X1, X2, ..., Xn]`

for deeper terms, but you get the idea.

This list can then be used in pattern matching, such as (using Haskell syntax):

- length nil = 0
- length [_|tail] = length tail + 1

The beauty of algebraic data types is that you can get this equational style of programming even for your own types. The ubiquitous example is a binary ordered tree, where I create a polymorphic type, i.e., expecting a concrete type for its labels:

- data Tree a = Leaf a | Branch a (Tree a) (Tree a)

You can then use it to check whether a value is contained in the tree, as:

- contains x (Leaf y) = x==y
- contains x (Branch y left right)
- | x==y = True
- | x<y = contains x left
- | x>y = contains x right

*Note 1:* Here you also get a glimpse into the guard system of Haskell. You are welcome!

*Note 2:* I currently abstract away the requirements of actually implementing **<** which is done via **classes** in Haskell’s generic type system. For the curious, one simply inserts an `Ord a =>`

before the type definition.

This is it. That is the type. Algebraic data types is the quintessence of WYSIWYG in the world of code.

But what happens if we are not satisfied with this explicit representation, but wants to *abstract* it away, and just provide operations to manipulate trees? Well, we have to define an **abstract data type**, which in Haskell is achieved by hiding the implementation behind a **module** and exporting just those abstract operations, like:

- module Tree(makeLeaf, makeBranch, getValue,
- getLeft, getRight, isLeaf)
- <i>... some really cool implementation of trees</i>

You might ask yourself why on earth we would want a different implementation of trees than that algebraic one anyway. One answer is that fully balanced trees with low update rates are quite susceptible to array implementations. I.e., fully balanced trees happen to be isomorphic to an ordered array. There is an array type in the **Prelude** (the standard library for Haskell) but in order not to get carried away into the more esoteric parts of that language, we will use simple lists. So, here is an implementation of that abstract tree:

- data Tree a = Tree { startR::Int, endR::Int, impl::[a] }
- -- first we have two helper functions
- midIndex tree = (endR tree + startR tree) / 2
- makeList (Tree start end list) = (take (end - start) . drop start) list
- -- As you see, building is expensive, accessing cheap
- makeLeaf x = Tree 0 1 [x]
- makeBranch x left right = Tree 0 (length app) app where
- app = makeList left ++ [x] ++ makeList right
- isLeaf [x] = True
- isLeaf _ = False
- getValue tree = tree !! midIndex tree
- getLeft (Tree start end list) = Tree start (midIndex tree) list
- getRight (Tree start end list) = Tree (midIndex tree + 1) end list

We now have this abstract tree (via that **Tree** module and its list implementation), which we can use as:

- contains x tree
- |isLeaf tree = value == x
- |x == value = True
- |x < value = contains x (getLeft tree)
- |x > value = contains x (getRight tree)
- where value = getValue tree

But we cannot use the nice equational appearance with pattern matching, as we did with the algebraic data type! This is what I propose to change. I want to have the pattern matching beauty of algebraic data types while being free to implement the type however I want.

What is needed is a mapping from the various parts of a pattern (or algebraic constructor) to a function, free to implement in whichever way, along with predicates associated with the constructors themselves.

A simple incarnation of that idea follows, in a Haskellish notation:

- dataclass AATree (tree a) =
- Leaf (getValue@a) by isLeaf |
- Branches (getValue@a) (getLeft@(tree a))
- (getRight@(tree a))
- instance AATree (Tree start end list) where
- <i>... definitions just as for the ADT above ...</i>

That way you can either use the abstract interface or the algebraic. This is beautiful, if I may say so myself

*Note:* In order to make this abstract data type an object-oriented type, we would have to define the abstract interface in a **class** structure. That is at least as OOish you can get in Haskell.

After writing this, I find out that one the Big Boys in functional programming, Philip Wadler, has come up with a very similar merging idea almost 20 years ago, two years before I even touched upon this idea myself. He calls it **views**. This does not make the idea less viable, though.

I will present a more detailed exposition on how to implement it – as a pre-processor – to Haskell and perhaps Scala, later. And, bring some new life into the **views** of Wadler’s.