Trivial Prolog in Java

001: package ca.draisey.free.tprolog;
002:
003:
004:
005:
006:
007: // -- a sentence is either a QuantifiedSentence rule or a free Clause --
008: abstract class Sentence {
009:         /*
010:         // a goofy little PrintWriter functor
011:         //      -- for such a simple routine Java is very clumsy
012:         //      -- makes my yearn for the simplicity of Ruby
013:         //      return ( astring do | pout | listing( f, these, pout, m ); end )
014:         abstract class Expressor {
015:                 abstract void lambda( java.io.PrintWriter pout );
016:         }
017:         static String astring( final Expressor x )
018:         {
019:                 final java.io.StringWriter sout = new java.io.StringWriter();
020:                 final java.io.PrintWriter pout = new java.io.PrintWriter( sout, true );
021:                 x.lambda( pout );
022:                 pout.flush();
023:                 return sout.toString();
024:         }
025:         */
026:
027:         // for pretty output
028:         public final String toString()
029:         {
030:                 return tostring( new Monitor() );
031:         }
032:         // package only version
033:         final String tostring( final Monitor m )
034:         {
035:                 final java.io.StringWriter sout = new java.io.StringWriter();
036:                 final java.io.PrintWriter pout = new java.io.PrintWriter( sout, true );
037:                 listing( 11, null, pout, m );
038:                 pout.flush();
039:                 return sout.toString();
040:         }
041:
042:         // listing options:
043:         //     listing( 0       -- 0 00 0       -- verbose node
044:         //     listing( 1       -- 0 00 1       -- verbose node dequantified
045:         // listing bitmapped options:
046:         //     listing( 2       -- 0 01 0
047:         //     listing( 3       -- 0 01 1
048:         //     listing( 4       -- 0 10 0
049:         //     listing( 5       -- 0 10 1
050:         //     listing( 6       -- 0 11 0
051:         //     listing( 7       -- 0 11 1
052:         // unused listing options:
053:         //     listing( 8       -- 1 00 0       
054:         //     listing( 9       -- 1 00 1
055:         // listing bitmapped options:
056:         //     listing( 10      -- 1 01 0
057:         //     listing( 11      -- 1 01 1
058:         //     listing( 12      -- 1 10 0
059:         //     listing( 13      -- 1 10 1
060:         //     listing( 14      -- 1 11 0
061:         //     listing( 15      -- 1 11 1
062:         // low bit field:
063:         //                      -- x xx 0       -- show quantifiers
064:         //                      -- x xx 1       -- hide quantifiers
065:         // middle bit fields:
066:         //                      -- x 01 x       -- show variable name
067:         //                      -- x 10 x       -- show bound term
068:         //                      -- x 11 x       -- show name and bound term
069:         // high bit fields
070:         //                      -- 0 xx x       -- long Horn Clause syntax
071:         //                      -- 1 xx x       -- short Horn Clause syntax
072:         final void listing( final int f, final Variables these, final java.io.PrintWriter p )
073:         {
074:                 listing( f, these, p, new Monitor() );
075:         }       
076:         abstract void listing( int f, Variables these, java.io.PrintWriter p, Monitor m );
077:
078:         // for ultimate output
079:         final void listVariables( final Variables these, final java.io.PrintWriter p )
080:         {
081:                 listVariables( these, p, new Monitor() );
082:         }
083:         void listVariables( final Variables these, final java.io.PrintWriter p, final Monitor m ) {}
084:
085:         final java.util.Iterator iterListedVariables( final Variables these )
086:         {
087:                 return iterListedVariables( these, new Monitor() );
088:         }
089:         final java.util.Iterator iterListedVariables( final Variables these, final Monitor m )
090:         {
091:                 return new java.util.Iterator() {
092:                         Sentence car = Sentence.this;
093:                         public final boolean hasNext() { return car.hasNextQuantifier(); }
094:                         public final Object next()
095:                         {
096:                                 final java.io.StringWriter sout = new java.io.StringWriter();
097:                                 final java.io.PrintWriter pout = new java.io.PrintWriter( sout, true );
098:                                 car.listed( these, pout, m );
099:                                 pout.flush();
100:                                 car = car.nextQuantifier();
101:                                 return sout.toString();
102:                         }
103:                         public final void remove() { throw new UnsupportedOperationException(); }
104:                 };
105:         }
106:
107:         // protected private for ultimate output
108:         boolean hasNextQuantifier() { return false; }
109:         Sentence nextQuantifier() { throw new java.util.NoSuchElementException(); }
110:         void listed( final Variables these, final java.io.PrintWriter p, final Monitor m ) { throw new java.util.NoSuchElementException(); }
111:
112:         // creates a new table of unbound Variables
113:         // -- nonrecursize -- only run on the head of a rule
114:         Variables instantiateVariables() { return null; }
115:         // -- part of the instantiation process for stripping unwanted qualifiers
116:         abstract Clause dequantifyClause();
117:         Term dequantifyTerm() { throw new UnsupportedOperationException(); }
118:
119:         // not all terms need to implement this, so we supply a dummy function here
120:         String getkey()
121:         {
122:                 throw new UnsupportedOperationException(); // never executes!
123:         }
124: }
125:
126: // fin