0% found this document useful (0 votes)
12 views50 pages

Chapter 7 - Programming For AI

Uploaded by

d4rx5wsrw5
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)
12 views50 pages

Chapter 7 - Programming For AI

Uploaded by

d4rx5wsrw5
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/ 50

Department of Computer Sciences

College of Computers & Information Technology

501481-3 Summer 2022

Artificial Intelligence
Chapter 7: Programming for AI
Prolog Basics

1
What is Logic Programming
• Two types of programming languages:
– Imperative languages or procedural languages
(C, C++, VB, C#, Java, …).
– Declarative languages (prolog, lisp, …).

• Logic programming is a type of


programming called declarative
programming.
Imperative or Procedural Languages
• Programmer gives all the steps to solve a problem. He
must know an algorithm to solve the problem.
• Example: find the average of a list of grades:
• Input number; total=0;
• for (i=0; i<number; i++)
{ Input grade;
total=total+grade;
}
• Average = total/number;
• Print Average;
Declarative Languages
• Programmer describes the problem without the
control flow, then the system will solve the
problem.
• Programmer must know the relations between
objects to solve the problem.
• Programmer does not need to know an algorithm
to solve the problem.

• Declarative languages consists of:


– Program or theory.
– Computation which is deduction.
Logic
• Propositional logic:
– Consists of
• statements which have truth value (true or false).
• Compound statements by using connectives (and,
or, not, implies) with statements.
• E.g. (p and q) or (c implies d)
– p = “Ali is a teacher”
– q = “Ali is tall”
– c = “if Ali is an Engineer”
– d = “Ali is good in Math”
First Order Predicate logic
• Propositional logic: No argument in
propositions.
• First order predicate logic represents relation
between objects:
Predicate name
• own(ahmed, car).
• friend(ali, ahmed).
• father(sami, ali).
• First order predicate logic implies a predicate
symbol and set of connectives (and, or, not,
implies).
Prolog
• Prolog is a declarative language based on first
order logic.
• Prolog means Programming in Logic.
• It consists of:
– Facts.
Clause
– Rules.
– Goals.
• Used in AI: expert systems, games, ….
Atoms
• a sequence of alphanumeric characters
• usually starts with a lower case letter
• Or it is a string enclosed in single quotes
• They represent constants.
• Examples:
– ali, salem, a, b, c, a1, a2, b3, c5, …
– ‘Mr. Ali’, ’Dr. Ahmad’
Variables
• A sequence of alphanumeric characters

• Start with an uppercase letter

• Examples:
• X, Y, Z, Parent, Child, X1, Y1, X2, X5, X6,
….
Predicates
• A predicate has the form
– p(t1,...,tn)
– where p is an atom and t1,...,tn are variables or
atoms.
– n is the number of arguments of predicate p (written
as p/n which represents the signature of predicate).
– Predicates can be of 0-arg, 1-arg, 2-arg, …
– It can be either true or false.
• Examples:
– father(ali, ahmed).
Prolog Knowledge Base

• Prolog Program consist of:


– Facts: asserts a property to an object, or relation
between two or more objects:
• parent(ali, salem).
• own(ali, car).
– Rules: allow to infer a relationship or a property
based on a precondition:
• brother(X,Y) :- father(F,X), father(F,Y).
– Goal: Query or questions asked by user.
• parent(ali, Y).
Goals
• A goal is a conjunction of predicates
– p(X,Y), q(Y,Z), t(Z,W).

– A goal is the question or query asked by the


user. Prolog will try to find an answer for the
goal.
Answers
• Given a goal, Prolog searches for
answer(s):
– “yes” (possibly with answer substitution)
– “no” (if no answer or no more answer found).
– Substitutions are bindings of variables that
make the goal true.
PROLOG Examples

14
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?-

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
yes
?-

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
yes
?- playsAirGuitar(jody).

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
yes
?- playsAirGuitar(jody).
yes
?-

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
yes
?- playsAirGuitar(jody).
yes
?- playsAirGuitar(mia).
no
© Patrick Blackburn, Johan Bos & Kristina
Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- teacher(jody).

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- teacher(jody).
ERROR: predicate teacher/1 not defined.
?-

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- party.

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- party.
yes
?-

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- rockConcert.

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 2
happy(yolanda).
listensmusic(mia).
listensmusic(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listensmusic(mia).
playsAirGuitar(yolanda):- listensmusic(yolanda).

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 2
fact
happy(yolanda).
listensmusic(mia).
listensmusic(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listensmusic(mia).
playsAirGuitar(yolanda):- listensmusic(yolanda).

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 2
fact
happy(yolanda).
fact
listensmusic(mia).
listensmusic(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listensmusic(mia).
playsAirGuitar(yolanda):- listensmusic(yolanda).

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 2
fact
happy(yolanda).
fact
listensmusic(mia).
rule
listensmusic(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listensmusic(mia).
playsAirGuitar(yolanda):- listensmusic(yolanda).

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 2
fact
happy(yolanda).
fact
listensmusic(mia).
rule
listensmusic(yolanda):- happy(yolanda).
rule
playsAirGuitar(mia):- listensmusic(mia).
playsAirGuitar(yolanda):- listensmusic(yolanda).

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 2
fact
happy(yolanda).
fact
listensmusic(mia).
rule
listensmusic(yolanda):- happy(yolanda).
rule
playsAirGuitar(mia):- listensmusic(mia).
rule
playsAirGuitar(yolanda):- listensmusic(yolanda).

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 2
happy(yolanda).
listensmusic(mia).
listensmusic(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listensmusic(mia).
playsAirGuitar(yolanda):- listensmusic(yolanda).

head body

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 2
happy(yolanda).
listensmusic(mia).
listensmusic(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listensmusic(mia).
playsAirGuitar(yolanda):- listensmusic(yolanda).

?-

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 2
happy(yolanda).
listensmusic(mia).
listensmusic(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listensmusic(mia).
playsAirGuitar(yolanda):- listensmusic(yolanda).

?- playsAirGuitar(mia).
yes
?-

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 2
happy(yolanda).
listensmusic(mia).
listensmusic(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listensmusic(mia).
playsAirGuitar(yolanda):- listensmusic(yolanda).

?- playsAirGuitar(mia).
yes
?- playsAirGuitar(yolanda).
yes

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Clauses
happy(yolanda).
listensmusic(mia).
listensmusic(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listensmusic(mia).
playsAirGuitar(yolanda):- listensmusic(yolanda).

There are five clauses in this knowledge base:


two facts, and three rules.

The end of a clause is marked with a full stop.

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Predicates
happy(yolanda).
listensmusic(mia).
listensmusic(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listensmusic(mia).
playsAirGuitar(yolanda):- listensmusic(yolanda).

There are three predicates


in this knowledge base:
happy, listens2music, and playsAirGuitar

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 3
happy(vincent).
listensmusic(butch).
playsAirGuitar(vincent):- listensmusic(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listensmusic(butch).

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Expressing Conjunction
happy(vincent).
listensmusic(butch).
playsAirGuitar(vincent):- listensmusic(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listensmusic(butch).

The comma “," expresses conjunction in Prolog

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 3
happy(vincent).
listensmusic(butch).
playsAirGuitar(vincent):- listensmusic(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listensmusic(butch).

?- playsAirGuitar(vincent).
no
?-

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Knowledge Base 3
happy(vincent).
listensmusic(butch).
playsAirGuitar(vincent):- listensmusic(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listensmusic(butch).

?- playsAirGuitar(butch).
yes
?-

© Patrick Blackburn, Johan Bos & Kristina


Striegnitz
Prolog Program Example
like(ali, car). Predicate name

like(ahmed, car). Predicate arguments

father(salem, ali). Predicate End


father(salem, ahmed).
brother(X,Y) :- father(P,X), father(P,Y).
friend(X,Y) :- like(X,C), like(Y,C).
and

if
Examples

Represent the salem

relation father-of

represented by father-of

the following ali

Semantic Net
Is-a

ahmad

using Prolog Is-a

Is-a

male
Examples
salem

male(ali). father-of
male(ahmed). father-of

male(salem).
father(salem, ali). Is-a
ali

father(salem, ahmed). ahmad

brother(A,B) :- father(P,A), Is-a

father(P,B), A\==B, Is-a

male(A), male(B).
male

• ?- father(X,ali).
Examples
• ?- father(X,ali).
X = salem
male(ali).
?; male(ahmed).
no (or false) male(salem).
? father(salem, ali).
father(salem, ahmed).
brother(A,B) :- father(P,A),
father(P,B), A\==B,
male(A), male(B).
Examples male(ali).
male(ahmed).
male(salem).
• ?- father(X,ali). father(salem, ali).
X = salem father(salem, ahmed).
?; brother(A,B) :- father(P,A),
no father(P,B), A\==B,
? male(A), male(B).

• ?- father(X,Y), brother(Y,Z).
Examples male(ali).
male(ahmed).
male(salem).
• ?- father(X,ali). father(salem, ali).
X = salem father(salem, ahmed).
?; brother(A,B) :- father(P,A),
no father(P,B), A\==B,
? male(A), male(B).

• ?- father(X,Y), brother(Y,Z).

X = salem, Y = ali, Z = ahmed ? ;


X = salem, Y=ahmed, Z=ali ? ;
no
?
Prolog

• You can try prolog by downloading:


SWI Prolog interpreter
SWI Prolog can be downloaded from web site:
http://www.swi-prolog.org/download/
Test Your Knowledge
What is wrong with the following program:
Predicate Like starts with capital letter

Like(Ali, ball). Atom Ali starts with capital letter

like(ahmed, ball) Missing end of predicate

friend(X,Y) :- like(X,B), like(Y,B), X\==y.

variable starts with small letter

You might also like