AI Session 4 Slides
AI Session 4 Slides
Session 4 Slides
Introduction
Visit gprolog.org to download the Prolog
Use the second option with MinGW gcc Under MSys
Introduction
Prolog is a logical and a declarative programming
language.
The name itself, Prolog, is short for PROgramming in
LOGic.
Prolog is sometimes called a declarative language or a
rule-based language because its programs consist of a list
of facts and rules
Prolog is the major example of a fourth generation
programming language supporting the declarative
programming paradigm.
So Prolog is just a collection of facts and rules that we can
query on
Facts and rules will be stored in a knowledge base
Changing the Font
Changing the Font
You click on Terminal and Change Font if you feel the text are too
small and adjust the text to the Font size of your choice
You change the Directory where your notepad plus files are being
stored by Selecting File and click on change Dir
Accessing File in Notepad
To access the file you can go to the Prolog Console and make sure you
save the Notepad file in a Folder maybe on Desktop for easy access to
the file
Click File and click Consult then select the folder with the file and
select the file
Or you can just type consult (name of the notepad file).
Creating the first Program
On the Prolog console
We just want write a statement Hello welcome to Prolog
?- write(‘Hello, welcome to Prolog’).
Creating a Fact in Prolog
Things to note:
a fact indicates a statement that is true
Fact example.
Kanos is a teacher
teacher(kanos).
Names of relationships should begin with a lower case.
The relationship name appears as the first term
Objects appear as comma-seprated arguments within parethesis.
A full stop must terminate a fact, rule or query.
Object names appear in lower case
We can use Notepad and save the file with pl extension when creating
our facts
Creating a Fact and Rule in Prolog
Now we can come up with some rules that are based on the facts that
we created
We want to create a rule that will check if its possible to travel from
Harare to Bindura
So we want to check if we can travel from point A to point C.
So we name that travel a rule
travel(A,C) :- next_to(A,B), next_to(B,C).
Creating a Query in Prolog
? – [facts].
Now l can create the queries that l want
I can check if Bindura is next to Harare
?- next_to(bindura, harare)
Will get True
We can show all the fruits that exist
?- fruit(Y).
This will show all the fruits that exist
Lets say l want to check if grape is a type of fruit defined in our facts
This will give a false result because we don’t have grapes defined in
our facts.
Tasks
Represent the following facts in prolog,
James is a male
John is a male
Alice is a female
Martha is a female
Cross is a parent of James
Alice is a parent of James
What‟s Prolog?
You can represent facts in prolog, facts what is known
male(james).
male(john).
female(alice).
female(martha).
parent(cross, james).
parent(alice, james).
Queries
is john a male?
? male(john).
If john is a male, then true will be shown.
Is martha a male?
? male(martha).
Task 2
Kanos is fat
The dog is red
Thomas likes Jane
fat(kanos).
red(dog).
likes(thomas, jane).
Task 3
likes(jane,peter).
likes(peter,jane).
likes(lulu,cross).
Judith Java
Angel AI
Homework