0% found this document useful (0 votes)
36 views16 pages

Structures and Matching: CS-521 (WEEK 4) D.C.SC

This document discusses structures and matching in Prolog. It defines structures as objects that have components, which can themselves be structures. Structures are treated as single objects with a functor to combine the components. Terms in Prolog include atoms, variables, and compound terms like structures. Two terms match if they are identical or if variables can be instantiated such that the terms become identical. Matching involves checking if structures have the same functor and if corresponding components match. The declarative meaning of Prolog programs determines if a goal is true and for what values, by considering variants and instances of clauses.

Uploaded by

Yuzanar Away III
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views16 pages

Structures and Matching: CS-521 (WEEK 4) D.C.SC

This document discusses structures and matching in Prolog. It defines structures as objects that have components, which can themselves be structures. Structures are treated as single objects with a functor to combine the components. Terms in Prolog include atoms, variables, and compound terms like structures. Two terms match if they are identical or if variables can be instantiated such that the terms become identical. Matching involves checking if structures have the same functor and if corresponding components match. The declarative meaning of Prolog programs determines if a goal is true and for what values, by considering variants and instances of clauses.

Uploaded by

Yuzanar Away III
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1

Structures and
Matching

CS-521(WEEK 4)
D.C.SC
2

Outline

 Data Objects
 Matching
 Declarative meaning of Prolog programs
 Procedural meaning
 Example : monkey and banana
 Order of clauses and goals
 The relation between Prolog and logic
3
2.1 Data objects
4

Structures

 Structured objects (structures) are objects that have several components


 Components themselves can, in turn, be structures.
 Structures treated in the program as single objects

Example date(1,may,2003)

 To combine the components into a single object is called functor. Eg - date


5

More Structures

 Components can also be variables or other structures

Example, any day in the month of May

 date(Day, may, 2003)

Day is a variable and can be instantiated in the execution.


6

terms

Example
 All data objects in Prolog are terms
 an atom is a term. Eg – may, 2003
date(Day,may,2003)
 a variable is a term. Eg- Day
 a compound term is a term. Eg- date(Day,may,2003)
7

Example ---Structured Objects

 P1=point(1,1)
 P2=point(2,3)
 S=seg(P1,P2)=seg(point(1,1),point(2,3))
 T=triangle(point(4,2),point(6,4),point(7,1))
8

Note about functors

 If the same name is defined for two different roles such as


point(X1,Y1) and point(X,Y,Z)

Prolog system will recognize the difference by the number


of arguments and will interpret this name as two funtors:
one of them with two arguments and the other with three
arguments.
9

2.2 Matching

Given two terms match if


they are identical or
 the variables in both terms can be instantiated to objects in such a way
that after the substitution of variables by these objects the terms become
identical
10

Example

In Prolog,

?- date(D,M,2001) = date(15,may,Y1).

Prolog system will answer:


D = 15
M = may
Y1 = 2001
11
The general rules to decide whether two terms, S and T match
are as follows:

 If S and T are constants then S and T match only if they are the same objects

 If S is a variable and T is anything, then they match and S is instantiated to T.


Conversely, if T is a variable then T is instantiated to S.
12
The general rules to decide whether two terms, S and T match
are as follows:

 If S and T are structures then they match only if


S and T have the same principle functor and
 all their corresponding components match
The resulting instantiation is determined by the matching of the components.
13

Illustration for matching

Facts
 vertical(seg(point(X,Y),point(X,Y1))). % same X value
 horizontal(seg(point(X,Y),point(X1,Y))). % same Y value

Questions
 ?- vertical(seg(point(1,1),point(1,2))).
 true
 ?- vertical(seg(point(1,1),point(2,Y))).
 false
 ?- horizontal(seg(point(1,1),point(2,Y))).
 Y=1 true
14

2.3 Declarative meaning of Prolog programs

Declarative meaning
 To determine whether a given goal is true and if so, for what values it
is true

Variant
 Variant of a clause C is such an instance of the clause C where each
variable is substituted by another variable

Instance
 An instance of a clause C is the clause C with each of its variables
substituted by some term
15

Example

For a clause
 hasachild(X) :- parent(X,Y).

Two variants of the clause are:


 hasachild(A) :- parent(A,B).
 hasachild(X1) :- parent(X1,X2).

Instances of this clause are:


 hasachild(peter) :- parent(peter,Z).
 hasachild(barry) :- parent(barry,small(caroline)).
16

Thank You.

You might also like