57
CSE 130 Programming Language Principles & Paradigms Lecture # 26 Logic Programming Languages

Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

Embed Size (px)

Citation preview

Page 1: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Logic Programming Languages

Page 2: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Logic Languages Intro

• Logic languages are programming languages that uses a form of symbolic logic of the logic languages is based directly on symbolic logic notation.

• The design of the logic languages is based on predicate calculus, using propositions– Propositions are logical statements that may or may not be true– Atomic Propositions are the simplest form and consist of compound

terms – Compound Terms are written the form of a mathematical function– Two parts to a compound term: a functor which is the function

symbol that names the relation and an ordered list of parameters• Likes(Bob,Outback)

Page 3: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Propositions

Definition: A proposition is a mapping of members of one set to another, represented as an expression or as a table or as a list of tuples

Examples:man(jake)like(bob, steak)man(joe)

Note: Be careful with a priori knowledge of these rules. Does like(bob,steak) mean that Bob likes steak, that steak likes bob or that Bob and Steak are similar?

Page 4: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Proposition Notation

• negation ¬ a not a• conjunction a ∩ b a and b• disjunction a ∪ b a or b• equivalence a ≡ b a is equivalent to b• implication a ⊃ b a implies b

a ⊂ b b implies a• universal ∀ XP For all X, P is true• existence ∃ X.P There exists a value

of X such that P is true

Note: From top to bottom in precedence, not is highest while existence is lowest

Page 5: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Proposition Examples

A ∩ ¬ B ⊃ D - A and not B implies D

Evaluation (A ∩ (¬ B)) ⊃ D

∀ X.(woman(X) ⊃ human(X))• for every value of X, if X is a woman then X is human

∃ X.(mother(mary,X) ∩ male(X))• there exists a value of X such that mary is the mother of X

and X is male

Page 6: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Clausal Form

• In any language you want simplicity and little redundancy, reg. predicate calculus tends to be redundant– Clausual form is a simple form of propositions– B1 ∪ B2 ∪ … ∪ BN ⊂ A1 ∩ A2 ∩ ... ∩ AM– If all the As are true then at least one B is true– This simplified form doesn’t need anything more

than conjunction / disjunction and implication

Page 7: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Clausal Form Contd.

• Antecedent: right side of proposition• Consequent: left side of proposition because it is the

consequence of the truth of the antecedent• Example: if bob likes fish and a trout is a fish then bob

likes trout:– likes(bob,trout) ⊂ likes(bob,fish) ∩ fish(trout)

– father(louis,al) ∪ father(louis, violet) ⊂ father(al, bob) ∩mother(violet,bob) ∩ grandfather(louis, bob)

• Meaning: if al is bob’s father and violet is bob’s mother and louisis bob’s grandfather then louis is either al’s or violet’s father

Page 8: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Theorem Proving

• Using predicate calculus we express a collection of propositions and then try to infer or test facts against them.

• Resolution: an inference rule that allows inferred propositions to be computed from given propositions.

Example:given:

P1 ⊂ P2 P2 implies P1Q1 ⊂ Q2 Q2 implies Q1

then if P1= Q2T ⊂ P2 P2 implies TQ1 ⊂ T T implies Q2

thereforeQ1 ⊂ P2 P2 implies Q1

Hmmmm….Transitive property

Page 9: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Resolution Example

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

thenwiser(joanne, jake) ⊂ mother(joanne, jake)

• being jake’s mother implies joanne is older• being older implies she is wiser• therefore, being his mother implies she is wiser

Page 10: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Resolution in action

• Step: Terms of the left side of the propositions are anded together

• Step: Terms of right side anded together• Step: Remove terms that appear on both sides• Example:

– father(bob, jake) ∪ mother(bob,jake) ⊂ parent(bob, jake)– grandather(bob,fred) ⊂ father(bob,jake) ∩ father(jake,fred)– father(bob, jake) ∪ mother(bob,jake) ∪ grandather(bob,fred) ⊂

parent(bob, jake) ∩ father(bob,jake) ∩ father(jake,fred)– mother(bob,jake) ∪ grandather(bob,fred) ⊂ parent(bob, jake)

∩ father(jake,fred)

Page 11: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Resolution Notes

• Resolution is more complex than shown, for example what to do when you have variables?– Answer: find values to find a match

• Unification is the process of determining useful values for variable, when instantiate the variables until we find a match (pretty much trial and error with back tracking)

• We use a restricted clausal form to simplify resolution called a Horn Clause.

• Two forms– Empty left called a headless horn clause – state facts

• Stooge(larry), father(bob,jake)– Headed Horn clause (single left side)

• Likes(bob, trout) ⊂ likes(bob, fish) ∩ fish(trout)

Page 12: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Logic Programming Overview

• Essential characteristic is declarative semantics– Simple way to determine meaning of statement which can be

derived from the statement itself– Compare imperative lang even an assignment statement requires

we potentially know the local/global variables, scoping rules, execution up until now (memory values) – everything depends on what has happened were in declarative we can just look at it and know what it means

– Both imperative and FPL are procedural tell the system both what and how you want to do something versus logic which tells what you want done but not necessarily how

• Seems like the idea is that we either treat the computer as really dumb guy who follows orders or one who thinks for himself?

• Downsides? – How would you do a sort in a declarative style language?

Page 13: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Prolog

• Goals: Automated theorem proving, AI (logic and natural language)

• Historical Catalyst: Fifth Generation Computing push in Japan in early 1980s

• Many variants• For purposes of the class I will use SWI Prolog

– http://www.swi-prolog.org/

• Some online resources – http://computing.unn.ac.uk/staff/cgpb4/prologbook/book.html– http://kti.mff.cuni.cz/~bartak/prolog/index.html

Page 14: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

SWI-Prolog in Action

• Prompting for a query with ?-• Normally interactive: get query, print result,

repeat

Welcome to SWI-Prolog (Version 3.4.2)Copyright (c) 1990-2000 University of Amsterdam.Copy policy: GPL-2 (see www.gnu.org)

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

?-

Page 15: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Terms

• Everything in Prolog is built from terms:– Prolog programs– The data manipulated by Prolog programs

• Three kinds of terms:– Constants: integers, real numbers, atoms– Variables– Compound terms

• For comments use %

Page 16: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Constants

• Integer constants: 123• Real constants: 1.23• Atoms:

– A lowercase letter followed by any number of additional letters, digits or underscores: fred

– A sequence of non-alphanumeric characters: *, ., =, @#$

– Plus a few special atoms: []

Page 17: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Atoms Are Not Variables

• An atom can look like an ML or Python variable:– i, size, length

• But an atom is not a variable; it is not bound to anything, never equal to anything else

• Think of atoms as being more like string constants: "i", "size", "length"

Page 18: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Variables

• Any name beginning with an uppercase letter or an underscore, followed by any number of additional letters, digits or underscores: X, Child, Fred, _, _123

• Variables you write will start with an uppercase letter

• Those starting with an underscore, including _, get special treatment

Page 19: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Compound Terms

• An atom followed by a parenthesized, comma-separated list of one or more terms: – x(y,z)– +(1,2)– parent(sandy,seth)– x(Y,x(Y,Z))

• A compound term can look like an ML function call: f(x,y)– Again, this is misleading– Think of them as structured data

Page 20: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Arithmetic

• Well it isn’t “logical” so it isn’t so obvious how to do– You do have the obvious operators: +,-,*, etc. but you won’t get

it do something • ?- 1+1.• ?- X = 1 + 1.

– Force a calculation using “is”• ?- X is 1 + 1.

– Try a test with is• ?- 3 is 1 + 1.• ?- 2 is 1 + 1.

– Arithmetic expressions as not generally evaluated in Prolog. They are parsed and stored as structures. The Unification process does not evaluate them either. Values will be calculated on the right hand side of 'is' and on both sides of '<', '>', '=<', '>=', '=:='.

Page 21: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Boolean Operators

• ; = Or - or maybe or_else• , = And - or maybe and_then• Boolean values: true and fail

– true.– fail.– true;fail.

true,fail.true,true.

• ?- Stooge=larry;Stooge=curly;Stooge=moe.Keep hitting ; (‘or_else’)

• You can use ()’s but you see an interesting action in many situations– ?- (X=1;X=2;X=3),(Y=1;Y=2;Y=3), SUM is X+Y.

Page 22: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Comma Operator

• Notice that the , is used for sequences– To this “AND Then” this etc.– ?- X=1+2, Answer = X. – ?- X=1+2, Answer is X. – ?- X=Y+Z, Z=1, Y=3, ANSWER is X.

Page 23: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Lists

• You do have lists in Prolog– ?- X = [1,2,3,4,5].– ?- [X | Y ] = [1,2,3,4,5].– ?- [Head | Tail ] = [1,2,3,4,5].– ?- [X,Y,Z] = [1,2,3].– ?- [X,Y,Z] = [1,2,3,4].

– Looks like pattern matching, not quite ML style though

Page 24: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Member

• The predicate 'member' is defined in prolog. member(X,Y) is true if Prolog can find a match between X and the elements of Y. – ?- listing(member).– ?- member(1, [1,2,3,4]).

?- member(shemp, [larry,curly,moe]).?- member(Stooge, [larry,curly,moe]).% use ; after each match

Page 25: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Loop?

• Remember FPL we didn’t have traditional loops, but we made do with recursion, what about here?

• ?- member(X,[1,2,3,4,5,6,7]), X mod 2 =:= 1.

• Of course this isn’t really what you are supposed to be doing but you can see that you can probably try to act like a C/Pascal/Fortran programmer in just about anything, though I guess you could try to go the other direction as well.

Page 26: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Prolog Basics

• Facts: headed or headless Horn clauses that are interpreted as unconditional assertions– Examples:

• female(shelley). -> shelley is female• father(bill, jake). -> bill is jake’s father or jake is bill’s father or

they have the same father (depends on usage)

• Rules: headed Horn clauses are that are interpreted as conditional assertions (if-then)– Examples:

• parent(X, Y) :- mother(X, Y).• parent(X, Y) :- father(X, Y).• grandparent(X, Z) :- parent(X, Y), parent(Y, Z).

– X is the grandparent of Z if X is the parent of one of Z’s parents.

Page 27: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Prolog Basics

• Goal Statements: in query mode, propositions are goals or queries to be proven true or not proven true (Prolog does not prove false).

• Example: father(bill, jake). returns yesfather(bill, bob). returns nofather(X, jake). returns jake

• Subgoals: goals attempted by the inference engine to find a way to prove the main goal

• Example: father(bob). man(X) :- father(X).Then

man(X). set X=bob and tries to get from father(bob) to man(bob); then returns bob

Page 28: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Prolog Basics: Unification

• Pattern-matching using Prolog terms• Two terms unify if there is some way of

binding their variables that makes them identical

• For instance, parent(sandy,Child) and parent(sandy,seth) unified by binding the variable Child to the atom seth

Page 29: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

The Prolog Database

• So we see that Prolog language system maintains a collection of facts and rules of inference– Somewhat like an internal database

• A Prolog program is just a set of data for this database

• The simplest kind of thing in the database is a fact: a term followed by a period

Page 30: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Example

• A Prolog program of six facts• Defining a predicate parent of arity 2• We would naturally interpret these as facts

about families: Kim is the parent of Holly and so on

parent(kim,holly).parent(margaret,kim).parent(margaret,kent).parent(esther,margaret).parent(herbert,margaret).parent(herbert,jean).

Page 31: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

The consult Predicate

• Predefined predicate to read a program from a file into the database

• File relations (or relations.pl) contains our parent facts

• Note issue with Perl extension collision

?- consult(relations).% relations compiled 0.00 sec, 0 bytes

Yes?-

Page 32: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Simple Queries

• A query asks the language system to prove something

• The answer will be Yes or No• (Some queries, like consult, are executed only

for their side-effects)

?- parent(margaret,kent).

Yes?- parent(fred,pebbles).

No?-

Page 33: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Final Period

• Queries can take multiple lines• If you forget the final period, Prolog prompts for

more input with |

?- parent(margaret,kent)| .

Yes?-

Page 34: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Queries With Variables

• Any term can appear as a query, including a term with variables

• The Prolog system shows the bindings necessary to prove the query

?- parent(P,jean).

P = herbert

Yes?- parent(P,esther).

No

Here, it waits for input. We hit Enter to make it proceed.

Page 35: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Flexibility

• Normally, variables can appear in any or all positions in a query:– parent(Parent,jean)– parent(esther,Child)– parent(Parent,Child)– parent(Person,Person)

Page 36: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Conjunctions

• A conjunctive query has a list of query terms separated by commas

• The Prolog system tries prove them all (using a single set of bindings)

?- parent(margaret,X), parent(X,holly).

X = kim

Yes

Page 37: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Multiple Solutions

• There might be more than one way to prove the query

• By typing ; rather than Enter, you ask the Prolog system to find more, once out it let’s you know you have no others

?- parent(margaret,Child).

Child = kim ;

Child = kent ;

No

Page 38: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

?- parent(Parent,kim), parent(Grandparent,Parent).

Parent = margaretGrandparent = esther ;

Parent = margaretGrandparent = herbert ;

No?- parent(esther,Child),| parent(Child,Grandchild),| parent(Grandchild,GreatGrandchild).

Child = margaretGrandchild = kimGreatGrandchild = holly

Yes

Page 39: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

The Need For Rules

• Previous example had a lengthy query for great-grandchildren of Esther

• It would be nicer to query directly:greatgrandparent(esther,GGC)

• But we do not want to add separate facts of that form to the database

• The relation should follow from the parentrelation already defined

Page 40: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

A Rule

• A rule says how to prove something: to prove the head, prove the conditions

• To prove greatgrandparent(GGP,GGC), find some GP and P for which you can prove parent(GGP,GP), then parent(GP,P) and then finally parent(P,GGC)

greatgrandparent(GGP,GGC) :-parent(GGP,GP), parent(GP,P),parent(P,GGC). conditions

head

Page 41: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

A Program With The Rule

• A program consists of a list of clauses• A clause is either a fact or a rule, and ends with

a period

parent(kim,holly).parent(margaret,kim).parent(margaret,kent).parent(esther,margaret).parent(herbert,margaret).parent(herbert,jean).greatgrandparent(GGP,GGC) :-

parent(GGP,GP), parent(GP,P), parent(P,GGC).

Page 42: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Example

• This shows the initial query and final result• Internally, there are intermediate goals:

– The first goal is the initial query– The next is what remains to be proved after

transforming the first goal using one of the clauses (in this case, the greatgrandparent rule)

– And so on, until nothing remains to be proved

?- greatgrandparent(esther,GreatGrandchild).

GreatGrandchild = holly

Yes

Page 43: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

greatgrandparent(esther,GreatGrandchild)

1. parent(kim,holly).2. parent(margaret,kim).3. parent(margaret,kent).4. parent(esther,margaret).5. parent(herbert,margaret).6. parent(herbert,jean).7. greatgrandparent(GGP,GGC) :-

parent(GGP,GP), parent(GP,P), parent(P,GGC).

parent(esther,GP), parent(GP,P), parent(P,GreatGrandchild)

parent(margaret,P), parent(P,GreatGrandchild)

parent(kim,GreatGrandchild)

Clause 7, binding GGP to esther and GGC to GreatGrandChild

Clause 4, binding GP to margaret

Clause 2, binding P to kim

Clause 1, binding GreatGrandchild to holly

Page 44: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Inference Process

• Bottom-up resolution: also called forward-chaining. The system begins with facts and rules and attempts to find a sequence of matches that lead to the goal.

• Top-down resolution: also called backward-chaining. The system begins with the goal and attempts to find a sequence of matching propositions that lead to some set of facts.– Prolog systems use backward chaining– During resolution if you have multiple substitutions do you do

a depth first or a breadth first search?• Backtracking: backing up and trying a different

subgoal when a dead-end is reached

Page 45: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Backtracking

• Given male(X), parent(X, shelley) we need to find a male X who is a parent of shelley, if you assume first rule evaluated depth first you see male(mike) then test parent(mike,shelley) and if you fail you back track and do the parent test over and over

• Interestingly if you do the order differently parent(X,shelley), male(X) you can really improve things! – Didn’t we just blow it there? What not How!?

Page 46: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Tracing

• You can see the resolution method that Prolog takes using trace.

• Consider the Prolog statements and rule here

speed(ford, 100).speed(chevy, 105).speed(bmw, 140).speed(acura, 150).time(ford,20).time(chevy,21).time(bmw,24).time(acura,24).distance(X,Y) :- speed(X,Speed) , time(X,Time), Y is Speed * Time.

Page 47: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Trace Contd.

?- trace.

Yes[trace] ?- distance(acura,AcuraDistance).

Call: (6) distance(acura, _G284) ? creepCall: (7) speed(acura, _G338) ? creepExit: (7) speed(acura, 150) ? creepCall: (7) time(acura, _G338) ? creepExit: (7) time(acura, 24) ? creep

^ Call: (7) _G284 is 150*24 ? creep^ Exit: (7) 3600 is 150*24 ? creep

Exit: (6) distance(acura, 3600) ? creep

Page 48: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Trace Backtrack Example

• Given – likes(jake, chocolate).– likes(jake, apricots).– likes(darcie, licorice).– likes(darcie, apricots).

• Then do a query like likes(jake,X),likes(darcie,X).– You will certainly get backtrack in the chocolate case

see the next slide to get the idea

Page 49: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Backtrack Trace Example

–?- consult(likes).–% likes compiled 0.00 sec, 860 bytes

–Yes–?- trace.

–Yes–[trace] ?- likes(jake,X),likes(darcie,X).– Call: (7) likes(jake, _G284) ? creep– Exit: (7) likes(jake, chocolate) ? creep– Call: (7) likes(darcie, chocolate) ? creep– Fail: (7) likes(darcie, chocolate) ? creep– Redo: (7) likes(jake, _G284) ? creep– Exit: (7) likes(jake, apricots) ? creep– Call: (7) likes(darcie, apricots) ? creep– Exit: (7) likes(darcie, apricots) ? creep

–X = apricots

Page 50: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Prolog Problems

• Resolution order control– Prolog allows us to control the ordering of pattern

matching, if we didn’t efficiency goes bye-bye in some cases

– Not only can we have slow running code but we can have runaway code as well

– The cut operator (!) can be used to tell Prolog not keep on trying something

• A,B,!,C,D– If a and b are good but c fails the whole process is cut off

and fails, so anytime C fails we are done

Page 51: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Prolog Problems

• The ability to tamper with control flow in Prolog is a huge problem because it pretty much kills one of the important aspects of a declarative logical lang like Prolog – that you don’t specify this type of information– Trade-off for efficiency here

Page 52: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Closed/World Assumption & Negation

• It really doesn’t know something is false– If you can’t prove it true it must be false (really just not true)

• Negation problem is related– Parent(bill,jake).– Parent(bill,shelley).– Sibling(X,Y) :- (Parent(M,X), Parent(M,Y)).

• Query: Sibling(X,Y)– X=JAKE Y=JAKE– You need to add a condition that X != Y but that isn’t so easy

since you have to have rules to show pairs of atoms are not the same in this case you have only two things but what happens if you have a lot of atoms!

• Solution? Sibling(x,y) :- Parent(M,X), Parent(M,Y), not(X=Y)

Page 53: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Negation Contd.

• What about not(not(some_goal)). -> some_goal– This would be what would happen if this were a

logical not• Logical not cannot be part of Prolog is because

of the Horn clause:– A :- B1 ∩ B2 ∩ … ∩ BN– If all B are true then A is true but regardless of B you

can conclude A is false. – From positive logic you can only conclude positive

logic, thus the use of the horn clause form prevents negative conclusions

Page 54: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Intrinsic Problems

• Sorting seems to be unreasonably hard– Sorted([]).– Sorted([x]).– Sorget([x,y | list]) :- x < = y, sorted([y | list]).

• However does this sort it or does it just put the choices in random orders until it is sorted!

Page 55: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Can’t finish without…

• Factorial!– factorial(0,1). – factorial(N,F) :- N>0, N1 is N-1, factorial(N1,F1), F is N * F1.

• We could also do it alternatively with an accumulator and get a tail recursive solution– factorial(0,F,F). – factorial(N,A,F) :- N > 0, A1 is N*A, N1 is N -1, factorial(N1,A1,F).

• For this version, use the following type of a goal: – ?- factorial(5,1,F). F=120

Hey aren’t we supposed to not get into all this how does it work underneath in LPL and FPL, we keep on doing that!

Page 56: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Logic Programming vs. Prolog

• Logic Programming: Nondeterministic– Arbitrarily choose rule to expand first– Arbitrarily choose subgoal to expand first– Results don’t depend on rule and subgoal ordering

• Prolog: Deterministic– Expand first rule first– Explore first subgoal first– Results may depend on ordering– Prolog is not purely declarative, it has that aspect of just making

logical assertions but also each rule can though of an instruction that says how to prove a goal within the framework of Prolog’s overall approach to proving things

Page 57: Logic Programming Languages - PINTclasses.pint.com/cse130/lecture26/big.pdf · Logic Programming Languages. ... – Atomic Propositions are the simplest form and consist of compound

CSE 130 Programming Language Principles & Paradigms Lecture # 26

Declarative Advantages

• Imperative languages are doomed to subtle side-effects and interdependencies (or so the argument goes)

• Simpler declarative semantics makes it easier to develop and maintain correct programs

• Higher-level, more like automatic programming: describe the problem and have the computer write the program

• Disadvantages – Performance– Ease/Difficulty of doing certain kind of problems

• Limited domain of usefulness