#### 6.6Types, Predicates and Accessors

 syntax(Array A)
The parent array type. Its type parameter is the type of the array’s elements.

The polymorphic Array type is covariant, meaning that (Array A) is a subtype of (Array B) if A is a subtype of B:
> (define arr (array #[1 2 3 4 5]))
> arr

- : (Array Positive-Byte)

(array #[1 2 3 4 5])

> (ann arr (Array Real))

- : (Array Real)

(array #[1 2 3 4 5])

> (ann arr (Array Any))

- : (Array Any)

(array #[1 2 3 4 5])

Because subtyping is transitive, the (Array A) in the preceeding subtyping rule can be replaced with any of (Array A)’s subtypes, including descendant types of Array. For example, (Mutable-Array A) is a subtype of (Array B) if A is a subtype of B:
> (define arr (mutable-array #[1 2 3 4 5]))
> arr

- : (Mutable-Array Integer)

(mutable-array #[1 2 3 4 5])

> (ann arr (Array Real))

- : (Array Real)

(mutable-array #[1 2 3 4 5])

> (ann arr (Array Any))

- : (Array Any)

(mutable-array #[1 2 3 4 5])

 syntax
The parent type of arrays whose elements can be mutated. Functions like array-set! and array-slice-set! accept arguments of this type. Examples of subtypes are Mutable-Array, FlArray and FCArray.

This type is invariant, meaning that (Settable-Array A) is not a subtype of (Settable-Array B) if A and B are different types, even if A is a subtype of B:
> (define arr (mutable-array #[1 2 3 4 5]))
> arr

- : (Mutable-Array Integer)

(mutable-array #[1 2 3 4 5])

> (ann arr (Settable-Array Integer))

- : (Settable-Array Integer)

(mutable-array #[1 2 3 4 5])

> (ann arr (Settable-Array Real))

eval:93:0: Type Checker: type mismatch

expected: (Settable-Array Real)

given: (Mutable-Array Integer)

in: Real

 syntax
The type of mutable arrays. Its type parameter is the type of the array’s elements.

Arrays of this type store their elements in a (Vectorof A):
> (define arr (mutable-array #[#[1 2] #[3 4]]))
> (vector-set! (mutable-array-data arr) 0 -10)
> arr

- : (Mutable-Array Integer)

(mutable-array #[#[-10 2] #[3 4]])

Mutable arrays are always strict.

 syntax
The type of array shapes and array indexes produced by math/array functions. Defined as (Vectorof Index).

Example:
 > (array-shape (array #[#[#[0]]])) - : Indexes '#(1 1 1)

 syntax
The type of array shapes and array indexes accepted by math/array functions. Defined as (U Indexes (Vectorof Integer)).

Examples:
> (define ds #(3 2))
> ds

- : (Vector Integer Integer)

'#(3 2)

> (make-array ds (void))

- : (Array Void)

(array #[#[#<void> #<void>] #[#<void> #<void>] #[#<void> #<void>]])

This makes indexes-accepting functions easier to use, because it is easier to convince Typed Racket that a vector contains Integer elements than that a vector contains Index elements.

In-Indexes is not defined as (Vectorof Integer) because mutable container types like Vector and Vectorof are invariant. In particular, (Vectorof Index) is not a subtype of (Vectorof Integer):
> (define js ((inst vector Index) 3 4 5))
> js

- : Indexes

'#(3 4 5)

> (ann js (Vectorof Integer))

eval:103:0: Type Checker: type mismatch

expected: (Vectorof Integer)

given: Indexes

in: Integer

> (ann js In-Indexes)

- : In-Indexes

'#(3 4 5)

 procedure(array? v) → Boolean v : Any
 procedure v : Any
 procedure v : Any
Predicates for the types Array, Settable-Array, and Mutable-Array.

Because Settable-Array and its descendants are invariant, settable-array? and its descendants’ predicates are generally not useful in occurrence typing. For example, if we know we have an Array but would like to treat it differently if it happens to be a Mutable-Array, we are basically out of luck:
> (: maybe-array-data (All (A) ((Array A) -> (U #f (Vectorof A)))))
 > (define (maybe-array-data arr) (cond [(mutable-array? arr)  (mutable-array-data arr)] [else  #f]))

eval:106:0: Type Checker: Polymorphic function

`mutable-array-data' could not be applied to arguments:

Argument 1:

Expected: (Mutable-Array A)

Given:    (Struct Mutable-Array)

in: #f

In general, predicates with a Struct filter do not give conditional branches access to a struct’s accessors. Because Settable-Array and its descendants are invariant, their predicates have Struct filters:
 > array? - : (-> Any Boolean : (Array Any)) # > settable-array? - : (-> Any Boolean : (Struct Settable-Array)) # > mutable-array? - : (-> Any Boolean : (Struct Mutable-Array)) #

 procedure(array-shape arr) → Indexes arr : (Array A)
Returns arr’s shape, a vector of indexes that contains the lengths of arr’s axes.

Examples:
 > (array-shape (array 0)) - : Indexes '#() > (array-shape (array #[0 1])) - : Indexes '#(2) > (array-shape (array #[#[0 1]])) - : Indexes '#(1 2) > (array-shape (array #[])) - : Indexes '#(0)

 procedure(array-size arr) → Index arr : (Array A)
Returns the number of elements in arr, which is the product of its axis lengths.

Examples:
 > (array-size (array 0)) - : Integer [more precisely: Index] 1 > (array-size (array #[0 1])) - : Integer [more precisely: Index] 2 > (array-size (array #[#[0 1]])) - : Integer [more precisely: Index] 2 > (array-size (array #[])) - : Integer [more precisely: Index] 0

 procedure(array-dims arr) → Index arr : (Array A)
Returns the number of arr’s dimensions. Equivalent to (vector-length (array-shape arr)).

 procedure(mutable-array-data arr) → (Vectorof A) arr : (Mutable-Array A)
Returns the vector of data that arr contains.