Building Parsers with Java

sjm.engine
Class Anonymous

java.lang.Object
  |
  +--sjm.engine.Variable
        |
        +--sjm.engine.Anonymous

public class Anonymous
extends Variable

An anonymous variable unifies successfully with any other term, without binding to the term.

Anonymous variables are useful for screening out unwanted terms. For example, if a program describes a marriage in terms of an id, the husband, wife, and the beginning and end dates of the marriage, its facts might look something like:

     marriage(001, balthasar, grimelda, 14560512, 14880711);
     // ... 
     marriage(257, kevin, karla, 19790623, present); 
 
A rule that extracts just the husband from marriage facts is:
     husband(Id, Hub) :- marriage(Id, Hub, _, _, _);
 
The underscores in this rule represent anonymous variables. When the rule executes, it will unify its marriage structure with marriage facts, without regard to the last three terms of those facts.

Without anonymous variables, the husband rule would need three unused variables. Note that the following approach would fail:

     husband(Id, Hub) :- 
         marriage(Id, Hub, Anon, Anon, Anon); // wrong 
 
This approach, while tempting, will not work because the variable Anon will bind to the structures it encounters. Issued against the example program, Anon will first bind to grimelda. Next, Anon will attempt to bind to 14560512. This will fail, since Anon will already be bound to grimelda.

The essential behavior anonymous variables provide is that they unify successfully without binding.


Fields inherited from class sjm.engine.Variable
instantiation, name
 
Constructor Summary
Anonymous()
          Constructs an anonymous variable.
 
Method Summary
 Term copyForProof(AxiomSource ignored, Scope ignored2)
          Returns this anonymous variable, which does not unify with anything and thus does not need to copy itself.
 java.lang.Object eval()
          Return the value of this anonymous variable to use in functions; this is meaningless in logic programming, but the method returns the name of this variable.
 Unification unify(Structure ignored)
          Returns an empty unification.
 Unification unify(Term ignored)
          Returns an empty unification.
 Unification unify(Variable ignored)
          Returns an empty unification.
 Unification variables()
          Returns an empty unification.
 
Methods inherited from class sjm.engine.Variable
definitionString, equals, isList, listTailString, toString, unbind
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Anonymous

public Anonymous()
Constructs an anonymous variable.
Method Detail

copyForProof

public Term copyForProof(AxiomSource ignored,
                         Scope ignored2)
Returns this anonymous variable, which does not unify with anything and thus does not need to copy itself.
Overrides:
copyForProof in class Variable
Parameters:
AxiomSource - ignored
Scope - ignored
Returns:
this anonymous variable

eval

public java.lang.Object eval()
Return the value of this anonymous variable to use in functions; this is meaningless in logic programming, but the method returns the name of this variable.
Overrides:
eval in class Variable
Returns:
the name of the anonymous variable

unify

public Unification unify(Structure ignored)
Returns an empty unification.

The unify methods indicate failure by returning null. Anonymous variables succeed without binding, so they always return empty unifications.

Overrides:
unify in class Variable
Parameters:
Structure - ignored
Returns:
A successful, but empty, unification

unify

public Unification unify(Term ignored)
Returns an empty unification.
Overrides:
unify in class Variable
Parameters:
Term - ignored
Returns:
an empty unification

unify

public Unification unify(Variable ignored)
Returns an empty unification.
Overrides:
unify in class Variable
Parameters:
Variable - ignored
Returns:
an empty unification

variables

public Unification variables()
Returns an empty unification.
Overrides:
variables in class Variable
Returns:
an empty unification

by Steve Metsker