Logic Programming Lecture 2: Unification and proof search.

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
Advertisements

Inference Rules Universal Instantiation Existential Generalization
SLD-resolution Introduction Most general unifiers SLD-resolution
Chapter 11 :: Logic Languages
1 Turing Machines and Equivalent Models Section 13.2 The Church-Turing Thesis.
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
PROBLEM SOLVING AND SEARCH
Inference in first-order logic Chapter 9. Outline Reducing first-order inference to propositional inference Unification Generalized Modus Ponens Forward.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
Logic Programming Lecture 3: Recursion, lists, and data structures.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Logic Programming Part 2: Semantics James Cheney CS 411.
Logic Programming James Cheney CS 411. Functional Programming Programs as functions Write down function you want to calculate Computer evaluates to a.
0 1 Todays Topics Resolution – top down and bottom up j-DREW BU procedure Subsumption – change to procedure Infinite Loops RuleML input – Prolog output.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
INFERENCE IN FIRST-ORDER LOGIC IES 503 ARTIFICIAL INTELLIGENCE İPEK SÜĞÜT.
Logic Programming Lecture 6: Parsing, Difference Lists and Definite Clause Grammars.
Formal Models of Computation Part II The Logic Model
Vilalta&Eick: Informed Search Informed Search and Exploration Search Strategies Heuristic Functions Local Search Algorithms Vilalta&Eick: Informed Search.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
1 Lecture 7 Implementing Prolog unification, backtracking with coroutines Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
Logic Programming Programming Lecture 1: Getting started with Prolog.
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.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Implementing Prolog unification, backtracking with coroutines Ras Bodik with Mangpo and Ali Hack Your Language! CS164: Introduction to Programming.
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.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
The AI War LISP and Prolog Basic Concepts of Logic Programming
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.
1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.
Basic Problem Solving Search strategy  Problem can be solved by searching for a solution. An attempt is to transform initial state of a problem into some.
Programming Languages Third Edition Chapter 4 Logic Programming.
Principles of programming languages 12: Logic programming Isao Sasano Department of Information Science and Engineering.
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
Inference in First Order Logic. Outline Reducing first order inference to propositional inference Unification Generalized Modus Ponens Forward and backward.
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.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Logic Programming Lecture 6: Parsing and Definite Clause Grammars.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Logic Programming Lecture 7: Search Strategies:
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
More on Prolog syntax Already seen program statement types: Already seen program statement types: rules: p(X) :- q(X,Y), r(Y,z). rules: p(X) :- q(X,Y),
Tests, Backtracking, and Recursion
Decidability continued….
Presentation transcript:

Logic Programming Lecture 2: Unification and proof search

James CheneyLogic ProgrammingSeptember 22, 2014 Outline for today Quick review Equality and unification How Prolog searches for answers

James CheneyLogic ProgrammingSeptember 22, 2014 Quick review Atoms bart ‘Mr. Burns’ Variables X Y Z Predicates p(t 1,...,t n ) Terms Facts father(homer, bart). Goals p(t 1,...,t n ),..., q(t 1 ',...,t n '). Rules p(ts) :- q(ts’),..., r(ts’’).

James CheneyLogic ProgrammingSeptember 18, 2014 Infix operators Prolog has built-in constants and infix operators Examples: Equality: t = u (or =(t,u)) Pairing: (t,u) (or,(t,u)) Empty list: [] List concatenation: [X|Y] (or.(X,Y)) You can also define your own infix operators!

James CheneyLogic ProgrammingSeptember 18, 2014 General observations Prolog is untyped everything is a "term" Prolog is declarative "predicates" with side effects, such as print, are the exception, not the rule Prolog does not have explicit control flow constructs (while, do) the search strategy allows us to simulate iteration but this is not usually the best way to program Therefore, try to forget what you already know from other languages

James CheneyLogic ProgrammingSeptember 18, 2014 Terms Also have... Numbers: Additional constants and infix operators More on these later.

