class instance with nested types

Andreas Rossberg
Fri, 27 Oct 2000 14:07:37 +0200

Matthias Höchsmann wrote:
> > type Sequence a = [a]
> > data Tree a = N a (Forest a) deriving (Ord,Eq,Show)
> > type Forest a = Sequence (Tree a)
> i want to construct a class Xy
> > class Xy s a where
> >      test :: s a -> a
> [...]
> > instance  ([] Tree) Char where
> > test x@(N a xs):txs = a

To make it syntactically correct this should at least be something like

> instance Xy ([] Tree) Char where
> test (N a xs:txs) = a

But the real problem is in the expression ([] Tree), which is the same
as writing [Tree]. This is not a legal type expression, since Tree is a
type constructor, not a ground type, so you cannot apply it to the list

What you are trying to say is probably something like this:

> instance Xy (\a . [Tree a]) Char	-- not Haskell

But unfortunately there are no lambdas on the type level - they would
render the type system undecidable. For the same reason it is not
allowed to use a type synonym in an instance declaration:

> instance Xy Forest Char		-- illegal

The only thing you can do is turning Forest into a data type:

> data Tree a = N a (Forest a) deriving (Ord,Eq,Show)
> data Forest a = Forest [Tree a]
> instance Xy Forest Char where
> test (Forest (N a xs:txs)) = a


	- Andreas

Andreas Rossberg,

:: be declarative. be functional. just be. ::