user1279812
user1279812

Reputation: 115

User input in prolog

I want to write a program that sets cold to true if the user enters winter and sets warm to true if the user enters summer. This is what I have so far:

start :- write('What season is it?: '), read(X), season(X).  
cold :- season(winter).  
warm :- season(summer).

However when I query start and enter winter for the season I get an error that season/1 is undefined. To fix this I tried changing my code to the following:

start :- season(X).  
season(X) :- write('What season is it?: '), read(X).  
cold :- season(winter).  
warm :- season(summer).  

Now when I query start it asks for the season as expected and I entered winter. I queried cold expecting a return of true since I figured season(winter) would be true, but instead I was again prompted to answer "what season is it?". How can I make this simple program work?

Upvotes: 1

Views: 10920

Answers (4)

user502187
user502187

Reputation:

It looks that what you want to do is kind of "forward chaining". So you want to be able to add a fact to the knowledge base, and then see additional facts pop up.

Such things were very fashionable in the 80's when expert systems emerged and can also be done with Prolog. Here is an article roughly describing the difference between "backward chaining" and "forward chaining":

Logic Programming Associates Ltd.
ProWeb: Expert System

I have tried to remodel something similar to the above forward chaining via the clause expansion mechanism that is available in many Prolog systems and via an agenda that is held in the knowledge base as ordinary facts. The expansion mechanism changes a rule of the following form:

P :- A

Into a rule where X is a fresh variable not occuring in P or A:

delta(X, P) :- A_new(X)

Where A_new is a condition that says that P is new when a new fact X has arrived. Lets consider your example:

?- ['delta.p'].
?- [user].
:- forward season/1.
:- forward cold/0.
:- forward warm/0.
cold :- season(winter).  
warm :- season(summer).
^D

These two Prolog rules would be turned into the following delta/2 rules via the rewriting. Since the rules are very simple, the delta/2 rules are also very simple:

?- listing(delta/2).
delta(X, cold) :-
    X = season(winter).
delta(X, warm) :-
    X = season(summer).

Here is an example session:

?- list.
Yes
?- volunteer(season(winter)).
Yes
?- list.
season(winter).
cold.
Yes

The rewriting in delta.p is very primitive. The rewriting can be enhanced to support rigid predicates and to support the dynamic removal of facts. Possible additional application areas of the "forward chaining" are then:

  • Natural Language Processing
  • Constraint Solving

P.S.: The predicate name volunteer/1 stems form Nexpert Object,
an expert system shell in use in the 80's and 90's.
NEXPERT OBJECT version 3.0, Jean-Marie Chauvet, Neuron Data, Inc.

P.P.S.: But you might want to lookup newer things, such as Rete-NT, or OWL:
http://answers.semanticweb.com/questions/3304/forward-vs-backward-chaining

Upvotes: 2

Dawit H.
Dawit H.

Reputation: 1

season(winter,cold).  
season(summer,warm).  
start:-write('What season is it?'),write(' '),read(X),season(X,Y),write(Y).  

The output will be look like this:
?- start.
What season is it? winter.
cold
true.

Upvotes: 0

Nick Barnes
Nick Barnes

Reputation: 21306

Because "cold/0" and "warm/0" take no arguments, it is impossible to change the result at runtime without altering their definition at runtime. The simplest way to do this is just to add or remove the facts "cold." and "warm." depending on user input, using assert/1 and retractall/1.

start :- write('What season is it?: '), read(X), season(X).
season(summer) :- retractall(cold), assert(warm).
season(winter) :- retractall(warm), assert(cold).

Upvotes: 1

Chetter Hummin
Chetter Hummin

Reputation: 6817

While defining the facts, you need to change the syntax a little bit.

start(Y) :- write('What season is it?: '), read(X), nl, season(X,Y).
season(winter,cold).
season(summer,warm).

Note that the user needs to end the input with a full stop. (eg. winter.)

Upvotes: 0

Related Questions