1

Is anyone aware of a built-in or any other way that will only make prolog go to the next clause when input from the terminal is given?

So for instance, the one giving the query will have to hit the y button to go the next clause after the query has been matched.

  • You can build a meta-interpreter that does this with less pain than you probably expect. – Daniel Lyons Oct 02 '17 at 16:44
  • [Here's an article](https://www.cs.unm.edu/~luger/ai-final2/CH6_Three%20Meta-Intrepeters%20-%20Prolog%20in%20Prolog,%20EXSHELL,%20and%20a%20Planner.pdf) that discusses some variations on how to address this, even allowing the user to specify whether a clause is true or not. – Daniel Lyons Oct 02 '17 at 20:17

1 Answers1

0

This is the so-called vanilla meta-interpreter (I cribbed from this answer by Jan Burse):

solve(true) :- !.
solve((A,B)) :- !, solve(A), solve(B).
solve(H) :- clause(H,B), solve(B).

You can extend this to await some input by adding a predicate to ask for input before continuing:

confirm :- write('Continue? [y/n] '), flush_output, get_char(y).

Now replace the second clause with this:

solve((A,B)) :- !, solve(A), confirm, solve(B).

Edit: I asked how to add built-ins and got a response that you could just add this clause:

solve(T) :- call(T).
Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77
  • Thanks a lot! I only have one month of experience in prolog, so i have to figure out your code first. Anyhow, My proffesor told me it could also be done with read/1 what is your take on this? – Willem van der Spek Oct 05 '17 at 09:59
  • @WillemvanderSpek `read/1` will read an entire term, so the user will have to enter a period and newline, and you will have to tolerate Prolog's prompt. But try `read(y)` instead of `get_char(y)` and see what happens. – Daniel Lyons Oct 05 '17 at 15:12