Presentation is loading. Please wait.

Presentation is loading. Please wait.

4. PROLOG Data Objects And PROLOG Arithmetic

Similar presentations


Presentation on theme: "4. PROLOG Data Objects And PROLOG Arithmetic"— Presentation transcript:

1 4. PROLOG Data Objects And PROLOG Arithmetic
Syntax of simple objects. Anonymous variables. PROLOG arithmetic.

2 PROLOG Data Objects Programs process data. PROLOG data objects : Terms
structures simple objects literal constants variables atoms numbers

3 PROLOG Data Objects II PROLOG data objects are called terms.
PROLOG distinguishes between terms according to their syntactic structure. Structures look different from simple objects. PROLOG is a typeless language. All it checks is that arithmetic operations are done with numbers and I/O is done with ASCII characters. Checks are done at run time. PROLOG fans claim that typelessness gives great flexibility. Personally, I think it’s a pain in the neck.

4 Atoms Atoms : non-numeric literal constants. Strings containing combinations of lower case letters upper case letters digits special characters like +, -, _, *, >, < etc. Three types of atom : alphanumerics, special character strings and quoted character strings. Alphanumerics : Similar to C++ or Java identifiers. Strings of letters, digits and _. Must start with a lower case letter. Relation names must always be alphanumeric atoms. e.g. octavius, constantine1, fred_bloggs.

5 Atoms II Special character strings :
Strings of the allowed special characters. May not contain letters, digits or _. e.g. >==>, ----, <<<>>>. Quoted character strings : Strings of any character enclosed between ‘. e.g. ‘Fred Bloggs’, ‘3 pounds of spuds.’. Very useful for atoms beginning with upper case letters. emperor(‘Octavius’).

6 Numbers PROLOG allows both integer and floating point types. Integer :
Floats : e.g. 0.23, , Floats using exponential notation : e.g. 12.3e34, -11.2e-45, 1.0e45. PROLOG suffers from the usual problems with floating point rounding errors. PROLOG is terrible at numeric computation. See below.

7 Variables Strings of letters, digits and _. Must start with an upper case letter or with _. Similar to alphanumerics. e.g. X, Variable, fred_bloggs, _23. PROLOG variables are logical variables, not store variables. Given values by instantiation during matching not by assignment . Sometimes we don’t want to give a variable a name because we don’t care about it’s value. Anonymous variables.

8 Variables II Very common in queries : |? - parent(_,gaius). true ?
Can also use them in facts or rules : killer(X) :- murdered(X,_). |? - killer(tiberius). yes GNU PROLOG doesn’t bother printing the name of tiberius’ killer. Each use of _ in a clause can take a different value.

9 Arithmetic Computation In PROLOG
PROLOG was designed for symbolic computation. Not good at numeric computation. PROLOG arithmetic operations : +, -, *, *, div, mod. Arithmetic doesn’t work as you might expect. |? - X = X = 3 + 4 yes |? - = means do these terms match? X is a logical variable so it matches with the structure Very useful.

10 Arithmetic Computation In PROLOG II
To get PROLOG to actually perform the arithmetic we must use the is operator. |? - X is X = 7 yes |? - X is * 3 / X = -2.5 ; |? - Not quite the same as assignment in C++ or Java. Means compute arithmetic expression on right hand side and test whether it matches with expression on left hand side. |? - X = 8, X is X = 8

11 Arithmetic Computation In PROLOG III
Can use expressions containing other logical variables but must be careful about the sub-goal ordering. |? - X is 3 + 4, Y is X + X. X = 7 Y = 14 yes |? - Y is X + X, X is uncaught exception:error(...) |? - Remember, PROLOG works left to right. Can’t use is backwards. It’s functional not relational.

12 Arithmetic Computation In PROLOG IV
The usual arithmetic comparison operators are available. =:= Equal. =\= Not equal. < Less than. > Greater than. >= Greater than or equal to. =< Less than or equal to. Like is they won’t work with uninstantiated logical variables. For non numeric values programmers normally use the general matching and non-matching operators, = and \=.

13 A Numeric Rule fact(1,1). % fact 1 fact(N,R) :- % fact 2
NewN is N - 1, fact(NewN,NewR), R is NewR * N. NB : Must use logical variables NewN and NewR so we can use is to force evaluation of the arithmetic expressions. |? - fact(1,V). V = 1 ? yes |? - fact(2,V). V = 2 ? |? - fact(4,V). V = 24 ? |? -

14 Summary PROLOG data objects are called Terms.
Terms are either structures or simple objects. Simple objects are variables or literal constants. Literal constants are numbers or atoms. PROLOG is typeless. Run time checks that only numbers are used in arithmetic and characters in I/O. PROLOG allows both integer and floating numeric types. Variables are logical variables not store variables. Given values by instantiation not assignment. Given values required to make query succeed.

15 Summary II Sometimes we don’t want to give a variable a name because we don’t care about its value. Anonymous variables. Denoted by _. Can be used in queries, facts or rules. PROLOG is for symbolic computation. Not very good for numeric computation. Provides usual arithmetical and logical operators (some with strange names). Must use is operator to force numeric computation to occur. Only one uninstantiated variable allowed. Must be on LHS of is. is will not work backwards.


Download ppt "4. PROLOG Data Objects And PROLOG Arithmetic"

Similar presentations


Ads by Google