(chibi assert)

A nice assert macro.

Assert macros are common in Scheme, in particular being helpful for domain checks at the beginning of a procedure to catch errors as early as possible. Compared to statically typed languages this has the advantages that the assertions are optional, and that they are not limited by the type system. SRFI 145 provides the related notion of assumptions, but the motivation there is to provide hints to optimizing compilers, and these are not required to actually signal an error.

(assert expr [msg ...])

Equivalent to SRFI 145 assume except that an error is guaranteed to be raised if expr is false. Conceptually shorthand for

(or expr (error "assertion failed" msg ...))

that is, evaluates expr and returns it if true, but raises an exception otherwise. The error is augmented to include the text of the failed expr. If no additional msg arguments are provided then expr is scanned for free variables in non-operator positions to report values from, e.g. in

(let ((x 3)) (assert (= x (+ x 1))))

the error would also report the bound value of x. This uses the technique from Oleg Kiselyov's good assert macro, which is convenient but fallible. It is thus best to keep the body of the assertion simple, moving any predicates you need to external utilities, or provide an explicit msg.

(assert test)
(assert test msg ...)