10.1 Logic - University of Hawaii

advertisement

Logic Programming & Prolog

• A Brief Introduction to Predicate Calculus

• Predicate Calculus and Proving Theorems

• An Overview of Logic Programming

• The Origins of Prolog

• The Basic Elements of Prolog

• Sebesta Ch. 16

1

Introduction

• Logic programming languages and declarative programming languages

– Express programs in a form of symbolic logic

– Use a logical inferencing process to produce results

2

• Declarative rather that procedural :

– only specification of results are stated

– not detailed procedures for producing them

Introduction to Predicate Calculus

• Proposition

– A logical statement

– May be true or false

– Consists of objects and relationships of objects to each other

– Examples:

• John is a student : student(john)

• Hillary is Bill's wife: wife(hillary,bill)

3

Introduction to Predicate Calculus

• Symbolic logic is a subset of formal logic

– expresses propositions

– expresses relationships between propositions

– describes how new propositions can be inferred from other propositions

4

• Predicate calculus

– Form of symbolic logic used for logic programming

• Prolog

– PL based on predicate calculus

Propositions

• Objects in propositions

– either constants or variables

• Constant

– A symbol that represents an object

– In Prolog, starts with a lower case letter

• Variable

– A symbol that can represent different objects at different times

– Instantiated with values during the reasoning process

• Different from variables in imperative PLs

– In Prolog, starts with an upper case letter or _

5

Atomic Propositions

• Atomic proposition

– Consist of compound terms

• Compound term

– An element of a mathematical relation

– Written like a mathematical function, e.g., f(x,y)

– Can be written as a table

• Mathematical function is a mapping

• Compound term composed of two parts

– Functor

• Function symbol that names the relationship

– Tuple

• Ordered list of parameters

6

Propositions in Prolog

• Prolog Examples

– Functors:

• and , or , not , student , like

– Propositions

• student(john)

• like(mary,mac)

• like(nick,windows)

• like(kris,linux)

7

Forms of Propositions

• Propositions can be stated in two forms; as

– Fact

• proposition that is assumed to be true

– Query

• whether a proposition is true

• Compound proposition

– Consists of two or more propositions

– Connected by logical operators

• and, or, not, etc.

8

Logical Operators

Name negation conjunction disjunction equivalence implication

Symbol Example

¬ a ¬ a

∩ a ∩ b

Meaning not a a ⊃ b a ⊂ b a and b a ∪ b a or b a ≡ b a is equivalent to b a implies b b implies a

9

Truth Tables

a b ¬ a a ∩ b a ∪ b a ⊃ b a ⊂ b a ≡ b

T T F T T T T T

T F F

F T T

F F T

F

F

F

T

T

F

F

T

T

T

F

T

F

F

T

10

Quantifiers

Name Example Meaning universal ∀ X.P

For all X, P is true existential ∃ X.P

There exists a value of X such that P is true

• Examples

– ∀ bird, wings(bird)

• all birds have wings

– ∃ bird, blue(wings, bird)

• at least one bird has blue wings

11

Clausal Form

• Too many ways to state the same preposition

– Use a standard form

• Clausal form :

B

1

∪ B

2

∪ … ∪ B n

⊂ A

1

∩ A

2

∩…∩ A m

– means if all the As are true, then at least one B is true

• Antecedent

– right side

– I.e., A

1

∩ A

2

• Consequent

∩…∩ A m

– left side

– I.e., B

1

∪ B

2

∪ … ∪ B n

12

Predicate Calculus and Theorem Proving

• The use of propositions is to discover new theorems

– infer them from known axioms and theorems

• Resolution

– an inference principle

– computes inferred propositions from given ones

• Example Propositions:

– older(joanne, jake)

– mother(joanne, jake)

• Rule:

– older(joanne, jake) ⊂ mother(joanne, jake)

– Joanne is older than Jake IF Joanne is the mother of Jake

13

Reasoning with Propositions – Example

• Rule: older(joanne, jake) ⊂ mother(joanne, jake)

– Joanne is Jake’s mom therefore Joanne is older than Jake

– a FACT with constants

• Rule: wiser(joanne, jake) ⊂ older(joanne, jake)

– Joanne is older than Jake, therefore Joanne is wiser than Jake

• To proceed, combine rules using “and” of left side terms, and “and” of right side terms (looks like addition)

A ⊂ B

C ⊂ D .

A ∩ C ⊂ B ∩ D

• older(joanne, jake) ∩ wiser(joanne, jake)

⊂ mother(joanne, jake) ∩ older(joanne, jake)

• Remove the same propositions on both sides

– will have no effect on T/F

• Result: wiser(joanne, jake) ⊂ mother(joanne, jake)

14

Proof by Contradiction

• Hypotheses

– A set of pertinent propositions

• Goal

– Negation of theorem stated as a proposition

• Combine

– negated unknown goal proposition

– with known true propositions

• When an inconsistency results

– this proves that the unknown propositions are not true

– if they were true, inconsistency would not be possible

15

Horn Clauses

• Theorem broving is basis for logic programming

