author | paulson |
Thu, 12 Sep 2019 14:51:50 +0100 | |
changeset 70689 | 67360d50ebb3 |
parent 69597 | ff784d5a5bfb |
child 71847 | da12452c9be2 |
permissions | -rw-r--r-- |
39307 | 1 |
(* Title: HOL/Imperative_HOL/Overview.thy |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
*) |
|
4 |
||
5 |
(*<*) |
|
6 |
theory Overview |
|
66453
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
63167
diff
changeset
|
7 |
imports Imperative_HOL "HOL-Library.LaTeXsugar" |
39307 | 8 |
begin |
9 |
||
10 |
(* type constraints with spacing *) |
|
61143 | 11 |
no_syntax (output) |
12 |
"_constrain" :: "logic => type => logic" ("_::_" [4, 0] 3) |
|
13 |
"_constrain" :: "prop' => type => prop'" ("_::_" [4, 0] 3) |
|
14 |
||
15 |
syntax (output) |
|
16 |
"_constrain" :: "logic => type => logic" ("_ :: _" [4, 0] 3) |
|
17 |
"_constrain" :: "prop' => type => prop'" ("_ :: _" [4, 0] 3) |
|
18 |
(*>*) |
|
39307 | 19 |
|
63167 | 20 |
text \<open> |
21 |
\<open>Imperative HOL\<close> is a leightweight framework for reasoning |
|
22 |
about imperative data structures in \<open>Isabelle/HOL\<close> |
|
58622 | 23 |
@{cite "Nipkow-et-al:2002:tutorial"}. Its basic ideas are described in |
24 |
@{cite "Bulwahn-et-al:2008:imp_HOL"}. However their concrete |
|
39307 | 25 |
realisation has changed since, due to both extensions and |
26 |
refinements. Therefore this overview wants to present the framework |
|
27 |
\qt{as it is} by now. It focusses on the user-view, less on matters |
|
40358 | 28 |
of construction. For details study of the theory sources is |
39307 | 29 |
encouraged. |
63167 | 30 |
\<close> |
39307 | 31 |
|
32 |
||
63167 | 33 |
section \<open>A polymorphic heap inside a monad\<close> |
39307 | 34 |
|
63167 | 35 |
text \<open> |
69597 | 36 |
Heaps (\<^type>\<open>heap\<close>) can be populated by values of class \<^class>\<open>heap\<close>; HOL's default types are already instantiated to class \<^class>\<open>heap\<close>. Class \<^class>\<open>heap\<close> is a subclass of \<^class>\<open>countable\<close>; see |
37 |
theory \<open>Countable\<close> for ways to instantiate types as \<^class>\<open>countable\<close>. |
|
39307 | 38 |
|
69597 | 39 |
The heap is wrapped up in a monad \<^typ>\<open>'a Heap\<close> by means of the |
39307 | 40 |
following specification: |
41 |
||
42 |
\begin{quote} |
|
69597 | 43 |
\<^datatype>\<open>Heap\<close> |
39307 | 44 |
\end{quote} |
45 |
||
46 |
Unwrapping of this monad type happens through |
|
47 |
||
48 |
\begin{quote} |
|
69597 | 49 |
\<^term_type>\<open>execute\<close> \\ |
39307 | 50 |
@{thm execute.simps [no_vars]} |
51 |
\end{quote} |
|
52 |
||
53 |
This allows for equational reasoning about monadic expressions; the |
|
63167 | 54 |
fact collection \<open>execute_simps\<close> contains appropriate rewrites |
39307 | 55 |
for all fundamental operations. |
56 |
||
39610 | 57 |
Primitive fine-granular control over heaps is available through rule |
63167 | 58 |
\<open>Heap_cases\<close>: |
39307 | 59 |
|
60 |
\begin{quote} |
|
61 |
@{thm [break] Heap_cases [no_vars]} |
|
62 |
\end{quote} |
|
63 |
||
64 |
Monadic expression involve the usual combinators: |
|
65 |
||
66 |
\begin{quote} |
|
69597 | 67 |
\<^term_type>\<open>return\<close> \\ |
68 |
\<^term_type>\<open>bind\<close> \\ |
|
69 |
\<^term_type>\<open>raise\<close> |
|
39307 | 70 |
\end{quote} |
71 |
||
69597 | 72 |
This is also associated with nice monad do-syntax. The \<^typ>\<open>string\<close> argument to \<^const>\<open>raise\<close> is just a codified comment. |
39307 | 73 |
|
74 |
Among a couple of generic combinators the following is helpful for |
|
75 |
establishing invariants: |
|
76 |
||
77 |
\begin{quote} |
|
69597 | 78 |
\<^term_type>\<open>assert\<close> \\ |
39307 | 79 |
@{thm assert_def [no_vars]} |
80 |
\end{quote} |
|
63167 | 81 |
\<close> |
39307 | 82 |
|
83 |
||
69597 | 84 |
section \<open>Relational reasoning about \<^type>\<open>Heap\<close> expressions\<close> |
39307 | 85 |
|
63167 | 86 |
text \<open> |
39307 | 87 |
To establish correctness of imperative programs, predicate |
88 |
||
89 |
\begin{quote} |
|
69597 | 90 |
\<^term_type>\<open>effect\<close> |
39307 | 91 |
\end{quote} |
92 |
||
63167 | 93 |
provides a simple relational calculus. Primitive rules are \<open>effectI\<close> and \<open>effectE\<close>, rules appropriate for reasoning about |
94 |
imperative operations are available in the \<open>effect_intros\<close> and |
|
95 |
\<open>effect_elims\<close> fact collections. |
|
39307 | 96 |
|
97 |
Often non-failure of imperative computations does not depend |
|
98 |
on the heap at all; reasoning then can be easier using predicate |
|
99 |
||
100 |
\begin{quote} |
|
69597 | 101 |
\<^term_type>\<open>success\<close> |
39307 | 102 |
\end{quote} |
103 |
||
69597 | 104 |
Introduction rules for \<^const>\<open>success\<close> are available in the |
63167 | 105 |
\<open>success_intro\<close> fact collection. |
39307 | 106 |
|
69597 | 107 |
\<^const>\<open>execute\<close>, \<^const>\<open>effect\<close>, \<^const>\<open>success\<close> and \<^const>\<open>bind\<close> |
63167 | 108 |
are related by rules \<open>execute_bind_success\<close>, \<open>success_bind_executeI\<close>, \<open>success_bind_effectI\<close>, \<open>effect_bindI\<close>, \<open>effect_bindE\<close> and \<open>execute_bind_eq_SomeI\<close>. |
109 |
\<close> |
|
39307 | 110 |
|
111 |
||
63167 | 112 |
section \<open>Monadic data structures\<close> |
39307 | 113 |
|
63167 | 114 |
text \<open> |
39307 | 115 |
The operations for monadic data structures (arrays and references) |
116 |
come in two flavours: |
|
117 |
||
118 |
\begin{itemize} |
|
119 |
||
120 |
\item Operations on the bare heap; their number is kept minimal |
|
121 |
to facilitate proving. |
|
122 |
||
123 |
\item Operations on the heap wrapped up in a monad; these are designed |
|
124 |
for executing. |
|
125 |
||
126 |
\end{itemize} |
|
127 |
||
128 |
Provided proof rules are such that they reduce monad operations to |
|
129 |
operations on bare heaps. |
|
39717 | 130 |
|
131 |
Note that HOL equality coincides with reference equality and may be |
|
132 |
used as primitive executable operation. |
|
63167 | 133 |
\<close> |
39307 | 134 |
|
63167 | 135 |
subsection \<open>Arrays\<close> |
39307 | 136 |
|
63167 | 137 |
text \<open> |
39307 | 138 |
Heap operations: |
139 |
||
140 |
\begin{quote} |
|
69597 | 141 |
\<^term_type>\<open>Array.alloc\<close> \\ |
142 |
\<^term_type>\<open>Array.present\<close> \\ |
|
143 |
\<^term_type>\<open>Array.get\<close> \\ |
|
144 |
\<^term_type>\<open>Array.set\<close> \\ |
|
145 |
\<^term_type>\<open>Array.length\<close> \\ |
|
146 |
\<^term_type>\<open>Array.update\<close> \\ |
|
147 |
\<^term_type>\<open>Array.noteq\<close> |
|
39307 | 148 |
\end{quote} |
149 |
||
150 |
Monad operations: |
|
151 |
||
152 |
\begin{quote} |
|
69597 | 153 |
\<^term_type>\<open>Array.new\<close> \\ |
154 |
\<^term_type>\<open>Array.of_list\<close> \\ |
|
155 |
\<^term_type>\<open>Array.make\<close> \\ |
|
156 |
\<^term_type>\<open>Array.len\<close> \\ |
|
157 |
\<^term_type>\<open>Array.nth\<close> \\ |
|
158 |
\<^term_type>\<open>Array.upd\<close> \\ |
|
159 |
\<^term_type>\<open>Array.map_entry\<close> \\ |
|
160 |
\<^term_type>\<open>Array.swap\<close> \\ |
|
161 |
\<^term_type>\<open>Array.freeze\<close> |
|
39307 | 162 |
\end{quote} |
63167 | 163 |
\<close> |
39307 | 164 |
|
63167 | 165 |
subsection \<open>References\<close> |
39307 | 166 |
|
63167 | 167 |
text \<open> |
39307 | 168 |
Heap operations: |
169 |
||
170 |
\begin{quote} |
|
69597 | 171 |
\<^term_type>\<open>Ref.alloc\<close> \\ |
172 |
\<^term_type>\<open>Ref.present\<close> \\ |
|
173 |
\<^term_type>\<open>Ref.get\<close> \\ |
|
174 |
\<^term_type>\<open>Ref.set\<close> \\ |
|
175 |
\<^term_type>\<open>Ref.noteq\<close> |
|
39307 | 176 |
\end{quote} |
177 |
||
178 |
Monad operations: |
|
179 |
||
180 |
\begin{quote} |
|
69597 | 181 |
\<^term_type>\<open>Ref.ref\<close> \\ |
182 |
\<^term_type>\<open>Ref.lookup\<close> \\ |
|
183 |
\<^term_type>\<open>Ref.update\<close> \\ |
|
184 |
\<^term_type>\<open>Ref.change\<close> |
|
39307 | 185 |
\end{quote} |
63167 | 186 |
\<close> |
39307 | 187 |
|
188 |
||
63167 | 189 |
section \<open>Code generation\<close> |
39307 | 190 |
|
63167 | 191 |
text \<open> |
39307 | 192 |
Imperative HOL sets up the code generator in a way that imperative |
193 |
operations are mapped to suitable counterparts in the target |
|
63167 | 194 |
language. For \<open>Haskell\<close>, a suitable \<open>ST\<close> monad is used; |
195 |
for \<open>SML\<close>, \<open>Ocaml\<close> and \<open>Scala\<close> unit values ensure |
|
39307 | 196 |
that the evaluation order is the same as you would expect from the |
197 |
original monadic expressions. These units may look cumbersome; the |
|
63167 | 198 |
target language variants \<open>SML_imp\<close>, \<open>Ocaml_imp\<close> and |
199 |
\<open>Scala_imp\<close> make some effort to optimize some of them away. |
|
200 |
\<close> |
|
39307 | 201 |
|
202 |
||
63167 | 203 |
section \<open>Some hints for using the framework\<close> |
39307 | 204 |
|
63167 | 205 |
text \<open> |
39307 | 206 |
Of course a framework itself does not by itself indicate how to make |
207 |
best use of it. Here some hints drawn from prior experiences with |
|
208 |
Imperative HOL: |
|
209 |
||
210 |
\begin{itemize} |
|
211 |
||
212 |
\item Proofs on bare heaps should be strictly separated from those |
|
213 |
for monadic expressions. The first capture the essence, while the |
|
214 |
latter just describe a certain wrapping-up. |
|
215 |
||
216 |
\item A good methodology is to gradually improve an imperative |
|
217 |
program from a functional one. In the extreme case this means |
|
218 |
that an original functional program is decomposed into suitable |
|
219 |
operations with exactly one corresponding imperative operation. |
|
220 |
Having shown suitable correspondence lemmas between those, the |
|
221 |
correctness prove of the whole imperative program simply |
|
222 |
consists of composing those. |
|
223 |
||
224 |
\item Whether one should prefer equational reasoning (fact |
|
63167 | 225 |
collection \<open>execute_simps\<close> or relational reasoning (fact |
226 |
collections \<open>effect_intros\<close> and \<open>effect_elims\<close>) depends |
|
39610 | 227 |
on the problems to solve. For complex expressions or |
228 |
expressions involving binders, the relation style usually is |
|
229 |
superior but requires more proof text. |
|
39307 | 230 |
|
231 |
\item Note that you can extend the fact collections of Imperative |
|
39610 | 232 |
HOL yourself whenever appropriate. |
39307 | 233 |
|
234 |
\end{itemize} |
|
63167 | 235 |
\<close> |
39307 | 236 |
|
62390 | 237 |
end |