Meta-interpreter for Prolog

From: Pascal Brisset <brisset_at_recherche.enac.fr>
Date: Fri 05 Oct 2001 04:31:54 PM GMT
Message-Id: <E15pXtC-0002Ix-00@mauve.recherche.enac.fr>
Sara Bernardini writes:
> [1  <text/plain; iso-8859-1 (quoted-printable)>]
> Hello,
> I am using ECLiPSe to construct a simple meta-interpreter for Prolog but I meet a problem when I try to write a clause to manage the cut.
> My simple interpreter is something like that:
> 
> solve(true):-!.
> solve((A,B)):-!,solve(A),solve(B).
> solve(not A):-!,not solve(A).
> solve(A):-clause(A,B),solve(B).
> 
> I would like adding a clause like   solve(!):-......  , but neither 
> 
> solve(!):-!.

 It cannot work because cut has a lexical scope in clauses and this scope
is lost when you evaluate them. You have to handle it manually. The following
solution manage it with an extra argument to implement the correct scope
(a cut affect the control from the entrance in the current clause):

---8<----------------------------------------------------------------
:- import get_cut/1, cut_to/1 from sepia_kernel.

solve(true, _CurrentCut).
solve((A,B), CurrentCut) :- solve(A, CurrentCut), solve(B, CurrentCut).
solve(not A, CurrentCut):- not solve(A, CurrentCut).
solve(!, CurrentCut) :- cut_to(CurrentCut).
solve(A, _CurrentCut) :-
 get_cut(Cut),
 myclause(A :- B),
 solve(B, Cut).

solve(Goal) :-
  get_cut(Cut),
  solve(Goal, Cut).
---8<----------------------------------------------------------------

 I do not know if cut_to/1 and get_cut/1 are well documented. They are
quite dangerous to use. Some typing would improve things slightly and
it may be simulated dynamically:

my_get_cut(level(L)) :- get_cut(L).
my_cut_to(level(L)) :- cut_to(L).
my_cut_to(_) :- error(5, "This is not a level").

With the following program

---8<----------------------------------------------------------------
myclause(p(1, Y, Z) :- (q(Y), !, r(Z))).
myclause(p(2, 3, 3) :- true).
myclause(q(1) :- true).
myclause(q(2) :- true).
myclause(r(1) :- true).
myclause(r(2) :- true).
---8<----------------------------------------------------------------

we get the expected result (though it is not a proof of correctness :-):

[eclipse 2]: solve(p(X, Y, Z)).

X = 1
Y = 1
Z = 1
More (0.00s cpu) ? ;

X = 1
Y = 1
Z = 2
More (0.00s cpu) ? ;

No (0.00s cpu)



--Pascal
Received on Fri Oct 05 17:35:52 2001

This archive was generated by hypermail 2.1.8 : Wed 16 Nov 2005 06:07:10 PM GMT GMT