• When propositions used for resolution, only restricted form can be used

• Horn clauses - have only two forms

– Headed clause

• single atomic proposition on left side

• propositions on the right connected with “and”

– Headless clause

• empty left side

• propositions on the right connected with “and”

• used to state facts

• Most propositions can be stated as Horn clauses

16

Unification: Resolution with Variables

• Instantiation

– Assigning temporary values to variables in propositions

• Unification

– Finding such values for variables so that matching/instantiation to succeeds

• When

1. a variable is instantiated with a value, and then

2. matching fails

• Then

3. unification needs to backtrack

• the variable is instantiated with a different value

17

Semantics of Logic Programming

• Declarative semantics

– Determining the meaning of each statement is simple

– Simpler than the semantics of imperative languages

• Programming is nonprocedural

– Programs do not state how to compute the result, but what the conditions the result needs to satisfy

• Facts and variables

– Are bound temporarily

– They can't be re-bound unless they are unbound first

– Binding and unbinding proceeds automatically

18

Example with Variables

1.

older(A, B) ⊂ mother(A, B) ; mother is older

2.

wiser(C, D) ⊂ older(C, D) ; older is wiser

3.

mother(joanne, jake) ; Joanne is Jake’s mom

19

A / joanne B / jake instantiation A / joanne B / jake

(1 & 3) older(joanne, jake) ⊂ mother(joanne,jake)

C / joanne D / jake instantiation C / joanne D / jake

(1 & 2) wiser(joanne, jake) ⊂ older(joanne, jake)

Example: Sorting a List

• Describe the characteristics of a sorted list, not the process of rearranging a list

sort(old_list, new_list) ⊂ permute

(old_list, new_list) ∩ sorted (new_list) sorted (list) ⊂ ∀ j such that 1≤j<n, list(j)≤list(j+1)

20

The Origins of Prolog

• Alain Colmerauer, Philippe

Roussel

• University of Aix- Marseille,

France

– Natural language processing

• Robert Kowalski

• University of Edinburgh,

Scotland, U.K.

– Automated theorem proving

21

Basic Elements of Prolog

• Edinburgh Syntax (most common)

• Atom

– symbolic value in Prolog

• Atom consists of either

– a string of letters, digits, and underscores beginning with a lowercase letter {a,b,….z}, e.g., lucky

– a string of printable ASCII characters delimited by apostrophes ' ,e.g. , 'this is a string'

• Constant

– an atom or an integer

• Variable

– a string of letters, digits, and underscores beginning with an uppercase letter, e.g., Lucky

22

Basic Elements of Prolog (cont.)

• Term

– A constant, variable, or structure

– cat(lucky) , Cat , sibling(S1,S2)

• Structure

– Represents atomic proposition

– functor ( parameter list )

• Instantiation

– Binding of a variable to a value

– Only as long as it takes to satisfy one complete goal

23

Statement of Facts

• Headless Horn clauses student(jonathan).

sophomore(ben).

brother(tyler, cj).

• Used for true statements, i.e. facts

24

Rule Statements

• Used for the hypotheses

• Headed Horn clauses

• Right side

– antecedent ( if part)

– May be single term or conjunction

• Left side

– consequent ( then part)

– Must be single term

• Conjunction :

– multiple terms separated by implied logical "and"

25

Rule Statements

parent(kim,kathy):- mother(kim,kathy).

• Variables ( universal objects ) are used to generalize meaning parent(X,Y):- mother(X,Y).

sibling(X,Y):- mother(M,X), mother(M,Y), father(F,X), father(F,Y).

26

Goal Statements

• Goal statement

– Proposition/ theorem we want to prove or disprove

• Headless Horn clause form student(james).

• Propositions with variables are legal goals father(X,joe).

• Conjunctive propositions are also legal goals cat(lucky) ∩ horse(mr_ed).

27

Inferencing Process

• Query is a Goal

• If a goal is a compound proposition, each of the sub-propositions is a sub-goal

• To prove a goal is true, must find a chain of inference rules and/or facts. For goal Q :

B :- A

C :- B

Q :- P

• Process of proving a sub-goal is called matching, satisfying, or resolution

28

Inferencing Process

• Bottom-up resolution, forward chaining

– Begin with facts and rules of database and attempt to find sequence that leads to goal

– Works well with a large set of possibly correct answers

• Top-down resolution, backward chaining

– Begin with goal and attempt to find sequence that leads to set of facts in database

– Works well with a small set of possibly correct answers

• Prolog implementations use backward chaining

29

Top-Down Example

• (Rule) #1 animal(A) -: cat(A).

• (Fact) #2 cat(lucky).

• Top-down resolution, backward chaining

– Is lucky an animal ?

• Goal: animal(lucky). ???

– Find rule to unify with Goal. Rule #1contains animal(A)

• #1 animal(A) -: cat(A).

– Unify lucky from Goal with variable A in rule #1, yields:

• #1(lucky) animal(lucky) -: cat(lucky).

– Fact: Lucky is a cat

• #2 cat(lucky).

– Combine #1lucky and #2 (add then remove anything on both sides)

