operatorsocamlevaluationlazy-sequencesppx

Is this possible to create an operator that evaluates argument left-to-right in OCaml


When you define an operator such as

let (++) a b = a :: b 

When you do

let v = foo a ++ bar b 

bar is evaluated before foo. A workaround is to use let expression, i.e.

let e1 = foo a in let e2 = bar b in e1 ++ e2

However, sometimes it'd be handy to define an operator such as it's always evaluated from left to right. Is there a way to do it in OCaml or with a ppx or with lazy ?


Solution

  • The evaluation order of function arguments is part of the semantics of the language (it is currently not specified in OCaml), you cannot change it.

    More often than not, whenever the order of evaluation of function argument starts to matter, it is a sign that there are too many loose global mutable states and that you want to tighten your grip on mutable states.

    For instance, if you need to enforce a strict order of mutation on a global state, the common solution is to use a state monad, or a more eager variant like

    type context (* all of your previous global state should fit here *)
    type 'a data_and_context = context * 'a
    type 'a m = context -> 'a data_and_context
    val (>>=): 'a m -> ('a -> 'b m) -> 'b m 
    

    This is essentially gives you the ability to define yourself how the state is evaluated in

    foo x >>= bar x