Version 4 (modified by simonmar@…, 8 years ago) (diff)

add sample code for fixity resolution

Ticket: #30

This is a proposal that doesn't affect anything except the presentation of the report, because all Haskell implementations currently do it this way anyway. There are some legal Haskell programs, according to the report, that aren't accepted by current implementations (see below). I propose we make the language specification match the implementations.

The Problem

The Haskell 98 context-free syntax includes fixity resolution as part of the grammar, with a fixed number of fixities ([1..9]), essentially using macro expansion to define the grammar. Apart from being really ugly, and not corresponding to any known implementation (long ago GHC used a yacc parser formulated like this, but not any more), this gives rise to some constructions that are very hard to parse correctly. For example, the expression:

  \x -> x == x == True

should be legal, and parse as (\x -> x == x) == True. The reason is that == is nonfix (assuming the == in scope is from the Prelude), and this combined with the rule that says "lambda expressions extend as far to the right as possible", forces the lambda expression to end before the second ==.

No known Haskell compiler actually parses this correctly. Admittedly it's a type error, but that's not the point - the grammar should be parsable.


  do a == b == c

This is legal syntax, and parses as do { a == b } == c, again because == is nonfix, and the second == closes the layout context by virtue of the layout rule's parse error condition.

As far as I know, no Haskell compiler gets this right.

The Solution

These constructions rely on the parser being aware of operator fixities during parsing, which is an unreasonable restriction - it's often easier to follow imports after parsing a file, rather than during it.

Hence, I propose that we remove operator fixity resolution from the context-free grammar, and specify it separately. The context-free grammar would parse applications of infix operators as a flat list to be resolved separately. The examples above would parse but fail during fixity resolution.

This would also let us expand the current paultry set of 9 fixity levels to an arbitrary limit, if we so wished (but that introduces backwards compatibility issues).

Sample code for resolving fixities is here: resolve.hs. I believe this implements Haskell 98 fixity resolution, without prefix negation (prefix negation could be added, but we might not need to; see NegativeSyntax). The core of the parser is 12 lines of code, with a few lines of datatype declarations and pretty-printing. Note that there is no upper limit on precedence levels, but there is a lower limit of zero. This code could serve as the basis for specifying fixity resolution in the Haskell' report.

Attachments (2)

Download all attachments as: .zip