1

The following is a simple meta-interpreter, as seen in many guides/textbooks.

prove(true) :- !.
prove((A,B)):- !, prove(A), prove(B).
prove(H) :- clause(H,B), prove(B),
   write(H), write(" <- "), writeln(B).

The following is a simple program to test the meta-interpreter with.

% parent facts
parent(john, jane).
parent(john, james).
parent(sally, jane).
parent(martha, sally).
parent(deirdre, martha).

% ancestor recursive definition
ancestor(X,Y) :- parent(X,Y), !.
ancestor(X,Y) :- parent(X,A), ancestor(A,Y).

The meta-interpreter works for programs that are simple facts. It also works for simple deduction. However it fails when the program uses a cut.

?- prove(ancestor(martha, jane)).

parent(martha,sally) <- true
parent(sally,jane) <- true
No permission to access private_procedure `!/0'
In:
   [6] clause(!,_692)
   [5] prove(!) at  line 20
   [3] prove(ancestor(sally,jane)) at  line 20
   [1] prove(ancestor(martha,jane)) at  line 20

Question: Is there a simple way to handle cuts in the programs that the meta-interpreter is trying to run?

I know how to extend the meta-interpreter to handle additional syntax, eg the following adds the ability handle \+ thanks to the answer here: negation \+ and vanilla meta-interpreter

prove(true) :- !.
prove((A,B)):- !, prove(A), prove(B).
prove(\+ A) :- !, \+ prove(A). %%% handling \+
prove(H) :- clause(H,B), prove(B),
   write(H), write(" <- "), writeln(B).
false
  • 10,264
  • 13
  • 101
  • 209
Penelope
  • 291
  • 6
  • 1
    [Kind of](https://stackoverflow.com/a/27255452/772868) simple. – false Dec 21 '22 at 08:20
  • And yes, there is also a way to interpret cut with cut alone, but it gets extremely complex. – false Dec 21 '22 at 08:23
  • And a remark on `write/1` and `writeln/1`: Rather use `writeq/1` and `nl` in stead. – false Dec 21 '22 at 08:24
  • That definition of `ancestor` is terribly unintuitive. "X is the ancestor of A" - that's backwards. X and Y have no meaning, so why use them instead of variables that have intuitive meaning? Here's a better definition: https://danielschlegel.org/wp/teaching/prolog-ancestor-example/ – brebs Dec 21 '22 at 09:13
  • hi @false I tried googling for reasons why writeq and nl is better but can't find any. I have seen websites suggests to avoid write/1 but without saying why. Can you give a hint? – Penelope Dec 21 '22 at 14:08
  • write/1's output cannot be read back for atoms requiring quoting. – false Dec 23 '22 at 21:32

0 Answers0