1
votes

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?

1
You are probably missing :- dynamic humidity/1, temp/1, sky/1. from the top of your file. You need to forewarn Prolog that you're going to be messing around with the dynamic store of these predicates. - Daniel Lyons
@DanielLyons Daniel is correct. It works Click run in the lower right. - Guy Coder

1 Answers

2
votes

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