Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional...

55
Logic Programming Tasanawan Soonklang

Transcript of Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional...

Page 1: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Logic ProgrammingTasanawan Soonklang

Page 2: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Programming paradigms

• Imperative• Object-oriented• Functional• Logic

Procedural programming

Non-procedural programmingor Declarative programming

Page 3: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Non-procedural programming

• So far programming has been algorithmic.• Procedural: statements (C, C++, FORTRAN)• Functional: expressions (Postscript, ML, LISP)• Now declarative languages – logic programming• Programs do not state now a result is to be

computed, but rather the form of the result

Page 4: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Title

• Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus et magna. Fusce sed sem sed magna suscipit egestas.

• Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus et magna. Fusce sed sem sed magna suscipit egestas.

• Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus et magna. Fusce sed sem sed magna suscipit egestas.

Logic Programming

Refers loosely to the use of

• facts and rules to represent information.

• deduction to answer queries.

algorithm = logic + control

• We supply logic part, Programming Language supplies control part.

Page 5: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Imperative vs. Logic

int fact(int n){

int i = 1;for (int j = n; j>1; --j)

i = i * j;return I;

}

fact(0,1). fact(N,F) :-

N>0, N1 is N-1, fact(N1,F1),

F is N * F1.

fact(0,1) :- true.

?- fact(3,W). W=6

Click to see the animation

Page 6: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Computing• Deals with relations rather than

functions. • Search facts in order looking for

matches.• Use rules for a logical inferencing

process to produce results

Premiseo programming with relations is more

flexible than with functions.

Page 7: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Reserve fight

Information• fight number• from city

• to city• departure time• arrival time

fight ( fight_number, from_city, to_city, departure_time, arrival_time )

fight(fight_number, “Bangkok”, “Los Angeles”, departure_time, arrival_time)

fight(fight1,“Bangkok”, X, depart1,arrival1), fight(fight2,X,“Los Angeles”,depart2,arrival2), depart2 >= arrive1+30

Page 8: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Logic

Propositional logicZeroth-order logic

Predicate logicFirst-order logic

Proving theorems

Page 9: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Proposition

• A declarative sentence. e.g. Woff is a dog.

e.g. Jim is the husband of Mary.

• A proposition is represented by a logical symbol.

P: Woff is a dog. R: Jim is the husband of Mary.

Page 10: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Proposition

• A logical statement that may or may not be true

• Consists of – objects– relationships of objects to each other

Page 11: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Symbolic Logic

• Logic which can be used for the basic needs of formal logic:– Express propositions– Express relationships between propositions– Describe how new propositions can be

inferred from other propositions

• Particular form of symbolic logic used for logic programming called predicate calculus

Page 12: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Object Representation

• Objects in propositions are represented by simple terms: either constants or variables

• Constant - a symbol that represents an object

• Variable - a symbol that can represent different objects at different times– Different from variables in imperative

languages

Page 13: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Compound Terms

• Atomic propositions consist of compound terms

• Compound term: one element of a mathematical relation, written like a mathematical function– Mathematical function is a mapping– Can be written as a table

Page 14: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Parts of a Compound Term

• Compound term composed of– Functor - function symbol that names

the relationship– Ordered list of parameters (tuple)

• Examples:

student(jon)

like(seth,OSX)

like(nick,windows)

like(jim,linux)

Page 15: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Forms of a Proposition

• Fact - proposition is assumed to be true

• Query - truth of proposition is to be determined

• Compound proposition:– Have two or more atomic propositions– Propositions are connected by operators

Page 16: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Logical Operators

Name Symbol Example Meaning

negation P not P

conjunction P Q P and Q

disjunction P Q P or Q

equivalence P Q P is equivalent to Q

implication

P QP Q

P implies QQ implies P

Page 17: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

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

Page 18: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Forms of a Proposition

• Too many ways to state the same thing• Use a standard form for propositions• Clausal form

– B1 B2 … Bn A1 A2 … Am

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

