hask1023
hask1023

Reputation: 109

Why do I get a parse error for my binary tree definition?

When I type this code in Haskell:

data BT a = Empty | Fork a (BT a) (BT a) 
  Empty :: BT a
  Fork  :: a -> BT a -> BT a -> BT a

I get

error: parse error on input `::'

I can't see what the issue is though, if I add deriving (show) to the top line, it changes to

error: parse error on input `Empty'

Upvotes: 0

Views: 95

Answers (1)

Iceland_jack
Iceland_jack

Reputation: 7014

Either write it using the "legacy" uniform datatype syntax:

data BT a = Empty | Fork a (BT a) (BT a)

or use equivalent GADT syntax

{-# Language GADTs #-}

data BT a where 
  Empty :: BT a
  Fork  :: a -> BT a -> BT a -> BT a

But not both :)

Upvotes: 6

Related Questions