Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Language Report: definition of reservedid

Tags:

syntax

haskell

OK, so I've spent all day playing with the Haskell Language Report (2010), and finding all sorts of "interesting" edge-cases. Stuff that you'd think should be an error, but which is actually allowed.

In particular, consider the following excerpt from the lexical syntax reference (section 10.2):

reservedid → case | class | data | default | deriving | do | else
           | foreign | if | import | in | infix | infixl |
           | infixr | instance | let | module | newtype | of
           | then | type | where | _

You might expect to see qualified in that list… But it isn't there. (Nor is as or hiding, for that matter.)

Now I'm wondering… Is this an accidental oversight in the Report? Or is this a deliberate design decision?

It appears that GHC at least follows the letter of the specification, for it will cheerfully allow you to define a variable who's name is, in fact, qualified. Weird, but true. So it seems that this name is only "special" on one context. By contrast, you can never name a variable module. We could have made this word special only at the start of the file… but we didn't.

like image 662
MathematicalOrchid Avatar asked Apr 11 '15 16:04

MathematicalOrchid


2 Answers

qualified, as and hiding only occur in specific places as keywords, thus can be used as variable names.

  • qualified only occurs after import.
  • as only occurs after import qualified PACKAGE
  • hiding only occurs after import [qualified] PACKAGE [as NAME]

I see what you mean when you point out that module could be one of these placement-specific keywords, but it seems counter-intuitive to name anything module. Maybe a previous version of Haskell allowed multiple modules in one file?

One could say the same for qualified and hiding, but I've used as a lot, like so:

zip [] _  = []
zip _  [] = []
zip (a:as) (b:bs) = (a, b) : zip as bs

So, with that in mind, I think it is indeed a design decision, one that I think a lot of languages* could do with including.

It might be worth pointing out that deriving could be one of these words, because it always follows a data declaration, as well as foreign, because it's only reserved with the FFI extension.


*especially javascript, with it's huge list of mainly pointless reserved words!

like image 165
AJF Avatar answered Oct 20 '22 04:10

AJF


First, the keywords "as", "hiding", and "qualified" were not in the original definition of Haskell. When they were added they were not made into reserved words for the sake of backwards compatibility. Adding reserved words is a sure way to break code. It was a very deliberate decision.

Second, as far as I know Haskell does not forbid multiple modules per file. The specification simply does not talk about files. (But I don't know of any implementation that allows it.)

like image 45
augustss Avatar answered Oct 20 '22 04:10

augustss