Prolog Workshop 6: Rules
Task 1: Please read Chapter 2 of Mastering Prolog by Rob Lucas (also available on
Blackboard), working your way through the examples in Prolog.
Looking at the example rules in the chapter, you will be asked to find out if you¡¯re playing tennis. For this to work you need to ask for not (raining). The prolog interpreter used will fail because it does not know raining and it doesn¡¯t make the closed world assumption. There are two simple ways to define raining:
If it is raining:
raining.
If it is not raining:
raining:-false.
You should attempt questions 4, 6 and 7 in the Exercises at the end of Chapter 2.
Task 2: Dynamic predicate changes during runtime using assert/1 to add information to the Prolog database and/or retract/1 to remove information from the Prolog database.
The SWISH Prolog interpreter requires a declaration of the dynamic predicate on the left hand side of the screen for this to work:
:- dynamic likes/2.
A dynamic predicate likes/2 declared in the Prolog database. If you have this query
likes(nellie, X).
Then the system will not find the likes rule and it may give you
procedure `likes(A,B)’ does not exist
Since we declare the dynamic predicate likes/2, we can use assert to define that nellie likes apples and can then query for it:
assert(likes(nellie, apple)), likes(nellie,X).
then the answer will be apple. Since the prolog interpreter is running as an online service, this has to happen in the same query because every query is handled independently.
Exercises:
Task 3: You do not need to write any code for this task just try to find answers for the following questions:
Q1: Which teams will play the first game at round 16? Q2: Which teams will play the third game at round 16? Q3: Which teams will play the second game at round 4? Q4: Is fra playing g1 at round 2?
Q5: Is fra playing g2 at round 2?
Q6: List all of the possible games and rounds between fra and ger? Q7: When could ger and fra play?
Observation: The previous questions could be answered by writing a Prolog code Q1-Q3 could be answered if we write a code for the game rule.
game(group, X, r16,g1).
game(group, X,r16,g3).
game(r16, X, r4,g2).
Q4-Q5 could be answered if we write a code for the playing rule. playing(fra,r2,g1).
playing(fra,r2,g2).
Q6 could be answered if you write a code for the plays_helper rule. plays_helper(fra,ger,R,G).
Q7 could be answered if you write a code for the plays rule. plays(fra,ger,R,G).
When you have finished, please present your answers to the Demonstrator.