computing fundamentals 1 lecture 4 quantification lecturer: patrick browne room k308 based on...

51
Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne http://www.comp.dit.ie/pbrowne/ Room K308 Based on Chapter 8. A Logical approach to Discrete Math By David Gries and Fred B. Schneider

Upload: brook-murphy

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Computing Fundamentals 1 Lecture 4

Quantification

Lecturer: Patrick Brownehttp://www.comp.dit.ie/pbrowne/

Room K308Based on Chapter 8.

A Logical approach to Discrete Math By David Gries and Fred B. Schneider

Page 2: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

• We now study quantifiers of operators. These can be logical (e.g. ˄) or arithmetic (e.g. +) operators that act on a range of objects named by ‘dummy variables’. We use the symbols and ∃ ∀ to indicate that a variable refers to some object(s) or all objects.

• ∀ is called the universal quantifier.• ∃ is called the existential quantifier.• Quantifiers can be applied to Symmetric and

Associative operations that have an Identity operation.

Quantification

Page 3: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

• We have already used expressions with constants and variables. We briefly mentioned functions application.

• A function is a rule for computing a result-value v from argument-value w, e.g.

• Definition of: g(z) = 3 ∙ z + 6 • The argument is designated in a function

application, which is a form of expression.

Functions

Page 4: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

• We apply function to argument (function application)• g(5)• Gives the value of 3 ∙ 5+6• To reduce brackets we can write function.argument. • We evaluate this function• g.5• = < Apply function>• 3 ∙ 5 + 6• = < Arithmetic>• 21• Function application can be defined as textual

substitution.• g.z:E

Functions

Page 5: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

• Function application can be defined as textual substitution. If

• g.z:E• Defines a function g then function

application g.X for any argument X is defined by

• g.X = E[z := X]

Functions

Page 6: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Functions

• This close correspondence between function application and textual substitution suggests that Leibniz (1.5) links equality and function application. So, we can reformulate Leibniz for functions.

g.Yg.XYX

Page 7: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Functions & Types

• Up to now we have used the Boolean type. • In Computer Science many types are

needed. – Simple types like integers and natural numbers– Complex types like sets, lists, sequences.

• Each function has a type which describes the types of its parameters and the type of its result:

• (8.1)1 f:arg1 arg⨯ 2 ... arg⨯ n r⟶

Page 8: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Functions & Types

• Some function signatures:• plus: (plus(1,3)or 1+3)ℤ⨯ℤ ⟶ℤ• not : ⟶ (not(true) or true)• less: ℤ⨯ℤ ⟶ (less(1,3)or 1<3)

Page 9: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Functions & Types

• Type and type correctness are syntactic notions. Type correctness depends only on the sequence of symbols in the proposed expression, and not on evaluation of the expression (in a state). For example1, (1/(x: )): ℤ ℝ is an expression even though its evaluation is undefined if x=0.

Page 10: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Functions & Types

• Certain restrictions are need to insure expression are type correct.

• During textual substitution E[x := F], x and F must have the same type.

• Equality b=c is defined only if b and c have the same type. Treating equality as an infix function:

• _=_ :t t ⨯ ⟶ . For any type t.• See exercise 13 in lab1. Checking the sort (or

type) of a term. This same-type restriction is relaxed in Order Sorted Algebras, where we have sub-types and super-types.

Page 11: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Syntax & interpretation of quantification

• Summation notation:• ∑n

i=1 expression • ∑3

i=1(i2) = 12+22+32

• In linear notation:• (∑i | 1 ≤ i ≤ n : e)• Or• (+i | 1 ≤ i ≤ n : e)• Where e is some expression (aka body) like i2

Page 12: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

General form of Quantification

Type separators

Range

Body Separators

(*x : type | R : P)

Binary operator

Dummy or quantified variable

Page 13: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Universal Quantifier

• The declaration introduces a typical term (or body) that may be optionally constrained or restricted in some way: For example:

( i: | i<10 : i∀ ℕ 2 < 100 )

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

Page 14: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Syntax & interpretation of quantification

• We use linear notation:• (+i | 1 ≤ i ≤ n : e)• The scope of the quantified variable i is

confined to expressions within the brackets. In this case quantified variable is know as a dummy, it does not obtain a value from the state in which the expression is evaluated. The dummy is used to represents a range of values.

