Logic Programming with Prolog: Resolution, Unification, Backtracking

Slides:



Advertisements
Similar presentations
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Advertisements

Some Prolog Prolog is a logic programming language
Chapter 11 :: Logic Languages
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming with Prolog: Resolution, Unification, Backtracking COMP 144 Programming Language Concepts Spring 2003 Stotts, Hernandez-Campos Modified.
1 Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language Concepts Spring 2003 The University of North Carolina at Chapel Hill.
Logic Languages Source: Scott, Michael Programming Language Pragmatics 3rd Ed.
COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
Programming Languages Third Edition
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 26: Introduction to Logic Programming with Prolog COMP 144 Programming Language.
C. Varela1 Logic Programming (PLP 11.3) Prolog Imperative Control Flow: Backtracking Cut, Fail, Not Carlos Varela Rennselaer Polytechnic Institute September.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
Introduction to Logic Programming with Prolog (Section 11.3)
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
The AI War LISP and Prolog Basic Concepts of Logic Programming
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
Knowledge Based Information System
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 4&7: Will cover material today Download.
1 Announcements We are almost done grading HW1 HW2 due today Download SWI Prolog! HW3 (Prolog) will be posted today, due on February 29 th Spring 16 CSCI.
Announcements Rainbow grades up HW1, Quiz 1-2 in HW Server Deadline for HW3 (Prolog) is extended to Monday, March 7 th. Submit in HW Server A few notes.
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
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.
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages And other online sources (see links on schedule page) Michael L. Scott Based on Programming Language.
Logic Programming Languages
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
Logic Programming Languages
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
CS 3304 Comparative Languages
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Tests, Backtracking, and Recursion
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Logic Programming Languages
Chapter 11 :: Logic Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Chapter 11 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Logic Programming Language
Chapter 12 :: Logic Languages
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Presentation transcript:

Logic Programming with Prolog: Resolution, Unification, Backtracking The University of North Carolina at Chapel Hill COMP 144 Programming Language Concepts Spring 2003 Logic Programming with Prolog: Resolution, Unification, Backtracking Stotts, Hernandez-Campos Modified by Charles Ling for CS2209b, UWO Use with Permission

Prolog PROgramming in LOGic It is the most widely used logic programming language Its development started in 1970 and it was result of a collaboration between researchers from Marseille, France, and Edinburgh, Scotland

What’s it good for? Knowledge representation Natural language processing State-space searching (Rubik’s cube) Logic problems Theorem provers Expert systems, deductive databases Agents

Terms to learn Predicate calculus Horn clause Resolution Unification Backtracking

The Logic Paradigm A logic program comprises Axioms are a theory collection of axioms (facts and rules) [Premises] Goal statements [Things to be proved] Axioms are a theory Goal statement is a theorem Computation is deduction to prove the theorem within the theory Interpreter tries to find a collection of axioms and inference steps that imply the goal

Relational Programming A predicate is a tuple: pred(a,b,c) Tuple is an element in a relation Prolog program is a specification of a relation (contrast to functional programming) brother (sam, bill) brother (sam, bob) Brother is not a function, since it maps “sam” to two different range elements Brother is a relation Relations are n-ary, not just binary family(jane,sam,[ann,tim,sean])

Relations… examples (2,4), (3,9),(4,16), (5,25),(6,36),(7,49), ... “square” (t,t,f), (t,f,t), (f,t,t), (f,f,f) … “xor” boolean algebra (smith, bob, 43, male, richmond, plumber), (smith, bob, 27, male, richmond, lawyer), (jones, alice, 31, female, durham, doctor), (jones, lisa, 12, female, raleigh, student), (smith, chris, 53, female, durham, teacher)

Relational Programming Prolog programs define relations and allow you to express patterns to extract various tuples from the relations Infinite relations cannot be defined by rote… need rules (A,B) are related if B is A*A (B,H,A) are related if A is ½ B*H or… gen all tuples like this (B,H,B*H*0.5) Prolog uses Horn clauses for explicit definition (facts) and for rules

