|
Language::Prolog::Interpreter - Prolog Interpreter alpha 0.02 |
Language::Prolog::Interpreter - Prolog Interpreter alpha 0.02
Language::Prolog::Interpreter->readFile('E:/src/PROLOG/flamenco.pr');
or
$a = <<'EOPROLOG';
parent(john,sally).
parent(john,joe).
parent(mary,joe).
parent(phil,beau).
parent(jane,john).
grandparent(X,Z) :-parent(X,Y),parent(Y,Z).
EOPROLOG
;
while ($a) {
eval 'Language::Prolog::Interpreter->readStatement(\$a)';
$@ && die $@,$a,"\n";
$a=~s/^\s*//;
}
# Above is same as
# eval 'Language::Prolog::Interpreter->readFile($pathtomyfile)';
$a = '?- grandparent(GPARENT,GCHILD).';
print $a,"\n";
$Q = Language::Prolog::Interpreter->readStatement(\$a);
while($Q->query()) {
print "found solutions\n";
print 'GPARENT = ',$Q->variableResult('GPARENT'),"\n";
print 'GCHILD = ',$Q->variableResult('GCHILD'),"\n\n";
}
print "no more solutions\n\n";
$a = 'member(A,[A|_]).';
$b = 'member(A,[_|B]) :- member(A,B).'; #Classic member
Language::Prolog::Interpreter->readStatement(\$a);
Language::Prolog::Interpreter->readStatement(\$b);
$a = '?- member(c(V),[a(a),b(b),c(c),d(d),c(q)]).';
print $a,"\n";
$Q = Language::Prolog::Interpreter->readStatement(\$a);
while($Q->query()) {
print "found solutions\n";
print 'V = ',$Q->variableResult('V'),"\n\n";
}
print "no more solutions\n\n";
A simple interpreter which doesn't allow infix operators (except for :- and ,, both of which are built in).
There are three possible statements:
.).
This gets added to the database.
.).
This gets added to the store.
?-, followed by a comma separated list of clauses, ending in a statement terminator (.).
This creates and returns a query.
/** ... **/.
Single-line/end-of-line comments are donnated by %.
Terms are:-
e.g [Term1,Term2]
e.g [Term1,Term2|Variable]
\w character class) starting with a lower case character.
e.g. this_Is_An_Atom
e.g. 'This is another atom!'
\w character class) starting with an upper case character or underscore
e.g. This_is_a_var, _and_this, _90
(, followed by a comma separated list of terms, terminating in a right bracket.
e.g clause(one), clause2(a,hello,'More !',[a,b,c])
:-, followed by optional whitespace, followed by a list of clauses separated by commas.
Jack Shirazi.
Since Mr Shirzai seems to have vanished, updated by Lee Goddard <lgoddard@cpan.org> to support file parsing, single- and multi-line comments, and multi-ilne clauses.
Copyright (C) 1995, Jack Shirazi. All Rights Reserved.
Updates Copyright (C) 2001, Lee Goddard. All Rights Reserved.
Usage is under the same terms as for Perl itself.
|
Language::Prolog::Interpreter - Prolog Interpreter alpha 0.02 |