Page 15: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Syntax & interpretation of quantification

• The range in linear notation can include any Boolean expression:

(+i | 0 ≤ i ≤ 7 ˄ even.i : i)

= 0 + 2 + 4 + 6• The above sums only even numbers

between 1 and 7 inclusive.

• See sumev in next slide.

Page 16: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Sum even numbers 0-n• mod SUM {• pr(INT)• op sumev : Int -> Int• op even_ : Int -> Int• var n : Int• **> Even returns n if n is even otherwise returns 0.• ceq even n = n if (n rem 2 == 0) .• ceq even n = 0 if (n rem 2 =/= 0) .• **> Sum of even numbers between 0 and n• -- base case• eq sumev(0) = 0 .• -- the general case• ceq sumev(n) = (even n) + sumev(n - 1) if (n =/= 0) .• }• red in SUM : sumev(7) . – gives 12

Page 17: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Syntax & interpretation of quantification

• We can have more than one dummy variable:

(+i,j | (1 ≤ i ≤ 2) ˄ (3 ≤ j ≤ 4): ij)

= 13 + 14 + 23 + 24 • The above sums ij over the given range

of i and j.

Page 18: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Syntax & interpretation of quantification

• Moving from summation we let * represent any binary operator (not just +).

• We consider symmetric associative binary operators that have an identity u.

• Symmetry: b * c = c * b• Associative: (b*c) * d = b * (c*d)• Identity u: u * b = b = b* u

Page 19: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Syntax & interpretation of quantification

• (8.6) (*x:t1,y:t2 | R : P)• The bound or dummy or quantified variables x

and y are distinct.• t1 and t2 are the types of the variables.• R is a Boolean expression; the range of the

quantification. x and y must satisfy R.• The expression P is the body of the

quantification.• The type (or sort in CafeOBJ terms) of the result

is the type of P.

Page 20: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Syntax & interpretation of quantification

• Specific instances of the general (8.6)1 (+i | 0 ≤ i < 4 : i∙8) = 0∙8 + 1∙8 + 2∙8 + 3∙8

(˄i | 0 ≤ i < 2 : i∙d≠6) = 0∙d≠6 ˄ 1∙d≠6 (⋁i | 0 ≤ i < 21 : b[i] = 0) = b[0]=0 ⋁ … ⋁ b[20]=0

Dot ∙ denote multiplication.

The variable d is not quantified.

b[i] is an array

Page 21: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Syntax & interpretation of quantification

Different representations of common quantifications

(+x | R : P) (∑x | R : P)

(∙x | R : P) (∏x | R : P)

( x | R : P)⋁ ( x | R : P)∃

(˄x | R : P) ( x | R : P)∀

Page 22: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Scope, Free, Bound

• There are two ways in which a variable can occur in a formula: free or bound.

• A variable is said to occur free in a expression E if and only if it is not within the “scope” of a quantifier.

• If a variable never occurs free in E, then we say the variable is bound.

Page 23: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Scope

• (8.7) (˄i | :x∙i = 0 )• (8.7) asserts that x multiplied by any integer

equals 0. This is only true for x=0. Expression depends on value of x not i.

• The occurrence of x in (8.7) is free.• The scope of the dummy i is the range and the

body of (8.7)• Occurrences of i in the scope of the dummy i

are said to be bound to dummy i.

Page 24: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Avoid Confusing Scope

• (8.8) i>0 ⋁ (˄i | 0≤i : x∙i=0)

• Two distinct variables both named i.

• In (8.8) the leftmost iis free.

• The second quantified i is bound.

• Best avoid this situation by renaming.

• Scope in quantified expressions can be considered similar to the concept of scope in programming languages.

Page 25: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Scope in a procedure1

procedure p(x : integer);var i:integerbegin i := x * y; x := 2 * I;end;• If variable is not local or an argument then it is free in a

function or procedure. Above, the scope of i is the procedure body, and the argument x is bound to the procedure p. In contrast y is free (it comes from outside the procedure, out of the blue, aka global). Note that a procedure differs from a function, it does not have to return a value, rather it can perform operations (e.g. open a file).

y = 3

def p(x): global y i = x * y x = 2 * 1 return

Page 26: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Quantifiers bind variables

• The quantifiers and are the binding ∀ ∃operators.

Page 27: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Free1

