/* \begin{verbatim} * A prolog-like interpreter with cuts in prolog, 23-Oct-92. * - mosh@cse.iitb.ernet.in, Computer Sc, IIT, Bombay 76, INDIA. * GPL(C) Mohsin Ahmed, http://www.cs.albany.edu/~mosh * * head( QueryList:in, O:{proved,failed}:out, C:{cut,backtrack}:out ), * Always succeeds. It tells the caller whether the QueryList * was proved or not. It also tells whether there was a cut or not. * * If there was a cut fail, then you do not retry. * Here "(O,C) == (failed,backtrack)" means retry last query. */ head( [], proved, backtrack ):-!. head( [!|T], O, cut ):-!, head( T, O, _ ). head( [H|T], Ox, Cx ):- head( H, O, C ), (O,C) \= (failed,backtrack), tail( O, C, T, Ox, Cx ), (Ox,Cx) \= (failed,backtrack). head( F, proved, backtrack ):- fact( F ). head( H, O, C ):- rule( H , B ), head( B, O, C ), (O,C) \= (failed,backtrack). head( _, failed, backtrack ). /* tail( O:{proved,failed}:in, C:{cut,backtrack}:in, * T:List:in, Ox:{proved,failed}:out, Cx:{cut,backtrack}:out ). * * Proceed to prove T, only if earlier part was proved. * Preserve any cuts made earlier or now. */ tail( failed, C, _, failed, C ):- !. tail( _, cut, T, Ox, cut ):- !,head( T, Ox, _ ). tail( _, _, T, Ox, Cx ):- head( T, Ox, Cx ). /* Top Level read-eval loop, eval input until end_of_file */ go:- repeat, write('?= '), read( Q ), go( Q ), Q = end_of_file. go( help ):-!, write(' * "go" to start, EOF to exit '), nl, write(' * file(Filename)/show/clear/help'), nl, write(' * Fact: fact( FACT ). '), nl, write(' * eg. ?= fact( a ). '), nl, write(' * Query: [B1,..,BN]/B/!, '), nl, write(' * eg. ?= [a,b,c,!,e,f]. '), nl, write(' * eg. ?= a. '), nl, write(' * Rule: rule( HEAD, QUERY ). '), nl, write(' * eg. ?= rule(a,[b,!,fail]). '), nl. go( fact(F) ):- !, assertz( fact(F) ), write( 'Fact asserted: ' ), write( fact(F) ), nl. go( rule(H,B) ):- !, assertz( rule(H,B) ), write( 'Rule asserted: ' ), write( rule(H,B) ), nl. go( file(F) ):- see( F ), go, seen, !. go( show ):- fact( F ), write(' Fact: '), write( F ), nl, fail. go( show ):- rule( H,B ), write(' Rule: '), write( H ), write( ' if '), write( B ), nl, fail. go( show ):- !. go( clear ):- !, retractall( rule(_,_) ), retractall( fact(_) ). go( end_of_file ):-!, write( end_of_file ), nl. go( Q ):- head( Q, O, _ ), !, write(' Query: '), write( Q ), write(' '), write( O ), nl. ?- go( help ). /* Test program: fact( d ). fact( b ). rule( a , [b,!,c] ). rule( a , d ). */ /* \end{verbatim} */