or is a short-circuiting logical disjunction special form on Boolean expressions.
Syntax
(or ⟨boolean-expr⟩*)
Semantics
- Every
⟨boolean-expr⟩must have typeBoolean, and theorexpression has typeBoolean. - Arguments are evaluated left-to-right:
- As soon as an argument evaluates to
True,orimmediately returnsTruewithout evaluating any remaining arguments. - If all arguments evaluate to
False,orreturnsFalse. - With zero arguments,
(or)evaluates toFalse(the identity element of disjunction).
- As soon as an argument evaluates to
- The last argument of a non-empty
orform is in tail position when theorexpression is in tail position (including forrectail calls).
Examples
(coalton-toplevel
(declare out-of-bounds? (UFix * UFix -> Boolean))
(define (out-of-bounds? index len)
(or (== len 0)
(>= index len))))
(coalton (Tuple (out-of-bounds? 2 5) (out-of-bounds? 5 5)))
; => #.(TUPLE COMMON-LISP:NIL COMMON-LISP:T)
Other notes
- Unlike Common Lisp’s
cl:or, which accepts arbitrary types and returns the first non-nilvalue, Coalton’sorrequires every operand to have typeBooleanand always returns aBoolean. - Because
oris a special form (to support short-circuiting), it cannot be passed as a first-class function value; useboolean-orwhen a function of type(Boolean * Boolean -> Boolean)is needed. - See
and.