You are on page 1of 45

Introduction to Artificial Intelligence

(INSY4091)

Lab - 1

1
Introduction to Programming
Paradigms
• How many programming languages do you know?
• Different programming paradigms:
– Procedural programming : emphasizes on procedures in terms of the underlying
machine model; focuses on how to achieve a particular goal
• C, C++, Java, Pascal
– Object oriented programming: more emphasis is on the data rather than the
procedure; the program is written as a collection of classes and objects which are
meant for communication
• C++, Java, Python, Ruby, C#
– Declarative programming: the main focus is to express the logic of a computation
without describing its control flow
• Functional programming : treats computation as the evaluation of
mathematical functions
– List, Python, Scala, F#
• Logical programming: program statements are expressed in terms of facts and
rules
– Prolog, datalog
• Prolog stands for programming in logic
• It is a logical programming language

2
Introduction to Programming
Paradigms…
• Logical programming is a computer programming paradigm in
which the program statements express the facts and rules about
different problems with in a system of formal logic
• It allows you to express what you want to compute, without
specifying how to compute it
• This makes it different from other programming paradigms where
you must provide explicit steps or algorithms for the computation
• In logical programming languages like prolog, you write facts and
rules about the problem domain, and the prolog interpreter uses
these facts and rulres to answer your queries
• This approach can make it easier to reason about your program and
its correctness

3
Introduction to Programming
Paradigms…
• For example, in prolog, you might write a rule that defines a parent-
child relationship
• Then you could ask prolog to find all individuals who are parents or
all individuals who are children, without having to write any
additional code for these queries
• In summary, logical programming enables you to declare WHAT the
program should accomplish leaving the details of HOW it
accomplishes up to the languages implementation
• There are different IDEs for prolog programming
• For this lab, you can use either GNU prolog or SWI prolog
• Next, we will go through the setup process of these IDEs

4
GNU prolog setup

5
GNU prolog setup …

6
GNU prolog setup …

7
GNU prolog setup …

8
GNU prolog setup …

9
GNU prolog setup …

10
GNU prolog setup …

11
GNU prolog setup …

12
GNU prolog setup …

13
SWI prolog setup
• Installing SWI Prolog on Windows:
• Follow the below steps to install SWI Prolog on Windows:
Step 1: Visit swi-prolog.org website using any web browser.

14
SWI prolog setup…
Step 2: Click on Download which is adjacent to Home, dropdown list
will appear then click on SWI-Prolog.

15
SWI prolog setup…
Step 3: New webpage will open, click on Stable release.

16
SWI prolog setup…
Step 4: After clicking on stable release new webpage will open which
will contain stable versions of prolog for different platforms. Under
binaries there are two stable releases for windows, first is SWI-
Prolog 9.0.4-1 for Microsoft Windows (64 bit) and the other is SWI-
Prolog 9.0.4-1 for Microsoft Windows (32 bit). Click on the one as
per your system configuration. Lets take the one for 64-bit
operating system.

17
SWI prolog setup…
Step 5. After clicking on SWI-Prolog 9.0.4-1 for Microsoft Windows (64
bit), a new webpage will open, check on I understand checkbox to
make the download link active. Then click on the download link,
downloading of the executable file will start shortly. It is a small
12.6 MB file that will hardly take a minute.

18
SWI prolog setup…
Step 6: Now check for the executable file in downloads in your system
and run it.

19
SWI prolog setup…
Step 7: It will prompt confirmation to make changes to your system.
Click on Yes.

20
SWI prolog setup…
Step 8: Setup screen will appear, click on Next.

21
SWI prolog setup…
Step 9: The next screen will be of License Agreement, click on I Agree.

22
SWI prolog setup…
Step 10: After it there will be screen of installing options so check the
box for Add swipl to the system path for all users, and also check
the box for create a desktop icon and then click on the Next button.

23
SWI prolog setup…
Step 11: The next screen will be of installing location so choose the
drive which will have sufficient memory space for installation. It
needed only a memory space of less than 50 MB.

24
SWI prolog setup…
Step 12: Next screen will be of choosing Start menu folder so don’t do
anything just click on Next Button.

25
SWI prolog setup…
Step 13: This last screen is of choosing components, all components
are already marked so don’t change anything just click on Install
button.

26
SWI prolog setup…
Step 14: After this installation process will start and will hardly take a
minute to complete the installation.

27
SWI prolog setup…
Step 15: Click on Finish after the installation process is complete.

28
SWI prolog setup…
Step 16: SWI Prolog is successfully installed on the system and an icon
is created on the desktop.

29
SWI prolog setup…
• Step 17: Run the software and see the interface.

