Trivial Prolog in Java

01: package ca.draisey.free.tprolog;
02:
03:
04:
05:
06:
07: // my new implementation has a extra level of indirection.  The instantiation
08: // of the rule is handled by creating a new table of Variables and the
09: // VariTerm is merely and index into this table -- so the the database
10: // rule and the instantiated sentence are in fact identical and only the
11: // table of Variables corresponding to each variable in the rule is unique
12: // to the instance.
13:
14:
15:
16:
17:
18: // universally quantified variable term -- occurs only in the database and user-supplied query
19: //      -- the instantiation of a rule requires identical variable references in the rule in the database
20: //      to be unified when it is instantiated -- this is handled transparently here as a single
21: //      QuanTerm object is used for every occurance of that variable in the rule -- so when instantiated
22: //      there is likewise a single VariTerm object -- further unification is unnecessary
23: //      -- this requires the database be preprocessed in this manner!
24: final class QuanTerm extends VariTerm {
25:         QuanTerm( final String n, final int i )
26:         {
27:                 super( i );
28:                 name = n;
29:         }
30:
31:         // for pretty output
32:         final void listing( final int f, final Variables these, final java.io.PrintWriter p, final Monitor m )
33:         {
34:                 switch( f & 6 ) {
35:                 case 2:
36:                         p.print( name );
37:                         return;
38:                 case 4:
39:                         if( these.ruleBindings[ index ] == null ) {
40:                                 p.print( "_" + these.hashCode() + "_" + index );
41:                         }
42:                         else {
43:                                 these.ruleBindings[ index ].listing( f, these.instanceBindings[ index ], p, m );
44:                         }
45:                         return;                 
46:                 case 6:
47:                         if( these.ruleBindings[ index ] == null ) {
48:                                 p.print( name + " = _" + these.hashCode() + "_" + index );
49:                         }
50:                         else {
51:                                 these.ruleBindings[ index ].listing( f, these.instanceBindings[ index ], p, m );
52:                         }
53:                         return;
54:                 default:
55:                         if( these.ruleBindings[ index ] == null ) {
56:                                 p.print( "QuanTerm( " + name + " = _" + these.hashCode() + "_" + index + " )" );
57:                         }
58:                         else {
59:                                 p.print( "QuanTerm( " + name + "=" );
60:                                 these.ruleBindings[ index ].listing( f, these.instanceBindings[ index ], p, m );
61:                                 p.print( " )" );
62:                         }
63:                         return;
64:                 }
65:         }
66:
67:         // return for creating Variable Table
68:         final int instantiationIndex()
69:         {
70:                 return index;
71:         }
72:
73:         // the QuanTerm node structure
74:         private final String name;              // supplemental info for pretty print
75: }
76:
77: // fin