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:
A single clause ending in a statement terminator (.).
.
This gets added to the database.
A single rule ending in a statement terminator (.).
This gets added to the store.
The he query characters ?-, followed by a comma separated list of clauses, ending in a statement terminator (.).
?-
This creates and returns a query.
Multi-line comments are Java-like, taking the form /** ... **/.
/** ... **/
Single-line/end-of-line comments are donnated by %.
%
Whitespace is ignored everywhere except in single quoted atoms
Terms are:-
Comma separated lists of terms enclosed in square brackets
e.g [Term1,Term2]
As List1, but final term is a variable separated by a '|'
e.g [Term1,Term2|Variable]
sequence of characters/digits/underscore (i.e \w character class) starting with a lower case character.
\w
e.g. this_Is_An_Atom
any sequence of characters enclosed in single quotes (')
e.g. 'This is another atom!'
sequence of characters/digits/underscore (i.e \w character class) starting with an upper case character or underscore
e.g. This_is_a_var, _and_this, _90
an Atom1 immediately followed by a left bracket, (, followed by a comma separated list of terms, terminating in a right bracket.
(
e.g clause(one), clause2(a,hello,'More !',[a,b,c])
A Clause, followed by optional whitespace, followed by :-, 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.
2 POD Errors
The following errors were encountered while parsing the POD:
'=item' outside of any '=over'
You forgot a '=back' before '=head1'
To install Language::Prolog::Interpreter, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Language::Prolog::Interpreter
CPAN shell
perl -MCPAN -e shell install Language::Prolog::Interpreter
For more information on module installation, please visit the detailed CPAN module installation guide.