The Assert and AssertList Objects

The Assert and AssertList objects are the two objects most commonly used in clauses in functions. The left hand side is always a pattern match, the right-hand side is an expression built using the variables bound in the pattern match.

Clauses built with the Assert object

When the left hand side of an Assert is evaluated, it returns a list of variable bindings if the pattern match is successful. Otherwise, the clause evaluates to null. For example, suppose we have a function named Commute with a single clause:

and we execute the function Commute[x + y]. The pattern matching process will succeed in this case and will bind "x" to the variable "a" and "y" to the variable "b".

If we tried to execute Commute[x - y], then the pattern matching process would fail because the Sub object doesn't match the Add in the pattern. In this case, the clause would return null and the function would also return null if this is the only clause contained in the function.

If the pattern match succeeds, then the right side is built using the values bound to each variable.

Patterns that generate more than one set of bindings

The section on Pattern Matching to Monoids describes these patterns in detail.

Clauses that use the AssertList object

Clauses that are built using the AssertList object typically contain pattern match terms that can generate more than one list of bindings. When the AssertList object is used, the right-hand side of the AssertList object is evaluated for each binding lists generated by the pattern match term. Each time that right-hand side of the AssertList object generates an expression from a binding list, that expression is added to the List object that is returned by the clause. For example, the clause

generates the following list:

The AssertByValue object

The difference between AssertByValue object and the Assert object in that Assert is evaluated on a call-by-name basis while AssertByValue is evaluated on a call-by-value basis. So any variables that appear on the right-hand side of the AssertByValue that are already bound are replaced before the execution of the assert is processed. This is demonstrated in the following two clauses:

In the first expression, the expression involving a^2 is bound to the "parent" variable, then the binding to parent is used to construct the second term (the AssertByValue term). The variable "parent" is replace before the clause is evaluated, which is equivalent to evaluating the clause

This clause is then processed to give the result on the right-hand side.

An example of the use of the AssertByValue object might be if you wanted to pass the expression needed for the right hand side of a clause into a function.