[Haskell-cafe] Fixity declaration extension

Ryan Ingram ryani.spam at gmail.com
Mon Aug 13 21:26:53 CEST 2012


When I was implementing a toy functional languages compiler I did away with
precedence declarations by number and instead allowed the programmer to
specify a partial order on declarations; this seems to be a much cleaner
solution and avoids arbitrary precedences between otherwise unrelated
operators defined in different modules.

You could write statements like

-- define + and - to have the same precedence
infixl + -

-- define * to have higher precedence than +
infixl * above +

-- define / to have the same precedence as *
infixr / equal *

-- $ is right-associative
infixr $
-- you can also separate precedence from fixity declaration
precedence $ below +

-- function application has higher precedence than all operators by
default, but you can override that
infixl . above APP

-- == is non-associative
infix ==

Here's some parses with this system:

a + b - c   =>   (a+b)-c
f.x.y z == g w  => (((f.x).y) z) == (g w)
a == b == c  => parse error (non-associative operator)
a * b / c => parse error (left-associative/right-associative operators with
same precedence)
a == b $ c => parse error (no ordering known between == and $)
a $ b + c => a $ (b+c)

I think this is a much cleaner way to solve the problem and I hope
something like it makes it into a future version of Haskell.

  -- ryan

On Sun, Aug 12, 2012 at 11:46 AM, Евгений Пермяков <permeakra at gmail.com>wrote:

>  fixity declaration has form *infix(l|r)? [Digit]* in haskell. I'm pretty
> sure, that this is not enough for complicated cases. Ideally, fixity
> declarations should have form *infix(l|r)? [Digit](\.(+|-)[Digit])** ,
> with implied infinitely long repeated (.0) tail. This will allow fine
> tuning of operator priorities and much easier priority selection. For
> example, it may be assumed, that bit operations like (.&.) operator have
> hightest priority and have priorities like 9.0.1 or 9.0.2, anti-lisps like
> ($) have lowest priority like 0.0.1, control operators have base priority
> 1.* and logic operations like (&&) have priority of 2.* and it will be
> possibly to add new operators between or above all (for example) control
> operators without moving fixity of other ones.
>
> Agda2 language supports wide priority range, but still without 'tails' to
> my knowledge. Is there any haskell-influenced language or experimental
> syntactic extension that address the issue?
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120813/5c015163/attachment.htm>


More information about the Haskell-Cafe mailing list