Download presentation
Presentation is loading. Please wait.
1
Computing Fundamentals 1 Lecture 4 Quantification
Lecturer: Patrick Browne Room K308 Based on Chapter 8. A Logical approach to Discrete Math By David Gries and Fred B. Schneider
2
Quantification 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. O = operations (,, , , ,˄, ˅) ˄ = ˄ ∃∃ ∀ ∀
3
Functions 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. ∙ ∙
4
Functions 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
5
Functions 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]
6
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.
7
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 ⨯ arg2 ...⨯ argn ⟶ r 1In CafeOBJ this would be written op f arg1 arg2 : -> r
8
Functions & Types Some function signatures:
plus: ℤ⨯ℤ ⟶ℤ (plus(1,3)or 1+3) not : ⟶ (not(true) or true) less: ℤ⨯ℤ ⟶ (less(1,3)or 1<3). Where ℤ represents the set of integers. ℤ
9
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. Where ℤ stands for integers and ℝ for real numbers. 1The notation variable:type is common in maths & computing: In CafeOBJ we can write this for Integers (Z) all positive and negative number including zero. red in INT : A:Int quo 0 . – where quo is the quotient or division operator for Integer numbers in CafeOBJ Which returns an error type, denoted by the type name preceded by a question mark. -- Does CafeOBJ consider zero a natural number? red in INT : 0 :is Int . -- In some contexts zero is not legal Or you could try this for rational numbers (Q) red in RAT : x:Nat / 0 . Which also returns an error type, denoted by the type preceded by a question mark. (A / 0):?Rat
10
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 17 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.
11
Syntax & interpretation of quantification
Summation notation: ∑ni=1 expression ∑3i=1(i2) = In linear notation: (∑i | 1 ≤ i ≤ n : e) Or (+i | 1 ≤ i ≤ n : e) Where e is some expression (aka body) like i2 The parentheses make the scope of a quantified or dummy variable explicit. ∑ ≤
12
General form of Quantification
Binary operator Range Type separators (*x : type | R : P) Dummy or quantified variable Body Separators
13
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 : i2 < 100 ) Which states that for all natural numbers less than 10 their square is less than 100.
14
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.
15
Syntax & interpretation of quantification
The range in linear notation can include any Boolean expression: (+i | 0 ≤ i ≤ 7 ˄ even(i) : i) = The above sums only even numbers between 1 and 7 inclusive. See sumev in next slide. Zero is even
16
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 recursive call ceq sumev(n) = (even n) + sumev(n - 1) i f (n =/= 0) . } red in SUM : sumev(7) . – gives 12
17
Syntax & interpretation of quantification
We can have more than one dummy variable: (+i,j | (1 ≤ i ≤ 2) ˄ (3 ≤ j ≤ 4): ij) = The above sums ij over the given range of i and j.
18
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
19
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 specifying 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 terminology) of the result is the type of P.
20
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 Here the dot ∙ denotes multiplication. The variable d is not quantified( not bound). b[i] is an array 1Here the dot ∙ denotes multiplication.
21
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) ∃
22
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. See
23
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. See
24
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.
25
Scope in a program1 procedure p(x : integer); var i:integer begin
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 * i return 1We will ignore call by value and call by reference issues, but if you are interested you can read on. See In computer programming, a free variable is a variable referred to in a function that is not a local variable or an argument of that function. For example, in f(x) = x+ y, x is bound variable and y is free variable. In programming languages, we call these as argument or parameter and local variable respectively.
26
Quantifiers bind variables
The quantifiers ∀ and ∃ are the binding operators for variable occurrences.
27
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. So, dummy are not free they are bound. It is best to keep variable names distinct. 1See In mathematics, and in other disciplines involving formal languages, including mathematical logic and computer science, a free variable is a notation that specifies places in an expression where substitution may take place. The idea is related to a placeholder (a symbol that will later be replaced by some literal string), or a wildcard character that stands for an unspecified symbol. The variable x becomes a bound variable, for example, when we write 'For all x, (x + 1)2 = x2 + 2x + 1.' or 'There exists x such that x2 = 2.' In either of these propositions, it does not matter logically whether we use x or some other letter. However, it could be confusing to use the same letter again elsewhere in some compound proposition. That is, free variables become bound, and then in a sense retire from further work supporting the formation of formulae.
28
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).
29
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. We usually use occurs(‘VarList’,’x,F’) as an occurs check . The occurs check ensures that a variable isn’t bound to a structure that contains itself.
30
Recall: Textual Substitution
31
Occurs Check The occurs check ensures that a variable isn’t bound to a structure that contains itself. In the last two examples, dummy y was first replaced by fresh variable j, as required by the caveat. Changing the dummy ensures that a free occurrence of y in the textual substitution x := F does not become bound. In the last two, dummy y was replaced by fresh variable j. Changing the dummy ensures that a free occurrence of y in the textual substitution x := F does not become bound.
32
Textual Substitution with Quantifiers
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 (). occurs(‘list1’,list2’) means that at least one variable from ‘list1’ occurs in lisr2.I
33
Examples of Quantified 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, no m O = operations (,, , , ,˄, ⋁) ≤ Changing the dummy ensures that a free occurrence of y in the textual substitution x := F does not become bound.
34
Examples of Quantified 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) Fresh dummy variable k. O = operations (,, , , ,˄, ⋁) ≤
35
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)
36
Rules about quantification
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.
37
Inference Rules about quantification (8.12)
As with Leibniz (1.5), we can use the (8.12) inference rules implicitly in substituting equals for equals.
38
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)) Think of f as the square function and f-1 as the square-root function
39
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. 1. From Logical Reasoning: A First Course by R Nederpelt, page 89
40
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.
41
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) 52 ∙(∏i|5<i≤10:i2)
42
Methods for describing ranges
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).
43
Collapse or Split ranges
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 2 < i < 21 B and C are the easiest to manipulate, because the upper bound of the lower adjacent range equals the lower bound of the upper adjacent range.
44
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] (+j | 0 ≤ j < y : b[j] = y)
45
English Quantification
All even integers that are multiples of 2 (∀x:ℤ | even(x):multiple(x,2)) Contrast this with similar statement using ‘set comprehension ‘ Lecture 5 Slide 14.
46
English Quantification
Express this set in English {x:ℤ+ | 0 ≤ x < 4} The set of non-negative integers less than 4.
47
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 . ‘mod’ is the remainder operation for integer division. (‘quo’ or ‘÷’ is the quotient or division) Course text (page 316) defines ‘÷’ and ‘mod’ b ÷ c = q, b mod c = r, where b = q*c + r and 0 ≤ r < c (or more complex ) {x:ℤ + | 0<x<6 ˄ x/2=x÷2 : x} where ‘/’ is division,’÷’ is integer division and ‘=’ is assumed to be overloaded or perhaps an autimatic casting between LHS and RHS.
48
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)
49
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)) An integer n is divisible by a different integer m if n is an integral multiple of m. For example, 15 is divisible by 3 because 15 = 5 · 3. The numbers which divide a given integer are called its divisors; for example, the divisors of 12 are 1, 2, 3, 4, 6, and 12 (which divides itself because 12 = 12 · 1)
50
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. Any occurrence of i outside P is a different i. (*i | R : P) If there is an x in P then x is free in P, because it is not quantified We ignore call by value and call by reference issues.
51
Set Construction as a specification
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.