60 likes | 174 Views
Chapter Two: Syntax and Meaning of Prolog Programs. 2.2 Matching. Matching is the most important operation on terms . Given two terms, they match if: they are identical, or
E N D
2.2 Matching • Matching is the most important operation on terms. • Given two terms, they match if: • they are identical, or • 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(D1, may, Y1) date(D, M, 2001) and date(D1, M1, 1444) date(X, Y, Z) and point(X, Y, Z)
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 ‘=‘ : ?- date(D, M, 2001)=date(D1, may, Y1). • Matching in Prolog always results in the most general instantiation.
2.2 Matching Example Consider the following terms in Prolog program: point (_, _). seg(point(_,_), point(_,_)). triangle(point(_,_), point(_,_), point(_,_)). Declare vertical and horizontal relations vertical (seg(point(X,_), point(X,_))). horizontal (seg(point(_,Y), point(_,Y))). Formulate the following questions and find Prolog answers: • Is the segment (1,1), (1,4) is vertical? • What is the other coordinate that make the segment (1,1), (2,Y) vertical? • What is the other coordinate that make the segment (1,1), (2,Y) horizontal? • Are there any vertical segment that start at point(2,3)? • Is there a segment that is both vertical and horizontal?
Meaning of Prolog programs • Consider the clause: P :- Q, R. • Declarative readings: • P is true if Q and R is true • From Q and R follows P • Procedural readings: • To solve problem P, first solve the subproblem Q, and then the subproblem R. • To satisfy P, first satisfy Q and then R.
Meaning of Prolog programs • Consider the clause: P :- Q; R. • ; means OR • Same as the following two clauses together: P :- Q.P :- R. • The comma binds stronger than the semicolon. • P :- Q,R;S,T,U. is understood as: • P:- (Q,R);(S,T,U). and means the same as the clauses: • P :- Q,R. • P :- S,T,U.