MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections 2.3 - 2.7.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Computer Science CPSC 322 Lecture 25 Top Down Proof Procedure (Ch 5.2.2)
Chapter 11 :: Logic Languages
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
1. An Overview of Prolog.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
Standard Logical Equivalences
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).
Logic.
Predicates and Quantifiers
Prolog Programming for Artificial Intelligence Three edition 2001
Prolog programming Introduction to Prolog
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
L41 Lecture 2: Predicates and Quantifiers.. L42 Agenda Predicates and Quantifiers –Existential Quantifier  –Universal Quantifier 
Chapter 12 - Logic Programming
Introduction to Prolog Proof Procedures. Exercise parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad,
Chapter 8: The Logical Paradigm Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
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.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CPSC 322 Introduction to Artificial Intelligence September 20, 2004.
LING 364: Introduction to Formal Semantics Lecture 3 January 19th.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
CHA2555 Week2: Knowledge Representation, Logic and Prolog Lee McCluskey First term:
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.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
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.
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.
Slide 1 Propositional Definite Clause Logic: Syntax, Semantics and Bottom-up Proofs Jim Little UBC CS 322 – CSP October 20, 2014.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Chapter 1, Part II: Predicate Logic With Question/Answer Animations.
MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2.
For Monday Read “lectures” 6,9-12 of Learn Prolog Now: saarland.de/~kris/learn-prolog-now/ saarland.de/~kris/learn-prolog-now/
CIS2326 Week2: Logic and Prolog Lee McCluskey First term:
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CHA2555 Week2 Practical: Lee McCluskey First term:
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.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
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.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
Recursive stack-based version of Back-chaining using Propositional Logic
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
Prolog Primarily for Symbolic (nonnumeric) Computation
Prolog programming Introduction to Prolog (part3)
Prolog programming Introduction to Prolog (part4)
How To Think Like a Prolog ?
Predicates and Quantifiers
Bottom Up: Soundness and Completeness
Presentation transcript:

MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections

MB: 2 March 2001CS360 Lecture 32 Where is the Prolog Program? n When one talks about Prolog programs, one is referring to the Knowledge base. n The definitions of the relations in the Knowledge base is the Prolog code.

MB: 2 March 2001CS360 Lecture 33 Goals n Goals are the terms that Prolog attempts to match against the clause heads in its Knowledge base. n The top level terms in a query are all goals: – ?: a(4, x), b(Y,7). n The top level terms in the body of a clause are all goals: – p(X) :- q(X, a), r(3, X).

MB: 2 March 2001CS360 Lecture 34 Goals and Data Structures n Prolog goals look exactly like Prolog data structures. n In other words, Prolog code looks just like Prolog data. This is intentional! n A Prolog program can modify itself as it is running. n A Prolog program can create new queries & pose them to the Prolog interpreter.

MB: 2 March 2001CS360 Lecture 35 Prolog’s Declarative Semantics n The prolog we have seen so far is called pure Prolog, i.e., everything can be expressed in FOPC.  A  P(ancestor(A,P)  parent(A,X)  ancestor(X,P)) n For example: ancestor(A,P) :- parent(A,X), ancestor(X,P). can be translated into the logical expression:  A  P(ancestor(A,P)  parent(A,X)  ancestor(X,P))

