Trivial Prolog in Java

01: package ca.draisey.free.tprolog;
02:
03:
04:
05:
06:
07: // Prolog in Java Project
08: // This is a trivial implemetation of a extremely lightweight prolog interpreter written for
09: // purely expository purposes.  It is not intended to be used in a real environment.  It is written
10: // in Java rather than pseudocode so that it may be compiled to check its "superficial" correctness.
11: // I hope that it genuinely is a correct implentation -- that it follows from its overall simplicity.
12:
13:
14:
15:
16:
17: // This implementation has unusual control flow.  Successful inference through unification of terms
18: // is not followed by a return from the called subroutine/method, but rather causes the passed
19: // "Successor" routine to be called.  So the successful flow of a prolog programme is to consume
20: // stack space and never release it!  This is kind of like completions in Scheme with the
21: // nondeterminism of prolog added -- i.e. unlike ordinary completions which wrap-up their called
22: // environment and release stack space, successors "interrupt" normal control flow, leaving the
23: // possibility of returning to this environment -- exactly what is necessary for prolog backtracking.
24: //
25: //      success in prolog       ==      unbounded recursion in the java implementation
26: //      failure in prolog       ==      normal control flow in the java implementation
27: //
28: // Needless to say, this is a very expensive way of implementing the nondeterminism required in Prolog.
29:
30:
31:
32:
33:
34: final class Database extends ListableDatabase implements Consultable {
35:         // Consultable methods
36:         public void abolishDatabase()
37:         {
38:                 dictionary.clear();
39:         }
40:         public void appendzDatabase( final Sentence rulez )
41:         {
42:                 final String key = rulez.getkey();
43:                 if( dictionary.containsKey( key ) ) {
44:                         dictionary.get( key ).add( rulez );
45:                 }
46:                 else {
47:                         java.util.List< Sentence > matches = new java.util.LinkedList< Sentence >();
48:                         matches.add( rulez );
49:                         dictionary.put( key, matches );
50:                 }
51:         }
52:
53:         // view database methods
54:         // keys to dictionary entry object
55:     protected java.util.Set< String > dictionaryKeySet()
56:         {
57:                 return dictionary.keySet();
58:         }
59:
60:         // protected private method to access the container
61:         java.util.Iterator< Sentence > matching( final String key )
62:         {
63:                 final java.util.List< Sentence > matches = dictionary.get( key );
64:                 if ( matches != null ) {
65:                         return matches.listIterator();
66:                 }
67:                 else {
68:                         return NOTMATCHING;
69:                 }
70:                     
71:         }
72:         // instance map of the key to dictionary entry object
73:         private final java.util.Map< String, java.util.List< Sentence > > dictionary = new java.util.HashMap< String, java.util.List< Sentence > >();
74:
75: }
76:
77: // fin