“Directionality” Parameters are not directional (in, out) Prolog programs can be run “in reverse” (2,4), (3,9),(4,16), (5,25),(6,36),(7,49), ... “square” can ask square(X,9) “what number, when squared, gives 9” can ask square(4,X) “what number is the square of 4”

Logic Programming Axioms, rules are written is standard form Horn clauses a consequent (head H) and a body (terms Bi) H :- B1, B2,…, Bn [In our notation: B1, B2,…, Bn -> H] when all Bi are true, we can deduce that H is true Horn clauses can capture most first-order predicate calculus statements but not all [What??] This is not the same issue as “can Prolog compute all computable functions”… any C program can be expressed in Prolog, and any Prolog program can be expressed in C

Prolog Programming Model A program is a database of (Horn) clauses order is important… one diff between prolog and logic Each clause is composed of terms: Constants (atoms, that are identifier starting with a lowercase letter, or numbers) e.g. curry, 4.5 Variables (identifiers starting with an uppercase letter) e.g. Food Structures (predicates or data structures) e.g. indian(Food), date(Year,Month,Day)

Resolution The derivation of new statements is called Resolution The logic programming system combines existing statements to find new statements… for instance C :- A, B D :- C D :- A, B A and B imply C If we know that A and B imply C, and that C implies D, then we can deduce that A and B imply D

Predicate Applied to a Variable Predicate Applied to an Atom Example flowery(X) :- rainy(X). rainy(rochester). flowery(rochester). Variable Predicate Applied to a Variable Free Variable X acquired value Rochester during the resolution This is known as Unification Predicate Applied to an Atom

Asks the interpreter to SWI-Prolog We will use SWI-Prolog for the Prolog programming assignments http://www.swi-prolog.org/ After the installation, try the example program ?- [likes]. % likes compiled 0.00 sec, 2,148 bytes Yes ?- likes(sam, curry). No ?- likes(sam, X). X = dahl ; X = tandoori ; X = kurma ; Load example likes.pl This goal cannot be proved, so it assumed to be false (This is the so called Close World Assumption) Asks the interpreter to find more solutions

Data Structures Data structures consist of an atom called the functor and a list of arguments e.g. date(Year,Month,Day) e.g. T = tree(3, tree(2,nil,nil), tree(5,nil,nil)) Data and predicates are all the same… prolog is symbolic… text matching most of the time 3 2 5 Functors

Principle of Resolution Prolog execution is based on the principle of resolution If C1 and C2 are Horn clauses and the head of C1 matches one of the terms in the body of C2, then we can replace the term in C2 with the body of C1 For example, C2: likes(sam,Food) :- indian(Food), mild(Food). C1: indian(dahl). C3: mild(dahl). We can replace the first and the second terms in C1 by C2 and C3 using the principle of resolution (after instantiating variable Food to dahl) Therefore, likes(sam, dahl) can be proved

Unification Prolog associates (binds) variables and values using a process known as unification Variable that receive a value are said to be instantiated Unification rules A constant unifies only with itself Two structures unify if and only if they have the same functor and the same number of arguments, and the corresponding arguments unify recursively A variable unifies with anything

Equality Equality is defined as unifiability For instance, An equality goal is using an infix predicate = For instance, ?- dahl = dahl. Yes ?- dahl = curry. No ?- likes(Person, dahl) = likes(sam, Food). Person = sam Food = dahl ; ?- likes(Person, curry) = likes(sam, Food). Food = curry ;

Equality What is the results of Internal Representation for an ?- likes(Person, Food) = likes(sam, Food). Person = sam Food = _G158 ; No Internal Representation for an uninstantiated variable Any instantiation proves the equality

Execution Order Prolog searches for a resolution sequence that satisfies the goal [automatically by Prolog Interpreter] In order to satisfy the logical predicate, we can imagine two search strategies: Forward chaining, derived the goal from the axioms Backward chaining, start with the goal and attempt to resolve them working backwards Backward chaining is usually more efficient, so it is the mechanism underlying the execution of Prolog programs Forward chaining is more efficient when the number of facts is small and the number of rules is very large

Backward Chaining in Prolog Backward chaining follows a classic depth-first backtracking algorithm Example Goal: Snowy(C)

