author | wenzelm |
Mon, 30 Nov 2015 15:23:02 +0100 | |
changeset 61758 | df6258b7e53f |
parent 61656 | cfabbc083977 |
child 61841 | 4d3527b94f2a |
permissions | -rw-r--r-- |
61656 | 1 |
(*:maxLineLen=78:*) |
2 |
||
29755 | 3 |
theory Isar |
4 |
imports Base |
|
5 |
begin |
|
20472 | 6 |
|
58618 | 7 |
chapter \<open>Isar language elements\<close> |
29759 | 8 |
|
58618 | 9 |
text \<open>The Isar proof language (see also |
58555 | 10 |
@{cite \<open>\S2\<close> "isabelle-isar-ref"}) consists of three main categories of |
57340 | 11 |
language elements: |
29759 | 12 |
|
61477 | 13 |
\<^enum> Proof \<^emph>\<open>commands\<close> define the primary language of |
39842 | 14 |
transactions of the underlying Isar/VM interpreter. Typical |
15 |
examples are @{command "fix"}, @{command "assume"}, @{command |
|
39844 | 16 |
"show"}, @{command "proof"}, and @{command "qed"}. |
39842 | 17 |
|
39844 | 18 |
Composing proof commands according to the rules of the Isar/VM leads |
19 |
to expressions of structured proof text, such that both the machine |
|
20 |
and the human reader can give it a meaning as formal reasoning. |
|
20472 | 21 |
|
61477 | 22 |
\<^enum> Proof \<^emph>\<open>methods\<close> define a secondary language of mixed |
39842 | 23 |
forward-backward refinement steps involving facts and goals. |
39846 | 24 |
Typical examples are @{method rule}, @{method unfold}, and @{method |
39844 | 25 |
simp}. |
29759 | 26 |
|
39842 | 27 |
Methods can occur in certain well-defined parts of the Isar proof |
28 |
language, say as arguments to @{command "proof"}, @{command "qed"}, |
|
29 |
or @{command "by"}. |
|
30 |
||
61477 | 31 |
\<^enum> \<^emph>\<open>Attributes\<close> define a tertiary language of small |
39849 | 32 |
annotations to theorems being defined or referenced. Attributes can |
33 |
modify both the context and the theorem. |
|
39842 | 34 |
|
39849 | 35 |
Typical examples are @{attribute intro} (which affects the context), |
36 |
and @{attribute symmetric} (which affects the theorem). |
|
58618 | 37 |
\<close> |
29759 | 38 |
|
39 |
||
58618 | 40 |
section \<open>Proof commands\<close> |
20520 | 41 |
|
61477 | 42 |
text \<open>A \<^emph>\<open>proof command\<close> is state transition of the Isar/VM |
39849 | 43 |
proof interpreter. |
44 |
||
40126 | 45 |
In principle, Isar proof commands could be defined in user-space as |
46 |
well. The system is built like that in the first place: one part of |
|
47 |
the commands are primitive, the other part is defined as derived |
|
48 |
elements. Adding to the genuine structured proof language requires |
|
49 |
profound understanding of the Isar/VM machinery, though, so this is |
|
50 |
beyond the scope of this manual. |
|
39842 | 51 |
|
52 |
What can be done realistically is to define some diagnostic commands |
|
39844 | 53 |
that inspect the general state of the Isar/VM, and report some |
54 |
feedback to the user. Typically this involves checking of the |
|
61477 | 55 |
linguistic \<^emph>\<open>mode\<close> of a proof state, or peeking at the pending |
39842 | 56 |
goals (if available). |
39845 | 57 |
|
58 |
Another common application is to define a toplevel command that |
|
40126 | 59 |
poses a problem to the user as Isar proof state and processes the |
60 |
final result relatively to the context. Thus a proof can be |
|
39845 | 61 |
incorporated into the context of some user-space tool, without |
58618 | 62 |
modifying the Isar proof language itself.\<close> |
39842 | 63 |
|
58618 | 64 |
text %mlref \<open> |
39842 | 65 |
\begin{mldecls} |
66 |
@{index_ML_type Proof.state} \\ |
|
67 |
@{index_ML Proof.assert_forward: "Proof.state -> Proof.state"} \\ |
|
68 |
@{index_ML Proof.assert_chain: "Proof.state -> Proof.state"} \\ |
|
69 |
@{index_ML Proof.assert_backward: "Proof.state -> Proof.state"} \\ |
|
70 |
@{index_ML Proof.simple_goal: "Proof.state -> {context: Proof.context, goal: thm}"} \\ |
|
71 |
@{index_ML Proof.goal: "Proof.state -> |
|
72 |
{context: Proof.context, facts: thm list, goal: thm}"} \\ |
|
73 |
@{index_ML Proof.raw_goal: "Proof.state -> |
|
74 |
{context: Proof.context, facts: thm list, goal: thm}"} \\ |
|
39845 | 75 |
@{index_ML Proof.theorem: "Method.text option -> |
76 |
(thm list list -> Proof.context -> Proof.context) -> |
|
77 |
(term * term list) list list -> Proof.context -> Proof.state"} \\ |
|
39842 | 78 |
\end{mldecls} |
79 |
||
61439 | 80 |
\<^descr> Type @{ML_type Proof.state} represents Isar proof states. |
39864 | 81 |
This is a block-structured configuration with proof context, |
82 |
linguistic mode, and optional goal. The latter consists of goal |
|
61493 | 83 |
context, goal facts (``\<open>using\<close>''), and tactical goal state |
39864 | 84 |
(see \secref{sec:tactical-goals}). |
39842 | 85 |
|
86 |
The general idea is that the facts shall contribute to the |
|
39844 | 87 |
refinement of some parts of the tactical goal --- how exactly is |
88 |
defined by the proof method that is applied in that situation. |
|
39842 | 89 |
|
61439 | 90 |
\<^descr> @{ML Proof.assert_forward}, @{ML Proof.assert_chain}, @{ML |
39842 | 91 |
Proof.assert_backward} are partial identity functions that fail |
61493 | 92 |
unless a certain linguistic mode is active, namely ``\<open>proof(state)\<close>'', ``\<open>proof(chain)\<close>'', ``\<open>proof(prove)\<close>'', respectively (using the terminology of |
58555 | 93 |
@{cite "isabelle-isar-ref"}). |
39842 | 94 |
|
95 |
It is advisable study the implementations of existing proof commands |
|
96 |
for suitable modes to be asserted. |
|
97 |
||
61493 | 98 |
\<^descr> @{ML Proof.simple_goal}~\<open>state\<close> returns the structured |
39842 | 99 |
Isar goal (if available) in the form seen by ``simple'' methods |
39846 | 100 |
(like @{method simp} or @{method blast}). The Isar goal facts are |
39842 | 101 |
already inserted as premises into the subgoals, which are presented |
39844 | 102 |
individually as in @{ML Proof.goal}. |
39842 | 103 |
|
61493 | 104 |
\<^descr> @{ML Proof.goal}~\<open>state\<close> returns the structured Isar |
39842 | 105 |
goal (if available) in the form seen by regular methods (like |
106 |
@{method rule}). The auxiliary internal encoding of Pure |
|
107 |
conjunctions is split into individual subgoals as usual. |
|
108 |
||
61493 | 109 |
\<^descr> @{ML Proof.raw_goal}~\<open>state\<close> returns the structured |
39842 | 110 |
Isar goal (if available) in the raw internal form seen by ``raw'' |
39846 | 111 |
methods (like @{method induct}). This form is rarely appropriate |
56579 | 112 |
for diagnostic tools; @{ML Proof.simple_goal} or @{ML Proof.goal} |
39846 | 113 |
should be used in most situations. |
39842 | 114 |
|
61493 | 115 |
\<^descr> @{ML Proof.theorem}~\<open>before_qed after_qed statement ctxt\<close> |
39845 | 116 |
initializes a toplevel Isar proof state within a given context. |
117 |
||
61493 | 118 |
The optional \<open>before_qed\<close> method is applied at the end of |
39845 | 119 |
the proof, just before extracting the result (this feature is rarely |
120 |
used). |
|
121 |
||
61493 | 122 |
The \<open>after_qed\<close> continuation receives the extracted result |
39845 | 123 |
in order to apply it to the final context in a suitable way (e.g.\ |
124 |
storing named facts). Note that at this generic level the target |
|
125 |
context is specified as @{ML_type Proof.context}, but the usual |
|
126 |
wrapping of toplevel proofs into command transactions will provide a |
|
40126 | 127 |
@{ML_type local_theory} here (\chref{ch:local-theory}). This |
128 |
affects the way how results are stored. |
|
39845 | 129 |
|
61493 | 130 |
The \<open>statement\<close> is given as a nested list of terms, each |
39845 | 131 |
associated with optional @{keyword "is"} patterns as usual in the |
40126 | 132 |
Isar source language. The original nested list structure over terms |
61493 | 133 |
is turned into one over theorems when \<open>after_qed\<close> is |
40126 | 134 |
invoked. |
58618 | 135 |
\<close> |
39842 | 136 |
|
20520 | 137 |
|
58618 | 138 |
text %mlantiq \<open> |
39843 | 139 |
\begin{matharray}{rcl} |
61493 | 140 |
@{ML_antiquotation_def "Isar.goal"} & : & \<open>ML_antiquotation\<close> \\ |
39843 | 141 |
\end{matharray} |
142 |
||
61493 | 143 |
\<^descr> \<open>@{Isar.goal}\<close> refers to the regular goal state (if |
39843 | 144 |
available) of the current proof state managed by the Isar toplevel |
145 |
--- as abstract value. |
|
146 |
||
147 |
This only works for diagnostic ML commands, such as @{command |
|
148 |
ML_val} or @{command ML_command}. |
|
58618 | 149 |
\<close> |
39843 | 150 |
|
58618 | 151 |
text %mlex \<open>The following example peeks at a certain goal configuration.\<close> |
39843 | 152 |
|
40964 | 153 |
notepad |
154 |
begin |
|
39846 | 155 |
have A and B and C |
58728 | 156 |
ML_val |
157 |
\<open>val n = Thm.nprems_of (#goal @{Isar.goal}); |
|
158 |
@{assert} (n = 3);\<close> |
|
58801 | 159 |
sorry |
160 |
end |
|
39843 | 161 |
|
58618 | 162 |
text \<open>Here we see 3 individual subgoals in the same way as regular |
163 |
proof methods would do.\<close> |
|
39843 | 164 |
|
20520 | 165 |
|
58618 | 166 |
section \<open>Proof methods\<close> |
20472 | 167 |
|
61493 | 168 |
text \<open>A \<open>method\<close> is a function \<open>context \<rightarrow> thm\<^sup>* \<rightarrow> goal |
169 |
\<rightarrow> (cases \<times> goal)\<^sup>*\<^sup>*\<close> that operates on the full Isar goal |
|
39847 | 170 |
configuration with context, goal facts, and tactical goal state and |
39843 | 171 |
enumerates possible follow-up goal states, with the potential |
61477 | 172 |
addition of named extensions of the proof context (\<^emph>\<open>cases\<close>). |
39847 | 173 |
The latter feature is rarely used. |
174 |
||
175 |
This means a proof method is like a structurally enhanced tactic |
|
176 |
(cf.\ \secref{sec:tactics}). The well-formedness conditions for |
|
177 |
tactics need to hold for methods accordingly, with the following |
|
178 |
additions. |
|
179 |
||
61416 | 180 |
\<^item> Goal addressing is further limited either to operate |
61477 | 181 |
uniformly on \<^emph>\<open>all\<close> subgoals, or specifically on the |
182 |
\<^emph>\<open>first\<close> subgoal. |
|
39847 | 183 |
|
184 |
Exception: old-style tactic emulations that are embedded into the |
|
185 |
method space, e.g.\ @{method rule_tac}. |
|
186 |
||
61416 | 187 |
\<^item> A non-trivial method always needs to make progress: an |
61572 | 188 |
identical follow-up goal state has to be avoided.\<^footnote>\<open>This |
61493 | 189 |
enables the user to write method expressions like \<open>meth\<^sup>+\<close> |
39847 | 190 |
without looping, while the trivial do-nothing case can be recovered |
61572 | 191 |
via \<open>meth\<^sup>?\<close>.\<close> |
39847 | 192 |
|
193 |
Exception: trivial stuttering steps, such as ``@{method -}'' or |
|
194 |
@{method succeed}. |
|
195 |
||
61416 | 196 |
\<^item> Goal facts passed to the method must not be ignored. If there |
39847 | 197 |
is no sensible use of facts outside the goal state, facts should be |
198 |
inserted into the subgoals that are addressed by the method. |
|
199 |
||
200 |
||
61416 | 201 |
\<^medskip> |
202 |
Syntactically, the language of proof methods appears as |
|
40126 | 203 |
arguments to Isar commands like @{command "by"} or @{command apply}. |
204 |
User-space additions are reasonably easy by plugging suitable |
|
205 |
method-valued parser functions into the framework, using the |
|
206 |
@{command method_setup} command, for example. |
|
39843 | 207 |
|
39844 | 208 |
To get a better idea about the range of possibilities, consider the |
40126 | 209 |
following Isar proof schemes. This is the general form of |
210 |
structured proof text: |
|
39843 | 211 |
|
61416 | 212 |
\<^medskip> |
39843 | 213 |
\begin{tabular}{l} |
61493 | 214 |
@{command from}~\<open>facts\<^sub>1\<close>~@{command have}~\<open>props\<close>~@{command using}~\<open>facts\<^sub>2\<close> \\ |
215 |
@{command proof}~\<open>(initial_method)\<close> \\ |
|
216 |
\quad\<open>body\<close> \\ |
|
217 |
@{command qed}~\<open>(terminal_method)\<close> \\ |
|
39843 | 218 |
\end{tabular} |
61416 | 219 |
\<^medskip> |
39843 | 220 |
|
61493 | 221 |
The goal configuration consists of \<open>facts\<^sub>1\<close> and |
222 |
\<open>facts\<^sub>2\<close> appended in that order, and various \<open>props\<close> being claimed. The \<open>initial_method\<close> is invoked |
|
40126 | 223 |
with facts and goals together and refines the problem to something |
61493 | 224 |
that is handled recursively in the proof \<open>body\<close>. The \<open>terminal_method\<close> has another chance to finish any remaining |
39843 | 225 |
subgoals, but it does not see the facts of the initial step. |
226 |
||
61416 | 227 |
\<^medskip> |
228 |
This pattern illustrates unstructured proof scripts: |
|
39843 | 229 |
|
61416 | 230 |
\<^medskip> |
39843 | 231 |
\begin{tabular}{l} |
61493 | 232 |
@{command have}~\<open>props\<close> \\ |
233 |
\quad@{command using}~\<open>facts\<^sub>1\<close>~@{command apply}~\<open>method\<^sub>1\<close> \\ |
|
234 |
\quad@{command apply}~\<open>method\<^sub>2\<close> \\ |
|
235 |
\quad@{command using}~\<open>facts\<^sub>3\<close>~@{command apply}~\<open>method\<^sub>3\<close> \\ |
|
39843 | 236 |
\quad@{command done} \\ |
237 |
\end{tabular} |
|
61416 | 238 |
\<^medskip> |
39843 | 239 |
|
61493 | 240 |
The \<open>method\<^sub>1\<close> operates on the original claim while |
241 |
using \<open>facts\<^sub>1\<close>. Since the @{command apply} command |
|
242 |
structurally resets the facts, the \<open>method\<^sub>2\<close> will |
|
243 |
operate on the remaining goal state without facts. The \<open>method\<^sub>3\<close> will see again a collection of \<open>facts\<^sub>3\<close> that has been inserted into the script explicitly. |
|
39843 | 244 |
|
61416 | 245 |
\<^medskip> |
246 |
Empirically, any Isar proof method can be categorized as |
|
39847 | 247 |
follows. |
39843 | 248 |
|
61477 | 249 |
\<^enum> \<^emph>\<open>Special method with cases\<close> with named context additions |
39847 | 250 |
associated with the follow-up goal state. |
39843 | 251 |
|
39847 | 252 |
Example: @{method "induct"}, which is also a ``raw'' method since it |
253 |
operates on the internal representation of simultaneous claims as |
|
46134 | 254 |
Pure conjunction (\secref{sec:logic-aux}), instead of separate |
255 |
subgoals (\secref{sec:tactical-goals}). |
|
39843 | 256 |
|
61477 | 257 |
\<^enum> \<^emph>\<open>Structured method\<close> with strong emphasis on facts outside |
39847 | 258 |
the goal state. |
259 |
||
260 |
Example: @{method "rule"}, which captures the key ideas behind |
|
57340 | 261 |
structured reasoning in Isar in its purest form. |
39843 | 262 |
|
61477 | 263 |
\<^enum> \<^emph>\<open>Simple method\<close> with weaker emphasis on facts, which are |
57340 | 264 |
inserted into subgoals to emulate old-style tactical ``premises''. |
39843 | 265 |
|
39847 | 266 |
Examples: @{method "simp"}, @{method "blast"}, @{method "auto"}. |
39843 | 267 |
|
61477 | 268 |
\<^enum> \<^emph>\<open>Old-style tactic emulation\<close> with detailed numeric goal |
39847 | 269 |
addressing and explicit references to entities of the internal goal |
270 |
state (which are otherwise invisible from proper Isar proof text). |
|
61493 | 271 |
The naming convention \<open>foo_tac\<close> makes this special |
40126 | 272 |
non-standard status clear. |
39843 | 273 |
|
39847 | 274 |
Example: @{method "rule_tac"}. |
39843 | 275 |
|
276 |
||
39847 | 277 |
When implementing proof methods, it is advisable to study existing |
278 |
implementations carefully and imitate the typical ``boiler plate'' |
|
279 |
for context-sensitive parsing and further combinators to wrap-up |
|
61572 | 280 |
tactic expressions as methods.\<^footnote>\<open>Aliases or abbreviations of |
39848 | 281 |
the standard method combinators should be avoided. Note that from |
282 |
Isabelle99 until Isabelle2009 the system did provide various odd |
|
57340 | 283 |
combinations of method syntax wrappers that made applications more |
61572 | 284 |
complicated than necessary.\<close> |
58618 | 285 |
\<close> |
39847 | 286 |
|
58618 | 287 |
text %mlref \<open> |
39847 | 288 |
\begin{mldecls} |
289 |
@{index_ML_type Proof.method} \\ |
|
290 |
@{index_ML METHOD_CASES: "(thm list -> cases_tactic) -> Proof.method"} \\ |
|
291 |
@{index_ML METHOD: "(thm list -> tactic) -> Proof.method"} \\ |
|
292 |
@{index_ML SIMPLE_METHOD: "tactic -> Proof.method"} \\ |
|
293 |
@{index_ML SIMPLE_METHOD': "(int -> tactic) -> Proof.method"} \\ |
|
294 |
@{index_ML Method.insert_tac: "thm list -> int -> tactic"} \\ |
|
295 |
@{index_ML Method.setup: "binding -> (Proof.context -> Proof.method) context_parser -> |
|
296 |
string -> theory -> theory"} \\ |
|
297 |
\end{mldecls} |
|
298 |
||
61439 | 299 |
\<^descr> Type @{ML_type Proof.method} represents proof methods as |
39864 | 300 |
abstract type. |
39847 | 301 |
|
61493 | 302 |
\<^descr> @{ML METHOD_CASES}~\<open>(fn facts => cases_tactic)\<close> wraps |
303 |
\<open>cases_tactic\<close> depending on goal facts as proof method with |
|
39847 | 304 |
cases; the goal context is passed via method syntax. |
305 |
||
61493 | 306 |
\<^descr> @{ML METHOD}~\<open>(fn facts => tactic)\<close> wraps \<open>tactic\<close> depending on goal facts as regular proof method; the goal |
39847 | 307 |
context is passed via method syntax. |
308 |
||
61493 | 309 |
\<^descr> @{ML SIMPLE_METHOD}~\<open>tactic\<close> wraps a tactic that |
39847 | 310 |
addresses all subgoals uniformly as simple proof method. Goal facts |
61493 | 311 |
are already inserted into all subgoals before \<open>tactic\<close> is |
39847 | 312 |
applied. |
313 |
||
61493 | 314 |
\<^descr> @{ML SIMPLE_METHOD'}~\<open>tactic\<close> wraps a tactic that |
46267 | 315 |
addresses a specific subgoal as simple proof method that operates on |
61493 | 316 |
subgoal 1. Goal facts are inserted into the subgoal then the \<open>tactic\<close> is applied. |
39847 | 317 |
|
61493 | 318 |
\<^descr> @{ML Method.insert_tac}~\<open>facts i\<close> inserts \<open>facts\<close> into subgoal \<open>i\<close>. This is convenient to reproduce |
39847 | 319 |
part of the @{ML SIMPLE_METHOD} or @{ML SIMPLE_METHOD'} wrapping |
320 |
within regular @{ML METHOD}, for example. |
|
321 |
||
61493 | 322 |
\<^descr> @{ML Method.setup}~\<open>name parser description\<close> provides |
39848 | 323 |
the functionality of the Isar command @{command method_setup} as ML |
324 |
function. |
|
58618 | 325 |
\<close> |
39847 | 326 |
|
58618 | 327 |
text %mlex \<open>See also @{command method_setup} in |
58555 | 328 |
@{cite "isabelle-isar-ref"} which includes some abstract examples. |
39851 | 329 |
|
61416 | 330 |
\<^medskip> |
331 |
The following toy examples illustrate how the goal facts |
|
56579 | 332 |
and state are passed to proof methods. The predefined proof method |
39848 | 333 |
called ``@{method tactic}'' wraps ML source of type @{ML_type |
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
334 |
tactic} (abstracted over @{ML_text facts}). This allows immediate |
58618 | 335 |
experimentation without parsing of concrete syntax.\<close> |
20472 | 336 |
|
40964 | 337 |
notepad |
338 |
begin |
|
57340 | 339 |
fix A B :: bool |
39847 | 340 |
assume a: A and b: B |
341 |
||
342 |
have "A \<and> B" |
|
60754 | 343 |
apply (tactic \<open>resolve_tac @{context} @{thms conjI} 1\<close>) |
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
58801
diff
changeset
|
344 |
using a apply (tactic \<open>resolve_tac @{context} facts 1\<close>) |
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
58801
diff
changeset
|
345 |
using b apply (tactic \<open>resolve_tac @{context} facts 1\<close>) |
39847 | 346 |
done |
347 |
||
348 |
have "A \<and> B" |
|
349 |
using a and b |
|
57340 | 350 |
ML_val \<open>@{Isar.goal}\<close> |
351 |
apply (tactic \<open>Method.insert_tac facts 1\<close>) |
|
60754 | 352 |
apply (tactic \<open>(resolve_tac @{context} @{thms conjI} THEN_ALL_NEW assume_tac @{context}) 1\<close>) |
39847 | 353 |
done |
40964 | 354 |
end |
39847 | 355 |
|
61416 | 356 |
text \<open> |
357 |
\<^medskip> |
|
358 |
The next example implements a method that simplifies |
|
58618 | 359 |
the first subgoal by rewrite rules that are given as arguments.\<close> |
39848 | 360 |
|
57340 | 361 |
method_setup my_simp = |
362 |
\<open>Attrib.thms >> (fn thms => fn ctxt => |
|
39848 | 363 |
SIMPLE_METHOD' (fn i => |
364 |
CHANGED (asm_full_simp_tac |
|
57340 | 365 |
(put_simpset HOL_basic_ss ctxt addsimps thms) i)))\<close> |
366 |
"rewrite subgoal by given rules" |
|
39848 | 367 |
|
58618 | 368 |
text \<open>The concrete syntax wrapping of @{command method_setup} always |
39848 | 369 |
passes-through the proof context at the end of parsing, but it is |
370 |
not used in this example. |
|
371 |
||
372 |
The @{ML Attrib.thms} parser produces a list of theorems from the |
|
373 |
usual Isar syntax involving attribute expressions etc.\ (syntax |
|
58555 | 374 |
category @{syntax thmrefs}) @{cite "isabelle-isar-ref"}. The resulting |
40149
4c35be108990
proper markup of uninterpreted ML text as @{ML_text}, not @{verbatim};
wenzelm
parents:
40126
diff
changeset
|
375 |
@{ML_text thms} are added to @{ML HOL_basic_ss} which already |
40126 | 376 |
contains the basic Simplifier setup for HOL. |
39848 | 377 |
|
378 |
The tactic @{ML asm_full_simp_tac} is the one that is also used in |
|
379 |
method @{method simp} by default. The extra wrapping by the @{ML |
|
380 |
CHANGED} tactical ensures progress of simplification: identical goal |
|
381 |
states are filtered out explicitly to make the raw tactic conform to |
|
382 |
standard Isar method behaviour. |
|
383 |
||
61416 | 384 |
\<^medskip> |
385 |
Method @{method my_simp} can be used in Isar proofs like this: |
|
58618 | 386 |
\<close> |
39847 | 387 |
|
40964 | 388 |
notepad |
389 |
begin |
|
57340 | 390 |
fix a b c :: 'a |
39851 | 391 |
assume a: "a = b" |
392 |
assume b: "b = c" |
|
393 |
have "a = c" by (my_simp a b) |
|
40964 | 394 |
end |
39851 | 395 |
|
58618 | 396 |
text \<open>Here is a similar method that operates on all subgoals, |
397 |
instead of just the first one.\<close> |
|
39851 | 398 |
|
57340 | 399 |
method_setup my_simp_all = |
400 |
\<open>Attrib.thms >> (fn thms => fn ctxt => |
|
39851 | 401 |
SIMPLE_METHOD |
402 |
(CHANGED |
|
403 |
(ALLGOALS (asm_full_simp_tac |
|
57340 | 404 |
(put_simpset HOL_basic_ss ctxt addsimps thms)))))\<close> |
405 |
"rewrite all subgoals by given rules" |
|
39851 | 406 |
|
40964 | 407 |
notepad |
408 |
begin |
|
57340 | 409 |
fix a b c :: 'a |
39851 | 410 |
assume a: "a = b" |
411 |
assume b: "b = c" |
|
412 |
have "a = c" and "c = b" by (my_simp_all a b) |
|
40964 | 413 |
end |
39848 | 414 |
|
61416 | 415 |
text \<open> |
416 |
\<^medskip> |
|
417 |
Apart from explicit arguments, common proof methods |
|
57946
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
418 |
typically work with a default configuration provided by the context. As a |
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
419 |
shortcut to rule management we use a cheap solution via the @{command |
58618 | 420 |
named_theorems} command to declare a dynamic fact in the context.\<close> |
39848 | 421 |
|
57946
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
422 |
named_theorems my_simp |
39847 | 423 |
|
58618 | 424 |
text \<open>The proof method is now defined as before, but we append the |
425 |
explicit arguments and the rules from the context.\<close> |
|
39847 | 426 |
|
57340 | 427 |
method_setup my_simp' = |
428 |
\<open>Attrib.thms >> (fn thms => fn ctxt => |
|
57946
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
429 |
let |
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
430 |
val my_simps = Named_Theorems.get ctxt @{named_theorems my_simp} |
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
431 |
in |
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
432 |
SIMPLE_METHOD' (fn i => |
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
433 |
CHANGED (asm_full_simp_tac |
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
434 |
(put_simpset HOL_basic_ss ctxt |
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
435 |
addsimps (thms @ my_simps)) i)) |
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
436 |
end)\<close> |
57340 | 437 |
"rewrite subgoal by given rules and my_simp rules from the context" |
39847 | 438 |
|
58618 | 439 |
text \<open> |
61416 | 440 |
\<^medskip> |
441 |
Method @{method my_simp'} can be used in Isar proofs |
|
39848 | 442 |
like this: |
58618 | 443 |
\<close> |
39847 | 444 |
|
40964 | 445 |
notepad |
446 |
begin |
|
57342 | 447 |
fix a b c :: 'a |
39847 | 448 |
assume [my_simp]: "a \<equiv> b" |
449 |
assume [my_simp]: "b \<equiv> c" |
|
39848 | 450 |
have "a \<equiv> c" by my_simp' |
40964 | 451 |
end |
39847 | 452 |
|
61416 | 453 |
text \<open> |
454 |
\<^medskip> |
|
455 |
The @{method my_simp} variants defined above are |
|
39851 | 456 |
``simple'' methods, i.e.\ the goal facts are merely inserted as goal |
457 |
premises by the @{ML SIMPLE_METHOD'} or @{ML SIMPLE_METHOD} wrapper. |
|
458 |
For proof methods that are similar to the standard collection of |
|
40126 | 459 |
@{method simp}, @{method blast}, @{method fast}, @{method auto} |
460 |
there is little more that can be done. |
|
39847 | 461 |
|
462 |
Note that using the primary goal facts in the same manner as the |
|
39848 | 463 |
method arguments obtained via concrete syntax or the context does |
464 |
not meet the requirement of ``strong emphasis on facts'' of regular |
|
465 |
proof methods, because rewrite rules as used above can be easily |
|
61493 | 466 |
ignored. A proof text ``@{command using}~\<open>foo\<close>~@{command |
467 |
"by"}~\<open>my_simp\<close>'' where \<open>foo\<close> is not used would |
|
39848 | 468 |
deceive the reader. |
39847 | 469 |
|
61416 | 470 |
\<^medskip> |
471 |
The technical treatment of rules from the context requires |
|
39847 | 472 |
further attention. Above we rebuild a fresh @{ML_type simpset} from |
61477 | 473 |
the arguments and \<^emph>\<open>all\<close> rules retrieved from the context on |
39848 | 474 |
every invocation of the method. This does not scale to really large |
475 |
collections of rules, which easily emerges in the context of a big |
|
476 |
theory library, for example. |
|
39847 | 477 |
|
39848 | 478 |
This is an inherent limitation of the simplistic rule management via |
57946
6a26aa5fa65e
updated documentation concerning 'named_theorems';
wenzelm
parents:
57342
diff
changeset
|
479 |
@{command named_theorems}, because it lacks tool-specific |
39847 | 480 |
storage and retrieval. More realistic applications require |
481 |
efficient index-structures that organize theorems in a customized |
|
482 |
manner, such as a discrimination net that is indexed by the |
|
39848 | 483 |
left-hand sides of rewrite rules. For variations on the Simplifier, |
484 |
re-use of the existing type @{ML_type simpset} is adequate, but |
|
40126 | 485 |
scalability would require it be maintained statically within the |
58618 | 486 |
context data, not dynamically on each tool invocation.\<close> |
39847 | 487 |
|
29759 | 488 |
|
58618 | 489 |
section \<open>Attributes \label{sec:attributes}\<close> |
20472 | 490 |
|
61493 | 491 |
text \<open>An \<^emph>\<open>attribute\<close> is a function \<open>context \<times> thm \<rightarrow> |
492 |
context \<times> thm\<close>, which means both a (generic) context and a theorem |
|
45414 | 493 |
can be modified simultaneously. In practice this mixed form is very |
61477 | 494 |
rare, instead attributes are presented either as \<^emph>\<open>declaration |
61493 | 495 |
attribute:\<close> \<open>thm \<rightarrow> context \<rightarrow> context\<close> or \<^emph>\<open>rule |
496 |
attribute:\<close> \<open>context \<rightarrow> thm \<rightarrow> thm\<close>. |
|
39849 | 497 |
|
498 |
Attributes can have additional arguments via concrete syntax. There |
|
499 |
is a collection of context-sensitive parsers for various logical |
|
500 |
entities (types, terms, theorems). These already take care of |
|
501 |
applying morphisms to the arguments when attribute expressions are |
|
502 |
moved into a different context (see also \secref{sec:morphisms}). |
|
503 |
||
504 |
When implementing declaration attributes, it is important to operate |
|
505 |
exactly on the variant of the generic context that is provided by |
|
506 |
the system, which is either global theory context or local proof |
|
507 |
context. In particular, the background theory of a local context |
|
58618 | 508 |
must not be modified in this situation!\<close> |
39849 | 509 |
|
58618 | 510 |
text %mlref \<open> |
39849 | 511 |
\begin{mldecls} |
45414 | 512 |
@{index_ML_type attribute} \\ |
39849 | 513 |
@{index_ML Thm.rule_attribute: "(Context.generic -> thm -> thm) -> attribute"} \\ |
514 |
@{index_ML Thm.declaration_attribute: " |
|
515 |
(thm -> Context.generic -> Context.generic) -> attribute"} \\ |
|
516 |
@{index_ML Attrib.setup: "binding -> attribute context_parser -> |
|
517 |
string -> theory -> theory"} \\ |
|
518 |
\end{mldecls} |
|
519 |
||
61439 | 520 |
\<^descr> Type @{ML_type attribute} represents attributes as concrete |
39864 | 521 |
type alias. |
39849 | 522 |
|
61493 | 523 |
\<^descr> @{ML Thm.rule_attribute}~\<open>(fn context => rule)\<close> wraps |
39849 | 524 |
a context-dependent rule (mapping on @{ML_type thm}) as attribute. |
525 |
||
61493 | 526 |
\<^descr> @{ML Thm.declaration_attribute}~\<open>(fn thm => decl)\<close> |
39849 | 527 |
wraps a theorem-dependent declaration (mapping on @{ML_type |
528 |
Context.generic}) as attribute. |
|
529 |
||
61493 | 530 |
\<^descr> @{ML Attrib.setup}~\<open>name parser description\<close> provides |
39849 | 531 |
the functionality of the Isar command @{command attribute_setup} as |
532 |
ML function. |
|
58618 | 533 |
\<close> |
39849 | 534 |
|
58618 | 535 |
text %mlantiq \<open> |
45592 | 536 |
\begin{matharray}{rcl} |
61493 | 537 |
@{ML_antiquotation_def attributes} & : & \<open>ML_antiquotation\<close> \\ |
45592 | 538 |
\end{matharray} |
539 |
||
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
51717
diff
changeset
|
540 |
@{rail \<open> |
45592 | 541 |
@@{ML_antiquotation attributes} attributes |
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
51717
diff
changeset
|
542 |
\<close>} |
45592 | 543 |
|
61493 | 544 |
\<^descr> \<open>@{attributes [\<dots>]}\<close> embeds attribute source |
45592 | 545 |
representation into the ML text, which is particularly useful with |
546 |
declarations like @{ML Local_Theory.note}. Attribute names are |
|
547 |
internalized at compile time, but the source is unevaluated. This |
|
548 |
means attributes with formal arguments (types, terms, theorems) may |
|
549 |
be subject to odd effects of dynamic scoping! |
|
58618 | 550 |
\<close> |
45592 | 551 |
|
58618 | 552 |
text %mlex \<open>See also @{command attribute_setup} in |
553 |
@{cite "isabelle-isar-ref"} which includes some abstract examples.\<close> |
|
30272 | 554 |
|
20472 | 555 |
end |