4. PROLOG Data Objects And PROLOG Arithmetic

Download Report

Transcript 4. PROLOG Data Objects And PROLOG Arithmetic

4. PROLOG Data Objects And PROLOG Arithmetic

• • • • PROLOG data objects.

Syntax of simple objects.

Anonymous variables.

PROLOG arithmetic.

4.1

PROLOG Data Objects

• • Programs process data.

PROLOG data objects : Terms structures simple objects variables literal constants numbers atoms 4.2

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.3

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 .

4.4

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’).

4.5

Numbers

• • • • • PROLOG allows both integer and floating point types.

Integer : – e.g. 23 , 10243 , -23 .

• Floats : – e.g. 0.23

, 1.0234

, -12.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.

4.6

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

not by

assignment

.

during

matching

• Sometimes we don’t want to give a variable a name because we don’t care about it’s value.

Anonymous

variables.

4.7

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.

4.8

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 = 3 + 4.

X = 3 + 4 yes |? • • = means do these terms match? X is a logical variable so it matches with the

structure

3 + 4 .

– Very useful.

4.9

Arithmetic Computation In PROLOG II

• To get PROLOG to actually perform the arithmetic we must use the is operator.

|? - X is 3 + 4.

X = 7 yes |? - X is 1 + 2 * 3 / 4 - 5.

X = -2.5 ; yes |? • • 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 4 + 4.

X = 8 yes |? 4.10

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 3 + 4.

uncaught exception:error(...) |? • • Remember, PROLOG works left to right.

Can’t use is backwards. It’s

functional

not

relational

.

4.11

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 \= .

4.12

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 so we can use is and NewR to force evaluation of the arithmetic expressions. |? - fact(1,V).

V = 1 ?

yes |? - fact(2,V).

V = 2 ?

yes |? - fact(4,V).

V = 24 ?

yes |? 4.13

Summary

• PROLOG data objects are called

Terms

.

– Terms are either

structures

or

simple objects

.

 Simple objects are

variables constants

.

or

literal

 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.

4.14

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.

4.15