The side-condition, hidden-side-condition, where, and where/hidden clauses behave as in the reduction-relation form.
Raises an exception recognized by exn:fail:redex? if no clauses match, if one of the clauses matches multiple ways (and that leads to different results for the different matches), or if the contract is violated.
Note that metafunctions are assumed to always return the same results for the same inputs, and their results are cached, unless caching-enabled? is set to #f. Accordingly, if a metafunction is called with the same inputs twice, then its body is only evaluated a single time.
As an example, these metafunctions finds the free variables in an expression in the lc-lang above:
|free-vars : e -> (x ...)|
|[(free-vars (e_1 e_2 ...))|
|(∪ (free-vars e_1) (free-vars e_2) ...)]|
|[(free-vars x) (x)]|
|[(free-vars (lambda (x ...) e))|
|(- (free-vars e) (x ...))])|
The first argument to define-metafunction is the grammar (defined above). Following that are three cases, one for each variation of expressions (e in lc-lang). The free variables of an application are the free variables of each of the subterms; the free variables of a variable is just the variable itself, and the free variables of a lambda expression are the free variables of the body, minus the bound parameters.
Here are the helper metafunctions used above.
|∪ : (x ...) ... -> (x ...)|
|[(∪ (x_1 ...) (x_2 ...) (x_3 ...) ...)|
|(∪ (x_1 ... x_2 ...) (x_3 ...) ...)]|
|[(∪ (x_1 ...))|
|- : (x ...) (x ...) -> (x ...)|
|[(- (x ...) ()) (x ...)]|
|[(- (x_1 ... x_2 x_3 ...) (x_2 x_4 ...))|
|(- (x_1 ... x_3 ...) (x_2 x_4 ...))|
|(side-condition (not (memq (term x_2) (term (x_3 ...)))))]|
|[(- (x_1 ...) (x_2 x_3 ...))|
|(- (x_1 ...) (x_3 ...))])|
For example, define-metafunction/extension may be used to extend the free-vars function above to the forms introduced by the language lc-num-lang.
|(define-metafunction/extension free-vars lc-num-lang|
|free-vars-num : e -> (x ...)|
|[(free-vars-num (+ e_1 e_2))|
|(∪ (free-vars-num e_1)|
Relations are like metafunctions in that they are called with arguments and return results (unlike in, say, prolog, where a relation definition would be able to synthesize some of the arguments based on the values of others).
Unlike metafunctions, relations check all possible ways to match each case, looking for a true result and if none of the clauses match, then the result is #f. If there are multiple expressions on the right-hand side of a relation, then all of them must be satisfied in order for that clause of the relation to be satisfied.
Note that relations are assumed to always return the same results for the same inputs, and their results are cached, unless caching-enable? is set to #f. Accordingly, if a relation is called with the same inputs twice, then its right-hand sides are evaluated only once.
|(current-traced-metafunctions) → (or/c 'all (listof symbol?))|
|(current-traced-metafunctions traced-metafunctions) → void?|
|traced-metafunctions : (or/c 'all (listof symbol?))|
Defaults to '().