1 / 14

7. Matching

7. Matching. Matching and unification. Rules for matching terms. Examples of matching. The most general instantiation. Computation using matching. Matching And Unification. Matching is making things equal . Sometimes matching is called unification . Strictly speaking this is incorrect.

curt
Download Presentation

7. Matching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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). <CTRL-C> Prolog interruption (h for help) ? a execution aborted | ?-

  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 | ?-

  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.

  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 | ?-

  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. 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.

  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))

  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

  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 | ?-

  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.

  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.

  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.

  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.

More Related