sjm.engine
Class Variable
java.lang.Object
|
+--sjm.engine.Variable
- Direct Known Subclasses:
- Anonymous
- public class Variable
- extends java.lang.Object
- implements ArithmeticTerm, ComparisonTerm
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.
|
Constructor Summary |
Variable(java.lang.String name)
Create a variable with the given name. |
|
Method Summary |
Term |
copyForProof(AxiomSource ignored,
Scope scope)
Create a copy that uses the provided scope. |
java.lang.String |
definitionString()
Returns string representation of this variable, showing
both its name and its value. |
boolean |
equals(java.lang.Object o)
Returns true if the supplied object is an equivalent
variable. |
java.lang.Object |
eval()
Returns the value of this variable. |
boolean |
isList()
Returns true if this variable is uninstantiated, or if it
contains a list. |
java.lang.String |
listTailString()
Returns a string representation of this variable as the
tail of a list. |
java.lang.String |
toString()
Returns a string representation of this variable. |
void |
unbind()
Marks this variable as no longer having an instantiated
value. |
Unification |
unify(Structure s)
Instantiates this variable with the supplied structure, or
forwards the request to its instantiation if it already has
one. |
Unification |
unify(Term t)
Unifies this variable with the supplied term. |
Unification |
unify(Variable v)
Instantiates this variable with the supplied variable, or
forwards the request to its instantiation if it already has
one. |
Unification |
variables()
Returns a unification containing just this variable. |
| Methods inherited from class java.lang.Object |
clone,
finalize,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
name
public final java.lang.String name
instantiation
protected Term instantiation
Variable
public Variable(java.lang.String name)
- Create a variable with the given name.
copyForProof
public Term copyForProof(AxiomSource ignored,
Scope scope)
- Create a copy that uses the provided scope.
- Parameters:
AxiomSource - ignoredScope - the scope to use for variables in the
copy- Returns:
- a copy that uses the provided scope
definitionString
public java.lang.String definitionString()
- Returns string representation of this variable, showing
both its name and its value.
- Returns:
- a string representation of this variable, showing
both its name and its value.
equals
public boolean equals(java.lang.Object o)
- Returns true if the supplied object is an equivalent
variable.
- Overrides:
- equals in class java.lang.Object
- Parameters:
object - the object to compare- Returns:
- true, if the supplied object has the same
name, and it the two variables' instantiations
are equal
eval
public java.lang.Object eval()
- Returns the value of this variable.
- Returns:
- the value of this variable
- Throws:
- EvaluationException - if this variable's
value is undefined
isList
public boolean isList()
- Returns true if this variable is uninstantiated, or if it
contains a list.
- Tags copied from interface: Term
- Returns:
- true, if this term is a list
listTailString
public java.lang.String listTailString()
- Returns a string representation of this variable as the
tail of a list.
- Returns:
- a string representation of this variable as the
tail of a list
toString
public java.lang.String toString()
- Returns a string representation of this variable.
- Overrides:
- toString in class java.lang.Object
- Returns:
- a string representation of this variable
unbind
public void unbind()
- Marks this variable as no longer having an instantiated
value.
unify
public Unification unify(Structure s)
- Instantiates this variable with the supplied structure, or
forwards the request to its instantiation if it already has
one.
- Parameters:
Structure - a structure to unify with- Returns:
- a unification. If this variable is already
instantiated, the unification is the result of
unifying with the input structure. Otherwise, the
result is a new unification containing just this
variable, instantiated to the input structure.
unify
public Unification unify(Term t)
- Unifies this variable with the supplied term.
This method dispatches the unify request to either a
structure or a variable. The receiver will get a signature
match from this object as a Variable, not just a Term.
- Parameters:
Term - a term to unify with- Returns:
- the sum of the variables that bind to values to
make the unification work; Returns null if the
unification fails.
unify
public Unification unify(Variable v)
- Instantiates this variable with the supplied variable, or
forwards the request to its instantiation if it already has
one.
- Parameters:
Variable - a variable to unify with- Returns:
- the sum of the variables that bind to values to make
the unification work; Returns null if the
unification fails.
variables
public Unification variables()
- Returns a unification containing just this variable.
- Returns:
- a unification containing just this variable