Trivial Prolog in Java

01: package ca.draisey.free.tprolog;
02:
03:
04:
05:
06:
07: // atoms are formal constants or the names of formal functions or predicates
08: final class AtomTerm extends Term {
09:         AtomTerm( final String name )
10:         {
11:                 atom = name;
12:         }
13:
14:         // for pretty output
15:         final void listing( final int f, final Variables these, final java.io.PrintWriter p, final Monitor m )
16:         {
17:                 //System.out.print("a");
18:                 if( ( f & -2 ) == 0 ) {
19:                         p.print( "AtomTerm( " + atom + " )" );
20:                 }
21:                 else if( ! atom.equals( "null" ) ) {
22:                         p.print( atom );
23:                 }
24:         }
25:
26:         // a helper function for indexing the database
27:         final String getkey() {
28:                 return atom;
29:         }
30:
31:         // unifythrough -- by juggling terms unifythrough does dynamic dispatch on its Term argument --
32:         protected final void unifythrough( final Term that, final Variables these, final Variables those, final Successor success )
33:         {
34:                 that.unifytoAtom( this, those, these, success );
35:         }
36:
37:         // the meat of class AtomTerm
38:         protected final void unifytoAtom( final AtomTerm that, final Variables these, final Variables those, final Successor success )
39:         {
40:                 //System.out.print("A");
41:                 if( atom.equals( that.atom ) ) success.succeed();
42:         }
43:         // unifytoPair fails by default
44:         // all terms inherit the ability to bind an unbound variable from Term -- always succeeds --
45:
46:         // the AtomTerm node structure
47:         private final String atom;
48: }
49:
50: // fin