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