Logic Programming in Prolog Part II Substitutions, SLD Resolution Prolog Execution. Raj Sunderraman

Similar documents
Definite Logic Programs: Derivation and Proof Trees. CSE 505 Computing with Logic Stony Brook University

Resolution and Refutation

Chapter 3. SLD-Resolution. 3.1 Informal Introduction

Top-Down Execution CS240B Notes

Logik für Informatiker: PROLOG Part 6: Cuts & Negation

Prolog. Resolution (for beginners?) Resolution (cont.) Resolution (cont.) Unification. Resolution (still cont.) Resolution & Unification

Resolution for Predicate Logic

Logic and Resolution Representation and Reasoning

Computational Logic Automated Deduction Fundamentals

Outline. Logic. Definition. Theorem (Gödel s Completeness Theorem) Summary of Previous Week. Undecidability. Unification

Computational Logic Fundamentals (of Definite Programs): Syntax and Semantics

The non-logical symbols determine a specific F OL language and consists of the following sets. Σ = {Σ n } n<ω

Computing logical consequences

Closed Book Examination. Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. M.Sc. in Advanced Computer Science

Unification and occur check. The programming language Prolog

Convert to clause form:

Strong AI vs. Weak AI Automated Reasoning

Jacques Fleuriot. Automated Reasoning Rewrite Rules

Lifted Inference: Exact Search Based Algorithms

Figure SUBSTITUTION AND UNIFICATION

Models. Let our knowledge base KB, consist of a set of formulas. We say that I is a model of KB or that I satisfies KB

Advanced Topics in LP and FP

Computational Logic. Davide Martinenghi. Spring Free University of Bozen-Bolzano. Computational Logic Davide Martinenghi (1/26)

Resolution for predicate logic

Negative Hyper-Resolution as Procedural Semantics of Disjunctive Logic Programs

CS156: The Calculus of Computation Zohar Manna Winter 2010

Foundations of Logic Programming

Lecture 10: Even more on predicate logic" Prerequisites for lifted inference: Skolemization and Unification" Inference in predicate logic"

Introduction to Predicate Logic Part 1. Professor Anita Wasilewska Lecture Notes (1)

Automated Reasoning (in First Order Logic) Andrei Voronkov. University of Manchester

Inductive Logic Programming. Part 2. Based partially on Luc De Raedt s slides

Data structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:

Notation for Logical Operators:

Handout Proof Methods in Computer Science

CS 4700: Artificial Intelligence

Lecture Notes 3 Multiple Random Variables. Joint, Marginal, and Conditional pmfs. Bayes Rule and Independence for pmfs

Section Summary. Section 1.5 9/9/2014

and u and v are orthogonal if and only if u v = 0. u v = x1x2 + y1y2 + z1z2. 1. In R 3 the dot product is defined by

CogSysI Lecture 8: Automated Theorem Proving

Logical Inference 2 rule based reasoning

CSE507. Satisfiability Modulo Theories. Computer-Aided Reasoning for Software. Emina Torlak

Exercises: Artificial Intelligence. Automated Reasoning: Good to walk

4 Predicate / First Order Logic

Query answering using views

Logic. Knowledge Representation & Reasoning Mechanisms. Logic. Propositional Logic Predicate Logic (predicate Calculus) Automated Reasoning

2 Series Solutions near a Regular Singular Point

GAV-sound with conjunctive queries

Inductive Logic Programming: The Problem Specification

Lecture Predicates and Quantifiers 1.5 Nested Quantifiers

Basics on Probability. Jingrui He 09/11/2007

Resolution for Predicate Logic

COMP9414: Artificial Intelligence First-Order Logic

An Early (1971) Conversation. Logic Programming (PLP 11.3) Horn Clauses Introduction to Prolog: Resolution, Unification.

Inference in First Order Logic

Logic and Proof. Computer Science Tripos Part IB Michaelmas Term. Aaron R. Coble. Lawrence C Paulson Computer Laboratory University of Cambridge

Lecture Notes in Machine Learning Chapter 3: Languages for learning

MTH310 EXAM 2 REVIEW

Before you begin read these instructions carefully.

Fuzzy Datalog with background knowledge

Fixed point theorems in fuzzy metric spaces using (CLRG) property

Kreisel s Conjecture with minimality principle

Resolution (14A) Young W. Lim 8/15/14


5. Series Solutions of ODEs

ITEC Prolog Programming

+ 1 3 x2 2x x3 + 3x 2 + 0x x x2 2x + 3 4

