The array dimension is now stored inside the array. The main motivation is that it reflects the actual runtime implementation. We need to store the array size to be able to GC it. So, it feels silly to have the array size stored in each array object, but we cannot use this information. For example, in the `hashmap` we implemented the bucket array using `array`, and we store the `size` of the array. Same for the Lean3 `buffer` object. The `buffer` object doesn't even need to exist. The actual `array` implementation is the `buffer`
27 lines
558 B
Text
27 lines
558 B
Text
#check @array.mk
|
|
|
|
#eval mk_array 4 1
|
|
|
|
def v : array nat := @array.mk nat 10 (λ ⟨i, _⟩, i)
|
|
|
|
#eval array.map (+10) v
|
|
|
|
def w : array nat :=
|
|
(mk_array 9 1).push 3
|
|
|
|
def f : fin w.sz → nat :=
|
|
array.cases_on w (λ _ f, f)
|
|
|
|
#eval f ⟨1, sorry⟩
|
|
#eval f ⟨9, sorry⟩
|
|
|
|
#eval (((mk_array 1 1).push 2).push 3).foldl 0 (+)
|
|
|
|
def array_sum (a : array nat) : nat :=
|
|
a.foldl 0 (+)
|
|
|
|
#eval array_sum (mk_array 10 1)
|
|
|
|
#eval (mk_array 10 1).data ⟨1, dec_trivial⟩ + 20
|
|
#eval (mk_array 10 1).data ⟨2, dec_trivial⟩
|
|
#eval (mk_array 10 3).data ⟨2, dec_trivial⟩
|