1.2. Desarrollo de las teorías y conceptos
1.2.3. Elementos del plan estratégico
1.2.3.2. Misión
algebra Recordw(A)
import A
carriers Recordw(A) = As(1)× · · · ×As(n)
constants
operations Create : As(1)× · · · ×As(n)→Recordw(A)
Get field1 : Recordw(A)→As(1)
... ...
Get fieldn : Recordw(A)→As(n)
Change field1 : As(1)×Recordw(A)→Recordw(A)
... ...
Change fieldn : As(n)×Recordw(A)→Recordw(A)
definitions
Create(a1, . . . ,an) = (a1, . . . ,an)
Get field1((a1, . . . ,an)) = a1
... ... Get fieldn((a1, . . . ,an)) = an
Change field1(b1,(a1, . . . ,an)) = (b1,a2, . . . ,an)
... ...
Change fieldn(bn,(a1, . . . ,an)) = (a1, . . . ,an −1,bn)
It is possible to define more operations on records using operations on A and these simple operations on Recordw(A).
6.2
Dynamic Arrays
Arrays store data of the same type. The data is stored in locations or cells that have addresses. Arrays have operations that allow any location to be read or updated. In particular, arrays can be
(i) static in the sense that they are of fixed length n, like the record structure considered in Section 6.1; or
(ii) dynamic in the sense that an array can grow in size as required — say, if an element is inserted into a position that is beyond the current length of the array.
The addresses are usually based on a simple indexing of locations in space. Space is usually 1, 2 or 3 dimensions.
We construct an algebraic model of dynamic arrays. We will do this in a very general way by augmenting an algebra A with arrays of arbitrary length to store the elements of A. Thus, foreach setAs of data inA, we will have an array for that data of sorts, as shown in Figure6.2.
· · · . . . . . . a1 a2 a3 · · · al−1 al ? ? . . . . . . length l ? empty locations s-array ... ...
Figure 6.2: A dynamic 1-dimensional array to store data from As for each sort s ∈S.
6.2.1
Signature/Interface of Dynamic Arrays
We first produce a signature for arrays that simply lists the sorts, constants and operations of our model.
Old Signature/Interface Suppose we have a signature Σ with sorts . . . ,s, . . . to provide us with information about the elements that we shall store.
In each array of sort s, some locations have data and others are empty or uninitialised. Thus, we shall also need to model error cases that can arise from trying to access empty or uninitialised addresses. Thus, we construct the new signature
Σu that will add to the signature Σ new sort names
. . . ,su, . . .
and constants
us for each sort s in Σ
for distinguished elements which we can use to flag error conditions (as described in Sec- tion 4.5.2).
There are several choices for the addresses of arrays. For simplicity, we will model a one- dimensional array and use natural numbers as addresses. Thus, we shall also use the signature ΣNaturals for addresses.
New Signature/Interface Let us picture, informally, the idea of an array. We want to store and retrieve elements in given addresses within an array. So, we shall need sorts for data, addresses and arrays of data. We have sorts for data from the imported signature Σ and we shall also import the signature ΣNaturals to provide us with a scheme for addresses.
For each sorts of the signatureΣ, we are going to have an array that will store the elements of sort s. For each data sorts, let
arrays
6.2. DYNAMIC ARRAYS 165
To store elements in an array of sort s, we define the operation inserts :s ×nat×arrays →arrays
that will allow us to insert an element of sort s at a given address into an array of sort s; and we define a constant
nulls :→arrays
that represents an array with no stored values. These constants and operations will allow us to create arrays.
For the array to be dynamic though, we shall also need an operation lengths :arrays →nat
to determine the current length of an array of sort s.
Finally, to be able to access the information stored in an array of sort s, we need the operation
reads :nat×arrays →su
that returns the data stored in a given address from an array; if the given array position has not been initialised (because no data has yet been inserted) then we can flag this with an unspecified element.
Using theimport notation to combine the sets of sort names, constant symbols and oper- ation symbols, we get the signature:
signature Array(Σ) import Σu,ΣNaturals
sorts . . . ,arrays, . . .
constants . . . ,nulls :→arrays, . . .
operations . . . ,inserts : s×nat×arrays →arrays, . . .
. . . ,lengths : arrays →nat, . . .
. . . ,reads : nat×arrays →su, . . .
These operations may be expected to satisfy the following axioms: axioms
reads(i,nulls) = us
reads(i,inserts(x,j,a)) =
(
x if i =j; reads(i,a) otherwise.
lengths(nulls) = 0
lengths(insert(x,j,a)) = max(j,lengths(a))
endaxioms
6.2.2
Algebra/Implementation of Dynamic Arrays
Now we will make a precise model of these operations by constructing a ΣArray-algebra A Array
from a Σ-algebra A. There are different ways of modelling arrays. We will give a slightly elaborate model that emphasises the rˆole of empty or uninitialised cells in an array.
Old Algebra/Implementation We implement the signatureΣ with an algebraA to define the data that we want to store in the array. Then, we construct the algebraAu from Ato add
extra elements for flagging errors as described in Section 4.5.2.
New Algebra/Implementation LetAs be a non-empty set of data and let us 6∈As be an
object we may use to mark unspecified data. We will also use the algebraNof natural numbers to implement the addressing mechanism. We can picture an example of an array of length l as shown in Figure 6.3. Data Addresses . . . . . . . . . . u 1 2 b b l−2 u a u u · · · · l l−1 l+ 1l+ 2
Figure 6.3: Model of a finite array of lengthl. After positionl, every cell is uninitialised. Some cells before position l may also be uninitialised.
We model a finite array by means of a function and a number, i.e., a pair a∗ = (a,l),
where: the function
a :N→Aus
gives the data stored in each address i.e.,
a(i) = datum fromAs stored at address i
and
a(i) =us means address i has not been initialised;
and
l ∈N
gives the length of the array. Furthermore, since the array is finite and of length l, we will assume that
a(i) =us for all i >l.
We define the set
Arrays(A) = {(a,l)∈[N→A u
s]×N| a(i) = us for i >l}.
of allfinite arrays overAs. Thus, a finite array is modelled as an infinite sequencea of addresses,
together with a bound l on the location of addresses that have been assigned elements, and an assignment of data to some of the addresses up to l.
To interpret the operation inserts we take the function
6.2. DYNAMIC ARRAYS 167 defined by Inserts(x,i,(a,l)) = ( (b,l) if i ≤l; (b,i) if i >l;
where the element x is inserted into the ith position of the array (a,l)∈Array
s(A) by
b(j) = (
a(j) ifj 6=i; x if j =i.
Note that our operationInserts automatically extends the length of the array if we try to insert
an element in a position that would otherwise be past the end of the array, i.e., if i > l. In this case, the operation only adds one element to the array, as all the intermediate values in the positions between the end of the old array (at l) and the end of the new array (at i) retain their value of u.
In addition to the constants of the imported algebras Au for data and N for addresses, we
add that of the newly created array Null∗s ∈Arrays(A) defined as
Null∗s = (Nulls,0)
for any i ∈Nby
Nulls(i) =us.
To interpret the operation lengths we take the function
Lengths :Arrays(A)→N defined by
Lengths((a,l)) =l.
To interpret the operation reads we take the function
Reads :N×Arrays(A)→A u s
defined by
Reads(i,(a,l)) =a(i)
which reads the ith element of the array (a,l)∈Array
s(A).
algebra Array(A) import Au,N
carriers . . . ,Arrays(A), . . .
constants . . . ,Null∗
s : →Arrays(A), . . .
operations . . . ,Lengths : Arrays(A)→N, . . .
. . . ,Reads : N×Arrays(A)→Aus, . . .
. . . ,Inserts : As ×N×Arrays(A)→Arrays(A), . . .
definitions
. . . ,Null∗
s = (Nulls,0), . . .
. . . ,Nulls(i) = us, . . .
. . . ,Reads(i,(a, l)) = a(i), . . .
. . . ,Inserts(x, i,(a, l)) = (b,Max(i, l)), . . .
where b(j) =
a(j) if j ≤Max(i,l) and j 6=i; x if j ≤Max(i,l) andj =i; u otherwise.
. . . ,Lengths((a,l)) = l, . . .