| Title: Variable
Description: None
Copyright (c) 1999 Steven J. Metsker.
Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.
A variable is a named term that can unify with other
terms.
A variable has a name, such as "X" or "Person", and an
instantiation. When a variable unifies with a term, it
"instantiates" to it, taking the term as its value. The
instantiation of a variable may be another variable, or a
structure.
The scope of a variable is the rule in which it is
contained. For example, consider the member program:
member(X, [X | Rest]);
member(X, [Y | Rest]) :- member(X, Rest);
In this program, the variable "X" in the first rule is the
same variable both times it appears in the rule. However,
this variable is completely independent of the variable
named "X" in the second rule. Variables with the same name
in a rule are the same variable, but variables with the
same name in different rules are different variables. This
is another way of saying that a variable's scope is the
rule in which it appears.
To be more specific, the scope of a variable is the
dynamic rule in which the variable appears. Since
rules may execute recursively, dynamic rules each need an
independent copy of a defining rule's variables. In the
member program, for example, the second rule may prove
itself by reinvoking itself, with a (slightly) different
set of variable instantiations.
Consider the query member(c, [a, b, c]) . This
query will unify with the second rule, and try to prove the
second rule's tail, which will be member(c, [b,
c]) . This structure will try to prove itself, and it
too will unify with the second rule. At this point, the
proof of member(c, [a, b, c]) will be waiting
upon the proof of member(c, [b, c]). That is, the two
dynamic copies of the rule, will be in different states,
because of their variables. For example, the instantiation
of Rest in the first execution of the rule
will be [b, c], and the value of
Rest in the second rule will [c] .
Variables have a name and an instantiation which is unique
within a scope; each dynamic version of a rule has a unique
Scope.
author: Steven J. Metsker version: 1.0 |