1

I'm new to prolog. The idea of my project is to say "The room X is free if none is a guest of X, while X is taken if a family lives in X". I use a predicate

guest(FamilySurname,RoomTaken)

So this mean that a family has taken that room.

taken(X) :- guest(_,X).

So if a family lives in room X, then X is taken.

My problem is how could i say that the room X is free? I should use a kind of NOT, like:

free(X) :- "NOT EXIST" guest(_,X).

How could i translate that "NOT EXIST" in prolog? I have tried with ! but it doesn't work properly... maybe I'm placing it in the wrong way.

Sorry for my english.

2 Answers2

2

Check the following code:

taken(X, Y) :- guest(Y,X).
free(X) :- \+ guest(_,X).

I made the a little change to taken, now it shows you who is on that room.

guest(albert, 123).

?- taken(123, R).

R = albert.

And the free() predicate it's pretty straightforward, I used the negation operator, you can check How to negate in Prolog

xeitor
  • 368
  • 4
  • 15
0

The code in the first answer does not seem to be a solution, i.e. the query ?- free(X) will not give an answer, where as the query ?- free(foo) will be successful. One needs to consider query floundering in Prolog into account, i.e. all variables

Consider the following code in which two alternatives for presenting the free predicate.

 room(123).
 room(124).
 guest(albert, 123).

 taken(Room) :- guest(_, Room).

 free2(Room) :- room(Room),
                \+ taken(Room).

 free(Room) :- room(Room),
               forall(guest(_, RoomTaken),
                      RoomTaken \= Room). 
tphilipp
  • 441
  • 2
  • 7