Java Notational Conventions (Full Notes)
Java Notational Conventions (Full Notes)
Notational Conventions
1
Notational Conventions Notational Conventions
i ∈ (x..y) , x<i<y
i ∈ [x..y) , x≤i<y
i ∈ (x..y] , x<i≤y
Slide 2
i ∈ [x..y] , x≤i≤y
This notation has a natural lifting to arrays. For an array a, value v and
natural numbers x and y:
a[x..y) ≤ z , ∀v ∈ a[x..y).[ v ≤ z ]
Some more examples of array properties that we are able to define with the above notation
are:
a(x..y] ≥ z , ∀v ∈ a(x..y].[ v ≥ z ]
a[x..y] 6= z , ∀v ∈ a[x..y].[ v 6= z ]
We will see later that using closed-open intervals for array slices is often convenient for
our reasoning.
2
Notational Conventions Notational Conventions
Notice how the interval’s values are truncated to the bounds of the array a.
Although we rarely use them, we can define the three other types of array intervals (to
match the other number ranges) in terms of our array slice notation.
Specifically:
a(x..y) , a[x+1..y)
a(x..y] , a[x+1..y+1)
a[x..y] , a[x..y+1)
3
Notational Conventions Notational Conventions
To simplify our notation, we drop the interval bounds when they refer to
the start or end of an array. Thus, for an array a and natural number i:
a[..i) describes the array slice from 0 up to, but not including, i
Slide 4
We will often need to use the array contents annotations a[..)old or a[..)pre , as it is very
common for methods to update (or potentially update) the contents of arrays.
4
Notational Conventions Notational Conventions
N rOccurs(a[..), v) , | { k | a[k] = v } |
Slide 5
a[..) ≈ b[..) means that the contents of arrays a and b are identical:
For a set S, the term |S| denotes the cardinality (or size) of the set. So, N rOccurs counts
the number of occurrences of a value v in an array. The definition of ∼ then requires that
both arrays contain the same number of occurrences of each possible value.
The definitions of ∼ and ≈ can also be applied to arbitrary array slices, in which case
they only consider their respective array contents in the given intervals. e.g.
a[1..a.length) ≈ b[1..b.length)
describes two arrays a and b that are identical, except for their first element.
5
Notational Conventions Notational Conventions
Π a[x..y) , Πy−1
k=x a[k]
, a[x] ∗ a[x + 1] ∗ ... ∗ a[y − 1]
P
Important: a[x..y) and Πa[x..y) are only well-defined for ranges [x..y)
that lie within the bounds of array a.
Drossopoulou & Wheelhouse (DoC) Discrete Mathematics, Logic & Reasoning 6 / 11
P
Note that the sum of an empty array slice (e.g. a[..0)) is defined to be 0 (the unit of
addition).
Similarly, the product of an empty array slice (e.g.Π a[a.length..)) is defined to be 1 (the
unit of multiplication).
6
Notational Conventions Notational Conventions
Swapped( a[..), b[..), i, j ) means that the arrays a and b are identical,
except for the elements at indexes i and j, which have been swapped:
Swapped( a[..), b[..), i, j ) , a.length = b.length
∧ i, j ∈ [0..a.length)
∧ b[i] = a[j] ∧ b[j] = a[i]
∧ ∀k∈[0..a.length)\{i,j}.[ a[k] = b[k] ]
A version of Swapped could also be defined for arbitrary array slices, but the above
version is sufficient for all of our needs in this module.
7
Notational Conventions Notational Conventions
Important: min and max are only well-defined for an array whose
contents are comparable (such as Integers).
8
Notational Conventions Notational Conventions
Permutation Lemmas:
a[..) ∼ b[..) −→ b[..) ∼ a[..) (∼Symm)
a[..) ∼ b[..) ∧ b[..) ∼ c[..) −→ a[..) ∼ c[..) (∼Trans)
a[..) ∼ b[..) −→ a.length = b.length (∼Length)
These Lemmas for deep equality and permutation of arrays allow us to quickly deduce
helpful related properties as well as treating both ≈ and ∼ as equality relations.
9
Notational Conventions Notational Conventions
We define the concatenation of arrays (:) via their deep equality relation
Slide 10
with a third “observer” array. That is, for arbitrary arrays a, b and c we
have:
Notice that from the definition of ≈ we know that when a[..) ≈ b[..) : c[..)
then a.length = b.length + c.length
Another, equivalent, but longer, definition of concatenation would be:
a[..) ≈ b[..) : c[..) , a.length = b.length + c.length
∧ ∀i ∈ [0..b.length).[ a[i] = b[i] ]
∧ ∀i ∈ [0..c.length).[ a[i + b.length] = c[i] ]
Using deep equality, array slices and concatenation we can describe many interesting
properties of arrays. For example:
a[..) ≈ a[0..1) : b[..) : a[b.length + 1..a.length)
says that the contents of the array a from index 1 to index b.length is the same as the
array b. It does not say anything about the other elements of the array a. This notation
can be very useful for describing changes to just part of an array, or even for specifying
which parts of an array are unmodified.
10
Notational Conventions Notational Conventions
In RngWeak the premise says a and b are identical except for the range [i..j), and the
conclusion says that a and b are identical except for the range [m..n). The lemma holds,
since the range [m..n) includes the range [i..j). We can prove this Lemma as follows:
Given:
(1) b[..) ≈ a[0..i) : b[i..j) : a[j..)
(2) x ≤ i ∧ j ≤ y
To Show:
(α) b[..) ≈ a[0..x) : b[x..y) : a[y..)
Proof:
From (1) and the definition of concatenation we have:
(3) b.length = i + (j − i) + (a.length − j)
(4) ∀k ∈ [0..i). b[k] = a[k]
(5) ∀k ∈ [j..a.length). b[k] = a[k]
From (3) we obtain by arithmetic:
(6) b.length = a.length
From (4), (2) and since x ≤ i ∧ k ≤ x −→ k ≤ i, we obtain:
(7) ∀k ∈ [0..x). b[k] = a[k]
Similarly, from (5) and (2) we obtain:
(8) ∀k ∈ [y..a.length). b[k] = a[k]
From (6), (7) and (8) and definition of concatenation (:), we obtain (α).
11