I am learning haskell. When I compile the code:
data Bintree a = Nulltree | Node (Bintree a) a (Bintree a)
Nulltree :: Bintree a
I get the following error:
Invalid type signature: Nulltree :: Bintree a Should be of form <variable> :: <type>
So how do I prevent this. I tried writing:
data Bintree a = Nulltree | Node (Bintree a) a (Bintree a)
Nulltree :: a -> Bintree a
but this gives the same error.
In standard Haskell, you cannot give type signatures to data constructors. The line
data Bintree a = Nulltree | Node (Bintree a) a (Bintree a)
implies that Nulltree :: BinTree a. You don't need to say it yourself, and you can't. Using the GADTSyntax language extension, you could instead write
data Bintree a where
Nulltree :: BinTree a
Node :: Bintree a -> a -> Bintree a -> Bintree a
(Thanks to chi for pointing out that GADTSyntax is sufficient, and the not-so-beginner-friendly GADTs extension is not required.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With