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 structuressimple objectsliteral constantsvariablesatomsnumbers
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 AtomsAtoms : non-numeric literal constants. Strings containing combinations oflower case lettersupper case lettersdigitsspecial 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 VariablesStrings 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).yesGNU 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 + 4yes|? -= means do these terms match?X is a logical variable so it matches with the structureVery useful.
10 Arithmetic Computation In PROLOG II To get PROLOG to actually perform the arithmetic we must use the is operator.|? - X isX = 7yes|? - 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 isX = 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 = 7Y = 14yes|? - Y is X + X, X isuncaught 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 IISometimes 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.