operators in prolog © patrick blackburn, johan bos & kristina striegnitz

34
Operators in Prolog http://www.learnprolognow.org © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Upload: denis-ryan

Post on 17-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Operators in Prolog

http://www.learnprolognow.org © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Page 2: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Arithmetic in Prolog

• Prolog provides a number of basic arithmetic tools

• Integer and real numbers

2 + 3 = 53 x 4 = 125 – 3 = 23 – 5 = -24 2 = 21 is the remainder when 7 is

divided by 2

?- 5 is 2+3.?- 12 is 34.?- 2 is 5-3.?- -2 is 3-5.?- 2 is 4/2.?- 1 is mod(7,2).

Arithmetic Prolog

Page 3: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Example queries

?- 10 is 5+5.yes

?- 4 is 2+3.no

?- X is 3 4.X=12yes

?- R is mod(7,2).R=1yes

Page 4: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Defining predicates with arithmetic

addThreeAndDouble(X, Y):- Y is (X+3) 2.

Page 5: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Defining predicates with arithmetic

addThreeAndDouble(X, Y):- Y is (X+3) 2.

?- addThreeAndDouble(1,X).X=8yes

?- addThreeAndDouble(2,X).X=10yes

Page 6: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

A closer look

• It is important to know that +, -, / and do not carry out any arithmetic

• Expressions such as 3+2, 4-7, 5/5 are ordinary Prolog terms– Functor: +, -, /, – Arity: 2– Arguments: integers

Page 7: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

A closer look

?- X = 3 + 2.

Page 8: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

A closer look

?- X = 3 + 2.X = 3+2yes

?-

Page 9: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

A closer look

?- X = 3 + 2.X = 3+2yes

?- 3 + 2 = X.

Page 10: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

A closer look

?- X = 3 + 2.X = 3+2yes

?- 3 + 2 = X.X = 3+2yes ?-

Page 11: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

The is/2 predicate

• To force Prolog to actually evaluate arithmetic expressions, we have to use

is

just as we did in the other examples• This is an instruction for Prolog to carry out

calculations• Because this is not an ordinary Prolog

predicate, there are some restrictions

Page 12: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

The is/2 predicate

?- X is 3 + 2.

Page 13: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

The is/2 predicate

?- X is 3 + 2.X = 5yes

?-

Page 14: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

The is/2 predicate

?- X is 3 + 2.X = 5yes

?- 3 + 2 is X.

Page 15: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

The is/2 predicate

?- X is 3 + 2.X = 5yes

?- 3 + 2 is X.ERROR: is/2: Arguments are not sufficiently instantiated

?-

Page 16: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

The is/2 predicate

?- X is 3 + 2.X = 5yes

?- 3 + 2 is X.ERROR: is/2: Arguments are not sufficiently instantiated

?- Result is 2+2+2+2+2.

Page 17: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

The is/2 predicate

?- X is 3 + 2.X = 5yes

?- 3 + 2 is X.ERROR: is/2: Arguments are not sufficiently instantiated

?- Result is 2+2+2+2+2.Result = 10yes

?-

Page 18: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Restrictions on use of is/2

• We are free to use variables on the right hand side of the is predicate

• But when Prolog actually carries out the evaluation, the variables must be instantiated with a variable-free Prolog term

• This Prolog term must be an arithmetic expression

Page 19: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Notation

• Two final remarks on arithmetic expressions– 3+2, 4/2, 4-5 are just ordinary Prolog terms in

a user-friendly notation: 3+2 is really +(3,2) and so on.

– Also the is predicate is a two-place Prolog predicate

Page 20: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Notation

• Two final remarks on arithmetic expressions– 3+2, 4/2, 4-5 are just ordinary Prolog terms in

a user-friendly notation: 3+2 is really +(3,2) and so on.

– Also the is predicate is a two-place Prolog predicate

?- is(X,+(3,2)). X = 5yes

Page 21: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Comparing terms: ==/2

• Prolog contains an important predicate for comparing terms

• This is the identity predicate ==/2

• The identity predicate ==/2 does not instantiate variables, that is, it behaves differently from =/2

Page 22: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Comparing terms: ==/2

• Prolog contains an important predicate for comparing terms

• This is the identity predicate ==/2

• The identity predicate ==/2 does not instantiate variables, that is, it behaves differently from =/2

?- a==a.yes

?- a==b.no

?- a=='a'.yes

?- a==X.X = _443no

Page 23: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Comparing variables

• Two different uninstantiated variables are not identical terms

• Variables instantiated with a term T are identical to T

Page 24: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Comparing variables

• Two different uninstantiated variables are not identical terms

• Variables instantiated with a term T are identical to T

?- X==X.X = _443 yes

?- Y==X. Y = _442 X = _443 no

?- a=U, a==U.U = _443 yes

Page 25: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Comparing terms: \==/2

• The predicate \==/2 is defined so that it succeeds in precisely those cases where ==/2 fails

• In other words, it succeeds whenever two terms are not identical, and fails otherwise

Page 26: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Comparing terms: \==/2

• The predicate \==/2 is defined so that it succeeds in precisely those cases where ==/2 fails

• In other words, it succeeds whenever two terms are not identical, and fails otherwise

?- a \== a.no

?- a \== b.yes

?- a \== 'a'.no

?- a \== X.X = _443yes

Page 27: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Arithmetic terms

• +, -, <, >, etc are functors and expressions such as 2+3 are actually ordinary complex terms

• The term 2+3 is identical to the term +(2,3)

Page 28: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Arithmetic terms

• +, -, <, >, etc are functors and expressions such as 2+3 are actually ordinary complex terms

• The term 2+3 is identical to the term +(2,3)

?- 2+3 == +(2,3).yes

?- -(2,3) == 2-3.yes

?- (4<2) == <(4,2).yes

Page 29: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Summary of comparison predicates

Negation of arithmetic equality predicate=\=

Arithmetic equality predicate=:=

Negation of identity predicate\==

Identity predicate==

Negation of unification predicate\=

Unification predicate =

Page 30: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Precedence

• Every operator has a certain precedence to work out ambiguous expressions

• For instance, does 2+3*3 mean 2+(3*3), or (2+3)*3?

• Because the precedence of + is greater than that of *, Prolog chooses + to be the main functor of 2+3*3

Page 31: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Associativity

• Prolog uses associativity to disambiguate operators with the same precedence value

• Example: 2+3+4Does this mean (2+3)+4 or 2+(3+4)?– Left associative– Right associative

Page 32: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Types of operators in Prolog

• yfx left-associative, infix• xfy right-associative, infix• xfx non-associative, infix• fx non-associative, prefix• fy right-associative, prefix• xf non-associative, postfix• yf left-associative, postfix

Page 33: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Operators in SWI Prolog

Page 34: Operators in Prolog  © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Summary of this lecture

• In this lecture we have– Arithmetic operators– Comparing operators– Precedence and Associativity