Analysis of Algorithms I: Asymptotic Notation, Induction, and MergeSort

Lecture Notes on Unification

Predicates and Quantifiers. Nested Quantifiers Discrete Mathematic. Chapter 1: Logic and Proof

Notes on the Dual Ramsey Theorem

arxiv:cs/ v1 [cs.pl] 31 Jan 2002

Reprinted from the Bulletin of Informatics and Cybernetics Research Association of Statistical Sciences,Vol.41

Maxima and Minima. (a, b) of R if

König s Lemma and Kleene Tree

David Adam. Jean-Luc Chabert LAMFA CNRS-UMR 6140, Université de Picardie, France

Packet #2: Set Theory & Predicate Calculus. Applied Discrete Mathematics

1 Introduction to Predicate Resolution

Abstract Interpretation of Resolution-Based Semantics

Announcements. Project 1 underway... Second midterm Friday. discuss ideas with TAs check out wiki

When describing some state of affairs in the real world we often use declarative 1 sentences

Lower semicontinuous and Convex Functions

Theory Combination. Clark Barrett. New York University. CS357, Stanford University, Nov 2, p. 1/24

Probability and Statistics

α-formulas β-formulas

UNIT-2: MULTIPLE RANDOM VARIABLES & OPERATIONS

Data Structures and Algorithms CMPSC 465

Hyper-Resolution AUTOMATED REASONING SLIDES 8: HYPER-RESOLUTION Hyper-resolution Refinement The Otter Theorem Prover Generalised Hyper-resolution.

Input: A set (x i -yy i ) data. Output: Function value at arbitrary point x. What for x = 1.2?

COMP 409: Logic Homework 5

Quasi-Terminating Logic Programs for Ensuring the Termination of Partial Evaluation

3.17 Semantic Tableaux for First-Order Logic

Lecture 5: Splay Trees

Foundations of Artificial Intelligence

Completeness of Hyper-Resolution via the Semantics of Disjunctive Logic Programs

Simon Fraser University School of Computing Science CMPT 383

CSI30. Chapter 1. The Foundations: Logic and Proofs Nested Quantifiers

Proposi'onal Logic Not Enough

Brief Introduction to Prolog

Predicate Logic: Syntax

ON T-FUZZY GROUPS. Inheung Chon

Transcription:

Logic Programming in Prolog Part II Substitutions, SLD Resolution Prolog Execution Raj Sunderraman

Puzzle Revisited faster solution didbetter(a,b,[a,b,_]). didbetter(a,c,[a,_,c]). didbetter(b,c,[_,b,c]). first(x,[x _]). makelistoffriends(0,[]). makelistoffriends(n,[person(_,_,_) L]) :- M is N-1, makelistoffriends(m,l). answer(aussie,richardsport) :- makelistoffriends(3,friends), didbetter(person(michael,_,basketball),person(_,american,_),friends), didbetter(person(simon,israeli,_),person(_,_,tennis),friends), first(person(_,_,cricket),friends), member(person(aussie,australian,_),friends), member(person(richard,_,richardsport),friends).

Substitutions Definition: A substitution is a finite set of pairs of terms, {X1 t1,, Xn tn}, where each ti is a term and each Xi is a variable such that Xi!= ti and Xi!= Xj if i!= j. Empty substitution is represented by ε θ = {X 20, Y f(a), Z [1,2,3]} Definition: The application Xθ of a substitution θ to a variable X is defined as follows: Xθ = t if X := t is in θ = X otherwise Y {X 20, Y f(a), Z [1,2,3]} = f(a) U {X 20, Y f(a), Z [1,2,3]} = U Definition: Let θ be a substitution {X1 t1,, Xn tn} and E is a term or formula. Then, application of θ to E, Eθ is a term or formula obtained from E by simultaneously replacing every occurrence of Xi by ti, 1<= i<=n. Eθ is called an instance of E. p(f(x,z),f(y,a)) {X a, Y Z, W b} = p(f(a,z),f(z,a)) p(x,y) {X f(y), Y b} = p(f(y),b)

Substitutions continued Definition: Let θ = {X1 s1,, Xm sm} and σ = {Y1 t1,, Yn tn} The composition of θσ is obtained from the set {X1 s1σ,, Xm smσ, Y1 t1,, Yn tn} by - removing all Xi siσ where Xi = siσ, and - removing those Yj tj for which Yj is in {X1,, Xm}. example: {X f(z), Y W}{X a, Z a, W Y} = {X f(a), Z a, W Y} Properties: E(θσ) = (E θ)σ (θσ)ω = θ(σω) θε = εθ = θ composition of substitutions is not commutative {X f(y)}{y a} = {X f(a), Y a} {Y a}{x f(y)} = {Y a, X f(y)}