James CheneyLogic ProgrammingSeptember 22, 2014 Unification (I) The equation t = u is a basic goal with a special meaning What happens if we ask: ?- X = c ?- f(X,g(Y,Z)) = f(c,g(X,Y)) ?- f(X,g(Y,f(X))) = f(c,g(X,Y)) And how does it do that?

James CheneyLogic ProgrammingSeptember 22, 2014 Unification (II) ?- X = c. X=c yes ?- f(X,g(Y,Z)) = f(c,g(X,Y)). X=c Y=c Z=c yes ?- f(X,g(Y,f(X))) = f(c,g(X,Y)). no

James CheneyLogic ProgrammingSeptember 22, 2014 Unification (III) A substitution is a mapping from variables to terms X 1 =t 1,...,X n =t n Given two terms t and u with free variables X 1...X n a unifier is a substitution that makes t and u equal

James CheneyLogic ProgrammingSeptember 22, 2014 Example (I) f(X,g(Y,Z)) = f(c,g(X,Y)) ff gg YZ c XY X X = c Y = X Z = Y

James CheneyLogic ProgrammingSeptember 22, 2014 Example (I) f(X,g(Y,Z)) = f(c,g(X,Y)) ff gg YZ c XY X X = c Y = c Z = c c cccc

James CheneyLogic ProgrammingSeptember 22, 2014 Example (II) f(X,g(Y,f(X))) = f(c,g(X,Y)) ff gg Y c XY X f X X = c Y = X

James CheneyLogic ProgrammingSeptember 22, 2014 Example (II) f(X,g(Y,f(X))) = f(c,g(X,Y)) ff gg Y c XY X f X X = c Y = c Y = f(X) f(X) = c?? c ccc c

James CheneyLogic ProgrammingSeptember 22, 2014 Robinson's Algorithm (I) Consider a general unification problem t 1 = u 1, t 2 = u 2,..., t n = u n Reduce the problem by decomposing each equation into one or more "smaller" equations Succeed if we reduce to a "solved form", otherwise fail

James CheneyLogic ProgrammingSeptember 22, 2014 Robinson's Algorithm (II) Two constants unify if they are equal. c = c, P → P c = d, P → fail.

James CheneyLogic ProgrammingSeptember 22, 2014 Robinson's Algorithm (III) Two function applications unify if the head symbols are equal, and the corresponding arguments unify. f(t 1,...,t n ) = f(u 1,...,u n ), P → t 1 = u 1,... t n = u n, P Must have equal numbers of arguments f(...) = g(...), P → fail f(...) = c, P → fail.

James CheneyLogic ProgrammingSeptember 22, 2014 Robinson's Algorithm (IV) Otherwise, a variable X unifies with a term t provided X does not occur in t. X = t, P → P[t/X] (occurs-check: X must not be in Vars(t)) Proceed by substituting t for X in P.

James CheneyLogic ProgrammingSeptember 22, 2014 Occurs check What happens if we try to unify X with something that contains X? ?- X = f(X). Logically, this should fail there is no (finite) unifier! Most Prolog implementations skip this check for efficiency reasons can use unify_with_occurs_check/2

