Chapter Two: Syntax and Meaning of Prolog Programs
Chapter two: 2.1 Data objects data objects simple objects constants atoms numbers variable structures
2.1 Data objects 2.1.1 Constants: atoms String of letters, digits and the underscore character ‘_’ starting with a lower- case letter e.g. anna x25 x_34a x___y3 miss_Jones String of special characters e.g. <----> ===> … ::= note: when using atoms of this form, be careful because some of strings already have a predefined meaning e.g. ‘:-‘ String of characters enclosed in single quotes e.g. ‘Tom’ ‘tom’ ‘SarahJones’ data objects simple objects constants atoms numbers variable structures
The lexical scope of atom names is the whole program. This means that for example if the atom bob appeared in two clauses , then it always represents the same object in both clauses
2.1 Data objects 2.1.1 Constants: numbers Integer numbers simple objects constants atoms numbers variable structures 2.1.1 Constants: numbers Integer numbers e.g. 1 133 0 -97 Real numbers e.g. 3.14 -0.0035 100.2
2.1 Data objects 2.1.2 Variables Variables are strings of letters, digits and underscores characters, starting with upper-case letter or an underscore. e.g. X , Result , Object2 , _23 Shopping_list _ (“the underscore” is the anonymous variable) data objects simple objects constants atoms numbers variable structures
But each occurrence of X in the same clause means the same variable. The lexical scope of variable names is one clause except the anonymous variable. This means that for example if the variable X appeared in two clauses then it signifies two different variables . eg: offspring (X , Y) :- parent (Y , X) mother (X , Y) :- parent (X , Y) , female (X) But each occurrence of X in the same clause means the same variable. X in the first clause is different from X in the second one Each X in this clause has the same value
somebody_has_child:-parent(_,_). somebody_has_child:-parent(X,Y). The anonymous variable signifies new variable each time it occurs even in the same clause. somebody_has_child:-parent(_,_). somebody_has_child:-parent(X,Y). somebody_has_child:-parent(X,X). If the anonymous variable appears in a query then its value will not be output when Prolog answers the query. eg: if we are interested in people who have children but not in the names of the children, then we can ask ?- parent (X , _)
2.1 Data objects 2.1.3 Structures Structures are objects that have several components which can be structures themselves. e.g. date (1, may, 2001) to represent any day in may; day is variable date (Day, may, 2001) All structures can be pictured as tree. Each functor is defined by name and arity (#of arguments) data objects simple objects constants atoms numbers variable structures functor arguments (all are constants) date may 1 2001
2.1 Data objects 2.1.3 Structures Example: how structures can be used to represent the following geometric objects ? A point in 2 dimensional space A line segment A triangle Draw the tree structures that correspond to them.
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) m point (7,1))
Represent the following equation as a structure and draw the tree structure that corresponds to : (a + b) * (c - 5) * ( + (a , b) , - (c , 5))