0% found this document useful (0 votes)
4 views

Unit 3 Prolog Programming-1

The document provides an introduction to Prolog, a logic programming language, covering its syntax, basic constructs like facts, rules, and queries, as well as its historical development. It emphasizes Prolog's declarative nature and its application in knowledge-rich tasks, particularly in artificial intelligence. Examples of Prolog programs and knowledge bases illustrate how facts and rules are used to derive conclusions and answer queries.

Uploaded by

Aditya Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 3 Prolog Programming-1

The document provides an introduction to Prolog, a logic programming language, covering its syntax, basic constructs like facts, rules, and queries, as well as its historical development. It emphasizes Prolog's declarative nature and its application in knowledge-rich tasks, particularly in artificial intelligence. Examples of Prolog programs and knowledge bases illustrate how facts and rules are used to derive conclusions and answer queries.

Uploaded by

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

Prolog

Logic Programming
SWI Prolog

• Freely available Prolog interpreter


• Works with
– Linux,
– Windows, or
– Mac OS
• There are many more Prolog
interpreters
• Not all are ISO compliant
Lecture 1

• Theory
– Introduction to Prolog
– Facts, Rules and Queries
– Prolog Syntax
Aim of this lecture
• Give some simple examples of Prolog
programs
• Discuss the three basic constructs in Prolog:
– Facts
– Rules
– Queries
• Introduce other concepts, such as
– the role of logic
– unification with the help of variables
• Begin the systematic study of Prolog by
defining terms, atoms, and variables
Prolog

• "Programming with Logic"


• Declarative
• Very different from other (procedural)
programming languages
• Good for knowledge-rich tasks
History of Prolog

first Prolog interpreter by


Colmerauer and Roussel

1972 1977 1980 1980s/1990s 2005


History of Prolog

implementation of
DEC10 compiler by
Warren

1972 1977 1980 1980s/1990s 2005


History of Prolog

Definite Clause Grammars


implementation by Pereira
and Warren

1972 1977 1980 1980s/1990s 2005


History of Prolog

Prolog grows in popularity


especially in Europe and Japan

1972 1977 1980 1980s/1990s 2005


History of Prolog

Prolog used to program


natural language
interface in
International Space
Station by NASA

1972 1977 1980 1980s/1990s 2005


Basic idea of Prolog

• 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
http://www.cs.trincoll.edu/~ram/cpsc352/n
otes/prolog/factsrules.html
Facts, Rules and Queries
Symbols

• Prolog expressions are comprised of the following


truth-functional symbols, which have the same
interpretation as in the predicate calculus.

Predicate
English PROLOG
Calculus
and ^ ,
or v ;
if --> :-
not ~ not
Variables and Names

Variables begin with an uppercase letter. Predicate names,


function names, and the names for objects must begin with a
lowercase letter. Rules for forming names are the same as for
the predicate calculus.
•mother_of
•male
•female
•greater_than
•socrates
Facts
A fact is a predicate expression that makes a declarative
statement about the problem domain. Whenever a variable
occurs in a Prolog expression, it is assumed to be universally
quantified. Note that all Prolog sentences must end with a
period.

•likes(john, susie). /* John likes Susie */


•likes(X, susie). /* Everyone likes Susie */
•likes(john, Y). /* John likes everybody */
•likes(john, Y), likes(Y, john). /* John likes everybody and everybody likes John */
•likes(john, susie); likes(john,mary). /* John likes Susie or John likes Mary */
•not(likes(john,pizza)). /* John does not like pizza */
•likes(john,susie) :- likes(john,mary)./* John likes Susie if John likes Mary.
Rules
A rule is a predicate expression that uses logical implication (:-)
to describe a relationship among facts. Thus a Prolog rule takes
the form
left_hand_side :- right_hand_side .
•This sentence is interpreted as: left_hand_side if right_hand_side.
•The left_hand_side is restricted to a single, positive, literal, which
means it must consist of a positive atomic expression. It cannot be
negated and it cannot contain logical connectives.
•This notation is known as a Horn clause. In Horn clause logic, the left
hand side of the clause is the conclusion, and must be a single positive
literal.
•The right hand side contains the premises. The Horn clause calculus is
equivalent to the first-order predicate calculus.
Rules
Examples of valid rules:

friends(X,Y) :- likes(X,Y),likes(Y,X). /* X and Y are friends if they


like each other */
hates(X,Y) :- not(likes(X,Y)). /* X hates Y if X does not like
Y. */
enemies(X,Y) :- not(likes(X,Y)),not(likes(Y,X)). /* X and Y are enemies
if they don't like each other */

Examples of invalid rules:

left_of(X,Y) :- right_of(Y,X) /* Missing a period */


likes(X,Y),likes(Y,X) :- friends(X,Y). /* LHS is not a single literal */
not(likes(X,Y)) :- hates(X,Y). /* LHS cannot be negated */
Queries
• The Prolog interpreter responds to queries about the facts
and rules represented in its database.
• The database is assumed to represent what is true about a
particular problem domain.
• In making a query you are asking Prolog whether it can prove
that your query is true.
• If so, it answers "yes" and displays any variable bindings that
it made in coming up with the answer.
• If it fails to prove the query true, it answers "No".
• Whenever you run the Prolog interpreter, it will prompt you
with ?-. For example, suppose our database consists of the
following facts about a fictitious family.
Queries
• father_of(joe,paul).
• father_of(joe,mary).
• mother_of(jane,paul).
• mother_of(jane,mary).
• male(paul).
• male(joe).
• female(mary).
• female(jane).
Queries
• We get the following results when we make queries about this
database. (I've added comments, enclosed in /*..*/, to interpret
each query.)
Queries
•| ?- ['family.pl'].
•compiling
/home/ram/public_html/cpsc352/prolog/family.pl for
byte code...
•/home/ram/public_html/cpsc352/prolog/family.pl
compiled, 9 lines read - 999 bytes written, 94 ms

•(10 ms) yes •(10 ms) yes


•| ?- listing. •| ?- father_of(joe,paul).

•mother_of(jane, paul). •true ?


•mother_of(jane, mary).
•yes
•male(paul). •| ?- father_of(paul,mary).
•male(joe).
•no
•female(mary). •| ?- father_of(X,mary).
•female(jane).
•X = joe
•father_of(joe, paul).
•father_of(joe, mary). •yes
•| ?-
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
rule
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
happy(yolanda). fact
listens2music(mia). fact
rule
listens2music(yolanda):- happy(yolanda).
rule
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
happy(yolanda). fact
listens2music(mia). fact
rule
listens2music(yolanda):- happy(yolanda).
rule
playsAirGuitar(mia):- listens2music(mia).
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

You might also like