com.svincent.moksa
Class Wam

java.lang.Object
  |
  +--com.svincent.moksa.WamObject
        |
        +--com.svincent.moksa.Wam

public class Wam
extends WamObject

An implementation of a Warren's Abstract Machine, as documented in Warren's Abstract Machine: a Tutorial Reconstruction, by Hassan Ait-Kaci.

Some variations may exist between my implementation and Hassan's description.

There are a number of instructions in Warren's machine that we don't implement here. In particular, since we are representing Prolog terms as Java compoundTerms, and have no heap, most of the instructions described in chapter 2 (up until section 2.3) of Hassan's book are unneccesary.

Most of them are replaced by new calls and calls to PrologTerm.unify.


Inner Class Summary
static class Wam.ChoicePoint
          Represents a choice point.
static class Wam.ExceptionHandler
          A registered exception handler for the current thread of execution.
 
Field Summary
 Continuation Fail
          A very common continuation
 
Constructor Summary
Wam(PrologEngine _engine)
           
 
Method Summary
 void asserta(Rule rule)
          Add a new alternative rule for the given name.
 void assertz(Rule rule)
          Add a new alternative rule for the given name.
 boolean badparm(int parmIdx, PrologTerm desiredValue)
          Called when facts are processing their parameters.
 Continuation findExceptionHandler(PrologTerm exception)
           
 Continuation getContinuation()
          Get the current continuation.
 PrologEngine getEngine()
           
 PrologFactory getFactory()
           
 Io getIo()
           
protected  Wam.ChoicePoint getLastChoicepoint()
          Retrieve the current trail pointer.
 PrologTerm getRegister(int idx)
          Get the value of register 'idx'.
 Rule getRule(java.lang.String name)
          Get the first alternative rule for the given name.
 long getTimestamp()
          Retrieve a timestamp.
protected  int getTrailPointer()
          Retrieve the current trail pointer.
 boolean invoke(PrologTerm _goal)
          Invoke a goal.
 Trailable popTrail()
           
 void retryMeElse(Rule next)
          We just backtracked to the current choicepoint.
 void setContinuation(Continuation _continuation)
          Set the value of continuation register.
 void setRegister(int idx, PrologTerm v)
          Set the value of register 'idx'.
 void trail(Trailable obj)
          Places this object on the trail stack, marking it to be undone when the trail is unwound.
 void trustMe()
          We just backtracked to the last choicepoint.
 void tryMeElse(Rule rule, int arity, Rule next)
          Allocates a new choice point frame on the stack setting its next clause field to 'next'.
protected  void unwindTrail()
          Unwinds the trail stack, which contains things that need to be undone when a choicepoint occurs.
 
Methods inherited from class com.svincent.moksa.WamObject
tag, tag
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

Fail

public Continuation Fail
A very common continuation
Constructor Detail

Wam

public Wam(PrologEngine _engine)
Method Detail

getEngine

public PrologEngine getEngine()

getFactory

public PrologFactory getFactory()

getIo

public Io getIo()

getTimestamp

public long getTimestamp()
Retrieve a timestamp. Note that this increments the timestamp.

getContinuation

public Continuation getContinuation()
Get the current continuation.

setContinuation

public void setContinuation(Continuation _continuation)
Set the value of continuation register.

getRegister

public PrologTerm getRegister(int idx)
Get the value of register 'idx'.

setRegister

public void setRegister(int idx,
                        PrologTerm v)
Set the value of register 'idx'.

badparm

public boolean badparm(int parmIdx,
                       PrologTerm desiredValue)
                throws PrologException
Called when facts are processing their parameters.
Returns:
true iff the parm is bad.

getRule

public Rule getRule(java.lang.String name)
Get the first alternative rule for the given name.

invoke

public boolean invoke(PrologTerm _goal)
               throws PrologException
Invoke a goal.

tryMeElse

public void tryMeElse(Rule rule,
                      int arity,
                      Rule next)

Allocates a new choice point frame on the stack setting its next clause field to 'next'.

This should be called before the first clause of any multi-clause rule. Subsequent clauses, except for the last clause, should call retryMeElse.

The last clause should call trustMe.

Parameters:
arity - the arity of the procedure offering alternatives. Needed so that the Wam knows how many argument registers to save.
next - a pointer to the next choicepoint continuation.
See Also:
retryMeElse(Rule), trustMe()

retryMeElse

public void retryMeElse(Rule next)

We just backtracked to the current choicepoint. Setup the next possible choice.

One way of thinking about this is: if there are N choicepoints (choice1..choiceN), then retryMeElse should be called before executing choice2 through choice(N-1).

Parameters:
arity - the arity of the procedure offering alternatives. Needed so that the Wam knows how many argument registers to save.
next - a pointer to the next choicepoint continuation.
See Also:
tryMeElse(Rule, int, Rule)

trustMe

public void trustMe()

We just backtracked to the last choicepoint. No more choices, so we've got to trust this fellow.

See Also:
tryMeElse(Rule, int, Rule)

getLastChoicepoint

protected Wam.ChoicePoint getLastChoicepoint()
Retrieve the current trail pointer. XXX empty?

getTrailPointer

protected int getTrailPointer()
Retrieve the current trail pointer.

trail

public void trail(Trailable obj)
Places this object on the trail stack, marking it to be undone when the trail is unwound.

unwindTrail

protected void unwindTrail()

Unwinds the trail stack, which contains things that need to be undone when a choicepoint occurs.

This version of the method unwinds the trail to the last ChoicePoint.


popTrail

public Trailable popTrail()

findExceptionHandler

public Continuation findExceptionHandler(PrologTerm exception)
                                  throws PrologException

asserta

public void asserta(Rule rule)
Add a new alternative rule for the given name.

assertz

public void assertz(Rule rule)
Add a new alternative rule for the given name.



This is documentation for Moksa Prolog, which can be found at http://www.svincent.com/moksa/

Copyright © 1999 Shawn P. Vincent.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.