Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Why is extra space needed in arithmetic sequence for user defined enum? [duplicate]

So for any built-in types in Haskell, I can construct an arithmetic sequence, like this one (for Int or Integer, etc.)

[1..5]

But if I define an enum type like:

data Suit = Club | Diamond | Heart | Spade deriving (Show, Enum) 

when I create an arithmetic sequence, I have to include space before an enum instance and the dot dot e.g.

[Club .. Diamond]

Why?

like image 301
MicMac Avatar asked Apr 04 '18 02:04

MicMac


1 Answers

The problem is that, according to Haskell's lexical rules, modid.varsym is a reference to a variable in a module. A modid is a sequence of 1 or more capitalized identifiers separated by dots and a varsym is a symbolic name. Club is a valid modid and . is a valid varsym (note that there's even an operator in the Prelude with that name and that can be accessed as Prelude..). So X.. is seen as the qualified name of a variable named . in the module X.

So Club..Diamond is tokenized as "qualified variable name, constructor name", not "constructor name, dotdot, constructor name"1. And when it tries to resolve the qualified variable name, it fails because there isn't an actually a module named Club.

The problem doesn't occur with [1..5] because 1 is not a valid module name, nor is there another way that 1.. or 1. form a valid token. Note that, unlike some languages, 1. is not allowed as a shorter way to write 1.0. If it were, you'd run into a similar problem because 1..5 would now be tokenized as "number, dot, number". But it's not, so the problem does not occur.


1 "Qualified variable name" wins out over "constructor name, dotdot" because of the maximum munch rule, which says that when there are multiple possibilities to match a token, take the one that matches the longest substring from the current position.

like image 107
sepp2k Avatar answered Oct 29 '22 16:10

sepp2k