A rule which uses a bind-free hypothesis has similarities to both a
rule which uses a syntaxp hypothesis and to a :meta rule.
Bind-free is like syntaxp, in that it logically always
returns t but may affect the application of a :rewrite
or :linear rule when it is called at the top-level of a
hypothesis. It is like a :meta rule, in that it allows the
user to perform transformations of terms under progammatic control.

Note that a bind-free hypothesis does not, in general, deal with the
meaning or semantics or values of the terms, but rather with their
syntactic forms. Before attempting to write a rule which uses
bind-free, the user should be familiar with syntaxp and the
internal form that ACL2 uses for terms. This internal form is
similar to what the user sees, but there are subtle and important
differences. Trans can be used to view this internal form.

Just as for a syntaxp hypothesis, there are two types of
bind-free hypotheses. The simpler type of bind-free
hypothesis may be used as the nth hypothesis in a :rewrite
or :linear rule whose :corollary is
(implies (and hyp1 ... hypn ... hypk) (equiv lhs rhs)) provided term
is a term, term contains at least one variable, and every variable
occuring freely in term occurs freely in lhs or in some hypi,
i<n. In addition, term must not use any stobjs.
(Later below we will describe the second type, an extendedbind-free hypothesis, which may use state.)

We begin our description of bind-free by examining the
first example above in some detail.

We wish to write a rule which will cancel ``like'' addends from both
sides of an equality. Clearly, one could write a series of rules such
as

with one rule for each combination of positions the matching addends
might be found in (if one knew before-hand the maximum number of
addends that would appear in a sum). But there is a better way.
(In what follows, we assume the presence of an appropriate set of rules
for simplifying sums.)

When ACL2 attempts to apply cancel-matching-addends-equal to the
term under discussion, it first forms a substitution that instantiates
the left-hand side of the conclusion so that it is identical to the
target term. This substitution is kept track of by the substitution
alist:

ACL2 then attempts to relieve the hypotheses in the order they were
given. Ordinarily this means that we instantiate each hypothesis
with our substitution and then attempt to rewrite the resulting
instance to true. Thus, in order to relieve the first hypothesis,
we rewrite:

(RATIONALP (binary-+ '3
(binary-+ (expt a n) (foo a c)))).

Let us assume that the first two hypotheses rewrite to t. How
do we relieve the bind-free hypothesis? Just as for a syntaxp
hypothesis, ACL2 evaluates (find-match-in-plus-nests lhs rhs)
in an environment where lhs and rhs are instantiated as determined
by the substitution. In this case we evaluate

Observe that, just as in the case of a syntaxp hypothesis, we
substitute the quotation of the variables bindings into the term to be
evaluated. See syntaxp for the reasons for this. The result of this
evaluation, ((X . (EXPT A N))), is then used to extend the
substitution alist:

When this rule fires, it adds the negation of a common term
to both sides of the equality by selecting a binding for the
otherwise-free variable x, under programmatic control. Note
that other mechanisms such as the binding of free-variables
may also extend the substitution alist.

Just as for a syntaxp test, a bind-free form signals
failure by returning nil. However, while a syntaxp test
signals success by returning true, a bind-free form signals
success by returning an alist which is used to extend the current
substitution alist. Because of this use of the alist, there are
several restrictions on it -- in particular the alist must only
bind variables, these variables must not be already bound by the
substitution alist, and the variables must be bound to ACL2 terms.
If term returns an alist and the alist meets these restrictions,
we append the alist to the substitution alist and use the result as
the new current substitution alist. This new current
substitution alist is then used when we attempt to relieve the next
hypothesis or, if there are no more, instantiate the right hand side
of the rule.

There is also a second, optional, var-list argument to a bind-free
hypothesis. If provided, it must be either t or a list of variables. If
it is not provided, it defaults to t. If it is a list of variables, this
second argument is used to place a further restriction on the possible values
of the alist to be returned by term: any variables bound in the alist
must be present in the list of variables. We strongly recommend the use of
this list of variables, as it allows some consistency checks to be performed
at the time of the rule's admittance which are not possible otherwise.

An extended bind-free hypothesis is similar to the simple type
described above, but it uses two additional variables, mfc and state,
which must not be bound by the left hand side or an earlier hypothesis
of the rule. They must be the last two variables mentioned by term:
first mfc, then state. These two variables give access to
the functions mfc-xxx; see extended-metafunctions. As
described there, mfc is bound to the so-called
metafunction-context and state to ACL2's state. See bind-free-examples
for examples of the use of these extended bind-free hypotheses.