0% found this document useful (0 votes)
2 views67 pages

Lab Manual AL

The document is a lab manual on Logic Programming, specifically focusing on Prolog, a declarative programming language based on Horn clause logic. It explains the basic concepts of Prolog, including defining relations through facts and rules, and how to use Prolog for knowledge-rich tasks in artificial intelligence. Additionally, it provides examples of knowledge bases and queries to demonstrate the functionality of Prolog.

Uploaded by

yoseftirusew1194
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)
2 views67 pages

Lab Manual AL

The document is a lab manual on Logic Programming, specifically focusing on Prolog, a declarative programming language based on Horn clause logic. It explains the basic concepts of Prolog, including defining relations through facts and rules, and how to use Prolog for knowledge-rich tasks in artificial intelligence. Additionally, it provides examples of knowledge bases and queries to demonstrate the functionality of Prolog.

Uploaded by

yoseftirusew1194
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/ 67

Lab Manual

Logic Programming
• Prolog is the only successful example of the family
of logic programming languages
• A Prolog program is a theory written in a subset of
first-order logic, called Horn clause logic
• Prolog is declarative.
• A Prolog programmer concentrates on what the
program needs to do, not on how to do it
• It will look and reason, using available facts and
rules, and then tells us an answer (or answers).
• The other major language for Artificial Intelligence
programming is LISP, which is a functional (or
applicative) language.
Basic idea of Prolog
• Programming with Logic
• Declarative
• Very different from other (procedural) programming
languages
• Good for knowledge-rich tasks
Procedre in logic programming
• Describe the situation of interest
• Ask a question
• Prolog logically deduces new facts about the situation
we described
• Prolog gives us its deductions back as answers
Consequences
• Think declaratively, not procedurally
– Challenging
– Requires a different mindset
• High-level language
– Not as efficient as, say, C
– Good for rapid prototyping
– Useful in many AI applications
SWI Prolog
• Freely available Prolog interpreter
• Works with
– Linux,
– Windows, or
– Mac OS
• There are many more Prolog interpreters
• Not all are ISO compliant
Defining Relations by Facts
• In Prolog, we write fact in the form
– predicate(atom1,….)
– Predicate is a name that we give to a relation
– An atom is a constant value, usually written in lower
case
E.g. parent( tom,bob).
• parent( pam, bob). parent( tom,bob). parent( tom,liz).
parent( bob, ann). parent( bob,pat). parent( pat,jim).
• A relation is a collection of facts. parent is the name of
a relation.
• A relation of arity n is a function from n-tuples
(elements of a Cartesian product) to {true, false}. (It
can also be considered a subset of the n-tuples.)
Defining Relations by Rules
• The offspring relation:
For all X and Y,
Y is an offspring of X if
X is a parent of Y
• This relation is defined by a rule, corresponding
to the Prolog clause
offspring( Y,X) :- parent( X,Y).
• Alternative reading:
For all X and Y,
if X is a parent of Y,
then Y is an offspring of X
Rules
• Rules are clauses.
• Facts are clauses
• A rule has a condition and a conclusion
• The conclusion of a Prolog rule is its head
• The condition of a Prolog rule is its body
• If the condition of a rule is true, then it follows
that its conclusion is true also
• Rule is in the form
– predicate(Var1,…):- predicate1(…),
predicate2(…), …
– Where Var1 is a variable, usually begins
with upper case
– Yes, it’s just a rewriting of
• HB1,B2,…Bn
– Fact is a rule that does not have the right
hand side.
How Prolog Rules are Used
• Prolog rules may be used to define relations
• The offspring relation is defined by the rule
offspring( Y,X) :- parent( X,Y):
– if (X,Y) is in the parent relation, then (Y,X) is
in the offspring relation
• When a goal of the form offspring( Y,X) is set
up, the goal succeeds if parent( X,Y) succeeds
• Procedurally, when a goal matches the head of a
rule, Prolog sets up its body as a new goal
Example

?-offspring(liz,tom).
• No fact matches this query
• The head of the clause
offspring( Y,X) :- parent( X,Y) does
• Y is replaced with liz, X is replaced with tom
• The instantiated body parent( tom,liz) is set up as
a new goal
• ?-parent( tom,liz) succeeds
• offspring( liz,tom) therefore succeeds too
More Family Relations
• female and male are defined extensionally,
i.e., by facts; mother and grandparent are
defined intensionally, I.e., by rules
• female(pam). … male(jim).
• mother( X,Y) :- parent( X,Y), female( X).
• grandparent( X,Z) :- parent( X,Y),
parent( Y,Z).
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

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

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

?- woman(mia).
yes
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
yes
?- playsAirGuitar(jody).
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
yes
?- playsAirGuitar(jody).
yes
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
yes
?- playsAirGuitar(jody).
yes
?- playsAirGuitar(mia).
no
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- tattoed(jody).
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- tattoed(jody).
no
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- tattoed(jody).
ERROR: predicate tattoed/1 not defined.
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

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

