prolog - university of toronto · prolog prolog inference rules programming in prolog recursion...

21

Upload: dinhhanh

Post on 28-Apr-2019

278 views

Category:

Documents


6 download

TRANSCRIPT

Prolog

Reading� Sethi� Chapter ���

� Overview

� Predicate Calculus

� Substitution and Uni�cation

� Introduction to Prolog

� Prolog Inference Rules

� Programming in Prolog

� Recursion

� List Processing

� Arithmetic

� Higher�order programming

� Miscellaneous functions

� Conclusion

Prolog

Programming in Logic

� Idea emerged in early ���s�

most work done at Univ� of Edinburgh�

� Based on a subset of �rst�order logic�

� Feed it theorems and pose queries�

system does the rest�

� main uses�

� Originally� mainly for natural language

processing�

� Now �nding uses in database systems

and even rapid prototyping systems of

industrial software�

� Popular languages� Prolog� XSB� LDL�

Coral� Datalog� SQL�

Logic Programming Framework

Answer�

Query�

Programming Environment

Is q�X�� ��� �XN� true�

�YesNo� variable bindings

Knowledge Base�

Proof Procedure

Facts � Rules

Declarative Languages

In its purest form� Logic programming is an

example of declarative programming�

Popular in database systems and arti�cial in�

telligence�

Declarative speci�cations� Specify what you

want� but not how to compute it�

Example� Find X and Y such that

� X � � Y � �

X � Y � �

A method program� for solving these is how

to get values for X and Y� But all we gave

was a speci�cation� or declaration of what we

want� Hence the name�

Examples

� �Retrieve the telephone number of the per�

son whose name is Tom Smith� easy�

� �Retrieve the telephone number of the per�

son whose address is �� Black St� hard�

� �Retrieve the name of the person whose

telephone number is ��������� hard�

Each command speci�es what we want but

not how to get the answer� A database sys�

tem would use a di�erent algorithm for each

of these cases�

Can also return multiple answers�

� �Retrieve the names of all people who live

on Oak St��

Algorithm � Logic � Control

� Users specify �logic� � what the algorithm

does � using logical rules and facts�

� �Control� �how the algorithm is to be im�

plemented � is built into Prolog�

i�e�� Search procedures are built into Prolog�

They apply logical rules in a particular order

to answer user questions�

Example� P if Q� and Q� and and Qk

can be read as

to deduce P�

deduce Q�

deduce Q�

���

deduce Qk

Users specify what they want using classical

�rst�order logic predicate calculus��

Classical First�Order Logic

� The simplest kind of logical statement is an

atomic formula� e�g��

mantom� tom is a man�

womanmary� mary is a woman�

marriedtom�mary�

tom and mary are married�

�More complex formulas can be built up using

logical connectives� �� �� �� �X� �X� e�g��

smarttom� � dumbtom�

smarttom� � talltom�

� dumbtom�

�X marriedtom� X�

tom is married to something�

�X lovestom� X�

tom loves everything�

�X marriedtom� X� � femaleX� � humanX��

tom is married to a human female�

Logical Implication

richtom� � �smarttom�

This implies that if tom is smart� then he must

be rich� So� we often write this as

richtom� � smarttom�

In general� P � Q and Q � P are abbrevia�

tions for P� � Q�

For example�

�X personX� � smartX�� � richX��

every person who is smart is also rich�

�X motherjohn�X�

john has a mother�

�X motherjohn�X� �

�Y motherjohn�Y� � Y � X�

john has exactly one mother�

Horn Rules

Logic programming is based on formulas called

Horn rules� These have the form

�x����xk �A� B� �B���� �Bj�

where k� j �

For example�

�X�Y AX� � BX�Y� � CY��

�X AX� � BX��

�X AX�d� � BX�e��

Ac�d� � Bd�e�

�X AX�

�X AX�d�

Ac�d�

Note that atomic formulas are also Horn rules�

often called facts�

A set of Horn rules is called a Logic Program

Logical Inference with Horn Rules

Logic Programming is based on a simple idea�

From rules and facts derive more facts

Example �� Given the facts A� B� C� D�

and these rules�

�� E � A � B

�� F � C � D

�� G � E � F

From ��� derive E

From ��� derive F

From ��� derive G

Example �� Given these facts�

manplato� �plato is a man��

mansocrates� �socrates is a man��

and this rule�

�X manX� � mortalX��

�all men are mortal��

derive� mortalplato�� mortalsocrates��

Recursive Inference

Example�

Given�

�X mortalX� � mortalson ofX���

mortalplato�

Derive�

mortalson ofplato��

using X � plato�

mortalson ofson ofplato���

using X � son ofplato��

mortalson ofson ofson ofplato����

using X � son ofson ofplato���

���

This kind of inference simulates recursive pro�

grams as we shall see��

��

Logic Programming

Horn rules correspond to programs� and a form

of Horn inference corresponds to execution�

For example� consider the following rule�

�X�Y pX� � qX�Y� � rX�Y� � sX�Y�

Later� we shall see that this rule can be inter�

preted as a program� where

p is the program name�

q�r�s are subroutine names�

X is a parameter of the program� and

Y is a local variable�

��

Non�Horn Formulas

The following formulas are not Horn�

A � � B

A � B

A � B � C

�X AX� � BX��

A � B � C�

�X flagX� � redX� � whiteX���

�every �ag is red or white��

�X �Y wifeX� � marriedX�Y��

�every wife is married to someone��

��

Non�Horn Inference

Inference with non�Horn formulas is more com�

plex than with Horn rules alone�

Example�

A � B

A � C

B � C non�Horn�

We can infer A� but must do case analysis�

either B or C is true

if B then A

if C then A

Therefore� A is true in all cases

Non�Horn formulas do not correspond to pro�

grams� and non�Horn inference does not cor�

respond to execution�

��

Logical Equivalence

Many non�Horn formulas can be put into Horn

form using two methods�

�� logical equivalence

�� skolemization

Example �� Logical Equivalance�

�A � �B �A � ��B�

�A � B

B � �A

Horn� B � A

Logical Laws�

��A A

� A � B� �A � �B

A � B � C� A � B� � A � C�

A � B A � �B

Example �� Logical Equivalance�

A � B � C� A � �B � C�

A � �B � �C�

A � �B� � A � �C�

Horn� A � B� � A � C�

��

Example �� Logical Equivalence�

A � B � C� A � �B � C�

A � �B � �C�

A � �B � ��C�

A � �B � C�

A � �B� � A � C�

non Horn� A � B� � A � C�

In general� rules of the following form cannot

be converted into Horn form�

�x� A� � ��� �An�� B� � ��� �Bm��

For example�

A � B� � C � D�

A � B� � C

A � B�

�X AX� � Bx�� � CX� � DX��

i�e�� if it is possible to infer a non�trivial dis�

junction from a set of formulas� then the set

is inherently non�Horn�

A rule like p � q � q infers a trivial dis�

junction� since the rule is a logical tautology�

Such rules can simply be ignored��

��

Skolemization

Non�Horn formulas like �x A x� can be con�

verted to Horn form�

Example ��

Replace �� �X motherjohn�X� non�Horn�

with �� motherjohn�m� Horn�

Here� m is a new constant symbol� called a

skolem constant� that stands for the unknown�

mother of john�

Note� �� � ��� but they say almost� the

same thing� In particular� �� can sometimes

be replaced by �� during inference� as we

shall see�

��

Skolemization �Cont�d�

Example �� A non�Horn formula�

�� �X personX� � �Y motherX�Y��

�every person has a mother��

Let mx� stand for the unknown� mother of

X� Then� we can replace �� by a Horn rule�

�� �X personX� � motherX�mX���

mX� is called a skolem function�

It is an arti�cial name we have created�

e�g�� mmary� denotes the mother of mary�

mtom� denotes the mother of tom�

mjfk� denotes the mother of jfk�

So� we only need �X because we don�t have a

name for X� By creating arti�cal names skolem

symbols�� we can eliminate many ��s� and

convert many formulas to Horn rules� which

Prolog can then use�

Skolemization is a technical device for doing

inference�

��

Inference with Skolemization

�� �X manX� � personX��

�every man is a person��

�� �X �Y personX� � motherX�Y��

�every person has a mother��non Horn�

�� �X�Y motherX�Y� � lovesY�X��

�every mother loves her children��

�� manplato� �plato is a man��

Question� �Y lovesY�plato�

�does someone love plato���

Step �� Skolemize �� to get a Horn rule�

��� �X personX� � motherX� mX���

Step �� Use Horn inference�

personplato� from ��motherplato�mplato�� from ���lovesmplato��plato� from ��

Thus� �Y lovesY�plato�

i�e�� Y � mplato�� So� answer is YES�

Skolem Dependencies

�� �X �Y pX�Y�

skolemizes to �Y pa�Y��

where a is a skolem constant�

�� �Y �X pX�Y�

skolemizes to �Y pbY��Y��

where b is a skolem function�

i�e�� in ��� X depends on Y�

But in ��� X is independent of Y�

�� �X �Y �Z qX�Y�Z�

skolemizes to �X �Y qX�Y�cX�Y���

where c is a skolem function of both X and Y�

i�e�� in ��� Z depends on both X and Y�

Skolem Dependencies �Concrete Examples

�X �Y lovesX�Y� ��someone loves everybody��

� �Y lovesp�Y� ��p loves everybody��

�X �Y motherX�Y� ��everyone has a mother��

� �X motherX�mX��

��m�X� is the mother of X��

�X �Y �Z ownsX�Y� � documentZ�X�Y�

��if X owns Y� then there is a document� Z� saying

that X owns Y��

� �X �Y ownsX�Y� � documentdX�Y��X�Y�

��d�X�Y� is a document saying that X owns Y��

��