4

I want to extend an toy functional programming language to accept Haskell-like infix operators. Here are the characteristics I am interested in:

  • Parser dynamically updates itself to accept newly defined operators.
  • Infix operators can be converted to functions surrounding them in parenthesis.
  • Functions can be converted to infix operators surrounding them in graves.
  • Function calls with Haskell syntax (optional parenthesis with space separated arguments).

Is there any existing library that implements it?

My current parser is written in Megaparsec so i will appreciate a solution compatible with it.

user3368561
  • 779
  • 6
  • 18
  • 3
    Haskell's designers made this tractable by using a separate alphabet for prefix functions (whose names are alphanumeric) and infix operators (whose names are symbolic). GHC implements associativity and precedence in the renaming step, ie, after parsing. So the parser never has to dynamically update itself – Benjamin Hodgson Jun 27 '18 at 17:45
  • 4
    See also [this report](http://www.cse.chalmers.se/~nad/publications/danielsson-norell-mixfix.pdf) on how Agda handles its "mixfix" operators. They build a "precedence graph" of operators, again doing all the hard work in the renaming phase – Benjamin Hodgson Jun 27 '18 at 17:50
  • 1
    You probably want to split up parsing into a sort-of-tokenisation step and a later step when you have available the ability to look up precedence and associativity you can use a more standard algorithm for expressions made out of operators. E.g. have a variant for expressions of `OperatorExpression Expression [(Operator, Expression)]`. If you want to support prefix and post script expressions as well then have fun. – Dan Robertson Jun 27 '18 at 17:55

0 Answers0