prolog project
The sample program I have written in Prolog is designed to answer yes or no questions about patrol membership and leadership. Prolog is a logical programming language, so it is designed to understand facts and use those facts with rules to answer logical questions. In the case of our program it will answer four questions:
1) Is this scout in this patrol?
2) Is this scout a patrol leader?
3) Are two scouts in the same patrol?
4) Is this scout the patrol leader of this other scout?
Let's first understand how to run this program and then we'll look at the code and assign you some coding!
1) Is this scout in this patrol?
2) Is this scout a patrol leader?
3) Are two scouts in the same patrol?
4) Is this scout the patrol leader of this other scout?
Let's first understand how to run this program and then we'll look at the code and assign you some coding!
The Code
We need a program to run, so either download the file below, or copy this code into your favorite text editor and save it as patrol.pl - I use notepad++.
inPatrol(barracuda,mark).
inPatrol(barracuda,ben).
inPatrol(barracuda,reed).
inPatrol(dragon,kevin).
inPatrol(dragon,chris).
inPatrol(dragon,zach).
patrolLeader(ben).
patrolLeader(kevin).
samePatrol(Scout1,Scout2) :- inPatrol(Patrol,Scout1), inPatrol(Patrol,Scout2), Scout1 \= Scout2.
leads(Leader,Scout) :- patrolLeader(Leader), samePatrol(Leader,Scout).
inPatrol(barracuda,mark).
inPatrol(barracuda,ben).
inPatrol(barracuda,reed).
inPatrol(dragon,kevin).
inPatrol(dragon,chris).
inPatrol(dragon,zach).
patrolLeader(ben).
patrolLeader(kevin).
samePatrol(Scout1,Scout2) :- inPatrol(Patrol,Scout1), inPatrol(Patrol,Scout2), Scout1 \= Scout2.
leads(Leader,Scout) :- patrolLeader(Leader), samePatrol(Leader,Scout).
patrol.pl | |
File Size: | 0 kb |
File Type: | pl |
Notes on Prolog
There are a few key things we need to know about Prolog:
1) There are two types in Prolog, variables and defined values.
- If a words starts with a capital letter, it is considered a variable and can be assigned any value (ex: Scout1 in code above).
- If a word starts with a lower case letter, then it is considered a defined value (ex: mark in code above).
* So mark is a defined value, in this case a scout's name, but Mark would be a variable that could be anything.
2) When getting results from SWI-Prolog, hit ; or spacebar if there are multiple answers, or hit enter to stop displaying other answers.
1) There are two types in Prolog, variables and defined values.
- If a words starts with a capital letter, it is considered a variable and can be assigned any value (ex: Scout1 in code above).
- If a word starts with a lower case letter, then it is considered a defined value (ex: mark in code above).
* So mark is a defined value, in this case a scout's name, but Mark would be a variable that could be anything.
2) When getting results from SWI-Prolog, hit ; or spacebar if there are multiple answers, or hit enter to stop displaying other answers.
how to run our prolog program
Find and run SWI-Prolog. I installed my version using the default location, so I will find it here.
This is what it should look like when you run SWI-Prolog.
To load your program, use File -> Consult.
Find your Prolog file and open it.
When you load your Prolog file it should look similar to this.
Now let's ask it a simple question:
Is reed in the barracuda patrol?
To do this we ask Prolog to check for the fact:
inPatrol(barracuda,reed).
Note: Order is important! It will never find inPatrol(reed,barracuda) because it will look for a scout named barracuda in the reed patrol.
You can do this same thing for patrolLeader(ben).
Is reed in the barracuda patrol?
To do this we ask Prolog to check for the fact:
inPatrol(barracuda,reed).
Note: Order is important! It will never find inPatrol(reed,barracuda) because it will look for a scout named barracuda in the reed patrol.
You can do this same thing for patrolLeader(ben).
Now let's ask the question:
Is ben in the same patrol as mark?
Is mark in the same patrol as zach?
To do this we use:
samePatrol(Scout1,Scout2) where Scout1 and Scout2 are scout names.
We see that ben and mark are in the same patrol. But we find that mark and zach are not.
Because of the way I coded this rule, order does not matter here. So samePatrol(mark,ben) would also return true. Try it!
Is ben in the same patrol as mark?
Is mark in the same patrol as zach?
To do this we use:
samePatrol(Scout1,Scout2) where Scout1 and Scout2 are scout names.
We see that ben and mark are in the same patrol. But we find that mark and zach are not.
Because of the way I coded this rule, order does not matter here. So samePatrol(mark,ben) would also return true. Try it!
Now let's ask the question:
Is ben the patrol leader for mark?
We ask Prolog this question by:
leads(ben,mark).
Since ben is a patrol leader, and ben and mark are in the same patrol, Prolog tells us this is true.
However, once again order is important for this rule. If we ask the question the other way around:
Is mark the patrol leader for ben?
We find that this is not true (because mark is not a patrol leader).
Is ben the patrol leader for mark?
We ask Prolog this question by:
leads(ben,mark).
Since ben is a patrol leader, and ben and mark are in the same patrol, Prolog tells us this is true.
However, once again order is important for this rule. If we ask the question the other way around:
Is mark the patrol leader for ben?
We find that this is not true (because mark is not a patrol leader).
what is this code doing?
Let's look at some of the facts:
inPatrol(barracuda,mark).
inPatrol(dragon,kevin).
These two statements let Prolog know that mark is in the barracuda patrol and that kevin is in the dragon patrol. Pretty simple right?
But if we write them in the opposite order:
inPatrol(mark,barracuda).
This tells Prolog that scout barracuda is in the mark patrol. This is not what we want, be careful about order!
Let's look at the other facts:
patrolLeader(ben).
patrolLeader(kevin).
These statements tell Prolog that ben is a patrol leader and that kevin is a patrol leader too.
Now let's look at the first rule:
samePatrol(Scout1,Scout2) :- inPatrol(Patrol,Scout1), inPatrol(Patrol,Scout2), Scout1 \= Scout2.
When we ask Prolog a question like "Is ben in the same patrol as mark?" it will use this rule to determine the answer. In this case we would enter samePatrol(ben,mark). into the Prolog interpreter and it would assign ben to Scout1 and mark to Scout2. It would then look at the facts above and try to find a fact that matches inPatrol(Patrol,ben) where Patrol is a variable that could be anything. In this case, Prolog will find that Patrol can only be barracuda. Then it will look at the facts and try to find one that matches inPatrol(barracuda,mark). Since this fact exists (and is therefore true), Prolog will return true.
Let's look at the second rule:
leads(Leader,Scout) :- patrolLeader(Leader), samePatrol(Leader,Scout).
This rule looks similar to the first, but checks these against different facts and rules. So if we ask the question "Is ben the patrol leader for mark?", we would enter leads(ben,mark). into the interpreter. Prolog would assign ben to Leader and mark to Scout. It would then check our facts to see if there is one that matches patrolLeader(ben). Since this fact exists, Prolog then checks if samePatrol(ben,mark). Since this is a rule and not a fact, it must perform the same calculations described above for the first rule. The samePatrol question will return true, and since patrolLeader(ben) already returned true, Prolog will return true for the leads question.
inPatrol(barracuda,mark).
inPatrol(dragon,kevin).
These two statements let Prolog know that mark is in the barracuda patrol and that kevin is in the dragon patrol. Pretty simple right?
But if we write them in the opposite order:
inPatrol(mark,barracuda).
This tells Prolog that scout barracuda is in the mark patrol. This is not what we want, be careful about order!
Let's look at the other facts:
patrolLeader(ben).
patrolLeader(kevin).
These statements tell Prolog that ben is a patrol leader and that kevin is a patrol leader too.
Now let's look at the first rule:
samePatrol(Scout1,Scout2) :- inPatrol(Patrol,Scout1), inPatrol(Patrol,Scout2), Scout1 \= Scout2.
When we ask Prolog a question like "Is ben in the same patrol as mark?" it will use this rule to determine the answer. In this case we would enter samePatrol(ben,mark). into the Prolog interpreter and it would assign ben to Scout1 and mark to Scout2. It would then look at the facts above and try to find a fact that matches inPatrol(Patrol,ben) where Patrol is a variable that could be anything. In this case, Prolog will find that Patrol can only be barracuda. Then it will look at the facts and try to find one that matches inPatrol(barracuda,mark). Since this fact exists (and is therefore true), Prolog will return true.
Let's look at the second rule:
leads(Leader,Scout) :- patrolLeader(Leader), samePatrol(Leader,Scout).
This rule looks similar to the first, but checks these against different facts and rules. So if we ask the question "Is ben the patrol leader for mark?", we would enter leads(ben,mark). into the interpreter. Prolog would assign ben to Leader and mark to Scout. It would then check our facts to see if there is one that matches patrolLeader(ben). Since this fact exists, Prolog then checks if samePatrol(ben,mark). Since this is a rule and not a fact, it must perform the same calculations described above for the first rule. The samePatrol question will return true, and since patrolLeader(ben) already returned true, Prolog will return true for the leads question.