Professional Documents
Culture Documents
(b;i:e)U]= ! i=j-e
i#j-bU] 0
Notice the similarity between the notation (s; x:v) used in section 4.6
to denote a modified state s and the notation (b; i:e) to denote a modi-
fied array b .
Example 2 illustrates nested use of the notatiotJ.. Since (b; 0:8) is the
array (function) (8,4,6), it can be used in the first position of the nota-
tion. Nested parentheses do become burdensome, so we drop them and
rely instead on the convention that rightmost pairs "i:e" are dominant
and have precedence. Thus the last line of example 2 is equivalent to
(b; 0:8; 2:9; 0:7).
b:=(b; i:e)
Simplifying expressions
It is sometimes necessary to simplify expressions (including predicates)
containing the new notation. This can often be done using a two-case
analysis as shown below, which is motivated by definition (5.1.2). The
first step is the hardest, so let us briefly explain it. First, note that either
i = j or i"# j. In the former case (b; i:5)U] = 5 reduces to 5 = 5; in the
second case it reduces to b U] = 5.
(b; i :5)U] 5=
= (i = j A 5 = 5) v (i "# jAb U] = 5) (DeL of (b; i :5»
= (i =j) v (i"#j A bU]=5) «5 =5) = T, and-simp!.)
= (i = j v i "# j) A (i = j v b U] = 5) (Distributivity)
= T A (i = j v bU] =5) (Excluded middle)
=i=j V bU]=5 (and-simp!.)
92 Part I. Propositions and Predicates
define a type t and two variables p and q with type t. Each variable contains
two fields; the first is named n and can contain a string of 0 to 10 characters
-e.g. a person's name- and the second is named age and can contain an
integer. The following assignments indicate how the components of p and q can
be assigned and referenced. After their execution, both p and q contain 'Hehner'
in the first component and 32 in the second. Note how q.age refers to field age
of record variable q.
An array consists of a set of individual values, all of the same type (the old
view). A record consists of a set of individual values, which can be of different
types. In order to allow components to have different types we have sacrificed
some flexibility: components must be referenced using their name (instead of an
expression). Nevertheless, arrays and records are similar.
Develop a functional view for records, similar to the functional view for arrays
just presented.
Section 5.2 Array Sections and Pictures 93
b[O:n-I] denotes the whole array, while if O~i ~j <n, b[i:j] refers to
the array section composed of b[i], b[i+I], ... , b[j]. If i = j+l, b[i:j]
refers to an empty section of b .
Quite often, we have to assert something like "all elements of array b
are less than x", or "array b contains only zeroes". These might be writ-
ten as follows.
(Ai:O~i<n:b[i]<x)
(A i: 0 ~ i < n: b [i) = 0)
Because such assertions occur so frequently, we abbreviate them; these
two assertions would be written as b <x and b =0, respectively. That is,
the relational operators denote element-wise comparison when applied to
arrays. Here are some more examples, using arrays b [O:n -I] and
c[O:n-l] and simple variable x.
Be very careful with = and #, for the last example shows that b = y can
be different from , (b # y)! Similarly, b ~y can be different from
,(b>y).
We also use the notation x E b to assert that the value of x is equal to
(at least) one of the values b [i]. Thus, using domain(b) to represent the
set of subscript values for b, x E b is equivalent to
(E i: i E domain (b ): x = b [i))
Such abbreviations can make program specification -and understand-
ing the specification later- easier. However, when developing a program
94 Part I. Propositions and Predicates
Array pictures
Let us now turn to a slightly different subject, using pictures for some
predicates that describe arrays. Suppose we are writing a program to sort
an array b[O:n -I], with initial values B[O:n -I] -i.e. initially, b = B.
We want to describe the following conditions:
(I) b [O:k -I] is sorted and all its elements are at most x,
(2) the value that belongs in b [k] is in simple variable x,
(3) every value in b[k+l:n-l] is at least x.
where
ordered(b[O:k -I]) =(Ai: 0 ~i <k -I: b[i] ~b[i+I])
k k+1 n-I
b I I ~x I
o k h n
(a) 0 ~ k ~ h ~ n /\ b --,~:..:..:x-LI=_x"--l-I_--ll_=...c.x'--ll_?:........:..:.x---,'
1-'
o i n
(b) O~i <n /\ b' ordered I I
96 Part I. Propositions and Predicates
defines an array of arrays. That is, b [0] (and similarly b [I]) is an array
consisting of three elements named b[O][1], b[0][2] and b[0][3]. One can
also have an "array of arrays of arrays", in which case three subscripts
could be used -e.g. d[i]U][k]- and so forth.
Array of arrays take the place of two-dimensional arrays in FOR-
TRAN and PLj I. For example, (5.3.1) could be thought of as equivalent
to the PLjI declaration
We want to define the notation (b; s:e) for any selector s. We do this
recursively on the length of s. The first step is to determine the base case,
(b; c:e).
Let x be a simple variable (which contains a scalar or function). Since
x and x 0 c are equivalent, the assignments x:= e and x 0 c:= e are also
e =(x; c:e)
(b; c:g) = g
(b;[i]os:e)U] = !
i # j - b UJ
i=j-(bU];s:e) 0
Example 2. In this and the following examples, let c[l:3] = (6,7,8) and
b[0:1][1:3] = «0, 1,2),(3,4,5». Then
Again, all but the outer parentheses can be omitted. For example, the
following two expressions are equivalent. They define an array (function)
that is the same as b except in three positions -[i]U], U] and [k][i].
98 Part I. Propositions and Predicates
Modify the notation of this section to allow references to subrecords of arrays and
subarrays of records, etc.