• The formal definition of free: • (8.3) The occurrence of i in expression i

is free.• (8.4) If i is free in E then that same

occurrence is free in (E) , f(..,E,..), (*x | E:F), and (*x | F:E) provided that iis not on one of the dummies in list x. It is best to keep variable names distinct.

Page 28: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Occurs

• We define the predicate: • occurs(‘v’,’e’)• To mean that at least one variable in the list ‘v’ of

variables occurs free in at least one expression in the expression list ‘e’.

• The quotes mean we are considering a list of variables and a list expressions rather than values.

• Here we are interested in the occurrence of the variable that may be bound or free not the variable itself. An occurrence is in an expression. We are just talking about scope.

Page 29: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Bound

• (8.10) Let an occurrence of i be free in expression E. That occurrence of i is bound (to dummy i) in the expression (*x | E:F) or (*x | F:E) provided that i is one of the dummies in list x.

• Suppose an occurrence of i is bound in expression E. Then it is also bound to the same dummy) in (E) , f(..,E,..), (*x | E :F), and (*x | F:E).

Page 30: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Example

i + j + (∑i | 1≤ i ≤ 10 : b[i]j)+(∑i | 1≤ i ≤ 10 : (∑j | 1≤ j ≤ 10 : c[i,j]))On first line i and j are freeOn the second line j is free.All other occurrences of i & j are boundThere are two distinct dummies called i.

Page 31: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Textual Substitution

• Textual substitution that worked for propositions can be extended to cover quantification.

• (8.11) provided occurs(‘y’,’x,F’) (*y|R : P)[x:=F] = (*y|R[x:=F] : P[x:=F])

• The caveat in (8.11) means that the dummy of list y will have to be replaced by a fresh variable if that dummy occurs free in x or F. A fresh variable is a variable that does not already occur in the expression under consideration.

Page 32: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Textual Substitution =>

(+x|1 ≤ x ≤2:y)[y:=y+z] (+x|1 ≤ x ≤2:y+z)

(+i| 0≤i<n:b[i]=n)[n:=m] (+i| 0≤i<m:b[i]=m)

(+y| 0≤y<n:b[y]=n)[n:=y](+j| 0≤j<n:b[j]=n)[n:=y]

(+j| 0≤j<y:b[j]=y)To avoid clash use variable j instead of y.

(+y| 0≤y<n:b[y]=n)[y:=m] (+j| 0≤j<n:b[j]=n)Fresh variable j

Page 33: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Textual Substitution

=>(*x | 0≤x+r<n : x+ k)[k:=6] (*x | 0 ≤ x + r < n : x+6)

(+y | 0≤y<m : b[y]=m)[m:=y] (+k | 0 ≤ k < y : b[k] = y)

Page 34: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Quantified Textual Substitution Example

• (*x | 0 ≤ x + r < n : x + v)[v := 3]

• Solution• (*x | 0 ≤ x + r < n : x + 3)

• (+y | 0 ≤ y < m : b[y]=m)[m:=y]

• Solution: needs renaming of quantified variable y to k.

• (+k | 0 ≤ k < y : b[k] = y)

Page 35: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Rules about quantification

• Recall, for quantification we are interested in symmetric associative binary operators that have an identity u.

• Symmetry: b * c = c * b• Associative: (b*c) * d = b * (c*d)• Identity u: u * b = b = b* u• We need additional inference rules for

quantified expressions.

