[Haskell-cafe] Re: non-total operator precedence order (was:Fractional/negative fixity?)

Jan-Willem Maessen jmaessen at alum.mit.edu
Fri Nov 10 14:05:10 EST 2006


On Nov 9, 2006, at 7:16 PM, Benjamin Franksen wrote:

> Carl Witty wrote:
>
>> On Thu, 2006-11-09 at 22:20 +0100, Benjamin Franksen wrote:
>>> Henning Thielemann wrote:
>>>>  Maybe making fixity declarations like type class instance  
>>>> declarations
>>>>  is
>>>> good.
>>>
>>> I thought so too at first but, having thought about it for a  
>>> while, I now
>>> see this will cause /major/ problems. The precedence relations as
>>> declared explicitly by the programmer must form a DAG, with the  
>>> vertices
>>> being the operator classes with equal precedence. There are two  
>>> ways you
>>> can break the DAG: by introducing a 'smaller' or 'larger'  
>>> relation when
>>> another module has already declared them to have equal precedence  
>>> (resp.
>>> the other way around); or by introducing a cycle. Both can be caused
>>> simply by importing yet another module. I think it would be  
>>> unacceptable
>>> not to provide some way for the programmer to resolve such  
>>> conflicts.
>>
>> [ ... possibilities for resolving conflicts omitted ... ]
>>
>> Another possibility is:
>>
>> If you have operators op1 and op2, where the compiler sees  
>> conflicting
>> requirements for the precedence of op1 and op2, then they are  
>> treated as
>> non-associative relative to each other: the expression
>>   a op1 b op2 c
>> is illegal, and the programmer must instead write
>>   (a op1 b) op2 c
>> or
>>   a op1 (b op2 c)
>
> It's a possibility. However, I fear that such conflicting  
> precedences might
> not come in nice little isolated pairs. For instance, each operator  
> that is
> in the same precedence class as op1 (i.e. has been declared as  
> having equal
> precedence) will now be 'incompatible' with any that is in the same  
> class
> as op2, right?

Well, look at it from the perspective of the reader.  Does the reader  
of your code know beyond a shadow of a doubt what the intended  
precedence will be in these cases?  If not, there should be  
parentheses there---quite apart from what the parser may or may not  
permit you to do.  If the parser can't figure it out, you can bet  
your readers will have trouble as well.

> It gets worse if the conflict creates a cycle in a chain of
> large operator classes. Thus one single bad declaration can tear a  
> gaping
> hole into an otherwise perfectly nice and consistent DAG of precedence
> order relations, possibly invalidating a whole lot of code.

Requiring parenthesization solves these problems in a stroke.

-Jan-Willem Maessen
  who can't reliably parenthesize the C expression   a==b << 3&&4 | 17
  (yes, the horrific whitespace is deliberate!)


> Although one
> could view this as a bug in the offending module it makes me somewhat
> uneasy that one additional import can have such a drastic effect on  
> the
> code in a module /even if you don't use anything from that module/.
>
> Ben
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2425 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20061110/8649ea2a/smime.bin


More information about the Haskell-Cafe mailing list