• animal(lucky) and cat(lucky) -: cat(lucky).

– Proven.

True if True!

– GOAL: animal(lucky).

30

Bottom Up Example

• (Rule) #1 animal(A) -: cat(A).

• (Fact) #2 cat(lucky).

• Top-down resolution, forward chaining

– Given: lucky is a cat

• #2 cat(lucky)

– Given: If A is a cat , A is an animal

• #1 animal(A) -: cat(A).

– Unify lucky from #2 with variable A in rule #1, yields:

• #1(lucky) animal(lucky) -: cat(lucky).

• #2 cat(lucky).

• #2 animal(lucky) -: True.

– Proven: lucky is an animal

– GOAL: animal(lucky).

31

Inferencing Process Examples

• (Rule) #1 animal(A) -: cat(A).

• (Fact) #2 cat(lucky).

• Negation by failure

– Assume the opposite of what you want to prove

• #3 ¬ animal(lucky)

– To unify with query, Negate Rule

• #1 ¬ cat(A) -: ¬ animal(A)

– unify lucky #3 / A in #1 gives

• #4 ¬ cat(lucky)

– Unify lucky in #4/ lucky in #2

• #5 ¬cat(lucky) ∩ cat(lucky)

– Contradiction in #5 due to #3

– Proves Goal: animal(lucky)

32

Inferencing Process

• When goal has more than one sub-goal can use either

– Depth-first search

• Find a complete proof for the 1 st sub-goal then work on others

– Breadth-first search

• Work on all sub-goals in parallel

33

• Prolog uses depth-first search

– Can be done with fewer computer resources

Inferencing Process (cont.)

• For a goal with multiple sub-goals,

– if fails to show truth of one of sub-goals,

– then reconsider previous sub-goal(s) to find an alternative solution

• backtracking

• Begin search where previous search left off

• Can take lots of time and space

– May find all possible proofs to every sub-goal

34

Simple Arithmetic

• Prolog supports integer variables and integer arithmetic

• is operator

– takes an arithmetic expression as right operand and variable as left operand

– A is B / 10 + C

• Not the same as an assignment statement!

– A temporary binding while processing continues

• There can’t be any unbound variables on the right hand side!

35

Example

speed(ford,100).

speed(chevy,105).

speed(dodge,95).

speed(volvo,80).

time(ford,20).

time(chevy,21).

time(dodge,24).

time(volvo,24).

distance(X,Y) :- speed(X,Speed), time(X,Time),

Y is Speed * Time.

?distance(chevy,D).

D = 2205

36

Trace

• Built-in structure that displays instantiations at each step

• Tracing model of execution - four events:

– Call

• beginning of attempt to satisfy goal

– Exit

• when a goal has been satisfied

– Redo

• when backtrack occurs

– Fail

• when goal fails

37

Example

likes (jake, chocolate).

likes (jake, apricots).

likes (darcie, licorice).

likes (darcie, apricots).

Control flow model for the goal

?trace.

?likes(jake,X),likes(darcie,X).

(1) 1 Call: likes(jake,_0)?

(1) 1 Exit: likes(jake,chocolate)

(2) 1 Call: likes(darcie,chocolate)?

(2) 1 Fail: likes(darcie,chocolate)

(1) 1 Redo: likes(jake,_0)?

(1) 1 Exit: likes(jake,apricots)

(2) 1 Call: likes(darcie,apricots)?

(2) 1 Exit: likes(darcie,apricots)

Call

Exit

Call

Exit likes (jake, X) likes (darcie, X)

Fail

Redo

Fail

Redo

X = apricots

38

List Structure

• List is a sequence of any number of elements

• Elements can be atoms, atomic propositions, or other terms (including other lists)

[apple, prune, grape, kumquat]

[]

• empty list

[X | Y]

• head X and tail Y

• X and Y can each be of any length

39

Example: append

• Definition of append function:

append([], List, List).

append([Head | List_1], List_2, [Head | List_3])

:- append (List_1, List_2, List_3).

1. appending an empty list does nothing

2. if

List_1 + List_2 = List_3 then

Head + List_1 + List_2 = Head + List_3

40

Example: reverse

• Definition of reverse function: reverse([], []).

reverse([Head | Tail], List)

:- reverse (Tail, Result), append (Result, [Head], List).

• Lecture-question:

– How would you express these two rules in plain English?

41

Broader Issues in Prolog

• Resolution order control

– Used for efficiency, but violates declarative style

• The closed-world assumption

– Assume you know everything about the world

• The negation problem

– Is a proposition FALSE if it can’t be proven TRUE?

• Intrinsic efficiency limitations

– Not meant for complex numerical calculations

42

Applications of Logic Programming

• Relational database management systems

43

• Expert and knowledge-based systems

• Natural language processing

• Education

Conclusions

• Advantages:

• Prolog programs based on logic

– More logically organized and written (aid readability and writability)

• Processing is naturally parallel

– Prolog interpreters can take advantage of multiprocessor machines

• Programs are concise

– Fewer lines of code necessary

– Development time is decreased – good for prototyping

44

Download