I am trying to use Prolog to represent state of a room.
I have a set of rules and a set of facts, but sometimes some of the facts are not defined. For instance, temperature in a room can decrease due to either cooling or opening a window, but sometimes I do not have a window sensor.
% Rules
temperature_trend(decrease) :-
cooling(on).
temperature_trend(decrease) :-
window(open).
% Facts
cooling(off).
%window(close). % Unknown, I do not have a window sensor
% Main
main() :-
temperature_trend(decrease).
If I run this program I would get an undefined procedure
error. I can deal with
this by explicitly setting the window
status to "anything" with window(W).
(I programmatically prepare the Prolog
source, so this is quite easy).
Now the query temperature_trend(decrease)
would succeed because window(W)
would lead to window(open)
. However, in this
case I want to know that W = open
.
Is there a way to return the variable assignments for this fact? Or maybe am I approaching the problem in the wrong way?
Note that the
rule tree could be arbitrarily deep, for instance I could add a new rule next_temperature(lower) :- temperature_trend(decrease).
and I still
want to know that next_temperature(lower)
succeeds only by setting W = open
. Terms are also more complex
because they also have a time index (T = 232
).
Maybe one option would be to return a list of assignments, which would be empty if all facts were known.
window/1
as dynamic so thatwindow(X)
query will simply fail if the facts don't exist.:- dynamic(window/1).
– lurkerwindow(W).
as a fact, the interpreter will "assign"W = open
and the query would succeed. I want to know that such "assignment" happened, i.e., that the query would succeed by addingwindow(open).
to my facts. – Claudio