Chapter Two cont Syntax and Meaning of Prolog

  • Slides: 7
Download presentation
Chapter Two: (cont. ) Syntax and Meaning of Prolog Programs 1

Chapter Two: (cont. ) Syntax and Meaning of Prolog Programs 1

2. 2 Matching � Matching is the most important operation on terms. � Given

2. 2 Matching � Matching is the most important operation on terms. � Given two terms, they match if: they are identical, or 2. the variables in both terms can be instantiated to objects in such a way after the substitution of variables by these objects the terms become identical. Example date(D, M, 2001) and date(D 1, may, Y 1) date(D, M, 2001) and date(D 1, M 1, 1444) date(X, Y, Z) and point(X, Y, Z) 1. 2

The general rules to decide weather two terms match � If S and T

The general rules to decide weather two terms match � If S and T are constants then S and T match only if they are the same object. � If S is a variable and T is anything, then they match, and S is instantiated to T. (And vice versa) � If S and T are structures then the match only if: �S and T have the same principle functor, and � All their corresponding components match. 3

2. 2 Matching � Matching is a process that takes as inputs two terms

2. 2 Matching � Matching is a process that takes as inputs two terms and check whether they match. If the terms do not match, we say this process fails. If they match, the process succeeds. � To request Prolog matching operation, we use ‘=‘ : ? - parent(X, anna)= parent (bob, Y). Prolog Answer : X = bob Y = anna yes 4

2. 2 Matching Example Consider the following terms in Prolog program: point (_, _).

2. 2 Matching Example Consider the following terms in Prolog program: point (_, _). seg(point(_, _), point(_, _)). triangle(point(_, _), point(_, _)). Declare vertical and horizontal relations vertical (seg(point(X, _), point(X, _))). horizontal (seg(point(_, Y), point(_, Y))). 5

2. 2 Matching Formulate the following questions and find Prolog answers: � Is the

2. 2 Matching Formulate the following questions and find Prolog answers: � Is the segment (1, 1), (1, 4) vertical? ? - vertical (seg(point(1, 1), point(1, 4))). Prolog Answer : yes � What is the other coordinate that make the segment (1, 1), (2, Y) vertical? ? - vertical (seg(point(1, 1), point(2, Y))). Prolog Answer : no 6

2. 2 Matching � What is the other coordinate that make the segment (1,

2. 2 Matching � What is the other coordinate that make the segment (1, 1), (2, Y) horizontal? ? - horizontal (seg(point(1, 1), point(2, Y))). Prolog Answer : � Are Y=1 there any vertical segment that start at point(2, 3)? ? - vertical (seg(point(2, 3), point(_, _))). Prolog Answer : � Is yes there a segment that is both vertical and horizontal? ? - vertical (S), horizontal (S). Prolog Answer : S = seg(point(X, Y), point(X, Y)) yes 7