Matching Matching and unification. Rules for matching terms. Examples of matching. –The most general instantiation. Computation using matching.

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

SLD-resolution Introduction Most general unifiers SLD-resolution
Chapter 11 :: Logic Languages
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Prolog.
Prolog Programming (Volume 5) Dr W.F. Clocksin. List cells + Unification = Great Idea Normally we use ‘proper lists’, which are defined according to the.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
4. PROLOG Data Objects And PROLOG Arithmetic
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 2 Theory –Unification –Unification in Prolog –Proof search Exercises –Correction exercises.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
Formal Models of Computation Part II The Logic Model
Relations And Functions. A relation from non empty set A to a non empty set B is a subset of cartesian product of A x B. This is a relation The domain.
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.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
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.
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 defining.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Chapter 2 Syntax and meaning of prolog programs Part 1.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
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),
Logic Programming Lecture 2: Unification and proof search.
Straight Line Graph.
tautology checking continued
CS 3304 Comparative Languages
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
PROLOG.
Tests, Backtracking, and Recursion
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Chapter 11 :: Logic Languages
Linear graphs.
COSC 3340: Introduction to Theory of Computation
Learning Resource Services
Relational Algebra 1.
Artificial Intelligence CS370D
Prolog syntax + Unification
KS4 Mathematics Linear Graphs.
Finite Automata Reading: Chapter 2.
Objectives Find the magnitude and direction of a vector.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Chapter 11 :: Logic Languages
Three Special Structures – Case, Do While, and Do Until
Chapter 12 :: Logic Languages
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
Chapter 12 :: Logic Languages
KS3 Mathematics A5 Functions and graphs
Chapter Two: Syntax and Meaning of Prolog Programs
Chapter 12 :: Logic Languages
Chapter 2 Syntax and meaning of prolog programs
A8 Linear and real-life graphs
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
PROLOG.
Presentation transcript:

Matching Matching and unification. Rules for matching terms. Examples of matching. –The most general instantiation. Computation using matching.

7.2 Matching And Unification Matching is making things equal. Sometimes matching is called unification. –Strictly speaking this is incorrect. –PROLOG does not fully implement logical unification. –In formal logic : | ?- X = f(X). no | ?- GNU PROLOG goes into an infinite loop. | ?- X = f(X). Prolog interruption (h for help) ? a execution aborted | ?-

7.3 Matching And Unification II Some PROLOGs do this : | ?- X = f(X). X = f(f(f(f(f(f(f(f(f(f(... For efficiency reasons some modern PROLOGs include the occurs check in the matching process. –If the same logical variable occurs on both sides of the = the query is rejected. | ?- X = f(X). Occurs check violation. Query aborted. no | ?-

7.4 Rules For Matching Any two terms T1 and T2 match if : –1. T1 and T2 are identical literal constants. –2. T1 is a logical variable and T2 is a literal constant or a structured object.  T1 is instantiated to T2. –3. T2 is a logical variable and T1 is a literal constant or a structured object.  T2 is instantiated to T1. –4. T1 and T2 are both logical variables. The variables are cross instantiated with each other’s values. –5. T1 and T2 are both structured objects and if  5.1 T1 and T2 have the same functor and the same arity.  5.2 The corresponding components of T1 and T2 match.

7.5 Examples Of Matching Rule 1 : | ?- fred = fred. yes | ?- 1 = 1. yes | ?- hello = goodbye. no | ?- Rule 2 : | ?- A = slot(monday, 22, 12). A = slot(monday, 22, 10) yes | ?- B = slot(Day, Date, Hour). B = slot(Day, Date, Hour) yes | ?-

7.6 The Most General Instantiation Some PROLOGs would invent internal names for Day, Date and Hour in the last query. – _1, _2 and _3 are typical. –Internal logical variables. Whenever PROLOG performs a match it generates the most general instantiation (MGI) possible. –Logical variables are instantiated only as much as can be deduced from the terms being matched. –PROLOG can’t ‘invent’ information, it’s a program, not a sentient being. The concept of MGI is similar to that of the least fixed point which occurs all over the place in the theory of programming language, for example in the formal semantics of PROLOG.

7.7 Examples Of Matching II Rule 3 is the same as rule 2 but with the arguments the other way round : | ?- slot(monday, 22, 12) = A. A = slot(monday, 22, 10). yes | ?- slot(Day, Date, Hour) = B. B = slot(Day, Date, Hour) | ?- Rule 4 : | ?- A = B. B = A | ?- Some PROLOGs would invent internal names for Day, Date, Hour, A and B.

7.8 Recursive Matching Rule 5 is recursive. To match two structures the system matches their corresponding components. –If the components are structures then their components must be matched as well. Rule 5 is often called the tree matching rule since any structured object can be represented as a tree. Example : triangle(point(1,1), P2, point(2,3)) triangle(P1, point(5,Y1), point(2,Y2))

7.9 Recursive Matching II Both structures represent triangles; their components are the three corners of a triangle represented by the x and y distances of the points from some origin. To match the structures PROLOG must match the corresponding components. The matching process can be viewed as one of overlaying one tree onto the other. P1 = point(1, 1) P2 = point(5, Y1) Y2 = 3

7.10 Matching Lists Lists are structures with two components, the head and the tail. –Of course, the head and the tail may themselves be lists. | ?- [] = []. yes | ?- [1 | Xs] = [X, 2,3]. X = 1 Xs = [2,3] yes | ?- [1, [hello, world] ] = [A,[B,C]]. A = 1 B = hello C = world | ?-

7.11 Computation Using Matching Matching is an extremely powerful concept. It is possible to perform useful computations just using matching. vertical(line(point(X,_)), point(X,_)). A line is a structure with two components : the position of the two ends of the line. –Each position is represented by a structure containing its x and y coordinates. The fact states that a line is vertical if the x coordinates of its two ends are the same. –A fact containing logical variables.

7.12 Computation Using Matching II | ?- vertical(line(point(1,1),point(1,2))). yes | ?- vertical(line(point(1,1),point(2,Y))). no | ?- vertical(line(point(1,1),point(Y,6))). Y = 1 yes | ?- vertical(line(point(2,3),P)). P = point(2, _) yes | ?- The last answer states that the line is vertical if the x coordinate of P is 2. The value of its y coordinate is irrelevant so the most general instantiation leaves that value uninstantiated. –Some PROLOGs would generate an internal name for the y coordinate.

7.13 Computation Using Matching III Similarly we can define horizontal : horizontal(line(point(_,Y),point(_,Y))). | ?- horizontal(line(point(1,1),point(2,1))). yes | ?- horizontal(line(point(1,1),point(X,2))). no | ?- horizontal(line(point(1,6),point(2,Y))). Y = 6 yes | ?- horizontal(line(point(10,10),P)). P = point(_,10) yes | ?- vertical(L), horizontal(L). L = line(point(A, B),point(A, B)). yes | ?- The only line that is vertical and horizontal is a point. –It doesn’t matter where that point is  internal names.

7.14 Summary Matching is making things equal. –PROLOG sets the logical variables to any values required to make the two terms equal. Five rules for matching. –1. Literal constants match if they are the same. –2,3. A logical variable matches with a literal constant or a structure. The variables are instantiated with the literal constant or structure. –4. Two logical variables match. The variables are cross instantiated with each other’s values. –5. Two structures match if they have the same functor, the same arity and their components match. PROLOG generates the most general instantiation. –Does just as much as required. –Doesn’t invent information.