PreviousUpNext

5.3.12  Binary Operators

Some languages treat alphanumeric function names like sin as a totally separate category from infix operators like +. C, for example, will not let you define infix operators at all.

Mythryl regards the distinction between alpanumeric prefix function names and non-alpha infix function names as being a largely irrelevant matter of surface syntax. The Mythryl compiler resolves infix operators into exactly the same syntax tree form as prefix function names very early in processing and completely disregards the distinction thereafter.

Mythryl function names are prefix until specifically declared infix. Standard arithmetic operators like + are predefined as infix in the Mythryl standard library. If you wish to use any other function name as infix, you must declare it yourself.

Most of the default infix declarations may be found in src/lib/core/init/pervasive.pkg:

    infix  my 90  ** ;
    infix  my 80  * / % div & // ;
    infix  my 70  $ + - ~ | ^ ? \ ;
    infixr my 60  @ . ! << >> >>> in ;
    infix  my 50  > < >= <= == != =~ .. ;
    infix  my 40  := o ;
    infix  my 20  ==> ;
    infix  my 10  then ;

The decimal numbers give precedence, larger numbers binding more tightly.

Operators declared infix using the infix form are treated as left-associative; those declared using infixr as right-associative.

The Mythryl parser makes no inherent distinction between alphabetic and non-alphabetic function names, as the above div and then declarations attest.

There are two ways to assign a meaning to a binary operator.

One is to simply to assign it the same meaning as some other function:

    linux$ my

    eval:  fun divmod (i, j) = ((i / j), (i % j));

    eval:  infix my 80 /% ;

    eval:  /% = divmod;

    eval:  27 /% 6;
    (4, 3)

This is often the most practical approach, since you typically want to have a convenient vanilla name for the function anyhow, for passing naturally to functions like map.

The second way is to just directly define the operator using infix syntax. This is unquestionably the more elegant approach:

    linux$ my
    eval:  infix my 80  /% ;
    eval:  fun i /% j = ((i / j), (i % j));
    eval:  27 /% 6;
    (4, 3)

Comments and suggestions to: bugs@mythryl.org

PreviousUpNext