[Ur] Functional infix operators ?

Gabriel Riba griba2001 at gmail.com
Sun Feb 22 09:58:15 EST 2015


Sergey Mironov <grrwlf <at> gmail.com> writes:

> 
> I'd be happy to have a feature of calling 2-arg function [func a b]
> using (a`func`b) infix notation. I suppose, adding this syntax would
> be easier than full-scale infix operators support suggested by Ziv,
> and could be a starter point for the larger-scale work.
> 

A better alternative for backtick quoted paths, lexified as one entity it
can be treated as an operator with precedence and associativity, and it can
be intermixed with other complex expressions without the need for them to be
parenthesized.

So it works with this test program:

(* --- Test program *)

structure Test = struct
  structure Nested = struct
    fun myprod (x: int) (y: int) = x * y
  end
end

val test = (1 + 0 `plus` 1 + 1 `Test.Nested.myprod` 2 + 1) =
           (((1 + 0) `plus` (1 + 1)) `Test.Nested.myprod` (2 + 1))

fun main (): transaction page = let
          in return <xml><body>{[test]}</body></xml>
          end

(* --- Mods needed *)

To achieve this the following insertions are needed:

* In urweb.lex :

** In the definitions section:

backtick_path = `([A-Z][A-Za-z0-9_]*\.)*[a-z_][A-Za-z0-9_']*`;

** In the rules section:

<INITIAL> {backtick_path}  => (Tokens.BACKTICK_PATH (
                               substring (yytext, 1, size yytext -2),
                               pos yypos, pos yypos + size yytext));

* In urweb.grm :

** In the declarations section

*** To the terminals production (%term)

 | BACKTICK_PATH of string

*** To the operators associativity commands, before (%left ANDALSO)

  %left BACKTICK_PATH

*** To the "eexp" production, with the other binary ops. cases

       | eexp BACKTICK_PATH eexp      (let

                val path = String.tokens (fn ch => ch = #".") BACKTICK_PATH
                val pathModules = List.take (path, (length path -1))
                val pathOp = List.last path
                    
                val e = (EVar (pathModules, pathOp, Infer)
                        , s (BACKTICK_PATHleft, BACKTICK_PATHright))
                val e = (EApp (e, eexp1), s (eexp1left, BACKTICK_PATHright))
                in
                   (EApp (e, eexp2), s (eexp1left, eexp2right))
                end)





More information about the Ur mailing list