3

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 with hugs fails with the following error:

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

Line 16 is the line with the first | in the code above.

Angra Mainyu
  • 184
  • 3
  • 13
keiter
  • 3,534
  • 28
  • 38
  • What compiler are you using? GHCi (v 6.10.4) complains about the underscores, but when I s/_/M/, it compiles fine. – perimosocordiae Mar 17 '10 at 23:10
  • I'm using hugs98 200609-4. And it's indeed the underscores causing the problem. :) – keiter Mar 17 '10 at 23:25
  • 8
    Just as a side note, I would recommend GHC over Hugs in almost any situation. A lot of older documents say that Hugs is better for beginners because it has a REPL, but that's not true so much any more with GHCi actually offering a better REPL than Hugs'. – Chuck Mar 17 '10 at 23:30

1 Answers1

13

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
  • 234,037
  • 30
  • 302
  • 389