Why doesn't `(or)` retain lazy evaluation when redefined?

#1

If we use the (or) operator as originally defined, it behaves like a normal short-circuiting operator as we would expect:

# true or (failwith "!");;
val - : bool = true

But if we redefine it, it loses its lazy evaluation of the second argument:

# let (or) option default = match option with
  | Some value -> value
  | None -> default;;
val ( or ) : 'a option -> 'a -> 'a = <fun>
# (Some 1) or (failwith "!");;
Exception: Failure "!".

Anyone know why?

0 Likes

#2

The compiler primitive %sequor is short-circuiting not the operators (or) or ||. For instance, with

external f: bool -> bool -> bool = "%sequor"
let (or) = (or)

the function f is short-circuiting, contrarily to the redefined operator (or).

0 Likes

#3

Makes sense. Thanks!

0 Likes