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

128 views Asked by At

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 ?

2

There are 2 answers

3
octachron On BEST ANSWER

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
1
Butanium On

Another solution which might be less handy as it requires more refactoring is to use some lazy operator as suggested by @Null on the OCaml discord:

let (+++) a b = let lazy b = b in a + b
let (!!!) (lazy a) = a
let _ = !!! (lazy (printf "%d%!" 1;1)) +++ lazy (printf "%d%!" 2;2) +++ lazy (printf "%d%!" 3;3) +++ lazy (printf "%d%!" 4;4)
1234