Theory Containers_Userguide
theory Containers_Userguide imports
Card_Datatype
List_Proper_Interval
Containers
begin
chapter ‹User guide›
text_raw ‹\label{chapter:Userguide}›
text ‹
This user guide shows how to use and extend the lightweight containers framework (LC).
For a more theoretical discussion, see \<^cite>‹"Lochbihler2013ITP"›.
This user guide assumes that you are familiar with refinement in the code generator \<^cite>‹"HaftmannBulwahn2013codetut" and "HaftmannKrausKuncarNipkow2013ITP"›.
The theory ‹Containers_Userguide› generates it; so if you want to experiment with the examples, you can find their source code there.
Further examples can be found in the @{dir ‹Examples›} folder.
›
section ‹Characteristics›
text_raw ‹
\isastyletext
\begin{itemize}
›
text_raw ‹
\isastyletext
\item \textbf{Separate type classes for code generation}
\\
LC follows the ideal that type classes for code generation should be separate from the standard type classes in Isabelle.
LC's type classes are designed such that every type can become an instance, so well-sortedness errors during code generation can always be remedied.
›
text_raw ‹
\isastyletext
\item \textbf{Multiple implementations}
\\
LC supports multiple simultaneous implementations of the same container type.
For example, the following implements at the same time
(i)~the set of @{typ bool} as a distinct list of the elements,
(ii)~@{typ "int set"} as a RBT of the elements or as the RBT of the complement, and
(iii)~sets of functions as monad-style lists:
\par
›
value "({True}, {1 :: int}, - {2 :: int, 3}, {λx :: int. x * x, λy. y + 1})"
text_raw ‹
\isastyletext
\par
The LC type classes are the key to simultaneously supporting different implementations.
\item \textbf{Extensibility}
\\
The LC framework is designed for being extensible.
You can add new containers, implementations and element types any time.
\end{itemize}
›
section ‹Getting started›
text_raw ‹\label{section:getting:started}›
text ‹
Add the entry theory @{theory Containers.Containers} for LC to the end of your imports.
This will reconfigure the code generator such that it implements the types @{typ "'a set"} for sets and @{typ "('a, 'b) mapping"} for maps with one of the data structures supported.
As with all the theories that adapt the code generator setup, it is important that @{theory Containers.Containers} comes at the end of the imports.
\textbf{Note:} LC should not be used together with the theory @{text "HOL-Library.Code_Cardinality"}.
Run the following command, e.g., to check that LC works correctly and implements sets of @{typ int}s as red-black trees (RBT):
›
value [code] "{1 :: int}"
text ‹
This should produce @{value [names_short] "{1 :: int}"}.
Without LC, sets are represented as (complements of) a list of elements, i.e., @{term "set [1 :: int]"} in the example.
›
text ‹
If your exported code does not use your own types as elements of sets or maps and you have not declared any code equation for these containers, then your \isacommand{export{\isacharunderscore}code} command will use LC to implement @{typ "'a set"} and @{typ "('a, 'b) mapping"}.
Our running example will be arithmetic expressions.
The function @{term "vars e"} computes the variables that occur in the expression @{term e}
›
type_synonym vname = string