Page 36: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Inference Rules about quantification (8.12)

):]:[|(*):]:[|(* SQzExSPzEx

QP

]):[:|(*]):[:|(*

)(

QzERxPzERx

QPR

Page 37: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

• 8.22 allows the change of dummy variable.

• 8.22 provided occurs(‘y’,’R,P’) (*x|R:P) = (*y| R[x:=f.y] : P[x:=f.y])

Where f is a function with an inverse i.e.

(x = f.y) (y = f-1.x)

(4 = squared(2)) (2 = sqroot(4))

Page 38: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Proof of 8.22Start with RHS: (*y|R[x:=f.y] : P[x:=f.y])= < (8.14) > (*y|R[x:=f.y] : (*x |f.y : P)= < (8.20) > (*x,y|R[x:=f.y] ˄ x = f.y : P)= < (3.84a) > (*x,y|R[x:=x] ˄ x = f.y : P)= < (8.20) > (*x|R:(*y = f.y : P))= < (8.14) inverse of f > (*x|R:(*y | y =f-1.x : P))= < (8.14) > (*x|R: P[y:=f-1.x])= < textual substitution > (*x|R:P)

Page 39: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Splitting ranges

• In computer science ranges are often split for operations that involve repetitions or loops, especially when determining the invariant of such a repetition1.

• An invariant of a loop is a predicate that that holds true for every repetition step and describes a kind of overall property of the whole loop.

Page 40: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Manipulating ranges

• Manipulating ranges allows us to split a range into two distinct ranges. The first example below takes one right hand (n+1) term out of the range. The second example below takes one left hand (0) term out of the range.

Page 41: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Manipulating ranges

(∑i | 0≤i<n+1:b[i])sum

(∑i| 0≤i<n: b[i]) + b[n]

(∏i| 0≤i<n+1:b[i])product

b[0]∙(∏ i | 0≤i<n : b[i])

(∀i| 0≤i≤n:b[i]=0)For all

(∀i| 0≤i<n:b[i]=0) ˄ b[n]=0

(∏i|5≤i≤10:i2)

product

52 ∙(∏i|5<i≤10:i2)

Page 42: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Methods for describing ranges

A 2 ≤ i ≤ 15

B 2 ≤ i < 16

C 1 < i ≤ 15

D 1 < i < 16

Various ways to describe i for values 2..15

For B and C the number of the number of values of i in the range is equal to the upper bound minus the lower bound (16-2 or 15-1).

Page 43: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Collapse or Split ranges

A 2 ≤ i ≤ 15 ⋁ 16 ≤ i ≤ 20 2 ≤ i ≤ 20

B 2 ≤ i < 16 ⋁ 16 ≤ i < 21 2 ≤ i < 21

C 1 < i ≤ 15 ⋁ 15 < i ≤ 20 1 < i ≤ 20

D 2 < i < 16 ⋁ 15 < i < 21 1 < i < 21

Page 44: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Substitutions in Quantification

• (*x | 0 ≤ x + r < n : x + v)[v := 3]

• Solution• (*x | 0 ≤ x + r < n : x + 3)

• (+y | 0 ≤ y < n : b[y]=n)[n:=y]• Solution• (+j | 0 ≤ j < y : b[j] = y)

Page 45: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

English Quantification

• All even integers that are multiples of 2 • ( x∀ : ℤ | even.x:multiple(x,2))

Page 46: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

English Quantification

• Express this set in English• {x:ℤ+ | 0 ≤ x < 4}• The set of non-negative integers less than

4.

Page 47: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

English Quantification

• The set of positive even integers that are less than 26.

{x: + ℤ | 0<x<26 ˄ (x mod 2 = 0) : x}

• The set of odd integers.• {x: ℤ | (x mod 2 = 1) : x}• OR• {x: ℤ | : 2*x-1}

Modulus or remainder function.

red in INT : 3 rem 2 .

red in INT : 2 rem 2 .

red in INT : 2 quo 2 .

red in INT : 2 divides 2 .

Page 48: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

English Quantification

• Express this in English• {z | ( x,y ∃ | 0≤x ˄ 2≤y≤ 3 : z = xy}

• The set of squares and cubes of all natural numbers.

• All even integers are multiples of 2.• ( x: ∀ ℤ | even.x : multiple(x,2))• Where multiple(x,2)is a predicate that returns

true if x is a multiple of 2 (or 2 goes into x evenly)

Page 49: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

English Quantification Example

• Some integer between 50 and m is a multiple of y.

• (∃i: ℤ | 50 ≤ i ≤ m : mutiple(i,y))

• Every integer is larger than one integer and smaller than another integer.

• ( z:∀ |:ℤ ( w,v∃ : | w≠v : w < z < ℤv))

Page 50: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Quantification and Programming Language C.

void P(int x) { int i; i = x*x; x = i*2;}• The scope rules for quantification are similar to

local variables in a C like language. The scope of i is P in the C function, in maths:

• (*i | R : P)• We ignore call by value and call by reference

issues.

Page 51: Computing Fundamentals 1 Lecture 4 Quantification Lecturer: Patrick Browne  Room K308 Based on Chapter 8. A Logical approach

Set Construction as a specification