Exer ises in Quanti er Manipulation

Roland Ba khouse 

Diethard Mi haelis

y

February 6, 2006

Abstra t
The Eindhoven quanti er notation is systemati , unlike standard mathemati ial
notation. This has the major advantage that al ulations with quanti ed expressions
be ome more straightforward be ause the al ulational rules need be given just on e
for a great variety of di erent quanti ers.
We demonstrate the ease of al ulation with nite quanti ations by onsidering
a number of examples. Two are simple warm-up exer ises, using boolean equality as
the quanti er. Three are taken from books of hallenging mathemati al problems,
and one is a problem on o ted by the authors to demonstrate the te hniques.

1 Introdu tion
Quanti ations, both logi al ( 8 , 9 ) and arithmeti (  ,  , et .) abound in program
spe i ations, but the manipulation of quanti ed expressions is rarely dis ussed in any
detail. (An important ex eption is [GKP89℄.) Moreover, many texts (in mathemati s and
omputing) adopt di erent (unsystemati ) notations for di erent quanti ers, obs uring
the ommonalities in their properties. Even worse, the dotdotdot (\ : : : ") notation is
often used, leading inevitably to mistakes1. In ontrast, the \Eindhoven" quanti er
notation is a uniform notation for expressing quanti ations over an arbitrary abelian
monoid. It has been used sin e the early 1970s [Dij75, Dij76℄. Initially, little or no
explanation of the rules for manipulating quanti ers was given, ex ept for some of the 
S hool

of Computer S ien e and Information Te hnology, University of Nottingham, Nottingham NG8

1BB, England

y Beethovenstr.

1 The

55, 90513 Zirndorf, Germany

website

\The

most

ommon

www.vanderbilt.edu/~s he tex/ ommerrs

errors

reports

\many

in
errors"

undergraduate
in

the

use

of

mathemati s"
dotdotdot

nota-

tion (\ellipses"). Of ourse, the error is the tea her's use of the notation, and should not be blamed on
the student!

1

2
most basi (like empty range and range splitting [Gri81℄). Ba khouse [Ba 86℄ ompiled
a list of rules for nite quanti ations, partly based on the rules given by Knuth for
summation [Knu68℄; these were re ently updated [Ba 03℄ bringing the naming of the
rules into line with that used elsewhere.
In this paper, we demonstrate the importan e of manipulating quanti ers using a
number of exer ises drawn from books about hallenging mathemati al problems. Our
al ulations lead to straightforward solutions to the problems and, in one ase, to a new
theorem. The al ulations are goal-dire ted and, we hope, may be of bene t to tea hing
the al ulational method.
Full details of the notation and the rules we use are given in [Ba 03℄.

2 Warm-up Exer ises
A standard exer ise is to prove that the parity of a sum of a nite bag of integers is odd
equivales the number of odd elements is odd. This is a ni e warm-up exer ise in the use
of the quanti er al ulus:
=

=

=

=

even: hk :: nki
f
even distributes through summation g
hk :: even:nki
f
trading, even:nk  (odd:nk ) false) g
hk : odd:nk : falsei
f
false  even:1 g
hk : odd:nk : even:1i
f
even distributes through summation g
even: hk : odd:nk : 1i :

The al ulation uses two quanti ers: the familiar sum (  ) quanti er, and the unfamiliar
equivales (  ) quanti er. Possibly also unfamiliar is the distributivity of even over nite
summation. This is a onsequen e of two properties:

even:0  true ;
(the unit of addition is mapped to the unit of equivales) and2

even:(m+n)  even:m  even:n
2 O urren es

of the binary equivales symbol \

tively. O urren es of the equality symbol \

= 

"

and, later, inequivales \

6 "

" should be read onjun tionally.

should be read asso ia-

3
(binary addition is mapped to binary equivales). The \trading" rule for equivales (used
in step 2) is like the trading rule for universal quanti ation. An impli ation, P) , in
the term is \traded" into a onjun t, P ^ , in the range. (Although we don't use it here,
it is worth pointing out that the trading rule for inequivalen e is like the trading rule
for existential quanti ation | a onjun t is traded with a onjun t.)
Note that a spe ial ase for an empty bag does not need to be made. Even in this
ase, all quanti ations are well de ned; an equivales-quanti ation over an empty range
is true .
It's useful to take this example a little further. Note that hk : odd:nk : 1i is a ount
of the odd numbers in the bag. Sometimes, we want to ount the number of values in a
given nite range that satisfy a given property p . The following is useful.
=

=

even:hk : pk : 1i
f
even distributes through summation g
hk : pk : even:1i
f
trading, even:1  false , (q)false)  q  false g
hk :: pk  falsei
f

=

=

asso iativity and symmetry g

hk :: pki  hk :: falsei
f
false  even:1 , distributivity g
hk :: pki  even: h k :: 1i :

In summary,

even:hk : pk : 1i  hk :: pki  even: hk :: 1i :
(In parti ular, pk an be instantiated to odd:nk .)
(1)

The equivales-quanti er will be used again later. Be ause it is unfamiliar, we o er
the following exer ise relating it to the di erent-from ( 6 ) quanti er (more ommonly
known as ex lusive-or).
The problem is to relate hk :: pki to h6k :: pk i . Noting the distributivity laws:
:(p  q)  :p 6 :q , and :true  false , negation distributes through an arbitrary nite
equivales-quanti ation turning it into a di erent-from quanti ation. So,

h6k :: pki
=

f

double negation, distributivity g

: hk :: :pki

4
=

f

de nition of negation g

f

asso iativity and symmetry of equivales g

hk :: pk  falsei  false
=

=

hk :: pki  hk :: falsei  false
f
hk::falsei = even: h k :: 1i , odd
hk :: pki  odd: h k :: 1i :

is :even

g

We on lude that

h6k :: pki  hk :: pki  odd: hk :: 1i :

(2)

3 Even Numbers of Even Di eren es
The following exer ise was designed by the authors to illustrate manipulations with
quanti ers.
Suppose fnk j 0  k < Mg is a bag of integers. Consider the bag of di eren es

fnj-nk j j < kg :
(For brevity, we assume throughout that the type of dummies j and k is the set of the
rst M natural numbers.) The question is: how an we evaluate whether the number of
even di eren es is even with minimal e ort? To resolve this question, we try to simplify
the spe i ation:

even: h j;k : j < k ^ even:(nj-nk ) : 1i :
We begin our al ulation with a use of the warm-up exer ises.

even: h j;k : j < k ^ even:(nj-nk) : 1i
=

f

(1)

g

h j;k : j < k : even:nj  even:nki  even: h j;k : j < k : 1i

even over summation g

even: h j;k : j < k : nj+nki  even:  j;k : j < k : 21 + 12 :

We now al ulate the value of h j;k : j < k : nj +nk i . (The value of  j;k : j < k : 12 + 12
is easily seen to be the sum of the rst M natural numbers, whi h is well known to
be 12  (M-1)  M . It is amusing to note, however, that it is a spe ial ase of the rst
=

f

distributivity of

summation, as made obvious by the way it has been written. The al ulation below is
an attra tive way of deriving the standard formula. )

5

h j;k : j < k : nj+nki
f

=

aiming to eliminate the awkward range restri tion,
we exploit symmetry of addition g 

h j;k : j < k _ k < j : nj +nki
f
j < k _ k < j  :(j = k) , range splitting g
1
2  (h j;k :: nj +nk i - h j;k : j = k : nj +nk i)
1
2

=

=

f

asso iativity and symmetry, one-point rule

f

dummy renaming and distributivity g

f

arithmeti , nesting and asso iativity and symmetry g

f

distributivity (of produ t over addition) g

f

range of

1
2 

(h j;k :: nji + h j;k :: nk i - hj :: 2  nji)

1
2 

2  (h j;k :: nji - hj :: nji)

=

=

g

hj :: hk :: nji - nji
=

hj :: nj  (hk :: 1i - 1)i
=
(

M-1)  hj :: nj i :

k is 0  k < M , distributivity g

Consequently,

even: h j;k : j < k : nj+nki  odd:M _ even: hj :: nj i :
Similarly, we have:

even: h j;k : j < k : 1i
=

above with

f

ase analysis on

even:( 12  (M-1)  M)
=

nj = 12 , or standard formula g

f

even:b M2 :

even:M g

Substituting, we have determined that 

even: h j;k : j < k ^ even:(nj-nk ) : 1i
odd:M _ even: hj : odd:nj : 1i  even:b M2 :

6
We on lude that, when M is even, the evenness of the number of even di eren es is
determined by rst determining whether the number of odd numbers is even, and then
omparing this boolean for equality with even:b M2 . That is, we evaluate:

even: hj : odd:nj : 1i  even:b M2 :
In the ase that M is odd, the answer is independent of the numbers in the bag; it is
simply

even:b M2 :
An interesting feature of this example is that we have hosen to simplify summations
rather than to use the equivales quanti er. The ru ial step of eliminating the awkward
range restri tion, j < k , exploits a property of addition that has no ounterpart for
equivales. This extra degree of al ulational freedom signi antly enhan es the elegan e
of the al ulation.

4 1906 Hungarian Contest Problem
Exer ise [Zei99, 3.4.8℄ is a spe ial ase of the following.
Suppose a bag of M numbers is given. Let a and b be both arbitrary arrangements
of the bag. Prove that, if M is odd, the produ t

a -b1)(a2-b2)(a3-b3) : : : (aM-bM)

( 1

is an even number. (In [Zei99, 3.4.8℄, the bag is the set of numbers 1 , 2 ,
The al ulational solution is as follows.
=

(

=

even: hk :: ak-bki
f
even distributes through produ t and summation g
h9k :: even:ak  even:bki
f

dis ussed below g

f

(2)

f

asso iativity and symmetry of equivalen e g

h6k :: even:ak  even:bki
g

hk :: even:ak  even:bki  odd: hk :: 1i
=

hk :: even:aki  hk :: even:bki  odd: hk :: 1i

=

: : : , M .)

a and b are rearrangements of the same bag g
odd: hk :: 1i :
f

7
The se ond step of this al ulation is the most risky be ause it is a strengthening step.
It is suggested by the goal of separating the two operands of the equivalen e in the term
of the quanti ation. Formally, the property used is that, for all predi ates p ,
(3)

h9k :: pki

(

h6k :: pki

In words, (3) is read as an in lusive-or is weaker than an ex lusive-or. Its truth is established by observing that the ontrapositive law, h8k :: pk i ) hk :: pk i for all predi ates
p , is an immediate onsequen e of Leibniz's rule. (Substitute true for pk in the right
side of the impli ation.)
A spe ial ase of (3) is

even: hk :: nki ^ odd: hk :: 1i :
(Take pk to be even:nk and use distributivity of even through summation and (2).
(4)

h9k :: even:nki

(

The property is better known in the ontrapositive form: the sum of an odd number of
odd numbers is odd.) In this way, our proof is dire tly omparable to Zeitz's solution 2
[Zei99, 3.4.8℄. The di eren e is that Zeitz's solution has a \rabbit"3: an (impli it) proof
by ontradi tion is used in order to repla e multipli ation by addition. Our solution
repla es disjun tion by equivales, but the repla ement is suggested dire tly by the shape
of the formulae.

5 Summing Absolute Di eren es
In this se tion and the next, we onsider two problems taken from [SA03℄ that involve
sums of absolute di eren es. We present al ulational solutions. Our al ulations lead
to theorems that are stronger than those stated in [SA03℄.
It is useful to \lift" a binary operator  on numbers to bags of equal size by de ning,
for bags a and , ea h of size m ,

a

=

hBAG i : 0  i < m : ai ii :

We extend unary operators to bags (in parti ular, the absolute value operator) in the
same way. Thus, ja- j denotes the bag of absolute di eren es

hBAG i : 0  i < m : jai- iji :

Quanti ers are fun tions from bags to values. For example, the sum quanti er, \  ",
maps a bag of numbers to a number. Combined with lifting, this gives the on ise
notation  ja- j for h i : 0  i < m : jai - i ji .
This se tion's problem is as follows. The numbers 1 , 2 , 3 , : : : 2N are divided
over two sequen es a and b , ea h of length N , in su h a way that a is in reasing and
b is de reasing. Prove that
3 Zeitz's

solution begins with the words \The rux move: onsider the

sum

of the terms".

8 

ja-bj

=

N2 :

The rst step in the solution is to \disentangle" summation and absolute values. Let
and b be arbitrary equal-length sequen es of numbers. Then 

ja-bj
=

f

de nition of absolute value

f

asso iativity and symmetry of summation, 

((a"b) - (a#b))
=

a

g

distributivity of negation over sum g 

(a"b) - (a#b) :

a and b of equal length, 
(a"b) - (a#b) :

In summary, for arbitrary sequen es 

ja-bj

(5)

=

Ex ept for their having equal length, the above al ulation makes no assumption about
the sequen es a or b . We now take this information into a ount.
Suppose i and j index elements of the sequen es. Then

ai#bi < aj"bj
f

=

=

(

distributivity of ( <x ) over minimum,
and ( x< ) over maximum (for any

x) g

ai < aj _ ai < bj _ bi < aj _ bi < bj
f
a is in reasing, b is de reasing g
i < j _ (ai < bj _ bi < aj) _ j < i
f
h8 k :: ak 6= bki . I.e. h8 k :: ak < bk _ bk < aki
i<j _ i=j _ j<i
f

=

true

:

g

inequalities g

The on lusion is that
(6)

h8 i;j :: ai#bi < aj"bji

where dummies i and j range over indexes of a and b . The signi an e of (6) is that 
(a"b) is the sum of the N largest elements in the on atenation of the two sequen es
(where N is the length of sequen e a ), and (a#b) is the sum of the N smallest
elements. Thus, ombining (6) with (5), we get the following theorem.

9
Theorem 7 Suppose 2N distin t numbers are divided over two sequen es a and
b , ea h of length N , in su h a way that a is in reasing and b is de reasing. Then, 
ja-bj is the di eren e between 
the sum of the

N largest

and 
the sum of the

N smallest

of the given numbers.

2

Applying this theorem to the ase that the numbers are 1 , 2 , 3 , : : : , 2N , using
the well-known formula for the sum of 1 , 2 , 3 , : : : , n , we get that ja-bj is
1

(2 

(2N)(2N + 1) - 21 N(N+1)) - 12 N(N+1) ;

whi h simpli es to N2 . (Note that N may be zero.)
Our al ulation does not presuppose a knowledge of the answer. The most ru ial
step is the rst one, in whi h the arithmeti involved in al ulating absolute di eren es
is disentangled from summation of a sequen e. The symmetry between minimum and
maximum in (5) suggests the al ulation that follows it.

6 A De Morgan-Like Theorem
For our nal exer ise, we onsider the following problem [SA03℄.

An arbitrary set of m+n numbers is divided into two arbitrary groups a1 , a2 ,
: : : , am and b1 , b2 , : : : , bn , and the numbers in ea h group arranged in as ending
order

a1 < a2 < : : : < am ; b1 < b2 < : : : < bn :
Then the same numbers are again divided into two arbitrary groups 1 , 2 , : : : ,
m and d1 , d2 , : : : , dn , and the numbers in ea h group arranged in as ending
order

1 < 2 < : : : < m ; d1 < d2 < : : : < dn :
Prove the equality

ja1- 1j + ja2- 2j + : : : + jam- mj

=

jb1-d1j + jb2-d2j + : : : + jbn-dnj :

10
Our solution to the problem involves identifying a novel theorem on as ending sequen es. We also eliminate the requirement that all the numbers are distin t. This leads
to novel appli ations (omitted here for brevity).
We assume that we are given a nite bag U of numbers. We assume that the elements
of the bag are (totally) ordered by the relation  . If is a subbag of U , we use #
to denote its size. The omplement of (in U ) is denoted . For all i , 0  i < # ,
the i th element of in the resulting ordered sequen e is denoted by :i . Formally, we
have, for all subbags of U ,

h8 j;k : 0  j  k < # : :j  :ki :

(8)

When U is a set (i.e. there are no dupli ates in the bag), we have the stronger: for all
subsets of U ,

h8 j;k : 0  j < # ^ 0  k < # : j < k  :j  :ki :

(9)

As in se tion 5, we \lift" operators to map sequen es to sequen es. In general, it is
not the ase that the indexing fun tion ( :i ) distributes through lifted operators. For
example, ja- j:i need not be equal to ja:i - :ij . It is the ase, however, for minimum
and maximum (with respe t to  ), be ause they are monotoni . This is important.
We are now in a position to reformulate the problem: An arbitrary bag U of numbers
is split into subbags a and a . The same bag U is split again into b and b , where
#a = #b . Prove that 

ja-bj

(10)

= 

ja-bj :

Before beginning the al ulation, we observe two ompli ations: 
In general, the two sums range over di erent numbers of elements. 
Order (absolute value) and arithmeti (addition and subtra tion) are heavily en-

tangled.

The rst step is thus to improve the situation: 

ja-bj

=

f

= 

ja-bj

de nition of absolute value

h i :: a:i " b:i - a:i # b:ii
=

f

=

g 

i :: a:i " b:i - a:i # b:i

for all i , ( :i ) distributes through max and min,
asso iativity and symmetry of addition g

= 

(a"b) -  (a#b)

= 

(a"b) +  (a#b)

=

f 

(a"b) -  (a#b)

arithmeti g 

(a"b) +  (a#b) :

11
This is mu h better. Both sides have #U summands, and they di er only by omplementation. This suggests a bold step:
( 

(a"b) +  (a#b) =  (a"b) +  (a#b)
f 
U =  (a"b) +  (a"b) =  (a#b) + (a#b) g
a"b = a#b ^ a#b = a"b :

We have thus redu ed the original problem to establishing the following theorem on
in reasing sequen es.
Theorem 11 (\De Morgan's Rule")
totally ordered bag U :

a"b

=

a#b and a#b

=

For all equal-length subbags

a and b of a

a"b .

2

We rst prove the theorem assuming that U is a set, and then show how to extend
the proof to bags. Clearly, it suÆ es to prove just the rst onjun t. (The se ond is
obtained from the rst by the repla ements a;b := a;b .)
We have:

a"b
=

=

a#b

f

#(a"b)

f

monotoni ity of min and max g

=

#(a#b) , omplementation g

8 i;j :: (a"b):i 6= (a#b):j

=

8 i;j :: a:i " b:i 6= a:j # b:j :

i and j . We show that a:i " b:i = a:j # b:j implies false.
a:i " b:i = a:j # b:j
f
a:i 6= a:j , b:i 6= b:j (be ause a and b are sets) g
(a:i  b:i = a:j  b:j)
_ (b:i  a:i = b:j  a:j) :

Now onsider arbitrary
=

By symmetry, it suÆ es to prove that just one of the two disjun ts implies false. We
pro eed with the rst.

a:i  b:i
=

f

=

a:j  b:j

disentangling

a from b g

12

a:i  a:j ^ a:j = b:i ^ b:i  b:j
=
f
a and a are disjoint, as are b and b g
a:i  a:j ^ a:j = b:i ^ b:i  b:j :
The next step is ru ial. The property a:i  a:j implies that (i+1) + j elements of U
are less than a:j ; similarly, the property b:i  b:j implies that (#b - 1 - i) + (#b - j)
elements of U are greater than b:i . Formally:
a:i  a:j
f

=

(9) and transitivity of 

g

h8k : k  i : a:k  a:ji ^ h8k : k < j : a:k  a:ji
)

a and a are disjoint subsets of U g
hk : U:k  a:j : 1i  (i+1) + j :
f

Similarly,

b:i  b:j
=

) 

g

h8k : i < k : b:i  b:ki ^ 8k : j  k : b:i  b:k
f

(9) and transitivity of

b and b are disjoint subsets of U g
hk : b:i  U:k : 1i  (#b - 1 - i) + (#b - j) :
f

We now ombine these two ounts with the middle onjun t:

a:j = b:i .

a:i  a:j ^ a:j = b:i ^ b:i  b:j
)

=

f

above, Leibniz g

f

b:i is an element of U g

hk : U:k  b:i _ b:i  U:k : 1i  (i+1) + j + (#b - 1 - i) + (#b - j)
f
arithmeti , #b + #b = #U g
hk : U:k =6 b:i : 1i  #U

=

false

:

This ompletes the proof of theorem 11, assuming that U is a set. Given a bag U ,
this lemma an be applied to the sets of pairs ( U:i ; i ) ordered lexi ographi ally by
(x;i)  (y;j) equivales x  y _ (x = y ^ i  j) .
It is interesting to ompare our al ulation with the argument given by Sav hev and
Andrees u [SA03℄. Their argument starts immediately with indu tion and a subsequent

13
ase analysis. Our al ulation is driven by heuristi prin iples, whi h lead to the dis overy of the mu h more general De Morgan-like theorem. Their argument has 10 lines
of displayed formulae (in luding 16 o urren es of ellipsis dots) and 20 lines of printed
text. Ours is mu h more detailed, but has just 16 al ulation steps, and a similar
amount of text (in luding the hints a ompanying al ulation steps). Their argument
assumes that U is a set, and is split into non-empty subsets; our al ulation assumes
that U is a bag, that is split arbitrarily.

7 Con lusion
Manipulating quanti ed expressions is an important al ulational skill. Elsewhere, Knuth,
Patashink and Graham [GKP89℄ also emphasise its importan e and give several examples
of non-trivial al ulations with the Sigma quanti er. We have provided further eviden e
for the importan e of the skill by ta kling a number of hallenging mathemati al exer ises.
A knowledgements We are grateful to Wim Feijen for bringing the problems in [SA03℄
to our attention, and to Jeremy Weissmann for omments on an earlier presentation of
se tion 5.

Referen es
[Ba 86℄ R.C. Ba khouse. Program Constru tion and Veri ation. Prenti e-Hall
International, 1986.
[Ba 03℄ Roland Ba khouse. Program Constru tion. Cal ulating Implementations
From Spe i ations. John Wiley Sons, Ltd., 2003.
[Dij75℄

Edsger W. Dijkstra. Guarded ommands, nondetermina y and formal derivation of programs. Communi ations of the ACM, 18:453{457, 1975.

[Dij76℄

Edsger W. Dijkstra. A Dis ipline of Programming. Prenti e-Hall, 1976.

[GKP89℄ Ronald L. Graham, Donald E. Knuth, and Oren Patashnik. Con rete Mathemati s. Addison-Wesley, 1989.
[Gri81℄

David Gries. The S ien e of Programming. Springer-Verlag, 1981.

[Knu68℄ Donald E. Knuth. The Art of Computer Programming., volume Vol. I: Fundamental Algorithms. Addison-Wesley Publishing Co., Menlo Park, 1968.

14
[SA03℄

Svetoslav Sav hev and Titu Andrees u. Mathemati al Miniatures, volume 43
of Anneli Lax New Mathemati al Library. The Mathemati al Asso iation of
Ameri a, 2003.

[Zei99℄

Paul Zeitz. The Art and Craft of Problem Solving. John Wiley & Sons In .,
1999.