class instance with nested types
Hello, I have the following problem: basic datatypes
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
and make an instance for list of characters
instance Xy [] Char where test [a] = a
this works, and an instance for a forest and tried something like this
instance ([] Tree) Char where test x@(N a xs):txs = a
I get illegal type errors. Is it possible to use nested types in a class ? Hope you can help me Matthias
On Fri, 27 Oct 2000, Matthias Höchsmann wrote:
Hello,
I have the following problem:
basic datatypes
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
and make an instance for list of characters
instance Xy [] Char where test [a] = a
this works, and an instance for a forest and tried something like this
instance ([] Tree) Char where test x@(N a xs):txs = a
Don't you mean test (N a xs:txs) = a ? /Lars L
Yes, I wanted to type it like you do. But anyway, i fixed the problem following Andreas Rossbergs suggestion. Matthias
Don't you mean
test (N a xs:txs) = a
?
/Lars L
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
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 constructor. 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
HTH, - Andreas -- Andreas Rossberg, rossberg@ps.uni-sb.de :: be declarative. be functional. just be. ::
I mumbled:
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 constructor.
The other way round, of course: you cannot apply the list constructor to it. - Andreas -- Andreas Rossberg, rossberg@ps.uni-sb.de :: be declarative. be functional. just be. ::
participants (3)
-
Andreas Rossberg -
Lars Lundgren -
Matthias Höchsmann