[Ur] Functional infix operators ?

Gabriel Riba griba2001 at gmail.com
Thu Feb 19 13:34:49 EST 2015


Adam Chlipala <adamc <at> csail.mit.edu> writes:

> 
> OK, I see.  By "functional," you meant "related to function application, 
> composition, etc.", not a general mechanism for introducing new infix 
> operators.
> 
> If you (or anyone else) want to experiment with tweaking src/urweb.grm 
> to add some new operators, I'll be happy to accept a patch embodying 
> tasteful choices. :)  (May also require adding to lib/ur/top.ur[s] new 
> definitions of conventionally named functions, to which the new 
> operators desugar.)
> 

Hello!

I've worked out a solution for the function application and composition
operators, after revising the mlton manuals for ml-lex and ml-yacc, and it
works.

I need your opinion to ensure if it is complete.

The operators in this trial are

(<|) : forward application (right associative), named FWDAPP in .lex and .grm
(|>) : reverse application (left associative), named REVAPP
(<<<) : right-to-left composition (right associative) (equivalent to ML's
(o)), named COMPOSE
(>>>) : left-to-right composition (right associative), named ANDTHEN

In the lexic document:

* I have added theese definitions, ordering those with common prefix as the
longer first.

---

In the grammar document:

* In the user
** I have defined a function "top_binop" after the "native_op" function to
refer to the "Top" library module with similar code.

* In the yacc declarations section

** I've added the op. names to the list of Terminals

** I've added the associativity commands, just after the DARROW op. (=>)
which separate patterns and expressions, for them to have the minimal
precedence.

   Function composition put after (binds tighter) function application.

* In the rules section
** I've added the operations to the "eexp" expresion production with the
other binary operations. This works now for normal expressions.

---

In the "top" library module "top.ur, top.urs":

Since the "compose" function was already there I didn't need to add anything
as "andThen" can be expressed in terms of compose with the arguments flipped.

(* ----- *)

Here are the diffs with version urweb-20150214.

#---------------

cat urweb.lex.diff
378a379,383
> <INITIAL> "<<<"       => (Tokens.COMPOSE (pos yypos, pos yypos + size
yytext));
> <INITIAL> ">>>"       => (Tokens.ANDTHEN (pos yypos, pos yypos + size
yytext));
> <INITIAL> "<|"        => (Tokens.FWDAPP (pos yypos, pos yypos + size yytext));
> <INITIAL> "|>"        => (Tokens.REVAPP (pos yypos, pos yypos + size yytext));
>

#---------------

cat urweb.grm.diff
218a219,226
> fun top_binop (oper, e1, e2, loc) =
>     let
>         val e = (EVar (["Top"], oper, Infer), loc)
>         val e = (EApp (e, e1), loc)
>     in
>         (EApp (e, e2), loc)
>     end
>
397a406
>  | FWDAPP | REVAPP | COMPOSE | ANDTHEN
556a566,569
> %left REVAPP
> %right FWDAPP
> %right COMPOSE
> %right ANDTHEN
1204a1218,1222
>        | eexp FWDAPP eexp               (EApp (eexp1, eexp2), s
(eexp1left, eexp2right))
>        | eexp REVAPP eexp               (EApp (eexp2, eexp1), s
(eexp1left, eexp2right))
>        | eexp COMPOSE eexp              (top_binop ("compose", eexp1,
eexp2, s (eexp1left, eexp2right)))
>        | eexp ANDTHEN eexp              (top_binop ("compose", eexp2,
eexp1, s (eexp1left, eexp2right)))
>

#---------------





More information about the Ur mailing list