Robin Andrews
Robin Andrews

Reputation: 3794

Prolog Procedure Does not Exist

I've copied the code from a text book for a simple UI in Prolog, as below, or on this URL: https://swish.swi-prolog.org/p/QgEReeXy.pl

/* Weather knowledge base*/
weather(good):-
    temp(high),
    humidity(dry),
    sky(sunny).

weather(bad):-
    (humidity(wet);
    temp(low);
    sky(cloudy)).

/* interface */
go:-
    write('Is the temperature high or low?'),
    read(Temp), nl,
    write('Is the sky sunny or cloudy?'),
    read(Sky), nl,
    write('Is the humidity dry or wet?'),
    read(Humidity), nl,
    assert(temp(Temp)),
    assert(sky(Sky)),
    assert(humidity(Humidity)),
    weather(Weather),
    write('The weather is '), write(Weather),
    retractall(temp(_)),
    retractall(sky(_)),
    retractall(humidity(_)).

When I run go. I get

procedure `temp(A)' does not exist
Reachable from:
      weather(A)
      go

Is this due to a small typo, or is there a bigger problem with the code please?

Upvotes: 1

Views: 2481

Answers (1)

Paulo Moura
Paulo Moura

Reputation: 18663

An alternative version of your code that doesn't require the use of dynamic predicates:

go :-
    write('Is the temperature high or low? '),
    read(Temparature),
    write('Is the sky sunny or cloudy? '),
    read(Sky),
    write('Is the humidity dry or wet? '),
    read(Humidity),
    once(weather(Temparature, Sky, Humidity, Weather)),
    nl, write('The weather is '), write(Weather).

weather(high, sunny, dry, good).
weather(low, _, _, bad).
weather(_, cloudy, _, bad).
weather(_, _, wet, bad).

Sample calls:

| ?- go.

Is the temperature high or low? high.
Is the sky sunny or cloudy? sunny.
Is the humidity dry or wet? dry.

The weather is good
yes

| ?- go.   

Is the temperature high or low? low.
Is the sky sunny or cloudy? sunny.
Is the humidity dry or wet? wet.

The weather is bad
yes

Upvotes: 2

Related Questions