Vous êtes sur la page 1sur 17

Logi

Programming and Prolog


Logi programming languages are not pro edural or fun tional.

LOGIC PROGRAMMING
AND PROLOG

Spe ify relations between obje ts


{ larger(3,2)
{ father(tom,jane)

Reading:

 Sebesta, hapter 16
Referen es:

 Clo ksin and Mellish, Ch. 1-4, 6, 8


 Online Resour es (tutorials, SWI page, et .)
Diane Horton 2000, and
Some material
Suzanne Stevenson 2001.
Modi ed by Sheila M Ilraith 2004.
1

Separate logi from ontrol:


{ Programmer de lares what fa ts and relations
are true.
{ System determines how to use fa ts to solve
problems.
{ System instantiates variables in order to make
relations true!
Computation engine: theorem-proving and re ursion (Uni ation, Resolution, Ba kward Chaining,
Ba ktra king)
{ Higher-level than imperative languages

Jumping Right In

We an also state rules, su h as this one:


sibling(X, Y) :- parent(P, X),
parent(P, Y).

Suppose we state these fa ts:


male(albert).
female(ali e).
male(edward).
female(vi toria).

parent(albert,edward).
parent(vi toria,edward).
parent(albert,ali e).
parent(vi toria,ali e).

We an then make queries:


?- male(albert).
Yes

Then the queries be ome more interesting:

?- male(vi toria).
No

?- sibling(albert, vi toria).
No

?- female(Person).
Person = ali e;
Person = vi toria;
No

?- sibling(edward, Sib).
Sib = edward;
Sib = ali e;
Sib = edward;
Sib = ali e;
No

?- parent(Person, edward).
Person = albert;
Person = vi toria;
No
?- parent(Person, edward), female(Person).
Person = vi toria;
No
3

Prolog vs S heme

Logi Programming

In S heme, we program with fun tions ("pro edures").


 A fun tion's arguments are di erent from
the fun tion's value.
 Give a single S heme fun tion, we an only
ask one kind of question:

 A program onsists of fa ts and rules.


 Running a program means asking queries.
 The language tries to nd one way

Here are the argument values; tell me


what is the fun tion's value.
In Prolog, we program with relations.
 There is no bias; all arguments are the
same.
 Given a single Prolog predi ate, we an ask
many kinds of question:

(or more) to prove that the query is true.

 This may have the side e e t of freezing


variable values.

 The language determines how to do all of


this, not the program.

 How does the language do it? Using uni ation, resolution, and ba ktra king.

Here are some of the argument values; tell me what the others have to
be in order to make a true statement.
5

The swi Interfa e on df

?- tra e.
Yes
[tra e

df% ls
family.pl

[tra e ?- parent(Person, edward).


df% swi
Wel ome to SWI-Prolog (Multi-threaded, Version 5.2.11)
[tra e ?- parent(Person, edward).
Copyright ( ) 1990-2003 University of Amsterdam.
Call: (7) parent(_G283, edward) ? reep <- CR to ontinue
SWI-Prolog omes with ABSOLUTELY NO WARRANTY. This is free software,
Exit: (7) parent(albert, edward) ? reep
and you are wel ome to redistribute it under ertain onditions.
Please visit http://www.swi-prolog.org for details.
Person = albert ;
Redo: (7) parent(_G283, edward) ? reep
For help, use ?- help(Topi ). or ?- apropos(Word).
Exit: (7) parent(vi toria, edward) ? reep
?- ['family'.
<------------- load file family.pl
% family ompiled 0.00 se , 5,264 bytes
Yes

Person = vi toria
Yes
[debug

?-

?- parent(Person, edward).
Person = albert;
<----------- ";" to get more
Person = vi toria;
No
?- parent(Person, edward).
Person = albert
Yes

<--------------"a", CR, spa e to break


7

(0) Call: parent(_57,edward) ?


(0) Exit: parent(albert,edward) ?
Person = albert;
(0) Redo: parent(albert,edward) ?
(0) Exit: parent(vi toria,edward) ?
Person = vi toria
Yes
[tra e

?- notra e.
Yes

df% pl
Wel ome to SWI-Prolog (Multi-threaded, Version 5.2.11) ...

?- parent(Person, edward).

For help, use ?- help(Topi ). or ?- apropos(Word).

Person = albert;
Person = vi toria;
No

?- [family.
[family loaded
Yes

?- halt.
df%

?- parent(Person, edward).
Person = albert;
Person = vi toria;
No
--- edit family.P and remove parent(albert,edward). --?- ['family'.
% family ompiled 0.00 se , 5,200 bytes
Yes
?- parent(Person, edward).
Person = vi toria;
No
?- halt.
df%
9

Some Prolog Syntax


Lexi al Rules:
 Variables are apitalized.
 Constants begin with a lower ase letter.
 Predi ate names begin with a lower ase
letter.
Simpli ed Grammar:

Prolog Queries
A query is a proposed fa t that is to be proven.

 If the query has no variables, returns yes/no.

< lause> ::= <pred> |

 If the query has variables, returns appro-

<pred> :- <pred> { , <pred> } .


<pred>

::= <pname>`(' <term> { , <term> } `)'

<term>

::= <int> | <atom> | <var>

priate values of variables ( alled a substitution).

Note: No blank between predi ate name and


opening bra ket.
10

11

Horn Clauses
(Rules)
A Horn Clause is:

Horn Clause Terminology

1 ^ h2 ^ h3 ^ : : : ^ hn

 Ante edents: onjun tion of zero or more


onditions whi h are atomi formulae in
predi ate logi

 Consequent: an atomi formula in predi ate logi

Horn Clause = Clause

Consequent = Goal = Head

Ante edents = Subgoals = Tail

Horn Clause with No Tail = Fa t

Horn Clause with Tail = Rule

In Prolog, a Horn lause


h1 ^ : : : ^ hn

is written
:- h1, , hn.

Meaning of a Horn lause:

Syntax elements:

`:-'

`,'

`.'

 \The onsequent is true if the ante edents


are all true"

 is true if 1,

true

2,

3,

. . . , and

hn

are all
12

13

Prolog Horn Clause Examples


A Horn lause with no tail:
male(albert).

Meaning of Prolog Rules


Without Variables
A prolog rule must have this form:
:
a1 ;
a2 ;
a3 ;
; a
whi h means in logi :

n:

I.e., a fa t: albert is a male dependent on no


other onditions

1 ^ a2 ^ a3 ^    ^ an ! :

A Horn lause with a tail:


father(albert,edward):male(albert), parent(albert,edward).

Restri tions

I.e., a rule: albert is the father of edward if albert is male and albert is a parent of edward's.

 There an be zero or more ante edents,


but they are onjoined; we annot disjoin
them.

 There annot be more than 1 onsequent.


14

15

Bending the Restri tions

Why Can't We
Disjoin Consequents?

Getting disjoined ante edents

Example:

Why did the designers of Prolog disallow this?

1 _ a2 _ a3_ ! :

Solution:
Getting more than 1 onsequent, onjoined

Example:

1 ^ a2 ^ a3 ! 1 ^ 2:

Solution:
Getter more than 1 onsequent, disjoined

Example:

1 ^ a2 ^ a3 ! 1 _ 2:

Solution:
16

17

Logi Review

Horn Clauses with Variables


Variables may appear in the ante edents
onsequent of a Horn lause:

and

 (X1,. . . ,X ) :- h(X1,. . . ,X ).
n

\For all values of X1,. . . ,X , the formula


(X1,. . . ,X ) is true if the formula h(X1,. . . ,X )
is true"
n

 (X1,. . . ,X ) :- h(X1,. . . ,X ,Y1,. . . ,Y ).


n

\For all values of X1,. . . ,X , the formula


(X1,. . . ,X ) is true if there exist values of
Y1,. . . ,Y su h that the formula h(X1,. . . ,X ,Y1,. . . ,Y )
is true"
n

18

19

Meaning of Prolog Rules


With Variables

Sample run

df% pl
Wel ome to SWI-Prolog (Multi-threaded, Version 5.2.11)...

Example:

?- ['family'.

isaMother(X) :- female(X), parent(X, Y).

Logi :

parent X; Y

) ^ f emale(X )  isaM other(X ):

But this is meaningless without quanti ers for


the variables.

Warning: (./family.pl:50):
Singleton variables: [Y
% family ompiled 0.00 se , 5,528 bytes
?- isaMother(X).
X = vi toria;
X = vi toria;
No

The rule

A Prolog rule of this form ( 0


):
(X1 ;    X ) :
a(X1 ;    X ; Y1 ;    Y ):
means:
n 

; m  n; k  o

8X1 ;    Xn

[9Y1;    Yk [a(X1;    Xm; Y1;    Yk )  (X1;    Xn)


20

21

Rule Ordering and Uni ation

1. rule ordering used in sear h


2. uni ation requires two instan es of the
same variable in the same rule to get the
same value
3. uni ation does not require di erently named
variables to get di erent values: hen e,
sibling(edward,edward).
4. all rules sear hed if requested by ';'

How Prolog Handles a Query


Example 1

Database:
1)
2)
3)
4)
5)

male(tom).
male(peter).
male(doug).
female(susan).
male(david).

6)
7)
8)
9)

parent(doug, susan).
parent(tom, william).
parent(doug, david).
parent(doug, tom).

10)

grandfather(GP, GC) :- male(GP),


parent(GP, X) ,
parent(X, GC).

Query:
| ?- grandfather(X,Y).

22

23

Tra e it in Prolog

Tra e it by hand

[tra e ?- grandfather(X,Y).
Call: (7) grandfather(_G283, _G284) ? reep
Call: (8) male(_G283) ? reep
Exit: (8) male(tom) ? reep
Call: (8) parent(tom, _L205) ? reep
Exit: (8) parent(tom, william) ? reep
Call: (8) parent(william, _G284) ? reep
Fail: (8) parent(william, _G284) ? reep
Redo: (8) male(_G283) ? reep
Exit: (8) male(peter) ? reep
Call: (8) parent(peter, _L205) ? reep
Fail: (8) parent(peter, _L205) ? reep
Redo: (8) male(_G283) ? reep
Exit: (8) male(doug) ? reep
Call: (8) parent(doug, _L205) ? reep
Exit: (8) parent(doug, susan) ? reep
Call: (8) parent(susan, _G284) ? reep
Fail: (8) parent(susan, _G284) ? reep
Redo: (8) parent(doug, _L205) ? reep
Exit: (8) parent(doug, david) ? reep
Call: (8) parent(david, _G284) ? reep
Fail: (8) parent(david, _G284) ? reep
Redo: (8) parent(doug, _L205) ? reep
Exit: (8) parent(doug, tom) ? reep
Call: (8) parent(tom, _G284) ? reep
Exit: (8) parent(tom, william) ? reep
Exit: (7) grandfather(doug, william) ? reep
X = doug
Y = william
Yes
24

25

Prolog Sear h Trees

Example 2

 Ea h node is an ordered list of goals.


 Ea h edge is labelled with the variable bindings that o urred due to applying a rule.
(The binding are in e e t throughout the
subtree.)

 Ea h leaf represents either su ess or failure.

Database:
1)
2)
3)
4)
5)
6)
7)
8)
9)

male(albert).
female(ali e).
male(edward).
female(vi toria).
parent(albert,edward).
parent(vi toria,edward).
parent(albert,ali e).
parent(vi toria,ali e).
sibling(X, Y) :- parent(P, X), parent(P, Y).

Query:
?- sibling(ali e,Asib).
Asib = edward ;
Asib = ali e ;
Asib = edward;
Asib = ali e ;
No
?- sibling(Asib, ali e).
Asib = edward ;
Asib = edward ;
Asib = ali e ;
Asib = ali e ;
No
26

27

Tra e it by hand

Tra e it in Prolog

[tra e
Call:
Call:
Exit:
Call:
Exit:
Exit:

?- sibling(ali e,Asib).
(7) sibling(ali e, _G284) ? reep
(8) parent(_L205, ali e) ? reep
(8) parent(albert, ali e) ? reep
(8) parent(albert, _G284) ? reep
(8) parent(albert, edward) ? reep
(7) sibling(ali e, edward) ? reep

Asib = edward ;
Redo: (8) parent(albert, _G284) ? reep
Exit: (8) parent(albert, ali e) ? reep
Exit: (7) sibling(ali e, ali e) ? reep
Asib = ali e
Redo: (8)
Exit: (8)
Call: (8)
Exit: (8)
Exit: (7)

;
parent(_L205, ali e) ? reep
parent(vi toria, ali e) ? reep
parent(vi toria, _G284) ? reep
parent(vi toria, edward) ? reep
sibling(ali e, edward) ? reep

Asib = edward ;
Redo: (8) parent(vi toria, _G284) ? reep
Exit: (8) parent(vi toria, ali e) ? reep
Exit: (7) sibling(ali e, ali e) ? reep
Asib = ali e ;
No
28

29

The Anonymous Variable

Pro edural Semanti s of Prolog

If a rule has a variable that appears only on e,


that variable is alled a \singleton variable".

Noti e the re ursion in this algorithm: \ nd"


alls \ nd". This reasoning is re ursively applied until we rea h rules that are fa ts.

Its value doesn't matter | it doesn't have to


mat h anything elsewhere in the rule.

This pro ess is alled Ba kward

Chaining

isaMother(X) :- female(X), parent(X, Y).

Su h a variable onsumes resour es at run time.

We an repla e it with \ ", the anonymous


variable. It mat hes anything.
If we don't, Prolog will warn us.
30

31

Logi Programming vs. Prolog


ousin(X,Y) :- parent(W,X), sister(W,Z),
parent(Z,Y).
ousin(X,Y) :- parent(W,X), brother(W,Z),
parent(Z,Y).

j ?- ousin(X,jane). % a query
Rule and Goal Ordering:
 There are two rules for ousin
 Whi h rule do we try rst?
 Ea h rule for ousin has several subgoals
 Whi h subgoal do we try rst?

32

Logi Programming vs. Prolog


Logi Programming:

Nondeterministi

 Arbitrarily hoose rule to expand rst


 Arbitrarily hoose subgoal to to explore

rst
 Results don't depend on rule and subgoal ordering
Prolog:

Deterministi

 Expand rst rule rst


 Explore rst subgoal rst
 Results may depend on rule and subgoal
ordering

33

Vous aimerez peut-être aussi