28
1 Predicates and quantifiers Chapter 8 Formal Specification using Z

1 Predicates and quantifiers Chapter 8 Formal Specification using Z

  • View
    219

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

1

Predicates and quantifiers

Chapter 8

Formal Specification using Z

Page 2: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

2

Predicates

• A predicate is a logical statement that depends on a value or values. When a predicate is applied to a particular value it becomes a proposition. For example:

• prime(x)

• depends on some numeric value of x

• prime(7)

• is a true proposition; the only devisors of 7 are 1 and 7.

• Prime(6) is a false proposition; 6 can be divided by 2,3.

• A predicate can be viewed as a propositional function, that for some set of arguments returns to either true or false

Page 3: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

3

Quantifiers

• Consider the following statements:• All cats have tails.• Some people like cake.• Everyone gets a break once in a while.• All these statements indicate how frequently

certain things are true. In predicate calculus we use quantifiers in this context.

Page 4: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

4

Universal Quantifier

• The Universal Quantifier is written :

and is pronounced ‘for all’. It is often used in

the form :

declaration | constraint predicate

Which states the for the declarations given, restricted to certain values (the constraint), the predicate holds.

Page 5: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

5

Universal Quantifier

• The constraint may be omitted.

• The declaration introduces a typical element that is then optionally constrained or restricted in some some way: For example:

i: | i < 10 i2 < 100

Which states that for all natural numbers less than 10 their square is less that 100 .

Separators

Constraint Predicate

Page 6: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

6

Universal Quantifier

• A universal quantifier can be viewed as a chain of conjunctions. So:

i: | i < 10 i2 < 100

is equivalent to:

02 < 100 12 < 100 92 < 100

If the set of values over which the variable is universally quantified is empty, then the quantification is defined as true.

i: | 0 i < 0 i2 < 100

is true.

Page 7: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

7

Existential Quantifier

• The Existential Quantifier is written :

and is pronounced ‘there exists’. It is often

used in the form :

declaration | constraint predicate

The declaration introduces a typical element that is then optionally constrained or restricted in some way: For example:

i: | i < 10 i2 < 100

Page 8: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

8

Existential Quantifier• There may be more than one value of i for

which this is true, in the previous example there are ten values, 0 to 9, that satisfy the predicate. Another example:

Even(x) k: k * 2 = x

The existential quantifier can be considered as a chain of disjunctions (ors). If the set of values over which the variable is existentially quantified is empty, then the quantification is defined as false.

i: | 0 i < 0 i2 < 100

Page 9: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

9

Unique Quantifier• The unique quantifier is similar to the

existential quantifier except that it states that there exists only one value for which the predicate is true. It is written as:

1

• An example:

1i: | i < 1 0 i2 < 100 i2 > 80

Page 10: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

10

Unique Quantifier• The previous unique quantifier example is

equivalent to saying that the predicate holds for i, but there is no value j for which it holds This is written as:

1i: | i < 1 0 i2 < 100 i2 > 80

1i: | i < 1 0 i2 < 100 i2 > 80

¬(j: | j < 1 0 i j j2 < 100 j2 > 80)

Page 11: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

11

Counting Quantifier• Some notations use a counting quantifier that

counts for how many values of a variable the predicate holds. In Z this is not needed, we use set comprehension instead.

Page 12: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

12

Set Comprehension

• Set Comprehension:

• It is possible to give the value of a set by giving a condition (a predicate) which must hold true for every member of the set. The general form is:

• {declaration | constraint expression}

• SetA ::= {x: | Even(x) x*x}

Page 13: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

13

Set Comprehension

• {declaration | constraint expression}

• The declaration is for a typical element and it gives the element’s name and type.

• The constraint restricts the possible values of the typical element. It is a logical expression which must be true for that value of the typical element to be included.

• The expression indicates the value to be held in the set

Page 14: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

14

Set Comprehension

• SetA ::= {x: | Even(x) x*x}

• The above defines a set of type integer. The value of x is constrained to be even. Each element will be the square of an even integer.

• SetB ::= {x: | Even(x) x}

• The above defines a set of type integer. The value of x is constrained to be even. Each element will be an even integer.

Page 15: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

15

Ranges of numbers

• We have already introduced m..n. This is shorthand for

• {i: | m i n i i}

Page 16: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

16

Relationship between Logic and Set Theory

• There is a direct relationship between some of the operations of logic and operations on sets:

• [X] any set S,T: X• S T == {x:X | x S x T x}

• S T == {x:X | x S x T x}

• S \ T == {x:X | x S x T x}

Page 17: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

17

Summary of Quantifier

x:T P

for all x of type T, P is true.

x:T P

there exists an x of type T, such that P is true.

1 x :T P