MB: 2 March 2001CS360 Lecture 36 Translating Prolog clauses into FOPC  A  P( n The clauses : ancestor(A,P) :- parent(A,P). ancestor (A,P):- parent(A,X), ancestor(X,P). Translates into FOPC as:  A  P( ancestor(A,P)  parent(A,P)  ( parent(A,X)  ancestor(X,P)))

MB: 2 March 2001CS360 Lecture 37 Prolog Declarative Semantics n In the body of the clause, the commas separating the top-level terms are understood to be conjunctions (and’s). n There are two ways to specify disjunctions (or’s): – Write separate clauses (as done for ancestor) – Use a semicolon (“;”)

MB: 2 March 2001CS360 Lecture 38 Disjunctions in Prolog n We could rewrite the ancestor example in Prolog as follows: ancestor(A,P) :- parent(A,P) ; parent(A,X), ancestor(X,P).

MB: 2 March 2001CS360 Lecture 39 Negation in Pure Prolog n We cannot express negation in the clause body in pure prolog:  H  W( – For example, we can’t express in pure Prolog the following FOPC expression  H  W( husband(H,W)  married(H,W)  male(H)   divorced(H,W) )

MB: 2 March 2001CS360 Lecture 310 Negation in Pure Prolog cont’d n Likewise, Prolog can’t tell us something is false rather it tells us that it can’t prove something is true. n For example, given the Knowledge base: – man(socrates). – mortal(X) :- man(X). n The answer to mortal(mike) would be no, not because it isn’t true, but because Prolog can’t prove it’s true using that KB.

MB: 2 March 2001CS360 Lecture 311 Prolog’s Procedural Semantics I n Prolog’s control flow different from languages like Java. n In Prolog no distinction between statements & method calls - all goals recursively matched against KB (system defined relations (write/1) have own definitions). n Goal parameters can be unbound variables. n Goals succeed or they fail.

MB: 2 March 2001CS360 Lecture 312 Prolog Control Flow Example n Given the query a(2), Prolog scans the KB for the first clause head that matches the first query goal. n If it finds a match then it recurses on the goals in the clause body until it bottoms out. n Query succeeded here. KB 1. a(X) :- b(X). 2. b(2). Execution trace of query [a(2)] (goal stack) 1. X = 2 (which clause [b(2)] matched & bindings) 2. [ ]

MB: 2 March 2001CS360 Lecture 313 Prolog Control Flow Example n Query: a(2) n Notice that after the query goal matched the clause head, the query goal (a(2)) is replaced by the first clause body goal (b(2)) in the next step of the trace with its variable bound. KB 1. a(X) :- b(X). 2. b(2). Execution trace of query [a(2)] (goal stack) 1. X = 2 (which clause [b(2)] matched & bindings) 2. [ ]

MB: 2 March 2001CS360 Lecture 314 Prolog Control Flow Example n Query: a(3), Prolog scans the KB for first clause head matching first query goal. n If it fails to find a match then it tries to backtrack its matches. n There are no other possible matches. n Query fails. KB 1. a(X) :- b(X). 2. b(2). Execution trace of query [a(3)] 1. X = 3 [b(3)] 

MB: 2 March 2001CS360 Lecture 315 Backtracking n Query: a(3), from point of failure, Prolog backtracks to last match (matches clause 1), Prolog continues its scan of KB for another clause head that matches goal. n Matches clause 2 and succeeds. KB : 1. a(X) :- b(X). 2. a(3). 3. b(2). Execution trace of query [a(3)] 1. X = 3 2. [b(3)] [ ] 

MB: 2 March 2001CS360 Lecture 316 Flow of Control with Conjunctions Query: a(X). KB: 1. a(X) :- b(X), c(X). 2. b(2). 3. c(2). Response: X = 2 ? [a(X)] 1.X = X’ I[b(X’), c(X’)] 2. X’ = 2 [c(2)] 3. [] Note renaming of variables in matched clause, X replaced by X’. Goal query is replaced by first clause body goal and rest of clause body goals are pushed onto goal stack.

MB: 2 March 2001CS360 Lecture 317 Flow of Control with Conjunctions Query: a(X). KB: 1. a(X) :- b(X), c(X). 2. b(2). 3. b(3) 4. c(3). Response: X = 3 ? [a(X)] 1.X = X’ [b(X’),c(X’)] 2. X’ = 2 3. X’ = 3 [c(2)] [c(3)]  4. [ ] Notice unbinding of variable (X’) when backtrack over binding point.

MB: 2 March 2001CS360 Lecture 318 Prolog Procedural Semantics II n When goal fails, control undoes any bindings done at that step & backs up to previous step. n It continues scan of KB for clause heads matching current goal. n If none found then continues to backtrack.

MB: 2 March 2001CS360 Lecture 319 Flow of Control with Conjunctions Query: a(X). KB: 1. a(X) :- b(X), c(X). 2. b(Y) :- d(Z), e(Y,Z). 3. b(3) 4. c(3). 5. d(5). 6. e(3,5). Response: X = 3 ? [a(X)] 1.X = X’ [b(X’), c(X’)] 2. X’ = Y [d(Z), e(Y,Z), c(Y)] 5. Z = 5 [e(Y,5), c(Y)] 6. Y = 3 [c(3)] 4. [ ]

MB: 2 March 2001CS360 Lecture 320 Flow of Control with Conjunctions Query: a(X). KB: 1. a(X) :- b(X), c(X). 2. b(Y) :- d(Z), e(Y,Z). 3. b(3) 4. c(4). 5. d(5). 6. e(3,5). 7. e(4,5). Response: X = 4 ? [a(X)] 1.X = X’ [b(X’), c(X’)] 2. X’ = Y [d(Z), e(Y,Z), c(Y)] 5. Z = 5 [e(Y, 5), c(Y)] 6. Y = 3 7. Y = 4 [c(3)] [c(4)]  4. [ ]

MB: 2 March 2001CS360 Lecture 321 Additional Answers n What happens when Prolog returns an answer and prompts you with “?”? n Since an answer was returned, the trace ended with a true and an empty goal stack. n If you type in “;” then Prolog will, in effect, change that true to fails and backtrack.

MB: 2 March 2001CS360 Lecture 322 Effects of Clause/Goal Ordering n A Prolog program can be declaratively correct, but procedurally inadequate (may recurse forever (p :- p.), or simply be inefficient). n The program’s behavior is determined by the order of the clauses in the KB and the order of the goals in the clauses. n Changing either can dramatically affect the behavior of the program.

MB: 2 March 2001CS360 Lecture 323 Example: Parental Fact KB n parent(pam,bob). n parent(tom,bob). n parent(tom, liz). n parent(bob, ann). n parent(bob,pat). n parent(pat, jim).

MB: 2 March 2001CS360 Lecture 324 Predecessor Relation Variations pred1(X,Z) :- parent(X, Z). pred1(X,Z) :- parent(X,Y), pred1(Y,Z). pred2(X,Z) :- parent(X,Y), pred2(Y,Z). pred2(X,Z) :- parent(X, Z). pred3(X,Z) :- parent(X, Z). pred3(X,Z) :- pred3(Y,Z), parent(X,Y). pred4(X,Z) :- pred4(Y,Z), parent(X,Y). pred4(X,Z) :- parent(X, Z).

MB: 2 March 2001CS360 Lecture 325 Computing predn(tom,pat) n For n from 1 to 3, Prolog returns yes. n For pred4(tom, pat), Prolog loops forever. n While the first 3 preds find an answer they do so with different amounts of computation.

MB: 2 March 2001CS360 Lecture 326 Trace of pred1(tom,pat) KB: 1. parent(pam,bob). 2. parent(tom,bob). 3. parent(tom, liz). 4. parent(bob, ann). 5. parent(bob,pat). 6. parent(pat, jim). 7. pred1(X,Z) :- parent(X, Z). 8. pred1(X,Z) :- parent(X,Y), pred1(Y,Z). [pred1(tom,pat)] 7. X=tom, Z=pat 8. X=tom,Z=pat [parent(tom,pat)] [parent(tom,Y),pred1(Y,pat)]  2. Y=bob [pred1(bob,pat)] 7. X’=bob, Z’=pat [parent(bob,pat)] 5. [ ]

MB: 2 March 2001CS360 Lecture 327 Exercises n Try doing the execution traces of pred2(tom,pat), pred3(tom,pat), and pred4(tom,pat)

MB: 2 March 2001CS360 Lecture 328 Quick Quiz n What is the difference between the structure of Prolog code and Prolog data? n What is a goal and how is it different from structured data? n How do you express a negated goal? n What does it mean when Prolog answers no

MB: 2 March 2001CS360 Lecture 329 Quick Quiz cont’d n In pure Prolog, does changing the order of the clauses or of the goals in the clauses change the declarative meaning? n What about the procedural meaning? n Is there anything declaratively wrong with: – ancestor(A,P) :- ancestor(X,P), parent(A,X). – ancestor(A,P) :- parent(A,P). n Is there anything procedurally wrong?

MB: 2 March 2001CS360 Lecture 330 Summary n Declarative semantics of pure Prolog program defines when a goal is true and for what instantiation of variables. n Commas between goals mean and, semicolons mean or. n Procedural semantics are determined by clause and goal ordering. n Goal failure causes backtracking and unbinding of affected variables.