Unifiers and Most General Unifiers (MGU) Definition: Let s and t be two terms. A substitution θ is a unifier for s and t if sθ = tθ. f(x,y) f(g(z),z) unifier: θ = {X g(z), Y Z} another unifier: σ = {X g(a), Y a, Z a} Definition: A substitution θ is more general than substitution σ if there exists a substitution ω such that σ = θω In previous example, σ = θ {Z a}. θ is more general that σ Definition: A unifier is said to be the most general unifier (mgu) of two terms if it is more general that any other unifier of the terms.

Algorithm for Most General Unifiers (MGU) Input: S = {E1,, En} Output: mgu(s) Method: 1. k := 0; σk = {} 2. If Sσk is a singleton, return σk otherwise find Dk, the disagreement set of Sσk 3. If there exists a variable v and a term t in Dk such that v does not appear in t, then σk+1 = σk {v t}, k++, goto step 2. Otherwise stop; S is not unifiable. Note: The disagreement set of a set of terms is obtained by extracting the first sub-term in each term that is different in each. S = { f(x,y), f(g(z),z) } k σk Sσk Disagreement set of Sσk 0 { } { f(x,y), f(g(z),z) } { X, g(z) } 1 {X g(z) } { f(g(z),y), f(g(z),z) } { Y, Z } 2 {X g(z), Y Z } { f(g(z),z) }

Another MGU Example S = { p(x,y, X), p(f(y), a, f(z)) } k σk Sσk Disagreement set of Sσk 0 { } { p(x,y, X), p(f(y), a, f(z)) } { X, f(y) } 1 {X f(y) } { p(f(y),y, f(y)), p(f(y), a, f(z)) } { Y, a } 2 {X f(a), Y a } { p(f(a),a, f(a)), p(f(a), a, f(z)) } { Z, a} 3 {X f(a), Y a, Z a } { p(f(a),a, f(a)) }

father(adam, mary). newborn(mary). proud(x) :- parent(x,), newborn(y). parent(x,y) :- father(x,y). parent(x,y) :- mother(x,y).?- proud(z). SLD Resolution Informal Introduction?- proud(z). proud(x) :- parent(x,y), newborn(y).?- parent(z,y), newborn(y). parent(x,y) :- father(x,y).?- father(z,y), newborn(y). father(adam, mary).?- newborn(mary). newborn(mary).

SLD Resolution Let?- A1,, Am. be a goal/query (call it G0) and let B0 :- B1,,Bn. be a renamed program fact/rule (call it R0) whose head predicate unifies with A1. The variables in the program fact/rule are renamed so that there are no common variables between the program fact/rule and G0) Let mgu(a1, B0) = θ1. The new goal/query (call it G1) is?- (B1,,Bn,A2,, Am) θ1 This process is repeated, i.e. G0 R 0 G1 R 1 G2 G(n-1) R (n-1) Gn θ1 θ2 θn until (a) empty goal, or (b) cannot find program fact/rule to unify, or infinite loop! The sequence G0, G1,, Gn, is called a SLD-derivation. A finite sequence ending in the empty goal is called a SLD-Refutation. For a finite SLD-derivation, θ = θ1θ2 θn is called the computed substitution.

SLD Resolution continued Example: G0:?- proud(z). R0: proud(x0) :- parent(x0,y0), newborn(y0). θ1 = {X0 Z} G1:?- parent(z,y0), newborn(y0). R1: parent(x1,y1) :- father(x1,y1). θ2 = {X1 Z, Y1 Y0} G2:?- father(z,y0), newborn(y0). R2: father(adam,mary). θ3 = {Z adam, Y0 mary} G 3 :?- newborn(mary). R 3 : newborn(mary). θ4 = {} θ = θ1θ2θ3θ4 = {X0 Z}{X1 Z, Y1 Y0} {Z adam, Y0 mary} ε = {X0 adam, X1 adam, Y1 mary, Z adam, Y0 mary} G 4 :

