Skip to content

Instantly share code, notes, and snippets.

@jennschiffer
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jennschiffer/9262061 to your computer and use it in GitHub Desktop.
Save jennschiffer/9262061 to your computer and use it in GitHub Desktop.
Things about GHC that really grinds my gears: a Kindle Single

Things about GHC that really grinds my gears

Ranges

[-5..0] -> -5,-4,-3,-2,-1,0

but

[-10..-5] -> "not in scope ..-"

to get it to work

[-10.. -5] or [-10 .. -5]

sup with dat

@sorenmacbeth
Copy link

this must be correct since everyone knows that all haskell code is perfect and infallible. God literally coded The Universe in Haskell. p sure that's in the bible.

@todd534
Copy link

todd534 commented Jul 18, 2014

... which is a big reason why we hail satan, tbh

@djspiewak
Copy link

For the record, this is arising from the fact that - is a unary operator and not part of the integer literal. This by itself makes a lot of sense, but combined with the flexibility of Haskell's infix identifier syntax, can produce some weirdness. The above is where you start seeing that weirdness.

The following might make it a bit more clear:

infixl 7 ..-
(..-) :: Int -> Int -> Int
a ..- b = [a .. -b]

Basically, Haskell is parsing ..- as a single infix operator that you're applying to the results of the expressions -(10) and 5. You can fake your way around this using the definition I gave above, or you can insert spaces (as you did). Certainly not the clearest error message, but it's sort of hard for GHC to do better in this case due to the flexibility of its identifier lexing.

As a sidebar, note that the operator I gave can't be used within a list comprehension ([ ... ]), so it only partially fakes around your issue.

As a deeper sidebar, it is technically possible for GHC to avoid some of these problems altogether by treating list comprehensions as a complex mixfix operator. However, as with nearly every parser in the history of ever, mixfix operators are a hack that don't work correctly in any sense of the word. In this particular case, we don't have a | in our comprehension, so there's no way for Haskell to directly disambiguate between our intended comprehension and a single element list containing the results of the expression -10 ..- 5. A more sophisticated generalized parser could return a forest containing both possibilities, allowing the typechecker to disambiguate (and ultimately select the option we originally intended). GHC's parser is too simplistic to perform this sort of late-stage disambiguation though, and so the designers had to (arbitrarily) choose one option or the other to get precedence, in this case the unary list with the funky operator.

@jhrr
Copy link

jhrr commented Jul 18, 2014

[-10..(-5)] should work

@yawaramin
Copy link

Can you perhaps use spaces consistently? Then no chance of ambiguity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment