344-302...

46
344-302 กกกกกกกกกกกกกกกกกกก กกกกกกกกกกกกกกก กกกก(Logic Programming and Prolog) กก.กกกกกก กกกกกกกกกกกกกก กกกกกกกกกกกกกกกกกกกกกกกกกก กกก กกกกกกกกกกก กกกกกกกกกกกกกกกกกกกกกกกก กกกกกกกกกก : CS 320 กกกกกกกก : 074-288596 E-mail : [email protected] Website : http://www.cs.psu.ac.th/wiphada

Upload: marlis

Post on 06-Jan-2016

23 views

Category:

Documents


2 download

DESCRIPTION

344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog). ดร.วิภาดา เวทย์ประสิทธิ์ ภาควิชาวิทยาการคอมพิวเตอร์ คณะวิทยาศาสตร์ มหาวิทยาลัยสงขลานครินทร์ ห้องทำงาน : CS 320 โทรศัพท์ : 074-288596 E-mail : [email protected] - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

344-302

การเขี�ยนโปรแกรมเชิ�งตรรกะและภาษาโพรล�อก(Logic

Programming and Prolog)

ดร.วิ�ภาดา เวิทย�ประสิ�ทธิ์�� ภาควิ�ชิาวิ�ทยาการคอมพ�วิเตอร� คณะวิ�ทยาศาสิตร�

มหาวิ�ทยาล$ยสิงขีลานคร�นทร� ห%องท&างาน : CS 320 โทรศ$พท� :

-074288596

E-mail : [email protected] :

http://www.cs.psu.ac.th/wiphada

Page 2: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 2344-302 LP and Prolog

วั�ตถุ�ประสงค์ 1. ให้�นั�กศึ�กษามี�ค์วัามีร� �ค์วัามีเข้�าใจเก��ยวัก�บการเข้�ยนัโปรแกรมีเชิ#ง

ตรรกะ 2. ให้�นั�กศึ�กษาสามีารถุเข้�ยนัโปรแกรมีภาษาโพรล็'อกได้�วิ�ธิ์�การเร�ยนการสิอน : การบรรยาย ปฏิ#บ�ต#การการวิ$ดผล : Assignment 25%

สอบกล็างภาค์ 35%สอบปล็ายภาค์ 40%

เวิลาเร�ยน : จั$นทร� 11-12 ห%อง วิท3ศ*กร� 11-12 ห%อง วิท3

ศ*กร� 13-15 ห%อง CS105 (LAB)

Page 3: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 3344-302 LP and Prolog

Chapter 1 : Introduction to LogicChapter 2 : Syntax and Meaning of Prolog ProgramsChapter 3 : Input and OutputChapter 4 : Knowledge RepresentationChapter 5 : Expert SystemChapter 6 : Controlling BacktrackingChapter 7 : Lists Operator Chapter 8 : Arithmetic and Program Examples

เนั,-อห้าวั#ชิา

Page 4: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 4344-302 LP and Prolog

PROLOG Programming for Artificial Intelligence, Second Edition, Ivan Bratko, Addison-Wesley, Pulishing, 1990.Prolog : A Relational language and its Applications, John Malpas, Prentice-Hall, Inc. 1987.Expert Systems and Applied Artificial Intelligence, Efraim Turban, Macmillan Publishing Company, 1992.

เอกสิารอ%างอ�ง

Page 5: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1

Introduction

Page 6: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

• Propositional logic Formal Logic

Predicate Logic Predicate Calculus Symbolic Logic Logic Programming

Content

Page 7: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 7344-302 LP and Prolog

A statement that affirms or denies something.

Proposition

Page 8: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 8344-302 LP and Prolog

Proposition : a logical statement that may or may not be true.

objectrelationships of objects

A = I like banana. trueB = I like orange. falseC = I like mango. trueA and B = I like banana and orange. false

Proposition

Page 9: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 9344-302 LP and Prolog

All men are mortal Socrates is a man

Therefore, Socrates is mortal into propositional logic yieldsA

B C ( means "therefore ").

Propositional Logic

Page 10: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 10344-302 LP and Prolog

Propositional expression

implicationa b ~a v b~a b a V b

equivalencea b (a & b) v (~a & ~b)

tautologies : always truea v ~a

contradiction : always falseb & ~b

Page 11: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 11344-302 LP and Prolog

Propositional expression

contrapositive law

a b ~b ~a

Page 12: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 12344-302 LP and Prolog

A B C : propositions1. property of and, or

A and True = AA and False = FalseA or True = TrueA or False = A

2. property of negationA and ~A = FalseA or ~A = True

Proposition expression

Page 13: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 13344-302 LP and Prolog

3. commutative law (กฎการสิล$บท�-)A and B = B and AA or B = B or A

4. associative law (กฎการเปล�-ยนกล*.ม)A and (B and C) = (A and B) and CA or (B or C) = (A or B) or C

5. distributive law (กฎการแจักแจัง)A and (B or C) = (A and B) or (A and C)A or (B and C) = (A or B) and (A or C)

Proposition expression

Page 14: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 14344-302 LP and Prolog

6. de morgan’s lawsnot (A and B) = not A or not B not (A or B) = not A and not B

7. inference rule (กฎการอน*มาน) chain rule modus ponens

1 A B 1 A

2 B C 2 A BA C B

Proposition expression

Page 15: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 15344-302 LP and Prolog

Chain rule

(1) All California are American. เห้ต�(2) All American are Car-Drivers. เห้ต�(3) All California are Car-Drivers. ผล็

C AA DC D

Page 16: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 16344-302 LP and Prolog

Modus Ponens

If P implies Q, and P is true, then Q is true.

Page 17: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 17344-302 LP and Prolog

Transitive

(1) All baseball player are gum-chewers. เห้ต�(2) Some baseball players are from Boston. เห้ต�(3) Some (people) from Boston are gum-chewers. ผล็

21

Page 18: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 18344-302 LP and Prolog

any logical system that abstracts the form of statements away from their content in order to establish abstract criteria of consistency and validity

Formal Logic

Page 19: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 19344-302 LP and Prolog

Formal Logic

Logic (ตรรก): true, false truth table : and, or, not, xor, implies, if and only if

A B A and B A or B A xor B AB AB ~A

T T T T F T T FT F F T T F F FF T F T T T F TF F F F F T T T

Formal Logic : use to describe propositions

check proposition for validation

Page 20: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 20344-302 LP and Prolog

Name Symbol Example Meaning Precedencenegation ~ ~a not a 1conjunction & a & b a and b 2disjunction V a v b a or b 2equivalence a b a is equivalent to b 2implication a b a implies b (if a then b) 3

double negation : ~ ~ a = a compound propositions

a & b ca & ~b d : (a & (~b)) d

well-formed formula (wff) : a complex formula that includes logical connectives

Formal Logic

Page 21: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 21344-302 LP and Prolog

predicate logic or First-order logic (FOL) is a language in symbolic science,

which is used by mathematicians, philos ophers, linguists, and computer scientist

s. FOL is a system of deduction extending

propositional logic by the ability to express relations betwe

en individuals (e.g. people, numbers, an d "things") more generally.

Predicate Logic

Page 22: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 22344-302 LP and Prolog

x (Man(x ) → Mortal(x ))Man(Socrates ) Mortal(Socrates)

Predicate Logic

Page 23: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 23344-302 LP and Prolog

constants : a symbol that represent an object.manjohn

variables : a symbol that can represent different objects at different times

man(X) . (ต�วัอ�กษรต�วัให้ญ่0) compound term 1. functor :function symbol that name the relation2. an ordered list of parameters

man(john) like(bob,steak)

Predicate Logic

Page 24: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 24344-302 LP and Prolog

variable can be appear in propositions using