30
GNU prolog vs SWI prolog
• Even though these two tools share many similarities, there are some
differences between them:
• According to information obtained from stack overflow, SWI prolog is slower
than GNU prolog
• But it is extremely robust, has a broad library support and has decent GUI
building systems
• On the other hand, GNU prolog is faster than SWI prolog but is less robust and
has fewer library support
• For instance, when you use SWI prolog it provides you a built-in text editor
where you enter the facts and rules of your program but when you use GNU
prolog you may not have built-in editor even though you can use any other
text editor
• In general, if you need a more robust and versatile prolog compiler with broad
library support and GUI programming support you may choose SWI prolog.
However, if you prioritize speed over other features GNU prolog might be a
better option
• In this lab you can use any one of these

31
Starting prolog, defining predicates
and getting queries answered
• Using any of the two, if you want to define some predicates without
having to first save them to file consult “user” as:
?- [user].
... type your predicates ...
^D

32
Starting prolog, defining predicates
and getting queries answered…

33
Starting prolog, defining predicates
and getting queries answered…
• However, it is a good idea to get in the habit of using files most of
the time, because what you type here will be lost as soon as you
exit Prolog
Using GNU Prolog
• Use a text editor like Notepad to crate the file where you enter the
facts and rules:

34
Starting prolog, defining predicates
and getting queries answered…
• Save the file as prolog file (using .pl file extension) under specific folder
• Click File, Change Dir…, select the directory(folder) that contains your file,
click Ok=> this will show you on the prompt that the directory has
changed
• Now, you can compile your program by placing the file name with in
square bracket followed by full stop at the prompt as
[example1].
• If this compilation is successful, it will show you something like:
... example1.pl compiled, 3 lines read - 464 bytes written, 0 ms
?-
• Now, you can submit your queries at the prompt and prolog will give you
the response
• If the code has some errors, you need to edit it and repeat the above
processes to compile and run the program.

35
Starting prolog, defining predicates
and getting queries answered…
Using SWI Prolog
• Start SWI prolog
• Click File, New, choose location, type file name(including .pl
extension), click save => this will open a second window which is a
text editor where you enter your program besides the first window:

36
Starting prolog, defining predicates
and getting queries answered…
• Now, you can arrange the two windows as shown above for ease
• The newly opened window (placed to the left) is the prolog text
editor
• Now, enter your facts and rules on the text editor
• Then click on Compile, Compile buffer
• Click on Ok. And it will be compiled and saved

37
Starting prolog, defining predicates
and getting queries answered…
• Now, in the query window (on the right), you can enter your query

• This query is asking the question ‘Is dog animal?’


• And prolog gives the response true.

38
Starting prolog, defining predicates
and getting queries answered…
• If you query asks the question ‘Is dog plant?’, prolog gives the
response false as follows

39
Starting prolog, defining predicates
and getting queries answered…
• If you want to ask prolog which objects are animals, you can write the
query ‘animal(X).’ at the prompt in the query window. X is a variable that
prolog tries to satisfy. If you press Enter key, prolog gives you a response
by satisfying the variable with the first answer as follows:

• If you want prolog to give you the next possible response, put ; next to
the current response

40
Basic Elements of Prolog Programming
• A prolog program basically consists of facts, rules and queries
• Facts and rules together comprise a knowledge base
• Facts and rules are predicates which represent relationships among
data objects
• A predicate consists of a head and a number of arguments
• It is a function which returns either true or false
Facts – declare things that are unconditionally true;
– Have a head and empty body
– Is a predicate that is true
– For example, if we say “Abebe is the father of Kebede”, then this is a fact
– This fact is represented in Prolog as:
father(abebe, kebede)

41
Basic Elements of Prolog
Programming…
• In this fact, father is the name of the relationship and abebe and
kebede are its arguments
• We can define facts as an explicit relationship between objects and
their properties
• So, facts are unconditionally true in nature
• Suppose we have some facts given below:
– Tom is a cat
– Hair is black
– Abebe is tall
• So, these are some facts that are unconditionally true.
• These are actually statements that we have to consider as true

42
Basic Elements of Prolog
Programming…
• Some guidelines when writing facts
1. Names of properties/relationships begin with lower case
letters
2. The relationship name appears as the first term
3. Objects apear as comma separated arguments within
parenthesis
4. A period “.” must end a fact
5. Objects also begin with lower case letters; they can also begin
with digits (like 1234), and can be string of characters
enclosded in quotes e.g. color(penink, ‘red’)

43
Basic Elements of Prolog
Programming…
Syntax
• The general syntax for facts is:
relation(object1, object2, …).

Example:

cat(tom).
black(hair).
tall(abebe).
male(abebe).
female(almaz).
mother(sara, dawit).

44
Basic Elements of Prolog
Programming…
Exercises:
1. Convert the following statements into prolog facts:
abebe is male
kebede is male
aster is female
sara is female
abebe is father of kebede
abebe is father of sara
aster is mother of kebede
aster is mother of sara
2. Create a prolog program with the above facts; compile the program;
query the program about who is male, who is female, …

45

You might also like