wenzelm@26840
|
1 |
theory HOL_Specific
|
wenzelm@43522
|
2 |
imports Base Main
|
wenzelm@26840
|
3 |
begin
|
wenzelm@26840
|
4 |
|
wenzelm@26852
|
5 |
chapter {* Isabelle/HOL \label{ch:hol} *}
|
wenzelm@26849
|
6 |
|
wenzelm@44112
|
7 |
section {* Inductive and coinductive definitions \label{sec:hol-inductive} *}
|
wenzelm@44112
|
8 |
|
wenzelm@44112
|
9 |
text {*
|
wenzelm@44112
|
10 |
An \textbf{inductive definition} specifies the least predicate (or
|
wenzelm@44112
|
11 |
set) @{text R} closed under given rules: applying a rule to elements
|
wenzelm@44112
|
12 |
of @{text R} yields a result within @{text R}. For example, a
|
wenzelm@44112
|
13 |
structural operational semantics is an inductive definition of an
|
wenzelm@44112
|
14 |
evaluation relation.
|
wenzelm@44112
|
15 |
|
wenzelm@44112
|
16 |
Dually, a \textbf{coinductive definition} specifies the greatest
|
wenzelm@44112
|
17 |
predicate~/ set @{text R} that is consistent with given rules: every
|
wenzelm@44112
|
18 |
element of @{text R} can be seen as arising by applying a rule to
|
wenzelm@44112
|
19 |
elements of @{text R}. An important example is using bisimulation
|
wenzelm@44112
|
20 |
relations to formalise equivalence of processes and infinite data
|
wenzelm@44112
|
21 |
structures.
|
wenzelm@44112
|
22 |
|
wenzelm@44112
|
23 |
\medskip The HOL package is related to the ZF one, which is
|
wenzelm@44112
|
24 |
described in a separate paper,\footnote{It appeared in CADE
|
wenzelm@44112
|
25 |
\cite{paulson-CADE}; a longer version is distributed with Isabelle.}
|
wenzelm@44112
|
26 |
which you should refer to in case of difficulties. The package is
|
wenzelm@44112
|
27 |
simpler than that of ZF thanks to implicit type-checking in HOL.
|
wenzelm@44112
|
28 |
The types of the (co)inductive predicates (or sets) determine the
|
wenzelm@44112
|
29 |
domain of the fixedpoint definition, and the package does not have
|
wenzelm@44112
|
30 |
to use inference rules for type-checking.
|
wenzelm@44112
|
31 |
|
wenzelm@44112
|
32 |
\begin{matharray}{rcl}
|
wenzelm@44112
|
33 |
@{command_def (HOL) "inductive"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
|
wenzelm@44112
|
34 |
@{command_def (HOL) "inductive_set"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
|
wenzelm@44112
|
35 |
@{command_def (HOL) "coinductive"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
|
wenzelm@44112
|
36 |
@{command_def (HOL) "coinductive_set"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
|
wenzelm@44112
|
37 |
@{attribute_def (HOL) mono} & : & @{text attribute} \\
|
wenzelm@44112
|
38 |
\end{matharray}
|
wenzelm@44112
|
39 |
|
wenzelm@44112
|
40 |
@{rail "
|
wenzelm@44112
|
41 |
(@@{command (HOL) inductive} | @@{command (HOL) inductive_set} |
|
wenzelm@44112
|
42 |
@@{command (HOL) coinductive} | @@{command (HOL) coinductive_set})
|
wenzelm@44112
|
43 |
@{syntax target}? @{syntax \"fixes\"} (@'for' @{syntax \"fixes\"})? \\
|
wenzelm@44112
|
44 |
(@'where' clauses)? (@'monos' @{syntax thmrefs})?
|
wenzelm@44112
|
45 |
;
|
wenzelm@44112
|
46 |
clauses: (@{syntax thmdecl}? @{syntax prop} + '|')
|
wenzelm@44112
|
47 |
;
|
wenzelm@44112
|
48 |
@@{attribute (HOL) mono} (() | 'add' | 'del')
|
wenzelm@44112
|
49 |
"}
|
wenzelm@44112
|
50 |
|
wenzelm@44112
|
51 |
\begin{description}
|
wenzelm@44112
|
52 |
|
wenzelm@44112
|
53 |
\item @{command (HOL) "inductive"} and @{command (HOL)
|
wenzelm@44112
|
54 |
"coinductive"} define (co)inductive predicates from the
|
wenzelm@44112
|
55 |
introduction rules given in the @{keyword "where"} part. The
|
wenzelm@44112
|
56 |
optional @{keyword "for"} part contains a list of parameters of the
|
wenzelm@44112
|
57 |
(co)inductive predicates that remain fixed throughout the
|
wenzelm@44112
|
58 |
definition. The optional @{keyword "monos"} section contains
|
wenzelm@44112
|
59 |
\emph{monotonicity theorems}, which are required for each operator
|
wenzelm@44112
|
60 |
applied to a recursive set in the introduction rules. There
|
wenzelm@44112
|
61 |
\emph{must} be a theorem of the form @{text "A \<le> B \<Longrightarrow> M A \<le> M B"},
|
wenzelm@44112
|
62 |
for each premise @{text "M R\<^sub>i t"} in an introduction rule!
|
wenzelm@44112
|
63 |
|
wenzelm@44112
|
64 |
\item @{command (HOL) "inductive_set"} and @{command (HOL)
|
wenzelm@44112
|
65 |
"coinductive_set"} are wrappers for to the previous commands,
|
wenzelm@44112
|
66 |
allowing the definition of (co)inductive sets.
|
wenzelm@44112
|
67 |
|
wenzelm@44112
|
68 |
\item @{attribute (HOL) mono} declares monotonicity rules. These
|
wenzelm@44112
|
69 |
rule are involved in the automated monotonicity proof of @{command
|
wenzelm@44112
|
70 |
(HOL) "inductive"}.
|
wenzelm@44112
|
71 |
|
wenzelm@44112
|
72 |
\end{description}
|
wenzelm@44112
|
73 |
*}
|
wenzelm@44112
|
74 |
|
wenzelm@44112
|
75 |
|
wenzelm@44112
|
76 |
subsection {* Derived rules *}
|
wenzelm@44112
|
77 |
|
wenzelm@44112
|
78 |
text {*
|
wenzelm@44112
|
79 |
Each (co)inductive definition @{text R} adds definitions to the
|
wenzelm@44112
|
80 |
theory and also proves some theorems:
|
wenzelm@44112
|
81 |
|
wenzelm@44112
|
82 |
\begin{description}
|
wenzelm@44112
|
83 |
|
wenzelm@44112
|
84 |
\item @{text R.intros} is the list of introduction rules as proven
|
wenzelm@44112
|
85 |
theorems, for the recursive predicates (or sets). The rules are
|
wenzelm@44112
|
86 |
also available individually, using the names given them in the
|
wenzelm@44112
|
87 |
theory file;
|
wenzelm@44112
|
88 |
|
wenzelm@44112
|
89 |
\item @{text R.cases} is the case analysis (or elimination) rule;
|
wenzelm@44112
|
90 |
|
wenzelm@44112
|
91 |
\item @{text R.induct} or @{text R.coinduct} is the (co)induction
|
wenzelm@44112
|
92 |
rule.
|
wenzelm@44112
|
93 |
|
wenzelm@44112
|
94 |
\end{description}
|
wenzelm@44112
|
95 |
|
wenzelm@44112
|
96 |
When several predicates @{text "R\<^sub>1, \<dots>, R\<^sub>n"} are
|
wenzelm@44112
|
97 |
defined simultaneously, the list of introduction rules is called
|
wenzelm@44112
|
98 |
@{text "R\<^sub>1_\<dots>_R\<^sub>n.intros"}, the case analysis rules are
|
wenzelm@44112
|
99 |
called @{text "R\<^sub>1.cases, \<dots>, R\<^sub>n.cases"}, and the list
|
wenzelm@44112
|
100 |
of mutual induction rules is called @{text
|
wenzelm@44112
|
101 |
"R\<^sub>1_\<dots>_R\<^sub>n.inducts"}.
|
wenzelm@44112
|
102 |
*}
|
wenzelm@44112
|
103 |
|
wenzelm@44112
|
104 |
|
wenzelm@44112
|
105 |
subsection {* Monotonicity theorems *}
|
wenzelm@44112
|
106 |
|
wenzelm@44112
|
107 |
text {*
|
wenzelm@44112
|
108 |
Each theory contains a default set of theorems that are used in
|
wenzelm@44112
|
109 |
monotonicity proofs. New rules can be added to this set via the
|
wenzelm@44112
|
110 |
@{attribute (HOL) mono} attribute. The HOL theory @{text Inductive}
|
wenzelm@44112
|
111 |
shows how this is done. In general, the following monotonicity
|
wenzelm@44112
|
112 |
theorems may be added:
|
wenzelm@44112
|
113 |
|
wenzelm@44112
|
114 |
\begin{itemize}
|
wenzelm@44112
|
115 |
|
wenzelm@44112
|
116 |
\item Theorems of the form @{text "A \<le> B \<Longrightarrow> M A \<le> M B"}, for proving
|
wenzelm@44112
|
117 |
monotonicity of inductive definitions whose introduction rules have
|
wenzelm@44112
|
118 |
premises involving terms such as @{text "M R\<^sub>i t"}.
|
wenzelm@44112
|
119 |
|
wenzelm@44112
|
120 |
\item Monotonicity theorems for logical operators, which are of the
|
wenzelm@44112
|
121 |
general form @{text "(\<dots> \<longrightarrow> \<dots>) \<Longrightarrow> \<dots> (\<dots> \<longrightarrow> \<dots>) \<Longrightarrow> \<dots> \<longrightarrow> \<dots>"}. For example, in
|
wenzelm@44112
|
122 |
the case of the operator @{text "\<or>"}, the corresponding theorem is
|
wenzelm@44112
|
123 |
\[
|
wenzelm@44112
|
124 |
\infer{@{text "P\<^sub>1 \<or> P\<^sub>2 \<longrightarrow> Q\<^sub>1 \<or> Q\<^sub>2"}}{@{text "P\<^sub>1 \<longrightarrow> Q\<^sub>1"} & @{text "P\<^sub>2 \<longrightarrow> Q\<^sub>2"}}
|
wenzelm@44112
|
125 |
\]
|
wenzelm@44112
|
126 |
|
wenzelm@44112
|
127 |
\item De Morgan style equations for reasoning about the ``polarity''
|
wenzelm@44112
|
128 |
of expressions, e.g.
|
wenzelm@44112
|
129 |
\[
|
wenzelm@44112
|
130 |
@{prop "\<not> \<not> P \<longleftrightarrow> P"} \qquad\qquad
|
wenzelm@44112
|
131 |
@{prop "\<not> (P \<and> Q) \<longleftrightarrow> \<not> P \<or> \<not> Q"}
|
wenzelm@44112
|
132 |
\]
|
wenzelm@44112
|
133 |
|
wenzelm@44112
|
134 |
\item Equations for reducing complex operators to more primitive
|
wenzelm@44112
|
135 |
ones whose monotonicity can easily be proved, e.g.
|
wenzelm@44112
|
136 |
\[
|
wenzelm@44112
|
137 |
@{prop "(P \<longrightarrow> Q) \<longleftrightarrow> \<not> P \<or> Q"} \qquad\qquad
|
wenzelm@44112
|
138 |
@{prop "Ball A P \<equiv> \<forall>x. x \<in> A \<longrightarrow> P x"}
|
wenzelm@44112
|
139 |
\]
|
wenzelm@44112
|
140 |
|
wenzelm@44112
|
141 |
\end{itemize}
|
wenzelm@44112
|
142 |
|
wenzelm@44112
|
143 |
%FIXME: Example of an inductive definition
|
wenzelm@44112
|
144 |
*}
|
wenzelm@44112
|
145 |
|
wenzelm@44112
|
146 |
|
wenzelm@44112
|
147 |
section {* Recursive functions \label{sec:recursion} *}
|
wenzelm@44112
|
148 |
|
wenzelm@44112
|
149 |
text {*
|
wenzelm@44112
|
150 |
\begin{matharray}{rcl}
|
wenzelm@44112
|
151 |
@{command_def (HOL) "primrec"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
|
wenzelm@44112
|
152 |
@{command_def (HOL) "fun"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
|
wenzelm@44112
|
153 |
@{command_def (HOL) "function"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\
|
wenzelm@44112
|
154 |
@{command_def (HOL) "termination"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\
|
wenzelm@44112
|
155 |
\end{matharray}
|
wenzelm@44112
|
156 |
|
wenzelm@44112
|
157 |
@{rail "
|
wenzelm@44112
|
158 |
@@{command (HOL) primrec} @{syntax target}? @{syntax \"fixes\"} @'where' equations
|
wenzelm@44112
|
159 |
;
|
wenzelm@44112
|
160 |
(@@{command (HOL) fun} | @@{command (HOL) function}) @{syntax target}? functionopts?
|
wenzelm@44112
|
161 |
@{syntax \"fixes\"} \\ @'where' equations
|
wenzelm@44112
|
162 |
;
|
wenzelm@44112
|
163 |
|
wenzelm@44112
|
164 |
equations: (@{syntax thmdecl}? @{syntax prop} + '|')
|
wenzelm@44112
|
165 |
;
|
wenzelm@44112
|
166 |
functionopts: '(' (('sequential' | 'domintros') + ',') ')'
|
wenzelm@44112
|
167 |
;
|
wenzelm@44112
|
168 |
@@{command (HOL) termination} @{syntax term}?
|
wenzelm@44112
|
169 |
"}
|
wenzelm@44112
|
170 |
|
wenzelm@44112
|
171 |
\begin{description}
|
wenzelm@44112
|
172 |
|
wenzelm@44112
|
173 |
\item @{command (HOL) "primrec"} defines primitive recursive
|
wenzelm@44112
|
174 |
functions over datatypes, see also \cite{isabelle-HOL}.
|
wenzelm@44112
|
175 |
|
wenzelm@44112
|
176 |
\item @{command (HOL) "function"} defines functions by general
|
wenzelm@44112
|
177 |
wellfounded recursion. A detailed description with examples can be
|
wenzelm@44112
|
178 |
found in \cite{isabelle-function}. The function is specified by a
|
wenzelm@44112
|
179 |
set of (possibly conditional) recursive equations with arbitrary
|
wenzelm@44112
|
180 |
pattern matching. The command generates proof obligations for the
|
wenzelm@44112
|
181 |
completeness and the compatibility of patterns.
|
wenzelm@44112
|
182 |
|
wenzelm@44112
|
183 |
The defined function is considered partial, and the resulting
|
wenzelm@44112
|
184 |
simplification rules (named @{text "f.psimps"}) and induction rule
|
wenzelm@44112
|
185 |
(named @{text "f.pinduct"}) are guarded by a generated domain
|
wenzelm@44112
|
186 |
predicate @{text "f_dom"}. The @{command (HOL) "termination"}
|
wenzelm@44112
|
187 |
command can then be used to establish that the function is total.
|
wenzelm@44112
|
188 |
|
wenzelm@44112
|
189 |
\item @{command (HOL) "fun"} is a shorthand notation for ``@{command
|
wenzelm@44112
|
190 |
(HOL) "function"}~@{text "(sequential)"}, followed by automated
|
wenzelm@44112
|
191 |
proof attempts regarding pattern matching and termination. See
|
wenzelm@44112
|
192 |
\cite{isabelle-function} for further details.
|
wenzelm@44112
|
193 |
|
wenzelm@44112
|
194 |
\item @{command (HOL) "termination"}~@{text f} commences a
|
wenzelm@44112
|
195 |
termination proof for the previously defined function @{text f}. If
|
wenzelm@44112
|
196 |
this is omitted, the command refers to the most recent function
|
wenzelm@44112
|
197 |
definition. After the proof is closed, the recursive equations and
|
wenzelm@44112
|
198 |
the induction principle is established.
|
wenzelm@44112
|
199 |
|
wenzelm@44112
|
200 |
\end{description}
|
wenzelm@44112
|
201 |
|
wenzelm@44112
|
202 |
Recursive definitions introduced by the @{command (HOL) "function"}
|
wenzelm@44112
|
203 |
command accommodate
|
wenzelm@44112
|
204 |
reasoning by induction (cf.\ \secref{sec:cases-induct}): rule @{text
|
wenzelm@44112
|
205 |
"c.induct"} (where @{text c} is the name of the function definition)
|
wenzelm@44112
|
206 |
refers to a specific induction rule, with parameters named according
|
wenzelm@44112
|
207 |
to the user-specified equations. Cases are numbered (starting from 1).
|
wenzelm@44112
|
208 |
|
wenzelm@44112
|
209 |
For @{command (HOL) "primrec"}, the induction principle coincides
|
wenzelm@44112
|
210 |
with structural recursion on the datatype the recursion is carried
|
wenzelm@44112
|
211 |
out.
|
wenzelm@44112
|
212 |
|
wenzelm@44112
|
213 |
The equations provided by these packages may be referred later as
|
wenzelm@44112
|
214 |
theorem list @{text "f.simps"}, where @{text f} is the (collective)
|
wenzelm@44112
|
215 |
name of the functions defined. Individual equations may be named
|
wenzelm@44112
|
216 |
explicitly as well.
|
wenzelm@44112
|
217 |
|
wenzelm@44112
|
218 |
The @{command (HOL) "function"} command accepts the following
|
wenzelm@44112
|
219 |
options.
|
wenzelm@44112
|
220 |
|
wenzelm@44112
|
221 |
\begin{description}
|
wenzelm@44112
|
222 |
|
wenzelm@44112
|
223 |
\item @{text sequential} enables a preprocessor which disambiguates
|
wenzelm@44112
|
224 |
overlapping patterns by making them mutually disjoint. Earlier
|
wenzelm@44112
|
225 |
equations take precedence over later ones. This allows to give the
|
wenzelm@44112
|
226 |
specification in a format very similar to functional programming.
|
wenzelm@44112
|
227 |
Note that the resulting simplification and induction rules
|
wenzelm@44112
|
228 |
correspond to the transformed specification, not the one given
|
wenzelm@44112
|
229 |
originally. This usually means that each equation given by the user
|
wenzelm@44112
|
230 |
may result in several theorems. Also note that this automatic
|
wenzelm@44112
|
231 |
transformation only works for ML-style datatype patterns.
|
wenzelm@44112
|
232 |
|
wenzelm@44112
|
233 |
\item @{text domintros} enables the automated generation of
|
wenzelm@44112
|
234 |
introduction rules for the domain predicate. While mostly not
|
wenzelm@44112
|
235 |
needed, they can be helpful in some proofs about partial functions.
|
wenzelm@44112
|
236 |
|
wenzelm@44112
|
237 |
\end{description}
|
wenzelm@44112
|
238 |
*}
|
wenzelm@44112
|
239 |
|
wenzelm@44112
|
240 |
|
wenzelm@44112
|
241 |
subsection {* Proof methods related to recursive definitions *}
|
wenzelm@44112
|
242 |
|
wenzelm@44112
|
243 |
text {*
|
wenzelm@44112
|
244 |
\begin{matharray}{rcl}
|
wenzelm@44112
|
245 |
@{method_def (HOL) pat_completeness} & : & @{text method} \\
|
wenzelm@44112
|
246 |
@{method_def (HOL) relation} & : & @{text method} \\
|
wenzelm@44112
|
247 |
@{method_def (HOL) lexicographic_order} & : & @{text method} \\
|
wenzelm@44112
|
248 |
@{method_def (HOL) size_change} & : & @{text method} \\
|
wenzelm@44112
|
249 |
\end{matharray}
|
wenzelm@44112
|
250 |
|
wenzelm@44112
|
251 |
@{rail "
|
wenzelm@44112
|
252 |
@@{method (HOL) relation} @{syntax term}
|
wenzelm@44112
|
253 |
;
|
wenzelm@44112
|
254 |
@@{method (HOL) lexicographic_order} (@{syntax clasimpmod} * )
|
wenzelm@44112
|
255 |
;
|
wenzelm@44112
|
256 |
@@{method (HOL) size_change} ( orders (@{syntax clasimpmod} * ) )
|
wenzelm@44112
|
257 |
;
|
wenzelm@44112
|
258 |
orders: ( 'max' | 'min' | 'ms' ) *
|
wenzelm@44112
|
259 |
"}
|
wenzelm@44112
|
260 |
|
wenzelm@44112
|
261 |
\begin{description}
|
wenzelm@44112
|
262 |
|
wenzelm@44112
|
263 |
\item @{method (HOL) pat_completeness} is a specialized method to
|
wenzelm@44112
|
264 |
solve goals regarding the completeness of pattern matching, as
|
wenzelm@44112
|
265 |
required by the @{command (HOL) "function"} package (cf.\
|
wenzelm@44112
|
266 |
\cite{isabelle-function}).
|
wenzelm@44112
|
267 |
|
wenzelm@44112
|
268 |
\item @{method (HOL) relation}~@{text R} introduces a termination
|
wenzelm@44112
|
269 |
proof using the relation @{text R}. The resulting proof state will
|
wenzelm@44112
|
270 |
contain goals expressing that @{text R} is wellfounded, and that the
|
wenzelm@44112
|
271 |
arguments of recursive calls decrease with respect to @{text R}.
|
wenzelm@44112
|
272 |
Usually, this method is used as the initial proof step of manual
|
wenzelm@44112
|
273 |
termination proofs.
|
wenzelm@44112
|
274 |
|
wenzelm@44112
|
275 |
\item @{method (HOL) "lexicographic_order"} attempts a fully
|
wenzelm@44112
|
276 |
automated termination proof by searching for a lexicographic
|
wenzelm@44112
|
277 |
combination of size measures on the arguments of the function. The
|
wenzelm@44112
|
278 |
method accepts the same arguments as the @{method auto} method,
|
wenzelm@44112
|
279 |
which it uses internally to prove local descents. The same context
|
wenzelm@44112
|
280 |
modifiers as for @{method auto} are accepted, see
|
wenzelm@44112
|
281 |
\secref{sec:clasimp}.
|
wenzelm@44112
|
282 |
|
wenzelm@44112
|
283 |
In case of failure, extensive information is printed, which can help
|
wenzelm@44112
|
284 |
to analyse the situation (cf.\ \cite{isabelle-function}).
|
wenzelm@44112
|
285 |
|
wenzelm@44112
|
286 |
\item @{method (HOL) "size_change"} also works on termination goals,
|
wenzelm@44112
|
287 |
using a variation of the size-change principle, together with a
|
wenzelm@44112
|
288 |
graph decomposition technique (see \cite{krauss_phd} for details).
|
wenzelm@44112
|
289 |
Three kinds of orders are used internally: @{text max}, @{text min},
|
wenzelm@44112
|
290 |
and @{text ms} (multiset), which is only available when the theory
|
wenzelm@44112
|
291 |
@{text Multiset} is loaded. When no order kinds are given, they are
|
wenzelm@44112
|
292 |
tried in order. The search for a termination proof uses SAT solving
|
wenzelm@44112
|
293 |
internally.
|
wenzelm@44112
|
294 |
|
wenzelm@44112
|
295 |
For local descent proofs, the same context modifiers as for @{method
|
wenzelm@44112
|
296 |
auto} are accepted, see \secref{sec:clasimp}.
|
wenzelm@44112
|
297 |
|
wenzelm@44112
|
298 |
\end{description}
|
wenzelm@44112
|
299 |
*}
|
wenzelm@44112
|
300 |
|
wenzelm@44112
|
301 |
|
wenzelm@44112
|
302 |
subsection {* Functions with explicit partiality *}
|
wenzelm@44112
|
303 |
|
wenzelm@44112
|
304 |
text {*
|
wenzelm@44112
|
305 |
\begin{matharray}{rcl}
|
wenzelm@44112
|
306 |
@{command_def (HOL) "partial_function"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
|
wenzelm@44112
|
307 |
@{attribute_def (HOL) "partial_function_mono"} & : & @{text attribute} \\
|
wenzelm@44112
|
308 |
\end{matharray}
|
wenzelm@44112
|
309 |
|
wenzelm@44112
|
310 |
@{rail "
|
wenzelm@44112
|
311 |
@@{command (HOL) partial_function} @{syntax target}?
|
wenzelm@44112
|
312 |
'(' @{syntax nameref} ')' @{syntax \"fixes\"} \\
|
wenzelm@44112
|
313 |
@'where' @{syntax thmdecl}? @{syntax prop}
|
wenzelm@44112
|
314 |
"}
|
wenzelm@44112
|
315 |
|
wenzelm@44112
|
316 |
\begin{description}
|
wenzelm@44112
|
317 |
|
wenzelm@44112
|
318 |
\item @{command (HOL) "partial_function"}~@{text "(mode)"} defines
|
wenzelm@44112
|
319 |
recursive functions based on fixpoints in complete partial
|
wenzelm@44112
|
320 |
orders. No termination proof is required from the user or
|
wenzelm@44112
|
321 |
constructed internally. Instead, the possibility of non-termination
|
wenzelm@44112
|
322 |
is modelled explicitly in the result type, which contains an
|
wenzelm@44112
|
323 |
explicit bottom element.
|
wenzelm@44112
|
324 |
|
wenzelm@44112
|
325 |
Pattern matching and mutual recursion are currently not supported.
|
wenzelm@44112
|
326 |
Thus, the specification consists of a single function described by a
|
wenzelm@44112
|
327 |
single recursive equation.
|
wenzelm@44112
|
328 |
|
wenzelm@44112
|
329 |
There are no fixed syntactic restrictions on the body of the
|
wenzelm@44112
|
330 |
function, but the induced functional must be provably monotonic
|
wenzelm@44112
|
331 |
wrt.\ the underlying order. The monotonicitity proof is performed
|
wenzelm@44112
|
332 |
internally, and the definition is rejected when it fails. The proof
|
wenzelm@44112
|
333 |
can be influenced by declaring hints using the
|
wenzelm@44112
|
334 |
@{attribute (HOL) partial_function_mono} attribute.
|
wenzelm@44112
|
335 |
|
wenzelm@44112
|
336 |
The mandatory @{text mode} argument specifies the mode of operation
|
wenzelm@44112
|
337 |
of the command, which directly corresponds to a complete partial
|
wenzelm@44112
|
338 |
order on the result type. By default, the following modes are
|
wenzelm@44112
|
339 |
defined:
|
wenzelm@44112
|
340 |
|
wenzelm@44112
|
341 |
\begin{description}
|
wenzelm@44112
|
342 |
\item @{text option} defines functions that map into the @{type
|
wenzelm@44112
|
343 |
option} type. Here, the value @{term None} is used to model a
|
wenzelm@44112
|
344 |
non-terminating computation. Monotonicity requires that if @{term
|
wenzelm@44112
|
345 |
None} is returned by a recursive call, then the overall result
|
wenzelm@44112
|
346 |
must also be @{term None}. This is best achieved through the use of
|
wenzelm@44112
|
347 |
the monadic operator @{const "Option.bind"}.
|
wenzelm@44112
|
348 |
|
wenzelm@44112
|
349 |
\item @{text tailrec} defines functions with an arbitrary result
|
wenzelm@44112
|
350 |
type and uses the slightly degenerated partial order where @{term
|
wenzelm@44112
|
351 |
"undefined"} is the bottom element. Now, monotonicity requires that
|
wenzelm@44112
|
352 |
if @{term undefined} is returned by a recursive call, then the
|
wenzelm@44112
|
353 |
overall result must also be @{term undefined}. In practice, this is
|
wenzelm@44112
|
354 |
only satisfied when each recursive call is a tail call, whose result
|
wenzelm@44112
|
355 |
is directly returned. Thus, this mode of operation allows the
|
wenzelm@44112
|
356 |
definition of arbitrary tail-recursive functions.
|
wenzelm@44112
|
357 |
\end{description}
|
wenzelm@44112
|
358 |
|
wenzelm@44112
|
359 |
Experienced users may define new modes by instantiating the locale
|
wenzelm@44112
|
360 |
@{const "partial_function_definitions"} appropriately.
|
wenzelm@44112
|
361 |
|
wenzelm@44112
|
362 |
\item @{attribute (HOL) partial_function_mono} declares rules for
|
wenzelm@44112
|
363 |
use in the internal monononicity proofs of partial function
|
wenzelm@44112
|
364 |
definitions.
|
wenzelm@44112
|
365 |
|
wenzelm@44112
|
366 |
\end{description}
|
wenzelm@44112
|
367 |
|
wenzelm@44112
|
368 |
*}
|
wenzelm@44112
|
369 |
|
wenzelm@44112
|
370 |
|
wenzelm@44112
|
371 |
subsection {* Old-style recursive function definitions (TFL) *}
|
wenzelm@44112
|
372 |
|
wenzelm@44112
|
373 |
text {*
|
wenzelm@44112
|
374 |
The old TFL commands @{command (HOL) "recdef"} and @{command (HOL)
|
wenzelm@44112
|
375 |
"recdef_tc"} for defining recursive are mostly obsolete; @{command
|
wenzelm@44112
|
376 |
(HOL) "function"} or @{command (HOL) "fun"} should be used instead.
|
wenzelm@44112
|
377 |
|
wenzelm@44112
|
378 |
\begin{matharray}{rcl}
|
wenzelm@44112
|
379 |
@{command_def (HOL) "recdef"} & : & @{text "theory \<rightarrow> theory)"} \\
|
wenzelm@44112
|
380 |
@{command_def (HOL) "recdef_tc"}@{text "\<^sup>*"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
|
wenzelm@44112
|
381 |
\end{matharray}
|
wenzelm@44112
|
382 |
|
wenzelm@44112
|
383 |
@{rail "
|
wenzelm@44112
|
384 |
@@{command (HOL) recdef} ('(' @'permissive' ')')? \\
|
wenzelm@44112
|
385 |
@{syntax name} @{syntax term} (@{syntax prop} +) hints?
|
wenzelm@44112
|
386 |
;
|
wenzelm@44112
|
387 |
recdeftc @{syntax thmdecl}? tc
|
wenzelm@44112
|
388 |
;
|
wenzelm@44112
|
389 |
hints: '(' @'hints' ( recdefmod * ) ')'
|
wenzelm@44112
|
390 |
;
|
wenzelm@44112
|
391 |
recdefmod: (('recdef_simp' | 'recdef_cong' | 'recdef_wf')
|
wenzelm@44112
|
392 |
(() | 'add' | 'del') ':' @{syntax thmrefs}) | @{syntax clasimpmod}
|
wenzelm@44112
|
393 |
;
|
wenzelm@44112
|
394 |
tc: @{syntax nameref} ('(' @{syntax nat} ')')?
|
wenzelm@44112
|
395 |
"}
|
wenzelm@44112
|
396 |
|
wenzelm@44112
|
397 |
\begin{description}
|
wenzelm@44112
|
398 |
|
wenzelm@44112
|
399 |
\item @{command (HOL) "recdef"} defines general well-founded
|
wenzelm@44112
|
400 |
recursive functions (using the TFL package), see also
|
wenzelm@44112
|
401 |
\cite{isabelle-HOL}. The ``@{text "(permissive)"}'' option tells
|
wenzelm@44112
|
402 |
TFL to recover from failed proof attempts, returning unfinished
|
wenzelm@44112
|
403 |
results. The @{text recdef_simp}, @{text recdef_cong}, and @{text
|
wenzelm@44112
|
404 |
recdef_wf} hints refer to auxiliary rules to be used in the internal
|
wenzelm@44112
|
405 |
automated proof process of TFL. Additional @{syntax clasimpmod}
|
wenzelm@44112
|
406 |
declarations (cf.\ \secref{sec:clasimp}) may be given to tune the
|
wenzelm@44112
|
407 |
context of the Simplifier (cf.\ \secref{sec:simplifier}) and
|
wenzelm@44112
|
408 |
Classical reasoner (cf.\ \secref{sec:classical}).
|
wenzelm@44112
|
409 |
|
wenzelm@44112
|
410 |
\item @{command (HOL) "recdef_tc"}~@{text "c (i)"} recommences the
|
wenzelm@44112
|
411 |
proof for leftover termination condition number @{text i} (default
|
wenzelm@44112
|
412 |
1) as generated by a @{command (HOL) "recdef"} definition of
|
wenzelm@44112
|
413 |
constant @{text c}.
|
wenzelm@44112
|
414 |
|
wenzelm@44112
|
415 |
Note that in most cases, @{command (HOL) "recdef"} is able to finish
|
wenzelm@44112
|
416 |
its internal proofs without manual intervention.
|
wenzelm@44112
|
417 |
|
wenzelm@44112
|
418 |
\end{description}
|
wenzelm@44112
|
419 |
|
wenzelm@44112
|
420 |
\medskip Hints for @{command (HOL) "recdef"} may be also declared
|
wenzelm@44112
|
421 |
globally, using the following attributes.
|
wenzelm@44112
|
422 |
|
wenzelm@44112
|
423 |
\begin{matharray}{rcl}
|
wenzelm@44112
|
424 |
@{attribute_def (HOL) recdef_simp} & : & @{text attribute} \\
|
wenzelm@44112
|
425 |
@{attribute_def (HOL) recdef_cong} & : & @{text attribute} \\
|
wenzelm@44112
|
426 |
@{attribute_def (HOL) recdef_wf} & : & @{text attribute} \\
|
wenzelm@44112
|
427 |
\end{matharray}
|
wenzelm@44112
|
428 |
|
wenzelm@44112
|
429 |
@{rail "
|
wenzelm@44112
|
430 |
(@@{attribute (HOL) recdef_simp} | @@{attribute (HOL) recdef_cong} |
|
wenzelm@44112
|
431 |
@@{attribute (HOL) recdef_wf}) (() | 'add' | 'del')
|
wenzelm@44112
|
432 |
"}
|
wenzelm@44112
|
433 |
*}
|
wenzelm@44112
|
434 |
|
wenzelm@44112
|
435 |
|
wenzelm@44112
|
436 |
section {* Datatypes \label{sec:hol-datatype} *}
|
wenzelm@44112
|
437 |
|
wenzelm@44112
|
438 |
text {*
|
wenzelm@44112
|
439 |
\begin{matharray}{rcl}
|
wenzelm@44112
|
440 |
@{command_def (HOL) "datatype"} & : & @{text "theory \<rightarrow> theory"} \\
|
wenzelm@44112
|
441 |
@{command_def (HOL) "rep_datatype"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
|
wenzelm@44112
|
442 |
\end{matharray}
|
wenzelm@44112
|
443 |
|
wenzelm@44112
|
444 |
@{rail "
|
wenzelm@44112
|
445 |
@@{command (HOL) datatype} (spec + @'and')
|
wenzelm@44112
|
446 |
;
|
wenzelm@44112
|
447 |
@@{command (HOL) rep_datatype} ('(' (@{syntax name} +) ')')? (@{syntax term} +)
|
wenzelm@44112
|
448 |
;
|
wenzelm@44112
|
449 |
|
wenzelm@44112
|
450 |
spec: @{syntax parname}? @{syntax typespec} @{syntax mixfix}? '=' (cons + '|')
|
wenzelm@44112
|
451 |
;
|
wenzelm@44112
|
452 |
cons: @{syntax name} (@{syntax type} * ) @{syntax mixfix}?
|
wenzelm@44112
|
453 |
"}
|
wenzelm@44112
|
454 |
|
wenzelm@44112
|
455 |
\begin{description}
|
wenzelm@44112
|
456 |
|
wenzelm@44112
|
457 |
\item @{command (HOL) "datatype"} defines inductive datatypes in
|
wenzelm@44112
|
458 |
HOL.
|
wenzelm@44112
|
459 |
|
wenzelm@44112
|
460 |
\item @{command (HOL) "rep_datatype"} represents existing types as
|
wenzelm@44112
|
461 |
inductive ones, generating the standard infrastructure of derived
|
wenzelm@44112
|
462 |
concepts (primitive recursion etc.).
|
wenzelm@44112
|
463 |
|
wenzelm@44112
|
464 |
\end{description}
|
wenzelm@44112
|
465 |
|
wenzelm@44112
|
466 |
The induction and exhaustion theorems generated provide case names
|
wenzelm@44112
|
467 |
according to the constructors involved, while parameters are named
|
wenzelm@44112
|
468 |
after the types (see also \secref{sec:cases-induct}).
|
wenzelm@44112
|
469 |
|
wenzelm@44112
|
470 |
See \cite{isabelle-HOL} for more details on datatypes, but beware of
|
wenzelm@44112
|
471 |
the old-style theory syntax being used there! Apart from proper
|
wenzelm@44112
|
472 |
proof methods for case-analysis and induction, there are also
|
wenzelm@44112
|
473 |
emulations of ML tactics @{method (HOL) case_tac} and @{method (HOL)
|
wenzelm@44112
|
474 |
induct_tac} available, see \secref{sec:hol-induct-tac}; these admit
|
wenzelm@44112
|
475 |
to refer directly to the internal structure of subgoals (including
|
wenzelm@44112
|
476 |
internally bound parameters).
|
wenzelm@44112
|
477 |
*}
|
wenzelm@44112
|
478 |
|
wenzelm@44112
|
479 |
|
wenzelm@44112
|
480 |
section {* Records \label{sec:hol-record} *}
|
wenzelm@44112
|
481 |
|
wenzelm@44112
|
482 |
text {*
|
wenzelm@44112
|
483 |
In principle, records merely generalize the concept of tuples, where
|
wenzelm@44112
|
484 |
components may be addressed by labels instead of just position. The
|
wenzelm@44112
|
485 |
logical infrastructure of records in Isabelle/HOL is slightly more
|
wenzelm@44112
|
486 |
advanced, though, supporting truly extensible record schemes. This
|
wenzelm@44112
|
487 |
admits operations that are polymorphic with respect to record
|
wenzelm@44112
|
488 |
extension, yielding ``object-oriented'' effects like (single)
|
wenzelm@44112
|
489 |
inheritance. See also \cite{NaraschewskiW-TPHOLs98} for more
|
wenzelm@44112
|
490 |
details on object-oriented verification and record subtyping in HOL.
|
wenzelm@44112
|
491 |
*}
|
wenzelm@44112
|
492 |
|
wenzelm@44112
|
493 |
|
wenzelm@44112
|
494 |
subsection {* Basic concepts *}
|
wenzelm@44112
|
495 |
|
wenzelm@44112
|
496 |
text {*
|
wenzelm@44112
|
497 |
Isabelle/HOL supports both \emph{fixed} and \emph{schematic} records
|
wenzelm@44112
|
498 |
at the level of terms and types. The notation is as follows:
|
wenzelm@44112
|
499 |
|
wenzelm@44112
|
500 |
\begin{center}
|
wenzelm@44112
|
501 |
\begin{tabular}{l|l|l}
|
wenzelm@44112
|
502 |
& record terms & record types \\ \hline
|
wenzelm@44112
|
503 |
fixed & @{text "\<lparr>x = a, y = b\<rparr>"} & @{text "\<lparr>x :: A, y :: B\<rparr>"} \\
|
wenzelm@44112
|
504 |
schematic & @{text "\<lparr>x = a, y = b, \<dots> = m\<rparr>"} &
|
wenzelm@44112
|
505 |
@{text "\<lparr>x :: A, y :: B, \<dots> :: M\<rparr>"} \\
|
wenzelm@44112
|
506 |
\end{tabular}
|
wenzelm@44112
|
507 |
\end{center}
|
wenzelm@44112
|
508 |
|
wenzelm@44112
|
509 |
\noindent The ASCII representation of @{text "\<lparr>x = a\<rparr>"} is @{text
|
wenzelm@44112
|
510 |
"(| x = a |)"}.
|
wenzelm@44112
|
511 |
|
wenzelm@44112
|
512 |
A fixed record @{text "\<lparr>x = a, y = b\<rparr>"} has field @{text x} of value
|
wenzelm@44112
|
513 |
@{text a} and field @{text y} of value @{text b}. The corresponding
|
wenzelm@44112
|
514 |
type is @{text "\<lparr>x :: A, y :: B\<rparr>"}, assuming that @{text "a :: A"}
|
wenzelm@44112
|
515 |
and @{text "b :: B"}.
|
wenzelm@44112
|
516 |
|
wenzelm@44112
|
517 |
A record scheme like @{text "\<lparr>x = a, y = b, \<dots> = m\<rparr>"} contains fields
|
wenzelm@44112
|
518 |
@{text x} and @{text y} as before, but also possibly further fields
|
wenzelm@44112
|
519 |
as indicated by the ``@{text "\<dots>"}'' notation (which is actually part
|
wenzelm@44112
|
520 |
of the syntax). The improper field ``@{text "\<dots>"}'' of a record
|
wenzelm@44112
|
521 |
scheme is called the \emph{more part}. Logically it is just a free
|
wenzelm@44112
|
522 |
variable, which is occasionally referred to as ``row variable'' in
|
wenzelm@44112
|
523 |
the literature. The more part of a record scheme may be
|
wenzelm@44112
|
524 |
instantiated by zero or more further components. For example, the
|
wenzelm@44112
|
525 |
previous scheme may get instantiated to @{text "\<lparr>x = a, y = b, z =
|
wenzelm@44112
|
526 |
c, \<dots> = m'\<rparr>"}, where @{text m'} refers to a different more part.
|
wenzelm@44112
|
527 |
Fixed records are special instances of record schemes, where
|
wenzelm@44112
|
528 |
``@{text "\<dots>"}'' is properly terminated by the @{text "() :: unit"}
|
wenzelm@44112
|
529 |
element. In fact, @{text "\<lparr>x = a, y = b\<rparr>"} is just an abbreviation
|
wenzelm@44112
|
530 |
for @{text "\<lparr>x = a, y = b, \<dots> = ()\<rparr>"}.
|
wenzelm@44112
|
531 |
|
wenzelm@44112
|
532 |
\medskip Two key observations make extensible records in a simply
|
wenzelm@44112
|
533 |
typed language like HOL work out:
|
wenzelm@44112
|
534 |
|
wenzelm@44112
|
535 |
\begin{enumerate}
|
wenzelm@44112
|
536 |
|
wenzelm@44112
|
537 |
\item the more part is internalized, as a free term or type
|
wenzelm@44112
|
538 |
variable,
|
wenzelm@44112
|
539 |
|
wenzelm@44112
|
540 |
\item field names are externalized, they cannot be accessed within
|
wenzelm@44112
|
541 |
the logic as first-class values.
|
wenzelm@44112
|
542 |
|
wenzelm@44112
|
543 |
\end{enumerate}
|
wenzelm@44112
|
544 |
|
wenzelm@44112
|
545 |
\medskip In Isabelle/HOL record types have to be defined explicitly,
|
wenzelm@44112
|
546 |
fixing their field names and types, and their (optional) parent
|
wenzelm@44112
|
547 |
record. Afterwards, records may be formed using above syntax, while
|
wenzelm@44112
|
548 |
obeying the canonical order of fields as given by their declaration.
|
wenzelm@44112
|
549 |
The record package provides several standard operations like
|
wenzelm@44112
|
550 |
selectors and updates. The common setup for various generic proof
|
wenzelm@44112
|
551 |
tools enable succinct reasoning patterns. See also the Isabelle/HOL
|
wenzelm@44112
|
552 |
tutorial \cite{isabelle-hol-book} for further instructions on using
|
wenzelm@44112
|
553 |
records in practice.
|
wenzelm@44112
|
554 |
*}
|
wenzelm@44112
|
555 |
|
wenzelm@44112
|
556 |
|
wenzelm@44112
|
557 |
subsection {* Record specifications *}
|
wenzelm@44112
|
558 |
|
wenzelm@44112
|
559 |
text {*
|
wenzelm@44112
|
560 |
\begin{matharray}{rcl}
|
wenzelm@44112
|
561 |
@{command_def (HOL) "record"} & : & @{text "theory \<rightarrow> theory"} \\
|
wenzelm@44112
|
562 |
\end{matharray}
|
wenzelm@44112
|
563 |
|
wenzelm@44112
|
564 |
@{rail "
|
wenzelm@44112
|
565 |
@@{command (HOL) record} @{syntax typespec_sorts} '=' \\
|
wenzelm@44112
|
566 |
(@{syntax type} '+')? (@{syntax constdecl} +)
|
wenzelm@44112
|
567 |
"}
|
wenzelm@44112
|
568 |
|
wenzelm@44112
|
569 |
\begin{description}
|
wenzelm@44112
|
570 |
|
wenzelm@44112
|
571 |
\item @{command (HOL) "record"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t = \<tau> + c\<^sub>1 :: \<sigma>\<^sub>1
|
wenzelm@44112
|
572 |
\<dots> c\<^sub>n :: \<sigma>\<^sub>n"} defines extensible record type @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"},
|
wenzelm@44112
|
573 |
derived from the optional parent record @{text "\<tau>"} by adding new
|
wenzelm@44112
|
574 |
field components @{text "c\<^sub>i :: \<sigma>\<^sub>i"} etc.
|
wenzelm@44112
|
575 |
|
wenzelm@44112
|
576 |
The type variables of @{text "\<tau>"} and @{text "\<sigma>\<^sub>i"} need to be
|
wenzelm@44112
|
577 |
covered by the (distinct) parameters @{text "\<alpha>\<^sub>1, \<dots>,
|
wenzelm@44112
|
578 |
\<alpha>\<^sub>m"}. Type constructor @{text t} has to be new, while @{text
|
wenzelm@44112
|
579 |
\<tau>} needs to specify an instance of an existing record type. At
|
wenzelm@44112
|
580 |
least one new field @{text "c\<^sub>i"} has to be specified.
|
wenzelm@44112
|
581 |
Basically, field names need to belong to a unique record. This is
|
wenzelm@44112
|
582 |
not a real restriction in practice, since fields are qualified by
|
wenzelm@44112
|
583 |
the record name internally.
|
wenzelm@44112
|
584 |
|
wenzelm@44112
|
585 |
The parent record specification @{text \<tau>} is optional; if omitted
|
wenzelm@44112
|
586 |
@{text t} becomes a root record. The hierarchy of all records
|
wenzelm@44112
|
587 |
declared within a theory context forms a forest structure, i.e.\ a
|
wenzelm@44112
|
588 |
set of trees starting with a root record each. There is no way to
|
wenzelm@44112
|
589 |
merge multiple parent records!
|
wenzelm@44112
|
590 |
|
wenzelm@44112
|
591 |
For convenience, @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"} is made a
|
wenzelm@44112
|
592 |
type abbreviation for the fixed record type @{text "\<lparr>c\<^sub>1 ::
|
wenzelm@44112
|
593 |
\<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n\<rparr>"}, likewise is @{text
|
wenzelm@44112
|
594 |
"(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m, \<zeta>) t_scheme"} made an abbreviation for
|
wenzelm@44112
|
595 |
@{text "\<lparr>c\<^sub>1 :: \<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n, \<dots> ::
|
wenzelm@44112
|
596 |
\<zeta>\<rparr>"}.
|
wenzelm@44112
|
597 |
|
wenzelm@44112
|
598 |
\end{description}
|
wenzelm@44112
|
599 |
*}
|
wenzelm@44112
|
600 |
|
wenzelm@44112
|
601 |
|
wenzelm@44112
|
602 |
subsection {* Record operations *}
|
wenzelm@44112
|
603 |
|
wenzelm@44112
|
604 |
text {*
|
wenzelm@44112
|
605 |
Any record definition of the form presented above produces certain
|
wenzelm@44112
|
606 |
standard operations. Selectors and updates are provided for any
|
wenzelm@44112
|
607 |
field, including the improper one ``@{text more}''. There are also
|
wenzelm@44112
|
608 |
cumulative record constructor functions. To simplify the
|
wenzelm@44112
|
609 |
presentation below, we assume for now that @{text "(\<alpha>\<^sub>1, \<dots>,
|
wenzelm@44112
|
610 |
\<alpha>\<^sub>m) t"} is a root record with fields @{text "c\<^sub>1 ::
|
wenzelm@44112
|
611 |
\<sigma>\<^sub>1, \<dots>, c\<^sub>n :: \<sigma>\<^sub>n"}.
|
wenzelm@44112
|
612 |
|
wenzelm@44112
|
613 |
\medskip \textbf{Selectors} and \textbf{updates} are available for
|
wenzelm@44112
|
614 |
any field (including ``@{text more}''):
|
wenzelm@44112
|
615 |
|
wenzelm@44112
|
616 |
\begin{matharray}{lll}
|
wenzelm@44112
|
617 |
@{text "c\<^sub>i"} & @{text "::"} & @{text "\<lparr>\<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<sigma>\<^sub>i"} \\
|
wenzelm@44112
|
618 |
@{text "c\<^sub>i_update"} & @{text "::"} & @{text "\<sigma>\<^sub>i \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr>"} \\
|
wenzelm@44112
|
619 |
\end{matharray}
|
wenzelm@44112
|
620 |
|
wenzelm@44112
|
621 |
There is special syntax for application of updates: @{text "r\<lparr>x :=
|
wenzelm@44112
|
622 |
a\<rparr>"} abbreviates term @{text "x_update a r"}. Further notation for
|
wenzelm@44112
|
623 |
repeated updates is also available: @{text "r\<lparr>x := a\<rparr>\<lparr>y := b\<rparr>\<lparr>z :=
|
wenzelm@44112
|
624 |
c\<rparr>"} may be written @{text "r\<lparr>x := a, y := b, z := c\<rparr>"}. Note that
|
wenzelm@44112
|
625 |
because of postfix notation the order of fields shown here is
|
wenzelm@44112
|
626 |
reverse than in the actual term. Since repeated updates are just
|
wenzelm@44112
|
627 |
function applications, fields may be freely permuted in @{text "\<lparr>x
|
wenzelm@44112
|
628 |
:= a, y := b, z := c\<rparr>"}, as far as logical equality is concerned.
|
wenzelm@44112
|
629 |
Thus commutativity of independent updates can be proven within the
|
wenzelm@44112
|
630 |
logic for any two fields, but not as a general theorem.
|
wenzelm@44112
|
631 |
|
wenzelm@44112
|
632 |
\medskip The \textbf{make} operation provides a cumulative record
|
wenzelm@44112
|
633 |
constructor function:
|
wenzelm@44112
|
634 |
|
wenzelm@44112
|
635 |
\begin{matharray}{lll}
|
wenzelm@44112
|
636 |
@{text "t.make"} & @{text "::"} & @{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>n \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
|
wenzelm@44112
|
637 |
\end{matharray}
|
wenzelm@44112
|
638 |
|
wenzelm@44112
|
639 |
\medskip We now reconsider the case of non-root records, which are
|
wenzelm@44112
|
640 |
derived of some parent. In general, the latter may depend on
|
wenzelm@44112
|
641 |
another parent as well, resulting in a list of \emph{ancestor
|
wenzelm@44112
|
642 |
records}. Appending the lists of fields of all ancestors results in
|
wenzelm@44112
|
643 |
a certain field prefix. The record package automatically takes care
|
wenzelm@44112
|
644 |
of this by lifting operations over this context of ancestor fields.
|
wenzelm@44112
|
645 |
Assuming that @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>m) t"} has ancestor
|
wenzelm@44112
|
646 |
fields @{text "b\<^sub>1 :: \<rho>\<^sub>1, \<dots>, b\<^sub>k :: \<rho>\<^sub>k"},
|
wenzelm@44112
|
647 |
the above record operations will get the following types:
|
wenzelm@44112
|
648 |
|
wenzelm@44112
|
649 |
\medskip
|
wenzelm@44112
|
650 |
\begin{tabular}{lll}
|
wenzelm@44112
|
651 |
@{text "c\<^sub>i"} & @{text "::"} & @{text "\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<sigma>\<^sub>i"} \\
|
wenzelm@44112
|
652 |
@{text "c\<^sub>i_update"} & @{text "::"} & @{text "\<sigma>\<^sub>i \<Rightarrow>
|
wenzelm@44112
|
653 |
\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow>
|
wenzelm@44112
|
654 |
\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr>"} \\
|
wenzelm@44112
|
655 |
@{text "t.make"} & @{text "::"} & @{text "\<rho>\<^sub>1 \<Rightarrow> \<dots> \<rho>\<^sub>k \<Rightarrow> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>n \<Rightarrow>
|
wenzelm@44112
|
656 |
\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
|
wenzelm@44112
|
657 |
\end{tabular}
|
wenzelm@44112
|
658 |
\medskip
|
wenzelm@44112
|
659 |
|
wenzelm@44112
|
660 |
\noindent Some further operations address the extension aspect of a
|
wenzelm@44112
|
661 |
derived record scheme specifically: @{text "t.fields"} produces a
|
wenzelm@44112
|
662 |
record fragment consisting of exactly the new fields introduced here
|
wenzelm@44112
|
663 |
(the result may serve as a more part elsewhere); @{text "t.extend"}
|
wenzelm@44112
|
664 |
takes a fixed record and adds a given more part; @{text
|
wenzelm@44112
|
665 |
"t.truncate"} restricts a record scheme to a fixed record.
|
wenzelm@44112
|
666 |
|
wenzelm@44112
|
667 |
\medskip
|
wenzelm@44112
|
668 |
\begin{tabular}{lll}
|
wenzelm@44112
|
669 |
@{text "t.fields"} & @{text "::"} & @{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<sigma>\<^sub>n \<Rightarrow> \<lparr>\<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
|
wenzelm@44112
|
670 |
@{text "t.extend"} & @{text "::"} & @{text "\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>\<rparr> \<Rightarrow>
|
wenzelm@44112
|
671 |
\<zeta> \<Rightarrow> \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr>"} \\
|
wenzelm@44112
|
672 |
@{text "t.truncate"} & @{text "::"} & @{text "\<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>, \<dots> :: \<zeta>\<rparr> \<Rightarrow> \<lparr>\<^vec>b :: \<^vec>\<rho>, \<^vec>c :: \<^vec>\<sigma>\<rparr>"} \\
|
wenzelm@44112
|
673 |
\end{tabular}
|
wenzelm@44112
|
674 |
\medskip
|
wenzelm@44112
|
675 |
|
wenzelm@44112
|
676 |
\noindent Note that @{text "t.make"} and @{text "t.fields"} coincide
|
wenzelm@44112
|
677 |
for root records.
|
wenzelm@44112
|
678 |
*}
|
wenzelm@44112
|
679 |
|
wenzelm@44112
|
680 |
|
wenzelm@44112
|
681 |
subsection {* Derived rules and proof tools *}
|
wenzelm@44112
|
682 |
|
wenzelm@44112
|
683 |
text {*
|
wenzelm@44112
|
684 |
The record package proves several results internally, declaring
|
wenzelm@44112
|
685 |
these facts to appropriate proof tools. This enables users to
|
wenzelm@44112
|
686 |
reason about record structures quite conveniently. Assume that
|
wenzelm@44112
|
687 |
@{text t} is a record type as specified above.
|
wenzelm@44112
|
688 |
|
wenzelm@44112
|
689 |
\begin{enumerate}
|
wenzelm@44112
|
690 |
|
wenzelm@44112
|
691 |
\item Standard conversions for selectors or updates applied to
|
wenzelm@44112
|
692 |
record constructor terms are made part of the default Simplifier
|
wenzelm@44112
|
693 |
context; thus proofs by reduction of basic operations merely require
|
wenzelm@44112
|
694 |
the @{method simp} method without further arguments. These rules
|
wenzelm@44112
|
695 |
are available as @{text "t.simps"}, too.
|
wenzelm@44112
|
696 |
|
wenzelm@44112
|
697 |
\item Selectors applied to updated records are automatically reduced
|
wenzelm@44112
|
698 |
by an internal simplification procedure, which is also part of the
|
wenzelm@44112
|
699 |
standard Simplifier setup.
|
wenzelm@44112
|
700 |
|
wenzelm@44112
|
701 |
\item Inject equations of a form analogous to @{prop "(x, y) = (x',
|
wenzelm@44112
|
702 |
y') \<equiv> x = x' \<and> y = y'"} are declared to the Simplifier and Classical
|
wenzelm@44112
|
703 |
Reasoner as @{attribute iff} rules. These rules are available as
|
wenzelm@44112
|
704 |
@{text "t.iffs"}.
|
wenzelm@44112
|
705 |
|
wenzelm@44112
|
706 |
\item The introduction rule for record equality analogous to @{text
|
wenzelm@44112
|
707 |
"x r = x r' \<Longrightarrow> y r = y r' \<dots> \<Longrightarrow> r = r'"} is declared to the Simplifier,
|
wenzelm@44112
|
708 |
and as the basic rule context as ``@{attribute intro}@{text "?"}''.
|
wenzelm@44112
|
709 |
The rule is called @{text "t.equality"}.
|
wenzelm@44112
|
710 |
|
wenzelm@44112
|
711 |
\item Representations of arbitrary record expressions as canonical
|
wenzelm@44112
|
712 |
constructor terms are provided both in @{method cases} and @{method
|
wenzelm@44112
|
713 |
induct} format (cf.\ the generic proof methods of the same name,
|
wenzelm@44112
|
714 |
\secref{sec:cases-induct}). Several variations are available, for
|
wenzelm@44112
|
715 |
fixed records, record schemes, more parts etc.
|
wenzelm@44112
|
716 |
|
wenzelm@44112
|
717 |
The generic proof methods are sufficiently smart to pick the most
|
wenzelm@44112
|
718 |
sensible rule according to the type of the indicated record
|
wenzelm@44112
|
719 |
expression: users just need to apply something like ``@{text "(cases
|
wenzelm@44112
|
720 |
r)"}'' to a certain proof problem.
|
wenzelm@44112
|
721 |
|
wenzelm@44112
|
722 |
\item The derived record operations @{text "t.make"}, @{text
|
wenzelm@44112
|
723 |
"t.fields"}, @{text "t.extend"}, @{text "t.truncate"} are \emph{not}
|
wenzelm@44112
|
724 |
treated automatically, but usually need to be expanded by hand,
|
wenzelm@44112
|
725 |
using the collective fact @{text "t.defs"}.
|
wenzelm@44112
|
726 |
|
wenzelm@44112
|
727 |
\end{enumerate}
|
wenzelm@44112
|
728 |
*}
|
wenzelm@44112
|
729 |
|
wenzelm@44112
|
730 |
|
wenzelm@44112
|
731 |
section {* Adhoc tuples *}
|
wenzelm@44112
|
732 |
|
wenzelm@44112
|
733 |
text {*
|
wenzelm@44112
|
734 |
\begin{matharray}{rcl}
|
wenzelm@44112
|
735 |
@{attribute_def (HOL) split_format}@{text "\<^sup>*"} & : & @{text attribute} \\
|
wenzelm@44112
|
736 |
\end{matharray}
|
wenzelm@44112
|
737 |
|
wenzelm@44112
|
738 |
@{rail "
|
wenzelm@44112
|
739 |
@@{attribute (HOL) split_format} ('(' 'complete' ')')?
|
wenzelm@44112
|
740 |
"}
|
wenzelm@44112
|
741 |
|
wenzelm@44112
|
742 |
\begin{description}
|
wenzelm@44112
|
743 |
|
wenzelm@44112
|
744 |
\item @{attribute (HOL) split_format}\ @{text "(complete)"} causes
|
wenzelm@44112
|
745 |
arguments in function applications to be represented canonically
|
wenzelm@44112
|
746 |
according to their tuple type structure.
|
wenzelm@44112
|
747 |
|
wenzelm@44112
|
748 |
Note that this operation tends to invent funny names for new local
|
wenzelm@44112
|
749 |
parameters introduced.
|
wenzelm@44112
|
750 |
|
wenzelm@44112
|
751 |
\end{description}
|
wenzelm@44112
|
752 |
*}
|
wenzelm@44112
|
753 |
|
wenzelm@44112
|
754 |
|
wenzelm@35757
|
755 |
section {* Typedef axiomatization \label{sec:hol-typedef} *}
|
wenzelm@26849
|
756 |
|
wenzelm@44111
|
757 |
text {* A Gordon/HOL-style type definition is a certain axiom scheme
|
wenzelm@44111
|
758 |
that identifies a new type with a subset of an existing type. More
|
wenzelm@44111
|
759 |
precisely, the new type is defined by exhibiting an existing type
|
wenzelm@44111
|
760 |
@{text \<tau>}, a set @{text "A :: \<tau> set"}, and a theorem that proves
|
wenzelm@44111
|
761 |
@{prop "\<exists>x. x \<in> A"}. Thus @{text A} is a non-empty subset of @{text
|
wenzelm@44111
|
762 |
\<tau>}, and the new type denotes this subset. New functions are
|
wenzelm@44111
|
763 |
postulated that establish an isomorphism between the new type and
|
wenzelm@44111
|
764 |
the subset. In general, the type @{text \<tau>} may involve type
|
wenzelm@44111
|
765 |
variables @{text "\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n"} which means that the type definition
|
wenzelm@44111
|
766 |
produces a type constructor @{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t"} depending on
|
wenzelm@44111
|
767 |
those type arguments.
|
wenzelm@44111
|
768 |
|
wenzelm@44111
|
769 |
The axiomatization can be considered a ``definition'' in the sense
|
wenzelm@44111
|
770 |
of the particular set-theoretic interpretation of HOL
|
wenzelm@44111
|
771 |
\cite{pitts93}, where the universe of types is required to be
|
wenzelm@44111
|
772 |
downwards-closed wrt.\ arbitrary non-empty subsets. Thus genuinely
|
wenzelm@44111
|
773 |
new types introduced by @{command "typedef"} stay within the range
|
wenzelm@44111
|
774 |
of HOL models by construction. Note that @{command_ref
|
wenzelm@44111
|
775 |
type_synonym} from Isabelle/Pure merely introduces syntactic
|
wenzelm@44111
|
776 |
abbreviations, without any logical significance.
|
wenzelm@44111
|
777 |
|
wenzelm@26849
|
778 |
\begin{matharray}{rcl}
|
wenzelm@35757
|
779 |
@{command_def (HOL) "typedef"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\
|
wenzelm@26849
|
780 |
\end{matharray}
|
wenzelm@26849
|
781 |
|
wenzelm@43467
|
782 |
@{rail "
|
wenzelm@44111
|
783 |
@@{command (HOL) typedef} alt_name? abs_type '=' rep_set
|
wenzelm@26849
|
784 |
;
|
wenzelm@26849
|
785 |
|
wenzelm@44111
|
786 |
alt_name: '(' (@{syntax name} | @'open' | @'open' @{syntax name}) ')'
|
wenzelm@26849
|
787 |
;
|
wenzelm@44111
|
788 |
abs_type: @{syntax typespec_sorts} @{syntax mixfix}?
|
wenzelm@26849
|
789 |
;
|
wenzelm@44111
|
790 |
rep_set: @{syntax term} (@'morphisms' @{syntax name} @{syntax name})?
|
wenzelm@43467
|
791 |
"}
|
wenzelm@26849
|
792 |
|
wenzelm@28760
|
793 |
\begin{description}
|
wenzelm@42994
|
794 |
|
wenzelm@35757
|
795 |
\item @{command (HOL) "typedef"}~@{text "(\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n) t = A"}
|
wenzelm@44111
|
796 |
axiomatizes a type definition in the background theory of the
|
wenzelm@44111
|
797 |
current context, depending on a non-emptiness result of the set
|
wenzelm@44111
|
798 |
@{text A} that needs to be proven here. The set @{text A} may
|
wenzelm@44111
|
799 |
contain type variables @{text "\<alpha>\<^sub>1, \<dots>, \<alpha>\<^sub>n"} as specified on the LHS,
|
wenzelm@44111
|
800 |
but no term variables.
|
wenzelm@35757
|
801 |
|
wenzelm@44111
|
802 |
Even though a local theory specification, the newly introduced type
|
wenzelm@44111
|
803 |
constructor cannot depend on parameters or assumptions of the
|
wenzelm@44111
|
804 |
context: this is structurally impossible in HOL. In contrast, the
|
wenzelm@44111
|
805 |
non-emptiness proof may use local assumptions in unusual situations,
|
wenzelm@44111
|
806 |
which could result in different interpretations in target contexts:
|
wenzelm@44111
|
807 |
the meaning of the bijection between the representing set @{text A}
|
wenzelm@44111
|
808 |
and the new type @{text t} may then change in different application
|
wenzelm@44111
|
809 |
contexts.
|
wenzelm@42994
|
810 |
|
wenzelm@44111
|
811 |
By default, @{command (HOL) "typedef"} defines both a type
|
wenzelm@44111
|
812 |
constructor @{text t} for the new type, and a term constant @{text
|
wenzelm@44111
|
813 |
t} for the representing set within the old type. Use the ``@{text
|
wenzelm@44111
|
814 |
"(open)"}'' option to suppress a separate constant definition
|
wenzelm@35757
|
815 |
altogether. The injection from type to set is called @{text Rep_t},
|
wenzelm@44111
|
816 |
its inverse @{text Abs_t}, unless explicit @{keyword (HOL)
|
wenzelm@44111
|
817 |
"morphisms"} specification provides alternative names.
|
wenzelm@42994
|
818 |
|
wenzelm@44111
|
819 |
The core axiomatization uses the locale predicate @{const
|
wenzelm@44111
|
820 |
type_definition} as defined in Isabelle/HOL. Various basic
|
wenzelm@44111
|
821 |
consequences of that are instantiated accordingly, re-using the
|
wenzelm@44111
|
822 |
locale facts with names derived from the new type constructor. Thus
|
wenzelm@44111
|
823 |
the generic @{thm type_definition.Rep} is turned into the specific
|
wenzelm@44111
|
824 |
@{text "Rep_t"}, for example.
|
wenzelm@44111
|
825 |
|
wenzelm@44111
|
826 |
Theorems @{thm type_definition.Rep}, @{thm
|
wenzelm@44111
|
827 |
type_definition.Rep_inverse}, and @{thm type_definition.Abs_inverse}
|
wenzelm@44111
|
828 |
provide the most basic characterization as a corresponding
|
wenzelm@44111
|
829 |
injection/surjection pair (in both directions). The derived rules
|
wenzelm@44111
|
830 |
@{thm type_definition.Rep_inject} and @{thm
|
wenzelm@44111
|
831 |
type_definition.Abs_inject} provide a more convenient version of
|
wenzelm@44111
|
832 |
injectivity, suitable for automated proof tools (e.g.\ in
|
wenzelm@44111
|
833 |
declarations involving @{attribute simp} or @{attribute iff}).
|
wenzelm@44111
|
834 |
Furthermore, the rules @{thm type_definition.Rep_cases}~/ @{thm
|
wenzelm@44111
|
835 |
type_definition.Rep_induct}, and @{thm type_definition.Abs_cases}~/
|
wenzelm@44111
|
836 |
@{thm type_definition.Abs_induct} provide alternative views on
|
wenzelm@44111
|
837 |
surjectivity. These rules are already declared as set or type rules
|
wenzelm@44111
|
838 |
for the generic @{method cases} and @{method induct} methods,
|
wenzelm@44111
|
839 |
respectively.
|
wenzelm@42994
|
840 |
|
wenzelm@35757
|
841 |
An alternative name for the set definition (and other derived
|
wenzelm@35757
|
842 |
entities) may be specified in parentheses; the default is to use
|
wenzelm@44111
|
843 |
@{text t} directly.
|
wenzelm@26849
|
844 |
|
wenzelm@28760
|
845 |
\end{description}
|
wenzelm@44111
|
846 |
|
wenzelm@44111
|
847 |
\begin{warn}
|
wenzelm@44111
|
848 |
If you introduce a new type axiomatically, i.e.\ via @{command_ref
|
wenzelm@44111
|
849 |
typedecl} and @{command_ref axiomatization}, the minimum requirement
|
wenzelm@44111
|
850 |
is that it has a non-empty model, to avoid immediate collapse of the
|
wenzelm@44111
|
851 |
HOL logic. Moreover, one needs to demonstrate that the
|
wenzelm@44111
|
852 |
interpretation of such free-form axiomatizations can coexist with
|
wenzelm@44111
|
853 |
that of the regular @{command_def typedef} scheme, and any extension
|
wenzelm@44111
|
854 |
that other people might have introduced elsewhere (e.g.\ in HOLCF
|
wenzelm@44111
|
855 |
\cite{MuellerNvOS99}).
|
wenzelm@44111
|
856 |
\end{warn}
|
wenzelm@26849
|
857 |
*}
|
wenzelm@26849
|
858 |
|
wenzelm@44111
|
859 |
subsubsection {* Examples *}
|
wenzelm@44111
|
860 |
|
wenzelm@44111
|
861 |
text {* Type definitions permit the introduction of abstract data
|
wenzelm@44111
|
862 |
types in a safe way, namely by providing models based on already
|
wenzelm@44111
|
863 |
existing types. Given some abstract axiomatic description @{text P}
|
wenzelm@44111
|
864 |
of a type, this involves two steps:
|
wenzelm@44111
|
865 |
|
wenzelm@44111
|
866 |
\begin{enumerate}
|
wenzelm@44111
|
867 |
|
wenzelm@44111
|
868 |
\item Find an appropriate type @{text \<tau>} and subset @{text A} which
|
wenzelm@44111
|
869 |
has the desired properties @{text P}, and make a type definition
|
wenzelm@44111
|
870 |
based on this representation.
|
wenzelm@44111
|
871 |
|
wenzelm@44111
|
872 |
\item Prove that @{text P} holds for @{text \<tau>} by lifting @{text P}
|
wenzelm@44111
|
873 |
from the representation.
|
wenzelm@44111
|
874 |
|
wenzelm@44111
|
875 |
\end{enumerate}
|
wenzelm@44111
|
876 |
|
wenzelm@44111
|
877 |
You can later forget about the representation and work solely in
|
wenzelm@44111
|
878 |
terms of the abstract properties @{text P}.
|
wenzelm@44111
|
879 |
|
wenzelm@44111
|
880 |
\medskip The following trivial example pulls a three-element type
|
wenzelm@44111
|
881 |
into existence within the formal logical environment of HOL. *}
|
wenzelm@44111
|
882 |
|
wenzelm@44111
|
883 |
typedef three = "{(True, True), (True, False), (False, True)}"
|
wenzelm@44111
|
884 |
by blast
|
wenzelm@44111
|
885 |
|
wenzelm@44111
|
886 |
definition "One = Abs_three (True, True)"
|
wenzelm@44111
|
887 |
definition "Two = Abs_three (True, False)"
|
wenzelm@44111
|
888 |
definition "Three = Abs_three (False, True)"
|
wenzelm@44111
|
889 |
|
wenzelm@44111
|
890 |
lemma three_distinct: "One \<noteq> Two" "One \<noteq> Three" "Two \<noteq> Three"
|
wenzelm@44111
|
891 |
by (simp_all add: One_def Two_def Three_def Abs_three_inject three_def)
|
wenzelm@44111
|
892 |
|
wenzelm@44111
|
893 |
lemma three_cases:
|
wenzelm@44111
|
894 |
fixes x :: three obtains "x = One" | "x = Two" | "x = Three"
|
wenzelm@44111
|
895 |
by (cases x) (auto simp: One_def Two_def Three_def Abs_three_inject three_def)
|
wenzelm@44111
|
896 |
|
wenzelm@44111
|
897 |
text {* Note that such trivial constructions are better done with
|
wenzelm@44111
|
898 |
derived specification mechanisms such as @{command datatype}: *}
|
wenzelm@44111
|
899 |
|
wenzelm@44111
|
900 |
datatype three' = One' | Two' | Three'
|
wenzelm@44111
|
901 |
|
wenzelm@44111
|
902 |
text {* This avoids re-doing basic definitions and proofs from the
|
wenzelm@44111
|
903 |
primitive @{command typedef} above. *}
|
wenzelm@44111
|
904 |
|
wenzelm@26849
|
905 |
|
haftmann@41644
|
906 |
section {* Functorial structure of types *}
|
haftmann@41644
|
907 |
|
haftmann@41644
|
908 |
text {*
|
haftmann@41644
|
909 |
\begin{matharray}{rcl}
|
haftmann@41752
|
910 |
@{command_def (HOL) "enriched_type"} & : & @{text "local_theory \<rightarrow> proof(prove)"}
|
haftmann@41644
|
911 |
\end{matharray}
|
haftmann@41644
|
912 |
|
wenzelm@43467
|
913 |
@{rail "
|
wenzelm@43488
|
914 |
@@{command (HOL) enriched_type} (@{syntax name} ':')? @{syntax term}
|
haftmann@41644
|
915 |
;
|
wenzelm@43488
|
916 |
"}
|
haftmann@41644
|
917 |
|
haftmann@41644
|
918 |
\begin{description}
|
haftmann@41644
|
919 |
|
wenzelm@43488
|
920 |
\item @{command (HOL) "enriched_type"}~@{text "prefix: m"} allows to
|
wenzelm@43488
|
921 |
prove and register properties about the functorial structure of type
|
wenzelm@43488
|
922 |
constructors. These properties then can be used by other packages
|
wenzelm@43488
|
923 |
to deal with those type constructors in certain type constructions.
|
wenzelm@43488
|
924 |
Characteristic theorems are noted in the current local theory. By
|
wenzelm@43488
|
925 |
default, they are prefixed with the base name of the type
|
wenzelm@43488
|
926 |
constructor, an explicit prefix can be given alternatively.
|
haftmann@41644
|
927 |
|
haftmann@41644
|
928 |
The given term @{text "m"} is considered as \emph{mapper} for the
|
haftmann@41644
|
929 |
corresponding type constructor and must conform to the following
|
haftmann@41644
|
930 |
type pattern:
|
haftmann@41644
|
931 |
|
haftmann@41644
|
932 |
\begin{matharray}{lll}
|
haftmann@41644
|
933 |
@{text "m"} & @{text "::"} &
|
haftmann@41644
|
934 |
@{text "\<sigma>\<^isub>1 \<Rightarrow> \<dots> \<sigma>\<^isub>k \<Rightarrow> (\<^vec>\<alpha>\<^isub>n) t \<Rightarrow> (\<^vec>\<beta>\<^isub>n) t"} \\
|
haftmann@41644
|
935 |
\end{matharray}
|
haftmann@41644
|
936 |
|
haftmann@41644
|
937 |
\noindent where @{text t} is the type constructor, @{text
|
haftmann@41644
|
938 |
"\<^vec>\<alpha>\<^isub>n"} and @{text "\<^vec>\<beta>\<^isub>n"} are distinct
|
haftmann@41644
|
939 |
type variables free in the local theory and @{text "\<sigma>\<^isub>1"},
|
haftmann@41644
|
940 |
\ldots, @{text "\<sigma>\<^isub>k"} is a subsequence of @{text "\<alpha>\<^isub>1 \<Rightarrow>
|
haftmann@41644
|
941 |
\<beta>\<^isub>1"}, @{text "\<beta>\<^isub>1 \<Rightarrow> \<alpha>\<^isub>1"}, \ldots,
|
haftmann@41644
|
942 |
@{text "\<alpha>\<^isub>n \<Rightarrow> \<beta>\<^isub>n"}, @{text "\<beta>\<^isub>n \<Rightarrow>
|
haftmann@41644
|
943 |
\<alpha>\<^isub>n"}.
|
haftmann@41644
|
944 |
|
haftmann@41644
|
945 |
\end{description}
|
haftmann@41644
|
946 |
*}
|
haftmann@41644
|
947 |
|
haftmann@41644
|
948 |
|
wenzelm@26849
|
949 |
section {* Arithmetic proof support *}
|
wenzelm@26849
|
950 |
|
wenzelm@26849
|
951 |
text {*
|
wenzelm@26849
|
952 |
\begin{matharray}{rcl}
|
wenzelm@28761
|
953 |
@{method_def (HOL) arith} & : & @{text method} \\
|
nipkow@30863
|
954 |
@{attribute_def (HOL) arith} & : & @{text attribute} \\
|
wenzelm@28761
|
955 |
@{attribute_def (HOL) arith_split} & : & @{text attribute} \\
|
wenzelm@26849
|
956 |
\end{matharray}
|
wenzelm@26849
|
957 |
|
wenzelm@26849
|
958 |
The @{method (HOL) arith} method decides linear arithmetic problems
|
wenzelm@26849
|
959 |
(on types @{text nat}, @{text int}, @{text real}). Any current
|
wenzelm@26849
|
960 |
facts are inserted into the goal before running the procedure.
|
wenzelm@26849
|
961 |
|
nipkow@30863
|
962 |
The @{attribute (HOL) arith} attribute declares facts that are
|
nipkow@30863
|
963 |
always supplied to the arithmetic provers implicitly.
|
nipkow@30863
|
964 |
|
wenzelm@26894
|
965 |
The @{attribute (HOL) arith_split} attribute declares case split
|
wenzelm@30865
|
966 |
rules to be expanded before @{method (HOL) arith} is invoked.
|
wenzelm@26849
|
967 |
|
nipkow@30863
|
968 |
Note that a simpler (but faster) arithmetic prover is
|
nipkow@30863
|
969 |
already invoked by the Simplifier.
|
wenzelm@26849
|
970 |
*}
|
wenzelm@26849
|
971 |
|
wenzelm@26849
|
972 |
|
wenzelm@30169
|
973 |
section {* Intuitionistic proof search *}
|
wenzelm@30169
|
974 |
|
wenzelm@30169
|
975 |
text {*
|
wenzelm@30169
|
976 |
\begin{matharray}{rcl}
|
wenzelm@30171
|
977 |
@{method_def (HOL) iprover} & : & @{text method} \\
|
wenzelm@30169
|
978 |
\end{matharray}
|
wenzelm@30169
|
979 |
|
wenzelm@43467
|
980 |
@{rail "
|
wenzelm@43467
|
981 |
@@{method (HOL) iprover} ( @{syntax rulemod} * )
|
wenzelm@43467
|
982 |
"}
|
wenzelm@30169
|
983 |
|
wenzelm@30171
|
984 |
The @{method (HOL) iprover} method performs intuitionistic proof
|
wenzelm@30171
|
985 |
search, depending on specifically declared rules from the context,
|
wenzelm@30171
|
986 |
or given as explicit arguments. Chained facts are inserted into the
|
wenzelm@35613
|
987 |
goal before commencing proof search.
|
wenzelm@35613
|
988 |
|
wenzelm@30169
|
989 |
Rules need to be classified as @{attribute (Pure) intro},
|
wenzelm@30169
|
990 |
@{attribute (Pure) elim}, or @{attribute (Pure) dest}; here the
|
wenzelm@30169
|
991 |
``@{text "!"}'' indicator refers to ``safe'' rules, which may be
|
wenzelm@30169
|
992 |
applied aggressively (without considering back-tracking later).
|
wenzelm@30169
|
993 |
Rules declared with ``@{text "?"}'' are ignored in proof search (the
|
wenzelm@43497
|
994 |
single-step @{method (Pure) rule} method still observes these). An
|
wenzelm@30169
|
995 |
explicit weight annotation may be given as well; otherwise the
|
wenzelm@30169
|
996 |
number of rule premises will be taken into account here.
|
wenzelm@30169
|
997 |
*}
|
wenzelm@30169
|
998 |
|
wenzelm@30169
|
999 |
|
wenzelm@30171
|
1000 |
section {* Coherent Logic *}
|
wenzelm@30171
|
1001 |
|
wenzelm@30171
|
1002 |
text {*
|
wenzelm@30171
|
1003 |
\begin{matharray}{rcl}
|
wenzelm@30171
|
1004 |
@{method_def (HOL) "coherent"} & : & @{text method} \\
|
wenzelm@30171
|
1005 |
\end{matharray}
|
wenzelm@30171
|
1006 |
|
wenzelm@43467
|
1007 |
@{rail "
|
wenzelm@43467
|
1008 |
@@{method (HOL) coherent} @{syntax thmrefs}?
|
wenzelm@43467
|
1009 |
"}
|
wenzelm@30171
|
1010 |
|
wenzelm@30171
|
1011 |
The @{method (HOL) coherent} method solves problems of
|
wenzelm@30171
|
1012 |
\emph{Coherent Logic} \cite{Bezem-Coquand:2005}, which covers
|
wenzelm@30171
|
1013 |
applications in confluence theory, lattice theory and projective
|
wenzelm@41048
|
1014 |
geometry. See @{file "~~/src/HOL/ex/Coherent.thy"} for some
|
wenzelm@30171
|
1015 |
examples.
|
wenzelm@30171
|
1016 |
*}
|
wenzelm@30171
|
1017 |
|
wenzelm@30171
|
1018 |
|
blanchet@43082
|
1019 |
section {* Proving propositions *}
|
blanchet@43082
|
1020 |
|
blanchet@43082
|
1021 |
text {*
|
blanchet@43082
|
1022 |
In addition to the standard proof methods, a number of diagnosis
|
blanchet@43082
|
1023 |
tools search for proofs and provide an Isar proof snippet on success.
|
blanchet@43082
|
1024 |
These tools are available via the following commands.
|
blanchet@43082
|
1025 |
|
blanchet@43082
|
1026 |
\begin{matharray}{rcl}
|
blanchet@43082
|
1027 |
@{command_def (HOL) "solve_direct"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
|
blanchet@43082
|
1028 |
@{command_def (HOL) "try"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
|
blanchet@43082
|
1029 |
@{command_def (HOL) "sledgehammer"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
|
blanchet@43082
|
1030 |
@{command_def (HOL) "sledgehammer_params"} & : & @{text "theory \<rightarrow> theory"}
|
blanchet@43082
|
1031 |
\end{matharray}
|
blanchet@43082
|
1032 |
|
wenzelm@43467
|
1033 |
@{rail "
|
wenzelm@43467
|
1034 |
@@{command (HOL) try} ( ( ( 'simp' | 'intro' | 'elim' | 'dest' ) ':' @{syntax thmrefs} ) + ) ?
|
wenzelm@43467
|
1035 |
@{syntax nat}?
|
wenzelm@43467
|
1036 |
;
|
wenzelm@43467
|
1037 |
@@{command (HOL) sledgehammer} ( '[' args ']' )? facts? @{syntax nat}?
|
blanchet@43082
|
1038 |
;
|
blanchet@43082
|
1039 |
|
wenzelm@43467
|
1040 |
@@{command (HOL) sledgehammer_params} ( ( '[' args ']' ) ? )
|
blanchet@43082
|
1041 |
;
|
blanchet@43082
|
1042 |
|
wenzelm@43467
|
1043 |
args: ( @{syntax name} '=' value + ',' )
|
blanchet@43082
|
1044 |
;
|
blanchet@43082
|
1045 |
|
wenzelm@43467
|
1046 |
facts: '(' ( ( ( ( 'add' | 'del' ) ':' ) ? @{syntax thmrefs} ) + ) ? ')'
|
blanchet@43082
|
1047 |
;
|
wenzelm@43467
|
1048 |
"} % FIXME try: proper clasimpmod!?
|
wenzelm@43467
|
1049 |
% FIXME check args "value"
|
blanchet@43082
|
1050 |
|
blanchet@43082
|
1051 |
\begin{description}
|
blanchet@43082
|
1052 |
|
blanchet@43082
|
1053 |
\item @{command (HOL) "solve_direct"} checks whether the current subgoals can
|
blanchet@43082
|
1054 |
be solved directly by an existing theorem. Duplicate lemmas can be detected
|
blanchet@43082
|
1055 |
in this way.
|
blanchet@43082
|
1056 |
|
blanchet@43082
|
1057 |
\item @{command (HOL) "try"} attempts to prove a subgoal using a combination
|
blanchet@43082
|
1058 |
of standard proof methods (@{text auto}, @{text simp}, @{text blast}, etc.).
|
blanchet@43082
|
1059 |
Additional facts supplied via @{text "simp:"}, @{text "intro:"},
|
blanchet@43082
|
1060 |
@{text "elim:"}, and @{text "dest:"} are passed to the appropriate proof
|
blanchet@43082
|
1061 |
methods.
|
blanchet@43082
|
1062 |
|
blanchet@43082
|
1063 |
\item @{command (HOL) "sledgehammer"} attempts to prove a subgoal using external
|
blanchet@43082
|
1064 |
automatic provers (resolution provers and SMT solvers). See the Sledgehammer
|
blanchet@43082
|
1065 |
manual \cite{isabelle-sledgehammer} for details.
|
blanchet@43082
|
1066 |
|
blanchet@43082
|
1067 |
\item @{command (HOL) "sledgehammer_params"} changes
|
blanchet@43082
|
1068 |
@{command (HOL) "sledgehammer"} configuration options persistently.
|
blanchet@43082
|
1069 |
|
blanchet@43082
|
1070 |
\end{description}
|
blanchet@43082
|
1071 |
*}
|
blanchet@43082
|
1072 |
|
blanchet@43082
|
1073 |
|
haftmann@31906
|
1074 |
section {* Checking and refuting propositions *}
|
haftmann@31906
|
1075 |
|
haftmann@31906
|
1076 |
text {*
|
haftmann@31906
|
1077 |
Identifying incorrect propositions usually involves evaluation of
|
blanchet@43082
|
1078 |
particular assignments and systematic counterexample search. This
|
haftmann@31906
|
1079 |
is supported by the following commands.
|
haftmann@31906
|
1080 |
|
haftmann@31906
|
1081 |
\begin{matharray}{rcl}
|
haftmann@31906
|
1082 |
@{command_def (HOL) "value"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
|
haftmann@31906
|
1083 |
@{command_def (HOL) "quickcheck"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
|
blanchet@43082
|
1084 |
@{command_def (HOL) "refute"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
|
blanchet@43082
|
1085 |
@{command_def (HOL) "nitpick"}@{text "\<^sup>*"} & : & @{text "proof \<rightarrow>"} \\
|
blanchet@43082
|
1086 |
@{command_def (HOL) "quickcheck_params"} & : & @{text "theory \<rightarrow> theory"} \\
|
blanchet@43082
|
1087 |
@{command_def (HOL) "refute_params"} & : & @{text "theory \<rightarrow> theory"} \\
|
blanchet@43082
|
1088 |
@{command_def (HOL) "nitpick_params"} & : & @{text "theory \<rightarrow> theory"}
|
haftmann@31906
|
1089 |
\end{matharray}
|
haftmann@31906
|
1090 |
|
wenzelm@43467
|
1091 |
@{rail "
|
wenzelm@43467
|
1092 |
@@{command (HOL) value} ( '[' name ']' )? modes? @{syntax term}
|
haftmann@31906
|
1093 |
;
|
haftmann@31906
|
1094 |
|
wenzelm@43467
|
1095 |
(@@{command (HOL) quickcheck} | @@{command (HOL) refute} | @@{command (HOL) nitpick})
|
wenzelm@43467
|
1096 |
( '[' args ']' )? @{syntax nat}?
|
haftmann@31906
|
1097 |
;
|
haftmann@31906
|
1098 |
|
wenzelm@43467
|
1099 |
(@@{command (HOL) quickcheck_params} | @@{command (HOL) refute_params} |
|
wenzelm@43467
|
1100 |
@@{command (HOL) nitpick_params}) ( '[' args ']' )?
|
haftmann@31906
|
1101 |
;
|
haftmann@31906
|
1102 |
|
wenzelm@43467
|
1103 |
modes: '(' (@{syntax name} +) ')'
|
haftmann@31906
|
1104 |
;
|
haftmann@31906
|
1105 |
|
wenzelm@43467
|
1106 |
args: ( @{syntax name} '=' value + ',' )
|
haftmann@31906
|
1107 |
;
|
wenzelm@43467
|
1108 |
"} % FIXME check "value"
|
haftmann@31906
|
1109 |
|
haftmann@31906
|
1110 |
\begin{description}
|
haftmann@31906
|
1111 |
|
haftmann@31906
|
1112 |
\item @{command (HOL) "value"}~@{text t} evaluates and prints a
|
haftmann@31906
|
1113 |
term; optionally @{text modes} can be specified, which are
|
haftmann@31906
|
1114 |
appended to the current print mode (see also \cite{isabelle-ref}).
|
haftmann@31906
|
1115 |
Internally, the evaluation is performed by registered evaluators,
|
haftmann@31906
|
1116 |
which are invoked sequentially until a result is returned.
|
haftmann@31906
|
1117 |
Alternatively a specific evaluator can be selected using square
|
haftmann@37419
|
1118 |
brackets; typical evaluators use the current set of code equations
|
haftmann@37419
|
1119 |
to normalize and include @{text simp} for fully symbolic evaluation
|
haftmann@37419
|
1120 |
using the simplifier, @{text nbe} for \emph{normalization by evaluation}
|
haftmann@37419
|
1121 |
and \emph{code} for code generation in SML.
|
haftmann@31906
|
1122 |
|
haftmann@31906
|
1123 |
\item @{command (HOL) "quickcheck"} tests the current goal for
|
blanchet@43082
|
1124 |
counterexamples using a series of assignments for its
|
haftmann@31906
|
1125 |
free variables; by default the first subgoal is tested, an other
|
haftmann@31906
|
1126 |
can be selected explicitly using an optional goal index.
|
bulwahn@41162
|
1127 |
Assignments can be chosen exhausting the search space upto a given
|
bulwahn@41162
|
1128 |
size or using a fixed number of random assignments in the search space.
|
bulwahn@41162
|
1129 |
By default, quickcheck uses exhaustive testing.
|
haftmann@31906
|
1130 |
A number of configuration options are supported for
|
haftmann@31906
|
1131 |
@{command (HOL) "quickcheck"}, notably:
|
haftmann@31906
|
1132 |
|
haftmann@31906
|
1133 |
\begin{description}
|
haftmann@31906
|
1134 |
|
bulwahn@41162
|
1135 |
\item[@{text tester}] specifies how to explore the search space
|
bulwahn@41162
|
1136 |
(e.g. exhaustive or random).
|
bulwahn@41162
|
1137 |
An unknown configuration option is treated as an argument to tester,
|
bulwahn@41162
|
1138 |
making @{text "tester ="} optional.
|
wenzelm@40515
|
1139 |
\item[@{text size}] specifies the maximum size of the search space
|
wenzelm@40515
|
1140 |
for assignment values.
|
haftmann@31906
|
1141 |
|
bulwahn@42956
|
1142 |
\item[@{text eval}] takes a term or a list of terms and evaluates
|
bulwahn@42956
|
1143 |
these terms under the variable assignment found by quickcheck.
|
wenzelm@42994
|
1144 |
|
wenzelm@40515
|
1145 |
\item[@{text iterations}] sets how many sets of assignments are
|
wenzelm@40515
|
1146 |
generated for each particular size.
|
haftmann@31906
|
1147 |
|
wenzelm@40515
|
1148 |
\item[@{text no_assms}] specifies whether assumptions in
|
wenzelm@40515
|
1149 |
structured proofs should be ignored.
|
blanchet@35331
|
1150 |
|
wenzelm@40515
|
1151 |
\item[@{text timeout}] sets the time limit in seconds.
|
bulwahn@40480
|
1152 |
|
wenzelm@40515
|
1153 |
\item[@{text default_type}] sets the type(s) generally used to
|
wenzelm@40515
|
1154 |
instantiate type variables.
|
bulwahn@40480
|
1155 |
|
wenzelm@40515
|
1156 |
\item[@{text report}] if set quickcheck reports how many tests
|
wenzelm@40515
|
1157 |
fulfilled the preconditions.
|
bulwahn@40480
|
1158 |
|
wenzelm@40515
|
1159 |
\item[@{text quiet}] if not set quickcheck informs about the
|
wenzelm@40515
|
1160 |
current size for assignment values.
|
bulwahn@40480
|
1161 |
|
wenzelm@40515
|
1162 |
\item[@{text expect}] can be used to check if the user's
|
wenzelm@40515
|
1163 |
expectation was met (@{text no_expectation}, @{text
|
wenzelm@40515
|
1164 |
no_counterexample}, or @{text counterexample}).
|
bulwahn@40480
|
1165 |
|
haftmann@31906
|
1166 |
\end{description}
|
haftmann@31906
|
1167 |
|
haftmann@31906
|
1168 |
These option can be given within square brackets.
|
haftmann@31906
|
1169 |
|
blanchet@43082
|
1170 |
\item @{command (HOL) "quickcheck_params"} changes
|
blanchet@43082
|
1171 |
@{command (HOL) "quickcheck"} configuration options persistently.
|
blanchet@43082
|
1172 |
|
blanchet@43082
|
1173 |
\item @{command (HOL) "refute"} tests the current goal for
|
blanchet@43082
|
1174 |
counterexamples using a reduction to SAT. The following configuration
|
blanchet@43082
|
1175 |
options are supported:
|
blanchet@43082
|
1176 |
|
blanchet@43082
|
1177 |
\begin{description}
|
blanchet@43082
|
1178 |
|
blanchet@43082
|
1179 |
\item[@{text minsize}] specifies the minimum size (cardinality) of the
|
blanchet@43082
|
1180 |
models to search for.
|
blanchet@43082
|
1181 |
|
blanchet@43082
|
1182 |
\item[@{text maxsize}] specifies the maximum size (cardinality) of the
|
blanchet@43082
|
1183 |
models to search for. Nonpositive values mean $\infty$.
|
blanchet@43082
|
1184 |
|
blanchet@43082
|
1185 |
\item[@{text maxvars}] specifies the maximum number of Boolean variables
|
blanchet@43082
|
1186 |
to use when transforming the term into a propositional formula.
|
blanchet@43082
|
1187 |
Nonpositive values mean $\infty$.
|
blanchet@43082
|
1188 |
|
blanchet@43082
|
1189 |
\item[@{text satsolver}] specifies the SAT solver to use.
|
blanchet@43082
|
1190 |
|
blanchet@43082
|
1191 |
\item[@{text no_assms}] specifies whether assumptions in
|
blanchet@43082
|
1192 |
structured proofs should be ignored.
|
blanchet@43082
|
1193 |
|
blanchet@43082
|
1194 |
\item[@{text maxtime}] sets the time limit in seconds.
|
blanchet@43082
|
1195 |
|
blanchet@43082
|
1196 |
\item[@{text expect}] can be used to check if the user's
|
blanchet@43082
|
1197 |
expectation was met (@{text genuine}, @{text potential},
|
blanchet@43082
|
1198 |
@{text none}, or @{text unknown}).
|
blanchet@43082
|
1199 |
|
blanchet@43082
|
1200 |
\end{description}
|
blanchet@43082
|
1201 |
|
blanchet@43082
|
1202 |
These option can be given within square brackets.
|
blanchet@43082
|
1203 |
|
blanchet@43082
|
1204 |
\item @{command (HOL) "refute_params"} changes
|
blanchet@43082
|
1205 |
@{command (HOL) "refute"} configuration options persistently.
|
blanchet@43082
|
1206 |
|
blanchet@43082
|
1207 |
\item @{command (HOL) "nitpick"} tests the current goal for counterexamples
|
blanchet@43082
|
1208 |
using a reduction to first-order relational logic. See the Nitpick manual
|
blanchet@43082
|
1209 |
\cite{isabelle-nitpick} for details.
|
blanchet@43082
|
1210 |
|
blanchet@43082
|
1211 |
\item @{command (HOL) "nitpick_params"} changes
|
blanchet@43082
|
1212 |
@{command (HOL) "nitpick"} configuration options persistently.
|
haftmann@31906
|
1213 |
|
haftmann@31906
|
1214 |
\end{description}
|
haftmann@31906
|
1215 |
*}
|
haftmann@31906
|
1216 |
|
haftmann@31906
|
1217 |
|
wenzelm@28752
|
1218 |
section {* Unstructured case analysis and induction \label{sec:hol-induct-tac} *}
|
wenzelm@26849
|
1219 |
|
wenzelm@26849
|
1220 |
text {*
|
wenzelm@27123
|
1221 |
The following tools of Isabelle/HOL support cases analysis and
|
wenzelm@27123
|
1222 |
induction in unstructured tactic scripts; see also
|
wenzelm@27123
|
1223 |
\secref{sec:cases-induct} for proper Isar versions of similar ideas.
|
wenzelm@26849
|
1224 |
|
wenzelm@26849
|
1225 |
\begin{matharray}{rcl}
|
wenzelm@28761
|
1226 |
@{method_def (HOL) case_tac}@{text "\<^sup>*"} & : & @{text method} \\
|
wenzelm@28761
|
1227 |
@{method_def (HOL) induct_tac}@{text "\<^sup>*"} & : & @{text method} \\
|
wenzelm@28761
|
1228 |
@{method_def (HOL) ind_cases}@{text "\<^sup>*"} & : & @{text method} \\
|
wenzelm@28761
|
1229 |
@{command_def (HOL) "inductive_cases"}@{text "\<^sup>*"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
|
wenzelm@26849
|
1230 |
\end{matharray}
|
wenzelm@26849
|
1231 |
|
wenzelm@43467
|
1232 |
@{rail "
|
wenzelm@43576
|
1233 |
@@{method (HOL) case_tac} @{syntax goal_spec}? @{syntax term} rule?
|
wenzelm@26849
|
1234 |
;
|
wenzelm@43576
|
1235 |
@@{method (HOL) induct_tac} @{syntax goal_spec}? (@{syntax insts} * @'and') rule?
|
wenzelm@26849
|
1236 |
;
|
wenzelm@43467
|
1237 |
@@{method (HOL) ind_cases} (@{syntax prop}+) (@'for' (@{syntax name}+))?
|
wenzelm@26849
|
1238 |
;
|
wenzelm@43467
|
1239 |
@@{command (HOL) inductive_cases} (@{syntax thmdecl}? (@{syntax prop}+) + @'and')
|
wenzelm@26849
|
1240 |
;
|
wenzelm@26849
|
1241 |
|
wenzelm@43467
|
1242 |
rule: 'rule' ':' @{syntax thmref}
|
wenzelm@43467
|
1243 |
"}
|
wenzelm@26849
|
1244 |
|
wenzelm@28760
|
1245 |
\begin{description}
|
wenzelm@26849
|
1246 |
|
wenzelm@28760
|
1247 |
\item @{method (HOL) case_tac} and @{method (HOL) induct_tac} admit
|
wenzelm@28760
|
1248 |
to reason about inductive types. Rules are selected according to
|
wenzelm@28760
|
1249 |
the declarations by the @{attribute cases} and @{attribute induct}
|
wenzelm@28760
|
1250 |
attributes, cf.\ \secref{sec:cases-induct}. The @{command (HOL)
|
wenzelm@28760
|
1251 |
datatype} package already takes care of this.
|
wenzelm@27123
|
1252 |
|
wenzelm@27123
|
1253 |
These unstructured tactics feature both goal addressing and dynamic
|
wenzelm@26849
|
1254 |
instantiation. Note that named rule cases are \emph{not} provided
|
wenzelm@27123
|
1255 |
as would be by the proper @{method cases} and @{method induct} proof
|
wenzelm@27123
|
1256 |
methods (see \secref{sec:cases-induct}). Unlike the @{method
|
wenzelm@27123
|
1257 |
induct} method, @{method induct_tac} does not handle structured rule
|
wenzelm@27123
|
1258 |
statements, only the compact object-logic conclusion of the subgoal
|
wenzelm@27123
|
1259 |
being addressed.
|
wenzelm@42994
|
1260 |
|
wenzelm@28760
|
1261 |
\item @{method (HOL) ind_cases} and @{command (HOL)
|
wenzelm@28760
|
1262 |
"inductive_cases"} provide an interface to the internal @{ML_text
|
wenzelm@26860
|
1263 |
mk_cases} operation. Rules are simplified in an unrestricted
|
wenzelm@26860
|
1264 |
forward manner.
|
wenzelm@26849
|
1265 |
|
wenzelm@26849
|
1266 |
While @{method (HOL) ind_cases} is a proof method to apply the
|
wenzelm@26849
|
1267 |
result immediately as elimination rules, @{command (HOL)
|
wenzelm@26849
|
1268 |
"inductive_cases"} provides case split theorems at the theory level
|
wenzelm@26849
|
1269 |
for later use. The @{keyword "for"} argument of the @{method (HOL)
|
wenzelm@26849
|
1270 |
ind_cases} method allows to specify a list of variables that should
|
wenzelm@26849
|
1271 |
be generalized before applying the resulting rule.
|
wenzelm@26849
|
1272 |
|
wenzelm@28760
|
1273 |
\end{description}
|
wenzelm@26849
|
1274 |
*}
|
wenzelm@26849
|
1275 |
|
wenzelm@26849
|
1276 |
|
wenzelm@26849
|
1277 |
section {* Executable code *}
|
wenzelm@26849
|
1278 |
|
wenzelm@43498
|
1279 |
text {* For validation purposes, it is often useful to \emph{execute}
|
wenzelm@43498
|
1280 |
specifications. In principle, execution could be simulated by
|
wenzelm@43498
|
1281 |
Isabelle's inference kernel, i.e. by a combination of resolution and
|
wenzelm@43498
|
1282 |
simplification. Unfortunately, this approach is rather inefficient.
|
wenzelm@43498
|
1283 |
A more efficient way of executing specifications is to translate
|
wenzelm@43498
|
1284 |
them into a functional programming language such as ML.
|
wenzelm@26849
|
1285 |
|
wenzelm@43498
|
1286 |
Isabelle provides two generic frameworks to support code generation
|
wenzelm@43498
|
1287 |
from executable specifications. Isabelle/HOL instantiates these
|
wenzelm@43498
|
1288 |
mechanisms in a way that is amenable to end-user applications.
|
wenzelm@43498
|
1289 |
*}
|
wenzelm@43498
|
1290 |
|
wenzelm@43498
|
1291 |
|
wenzelm@43498
|
1292 |
subsection {* The new code generator (F. Haftmann) *}
|
wenzelm@43498
|
1293 |
|
wenzelm@43498
|
1294 |
text {* This framework generates code from functional programs
|
haftmann@37397
|
1295 |
(including overloading using type classes) to SML \cite{SML}, OCaml
|
haftmann@39049
|
1296 |
\cite{OCaml}, Haskell \cite{haskell-revised-report} and Scala
|
wenzelm@43498
|
1297 |
\cite{scala-overview-tech-report}. Conceptually, code generation is
|
wenzelm@43498
|
1298 |
split up in three steps: \emph{selection} of code theorems,
|
wenzelm@43498
|
1299 |
\emph{translation} into an abstract executable view and
|
wenzelm@43498
|
1300 |
\emph{serialization} to a specific \emph{target language}.
|
wenzelm@43498
|
1301 |
Inductive specifications can be executed using the predicate
|
wenzelm@43498
|
1302 |
compiler which operates within HOL. See \cite{isabelle-codegen} for
|
wenzelm@43498
|
1303 |
an introduction.
|
haftmann@37397
|
1304 |
|
haftmann@37397
|
1305 |
\begin{matharray}{rcl}
|
haftmann@37397
|
1306 |
@{command_def (HOL) "export_code"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
|
haftmann@37397
|
1307 |
@{attribute_def (HOL) code} & : & @{text attribute} \\
|
haftmann@37397
|
1308 |
@{command_def (HOL) "code_abort"} & : & @{text "theory \<rightarrow> theory"} \\
|
haftmann@37397
|
1309 |
@{command_def (HOL) "code_datatype"} & : & @{text "theory \<rightarrow> theory"} \\
|
haftmann@37397
|
1310 |
@{command_def (HOL) "print_codesetup"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
|
haftmann@37397
|
1311 |
@{attribute_def (HOL) code_inline} & : & @{text attribute} \\
|
haftmann@37397
|
1312 |
@{attribute_def (HOL) code_post} & : & @{text attribute} \\
|
haftmann@37397
|
1313 |
@{command_def (HOL) "print_codeproc"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
|
haftmann@37397
|
1314 |
@{command_def (HOL) "code_thms"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
|
haftmann@37397
|
1315 |
@{command_def (HOL) "code_deps"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
|
haftmann@37397
|
1316 |
@{command_def (HOL) "code_const"} & : & @{text "theory \<rightarrow> theory"} \\
|
haftmann@37397
|
1317 |
@{command_def (HOL) "code_type"} & : & @{text "theory \<rightarrow> theory"} \\
|
haftmann@37397
|
1318 |
@{command_def (HOL) "code_class"} & : & @{text "theory \<rightarrow> theory"} \\
|
haftmann@37397
|
1319 |
@{command_def (HOL) "code_instance"} & : & @{text "theory \<rightarrow> theory"} \\
|
haftmann@37397
|
1320 |
@{command_def (HOL) "code_reserved"} & : & @{text "theory \<rightarrow> theory"} \\
|
haftmann@37397
|
1321 |
@{command_def (HOL) "code_monad"} & : & @{text "theory \<rightarrow> theory"} \\
|
haftmann@37397
|
1322 |
@{command_def (HOL) "code_include"} & : & @{text "theory \<rightarrow> theory"} \\
|
haftmann@37397
|
1323 |
@{command_def (HOL) "code_modulename"} & : & @{text "theory \<rightarrow> theory"} \\
|
haftmann@39832
|
1324 |
@{command_def (HOL) "code_reflect"} & : & @{text "theory \<rightarrow> theory"}
|
haftmann@37397
|
1325 |
\end{matharray}
|
haftmann@37397
|
1326 |
|
wenzelm@43467
|
1327 |
@{rail "
|
wenzelm@43467
|
1328 |
@@{command (HOL) export_code} ( constexpr + ) \\
|
wenzelm@43467
|
1329 |
( ( @'in' target ( @'module_name' @{syntax string} ) ? \\
|
wenzelm@43467
|
1330 |
( @'file' ( @{syntax string} | '-' ) ) ? ( '(' args ')' ) ?) + ) ?
|
haftmann@37397
|
1331 |
;
|
haftmann@37397
|
1332 |
|
wenzelm@43467
|
1333 |
const: @{syntax term}
|
haftmann@37397
|
1334 |
;
|
haftmann@37397
|
1335 |
|
haftmann@40959
|
1336 |
constexpr: ( const | 'name._' | '_' )
|
haftmann@37397
|
1337 |
;
|
haftmann@37397
|
1338 |
|
wenzelm@43467
|
1339 |
typeconstructor: @{syntax nameref}
|
haftmann@37397
|
1340 |
;
|
haftmann@37397
|
1341 |
|
wenzelm@43467
|
1342 |
class: @{syntax nameref}
|
haftmann@37397
|
1343 |
;
|
haftmann@37397
|
1344 |
|
haftmann@39049
|
1345 |
target: 'SML' | 'OCaml' | 'Haskell' | 'Scala'
|
haftmann@37397
|
1346 |
;
|
haftmann@37397
|
1347 |
|
wenzelm@43467
|
1348 |
@@{attribute (HOL) code} ( 'del' | 'abstype' | 'abstract' )?
|
haftmann@37397
|
1349 |
;
|
haftmann@37397
|
1350 |
|
wenzelm@43467
|
1351 |
@@{command (HOL) code_abort} ( const + )
|
haftmann@37397
|
1352 |
;
|
haftmann@37397
|
1353 |
|
wenzelm@43467
|
1354 |
@@{command (HOL) code_datatype} ( const + )
|
haftmann@37397
|
1355 |
;
|
haftmann@37397
|
1356 |
|
wenzelm@43467
|
1357 |
@@{attribute (HOL) code_inline} ( 'del' ) ?
|
haftmann@37397
|
1358 |
;
|
haftmann@37397
|
1359 |
|
wenzelm@43467
|
1360 |
@@{attribute (HOL) code_post} ( 'del' ) ?
|
haftmann@37397
|
1361 |
;
|
haftmann@37397
|
1362 |
|
wenzelm@43467
|
1363 |
@@{command (HOL) code_thms} ( constexpr + ) ?
|
haftmann@37397
|
1364 |
;
|
haftmann@37397
|
1365 |
|
wenzelm@43467
|
1366 |
@@{command (HOL) code_deps} ( constexpr + ) ?
|
haftmann@37397
|
1367 |
;
|
haftmann@37397
|
1368 |
|
wenzelm@43467
|
1369 |
@@{command (HOL) code_const} (const + @'and') \\
|
wenzelm@43467
|
1370 |
( ( '(' target ( syntax ? + @'and' ) ')' ) + )
|
haftmann@37397
|
1371 |
;
|
haftmann@37397
|
1372 |
|
wenzelm@43467
|
1373 |
@@{command (HOL) code_type} (typeconstructor + @'and') \\
|
wenzelm@43467
|
1374 |
( ( '(' target ( syntax ? + @'and' ) ')' ) + )
|
haftmann@37397
|
1375 |
;
|
haftmann@37397
|
1376 |
|
wenzelm@43467
|
1377 |
@@{command (HOL) code_class} (class + @'and') \\
|
wenzelm@43467
|
1378 |
( ( '(' target \\ ( @{syntax string} ? + @'and' ) ')' ) + )
|
haftmann@37397
|
1379 |
;
|
haftmann@37397
|
1380 |
|
wenzelm@43467
|
1381 |
@@{command (HOL) code_instance} (( typeconstructor '::' class ) + @'and') \\
|
wenzelm@43467
|
1382 |
( ( '(' target ( '-' ? + @'and' ) ')' ) + )
|
haftmann@37397
|
1383 |
;
|
haftmann@37397
|
1384 |
|
wenzelm@43467
|
1385 |
@@{command (HOL) code_reserved} target ( @{syntax string} + )
|
haftmann@37397
|
1386 |
;
|
haftmann@37397
|
1387 |
|
wenzelm@43467
|
1388 |
@@{command (HOL) code_monad} const const target
|
haftmann@37397
|
1389 |
;
|
haftmann@37397
|
1390 |
|
wenzelm@43467
|
1391 |
@@{command (HOL) code_include} target ( @{syntax string} ( @{syntax string} | '-') )
|
haftmann@37397
|
1392 |
;
|
haftmann@37397
|
1393 |
|
wenzelm@43467
|
1394 |
@@{command (HOL) code_modulename} target ( ( @{syntax string} @{syntax string} ) + )
|
haftmann@37397
|
1395 |
;
|
haftmann@37397
|
1396 |
|
wenzelm@43467
|
1397 |
@@{command (HOL) code_reflect} @{syntax string} \\
|
wenzelm@43467
|
1398 |
( @'datatypes' ( @{syntax string} '=' ( '_' | ( @{syntax string} + '|' ) + @'and' ) ) ) ? \\
|
wenzelm@43467
|
1399 |
( @'functions' ( @{syntax string} + ) ) ? ( @'file' @{syntax string} ) ?
|
haftmann@39832
|
1400 |
;
|
haftmann@39832
|
1401 |
|
wenzelm@43467
|
1402 |
syntax: @{syntax string} | ( @'infix' | @'infixl' | @'infixr' ) @{syntax nat} @{syntax string}
|
wenzelm@43467
|
1403 |
"}
|
haftmann@37397
|
1404 |
|
haftmann@37397
|
1405 |
\begin{description}
|
haftmann@37397
|
1406 |
|
haftmann@37397
|
1407 |
\item @{command (HOL) "export_code"} generates code for a given list
|
haftmann@39832
|
1408 |
of constants in the specified target language(s). If no
|
haftmann@39832
|
1409 |
serialization instruction is given, only abstract code is generated
|
haftmann@39832
|
1410 |
internally.
|
haftmann@37397
|
1411 |
|
haftmann@37397
|
1412 |
Constants may be specified by giving them literally, referring to
|
haftmann@37397
|
1413 |
all executable contants within a certain theory by giving @{text
|
haftmann@37397
|
1414 |
"name.*"}, or referring to \emph{all} executable constants currently
|
haftmann@37397
|
1415 |
available by giving @{text "*"}.
|
haftmann@37397
|
1416 |
|
haftmann@37397
|
1417 |
By default, for each involved theory one corresponding name space
|
haftmann@37397
|
1418 |
module is generated. Alternativly, a module name may be specified
|
haftmann@37397
|
1419 |
after the @{keyword "module_name"} keyword; then \emph{all} code is
|
haftmann@37397
|
1420 |
placed in this module.
|
haftmann@37397
|
1421 |
|
haftmann@39832
|
1422 |
For \emph{SML}, \emph{OCaml} and \emph{Scala} the file specification
|
haftmann@39832
|
1423 |
refers to a single file; for \emph{Haskell}, it refers to a whole
|
haftmann@39832
|
1424 |
directory, where code is generated in multiple files reflecting the
|
haftmann@39832
|
1425 |
module hierarchy. Omitting the file specification denotes standard
|
haftmann@37748
|
1426 |
output.
|
haftmann@37397
|
1427 |
|
haftmann@37397
|
1428 |
Serializers take an optional list of arguments in parentheses. For
|
haftmann@37397
|
1429 |
\emph{SML} and \emph{OCaml}, ``@{text no_signatures}`` omits
|
haftmann@37397
|
1430 |
explicit module signatures.
|
wenzelm@42994
|
1431 |
|
haftmann@39832
|
1432 |
For \emph{Haskell} a module name prefix may be given using the
|
haftmann@39832
|
1433 |
``@{text "root:"}'' argument; ``@{text string_classes}'' adds a
|
haftmann@39832
|
1434 |
``@{verbatim "deriving (Read, Show)"}'' clause to each appropriate
|
haftmann@39832
|
1435 |
datatype declaration.
|
haftmann@37397
|
1436 |
|
haftmann@37397
|
1437 |
\item @{attribute (HOL) code} explicitly selects (or with option
|
haftmann@38706
|
1438 |
``@{text "del"}'' deselects) a code equation for code generation.
|
haftmann@38706
|
1439 |
Usually packages introducing code equations provide a reasonable
|
haftmann@38706
|
1440 |
default setup for selection. Variants @{text "code abstype"} and
|
haftmann@38706
|
1441 |
@{text "code abstract"} declare abstract datatype certificates or
|
haftmann@38706
|
1442 |
code equations on abstract datatype representations respectively.
|
haftmann@37397
|
1443 |
|
haftmann@37397
|
1444 |
\item @{command (HOL) "code_abort"} declares constants which are not
|
haftmann@39832
|
1445 |
required to have a definition by means of code equations; if needed
|
haftmann@39832
|
1446 |
these are implemented by program abort instead.
|
haftmann@37397
|
1447 |
|
haftmann@37397
|
1448 |
\item @{command (HOL) "code_datatype"} specifies a constructor set
|
haftmann@37397
|
1449 |
for a logical type.
|
haftmann@37397
|
1450 |
|
haftmann@37397
|
1451 |
\item @{command (HOL) "print_codesetup"} gives an overview on
|
haftmann@37397
|
1452 |
selected code equations and code generator datatypes.
|
haftmann@37397
|
1453 |
|
haftmann@39832
|
1454 |
\item @{attribute (HOL) code_inline} declares (or with option
|
haftmann@39832
|
1455 |
``@{text "del"}'' removes) inlining theorems which are applied as
|
haftmann@39832
|
1456 |
rewrite rules to any code equation during preprocessing.
|
haftmann@37397
|
1457 |
|
haftmann@39832
|
1458 |
\item @{attribute (HOL) code_post} declares (or with option ``@{text
|
haftmann@39832
|
1459 |
"del"}'' removes) theorems which are applied as rewrite rules to any
|
haftmann@39832
|
1460 |
result of an evaluation.
|
haftmann@37397
|
1461 |
|
haftmann@39832
|
1462 |
\item @{command (HOL) "print_codeproc"} prints the setup of the code
|
haftmann@39832
|
1463 |
generator preprocessor.
|
haftmann@37397
|
1464 |
|
haftmann@37397
|
1465 |
\item @{command (HOL) "code_thms"} prints a list of theorems
|
haftmann@37397
|
1466 |
representing the corresponding program containing all given
|
haftmann@37397
|
1467 |
constants after preprocessing.
|
haftmann@37397
|
1468 |
|
haftmann@37397
|
1469 |
\item @{command (HOL) "code_deps"} visualizes dependencies of
|
haftmann@37397
|
1470 |
theorems representing the corresponding program containing all given
|
haftmann@37397
|
1471 |
constants after preprocessing.
|
haftmann@37397
|
1472 |
|
haftmann@37397
|
1473 |
\item @{command (HOL) "code_const"} associates a list of constants
|
haftmann@37397
|
1474 |
with target-specific serializations; omitting a serialization
|
haftmann@37397
|
1475 |
deletes an existing serialization.
|
haftmann@37397
|
1476 |
|
haftmann@37397
|
1477 |
\item @{command (HOL) "code_type"} associates a list of type
|
haftmann@37397
|
1478 |
constructors with target-specific serializations; omitting a
|
haftmann@37397
|
1479 |
serialization deletes an existing serialization.
|
haftmann@37397
|
1480 |
|
haftmann@37397
|
1481 |
\item @{command (HOL) "code_class"} associates a list of classes
|
haftmann@37397
|
1482 |
with target-specific class names; omitting a serialization deletes
|
haftmann@37397
|
1483 |
an existing serialization. This applies only to \emph{Haskell}.
|
haftmann@37397
|
1484 |
|
haftmann@37397
|
1485 |
\item @{command (HOL) "code_instance"} declares a list of type
|
haftmann@37397
|
1486 |
constructor / class instance relations as ``already present'' for a
|
haftmann@37397
|
1487 |
given target. Omitting a ``@{text "-"}'' deletes an existing
|
haftmann@37397
|
1488 |
``already present'' declaration. This applies only to
|
haftmann@37397
|
1489 |
\emph{Haskell}.
|
haftmann@37397
|
1490 |
|
haftmann@37397
|
1491 |
\item @{command (HOL) "code_reserved"} declares a list of names as
|
haftmann@37397
|
1492 |
reserved for a given target, preventing it to be shadowed by any
|
haftmann@37397
|
1493 |
generated code.
|
haftmann@37397
|
1494 |
|
haftmann@37397
|
1495 |
\item @{command (HOL) "code_monad"} provides an auxiliary mechanism
|
haftmann@37397
|
1496 |
to generate monadic code for Haskell.
|
haftmann@37397
|
1497 |
|
haftmann@37397
|
1498 |
\item @{command (HOL) "code_include"} adds arbitrary named content
|
haftmann@37397
|
1499 |
(``include'') to generated code. A ``@{text "-"}'' as last argument
|
haftmann@37397
|
1500 |
will remove an already added ``include''.
|
haftmann@37397
|
1501 |
|
haftmann@37397
|
1502 |
\item @{command (HOL) "code_modulename"} declares aliasings from one
|
haftmann@37397
|
1503 |
module name onto another.
|
haftmann@37397
|
1504 |
|
haftmann@39832
|
1505 |
\item @{command (HOL) "code_reflect"} without a ``@{text "file"}''
|
haftmann@39832
|
1506 |
argument compiles code into the system runtime environment and
|
haftmann@39832
|
1507 |
modifies the code generator setup that future invocations of system
|
haftmann@39832
|
1508 |
runtime code generation referring to one of the ``@{text
|
haftmann@39832
|
1509 |
"datatypes"}'' or ``@{text "functions"}'' entities use these precompiled
|
haftmann@39832
|
1510 |
entities. With a ``@{text "file"}'' argument, the corresponding code
|
haftmann@39832
|
1511 |
is generated into that specified file without modifying the code
|
haftmann@39832
|
1512 |
generator setup.
|
haftmann@39832
|
1513 |
|
haftmann@37397
|
1514 |
\end{description}
|
wenzelm@43498
|
1515 |
*}
|
haftmann@37397
|
1516 |
|
wenzelm@43498
|
1517 |
|
wenzelm@43498
|
1518 |
subsection {* The old code generator (S. Berghofer) *}
|
wenzelm@43498
|
1519 |
|
wenzelm@43498
|
1520 |
text {* This framework generates code from both functional and
|
wenzelm@43498
|
1521 |
relational programs to SML, as explained below.
|
wenzelm@26849
|
1522 |
|
wenzelm@26849
|
1523 |
\begin{matharray}{rcl}
|
wenzelm@43498
|
1524 |
@{command_def "code_module"} & : & @{text "theory \<rightarrow> theory"} \\
|
wenzelm@43498
|
1525 |
@{command_def "code_library"} & : & @{text "theory \<rightarrow> theory"} \\
|
wenzelm@43498
|
1526 |
@{command_def "consts_code"} & : & @{text "theory \<rightarrow> theory"} \\
|
wenzelm@43498
|
1527 |
@{command_def "types_code"} & : & @{text "theory \<rightarrow> theory"} \\
|
wenzelm@43497
|
1528 |
@{attribute_def code} & : & @{text attribute} \\
|
wenzelm@26849
|
1529 |
\end{matharray}
|
wenzelm@26849
|
1530 |
|
wenzelm@43467
|
1531 |
@{rail "
|
wenzelm@43498
|
1532 |
( @@{command code_module} | @@{command code_library} ) modespec? @{syntax name}? \\
|
wenzelm@43467
|
1533 |
( @'file' name ) ? ( @'imports' ( @{syntax name} + ) ) ? \\
|
wenzelm@43467
|
1534 |
@'contains' ( ( @{syntax name} '=' @{syntax term} ) + | @{syntax term} + )
|
wenzelm@26849
|
1535 |
;
|
wenzelm@26849
|
1536 |
|
wenzelm@43467
|
1537 |
modespec: '(' ( @{syntax name} * ) ')'
|
wenzelm@26849
|
1538 |
;
|
wenzelm@26849
|
1539 |
|
wenzelm@43467
|
1540 |
@@{command (HOL) consts_code} (codespec +)
|
wenzelm@26849
|
1541 |
;
|
wenzelm@26849
|
1542 |
|
wenzelm@26849
|
1543 |
codespec: const template attachment ?
|
wenzelm@26849
|
1544 |
;
|
wenzelm@26849
|
1545 |
|
wenzelm@43467
|
1546 |
@@{command (HOL) types_code} (tycodespec +)
|
wenzelm@26849
|
1547 |
;
|
wenzelm@26849
|
1548 |
|
wenzelm@43467
|
1549 |
tycodespec: @{syntax name} template attachment ?
|
wenzelm@26849
|
1550 |
;
|
wenzelm@26849
|
1551 |
|
wenzelm@43467
|
1552 |
const: @{syntax term}
|
wenzelm@26849
|
1553 |
;
|
wenzelm@26849
|
1554 |
|
wenzelm@43467
|
1555 |
template: '(' @{syntax string} ')'
|
wenzelm@26849
|
1556 |
;
|
wenzelm@26849
|
1557 |
|
wenzelm@43467
|
1558 |
attachment: 'attach' modespec? '{' @{syntax text} '}'
|
wenzelm@26849
|
1559 |
;
|
wenzelm@26849
|
1560 |
|
wenzelm@43497
|
1561 |
@@{attribute code} name?
|
wenzelm@43467
|
1562 |
"}
|
wenzelm@26849
|
1563 |
*}
|
wenzelm@26849
|
1564 |
|
wenzelm@27045
|
1565 |
|
wenzelm@43498
|
1566 |
subsubsection {* Invoking the code generator *}
|
wenzelm@43498
|
1567 |
|
wenzelm@43498
|
1568 |
text {* The code generator is invoked via the @{command code_module}
|
wenzelm@43498
|
1569 |
and @{command code_library} commands, which correspond to
|
wenzelm@43498
|
1570 |
\emph{incremental} and \emph{modular} code generation, respectively.
|
wenzelm@43498
|
1571 |
|
wenzelm@43498
|
1572 |
\begin{description}
|
wenzelm@43498
|
1573 |
|
wenzelm@43498
|
1574 |
\item [Modular] For each theory, an ML structure is generated,
|
wenzelm@43498
|
1575 |
containing the code generated from the constants defined in this
|
wenzelm@43498
|
1576 |
theory.
|
wenzelm@43498
|
1577 |
|
wenzelm@43498
|
1578 |
\item [Incremental] All the generated code is emitted into the same
|
wenzelm@43498
|
1579 |
structure. This structure may import code from previously generated
|
wenzelm@43498
|
1580 |
structures, which can be specified via @{keyword "imports"}.
|
wenzelm@43498
|
1581 |
Moreover, the generated structure may also be referred to in later
|
wenzelm@43498
|
1582 |
invocations of the code generator.
|
wenzelm@43498
|
1583 |
|
wenzelm@43498
|
1584 |
\end{description}
|
wenzelm@43498
|
1585 |
|
wenzelm@43498
|
1586 |
After the @{command code_module} and @{command code_library}
|
wenzelm@43498
|
1587 |
keywords, the user may specify an optional list of ``modes'' in
|
wenzelm@43498
|
1588 |
parentheses. These can be used to instruct the code generator to
|
wenzelm@43498
|
1589 |
emit additional code for special purposes, e.g.\ functions for
|
wenzelm@43498
|
1590 |
converting elements of generated datatypes to Isabelle terms, or
|
wenzelm@43498
|
1591 |
test data generators. The list of modes is followed by a module
|
wenzelm@43498
|
1592 |
name. The module name is optional for modular code generation, but
|
wenzelm@43498
|
1593 |
must be specified for incremental code generation.
|
wenzelm@43498
|
1594 |
|
wenzelm@43498
|
1595 |
The code can either be written to a file, in which case a file name
|
wenzelm@43498
|
1596 |
has to be specified after the @{keyword "file"} keyword, or be loaded
|
wenzelm@43498
|
1597 |
directly into Isabelle's ML environment. In the latter case, the
|
wenzelm@43498
|
1598 |
@{command ML} theory command can be used to inspect the results
|
wenzelm@43498
|
1599 |
interactively, for example.
|
wenzelm@43498
|
1600 |
|
wenzelm@43498
|
1601 |
The terms from which to generate code can be specified after the
|
wenzelm@43498
|
1602 |
@{keyword "contains"} keyword, either as a list of bindings, or just
|
wenzelm@43498
|
1603 |
as a list of terms. In the latter case, the code generator just
|
wenzelm@43498
|
1604 |
produces code for all constants and types occuring in the term, but
|
wenzelm@43498
|
1605 |
does not bind the compiled terms to ML identifiers.
|
wenzelm@43498
|
1606 |
|
wenzelm@43498
|
1607 |
Here is an example:
|
wenzelm@43498
|
1608 |
*}
|
wenzelm@43498
|
1609 |
|
wenzelm@43498
|
1610 |
code_module Test
|
wenzelm@43523
|
1611 |
contains test = "foldl op + (0 :: int) [1, 2, 3, 4, 5]"
|
wenzelm@43498
|
1612 |
|
wenzelm@43498
|
1613 |
text {* \noindent This binds the result of compiling the given term to
|
wenzelm@43498
|
1614 |
the ML identifier @{ML Test.test}. *}
|
wenzelm@43498
|
1615 |
|
wenzelm@43498
|
1616 |
ML {* @{assert} (Test.test = 15) *}
|
wenzelm@43498
|
1617 |
|
wenzelm@43498
|
1618 |
|
wenzelm@43498
|
1619 |
subsubsection {* Configuring the code generator *}
|
wenzelm@43498
|
1620 |
|
wenzelm@43498
|
1621 |
text {* When generating code for a complex term, the code generator
|
wenzelm@43498
|
1622 |
recursively calls itself for all subterms. When it arrives at a
|
wenzelm@43498
|
1623 |
constant, the default strategy of the code generator is to look up
|
wenzelm@43498
|
1624 |
its definition and try to generate code for it. Constants which
|
wenzelm@43498
|
1625 |
have no definitions that are immediately executable, may be
|
wenzelm@43498
|
1626 |
associated with a piece of ML code manually using the @{command_ref
|
wenzelm@43498
|
1627 |
consts_code} command. It takes a list whose elements consist of a
|
wenzelm@43498
|
1628 |
constant (given in usual term syntax -- an explicit type constraint
|
wenzelm@43498
|
1629 |
accounts for overloading), and a mixfix template describing the ML
|
wenzelm@43498
|
1630 |
code. The latter is very much the same as the mixfix templates used
|
wenzelm@43498
|
1631 |
when declaring new constants. The most notable difference is that
|
wenzelm@43498
|
1632 |
terms may be included in the ML template using antiquotation
|
wenzelm@43498
|
1633 |
brackets @{verbatim "{"}@{verbatim "*"}~@{text "..."}~@{verbatim
|
wenzelm@43498
|
1634 |
"*"}@{verbatim "}"}.
|
wenzelm@43498
|
1635 |
|
wenzelm@43498
|
1636 |
A similar mechanism is available for types: @{command_ref
|
wenzelm@43498
|
1637 |
types_code} associates type constructors with specific ML code.
|
wenzelm@43498
|
1638 |
|
wenzelm@43498
|
1639 |
For example, the following declarations copied from @{file
|
wenzelm@43498
|
1640 |
"~~/src/HOL/Product_Type.thy"} describe how the product type of
|
wenzelm@43498
|
1641 |
Isabelle/HOL should be compiled to ML. *}
|
wenzelm@43498
|
1642 |
|
wenzelm@43498
|
1643 |
typedecl ('a, 'b) prod
|
wenzelm@43498
|
1644 |
consts Pair :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) prod"
|
wenzelm@43498
|
1645 |
|
wenzelm@43498
|
1646 |
types_code prod ("(_ */ _)")
|
wenzelm@43498
|
1647 |
consts_code Pair ("(_,/ _)")
|
wenzelm@43498
|
1648 |
|
wenzelm@43498
|
1649 |
text {* Sometimes, the code associated with a constant or type may
|
wenzelm@43498
|
1650 |
need to refer to auxiliary functions, which have to be emitted when
|
wenzelm@43498
|
1651 |
the constant is used. Code for such auxiliary functions can be
|
wenzelm@43498
|
1652 |
declared using @{keyword "attach"}. For example, the @{const wfrec}
|
wenzelm@43498
|
1653 |
function can be implemented as follows:
|
wenzelm@43498
|
1654 |
*}
|
wenzelm@43498
|
1655 |
|
wenzelm@43498
|
1656 |
consts_code wfrec ("\<module>wfrec?") (* FIXME !? *)
|
wenzelm@43523
|
1657 |
attach {* fun wfrec f x = f (wfrec f) x *}
|
wenzelm@43498
|
1658 |
|
wenzelm@43498
|
1659 |
text {* If the code containing a call to @{const wfrec} resides in an
|
wenzelm@43498
|
1660 |
ML structure different from the one containing the function
|
wenzelm@43498
|
1661 |
definition attached to @{const wfrec}, the name of the ML structure
|
wenzelm@43498
|
1662 |
(followed by a ``@{text "."}'') is inserted in place of ``@{text
|
wenzelm@43498
|
1663 |
"\<module>"}'' in the above template. The ``@{text "?"}'' means that
|
wenzelm@43498
|
1664 |
the code generator should ignore the first argument of @{const
|
wenzelm@43498
|
1665 |
wfrec}, i.e.\ the termination relation, which is usually not
|
wenzelm@43498
|
1666 |
executable.
|
wenzelm@43498
|
1667 |
|
wenzelm@43498
|
1668 |
\medskip Another possibility of configuring the code generator is to
|
wenzelm@43498
|
1669 |
register theorems to be used for code generation. Theorems can be
|
wenzelm@43498
|
1670 |
registered via the @{attribute code} attribute. It takes an optional
|
wenzelm@43498
|
1671 |
name as an argument, which indicates the format of the
|
wenzelm@43498
|
1672 |
theorem. Currently supported formats are equations (this is the
|
wenzelm@43498
|
1673 |
default when no name is specified) and horn clauses (this is
|
wenzelm@43498
|
1674 |
indicated by the name \texttt{ind}). The left-hand sides of
|
wenzelm@43498
|
1675 |
equations may only contain constructors and distinct variables,
|
wenzelm@43498
|
1676 |
whereas horn clauses must have the same format as introduction rules
|
wenzelm@43498
|
1677 |
of inductive definitions.
|
wenzelm@43498
|
1678 |
|
wenzelm@43498
|
1679 |
The following example specifies three equations from which to
|
wenzelm@43498
|
1680 |
generate code for @{term "op <"} on natural numbers (see also
|
wenzelm@43498
|
1681 |
@{"file" "~~/src/HOL/Nat.thy"}). *}
|
wenzelm@43498
|
1682 |
|
wenzelm@43498
|
1683 |
lemma [code]: "(Suc m < Suc n) = (m < n)"
|
wenzelm@43498
|
1684 |
and [code]: "((n::nat) < 0) = False"
|
wenzelm@43498
|
1685 |
and [code]: "(0 < Suc n) = True" by simp_all
|
wenzelm@43498
|
1686 |
|
wenzelm@43498
|
1687 |
|
wenzelm@43498
|
1688 |
subsubsection {* Specific HOL code generators *}
|
wenzelm@43498
|
1689 |
|
wenzelm@43498
|
1690 |
text {* The basic code generator framework offered by Isabelle/Pure
|
wenzelm@43498
|
1691 |
has already been extended with additional code generators for
|
wenzelm@43498
|
1692 |
specific HOL constructs. These include datatypes, recursive
|
wenzelm@43498
|
1693 |
functions and inductive relations. The code generator for inductive
|
wenzelm@43498
|
1694 |
relations can handle expressions of the form @{text "(t\<^sub>1, \<dots>, t\<^sub>n) \<in>
|
wenzelm@43498
|
1695 |
r"}, where @{text "r"} is an inductively defined relation. If at
|
wenzelm@43498
|
1696 |
least one of the @{text "t\<^sub>i"} is a dummy pattern ``@{text "_"}'',
|
wenzelm@43498
|
1697 |
the above expression evaluates to a sequence of possible answers. If
|
wenzelm@43498
|
1698 |
all of the @{text "t\<^sub>i"} are proper terms, the expression evaluates
|
wenzelm@43498
|
1699 |
to a boolean value.
|
wenzelm@43498
|
1700 |
|
wenzelm@43523
|
1701 |
The following example demonstrates this for beta-reduction on lambda
|
wenzelm@43523
|
1702 |
terms (see also @{"file" "~~/src/HOL/Proofs/Lambda/Lambda.thy"}).
|
wenzelm@43523
|
1703 |
*}
|
wenzelm@43498
|
1704 |
|
wenzelm@43523
|
1705 |
datatype dB =
|
wenzelm@43523
|
1706 |
Var nat
|
wenzelm@43523
|
1707 |
| App dB dB (infixl "\<degree>" 200)
|
wenzelm@43523
|
1708 |
| Abs dB
|
wenzelm@43523
|
1709 |
|
wenzelm@43523
|
1710 |
primrec lift :: "dB \<Rightarrow> nat \<Rightarrow> dB"
|
wenzelm@43523
|
1711 |
where
|
wenzelm@43523
|
1712 |
"lift (Var i) k = (if i < k then Var i else Var (i + 1))"
|
wenzelm@43523
|
1713 |
| "lift (s \<degree> t) k = lift s k \<degree> lift t k"
|
wenzelm@43523
|
1714 |
| "lift (Abs s) k = Abs (lift s (k + 1))"
|
wenzelm@43523
|
1715 |
|
wenzelm@43523
|
1716 |
primrec subst :: "dB \<Rightarrow> dB \<Rightarrow> nat \<Rightarrow> dB" ("_[_'/_]" [300, 0, 0] 300)
|
wenzelm@43523
|
1717 |
where
|
wenzelm@43523
|
1718 |
"(Var i)[s/k] =
|
wenzelm@43523
|
1719 |
(if k < i then Var (i - 1) else if i = k then s else Var i)"
|
wenzelm@43523
|
1720 |
| "(t \<degree> u)[s/k] = t[s/k] \<degree> u[s/k]"
|
wenzelm@43523
|
1721 |
| "(Abs t)[s/k] = Abs (t[lift s 0 / k+1])"
|
wenzelm@43523
|
1722 |
|
wenzelm@43523
|
1723 |
inductive beta :: "dB \<Rightarrow> dB \<Rightarrow> bool" (infixl "\<rightarrow>\<^sub>\<beta>" 50)
|
wenzelm@43523
|
1724 |
where
|
wenzelm@43523
|
1725 |
beta: "Abs s \<degree> t \<rightarrow>\<^sub>\<beta> s[t/0]"
|
wenzelm@43523
|
1726 |
| appL: "s \<rightarrow>\<^sub>\<beta> t \<Longrightarrow> s \<degree> u \<rightarrow>\<^sub>\<beta> t \<degree> u"
|
wenzelm@43523
|
1727 |
| appR: "s \<rightarrow>\<^sub>\<beta> t \<Longrightarrow> u \<degree> s \<rightarrow>\<^sub>\<beta> u \<degree> t"
|
wenzelm@43523
|
1728 |
| abs: "s \<rightarrow>\<^sub>\<beta> t \<Longrightarrow> Abs s \<rightarrow>\<^sub>\<beta> Abs t"
|
wenzelm@43523
|
1729 |
|
wenzelm@43523
|
1730 |
code_module Test
|
wenzelm@43523
|
1731 |
contains
|
wenzelm@43523
|
1732 |
test1 = "Abs (Var 0) \<degree> Var 0 \<rightarrow>\<^sub>\<beta> Var 0"
|
wenzelm@43523
|
1733 |
test2 = "Abs (Abs (Var 0 \<degree> Var 0) \<degree> (Abs (Var 0) \<degree> Var 0)) \<rightarrow>\<^sub>\<beta> _"
|
wenzelm@43523
|
1734 |
|
wenzelm@43523
|
1735 |
text {*
|
wenzelm@43523
|
1736 |
In the above example, @{ML Test.test1} evaluates to a boolean,
|
wenzelm@43523
|
1737 |
whereas @{ML Test.test2} is a lazy sequence whose elements can be
|
wenzelm@43523
|
1738 |
inspected separately.
|
wenzelm@43523
|
1739 |
*}
|
wenzelm@43523
|
1740 |
|
wenzelm@43523
|
1741 |
ML {* @{assert} Test.test1 *}
|
wenzelm@43523
|
1742 |
ML {* val results = DSeq.list_of Test.test2 *}
|
wenzelm@43523
|
1743 |
ML {* @{assert} (length results = 2) *}
|
wenzelm@43523
|
1744 |
|
wenzelm@43523
|
1745 |
text {*
|
wenzelm@43498
|
1746 |
\medskip The theory underlying the HOL code generator is described
|
wenzelm@43498
|
1747 |
more detailed in \cite{Berghofer-Nipkow:2002}. More examples that
|
wenzelm@43498
|
1748 |
illustrate the usage of the code generator can be found e.g.\ in
|
wenzelm@43498
|
1749 |
@{"file" "~~/src/HOL/MicroJava/J/JListExample.thy"} and @{"file"
|
wenzelm@43498
|
1750 |
"~~/src/HOL/MicroJava/JVM/JVMListExample.thy"}.
|
wenzelm@43498
|
1751 |
*}
|
wenzelm@43498
|
1752 |
|
wenzelm@43498
|
1753 |
|
wenzelm@27045
|
1754 |
section {* Definition by specification \label{sec:hol-specification} *}
|
wenzelm@27045
|
1755 |
|
wenzelm@27045
|
1756 |
text {*
|
wenzelm@27045
|
1757 |
\begin{matharray}{rcl}
|
wenzelm@28761
|
1758 |
@{command_def (HOL) "specification"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
|
wenzelm@28761
|
1759 |
@{command_def (HOL) "ax_specification"} & : & @{text "theory \<rightarrow> proof(prove)"} \\
|
wenzelm@27045
|
1760 |
\end{matharray}
|
wenzelm@27045
|
1761 |
|
wenzelm@43467
|
1762 |
@{rail "
|
wenzelm@43467
|
1763 |
(@@{command (HOL) specification} | @@{command (HOL) ax_specification})
|
wenzelm@43467
|
1764 |
'(' (decl +) ')' \\ (@{syntax thmdecl}? @{syntax prop} +)
|
wenzelm@27045
|
1765 |
;
|
wenzelm@43467
|
1766 |
decl: ((@{syntax name} ':')? @{syntax term} '(' @'overloaded' ')'?)
|
wenzelm@43467
|
1767 |
"}
|
wenzelm@27045
|
1768 |
|
wenzelm@28760
|
1769 |
\begin{description}
|
wenzelm@27045
|
1770 |
|
wenzelm@28760
|
1771 |
\item @{command (HOL) "specification"}~@{text "decls \<phi>"} sets up a
|
wenzelm@27045
|
1772 |
goal stating the existence of terms with the properties specified to
|
wenzelm@27045
|
1773 |
hold for the constants given in @{text decls}. After finishing the
|
wenzelm@27045
|
1774 |
proof, the theory will be augmented with definitions for the given
|
wenzelm@27045
|
1775 |
constants, as well as with theorems stating the properties for these
|
wenzelm@27045
|
1776 |
constants.
|
wenzelm@27045
|
1777 |
|
wenzelm@28760
|
1778 |
\item @{command (HOL) "ax_specification"}~@{text "decls \<phi>"} sets up
|
wenzelm@28760
|
1779 |
a goal stating the existence of terms with the properties specified
|
wenzelm@28760
|
1780 |
to hold for the constants given in @{text decls}. After finishing
|
wenzelm@28760
|
1781 |
the proof, the theory will be augmented with axioms expressing the
|
wenzelm@28760
|
1782 |
properties given in the first place.
|
wenzelm@27045
|
1783 |
|
wenzelm@28760
|
1784 |
\item @{text decl} declares a constant to be defined by the
|
wenzelm@27045
|
1785 |
specification given. The definition for the constant @{text c} is
|
wenzelm@27045
|
1786 |
bound to the name @{text c_def} unless a theorem name is given in
|
wenzelm@27045
|
1787 |
the declaration. Overloaded constants should be declared as such.
|
wenzelm@27045
|
1788 |
|
wenzelm@28760
|
1789 |
\end{description}
|
wenzelm@27045
|
1790 |
|
wenzelm@27045
|
1791 |
Whether to use @{command (HOL) "specification"} or @{command (HOL)
|
wenzelm@27045
|
1792 |
"ax_specification"} is to some extent a matter of style. @{command
|
wenzelm@27045
|
1793 |
(HOL) "specification"} introduces no new axioms, and so by
|
wenzelm@27045
|
1794 |
construction cannot introduce inconsistencies, whereas @{command
|
wenzelm@27045
|
1795 |
(HOL) "ax_specification"} does introduce axioms, but only after the
|
wenzelm@27045
|
1796 |
user has explicitly proven it to be safe. A practical issue must be
|
wenzelm@27045
|
1797 |
considered, though: After introducing two constants with the same
|
wenzelm@27045
|
1798 |
properties using @{command (HOL) "specification"}, one can prove
|
wenzelm@27045
|
1799 |
that the two constants are, in fact, equal. If this might be a
|
wenzelm@27045
|
1800 |
problem, one should use @{command (HOL) "ax_specification"}.
|
wenzelm@27045
|
1801 |
*}
|
wenzelm@27045
|
1802 |
|
wenzelm@26840
|
1803 |
end
|