ocamlinfix-notationinfix-operator

What are the legal names for infix operators?


There seem to be a number of symbols that can act as infix operators; e.g.

What determines whether a function can act as an infix operator or not?


Solution

  • It's determined by the first symbol of the operator. From the manual:

    infix-symbol    ::= (= ∣  < ∣  > ∣  @ ∣  ^ ∣  | ∣  & ∣  + ∣  - ∣  * ∣  / ∣  $ ∣  %) { operator-char }  
        ∣    # { operator-char }+  
    
    prefix-symbol   ::= ! { operator-char }  
        ∣    (? ∣  ~) { operator-char }+  
    
    operator-char   ::= ! ∣  $ ∣  % ∣  & ∣  * ∣  + ∣  - ∣  . ∣  / ∣  : ∣  < ∣  = ∣  > ∣  ? ∣  @ ∣  ^ ∣  | ∣  ~
    

    So, for example, a custom operator defined like this:

    let (@?) a b = ...
    

    Would be considered an infix operator since it starts with @, while

    let (?@) a b = ...
    

    is a prefix operator because it starts with ?.

    There are some additions and exceptions to this, however, described in the manual here:

    Lastly, you might also want to consider precedence and associativity, which is listed in the table near the top, before the first section here