there exists a unique x of type T, such that P is true.

Page 18: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

18

Summary of Quantifier

{D | P t}

The set of t’s declared by D where P is true.

Page 19: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

19

Mixing Quantifiers

• What is the precedence or associativity used. Is forAll(x),forAll(y) equivalent to forAll(y),forAll(x)?

• Assuming standard notation, they are read left to right; two universal quantifiers following one another commute (just like multiplication a*b=b*a).

Page 20: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

20

Mixing Quantifiers

• Likewise, if you have two existential quantifiers, they commute, because

• Exists(x),Exists(y), p(x,y)

• is true if and only if

• Exists(y),Exists(x), p(x,y)

• is true.

Page 21: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

21

Mixing Quantifiers• But if you have mixed universal and existential

quantifiers, you have to be careful. If you write

(x), (y), p(x,y)

• that means that for each x, there exists a y, which may depend on x, for which p(x,y) is true.

• But if you write

(y), (x), p(x,y)

• you are saying that there is a y which works for any given x (including y=x), that make p(x,y) true.

Page 22: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

22

Mixing Quantifiers

L e t t h e p r o p o s i t i o n a l f u n c t i o n g ( x , y ) b ed e f i n e d a s :

g ( x , y ) = x < y

W r i t e e a c h o f t h e f o l l o w i n g p r o p o s i t i o n s i nw o r d s a n d s t a t e w h e t h e r t h e y a r e t r u e o rf a l s e .

12

) ( ( , ) )) ( ( , ) )

x y g x yy x g x y

Page 23: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

23

Mixing QuantifiersL e t t h e p r o p o s i t i o n a l f u n c t i o n g ( x , y ) b e d e f i n e d a s : g ( x , y ) = x < y W r i t e e a c h o f t h e f o l l o w i n g p r o p o s i t i o n s i n w o r d s a n d s t a t e w h e t h e r t h e y a r e t r u e o r f a l s e .

12

) ( ( , ) )) ( ( , ) )

x y g x yy x g x y

1 ) F o r a l l n a t u r a l n u m b e r s ( x ) t h e r e e x i s t s a y s u c h t h a t x < y . T h e s t a t e m e n t i s t r u e i f w e m a k e y = x + 1 . W e o n l y r e q u i r e t h e e x i s t e n c e o f o n e y t o m a k e t h e s t a t e m e n t t r u e . 2 ) T h e r e e x i s t s a n a t u r a l n u m b e r ( y ) s u c h t h a t f o r a l l x , x < y . T h e s t a t e m e n t i s f a l s e w h e n y = x . W e o n l y h a v e t o f i n d o n e y t h a t m a k e t h e s t a t e m e n t f a l s e .

Page 24: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

24

Exercises

Let O(x,y) be the predicate (or propositional function) “x is older than y”. The domain of discourse consists of three people Garry, who is 30; Ellen who is 20; and Martin who is 35. Write each of the following propositions in words and state whether they are true or false.

Page 25: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

25

Exercises

Let O (x,y) be the p ropositional function “x is o lder than y”. T he dom ain ofd iscourse consists of three people G arry, w ho is 30; E llen w ho is 20; and M artinw ho is 35 . W rite each of the fo llow ing propositions in w ords and state w hetherthey are true or false.

),( yxyOx),( yxyOx

),( yxyOx),( yxyOx

Page 26: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

26

Exercises

Let O(x,y) be the propositional function “x is older than y”. The domain of discourseconsists of three people Garry, who is 30; Ellen who is 20; and Martin who is 35. Writeeach of the following propositions in words and state whether they are true or false.

),( yxyOx Everyone is older than everyone, False),( yxyOx Everyone is older than someone, False),( yxyOx Someone is older than everyone, False),( yxyOx Someone is older than someone else, True

Page 27: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

27

Exercise

Determine whether each sentence 1-5 is a proposition. If it is a proposition write itsnegation.1. Waiter, will you serve the soup?2. Write a program to calculate the distance from A to B3. Four is greater than three.4. The cat is clever.5. Today is Christmas Day..

Page 28: 1 Predicates and quantifiers Chapter 8 Formal Specification using Z

28

Exercise

Determine whether each sentence 1-5 is a proposition. If it is a proposition write itsnegation.1. Waiter, will you serve the soup? Answer No2. Write a program to calculate the distance from A to B Answer No3. Four is greater than three. Answer Yes. Four is not greater than three.4. The cat is clever. Answer Yes. The cat is not clever5. Today is Christmas Day. Answer Yes. Today is not Christmas The notion of a proposition here is intuitive and id not defined precisely. Roughlyspeaking, a proposition is a fact, which is either true or false. So sentences stating factsare considered to be propositions while commands or questions are not consideredpropositions.