Vectors
In Kλ and Shen, (1 dimensional) vectors fulfil all the tasks of arrays. In Kλ there are only 4 primitive
functions concerned with vectors.
1. absvector - which creates an absolute (platform) vector with N addresses.
2. address-> - which destructively places an element E into a vector address N in vector V and
returns the resultant vector.
3. <-address - which extracts an element from a vector address N in vector V.
4. absvector? - which recognises a vector.
All of these functions are accessible from Shen but only the last has a type, since Kλ vectors
may have elements of any type. It is an error to try to access an address beyond the limit of the
vector or to supply any number which is not a whole number between 0 and the limit of the vector.
Note that 'absvector?' plugs into the native vector recognition routine. It is possible for this
function to return true to objects which are not vectors under other platfroms. In Lisp for instance,
strings are vectors of characters and the default representation of tuples in Shen is by use of
vectors. The function 'vector?' (see below) is better behaved in this respect.
Vectors are numbered from 0; so (absvector 100) creates a vector with addresses from 0 to 99.
If a vector V is created and nothing has been stored address V(N) then the result returned by
(<-address V N) is platform dependent.
In Shen absolute vectors are partioned into standard and non-standard vectors. In Shen, by convention,
when a standard vector V is created two operations are performed.
1. The 0th element of V is allocated a positive integer which indicates the size (limit) of the vector.
2. Every other address is allocated the failure object designated by the expression (fail).
The function 'vector' of type number --> (vector A) creates such a vector. If the 0th element of V
is not a non-negative integer then the vector is non standard. Hence access to the user contents
of the standard vector begins with the index N = 1.
The shortest standard vector is created by expression (vector 0) which creates a standard vector
which contains one element whose contents are zero. This called the empty vector and is significant
in pattern-matching over vectors (see next section). It is impossible to write to the empty vector
in a type secure way and hence under type security, the empty vector is immutable. Shen permits the
user to write <> as shorthand for the empty vector. The type of the empty vector is (vector A).
In Kλ the primitive function '<-address' in (<-address V N) accesses the Nth element of the vector V
including the 0th element which indicates the limit of the vector. This function has no type because
the 0th element of a standard vector V will be an integer irrespective of the rest of the vector.
The type secure version of '<-address' is the function '<-vector' of type (vector A) --> number --> A,
which accesses the contents of the standard vector. The operation (<-vector V 0) results in an error.
If <-vector accesses the failure object then an exception is returned as an error message. Otherwise
'<-vector' behaves exactly as does '<-address'. The function 'limit' has the type (vector A) --> number
and accesses the 0th element of a standard vector V. Both are simply defined in terms of '<-address'.
A 2-dimensional array is simply a vector of vectors and therefore has a type which is an instance of
(vector (vector A)). Note that a vector of vectors may incorporate vectors of different sizes
(the result is called a 'jagged array').
For changing the contents of a vector, the function 'address->' in (address-> X N V) places X in the Nth
element of V. The function vector-> is type secure version of address-> of type
((vector A) --> number --> A --> (vector A) and raises an error for N = 0.
The function 'vector?' returns true iff the argument is a standard vector. |