top | item 40593965

(no title)

LectronPusher | 1 year ago

I'm actually currently doing some research on operator precedence and syntax, so it's fun to see such a relevant post! One of my favorite syntaxes for custom operators comes from Agda and allows for easy definition of mixfix operators. Agda uses underscores to stand-in for operands in the name of arbirtrary functions (i.e. `_+_` as the sum). But when fully qualified with all underscores, this will act like a normal function (`_+_ 3 4 == 7`). But, uniquely among languages I've seen, it allows for partial application of arguments with operands in place, making it much easier to create new curried functions.

Here's an `if_then_else` example, modified from [0]:

    -- declare an operator function with underscores:
    if_then_else_ : {A : Set} -> Bool -> A -> A -> A
    -- define the function:
    if true then x else y = x
    if false then x else y = y
    -- call it:
    if_then_else_ cond first second
    -- or use it infix:
    if cond then first else second
    -- or partially apply "sections" of the operator:
    -- this is really cool
    (if_then first else second) cond
    (if cond then_else second) first
    if cond then first else_ second
    if cond then_else_ first second
    if_then first else_ cond second
    (if_then_else second) cond first
Of course, the paper by Agda authors Danielsson and Norell, Parsing Mixfix Operators [1], is a classic discussion at the theory of mixfix operators in expression grammars (they take a stance of precedence as a DAG). But I do not often see cited the earlier and similarly titled dissertation by Jacob Wieland, Parsing Mixfix Expressions [2], which goes further into defining classes of ambiguity in both operator expressions and overloaded operators, expresses these in an Earley style parser, and in my view is far more practical for real language designers.

[0] Agda Mixfix docs: https://agda.readthedocs.io/en/v2.6.4.3/language/mixfix-oper...

[1] Danielsson/Norell: https://scholar.google.com/scholar?q=parsing+mixfix+operator...

[2] Wieland: https://scholar.google.com/scholar?q=parsing+mixfix+expressi...

discuss

order

No comments yet.