• Antecedent - right side• Consequent - left side

Page 19: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Predicate Calculus and Proving Theorems

• A use of propositions is to discover new theorems that can be inferred from known axioms and theorems

• Resolution - an inference principle that allows inferred propositions to be computed from given propositions

Page 20: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Resolution

• Unification: finding values for variables in propositions that allows matching process to succeed

• Instantiation: assigning temporary values to variables to allow unification to succeed

• After instantiating a variable with a value, if matching fails, may need to backtrack and instantiate with a different value

Page 21: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Proof by Contradiction

• Hypotheses: a set of pertinent propositions

• Goal: negation of theorem stated as a proposition

• Theorem is proved by finding an inconsistency

Page 22: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Theorem Proving

• Basis for logic programming• When propositions used for resolution,

only restricted form can be used• Horn clause - can have only two forms

– Headed: single atomic proposition on left side– Headless: empty left side (used to state facts)

• Most propositions can be stated as Horn clauses

Page 23: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

First Order Logic (FOL)

• Allows the following to be modeled– Objects– properties of objects– relations among the objects

• Like propositional logic, FOL has sentences

• Additionally it has terms which allow the representation of objects

Page 24: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Terms

• A term is a logical expression which refers to an object.

• Elements of a term– Constant Symbols e.g. A, B, John– Predicate Symbols :- refer to a relation – Function Symbols :- refer to a relation which

is a function

Page 25: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Sentences

• Atomic Sentences - state a facte.g. company(gordon).e.g. location(gordon,usa).

• Complex Sentences - formed from:– atomic sentences– logical connectives– quantifiers

Page 26: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Quantifier

• Universal Quantifier ()– All cats are mammals.– x (Cat(x) Mammal(x))

• Extensile Quantifier ()– Spot has a sister who is a cat.– x ( Sister(x,Spot) Cat(x))

Page 27: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Prolog

• Programming Logic• Prolog is based upon First Order Logic (FOL)• A Prolog program consists of a Knowledge Base

composed of:

– facts– rules

• All facts and rules must be expressed as Horn Clauses

Page 28: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Applications

• Relational DBMS• Artificial Intelligence

– Expert systems– Natural language processing– Automatic theorem proving

Page 29: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

The Origins of Prolog

• University of Aix-Marseille– Natural language processing

• University of Edinburgh– Automated theorem proving

Page 30: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Syntax rules

• Predicates (functors) must start with lower-case letter.

• Constants begin with a lower-case letter or number.

• Variables begin with an upper-case letter or an (_).

Page 31: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Syntax rules

• All clauses have a head and a body.• head :- body.• The symbol :- is read if• All sentences (clauses) must end

with a period.

Page 32: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Edinburgh syntax

• Term: a constant, variable, or structure• Constant: an atom or an integer• Atom: symbolic value of Prolog• Atom consists of either:

– a string of letters, digits, and underscores beginning with a lowercase letter

– a string of printable ASCII characters delimited by apostrophes

Page 33: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Edinburgh syntax

• Variable: any string of letters, digits, and underscores beginning with an uppercase letter

• Instantiation: binding of a variable to a value– Lasts only as long as it takes to satisfy one

complete goal

• Structure: represents atomic proposition

functor(parameter list)

Page 34: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Fact statements

• Used for the hypotheses• Headless Horn clausesfemale(shelley).

male(bill).

father(bill,jake).

Page 35: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Rule statements

• Used for the hypotheses• Headed Horn clause• Right side: body (if part)

– May be single term or conjunction

• Left side: Head (then part)– Must be single term

• Conjunction: multiple terms separated by logical AND operations (implied)

Page 36: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Rule statements

ancestor(mary,shelley):-mother(mary,shelley).

• Can use variables (universal objects) to generalize meaning:parent(X,Y):- mother(X,Y).

parent(X,Y):- father(X,Y).

grandparent(X,Z):-

parent(X,Y), parent(Y,Z).

sibling(X,Y):- mother(M,X), mother(M,Y),

father(F,X), father(F,Y).

Page 37: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Goal statements

• For theorem proving, theorem is in form of proposition that we want system to prove or disprove

• Same format as headless Hornman(fred)

• Conjunctive propositions and propositions with variables also legal goalsfather(X,mike)

Page 38: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Simple Prolog facts

• A database of facts:

inclass(john, cmsc330).

inclass(mary, cmsc330).

inclass(george, cmsc330).

inclass(jennifer, cmsc330).

inclass(john, cmsc311).

inclass(george, cmsc420).

inclass(susan, cmsc420).

• Queries: Prolog can confirm these facts:

?-inclass(john, cmsc330). “yes” ?- inclass(susan, cmsc420). “yes”

?- inclass(susan, cmsc330). “no”

Page 39: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Simple Prolog facts & rules

• A database of facts:

dog(woff).

barks(woff).

barks(spot).

wags_tail(woff).

• A database of rules:

dog(X) :- barks(X), wags_tail(X).

• Queries:

?- dog(woff) => yes

?- dog(spot) => no

?- dog(Y) => Y = woff

Page 40: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Inferencing Process

• Facts and rules are Knowledge base (KB)• Prolog uses a goal directed search of the KB• Depth first search is used• Query clauses are used as goals and searched left

to right• KB clauses are searched in the order they occur in

the KB• Goals are matched to the head of clauses• Terms must unify based upon variable

substitution before they match

Page 41: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Inferencing Process

• Queries are called goals• If a goal is a compound proposition, each of the

facts is a subgoal• 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 subgoal called matching, satisfying, or resolution

Page 42: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Approaches

• 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

Page 43: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Subgoal Strategies

• When goal has more than one subgoal, can use either– Depth-first search: find a complete proof for

the first subgoal before working on others– Breadth-first search: work on all subgoals in

parallel

• Prolog uses depth-first search– Can be done with fewer computer resources

Page 44: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Backtracking

• With a goal with multiple subgoals, if fail to show truth of one of subgoals, reconsider previous subgoal to find an alternative solution: backtracking

• Begin search where previous search left off• Can take lots of time and space because

may find all possible proofs to every subgoal

Page 45: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Unification• Can use a form of substitution called unification

to derive other relationships.

inclass(susan, X). – Prolog searches database and responds “X=cmsc420.”

– Hitting Enter key, Prolog says “No” since no other fact.

inclass(john, Y).– Prolog has following responses:“Y=cmsc330.”“Y=cmsc311.”“no.”

Page 46: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Unification• Can define more complex queries:

takingboth(X):-inclass(X, cmsc330),inclass(X, cmsc311).

?-takingboth(john)yes?-takingboth(Y)Y=john;no

Page 47: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Simple Arithmetic

• Prolog supports integer variables and integer arithmetic

• is operator: takes an arithmetic expression as right operand and variable as left operandA is B / 17 + C

• Not the same as an assignment statement!

Page 48: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Data

• Data:Integers: 1, 2, 3, 4Reals: 1.2, 3.4, 6.7Strings: 'abc', '123'Facts: lower case namesVariables: Upper case namesLists: [a, b, c, d]

Page 49: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

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.

Page 50: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

List Structures

• basic data 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)

Page 51: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Append Example

append([], List, List).

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

append (List_1, List_2, List_3).

Page 52: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Reverse Example

reverse([], []).

reverse([Head | Tail], List) :-

reverse (Tail, Result),

append (Result, [Head], List).

Page 53: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Reverse Example

reverse([], []).

reverse([Head | Tail], List) :-

reverse (Tail, Result),

append (Result, [Head], List).

Page 54: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

Summary

So Prolog is:1. A database of facts.2. A database of queries.3. A sequential execution model:

Search facts in order looking for matches. If failure, back up to last match and try next entry in database.

Because of this last item, Prolog is not truly declarative. It does have an algorithmic execution model buried in structure of database.

Page 55: Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.

The End.