tags:

views:

108

answers:

1

I'm in the process of writing a small lisp interpreter in haskell. In the process i defined this datatype, to get a less typed number;

data Number = _Int Integer
            | _Rational Rational
            | _Float Double
    deriving(Eq,Show)

Compiling this fails with the following error:

ERROR "types.hs":16 - Syntax error in data type declaration (unexpected `|')

Line 16 is the line w. the first '|' in the code above.

Edit: It would appear that the underscores in the type constructors are causing the problem.

+8  A: 

Hugs is being a little bit roundabout here. The actual problem is not the |, but the underscores at the beginning of the constructor names — they aren't allowed to begin with underscores. It's not just a convention that constructors start with a capital letter, but part of Haskell's syntax.

My best guess as to what Hugs is "thinking" is that, since your first constructor wasn't named correctly, when you offer an alternative constructor afterward, Hugs says, "Wait, I haven't seen a valid constructor yet! What's going on?"

GHC gives a clearer error:

types.hs:1:14: Not a constructor: `_Int'
Chuck