Evaluation order

I just ported some code to OCaml and it was giving weird results. Not wrong just backwards. A quick inspection leads me to believe that (impure) code like Foo(x, y, z) -> Foo(r x, r y, r z) is being evaluated backwards, i.e. r z first.

Is there a switch or flag to control this? Or a preprocessor to make evaluation order explicit? I’d like evaluation order to be forwards for obvious reasons. I don’t want to mangle the code by hand because I’m planning on porting it to another language anyway.

No, that’s hard-coded. Evaluation can also be controlled with laziness, of course.

1 Like

Perhaps you need let rx = r x and ry = r y and rz = r z in Foo(rx,ry,rz).

1 Like

No. Evaluation order in OCaml is officially undefined, but de facto right-to-left. The only alternative is to let-bind your arguments to make evaluation order explicit.

Cheers,
Nicolas

2 Likes

I would swear it is right-to-left in bytecode only, and left-to-right in native code? Let’s google… Why are OCaml function arguments evaluated right-to-left?

No, evaluation order should still be right-to-left even in native-code. Having different results under bytecode and native-code is considered a bug.

Cheers,
Nicolas

1 Like

For my money the canonical source for why it’s de facto right to left is Xavier Leroy’s masters thesis.