Depth-first backtracking The search for a resolution is ordered and depth-first The behavior of the interpreter is predictable Ordering is fundamental in recursion Recursion is again the basic computational technique, as it was in functional languages Inappropriate ordering of the terms may result in non-terminating resolutions (infinite regression) For example: Graph edge(a,b). edge(b, c). edge(c, d). edge(d,e). edge(b, e). edge(d, f). path(X, X). path(X, Y) :- edge(Z, Y), path(X, Z). Correct

Depth-first backtracking The search for a resolution is ordered and depth-first The behavior of the interpreter is predictable Ordering is fundamental in recursion Recursion is again the basic computational technique, as it was in functional languages Inappropriate ordering of the terms may result in non-terminating resolutions (infinite regression) For example: Graph edge(a,b). edge(b, c). edge(c, d). edge(d,e). edge(b, e). edge(d, f). path(X, Y) :- path(X, Z), edge(Z, Y). path(X, X). Incorrect

Infinite Regression Goal

Backtracking under the hood Resolution/backtracking uses a frame stack Frame is a collection of bindings that causes a subgoal to unify with a rule New frame pushed onto stack when a new subgoal is to be unified Backtracking: pop a frame off when a subgoal fails

Backtracking under the hood Query is satisfied (succeeds) when all subgoals are unified Query fails when no rule matches a subgoal “;” query done when all frames popped off

Backtracking under the hood rainy(seattle) rainy(rochester) cold(rochester) snowy(X) :- rainy(X), cold(X). snowy(P). rainy(P), cold(P). rainy(P) database query first RHS match (a) first subgoal Creates this binding (unification) P\X: seattle (a)

Backtracking under the hood rainy(seattle) rainy(rochester) cold(rochester) snowy(X) :- rainy(X), cold(X). snowy(P). rainy(P), cold(P). rainy(P) cold(P) cold(seattle) database query first RHS match (a) first subgoal (b) second subgoal lookup binding for P Then try to find goal in DB, it’s not there so subgoal (b) fails (b) (no new bindings) Backtrack…pop (b) P\X: seattle (a)

Backtracking under the hood rainy(seattle) rainy(rochester) cold(rochester) snowy(X) :- rainy(X), cold(X). snowy(P). rainy(P), cold(P). rainy(P) database query first RHS match (a) first subgoal Try another binding in (a) P\X: rochester (a)

Backtracking under the hood rainy(seattle) rainy(rochester) cold(rochester) snowy(X) :- rainy(X), cold(X). snowy(P). rainy(P), cold(P). rainy(P) cold(P) database query first RHS match (a) first subgoal (b) second subgoal Lookup binding for P (b) (no new bindings) Then search DB for the subgoal P\X: rochester (a) Success…

Backtracking under the hood rainy(seattle) rainy(rochester) cold(rochester) snowy(X) :- rainy(X), cold(X). snowy(P). rainy(P), cold(P). rainy(P) cold(P) database query first RHS match (a) first subgoal (b) second subgoal Success… (b) (no new bindings) all stack frames stay display bindings that satisfy goal P\X: rochester (a) P = rochester

Backtracking under the hood rainy(seattle) rainy(rochester) cold(rochester) snowy(X) :- rainy(X), cold(X). snowy(P). rainy(P), cold(P). rainy(P) cold(P) database snowy(N) :- latitude(N,L), L > 60. query first RHS match (a) first subgoal (b) second subgoal If we had other rules, we would backtrack and keep going (b) (no new bindings) P\X: rochester (a) P = rochester

Examples Genealogy Data structures and arithmetic http://ktiml.mff.cuni.cz/~bartak/prolog/genealogy.html Data structures and arithmetic Prolog has an arithmetic functor is that unifies arithmetic values E.g. is (X, 1+2), X is 1+2 Dates example

Reading Assignment Read Guide to Prolog Example, Roman Barták Scott Ch. 11 intro Scott Sect. 11.3 intro, 11.3.1 Guide to Prolog Example, Roman Barták Go through the first two examples http://ktiml.mff.cuni.cz/~bartak/prolog/learning.html