0% found this document useful (0 votes)
27 views5 pages

Prolog Handout

This document provides instructions for Assignment 3 in CSCI1410. It explains how to complete the assignment in Prolog by developing a knowledge base of courses and prerequisites, and how to represent and query the knowledge base.

Uploaded by

Mr Power
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

Prolog Handout

This document provides instructions for Assignment 3 in CSCI1410. It explains how to complete the assignment in Prolog by developing a knowledge base of courses and prerequisites, and how to represent and query the knowledge base.

Uploaded by

Mr Power
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CSCI1410 Fall 2021

Assignment 3: Knowledge Representation and


Reasoning

Code Submission 1 Due Thursday, October 7 at 11:59pm ET


Code Submission 2 Due Saturday, October 9 at 11:59pm ET
Code Due Tuesday, October 12 at 11:59pm ET
Writeup Due Wednesday, October 13 at 11:59pm ET
Late Day Deadline Friday, October 15 at 11:59pm ET

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!

2.1 Loading a Knowledge Base


To use Prolog, open up the terminal in a department machine and navigate to
the directory where your knowledge base curriculum.pl is located. Type:

$ swipl

This should open up a prompt. Now, you can load your knowledge base as
follows:

1
?- [curriculum].

Or, if you’re using Windows, type:

?- make.

You should re-compile the knowledge base every time you make a change.

In order to exit SWI-Prolog, you can do the following:

?- 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.

2.3 Facts and Rules Syntax


Every fact, rule and query ends with a dot. We have provided you with a sample
knowledge base, example.pl. You can play around with it and pass it some
queries. The sample knowledge base is as follows:

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 %.

2.6 Querying the Knowledge Base


Once you’ve loaded the knowledge base, you can query it. Assume that the
knowledge base contains two facts:

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).

Prolog then returns:


?- X = elon

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.

You might also like