Skip to content

Commit

Permalink
More details in the documentation of native arrays
Browse files Browse the repository at this point in the history
Update doc/sphinx/language/core/primitive.rst

Co-authored-by: Jim Fehrle <jim.fehrle@gmail.com>

Add persistent data structure
  • Loading branch information
VincentSe committed Oct 2, 2020
1 parent 7ffb5e6 commit ab41b88
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions doc/sphinx/language/core/primitive.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ follows:
Axiom get_set_same : forall A t i (a:A), (i < length t) = true -> t.[i<-a].[i] = a.
Axiom get_set_other : forall A t i j (a:A), i <> j -> t.[i<-a].[j] = t.[j].

The complete set of such operators can be obtained looking at the :g:`PArray` module.
The rest of those operators can be obtained by looking at the :g:`PArray` module.

These primitive declarations are regular axioms. As such, they must be trusted and are listed by the
:g:`Print Assumptions` command.
Expand All @@ -150,7 +150,17 @@ extraction. Instead, it has to be provided by the user (if they want to compile
or execute the extracted code). For instance, an implementation of this module
can be taken from the kernel of Coq (see ``kernel/parray.ml``).

Primitive arrays expose a functional interface, but they are internally
implemented using a persistent data structure :cite:`ConchonFilliatre07wml`.
Update and access to an element in the most recent copy of an array are
constant time operations.
Coq's primitive arrays are persistent data structures. Semantically, a set operation
``t.[i <- a]`` represents a new array that has the same values as ``t``, except
at position ``i`` where its value is ``a``. The array ``t`` still exists, can
still be used and its values were not modified. Operationally, the implementation
of Coq's primitive arrays is optimized so that the new array ``t.[i <- a]`` does not
copy the whole ``t``. The details are in section 2.3 of :cite:`ConchonFilliatre07wml`.
In short, the implementation keeps one version of ``t`` as an OCaml native array and
other versions as lists of modifications to ``t``. Accesses to the native array
version are constant time operations. Accesses to versions where all the cells of
the array are modified have O(n) access time, the same as a list, which defeats
the purpose of Coq's primitive arrays. The version that is kept as the native array
changes dynamically upon each get and set call: the current list of modifications
is applied to the native array and the lists of modifications of the other versions
are updated so that they still represent the same values.

0 comments on commit ab41b88

Please sign in to comment.