quantifiers (ต�วับ0งปร#มีาณ). Name Example Meaning

universal XP For all X, P is trueexistential XP There exists a value of X

such that P is true. quantifiers have highest precedence than any of the operators

Predicate Logic

Page 25: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 25344-302 LP and Prolog

Traditional Logic

All S is P. No S is P. ~ Some S is P. Some S is not P. ~

All baseball player are gum-chewers.Some taxi-driver are not aggressive.

X(woman(X) human(X))X(mother(mary,X) & male(X))

Page 26: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 26344-302 LP and Prolog

Quantifiers

p, q : predicates X, Y : variables~Xp(X) = X~p(X)~Xp(X) = X~p(X)Xp(X) = Yp(Y) Xp(X) = Yp(Y) X(p(X) & q(Y)) = Yp(Y) & Yq(Y)X(p(X) & q(Y)) = Yp(Y) & Yq(Y)

Page 27: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 27344-302 LP and Prolog

Predicate calculus

The branch of symbolic logic that deals not only with relations between propositions as a

whole but also with their internal structure, e specially the relation between subject and pr

edicate. Symbols are used to represent the s ubject and predicate of the proposition, and t

he existential or universal quantifier is used t o denote whether the proposition is universal or particular in its application.

Page 28: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 28344-302 LP and Prolog

Predicate calculus

a system of symbolic logic that represents individuals and predicates and quantification over individuals (as well as the relations between propositions )

Page 29: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 29344-302 LP and Prolog

Predicate calculus

If it is not rain tomorrow, Tom will go to the mountains.~weather(rain,tomorrow) go(tom,mountain)

Emma is a Doberman and a good dog.gooddog(emma) & isa(emma,doberman)

Page 30: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 30344-302 LP and Prolog

Predicate calculus

All basketball player are tall.X(basketball_player(X) tall(X))

Some people like wine.X(person(X) & like(X,wine))

Nobody likes taxes ~Xlike(X,taxes)

Page 31: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 31344-302 LP and Prolog

a

c

d

b

A blocks world

Page 32: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 32344-302 LP and Prolog

A blocks world

on(c,a). on(b,d).ontable(a).ontable(d).clear(b).clear(c). hand_empty.

a

c

d

b

Page 33: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 33344-302 LP and Prolog

A blocks world

a block is clear :

X (~ Y on(Y,X) clear(X)).

pick up X and putdown X on Y :

XY ((hand_empty & clear(X) & clear(Y)

pick_up(X) & put_down(X,Y)) stack(X,Y))

ac

db

Page 34: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 34344-302 LP and Prolog

vertebrate

bird

bluebirdsmall

features flies

blue

ISA

ISA

hasproperty

hascolor

hascovering

hassize

Semantic network

Page 35: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 35344-302 LP and Prolog

Predicate Calculusvertebrate

bird

bluebirdsmall

features flies

blue

ISA

ISA

hasproperty

hascolor

hascovering

hassize

hassize(bluebird, small)hascovering(bird,features)hascolor(bluebird,blue)hadproperty(bird,flies)isa(bluebird,bird)isa(bird,vertebrate)

Semantic Network

Predicate Calculus

Page 36: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 36344-302 LP and Prolog

Predicate calculus

knows(mary, bob)knows(sam, bob)knows(sam, petricia).

Who knows bob?Who knows bob and petricia?Does mary know anybody?

Page 37: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 37344-302 LP and Prolog

Predicate calculus

Facts:father(bill, daniel)father(bill, kenneth)brother(daniel, kenneth)brother(john, bill)

Rule:uncle(U,N) :- brother(U,B), father(B,N)uncle(U,N) :- brother(U,B), mother(B,N)

U B

N

Page 38: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 38344-302 LP and Prolog

Facts:father(bill, daniel) father(bill, kenneth)brother(daniel, kenneth) brother(john, bill)

Rule:uncle(U,N) :- brother(U,B), father(B,N)

U B

N

john bill

daniel kenneth Predicate calculus

Page 39: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 39344-302 LP and Prolog

Predicate calculus

“george likes susie if george like kate.”likes(george, susie) :- likes(george, kate)“kate does not like wine.” not(likes(kate,wine))

Page 40: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 40344-302 LP and Prolog

Predicate calculus

Facts : likes(george,kate) likes(george,susie) likes(george,wine)

likes(susie,wine) likes(kate,beer) likes(kate,susie)

Rule : friend(X,Y) :- likes(X,Z), likes(Y,Z)

Page 41: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 41344-302 LP and Prolog

A treatment of formal logic in which a system of

symbols is used to represe nt quantities and relations

hips.

Symbolic Logic

Page 42: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 42344-302 LP and Prolog

express proposition.express the ......relationship between the prepositions.describe how new propositions can be inferred from other prepositions that are assumed to be true.EX : john is a man. man(john)

wichai is a man. man(wichai) EX : man(X)

Symbolic Logic

Page 43: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 43344-302 LP and Prolog

Logic Programming

B1 and … and Bn implies H treats the implications as

- goal reduction procedures to show/solve H,

show/solve B1 and … and Bn .

creating a program that enables the computer to reason logically

Page 44: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 44344-302 LP and Prolog

Logic Programming

Logic programming .....use a form of symbolic logic

Logic programming language .....language base on symbolic logic

Page 45: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)

Chapter 1 45344-302 LP and Prolog

Homework 1

I married a window (W) who has a grown-up daughter (D). My father (F) who visited us quite often, fell in love with my step-daughter and married her. Hence my father became my son-in-law and my step-daughter become my mother. Some months later, my wife gave birth to a son (S1), who became the brother–in-law of my father, as well as my uncle. The wife of my father, that is, my step-daughter, also had a son (S2).

Question 1. Is this statement true?

“I am my own grandfather.”

Question 2. Explain how to receive your solution.

Page 46: 344-302 การเขียนโปรแกรมเชิงตรรกะและภาษาโพรล็อก ( Logic Programming and Prolog)