SLD Resolution - Another Example grandfather(x,z) :- father(x,y), parent(y,z). parent(x,y) :- father(x,y). G0:?- grandfather(a,x). parent(x,y) :- mother(x,y). R0: grandfather(x0,z0) :- father(x0,y0), parent(y0,z0). father(a,b). mother(b,c). θ1 = {X0 a, Z0 X} G1:?- father(a,y0), parent(y0,x). R1: father(a,b). θ2 = {Y0 b} G2:?- parent(b,x). R2: parent(x2,y2) :- mother(x2,y2). θ3 = {X2 b, Y2 X} G 3 :?- mother(b,x). R 3 : mother(b,c). θ4 = {X c} θ = θ1θ2θ3θ4 = {X0 a, Z0 X} {Y0 b}{x2 b, Y2 X}{X c} = {X0 a, Y0 b, X2 b, Y2 c, X c} G 4 :

SLD Refutation Tree You may have noticed that in SLD Resolution there may be multiple choices for the program fact/rule. In fact, Prolog implementation will try these choices out exhaustively (in a depth-first manner using back-tracking) to obtain one or more SLD-refutations, resulting in one or more answers to the original goal/query. Example: G0:?- grandfather(a,x). R0: grandfather(x0,z0) :- father(x0,y0), parent(y0,z0). θ1 = {X0 a, Z0 X} G1:?- father(a,y0), parent(y0,x). R1: father(a,b). θ2 = {Y0 b} G2:?- parent(b,x). R2: parent(x2,y2) :- father(x2,y2). θ3 = {X2 b, Y2 X} G 3 :?- father(b,x). STUCK!! cannot progress; Prolog will backtrack and try other possibilities.

SLD Refutation Tree?- grandfather(a,x).?- father(a,y0), parent(y0,x).?- parent(b,x). grandfather(x0,z0) :- father(x0,y0), parent(y0,z0). θ1 = {X0 a, Z0 X} father(a,b). θ2 = {Y0 b} parent(x2,y2) :- father(x2,y2). θ3 = {X2 b, Y2 X} parent(x2,y2) :- mother(x2,y2). θ3 = {X2 b, Y2 X}?- father(b,x).?- mother(b,x). FAIL θ = θ1θ2θ3θ4 mother(b,c). θ4 = {X c} = {X0 a, Z0 X} {Y0 b}{x2 b, Y2 X}{X c} = {X0 a, Y0 b, X2 b, Y2 c, X c}

Practice Problems 1. Find the mgu, if any, for the following sets: S = { p(x,f(x)), p(y,f(a)) } T = { p(a,x), p(x,f(x)) } 2. Consider the following program: p(y) :- q(x,y), r(y). p(x) :- q(x,x). q(x,x) :- s(x). r(b). s(a). s(b). Draw the complete SLD-refutation tree for the goal:?- p(x).

SLD Refutation Tree - Another Example del(x1,[x1 T1],T1). θ1 = {X1 1, A 1, T1 [2,3], L [2,3] }?- del(a,[1,2,3],l). del(x2,[y2 T2],[Y2 S2]) :- del(x2,t2,s2). θ2 = {X2 A, Y 2 1, T2 [2,3], L [1 S2]} del(x,[x T],T). del(x,[y T],[Y S]) :- del(x,t,s). del(x3,[x3 T3],T3). θ3 = {X3 2, A 2, T3 [3], S2 [3] }?- del(a,[2,3],s2). del(x4,[y4 T4],[Y4 S4]) :- del(x4,t4,s4). θ4 = {X4 A, Y 4 2, T4 [3], S2 [2 S4]} del(x5,[x5 T5],T5). θ5 = {X5 3, A 3, T5 [], S4 [] }?- del(a,[3],s4). del(x6,[y6 T6],[Y6 S6]) :- del(x6,t6,s6). θ6= {X6 A, Y 6 3, T6 [], S4 [3 S6]} Answers:?- del(a,[],s4). θ1 = {X1 1, A 1, T1 [2,3], L [2,3] } θ2θ3 = {X2 A, Y 2 1, T2 [2,3], L [1 S2]} {X3 2, A 2, T3 [3], S2 [3] } = {X2 2, Y 2 1, T2 [2,3], L [1,3], X3 2, A 2, T3 [3], S2 [3]} FAIL θ2θ4θ5 = {X2 A, Y 2 1, T2 [2,3], L [1 S2]} {X4 A, Y 4 2, T4 [3], S2 [2 S4]} {X5 3, A 3, T5 [], S4 [] } = {X2 3, Y 2 1, T2 [2,3], L [1,2], X4 3, Y 4 2, T4 [3], S2 [2], X5 3, A 3, T5 [], S4 []}