data Children a = NoChildren | Cons a (Children a)
means your Children a
is isomorphic to [a]
, and hence your
data Tree a = EmptyTree | Node a (Children a)
is isomorphic to
data List a = Empty | Nonempty a [a]
which is again isomorphic to [a]
.
What you want is that the children themselves are Tree
s, so you should use
data Children a = NoChildren | Cons (Tree a) (Children a)
or plain
data Tree a = EmptyTree | Node a [Tree a]
The error is because subtree1
has type Tree a
for some a
belonging to Num
(ditto for subtree2
). Then when you write
tree1 = Node 83 (subtree1 `Cons` (subtree2 `Cons` NoChildren))
the inferred type of tree1
is Tree (Tree a)
(for some a
belonging to Num
), and hence
83 :: Tree a
But there's no Num
instance for Tree
s.