Question

Assume that we have prolog knowledge base like this:

guilty(X) :-
    commits(X,Y),
    crime(Y).
crime(murder).
crime(theft)

When I ask this question:

?- guilty(john)

I want that Prolog asks me a question like that:

is commits(john, murder) ?

and I answer no then

is commits(john, theft) ?

if I answer yes Prolog says

**yes**

How can I make something like this?

Thanks..

Was it helpful?

Solution

You need a modified proof engine, that when encounters an unknown fact query the user about.

Doing it with some generality can be an interesting task, Google for metainterpreter Prolog, if you are interested in this argument, the first link provides you the valuable page A Couple of Meta-interpreters in Prolog by Markus Triska, where you can learn more.

For your question, would suffice a rule

commits(Person, Crime) :-
    crime(Crime),
    format('is ~w ?', [commits(Person, Crime)]),
    read(yes).

test:

?- guilty(john).
is commits(john,murder) ?no.
is commits(john,theft) ?yes.
true.

note that read/1 requires a dot to terminate the input.

OTHER TIPS

You want an 'interactive shell' for your little reasoner. Building one is not difficult, but beyond the scope of a stackoverflow question. This tutorial builds one in the 2nd or 3rd lesson and generally answers this question. It calls facts like your user answers 'working storage'.

http://www.amzi.com/ExpertSystemsInProlog/

Prolog "executes" things from left to right. Try:

guilty(X) :-
    crime(Y),
    commits(X,Y).
crime(murder).
crime(theft)

So then guilty(X) depends on commits(X,murder) and/or commits(X,theft)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top