James CheneyLogic ProgrammingSeptember 22, 2014 Execution model The query is run by trying to find a solution to the goal using the clauses Unification is used to match goals and clauses There may be zero, one, or many solutions Execution may backtrack Formal model called SLD resolution which you'll see in the theory lectures

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (I) Idea: To solve atomic goal A, If B is a fact in the program, and θ(A) = θ(B), then return answer θ Else, if B :- G 1,...,G n is a clause in the program, and θ unifies A with B, then solve θ( G 1 )... θ(G n ) Else, give up on this goal. Backtrack to last choice point Clauses are tried in declaration order Compound goals are tried in left-right order

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (II) Prolog normally tries clauses in order of appearance in program. Assume: foo(a). foo(b). foo(c). Then: ?- foo(X). foo(X)

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (II) Prolog normally searches for clauses in order of appearance in database. Assume: foo(a). foo(b). foo(c). Then: ?- foo(X). X = a foo(X) X=a done

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (II) Prolog normally searches for clauses in order of appearance in database. Assume: foo(a). foo(b). foo(c). Then: ?- foo(X). X = a; X = b foo(X) X=a done X=b done

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (II) Prolog normally searches for clauses in order of appearance in database. Assume: foo(a). foo(b). foo(c). Then: ?- foo(X). X = a; X = b; X = c foo(X) X=a done X=b done X=c done

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (II) Prolog normally searches for clauses in order of appearance in database. Assume: foo(a). foo(b). foo(c). Then: ?- foo(X). X = a; X = b; X = c; no foo(X) X=a done X=b done X=c done

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (III) Prolog backtracks to the last choice point if a subgoal fails. Assume: bar(b). bar(c). baz(c). Then: ?- bar(X),baz(X). bar(X),baz(X)

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (III) Prolog backtracks to the last choice point if a subgoal fails. Assume: bar(b). bar(c). baz(c). Then: ?- bar(X),baz(X). bar(X),baz(X) X=b

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (III) Prolog backtracks to the last choice point if a subgoal fails. Assume: bar(b). bar(c). baz(c). Then: ?- bar(X),baz(X). bar(X),baz(X) X=b baz(b)

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (III) Prolog backtracks to the last choice point if a subgoal fails. Assume: bar(b). bar(c). baz(c). Then: ?- bar(X),baz(X). bar(X),baz(X) X=b baz(b)

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (III) Prolog backtracks to the last choice point if a subgoal fails. Assume: bar(b). bar(c). baz(c). Then: ?- bar(X),baz(X). bar(X),baz(X) X=b baz(b)

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (III) Prolog backtracks to the last choice point if a subgoal fails. Assume: bar(b). bar(c). baz(c). Then: ?- bar(X),baz(X). bar(X),baz(X) X=b X=c baz(b)

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (III) Prolog backtracks to the last choice point if a subgoal fails. Assume: bar(b). bar(c). baz(c). Then: ?- bar(X),baz(X). bar(X),baz(X) X=b X=c baz(c)baz(b)

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (III) Prolog backtracks to the last choice point if a subgoal fails. Assume: bar(b). bar(c). baz(c). Then: ?- bar(X),baz(X). X = c bar(X),baz(X) X=b X=c baz(c)baz(b) done

James CheneyLogic ProgrammingSeptember 22, 2014 Depth-first search (III) Prolog backtracks to the last choice point if a subgoal fails. Assume: bar(b). bar(c). baz(c). Then: ?- bar(X),baz(X). X = c; no bar(X),baz(X) X=b X=c baz(c)baz(b) done

James CheneyLogic ProgrammingSeptember 22, 2014 "Generate and test" Common Prolog programming idiom: find(X) :- generate(X), test(X). where test(X) checks if X is a solution generate(X) searches for solutions Can use to constrain (infinite) search space Can use different generators to get different search strategies besides depth- first

James CheneyLogic ProgrammingSeptember 22, 2014 Limitations of depth- first search Recursion needs to be handled carefully to avoid loops Rule order and goal order matter More in next lecture Not complete "in practice" legitimate answers may be missed due to loops

James CheneyLogic ProgrammingSeptember 22, 2014 Other search strategies Breadth-first search / iterative deepening Explore all alternatives, interleaved Price: memory overhead Bottom-up (forward chaining) Compute all possible answers derivable from facts & rules Only viable for "Datalog" programs with "flat" data (only constants and variables) Supported by commercial tools for big data (LogicBlox, DLV)

James CheneyLogic ProgrammingSeptember 22, 2014 Next time Recursion Lists, trees, data structures Further reading: LPN ch. 2 Tutorial #1 will be up soon