简体   繁体   中英

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, ie

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 ?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM