Introduction to Prolog Proof Procedures. Exercise parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad,

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

1. An Overview of Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 16- Prolog.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
Prolog OR (disjunction) “;” is same as a logical OR “;” is same as a logical OR It is also equivalent to using separate clauses... It is also equivalent.
Prolog Programming for Artificial Intelligence Three edition 2001
Prolog programming Introduction to Prolog (part4) CS 370 – CS461.
Prolog programming Introduction to Prolog
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for Ch.1 of Bratko For CSCE 580 Sp03 Marco Valtorta.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Prolog programming Introduction to Prolog CS370d - CS 461.
Formal Models of Computation Part II The Logic Model
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things.
CS 321 Programming Languages and Compilers Prolog part 2.
COSC 2P93 Logic Programming Instructor: Brian Ross Instructor: Brian Ross Texts: Texts: Prolog Programming for Artificial Intelligence,4e, Ivan Bratko,
The Evolution of Programming Languages Day 3 Lecturer: Xiao Jia The Evolution of PLs1.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
Flow of Control Chapter 5. Flow of Control n What order computer uses to get answers –sub-goal ordering –clause ordering n Prolog flow-of-control –sequence.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
For Wednesday No new reading Prolog handout 2 Chapter 9, exercise 4.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Introduction to Prolog Facts, Questions & Rules Atoms & Variables.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Programming Languages Third Edition Chapter 4 Logic Programming.
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
Lists in Prolog Sections 3.1, 3.2. Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling),
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Introduction to Prolog What if all we could do was to make assertions (statements) about the world? Example: family tree parent(linda, simon).male(simon).
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
Logic Programming Lecture 2: Unification and proof search.
The portion of a Prolog interpreter that executes queries (goals) is known as the inference engine. An inference engine is a kind of theorem prover, using.
COSC 2P93 Prolog: Debugging
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog programming Introduction to Prolog
Tests, Backtracking, and Recursion
Prolog programming Introduction to Prolog (part2)
Prolog Primarily for Symbolic (nonnumeric) Computation
Chapter 11 :: Logic Languages
Prolog programming Introduction to Prolog
Prolog programming Introduction to Prolog (part3)
Chapter 12 :: Logic Languages
Prolog programming Introduction to Prolog (part4)
How To Think Like a Prolog ?
Presentation transcript:

Introduction to Prolog Proof Procedures

Exercise parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad, cathy). n Who are Alex’s aunts? n Write a definition of aunt/2 –aunt(Aunt, NorN).

Recursion n Predicates may be recursive –Needs a base case – fact or non-recursive rule ancestor(Anc, Desc) :- parent(Anc, Desc). ancestor(Anc, Desc) :- parent(Parent, Desc), ancestor(Anc, Parent). n Usually write base case first – more later Base case: your parent is your ancestor Recursive case: an ancestor of your parent is your ancestor

Rule Diagrams n Each diagram a clause n Note recursion –Needs base case D A parent ancestor D A P parent ancestor

Alex’s Ancestors parent(mark, alex). parent(bob, mark). parent(franklin, bob). parent(garvie, franklin). ancestor(A, D) :- parent(A, D). ancestor(A, D) :- parent(P, D), ancestor(A, P). ?- ancestor(Who, alex). Who = mark ; Who = bob ; Who = franklin ; Who = garvie ; no

How Prolog Answers Questions n Prolog answers questions in order –first question first, then second, then third n Prolog uses facts/rules in order –first fact/rule first, then second, then third n Prolog uses recursion for rules –body = set of rules –answered in order to get answer for head

Asking About Grandparents ?- grandparent(tom, GC). n This is the main question –AKA “top-level goal” n The question Prolog prints answers to n Prolog needs to look for answers n Starts looking for clauses for grandparent/2

Goal #1 looking for grandparent(tom, GC) grandparent(X, Z) :-match: X=tom, Z=GC parent(X, Y), parent(Y, Z). creates sub-goals: parent(tom, Y), parent(Y, GC). Database facts & rules Commentary

Sub-Goaling n Found a rule for grandfather/2 –head of rule matches the question n Replace grandfather question with rule body –variables filled in from main question n Sub-questions answered in order –parent(tom, Y) first –parent(Y, GC) second

Sub-Goal #1 looking for parent(tom, Y) parent(pam, bob).pam  tom parent(tom, bob).match: bob = Y sub-goal #1 succeeds sub-goal #2 next parent(Y, GC) becomes parent(bob, GC)

Sub-Goal #2 looking for parent(bob, GC) parent(pam, bob).pam  bob parent(tom, bob).tom  bob parent(tom, liz).tom  bob parent(bob, ann).match: ann = GC sub-goal #2 succeeds ends sub-goaling, return to main question

Goal #1 (review) looking for grandparent(tom, GC) grandparent(X, Z) :-match: X=tom, Z=GC parent(X, Y),matched: Y=bob parent(Y, Z).matched: Z=GC=ann main goal succeeds

Top-Level ?- grandparent(tom, GC). GC = ann n Ann is one of tom’s grandchildren n Prolog waiting for user action n Let’s type ; –see if there are more answers

More Answers n If you ask for more answers, Prolog “backs up” and tries again –called “backtracking” n Go back to last question asked & see if it has a different answer –last question was parent(bob, GC) –answer was GC = ann

Sub-Goal #2: Backtracking parent(bob, pat).2 nd match: pat = GC sub-goal #2 succeeds again looking for parent(bob, GC) parent(pam, bob).pam  bob parent(tom, bob).tom  bob parent(tom, liz).tom  bob parent(bob, ann).1 st match: ann = GC

Goal #1: 2 nd Solution looking for grandparent(tom, GC) grandparent(X, Z) :-match: X=tom, Z=GC parent(X, Y),matched: Y=bob parent(Y, Z).matched: Z=GC=pat main goal succeeds again

Top-Level ?- grandparent(tom, GC). GC = ann ; GC = pat n Let’s see if there are any more answers n Once again back to the last question

Sub-Goal #2: Backtracking parent(pat, jim).pat  bob no more answers looking for parent(bob, GC) parent(pam, bob).pam  bob parent(tom, bob).tom  bob parent(tom, liz).tom  bob parent(bob, ann).1 st match: ann = GC parent(bob, pat).2 nd match: pat = GC

Backtracking n 2 nd sub-goal has no more answers n But Prolog’s not done –maybe the 1 st sub-goal has more answers

Sub-Goal #1: Backtracking parent(tom, liz).2 nd match: liz = Y sub-goal #1 succeeds again redo sub-goal #2 using new binding parent(liz, GC) looking for parent(tom, Y) parent(pam, bob).pam  tom parent(tom, bob).1 st match: bob = Y

Sub-Goal #2 looking for parent(liz, GC) parent(pam, bob).pam  liz parent(tom, bob).tom  liz parent(tom, liz).tom  liz parent(bob, ann).bob  liz parent(bob, pat).bob  liz parent(pat, jim).pat  liz no answers

Backtracking n Liz has no children at all –2 nd answer for 1 st sub-goal got us nothing n But don’t give up –maybe Tom has more children

Sub-Goal #1: Backtracking parent(bob, ann).bob  tom parent(bob, pat).bob  tom parent(pat, jim).pat  tom no more answers looking for parent(tom, Y) parent(pam, bob).pam  tom parent(tom, bob).1 st match: bob = Y parent(tom, liz).2 nd match: liz = Y

Backtracking n 1 st sub-goal had no more answers n Prolog returns to main goal looking for grandparent(tom, GC) grandparent(X, Z) :-1 st answer parent(X, Y), parent(Y, Z). no more facts or rules for grandparent/2

Top-Level n No more answers –and nowhere else to look ?- grandparent(tom, GC). GC = ann ; GC = pat ; No?-

Prolog Normal Flow of Control n Things done in sequence n If reach an impasse… –no answer found or no more answers found n …backtrack = back up –just follow code in reverse n “Depth first search” n There are ways to modify this – more later

Program Tracing n You can have Prolog trace its work ?- trace, grandparent(tom, GC). n Shows you what it’s doing as it does it Call: (7) grandparent(tom, _G301) ? creep Call: (8) parent(tom, _G371) ? creep Exit: (8) parent(tom, bob) ? creep Call: (8) parent(bob, _G301) ? just press Enter

Tracing “Ports” n Call port = when question first asked n Exit port = answer found n Redo port = looking for more answers n Fail port = no (more) answers found n (Number) = how deep –first 6 levels belong to the system

Trace of grandparent/2 ?- grandparent(tom, GC). call grandparent(tom, _G_1) call parent(tom, _G_2) exit parent(tom, bob) call parent(bob, _G_3) exit parent(bob, ann) exit grandparent(tom, ann) GC = ann ; find tom’s kids find bob’s kids

Trace of grandparent/2 redo grandparent(tom, ann) redo parent(bob, ann) exit parent(bob, pat) exit grandparent(tom, pat) GC = pat ; more of bob’s kids

Trace of grandparent/2 redo grandparent(tom, pat) redo parent(bob, pat) fail parent(bob, _G_3) redo parent(tom, bob) exit parent(tom, liz) call parent(liz, _G_4) fail parent(liz, _G_4) redo parent(tom, _G_2) fail parent(tom, _G_2) redo grandparent(tom, _G_1) fail grandparent(tom, _G_1) more of bob’s kids more of tom’s kids find liz’s kids more of tom’s kids more rules for grandparents?

Trace Prolog’s Work… mother(di, alex). mother(di, zachary). father(ralph, di). mother(esther, di). parent(P, C) :- father(P, C). parent(P, C) :- mother(P, C). ?- parent(ralph, Who), parent(Who, GC). (show all answers) use call, exit, redo, fail

Proof and Recursion n All variables are local n Different clauses = different variables n Recursive calls: different variables ancestor(A, D) :- parent(A, P), ancestor(P, D). ?- ancestor(tom, jim). [A 1 = tom, D 1 = jim, P 1 = bob] ?- ancestor(bob, jim) [A 2 = bob, D 2 = jim, P 2 = ann] ?- ancestor(ann, jim) [A 3 = ann, D 3 = jim] (fails)

Tracing Actions n Press enter/space to “creep” thru code n Press s to skip sub-goals for this question –when you’re not interested in how it works n Press n to stop tracing –or use notrace/0 at the prompt n Press ? for more options –find out about spy points

Infinite Loops n Because of Prolog’s simple-minded control, it’s easy to write infinite loops ancestor(X, Z) :- ancestor(X, Y), parent(Y, Z). ancestor(X, Z) :- parent(X, Z). ?- ancestor(tom, D). If Z is a variable when this matches Then 1 st sub-goal same as main goal Makes no progress Infinite loop May depend on exact question asked

Avoiding Infinite Loops n Write base case first –Prolog looks for non-recursive answers ancestor(A, D) :- parent(A, D). n Leave recursion to end of recursive body –Gather as much information as you can before you recur ancestor(A, D) :- parent(P, D), ancestor(A, P).

Exercise n Rewrite the predicate below to make it less likely to go into an infinite loop something(A, B) :- something(A, C), elfin(C, B, no). something(A, B) :- basic(B, C, A), multiple(C, B).

Brothers and Sisters n Sister = same parent(s) and female sister(Sister, Person) :- parent(Parent, Sister), parent(Parent, Person), female(Sister). n Note: better variable names than in the text n Note how we say they have the same parent –just use same variable for “both” parents

Tracing Sister/2 ?- sister(Who, pat). parent(Parent,Who), parent(Parent,pat), female(Who) parent(pam,bob), parent(pam,pat) (fails) parent(tom,bob), parent(tom,pat) (fails) parent(tom,liz), parent(tom,pat) (fails) parent(bob,ann), parent(bob,pat), female(ann). Who = ann ;

Tracing Sister/2 ?- sister(Who, pat). parent(Parent,Who), parent(Parent,pat), female(Who) parent(pam,bob), parent(pam,pat) (fails) parent(tom,bob), parent(tom,pat) (fails) parent(tom,liz), parent(liz,pat) (fails) parent(bob,ann), parent(bob,pat), female(ann). Who = ann ; parent(bob,pat), parent(bob,pat), female(pat). Who = pat

Pat Is Not Her Own Sister n Sister to Person must be a different person –how can we say that? –text uses different/2 –not actually defined different(X, Y) :- X \= Y. –the “not equals” predicate/operator –means “could not be the same”

Sisters (Revised) n Sister = same parent(s), female, different sister(Sister, Person) :- parent(Parent, Sister), parent(Parent, Person), female(Sister), different(Sister, Person). n Last line could just use \= Sister \= Person.

Tracing Revised Sister/2 ?- sister(W, pat). parent(P,W), parent(P,pat), female(W),W \= pat parent(pam,bob), parent(pam,pat) (fails) parent(tom,bob), parent(tom,pat) (fails) parent(tom,liz), parent(liz,pat) (fails) parent(bob,ann),parent(bob,pat),female(ann),ann\=pat. Who = ann ; parent(bob,pat),parent(bob,pat),female(pat),pat\=pat (fails). parent(pat,jim), parent(pat,pat) (fails)

Inequality & Variables n \= means “could not be the same” n But a variable can be the same as anything n So, \= fails if either argument is unbound ?- Who \= pat. No ?- Who \= Whom. No ?- Who = bob, Who \= pat. Who = bob “unbound” means: doesn’t have a value yet =/2 means “they are the same” not usually needed

Inequality and Proof n Recall that sub-goals are checked in order n different/2 called last in sister/2 –wouldn’t work properly if it were called first! ?- sister(W, pat). W \= pat, parent(P,W), parent(P,pat), female(W) W \= pat (fails) No n But pat does have a sister: ann

Exercise n Which of the following would work for the question sister(Who, pat)? sister(S, P) :- parent(PP, S), parent(PP, P), S \= P, female(S). sister(S, P) :- parent(PP, S), S \= P, parent(PP, P), female(S). sister(S, P) :- female(S), parent(PP, P), parent(PP, S), S \= P. What about ?- sister(pat, Who).

Next Time n Terms & Unification n Bratko, Chapter 2