Prolog Handout
Prolog Handout
1 Introduction
In this assignment, we will represent the rules of courses and prerequisites in a
knowledge base, implemented in Prolog.
2 Prolog
Prolog is a programming language that has its roots in first order logic. In this
assignment, we will be using SWI-Prolog. Below, you’ll find all the infor-
mation you need to complete the assignment. Feel free to come back to
this section after you’ve finished reading section 3!
$ swipl
This should open up a prompt. Now, you can load your knowledge base as
follows:
1
?- [curriculum].
?- make.
You should re-compile the knowledge base every time you make a change.
?- halt.
2.2 Variables
In Prolog, variables start with an uppercase letter and constants with a low-
ercase letter. For example, in singing(mark), mark is a constant while in
singing(X), X is a variable. We can use is to assign a value to a variable. For
example,
isTwo(X):-
X is 2.
singing(elon).
playingGuitar(mark).
playingGuitar(satya).
singing(jeff).
playingGuitar(elon):-
happy(elon).
happy(mark):-
singing(mark),
playingGuitar(mark).
happy(satya):-
singing(satya);
playingGuitar(satya).
2
sad(satya):-
not(playingGuitar(satya)).
grumpy(jeff):-
singing(jeff)->
false
;
true.
2.3.1 If
In the above KB, the first four lines are facts and the rest are rules. Consider
the rule on the fifth line:
playingGuitar(elon):- happy(elon).
The above line can be read as elon plays the guitar if elon is happy. The :-
operator is used to denote ”if” or ”is implied by”.
2.3.2 And
Consider the next rule:
happy(mark):-
singing(mark),
playingGuitar(mark).
The above can be read as mark is happy if mark is singing and playing the
guitar. Thus, the , in Prolog is used to denote “and”.
2.3.3 Or
Consider the next rule:
happy(satya):-
singing(satya);
playingGuitar(satya).
The above can be read as satya is happy if satya is singing or playing the
guitar. Thus, the ; in Prolog is used to denote “or”.
2.3.4 Not
Consider the next rule:
3
sad(satya):-
not(playingGuitar(satya)).
The above can be read as satya is sad if satya is not playing the Guitar. Thus,
not(...) is used to denote negation.
2.3.5 If-then-else
Consider the last rule:
grumpy(jeff):-
singing(jeff)->
false
;
true.
This can be read as, je↵ is grumpy unless he’s singing. The expression before
the -> is the expression that is checked for truth; if it’s true, the next line is
executed. If it’s false, the line after the ; is executed.
2.4 Operators
Since Prolog is built on true and false statements, you might find Boolean
expressions useful! Prolog supports comparison operators, as illustrated in the
table below:
Syntax Explanation
<Term1> @> <Term2> True if Term1 is after Term2 in the standard
order of terms.
<Term1> @< <Term2> True if Term1 is before Term2 in the standard
order of terms.
<Term1> @=> <Term2> True if Term1 is after Term2 in the standard
order of terms, or both terms are equal.
<Term1> @=< <Term2> True if Term1 is before Term2 in the standard
order of terms, or both terms are equal.
<Term1> == <Term2> True if both terms are equal.
<Term1> \== <Term2> True if terms are not equal.
2.5 Commenting
To add comments in Prolog, you can use /* */ or %.
4
singing(elon).
singing(jeff).
Now, we can ask the knowledge base if a fact is true or false, as follows:
?- singing(elon).
?- true.
In this example, we’ve asked the knowledge base if a fact we’ve given it is true.
We can also query for information that the knowledge base contains, as follows:
?- singing(X).
You’ll notice that it’s only returned one answer, while we know that there are
more! To continue, simply type ; after the first answer rather than pressing
enter. Prolog will then return:
?- X = jeff
If you have any further questions about syntax, you can post on Piazza or come
to TA hours!
Note: Every fact, rule and query in SWI-Prolog must end with a dot. If you
forget the dot, you will run into errors!
3 Your Task
3.1 Knowledge Base
A knowledge base contains facts and rules. Your first task is to create a knowl-
edge base describing courses and their prerequisites. The following is a graph of
courses and prerequisites in the Computer Science department. Note that the
graph does not strictly follow the Brown CS curriculum, meaning the rules of
the graph are not necessarily the same as the Brown CS department’s rules.