70 likes | 172 Views
Chapter Two: Syntax and Meaning of Prolog Programs. Chapter two:. 2.1 Data objects. 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
E N D
Chapter two: 2.1 Data objects
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. <----> ===> … ::= when using atoms of this form, be carefulbecause some of strings already havea predefined meaning e.g. ‘:-' • String of characters enclosed in single quotese.g. ‘Tom’ ‘tom’ ‘SarahJones’ The lexical scope of atom names is the whole program.
2.1 Data objects • 2.1.1 Constants: numbers • Prolog is a primarily a language for symbolic and non numerical computation. • In symbolic computations, integers are often used, e.g. to count the numbers of items in a list; but there is less need for real 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 anonymous variable) • The lexical scope of variable names is oneclause except the anonymous variable; it signifies new variable each time itoccurs even in the same clause. e.g. somebody_has_child:-parent(_,_). somebody_has_child:-parent(X,Y). somebody_has_child:-parent(X,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 variabledate (Day, may, 2001) • All structures can be pictured as tree. • Each functor is defined by name and arity(#of arguments) functor arguments (all are constants) date 1 may 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 • (a + b) * (c - 5) Draw the tree structures that correspond to them.