0% found this document useful (0 votes)
40 views29 pages

AI Session 4 Slides

The document discusses Prolog, including downloading and using Prolog, creating facts, rules and queries in Prolog. It provides examples of representing data as facts and rules, and executing queries based on the facts and rules. It also gives examples of representing data about teachers, students and modules as Prolog facts and rules.

Uploaded by

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

AI Session 4 Slides

The document discusses Prolog, including downloading and using Prolog, creating facts, rules and queries in Prolog. It provides examples of representing data as facts and rules, and executing queries based on the facts and rules. It also gives examples of representing data about teachers, students and modules as Prolog facts and rules.

Uploaded by

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

PROLOG

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

 Represent the following facts in prolog


Task 2

 fat(kanos).
 red(dog).
 likes(thomas, jane).
Task 3

 Jane likes peter


 peter likes jane
 Lulu likes cross
 Represent these facts in Prolog
Task 3

 likes(jane,peter).
 likes(peter,jane).
 likes(lulu,cross).

 We also want to check if these people are dating, so we


need to create a rule which stipulates that
 dating(X, Y) :- likes(X,Y), likes(Y,X).
 The comma stands for and and the ; stands for or
Task 3

 We also want to check if these people are friends and this


can happen if likes(X, Y) or likes(Y,X).
 friendship(X,Y) :- likes(X,Y); likes(X,Y).
Task 3: Queries

 Does lulu likes cross?


 is jane and peter dating?
Task 3: Queries

 Is lulu and cross dating or they are friends?


Homework

 You are given the following facts:

Teacher Module name


Kanos AI
Cross Java
Jane Maths
Suzan Geagraphy
Homework

 You are given the following facts:

Student Module name


Faith AI
Thomas Java
Judith Maths
Angel Geagraphy
Faith Java

Judith Java
Angel AI
Homework

 Represent these facts in prolog

 Also come up with the following rule:


 One teacher will guide a student if that student studies
that module on which the teacher teaches
Solution

 First come up with the facts from this problem statement


and represent the facts in Prolog Editor which is our
Notepad
 Notepad will act as our database or knowledge base with
facts and rules
 Things to note:
 Facts should begin with a lower case
 Variables should start with a capital letter
 Facts and rules are written in notepad
 Queries are executed in Prolog and are based on facts and
rules
Solution
Solution : Queries
 The first thing to make queries is to load the file in Prolog
 You select consult and the file name
 Who teaches ai?
 You use semicolon to check if there are more teachers who
teach ai
Solution : Queries
 Who teaches java?

 Faith learns which module?

 Which students learns java?

 Does Kanos guides student Thomas?

 Does Jane guides Judith?


Solution : Queries
Solution : Queries

You might also like