section‹Program and datatype refinement \label{sec:refinement}›
text‹ Code generation by shallow embedding (cf.~\secref{sec:principle}) allows to choose code equations and datatype constructors freely, given that some very basic syntactic properties are met; this flexibility opens up mechanisms for refinement which allow to extend the scope and quality of generated code dramatically. ›
subsection‹Program refinement›
text‹ Program refinement works by choosing appropriate code equations explicitly (cf.~\secref{sec:equations}); as example, we use Fibonacci numbers: ›
text‹ \noindent The runtime of the corresponding code grows exponential due to two recursive calls: ›
text %quote ‹ @{code_stmts fib constant: fib (Haskell)} ›
text‹ \noindent A more efficient implementation would use dynamic programming, e.g.~sharing of common intermediate results between recursive calls. This idea is expressed by an auxiliary operation which computes a Fibonacci number and its successor simultaneously: ›
text‹ \noindent The resulting code shows only linear growth of runtime: ›
text %quote ‹ @{code_stmts fib constant: fib fib_step (Haskell)} ›
subsection‹Datatype refinement›
text‹ Selecting specific code equations \emph{and} datatype constructors leads to datatype refinement. As an example, we will develop an alternative representation of the queue example given in \secref{sec:queue_example}. The amortised representation is convenient for generating code but exposes its \qt{implementation} details, which may be cumbersome when proving theorems about it. Therefore, here is a simple, straightforward representation of queues: ›
primrec %quote enqueue :: "'a ==> 'a queue ==> 'a queue"where "enqueue x (Queue xs) = Queue (xs @ [x])"
fun %quote dequeue :: "'a queue ==> 'a option × 'a queue"where "dequeue (Queue []) = (None, Queue [])"
| "dequeue (Queue (x # xs)) = (Some x, Queue xs)"
text‹ \noindent This we can use directly for proving; for executing, we provide an alternative characterisation: ›
definition %quote AQueue :: "'a list ==> 'a list ==> 'a queue"where "AQueue xs ys = Queue (ys @ rev xs)"
code_datatype %quote AQueue
text‹ \noindent Here we define a \qt{constructor} 🍋‹AQueue›which is defined in terms of ‹Queue›and interprets its arguments according to what the \emph{content} of an amortised queue is supposed to be. The prerequisite for datatype constructors is only syntactical: a constructor must be of type ‹τ = …==> κ α🪙1 … α🪙n›where ‹{α🪙1, …, α🪙n}› is exactly the set of \emph{all} type variables in ‹τ›; then ‹κ› is its corresponding datatype. The HOL datatype package by default registers any new datatype with its constructors, but this may be changed using @{command_def code_datatype}; the currently chosen constructors can be inspected using the @{command print_codesetup} command. Equipped with this, we are able to prove the following equations for our primitive queue operations which \qt{implement} the simple queues in an amortised fashion: ›
text‹ \noindent It is good style, although no absolute requirement, to provide code equations for the original artefacts of the implemented type, if possible; in our case, these are the datatype constructor 🍋‹Queue›and the case combinator 🍋‹case_queue›: ›
text‹ The same techniques can also be applied to types which are not specified as datatypes, e.g.~type 🍋‹int›is originally specified as quotient type by means of @{command_def typedef}, but for code generation constants allowing construction of binary numeral values are used as constructors for 🍋‹int›. This approach however fails if the representation of a type demands invariants; this issue is discussed in the next section. ›
text‹ Datatype representation involving invariants require a dedicated setup for the type and its primitive operations. As a running example, we implement a type 🍋‹'a dlist›of lists consisting of distinct elements. The specification of 🍋‹'a dlist›itself can be found in theory 🍋‹HOL-Library.Dlist›. The first step is to decide on which representation the abstract type (in our example 🍋‹'a dlist›) should be implemented. Here we choose 🍋‹'a list›. Then a conversion from the concrete type to the abstract type must be specified, here: ›
text %quote ‹ 🪙‹Dlist› ›
text‹ \noindent Next follows the specification of a suitable \emph{projection}, i.e.~a conversion from abstract to concrete type: ›
text %quote ‹ 🪙‹list_of_dlist› ›
text‹ \noindent This projection must be specified such that the following \emph{abstract datatype certificate} can be proven: ›
text‹ \noindent Note that so far the invariant on representations (🪙‹distinct›) has never been mentioned explicitly: the invariant is only referred to implicitly: all values in set 🍋‹{xs. list_of_dlist (Dlist xs) = xs}›are invariant, and in our example this is exactly 🍋‹{xs. distinct xs}›. The primitive operations on 🍋‹'a dlist›are specified indirectly using the projection 🍋‹list_of_dlist›. For the empty ‹dlist›,🍋‹Dlist.empty›, we finally want the code equation ›
text %quote ‹ 🍋‹Dlist.empty = Dlist []› ›
text‹ \noindent This we have to prove indirectly as follows: ›
lemma %quote [code]: "list_of_dlist Dlist.empty = []" by (fact list_of_dlist_empty)
text‹ \noindent This equation logically encodes both the desired code equation and that the expression 🍋‹Dlist›is applied to obeys the implicit invariant. Equations for insertion and removal are similar: ›
lemma %quote [code]: "list_of_dlist (Dlist.insert x dxs) = List.insert x (list_of_dlist dxs)" by (fact list_of_dlist_insert)
lemma %quote [code]: "list_of_dlist (Dlist.remove x dxs) = remove1 x (list_of_dlist dxs)" by (fact list_of_dlist_remove)
text‹ \noindent Then the corresponding code is as follows: ›
text %quote ‹ @{code_stmts Dlist.empty Dlist.insert Dlist.remove list_of_dlist (SML)} ›
text‹ To reduce manual work for datatype refinement, @{command_def lift_definition} is a valuable tool. See the corresponding section in 🍋‹"isabelle-isar-ref"›. See further 🍋‹"Haftmann-Kraus-Kuncar-Nipkow:2013:data_refinement"› for the meta theory of datatype refinement involving invariants. Typical data structures implemented by representations involving invariants are available in the library, theory 🍋‹HOL-Library.Mapping› specifies key-value-mappings (type 🍋‹('a, 'b) mapping›); these can be implemented by red-black-trees (theory 🍋‹HOL-Library.RBT›). ›
end
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-05-01)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.