?- party.
yes
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- rockConcert.
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- rockConcert.
no
?-
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
happy(yolanda). fact
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
happy(yolanda). fact
listens2music(mia). fact
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
happy(yolanda). fact
listens2music(mia). fact
listens2music(yolanda):- happy(yolanda). rule
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
happy(yolanda). fact
listens2music(mia). fact
listens2music(yolanda):- happy(yolanda). rule
playsAirGuitar(mia):- listens2music(mia). rule
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
happy(yolanda). fact
listens2music(mia). fact
listens2music(yolanda):- happy(yolanda). rule
playsAirGuitar(mia):- listens2music(mia). rule
playsAirGuitar(yolanda):- listens2music(yolanda). rule
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

head body
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

?-
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

?- playsAirGuitar(mia).
yes
?-
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

?- playsAirGuitar(mia).
yes
?- playsAirGuitar(yolanda).
yes
Clauses
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(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.
Predicates
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

There are three predicates


in this knowledge base:
happy, listens2music, and playsAirGuitar
Knowledge Base 3
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
Expressing Conjunction
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).

The comma “," expresses conjunction in Prolog


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

?- playsAirGuitar(vincent).
no
?-
Knowledge Base 3
happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).

?- playsAirGuitar(butch).
yes
?-
Expressing Disjunction

happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).

happy(vincent).
listens2music(butch).
playsAirGuitar(vincent):- listens2music(vincent), happy(vincent).
playsAirGuitar(butch):- happy(butch); listens2music(butch).
Prolog and Logic
• Clearly Prolog has something to do with
logic
• Operators
– Implication :-
– Conjunction ,
– Disjunction ;
• Use of modus ponens
• Negation
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).
Prolog Variables
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).
Variable Instantiation
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).
X=mia
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).
X=mia;
Asking Alternatives (;)
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).
X=mia;
X=jody
Asking Alternatives(;)
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).
X=mia;
X=jody;
X=yolanda
Asking Alternatives
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).
X=mia;
X=jody;
X=yolanda;
no
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- loves(marsellus,X), woman(X).
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- loves(marsellus,X), woman(X).
X=mia
yes
?-
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- loves(pumpkin,X), woman(X).
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- loves(pumpkin,X), woman(X).
no
?-
Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

jealous(X,Y):- loves(X,Z), loves(Y,Z).


Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

jealous(X,Y):- loves(X,Z), loves(Y,Z).

?- jealous(marsellus,W).
Knowledge Base 5
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

jealous(X,Y):- loves(X,Z), loves(Y,Z).

?- jealous(marsellus,W).
W=vincent
?-
Prolog Syntax
• What exactly are facts, rules and queries built
out of?
Terms
Terms

Simple
Simple Terms
Terms Complex
Complex Terms
Terms

Constants
Constants Variables
Variables

Atoms
Atoms Numbers
Numbers
Atoms
• A sequence of characters of upper-case letters, lower-
case letters, digits, or underscore, starting with a
lowercase letter
• Examples: butch, big_kahuna_burger, playGuitar

• An arbitrary sequence of characters enclosed in


single quotes
• Examples: 'Vincent', 'Five dollar shake', '@$%'

• A sequence of special characters


• Examples: : , ; . :-
Numbers
• Integers: 12, -34, 22342
• Floats: 34573.3234

Variables
A sequence of characters of upper-case
letters, lower-case letters, digits, or
underscore, starting with either an uppercase
letter or an underscore
Examples:
X, Y, Variable, Vincent, _tag
Complex Terms
• Atoms, numbers and variables are building
blocks for complex terms
• Complex terms are built out of a functor
directly followed by a sequence of
arguments
• Arguments are put in round brackets,
separated by commas
• The functor must be an atom
Examples of complex terms
• Examples we have seen before:
– playsAirGuitar(jody)
– loves(vincent, mia)
– jealous(marsellus, W)

• Complex terms inside complex terms:


– hide(X,father(father(father(butch))))
Arity
• The number of arguments a complex term has is
called its arity

• Examples:

woman(mia) is a term with arity 1


loves(vincent,mia) has arity 2
father(father(butch)) arity 1
Arity is important
• In Prolog you can define two predicates
with the same functor but with different
arity
• Prolog would treat this as two different
predicates
• In Prolog documentation arity of a
predicate is usually indicated with the
suffix "/" followed by a number to indicate
the arity
Example of Arity
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

• This knowledge base defines


– happy/1
– listens2music/1
– playsAirGuitar/1
Summary of this lecture
• Simple examples of Prolog programs
• Introduced three basic constructs in Prolog:
– Facts
– Rules
– Queries
• Discussed other concepts, such as
– the role of logic
– unification with the help of variables
• Definition of Prolog constructs:
terms, atoms, and variables

You might also like