2
+ − 1
theory FirstSteps
25
+ − 2
imports Base
2
+ − 3
begin
+ − 4
+ − 5
chapter {* First Steps *}
+ − 6
42
+ − 7
text {*
+ − 8
54
+ − 9
Isabelle programming is done in ML. Just like lemmas and proofs, ML-code
89
+ − 10
in Isabelle is part of a theory. If you want to follow the code given in
54
+ − 11
this chapter, we assume you are working inside the theory starting with
2
+ − 12
6
+ − 13
\begin{center}
5
+ − 14
\begin{tabular}{@ {}l}
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 15
\isacommand{theory} FirstSteps\\
5
+ − 16
\isacommand{imports} Main\\
+ − 17
\isacommand{begin}\\
6
+ − 18
\ldots
5
+ − 19
\end{tabular}
6
+ − 20
\end{center}
157
+ − 21
+ − 22
We also generally assume you are working with HOL. The given examples might
193
+ − 23
need to be adapted if you work in a different logic.
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 24
*}
5
+ − 25
20
+ − 26
section {* Including ML-Code *}
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 27
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 28
text {*
5
+ − 29
The easiest and quickest way to include code in a theory is
89
+ − 30
by using the \isacommand{ML}-command. For example:
2
+ − 31
75
+ − 32
\begin{isabelle}
+ − 33
\begin{graybox}
85
+ − 34
\isacommand{ML}~@{text "\<verbopen>"}\isanewline
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 35
\hspace{5mm}@{ML "3 + 4"}\isanewline
85
+ − 36
@{text "\<verbclose>"}\isanewline
+ − 37
@{text "> 7"}\smallskip
75
+ − 38
\end{graybox}
+ − 39
\end{isabelle}
2
+ − 40
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 41
Like normal Isabelle proof scripts, \isacommand{ML}-commands can be
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 42
evaluated by using the advance and undo buttons of your Isabelle
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 43
environment. The code inside the \isacommand{ML}-command can also contain
193
+ − 44
value and function bindings, for example
+ − 45
*}
+ − 46
+ − 47
ML %gray {*
+ − 48
val r = ref 0
+ − 49
fun f n = n + 1
+ − 50
*}
+ − 51
+ − 52
text {*
+ − 53
and even those can be undone when the proof
+ − 54
script is retracted. As mentioned in the Introduction, we will drop the
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 55
\isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"} scaffolding whenever we
107
+ − 56
show code. The lines prefixed with @{text [quotes] ">"} are not part of the
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 57
code, rather they indicate what the response is when the code is evaluated.
10
+ − 58
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 59
Once a portion of code is relatively stable, you usually want to export it
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 60
to a separate ML-file. Such files can then be included in a theory by using
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 61
the \isacommand{uses}-command in the header of the theory, like:
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 62
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 63
\begin{center}
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 64
\begin{tabular}{@ {}l}
54
+ − 65
\isacommand{theory} FirstSteps\\
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 66
\isacommand{imports} Main\\
58
+ − 67
\isacommand{uses} @{text "\"file_to_be_included.ML\""} @{text "\<dots>"}\\
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 68
\isacommand{begin}\\
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 69
\ldots
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 70
\end{tabular}
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 71
\end{center}
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 72
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 73
*}
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 74
126
+ − 75
section {* Debugging and Printing\label{sec:printing} *}
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 76
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 77
text {*
13
+ − 78
50
+ − 79
During development you might find it necessary to inspect some data
10
+ − 80
in your code. This can be done in a ``quick-and-dirty'' fashion using
12
+ − 81
the function @{ML "warning"}. For example
10
+ − 82
161
+ − 83
@{ML_response_fake [display,gray] "warning \"any string\"" "\"any string\""}
10
+ − 84
58
+ − 85
will print out @{text [quotes] "any string"} inside the response buffer
+ − 86
of Isabelle. This function expects a string as argument. If you develop under PolyML,
50
+ − 87
then there is a convenient, though again ``quick-and-dirty'', method for
107
+ − 88
converting values into strings, namely the function @{ML makestring}:
10
+ − 89
161
+ − 90
@{ML_response_fake [display,gray] "warning (makestring 1)" "\"1\""}
12
+ − 91
196
+ − 92
However, @{ML makestring} only works if the type of what is converted is monomorphic
78
+ − 93
and not a function.
12
+ − 94
52
+ − 95
The function @{ML "warning"} should only be used for testing purposes, because any
+ − 96
output this function generates will be overwritten as soon as an error is
50
+ − 97
raised. For printing anything more serious and elaborate, the
54
+ − 98
function @{ML tracing} is more appropriate. This function writes all output into
89
+ − 99
a separate tracing buffer. For example:
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 100
161
+ − 101
@{ML_response_fake [display,gray] "tracing \"foo\"" "\"foo\""}
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 102
58
+ − 103
It is also possible to redirect the ``channel'' where the string @{text "foo"} is
196
+ − 104
printed to a separate file, e.g., to prevent ProofGeneral from choking on massive
107
+ − 105
amounts of trace output. This redirection can be achieved with the code:
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 106
*}
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 107
69
+ − 108
ML{*val strip_specials =
42
+ − 109
let
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 110
fun strip ("\^A" :: _ :: cs) = strip cs
42
+ − 111
| strip (c :: cs) = c :: strip cs
+ − 112
| strip [] = [];
+ − 113
in implode o strip o explode end;
+ − 114
+ − 115
fun redirect_tracing stream =
+ − 116
Output.tracing_fn := (fn s =>
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 117
(TextIO.output (stream, (strip_specials s));
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 118
TextIO.output (stream, "\n");
69
+ − 119
TextIO.flushOut stream)) *}
14
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 120
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 121
text {*
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 122
Calling @{ML "redirect_tracing"} with @{ML "(TextIO.openOut \"foo.bar\")"}
58
+ − 123
will cause that all tracing information is printed into the file @{text "foo.bar"}.
75
+ − 124
107
+ − 125
You can print out error messages with the function @{ML error}; for example:
75
+ − 126
122
+ − 127
@{ML_response_fake [display,gray] "if 0=1 then true else (error \"foo\")"
+ − 128
"Exception- ERROR \"foo\" raised
+ − 129
At command \"ML\"."}
75
+ − 130
192
+ − 131
(FIXME @{ML Toplevel.debug} @{ML Toplevel.profiling})
+ − 132
*}
+ − 133
+ − 134
(*
207
+ − 135
ML {* reset Toplevel.debug *}
+ − 136
+ − 137
ML {* fun dodgy_fun () = (raise TYPE ("",[],[]); 1) *}
193
+ − 138
207
+ − 139
ML {* fun innocent () = dodgy_fun () *}
+ − 140
ML {* exception_trace (fn () => cterm_of @{theory} (Bound 0)) *}
+ − 141
ML {* exception_trace (fn () => innocent ()) *}
192
+ − 142
207
+ − 143
ML {* Toplevel.program (fn () => cterm_of @{theory} (Bound 0)) *}
192
+ − 144
207
+ − 145
ML {* Toplevel.program (fn () => innocent ()) *}
192
+ − 146
*)
+ − 147
+ − 148
text {*
126
+ − 149
Most often you want to inspect data of type @{ML_type term}, @{ML_type cterm}
+ − 150
or @{ML_type thm}. Isabelle contains elaborate pretty-printing functions for printing them,
+ − 151
but for quick-and-dirty solutions they are far too unwieldy. A simple way to transform
+ − 152
a term into a string is to use the function @{ML Syntax.string_of_term}.
+ − 153
+ − 154
@{ML_response_fake [display,gray]
+ − 155
"Syntax.string_of_term @{context} @{term \"1::nat\"}"
+ − 156
"\"\\^E\\^Fterm\\^E\\^E\\^Fconst\\^Fname=HOL.one_class.one\\^E1\\^E\\^F\\^E\\^E\\^F\\^E\""}
+ − 157
+ − 158
This produces a string with some additional information encoded in it. The string
+ − 159
can be properly printed by using the function @{ML warning}.
+ − 160
+ − 161
@{ML_response_fake [display,gray]
+ − 162
"warning (Syntax.string_of_term @{context} @{term \"1::nat\"})"
+ − 163
"\"1\""}
+ − 164
+ − 165
A @{ML_type cterm} can be transformed into a string by the following function.
+ − 166
*}
+ − 167
+ − 168
ML{*fun str_of_cterm ctxt t =
+ − 169
Syntax.string_of_term ctxt (term_of t)*}
+ − 170
+ − 171
text {*
149
+ − 172
In this example the function @{ML term_of} extracts the @{ML_type term} from
+ − 173
a @{ML_type cterm}. If there are more than one @{ML_type cterm}s to be
+ − 174
printed, you can use the function @{ML commas} to separate them.
126
+ − 175
*}
+ − 176
+ − 177
ML{*fun str_of_cterms ctxt ts =
+ − 178
commas (map (str_of_cterm ctxt) ts)*}
+ − 179
+ − 180
text {*
+ − 181
The easiest way to get the string of a theorem is to transform it
190
+ − 182
into a @{ML_type cterm} using the function @{ML crep_thm}.
+ − 183
*}
+ − 184
194
+ − 185
ML{*fun str_of_thm ctxt thm =
190
+ − 186
str_of_cterm ctxt (#prop (crep_thm thm))*}
+ − 187
+ − 188
text {*
+ − 189
Theorems also include schematic variables, such as @{text "?P"},
+ − 190
@{text "?Q"} and so on.
+ − 191
+ − 192
@{ML_response_fake [display, gray]
194
+ − 193
"warning (str_of_thm @{context} @{thm conjI})"
190
+ − 194
"\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q"}
+ − 195
+ − 196
In order to improve the readability of theorems we convert
158
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 197
these schematic variables into free variables using the
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 198
function @{ML Variable.import_thms}.
126
+ − 199
*}
+ − 200
158
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 201
ML{*fun no_vars ctxt thm =
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 202
let
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 203
val ((_, [thm']), _) = Variable.import_thms true [thm] ctxt
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 204
in
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 205
thm'
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 206
end
d7944bdf7b3f
removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 207
194
+ − 208
fun str_of_thm_no_vars ctxt thm =
171
+ − 209
str_of_cterm ctxt (#prop (crep_thm (no_vars ctxt thm)))*}
126
+ − 210
+ − 211
text {*
207
+ − 212
Theorem @{thm [source] conjI} is now printed as follows:
190
+ − 213
+ − 214
@{ML_response_fake [display, gray]
194
+ − 215
"warning (str_of_thm_no_vars @{context} @{thm conjI})"
196
+ − 216
"\<lbrakk>P; Q\<rbrakk> \<Longrightarrow> P \<and> Q"}
190
+ − 217
126
+ − 218
Again the function @{ML commas} helps with printing more than one theorem.
+ − 219
*}
+ − 220
+ − 221
ML{*fun str_of_thms ctxt thms =
190
+ − 222
commas (map (str_of_thm ctxt) thms)
+ − 223
194
+ − 224
fun str_of_thms_no_vars ctxt thms =
+ − 225
commas (map (str_of_thm_no_vars ctxt) thms) *}
126
+ − 226
+ − 227
section {* Combinators\label{sec:combinators} *}
+ − 228
+ − 229
text {*
131
+ − 230
For beginners perhaps the most puzzling parts in the existing code of Isabelle are
126
+ − 231
the combinators. At first they seem to greatly obstruct the
+ − 232
comprehension of the code, but after getting familiar with them, they
+ − 233
actually ease the understanding and also the programming.
+ − 234
+ − 235
The simplest combinator is @{ML I}, which is just the identity function defined as
+ − 236
*}
+ − 237
+ − 238
ML{*fun I x = x*}
+ − 239
+ − 240
text {* Another simple combinator is @{ML K}, defined as *}
+ − 241
+ − 242
ML{*fun K x = fn _ => x*}
+ − 243
+ − 244
text {*
+ − 245
@{ML K} ``wraps'' a function around the argument @{text "x"}. However, this
+ − 246
function ignores its argument. As a result, @{ML K} defines a constant function
+ − 247
always returning @{text x}.
+ − 248
+ − 249
The next combinator is reverse application, @{ML "|>"}, defined as:
+ − 250
*}
+ − 251
+ − 252
ML{*fun x |> f = f x*}
+ − 253
+ − 254
text {* While just syntactic sugar for the usual function application,
+ − 255
the purpose of this combinator is to implement functions in a
+ − 256
``waterfall fashion''. Consider for example the function *}
+ − 257
+ − 258
ML %linenosgray{*fun inc_by_five x =
+ − 259
x |> (fn x => x + 1)
+ − 260
|> (fn x => (x, x))
+ − 261
|> fst
+ − 262
|> (fn x => x + 4)*}
+ − 263
+ − 264
text {*
196
+ − 265
which increments its argument @{text x} by 5. It proceeds by first incrementing
126
+ − 266
the argument by 1 (Line 2); then storing the result in a pair (Line 3); taking
+ − 267
the first component of the pair (Line 4) and finally incrementing the first
+ − 268
component by 4 (Line 5). This kind of cascading manipulations of values is quite
+ − 269
common when dealing with theories (for example by adding a definition, followed by
+ − 270
lemmas and so on). The reverse application allows you to read what happens in
+ − 271
a top-down manner. This kind of coding should also be familiar,
149
+ − 272
if you have been exposed to Haskell's do-notation. Writing the function @{ML inc_by_five} using
126
+ − 273
the reverse application is much clearer than writing
+ − 274
*}
+ − 275
+ − 276
ML{*fun inc_by_five x = fst ((fn x => (x, x)) (x + 1)) + 4*}
+ − 277
+ − 278
text {* or *}
+ − 279
+ − 280
ML{*fun inc_by_five x =
+ − 281
((fn x => x + 4) o fst o (fn x => (x, x)) o (fn x => x + 1)) x*}
+ − 282
+ − 283
text {* and typographically more economical than *}
+ − 284
+ − 285
ML{*fun inc_by_five x =
+ − 286
let val y1 = x + 1
+ − 287
val y2 = (y1, y1)
+ − 288
val y3 = fst y2
+ − 289
val y4 = y3 + 4
+ − 290
in y4 end*}
+ − 291
+ − 292
text {*
+ − 293
Another reason why the let-bindings in the code above are better to be
+ − 294
avoided: it is more than easy to get the intermediate values wrong, not to
+ − 295
mention the nightmares the maintenance of this code causes!
+ − 296
215
+ − 297
In Isabelle, a ``real world'' example for a function written in
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 298
the waterfall fashion might be the following code:
177
+ − 299
*}
126
+ − 300
193
+ − 301
ML %linenosgray{*fun apply_fresh_args f ctxt =
+ − 302
f |> fastype_of
+ − 303
|> binder_types
+ − 304
|> map (pair "z")
+ − 305
|> Variable.variant_frees ctxt [f]
+ − 306
|> map Free
+ − 307
|> (curry list_comb) f *}
126
+ − 308
177
+ − 309
text {*
194
+ − 310
This code extracts the argument types of a given function @{text "f"} and then generates
193
+ − 311
for each argument type a distinct variable; finally it applies the generated
194
+ − 312
variables to the function. For example:
183
+ − 313
+ − 314
@{ML_response_fake [display,gray]
+ − 315
"apply_fresh_args @{term \"P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool\"} @{context}
+ − 316
|> Syntax.string_of_term @{context}
+ − 317
|> warning"
+ − 318
"P z za zb"}
177
+ − 319
184
+ − 320
You can read off this behaviour from how @{ML apply_fresh_args} is
183
+ − 321
coded: in Line 2, the function @{ML fastype_of} calculates the type of the
193
+ − 322
function; @{ML binder_types} in the next line produces the list of argument
184
+ − 323
types (in the case above the list @{text "[nat, int, unit]"}); Line 4
+ − 324
pairs up each type with the string @{text "z"}; the
183
+ − 325
function @{ML variant_frees in Variable} generates for each @{text "z"} a
193
+ − 326
unique name avoiding the given @{text f}; the list of name-type pairs is turned
184
+ − 327
into a list of variable terms in Line 6, which in the last line is applied
194
+ − 328
by the function @{ML list_comb} to the function. In this last step we have to
+ − 329
use the function @{ML curry}, because @{ML list_comb} expects the function and the
184
+ − 330
variables list as a pair.
177
+ − 331
+ − 332
The combinator @{ML "#>"} is the reverse function composition. It can be
+ − 333
used to define the following function
126
+ − 334
*}
+ − 335
+ − 336
ML{*val inc_by_six =
+ − 337
(fn x => x + 1)
+ − 338
#> (fn x => x + 2)
+ − 339
#> (fn x => x + 3)*}
+ − 340
+ − 341
text {*
+ − 342
which is the function composed of first the increment-by-one function and then
+ − 343
increment-by-two, followed by increment-by-three. Again, the reverse function
+ − 344
composition allows you to read the code top-down.
+ − 345
+ − 346
The remaining combinators described in this section add convenience for the
+ − 347
``waterfall method'' of writing functions. The combinator @{ML tap} allows
+ − 348
you to get hold of an intermediate result (to do some side-calculations for
+ − 349
instance). The function
+ − 350
+ − 351
*}
+ − 352
+ − 353
ML %linenosgray{*fun inc_by_three x =
+ − 354
x |> (fn x => x + 1)
+ − 355
|> tap (fn x => tracing (makestring x))
+ − 356
|> (fn x => x + 2)*}
+ − 357
+ − 358
text {*
+ − 359
increments the argument first by @{text "1"} and then by @{text "2"}. In the
+ − 360
middle (Line 3), however, it uses @{ML tap} for printing the ``plus-one''
+ − 361
intermediate result inside the tracing buffer. The function @{ML tap} can
+ − 362
only be used for side-calculations, because any value that is computed
+ − 363
cannot be merged back into the ``main waterfall''. To do this, you can use
+ − 364
the next combinator.
+ − 365
196
+ − 366
The combinator @{ML "`"} (a backtick) is similar to @{ML tap}, but applies a
+ − 367
function to the value and returns the result together with the value (as a
+ − 368
pair). For example the function
126
+ − 369
*}
+ − 370
+ − 371
ML{*fun inc_as_pair x =
+ − 372
x |> `(fn x => x + 1)
+ − 373
|> (fn (x, y) => (x, y + 1))*}
+ − 374
+ − 375
text {*
+ − 376
takes @{text x} as argument, and then increments @{text x}, but also keeps
+ − 377
@{text x}. The intermediate result is therefore the pair @{ML "(x + 1, x)"
+ − 378
for x}. After that, the function increments the right-hand component of the
+ − 379
pair. So finally the result will be @{ML "(x + 1, x + 1)" for x}.
+ − 380
+ − 381
The combinators @{ML "|>>"} and @{ML "||>"} are defined for
+ − 382
functions manipulating pairs. The first applies the function to
+ − 383
the first component of the pair, defined as
+ − 384
*}
+ − 385
+ − 386
ML{*fun (x, y) |>> f = (f x, y)*}
+ − 387
+ − 388
text {*
+ − 389
and the second combinator to the second component, defined as
+ − 390
*}
+ − 391
+ − 392
ML{*fun (x, y) ||> f = (x, f y)*}
+ − 393
+ − 394
text {*
+ − 395
With the combinator @{ML "|->"} you can re-combine the elements from a pair.
+ − 396
This combinator is defined as
+ − 397
*}
+ − 398
+ − 399
ML{*fun (x, y) |-> f = f x y*}
+ − 400
215
+ − 401
text {*
+ − 402
and can be used to write the following roundabout version
126
+ − 403
of the @{text double} function:
+ − 404
*}
+ − 405
+ − 406
ML{*fun double x =
+ − 407
x |> (fn x => (x, x))
+ − 408
|-> (fn x => fn y => x + y)*}
+ − 409
215
+ − 410
text {*
+ − 411
The combinator @{ML ||>>} plays a central rôle whenever your task is to update a
+ − 412
theory and the update also produces a side-result (for example a theorem). Functions
+ − 413
for such tasks return a pair whose second component is the theory and the fist
+ − 414
component is the side-result. Using @{ML ||>>}, you can do conveniently the update
+ − 415
and also accumulate the side-results. Considder the following simple function.
+ − 416
*}
+ − 417
+ − 418
ML %linenosgray{*fun acc_incs x =
+ − 419
x |> (fn x => ("", x))
+ − 420
||>> (fn x => (x, x + 1))
+ − 421
||>> (fn x => (x, x + 1))
+ − 422
||>> (fn x => (x, x + 1))*}
+ − 423
+ − 424
text {*
+ − 425
The purpose of Line 2 is to just pair up the argument with a dummy value (since
+ − 426
@{ML "||>>"} operates on pairs). Each of the next three lines just increment
+ − 427
the value by one, but also nest the intrermediate results to the left. For example
+ − 428
+ − 429
@{ML_response [display,gray]
+ − 430
"acc_incs 1"
+ − 431
"((((\"\", 1), 2), 3), 4)"}
+ − 432
+ − 433
You can continue this chain with:
+ − 434
+ − 435
@{ML_response [display,gray]
+ − 436
"acc_incs 1 ||>> (fn x => (x, x + 2))"
+ − 437
"(((((\"\", 1), 2), 3), 4), 6)"}
+ − 438
+ − 439
(FIXME: maybe give a ``real world'' example)
+ − 440
*}
+ − 441
126
+ − 442
text {*
196
+ − 443
Recall that @{ML "|>"} is the reverse function application. Recall also that
+ − 444
the related
126
+ − 445
reverse function composition is @{ML "#>"}. In fact all the combinators @{ML "|->"},
215
+ − 446
@{ML "|>>"} , @{ML "||>"} and @{ML "||>>"} described above have related combinators for
+ − 447
function composition, namely @{ML "#->"}, @{ML "#>>"}, @{ML "##>"} and @{ML "##>>"}.
+ − 448
Using @{ML "#->"}, for example, the function @{text double} can also be written as:
126
+ − 449
*}
+ − 450
+ − 451
ML{*val double =
+ − 452
(fn x => (x, x))
+ − 453
#-> (fn x => fn y => x + y)*}
+ − 454
+ − 455
text {*
+ − 456
+ − 457
(FIXME: find a good exercise for combinators)
127
+ − 458
+ − 459
\begin{readmore}
196
+ − 460
The most frequently used combinators are defined in the files @{ML_file
+ − 461
"Pure/library.ML"}
127
+ − 462
and @{ML_file "Pure/General/basics.ML"}. Also \isccite{sec:ML-linear-trans}
+ − 463
contains further information about combinators.
+ − 464
\end{readmore}
126
+ − 465
15
+ − 466
*}
+ − 467
10
+ − 468
2
+ − 469
section {* Antiquotations *}
+ − 470
+ − 471
text {*
49
+ − 472
The main advantage of embedding all code in a theory is that the code can
58
+ − 473
contain references to entities defined on the logical level of Isabelle. By
+ − 474
this we mean definitions, theorems, terms and so on. This kind of reference is
+ − 475
realised with antiquotations. For example, one can print out the name of the current
49
+ − 476
theory by typing
+ − 477
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 478
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 479
@{ML_response [display,gray] "Context.theory_name @{theory}" "\"FirstSteps\""}
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 480
5
+ − 481
where @{text "@{theory}"} is an antiquotation that is substituted with the
49
+ − 482
current theory (remember that we assumed we are inside the theory
89
+ − 483
@{text FirstSteps}). The name of this theory can be extracted using
49
+ − 484
the function @{ML "Context.theory_name"}.
5
+ − 485
89
+ − 486
Note, however, that antiquotations are statically linked, that is their value is
12
+ − 487
determined at ``compile-time'', not ``run-time''. For example the function
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 488
*}
5
+ − 489
69
+ − 490
ML{*fun not_current_thyname () = Context.theory_name @{theory} *}
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 491
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 492
text {*
2
+ − 493
89
+ − 494
does \emph{not} return the name of the current theory, if it is run in a
5
+ − 495
different theory. Instead, the code above defines the constant function
58
+ − 496
that always returns the string @{text [quotes] "FirstSteps"}, no matter where the
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 497
function is called. Operationally speaking, the antiquotation @{text "@{theory}"} is
5
+ − 498
\emph{not} replaced with code that will look up the current theory in
+ − 499
some data structure and return it. Instead, it is literally
+ − 500
replaced with the value representing the theory name.
2
+ − 501
132
+ − 502
In a similar way you can use antiquotations to refer to proved theorems:
133
+ − 503
@{text "@{thm \<dots>}"} for a single theorem
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 504
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 505
@{ML_response_fake [display,gray] "@{thm allI}" "(\<And>x. ?P x) \<Longrightarrow> \<forall>x. ?P x"}
75
+ − 506
133
+ − 507
and @{text "@{thms \<dots>}"} for more than one
132
+ − 508
+ − 509
@{ML_response_fake [display,gray] "@{thms conj_ac}"
+ − 510
"(?P \<and> ?Q) = (?Q \<and> ?P)
+ − 511
(?P \<and> ?Q \<and> ?R) = (?Q \<and> ?P \<and> ?R)
+ − 512
((?P \<and> ?Q) \<and> ?R) = (?P \<and> ?Q \<and> ?R)"}
+ − 513
149
+ − 514
You can also refer to the current simpset. To illustrate this we implement the
132
+ − 515
function that extracts the theorem names stored in a simpset.
131
+ − 516
*}
75
+ − 517
149
+ − 518
ML{*fun get_thm_names_from_ss simpset =
131
+ − 519
let
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 520
val {simps,...} = MetaSimplifier.dest_ss simpset
70
+ − 521
in
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 522
map #1 simps
131
+ − 523
end*}
54
+ − 524
131
+ − 525
text {*
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 526
The function @{ML dest_ss in MetaSimplifier} returns a record containing all
193
+ − 527
information stored in the simpset, but we are only interested in the names of the
184
+ − 528
simp-rules. So now you can feed in the current simpset into this function.
193
+ − 529
The current simpset can be referred to using the antiquotation @{text "@{simpset}"}.
81
+ − 530
131
+ − 531
@{ML_response_fake [display,gray]
149
+ − 532
"get_thm_names_from_ss @{simpset}"
+ − 533
"[\"Nat.of_nat_eq_id\", \"Int.of_int_eq_id\", \"Nat.One_nat_def\", \<dots>]"}
10
+ − 534
196
+ − 535
Again, this way of referencing simpsets makes you independent from additions
156
+ − 536
of lemmas to the simpset by the user that potentially cause loops.
+ − 537
192
+ − 538
On the ML-level of Isabelle, you often have to work with qualified names;
196
+ − 539
these are strings with some additional information, such as positional information
192
+ − 540
and qualifiers. Such bindings can be generated with the antiquotation
196
+ − 541
@{text "@{binding \<dots>}"}.
192
+ − 542
+ − 543
@{ML_response [display,gray]
+ − 544
"@{binding \"name\"}"
+ − 545
"name"}
+ − 546
196
+ − 547
An example where a binding is needed is the function @{ML define in
+ − 548
LocalTheory}. Below, this function is used to define the constant @{term
+ − 549
"TrueConj"} as the conjunction
192
+ − 550
@{term "True \<and> True"}.
+ − 551
*}
+ − 552
+ − 553
local_setup %gray {*
+ − 554
snd o LocalTheory.define Thm.internalK
+ − 555
((@{binding "TrueConj"}, NoSyn),
+ − 556
(Attrib.empty_binding, @{term "True \<and> True"})) *}
+ − 557
193
+ − 558
text {*
+ − 559
Now querying the definition you obtain:
+ − 560
+ − 561
\begin{isabelle}
+ − 562
\isacommand{thm}~@{text "TrueConj_def"}\\
+ − 563
@{text "> "}@{thm TrueConj_def}
+ − 564
\end{isabelle}
+ − 565
194
+ − 566
(FIXME give a better example why bindings are important; maybe
+ − 567
give a pointer to \isacommand{local\_setup})
193
+ − 568
196
+ − 569
While antiquotations have many applications, they were originally introduced
+ − 570
in order to avoid explicit bindings of theorems such as:
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 571
*}
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 572
69
+ − 573
ML{*val allI = thm "allI" *}
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 574
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 575
text {*
196
+ − 576
Such bindings are difficult to maintain and can be overwritten by the
+ − 577
user accidentally. This often broke Isabelle
49
+ − 578
packages. Antiquotations solve this problem, since they are ``linked''
89
+ − 579
statically at compile-time. However, this static linkage also limits their
201
+ − 580
usefulness in cases where data needs to be built up dynamically. In the
196
+ − 581
course of this chapter you will learn more about antiquotations:
122
+ − 582
they can simplify Isabelle programming since one can directly access all
196
+ − 583
kinds of logical elements from the ML-level.
2
+ − 584
*}
+ − 585
15
+ − 586
section {* Terms and Types *}
2
+ − 587
+ − 588
text {*
197
+ − 589
One way to construct Isabelle terms, is by using the antiquotation
89
+ − 590
\mbox{@{text "@{term \<dots>}"}}. For example:
2
+ − 591
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 592
@{ML_response [display,gray]
75
+ − 593
"@{term \"(a::nat) + b = c\"}"
+ − 594
"Const (\"op =\", \<dots>) $
149
+ − 595
(Const (\"HOL.plus_class.plus\", \<dots>) $ \<dots> $ \<dots>) $ \<dots>"}
2
+ − 596
207
+ − 597
will show the term @{term "(a::nat) + b = c"}, but printed using the internal
198
+ − 598
representation corresponding to the data type @{ML_type "term"}.
2
+ − 599
197
+ − 600
This internal representation uses the usual de Bruijn index mechanism---where
+ − 601
bound variables are represented by the constructor @{ML Bound}. The index in
+ − 602
@{ML Bound} refers to the number of Abstractions (@{ML Abs}) we have to skip
+ − 603
until we hit the @{ML Abs} that binds the corresponding variable. Note that
+ − 604
the names of bound variables are kept at abstractions for printing purposes,
+ − 605
and so should be treated only as ``comments''. Application in Isabelle is
+ − 606
realised with the term-constructor @{ML $}.
10
+ − 607
2
+ − 608
\begin{readmore}
13
+ − 609
Terms are described in detail in \isccite{sec:terms}. Their
78
+ − 610
definition and many useful operations are implemented in @{ML_file "Pure/term.ML"}.
2
+ − 611
\end{readmore}
193
+ − 612
+ − 613
Constructing terms via antiquotations has the advantage that only typable
+ − 614
terms can be constructed. For example
2
+ − 615
193
+ − 616
@{ML_response_fake_both [display,gray]
+ − 617
"@{term \"(x::nat) x\"}"
+ − 618
"Type unification failed \<dots>"}
+ − 619
194
+ − 620
raises a typing error, while it perfectly ok to construct the term
193
+ − 621
+ − 622
@{ML [display,gray] "Free (\"x\", @{typ nat}) $ Free (\"x\", @{typ nat})"}
+ − 623
+ − 624
with the raw ML-constructors.
13
+ − 625
Sometimes the internal representation of terms can be surprisingly different
157
+ − 626
from what you see at the user-level, because the layers of
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 627
parsing/type-checking/pretty printing can be quite elaborate.
2
+ − 628
10
+ − 629
\begin{exercise}
2
+ − 630
Look at the internal term representation of the following terms, and
89
+ − 631
find out why they are represented like this:
2
+ − 632
+ − 633
\begin{itemize}
+ − 634
\item @{term "case x of 0 \<Rightarrow> 0 | Suc y \<Rightarrow> y"}
+ − 635
\item @{term "\<lambda>(x,y). P y x"}
+ − 636
\item @{term "{ [x::int] | x. x \<le> -2 }"}
+ − 637
\end{itemize}
+ − 638
+ − 639
Hint: The third term is already quite big, and the pretty printer
+ − 640
may omit parts of it by default. If you want to see all of it, you
122
+ − 641
can use the following ML-function to set the printing depth to a higher
+ − 642
value:
12
+ − 643
75
+ − 644
@{ML [display,gray] "print_depth 50"}
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 645
\end{exercise}
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 646
13
+ − 647
The antiquotation @{text "@{prop \<dots>}"} constructs terms of propositional type,
50
+ − 648
inserting the invisible @{text "Trueprop"}-coercions whenever necessary.
68
+ − 649
Consider for example the pairs
12
+ − 650
126
+ − 651
@{ML_response [display,gray] "(@{term \"P x\"}, @{prop \"P x\"})"
+ − 652
"(Free (\"P\", \<dots>) $ Free (\"x\", \<dots>),
149
+ − 653
Const (\"Trueprop\", \<dots>) $ (Free (\"P\", \<dots>) $ Free (\"x\", \<dots>)))"}
65
+ − 654
108
8bea3f74889d
added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 655
where a coercion is inserted in the second component and
12
+ − 656
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 657
@{ML_response [display,gray] "(@{term \"P x \<Longrightarrow> Q x\"}, @{prop \"P x \<Longrightarrow> Q x\"})"
65
+ − 658
"(Const (\"==>\", \<dots>) $ \<dots> $ \<dots>, Const (\"==>\", \<dots>) $ \<dots> $ \<dots>)"}
12
+ − 659
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 660
where it is not (since it is already constructed by a meta-implication).
19
+ − 661
193
+ − 662
As already seen above, types can be constructed using the antiquotation
+ − 663
@{text "@{typ \<dots>}"}. For example:
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 664
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 665
@{ML_response_fake [display,gray] "@{typ \"bool \<Rightarrow> nat\"}" "bool \<Rightarrow> nat"}
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 666
19
+ − 667
\begin{readmore}
+ − 668
Types are described in detail in \isccite{sec:types}. Their
78
+ − 669
definition and many useful operations are implemented
+ − 670
in @{ML_file "Pure/type.ML"}.
19
+ − 671
\end{readmore}
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 672
*}
19
+ − 673
+ − 674
156
+ − 675
section {* Constructing Terms and Types Manually\label{sec:terms_types_manually} *}
12
+ − 676
+ − 677
text {*
81
+ − 678
While antiquotations are very convenient for constructing terms, they can
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 679
only construct fixed terms (remember they are ``linked'' at compile-time).
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 680
However, you often need to construct terms dynamically. For example, a
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 681
function that returns the implication @{text "\<And>(x::nat). P x \<Longrightarrow> Q x"} taking
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 682
@{term P} and @{term Q} as arguments can only be written as:
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 683
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 684
*}
12
+ − 685
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 686
ML{*fun make_imp P Q =
131
+ − 687
let
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 688
val x = Free ("x", @{typ nat})
131
+ − 689
in
+ − 690
Logic.all x (Logic.mk_implies (P $ x, Q $ x))
+ − 691
end *}
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 692
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 693
text {*
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 694
The reason is that you cannot pass the arguments @{term P} and @{term Q}
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 695
into an antiquotation. For example the following does \emph{not} work.
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 696
*}
13
+ − 697
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 698
ML{*fun make_wrong_imp P Q = @{prop "\<And>(x::nat). P x \<Longrightarrow> Q x"} *}
12
+ − 699
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 700
text {*
194
+ − 701
To see this, apply @{text "@{term S}"} and @{text "@{term T}"}
193
+ − 702
to both functions. With @{ML make_imp} you obtain the intended term involving
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 703
the given arguments
65
+ − 704
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 705
@{ML_response [display,gray] "make_imp @{term S} @{term T}"
162
+ − 706
"Const \<dots> $
+ − 707
Abs (\"x\", Type (\"nat\",[]),
+ − 708
Const \<dots> $ (Free (\"S\",\<dots>) $ \<dots>) $ (Free (\"T\",\<dots>) $ \<dots>))"}
68
+ − 709
193
+ − 710
whereas with @{ML make_wrong_imp} you obtain a term involving the @{term "P"}
68
+ − 711
and @{text "Q"} from the antiquotation.
+ − 712
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 713
@{ML_response [display,gray] "make_wrong_imp @{term S} @{term T}"
162
+ − 714
"Const \<dots> $
+ − 715
Abs (\"x\", \<dots>,
+ − 716
Const \<dots> $ (Const \<dots> $ (Free (\"P\",\<dots>) $ \<dots>)) $
192
+ − 717
(Const \<dots> $ (Free (\"Q\",\<dots>) $ \<dots>)))"}
65
+ − 718
192
+ − 719
There are a number of handy functions that are frequently used for
204
+ − 720
constructing terms. One is the function @{ML list_comb}, which takes a term
199
+ − 721
and a list of terms as arguments, and produces as output the term
192
+ − 722
list applied to the term. For example
+ − 723
+ − 724
@{ML_response_fake [display,gray]
+ − 725
"list_comb (@{term \"P::nat\"}, [@{term \"True\"}, @{term \"False\"}])"
+ − 726
"Free (\"P\", \"nat\") $ Const (\"True\", \"bool\") $ Const (\"False\", \"bool\")"}
+ − 727
193
+ − 728
Another handy function is @{ML lambda}, which abstracts a variable
+ − 729
in a term. For example
+ − 730
+ − 731
@{ML_response_fake [display,gray]
+ − 732
"lambda @{term \"x::nat\"} @{term \"(P::nat\<Rightarrow>bool) x\"}"
+ − 733
"Abs (\"x\", \"nat\", Free (\"P\", \"bool \<Rightarrow> bool\") $ Bound 0)"}
+ − 734
194
+ − 735
In the example, @{ML lambda} produces a de Bruijn index (i.e.~@{ML "Bound 0"}),
193
+ − 736
and an abstraction. It also records the type of the abstracted
+ − 737
variable and for printing purposes also its name. Note that because of the
+ − 738
typing annotation on @{text "P"}, the variable @{text "x"} in @{text "P x"}
+ − 739
is of the same type as the abstracted variable. If it is of different type,
+ − 740
as in
+ − 741
+ − 742
@{ML_response_fake [display,gray]
+ − 743
"lambda @{term \"x::nat\"} @{term \"(P::bool\<Rightarrow>bool) x\"}"
+ − 744
"Abs (\"x\", \"nat\", Free (\"P\", \"bool \<Rightarrow> bool\") $ Free (\"x\", \"bool\"))"}
49
+ − 745
193
+ − 746
then the variable @{text "Free (\"x\", \"bool\")"} is \emph{not} abstracted.
+ − 747
This is a fundamental principle
+ − 748
of Church-style typing, where variables with the same name still differ, if they
+ − 749
have different type.
192
+ − 750
193
+ − 751
There is also the function @{ML subst_free} with which terms can
194
+ − 752
be replaced by other terms. For example below, we will replace in
+ − 753
@{term "(f::nat\<Rightarrow>nat\<Rightarrow>nat) 0 x"}
+ − 754
the subterm @{term "(f::nat\<Rightarrow>nat\<Rightarrow>nat) 0"} by @{term y}, and @{term x} by @{term True}.
49
+ − 755
193
+ − 756
@{ML_response_fake [display,gray]
+ − 757
"subst_free [(@{term \"(f::nat\<Rightarrow>nat\<Rightarrow>nat) 0\"}, @{term \"y::nat\<Rightarrow>nat\"}),
+ − 758
(@{term \"x::nat\"}, @{term \"True\"})]
+ − 759
@{term \"((f::nat\<Rightarrow>nat\<Rightarrow>nat) 0) x\"}"
+ − 760
"Free (\"y\", \"nat \<Rightarrow> nat\") $ Const (\"True\", \"bool\")"}
+ − 761
+ − 762
As can be seen, @{ML subst_free} does not take typability into account.
+ − 763
However it takes alpha-equivalence into account:
+ − 764
+ − 765
@{ML_response_fake [display, gray]
+ − 766
"subst_free [(@{term \"(\<lambda>y::nat. y)\"}, @{term \"x::nat\"})]
+ − 767
@{term \"(\<lambda>x::nat. x)\"}"
+ − 768
"Free (\"x\", \"nat\")"}
192
+ − 769
13
+ − 770
\begin{readmore}
89
+ − 771
There are many functions in @{ML_file "Pure/term.ML"}, @{ML_file "Pure/logic.ML"} and
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 772
@{ML_file "HOL/Tools/hologic.ML"} that make such manual constructions of terms
49
+ − 773
and types easier.\end{readmore}
13
+ − 774
+ − 775
Have a look at these files and try to solve the following two exercises:
11
+ − 776
13
+ − 777
\begin{exercise}\label{fun:revsum}
58
+ − 778
Write a function @{text "rev_sum : term -> term"} that takes a
122
+ − 779
term of the form @{text "t\<^isub>1 + t\<^isub>2 + \<dots> + t\<^isub>n"} (whereby @{text "n"} might be zero)
11
+ − 780
and returns the reversed sum @{text "t\<^isub>n + \<dots> + t\<^isub>2 + t\<^isub>1"}. Assume
+ − 781
the @{text "t\<^isub>i"} can be arbitrary expressions and also note that @{text "+"}
13
+ − 782
associates to the left. Try your function on some examples.
11
+ − 783
\end{exercise}
+ − 784
15
+ − 785
\begin{exercise}\label{fun:makesum}
11
+ − 786
Write a function which takes two terms representing natural numbers
199
+ − 787
in unary notation (like @{term "Suc (Suc (Suc 0))"}), and produces the
11
+ − 788
number representing their sum.
+ − 789
\end{exercise}
+ − 790
122
+ − 791
There are a few subtle issues with constants. They usually crop up when
149
+ − 792
pattern matching terms or types, or when constructing them. While it is perfectly ok
122
+ − 793
to write the function @{text is_true} as follows
+ − 794
*}
+ − 795
+ − 796
ML{*fun is_true @{term True} = true
+ − 797
| is_true _ = false*}
+ − 798
+ − 799
text {*
+ − 800
this does not work for picking out @{text "\<forall>"}-quantified terms. Because
+ − 801
the function
+ − 802
*}
+ − 803
+ − 804
ML{*fun is_all (@{term All} $ _) = true
+ − 805
| is_all _ = false*}
+ − 806
+ − 807
text {*
123
+ − 808
will not correctly match the formula @{prop "\<forall>x::nat. P x"}:
122
+ − 809
+ − 810
@{ML_response [display,gray] "is_all @{term \"\<forall>x::nat. P x\"}" "false"}
+ − 811
+ − 812
The problem is that the @{text "@term"}-antiquotation in the pattern
123
+ − 813
fixes the type of the constant @{term "All"} to be @{typ "('a \<Rightarrow> bool) \<Rightarrow> bool"} for
122
+ − 814
an arbitrary, but fixed type @{typ "'a"}. A properly working alternative
+ − 815
for this function is
+ − 816
*}
+ − 817
+ − 818
ML{*fun is_all (Const ("All", _) $ _) = true
+ − 819
| is_all _ = false*}
+ − 820
+ − 821
text {*
+ − 822
because now
+ − 823
+ − 824
@{ML_response [display,gray] "is_all @{term \"\<forall>x::nat. P x\"}" "true"}
+ − 825
149
+ − 826
matches correctly (the first wildcard in the pattern matches any type and the
+ − 827
second any term).
122
+ − 828
123
+ − 829
However there is still a problem: consider the similar function that
131
+ − 830
attempts to pick out @{text "Nil"}-terms:
122
+ − 831
*}
+ − 832
+ − 833
ML{*fun is_nil (Const ("Nil", _)) = true
+ − 834
| is_nil _ = false *}
+ − 835
+ − 836
text {*
123
+ − 837
Unfortunately, also this function does \emph{not} work as expected, since
122
+ − 838
+ − 839
@{ML_response [display,gray] "is_nil @{term \"Nil\"}" "false"}
+ − 840
123
+ − 841
The problem is that on the ML-level the name of a constant is more
149
+ − 842
subtle than you might expect. The function @{ML is_all} worked correctly,
123
+ − 843
because @{term "All"} is such a fundamental constant, which can be referenced
+ − 844
by @{ML "Const (\"All\", some_type)" for some_type}. However, if you look at
122
+ − 845
+ − 846
@{ML_response [display,gray] "@{term \"Nil\"}" "Const (\"List.list.Nil\", \<dots>)"}
+ − 847
131
+ − 848
the name of the constant @{text "Nil"} depends on the theory in which the
198
+ − 849
term constructor is defined (@{text "List"}) and also in which data type
128
+ − 850
(@{text "list"}). Even worse, some constants have a name involving
+ − 851
type-classes. Consider for example the constants for @{term "zero"} and
131
+ − 852
\mbox{@{text "(op *)"}}:
122
+ − 853
+ − 854
@{ML_response [display,gray] "(@{term \"0::nat\"}, @{term \"op *\"})"
126
+ − 855
"(Const (\"HOL.zero_class.zero\", \<dots>),
122
+ − 856
Const (\"HOL.times_class.times\", \<dots>))"}
+ − 857
123
+ − 858
While you could use the complete name, for example
+ − 859
@{ML "Const (\"List.list.Nil\", some_type)" for some_type}, for referring to or
122
+ − 860
matching against @{text "Nil"}, this would make the code rather brittle.
198
+ − 861
The reason is that the theory and the name of the data type can easily change.
123
+ − 862
To make the code more robust, it is better to use the antiquotation
122
+ − 863
@{text "@{const_name \<dots>}"}. With this antiquotation you can harness the
200
+ − 864
variable parts of the constant's name. Therefore a function for
123
+ − 865
matching against constants that have a polymorphic type should
+ − 866
be written as follows.
122
+ − 867
*}
+ − 868
+ − 869
ML{*fun is_nil_or_all (Const (@{const_name "Nil"}, _)) = true
+ − 870
| is_nil_or_all (Const (@{const_name "All"}, _) $ _) = true
+ − 871
| is_nil_or_all _ = false *}
+ − 872
+ − 873
text {*
200
+ − 874
Occasionally you have to calculate what the ``base'' name of a given
124
+ − 875
constant is. For this you can use the function @{ML Sign.extern_const} or
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 876
@{ML Long_Name.base_name}. For example:
124
+ − 877
+ − 878
@{ML_response [display,gray] "Sign.extern_const @{theory} \"List.list.Nil\"" "\"Nil\""}
122
+ − 879
124
+ − 880
The difference between both functions is that @{ML extern_const in Sign} returns
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 881
the smallest name that is still unique, whereas @{ML base_name in Long_Name} always
124
+ − 882
strips off all qualifiers.
122
+ − 883
+ − 884
\begin{readmore}
162
+ − 885
Functions about naming are implemented in @{ML_file "Pure/General/name_space.ML"};
+ − 886
functions about signatures in @{ML_file "Pure/sign.ML"}.
122
+ − 887
\end{readmore}
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 888
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 889
Although types of terms can often be inferred, there are many
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 890
situations where you need to construct types manually, especially
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 891
when defining constants. For example the function returning a function
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 892
type is as follows:
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 893
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 894
*}
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 895
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 896
ML{*fun make_fun_type tau1 tau2 = Type ("fun", [tau1, tau2]) *}
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 897
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 898
text {* This can be equally written with the combinator @{ML "-->"} as: *}
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 899
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 900
ML{*fun make_fun_type tau1 tau2 = tau1 --> tau2 *}
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 901
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 902
text {*
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 903
A handy function for manipulating terms is @{ML map_types}: it takes a
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 904
function and applies it to every type in a term. You can, for example,
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 905
change every @{typ nat} in a term into an @{typ int} using the function:
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 906
*}
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 907
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 908
ML{*fun nat_to_int t =
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 909
(case t of
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 910
@{typ nat} => @{typ int}
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 911
| Type (s, ts) => Type (s, map nat_to_int ts)
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 912
| _ => t)*}
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 913
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 914
text {*
200
+ − 915
Here is an example:
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 916
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 917
@{ML_response_fake [display,gray]
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 918
"map_types nat_to_int @{term \"a = (1::nat)\"}"
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 919
"Const (\"op =\", \"int \<Rightarrow> int \<Rightarrow> bool\")
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 920
$ Free (\"a\", \"int\") $ Const (\"HOL.one_class.one\", \"int\")"}
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 921
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 922
(FIXME: readmore about types)
122
+ − 923
*}
162
+ − 924
122
+ − 925
124
+ − 926
section {* Type-Checking *}
+ − 927
+ − 928
text {*
+ − 929
131
+ − 930
You can freely construct and manipulate @{ML_type "term"}s and @{ML_type
+ − 931
typ}es, since they are just arbitrary unchecked trees. However, you
+ − 932
eventually want to see if a term is well-formed, or type-checks, relative to
+ − 933
a theory. Type-checking is done via the function @{ML cterm_of}, which
+ − 934
converts a @{ML_type term} into a @{ML_type cterm}, a \emph{certified}
+ − 935
term. Unlike @{ML_type term}s, which are just trees, @{ML_type "cterm"}s are
+ − 936
abstract objects that are guaranteed to be type-correct, and they can only
+ − 937
be constructed via ``official interfaces''.
+ − 938
124
+ − 939
+ − 940
Type-checking is always relative to a theory context. For now we use
+ − 941
the @{ML "@{theory}"} antiquotation to get hold of the current theory.
+ − 942
For example you can write:
+ − 943
149
+ − 944
@{ML_response_fake [display,gray] "cterm_of @{theory} @{term \"(a::nat) + b = c\"}" "a + b = c"}
124
+ − 945
+ − 946
This can also be written with an antiquotation:
+ − 947
+ − 948
@{ML_response_fake [display,gray] "@{cterm \"(a::nat) + b = c\"}" "a + b = c"}
+ − 949
+ − 950
Attempting to obtain the certified term for
+ − 951
+ − 952
@{ML_response_fake_both [display,gray] "@{cterm \"1 + True\"}" "Type unification failed \<dots>"}
+ − 953
+ − 954
yields an error (since the term is not typable). A slightly more elaborate
+ − 955
example that type-checks is:
+ − 956
+ − 957
@{ML_response_fake [display,gray]
+ − 958
"let
+ − 959
val natT = @{typ \"nat\"}
+ − 960
val zero = @{term \"0::nat\"}
+ − 961
in
+ − 962
cterm_of @{theory}
+ − 963
(Const (@{const_name plus}, natT --> natT --> natT) $ zero $ zero)
+ − 964
end" "0 + 0"}
+ − 965
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 966
In Isabelle not just terms need to be certified, but also types. For example,
198
+ − 967
you obtain the certified type for the Isabelle type @{typ "nat \<Rightarrow> bool"} on
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 968
the ML-level as follows:
160
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 969
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 970
@{ML_response_fake [display,gray]
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 971
"ctyp_of @{theory} (@{typ nat} --> @{typ bool})"
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 972
"nat \<Rightarrow> bool"}
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 973
207
+ − 974
or with the antiquotation:
+ − 975
+ − 976
@{ML_response_fake [display,gray]
+ − 977
"@{ctyp \"nat \<Rightarrow> bool\"}"
+ − 978
"nat \<Rightarrow> bool"}
+ − 979
160
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 980
\begin{readmore}
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 981
For functions related to @{ML_type cterm}s and @{ML_type ctyp}s see
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 982
the file @{ML_file "Pure/thm.ML"}.
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 983
\end{readmore}
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 984
124
+ − 985
\begin{exercise}
+ − 986
Check that the function defined in Exercise~\ref{fun:revsum} returns a
+ − 987
result that type-checks.
+ − 988
\end{exercise}
+ − 989
200
+ − 990
Remember Isabelle follows the Church-style typing for terms, i.e., a term contains
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 991
enough typing information (constants, free variables and abstractions all have typing
124
+ − 992
information) so that it is always clear what the type of a term is.
+ − 993
Given a well-typed term, the function @{ML type_of} returns the
+ − 994
type of a term. Consider for example:
+ − 995
+ − 996
@{ML_response [display,gray]
+ − 997
"type_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::nat\"})" "bool"}
+ − 998
+ − 999
To calculate the type, this function traverses the whole term and will
197
+ − 1000
detect any typing inconsistency. For example changing the type of the variable
149
+ − 1001
@{term "x"} from @{typ "nat"} to @{typ "int"} will result in the error message:
124
+ − 1002
+ − 1003
@{ML_response_fake [display,gray]
+ − 1004
"type_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::int\"})"
+ − 1005
"*** Exception- TYPE (\"type_of: type mismatch in application\" \<dots>"}
+ − 1006
+ − 1007
Since the complete traversal might sometimes be too costly and
149
+ − 1008
not necessary, there is the function @{ML fastype_of}, which
+ − 1009
also returns the type of a term.
124
+ − 1010
+ − 1011
@{ML_response [display,gray]
+ − 1012
"fastype_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::nat\"})" "bool"}
+ − 1013
177
+ − 1014
However, efficiency is gained on the expense of skipping some tests. You
124
+ − 1015
can see this in the following example
+ − 1016
+ − 1017
@{ML_response [display,gray]
+ − 1018
"fastype_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::int\"})" "bool"}
+ − 1019
149
+ − 1020
where no error is detected.
124
+ − 1021
+ − 1022
Sometimes it is a bit inconvenient to construct a term with
+ − 1023
complete typing annotations, especially in cases where the typing
+ − 1024
information is redundant. A short-cut is to use the ``place-holder''
+ − 1025
type @{ML "dummyT"} and then let type-inference figure out the
+ − 1026
complete type. An example is as follows:
+ − 1027
+ − 1028
@{ML_response_fake [display,gray]
+ − 1029
"let
126
+ − 1030
val c = Const (@{const_name \"plus\"}, dummyT)
+ − 1031
val o = @{term \"1::nat\"}
+ − 1032
val v = Free (\"x\", dummyT)
124
+ − 1033
in
126
+ − 1034
Syntax.check_term @{context} (c $ o $ v)
124
+ − 1035
end"
126
+ − 1036
"Const (\"HOL.plus_class.plus\", \"nat \<Rightarrow> nat \<Rightarrow> nat\") $
+ − 1037
Const (\"HOL.one_class.one\", \"nat\") $ Free (\"x\", \"nat\")"}
124
+ − 1038
+ − 1039
Instead of giving explicitly the type for the constant @{text "plus"} and the free
200
+ − 1040
variable @{text "x"}, type-inference fills in the missing information.
124
+ − 1041
+ − 1042
\begin{readmore}
+ − 1043
See @{ML_file "Pure/Syntax/syntax.ML"} where more functions about reading,
200
+ − 1044
checking and pretty-printing of terms are defined. Functions related to
+ − 1045
type-inference are implemented in @{ML_file "Pure/type.ML"} and
160
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1046
@{ML_file "Pure/type_infer.ML"}.
124
+ − 1047
\end{readmore}
162
+ − 1048
+ − 1049
(FIXME: say something about sorts)
124
+ − 1050
*}
+ − 1051
+ − 1052
2
+ − 1053
section {* Theorems *}
+ − 1054
+ − 1055
text {*
50
+ − 1056
Just like @{ML_type cterm}s, theorems are abstract objects of type @{ML_type thm}
201
+ − 1057
that can only be built by going through interfaces. As a consequence, every proof
124
+ − 1058
in Isabelle is correct by construction. This follows the tradition of the LCF approach
+ − 1059
\cite{GordonMilnerWadsworth79}.
107
+ − 1060
2
+ − 1061
78
+ − 1062
To see theorems in ``action'', let us give a proof on the ML-level for the following
+ − 1063
statement:
10
+ − 1064
*}
+ − 1065
+ − 1066
lemma
+ − 1067
assumes assm\<^isub>1: "\<And>(x::nat). P x \<Longrightarrow> Q x"
+ − 1068
and assm\<^isub>2: "P t"
13
+ − 1069
shows "Q t" (*<*)oops(*>*)
10
+ − 1070
+ − 1071
text {*
185
+ − 1072
The corresponding ML-code is as follows:
10
+ − 1073
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1074
@{ML_response_fake [display,gray]
42
+ − 1075
"let
138
+ − 1076
val assm1 = @{cprop \"\<And>(x::nat). P x \<Longrightarrow> Q x\"}
+ − 1077
val assm2 = @{cprop \"(P::nat\<Rightarrow>bool) t\"}
10
+ − 1078
+ − 1079
val Pt_implies_Qt =
+ − 1080
assume assm1
138
+ − 1081
|> forall_elim @{cterm \"t::nat\"};
10
+ − 1082
+ − 1083
val Qt = implies_elim Pt_implies_Qt (assume assm2);
+ − 1084
in
+ − 1085
Qt
+ − 1086
|> implies_intr assm2
+ − 1087
|> implies_intr assm1
48
+ − 1088
end" "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
12
+ − 1089
21
+ − 1090
This code-snippet constructs the following proof:
+ − 1091
+ − 1092
\[
+ − 1093
\infer[(@{text "\<Longrightarrow>"}$-$intro)]{\vdash @{prop "(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> P t \<Longrightarrow> Q t"}}
+ − 1094
{\infer[(@{text "\<Longrightarrow>"}$-$intro)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
+ − 1095
{\infer[(@{text "\<Longrightarrow>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"}, @{prop "P t"} \vdash @{prop "Q t"}}
+ − 1096
{\infer[(@{text "\<And>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
+ − 1097
{\infer[(assume)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "\<And>x. P x \<Longrightarrow> Q x"}}{}}
+ − 1098
&
+ − 1099
\infer[(assume)]{@{prop "P t"} \vdash @{prop "P t"}}{}
+ − 1100
}
+ − 1101
}
+ − 1102
}
+ − 1103
\]
+ − 1104
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1105
However, while we obtained a theorem as result, this theorem is not
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1106
yet stored in Isabelle's theorem database. So it cannot be referenced later
128
+ − 1107
on. How to store theorems will be explained in Section~\ref{sec:storing}.
21
+ − 1108
13
+ − 1109
\begin{readmore}
50
+ − 1110
For the functions @{text "assume"}, @{text "forall_elim"} etc
13
+ − 1111
see \isccite{sec:thms}. The basic functions for theorems are defined in
+ − 1112
@{ML_file "Pure/thm.ML"}.
+ − 1113
\end{readmore}
12
+ − 1114
207
+ − 1115
(FIXME: handy functions working on theorems, like @{ML ObjectLogic.rulify} and so on)
+ − 1116
+ − 1117
(FIXME how to add case-names to goal states - maybe in the
+ − 1118
next section)
10
+ − 1119
*}
+ − 1120
123
+ − 1121
section {* Theorem Attributes *}
+ − 1122
127
+ − 1123
text {*
193
+ − 1124
Theorem attributes are @{text "[symmetric]"}, @{text "[THEN \<dots>]"}, @{text
+ − 1125
"[simp]"} and so on. Such attributes are \emph{neither} tags \emph{nor} flags
136
+ − 1126
annotated to theorems, but functions that do further processing once a
193
+ − 1127
theorem is proved. In particular, it is not possible to find out
136
+ − 1128
what are all theorems that have a given attribute in common, unless of course
197
+ − 1129
the function behind the attribute stores the theorems in a retrievable
+ − 1130
data structure.
127
+ − 1131
193
+ − 1132
If you want to print out all currently known attributes a theorem can have,
+ − 1133
you can use the Isabelle command
127
+ − 1134
193
+ − 1135
\begin{isabelle}
+ − 1136
\isacommand{print\_attributes}\\
+ − 1137
@{text "> COMP: direct composition with rules (no lifting)"}\\
+ − 1138
@{text "> HOL.dest: declaration of Classical destruction rule"}\\
+ − 1139
@{text "> HOL.elim: declaration of Classical elimination rule"}\\
+ − 1140
@{text "> \<dots>"}
+ − 1141
\end{isabelle}
+ − 1142
+ − 1143
The theorem attributes fall roughly into two categories: the first category manipulates
207
+ − 1144
the proved theorem (for example @{text "[symmetric]"} and @{text "[THEN \<dots>]"}), and the second
+ − 1145
stores the proved theorem somewhere as data (for example @{text "[simp]"}, which adds
193
+ − 1146
the theorem to the current simpset).
127
+ − 1147
136
+ − 1148
To explain how to write your own attribute, let us start with an extremely simple
+ − 1149
version of the attribute @{text "[symmetric]"}. The purpose of this attribute is
+ − 1150
to produce the ``symmetric'' version of an equation. The main function behind
+ − 1151
this attribute is
127
+ − 1152
*}
+ − 1153
133
+ − 1154
ML{*val my_symmetric = Thm.rule_attribute (fn _ => fn thm => thm RS @{thm sym})*}
+ − 1155
+ − 1156
text {*
136
+ − 1157
where the function @{ML "Thm.rule_attribute"} expects a function taking a
149
+ − 1158
context (which we ignore in the code above) and a theorem (@{text thm}), and
207
+ − 1159
returns another theorem (namely @{text thm} resolved with the theorem
+ − 1160
@{thm [source] sym}: @{thm sym[no_vars]}).\footnote{The function @{ML RS} is explained
+ − 1161
later on in Section~\ref{sec:simpletacs}.} The function
+ − 1162
@{ML "Thm.rule_attribute"} then returns
156
+ − 1163
an attribute.
136
+ − 1164
+ − 1165
Before we can use the attribute, we need to set it up. This can be done
193
+ − 1166
using the Isabelle command \isacommand{attribute\_setup} as follows:
133
+ − 1167
*}
+ − 1168
193
+ − 1169
attribute_setup %gray my_sym = {* Scan.succeed my_symmetric *}
+ − 1170
"applying the sym rule"
133
+ − 1171
136
+ − 1172
text {*
207
+ − 1173
Inside the @{text "\<verbopen> \<dots> \<verbclose>"}, we have to specify a parser
+ − 1174
for the theorem attribute. Since the attribute does not expect any further
+ − 1175
arguments (unlike @{text "[THEN \<dots>]"}, for example), we use the parser @{ML
+ − 1176
Scan.succeed}. Later on we will also consider attributes taking further
+ − 1177
arguments. An example for the attribute @{text "[my_sym]"} is the proof
136
+ − 1178
*}
+ − 1179
+ − 1180
lemma test[my_sym]: "2 = Suc (Suc 0)" by simp
133
+ − 1181
+ − 1182
text {*
193
+ − 1183
which stores the theorem @{thm test} under the name @{thm [source] test}. You
+ − 1184
can see this, if you query the lemma:
+ − 1185
+ − 1186
\begin{isabelle}
+ − 1187
\isacommand{thm}~@{text "test"}\\
+ − 1188
@{text "> "}~@{thm test}
+ − 1189
\end{isabelle}
+ − 1190
+ − 1191
We can also use the attribute when referring to this theorem:
136
+ − 1192
+ − 1193
\begin{isabelle}
+ − 1194
\isacommand{thm}~@{text "test[my_sym]"}\\
+ − 1195
@{text "> "}~@{thm test[my_sym]}
+ − 1196
\end{isabelle}
+ − 1197
193
+ − 1198
As an example of a slightly more complicated theorem attribute, we implement
207
+ − 1199
our own version of @{text "[THEN \<dots>]"}. This attribute will take a list of theorems
194
+ − 1200
as argument and resolve the proved theorem with this list (one theorem
+ − 1201
after another). The code for this attribute is
193
+ − 1202
*}
+ − 1203
+ − 1204
ML{*fun MY_THEN thms =
+ − 1205
Thm.rule_attribute (fn _ => fn thm => foldl ((op RS) o swap) thm thms)*}
+ − 1206
+ − 1207
text {*
207
+ − 1208
where @{ML swap} swaps the components of a pair. The setup of this theorem
193
+ − 1209
attribute uses the parser @{ML Attrib.thms}, which parses a list of
+ − 1210
theorems.
+ − 1211
*}
+ − 1212
+ − 1213
attribute_setup %gray MY_THEN = {* Attrib.thms >> MY_THEN *}
+ − 1214
"resolving the list of theorems with the proved theorem"
+ − 1215
+ − 1216
text {*
+ − 1217
You can, for example, use this theorem attribute to turn an equation into a
+ − 1218
meta-equation:
+ − 1219
+ − 1220
\begin{isabelle}
+ − 1221
\isacommand{thm}~@{text "test[MY_THEN eq_reflection]"}\\
+ − 1222
@{text "> "}~@{thm test[MY_THEN eq_reflection]}
+ − 1223
\end{isabelle}
+ − 1224
+ − 1225
If you need the symmetric version as a meta-equation, you can write
+ − 1226
+ − 1227
\begin{isabelle}
+ − 1228
\isacommand{thm}~@{text "test[MY_THEN sym eq_reflection]"}\\
+ − 1229
@{text "> "}~@{thm test[MY_THEN sym eq_reflection]}
+ − 1230
\end{isabelle}
+ − 1231
194
+ − 1232
It is also possible to combine different theorem attributes, as in:
193
+ − 1233
+ − 1234
\begin{isabelle}
+ − 1235
\isacommand{thm}~@{text "test[my_sym, MY_THEN eq_reflection]"}\\
+ − 1236
@{text "> "}~@{thm test[my_sym, MY_THEN eq_reflection]}
+ − 1237
\end{isabelle}
+ − 1238
+ − 1239
However, here also a weakness of the concept
194
+ − 1240
of theorem attributes shows through: since theorem attributes can be
193
+ − 1241
arbitrary functions, they do not in general commute. If you try
+ − 1242
+ − 1243
\begin{isabelle}
+ − 1244
\isacommand{thm}~@{text "test[MY_THEN eq_reflection, my_sym]"}\\
+ − 1245
@{text "> "}~@{text "exception THM 1 raised: RSN: no unifiers"}
+ − 1246
\end{isabelle}
+ − 1247
+ − 1248
you get an exception indicating that the theorem @{thm [source] sym}
+ − 1249
does not resolve with meta-equations.
+ − 1250
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1251
The purpose of @{ML Thm.rule_attribute} is to directly manipulate theorems.
194
+ − 1252
Another usage of theorem attributes is to add and delete theorems from stored data.
+ − 1253
For example the theorem attribute @{text "[simp]"} adds or deletes a theorem from the
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1254
current simpset. For these applications, you can use @{ML Thm.declaration_attribute}.
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1255
To illustrate this function, let us introduce a reference containing a list
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1256
of theorems.
133
+ − 1257
*}
+ − 1258
193
+ − 1259
ML{*val my_thms = ref ([] : thm list)*}
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1260
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1261
text {*
207
+ − 1262
The purpose of this reference is that we are going to add and delete theorems
+ − 1263
to the referenced list. However, a word of warning: such references must not
+ − 1264
be used in any code that is meant to be more than just for testing purposes!
+ − 1265
Here it is only used to illustrate matters. We will show later how to store
+ − 1266
data properly without using references.
193
+ − 1267
207
+ − 1268
We need to provide two functions that add and delete theorems from this list.
153
+ − 1269
For this we use the two functions:
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1270
*}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1271
207
+ − 1272
ML{*fun my_thm_add thm ctxt =
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1273
(my_thms := Thm.add_thm thm (!my_thms); ctxt)
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1274
207
+ − 1275
fun my_thm_del thm ctxt =
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1276
(my_thms := Thm.del_thm thm (!my_thms); ctxt)*}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1277
133
+ − 1278
text {*
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1279
These functions take a theorem and a context and, for what we are explaining
156
+ − 1280
here it is sufficient that they just return the context unchanged. They change
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1281
however the reference @{ML my_thms}, whereby the function @{ML Thm.add_thm}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1282
adds a theorem if it is not already included in the list, and @{ML
194
+ − 1283
Thm.del_thm} deletes one (both functions use the predicate @{ML
+ − 1284
Thm.eq_thm_prop}, which compares theorems according to their proved
+ − 1285
propositions modulo alpha-equivalence).
133
+ − 1286
+ − 1287
207
+ − 1288
You can turn functions @{ML my_thm_add} and @{ML my_thm_del} into
194
+ − 1289
attributes with the code
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1290
*}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1291
207
+ − 1292
ML{*val my_add = Thm.declaration_attribute my_thm_add
+ − 1293
val my_del = Thm.declaration_attribute my_thm_del *}
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1294
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1295
text {*
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1296
and set up the attributes as follows
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1297
*}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1298
193
+ − 1299
attribute_setup %gray my_thms = {* Attrib.add_del my_add my_del *}
207
+ − 1300
"maintaining a list of my_thms - rough test only!"
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1301
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1302
text {*
207
+ − 1303
The parser @{ML Attrib.add_del} is a pre-defined parser for
194
+ − 1304
adding and deleting lemmas. Now if you prove the next lemma
207
+ − 1305
and attach to it the attribute @{text "[my_thms]"}
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1306
*}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1307
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1308
lemma trueI_2[my_thms]: "True" by simp
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1309
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1310
text {*
194
+ − 1311
then you can see it is added to the initially empty list.
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1312
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1313
@{ML_response_fake [display,gray]
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1314
"!my_thms" "[\"True\"]"}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1315
160
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1316
You can also add theorems using the command \isacommand{declare}.
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1317
*}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1318
207
+ − 1319
declare test[my_thms] trueI_2[my_thms add]
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1320
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1321
text {*
207
+ − 1322
With this attribute, the @{text "add"} operation is the default and does
+ − 1323
not need to be explicitly given. These three declarations will cause the
194
+ − 1324
theorem list to be updated as:
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1325
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1326
@{ML_response_fake [display,gray]
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1327
"!my_thms"
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1328
"[\"True\", \"Suc (Suc 0) = 2\"]"}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1329
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1330
The theorem @{thm [source] trueI_2} only appears once, since the
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1331
function @{ML Thm.add_thm} tests for duplicates, before extending
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1332
the list. Deletion from the list works as follows:
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1333
*}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1334
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1335
declare test[my_thms del]
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1336
156
+ − 1337
text {* After this, the theorem list is again:
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1338
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1339
@{ML_response_fake [display,gray]
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1340
"!my_thms"
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1341
"[\"True\"]"}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1342
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1343
We used in this example two functions declared as @{ML Thm.declaration_attribute},
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1344
but there can be any number of them. We just have to change the parser for reading
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1345
the arguments accordingly.
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1346
156
+ − 1347
However, as said at the beginning of this example, using references for storing theorems is
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1348
\emph{not} the received way of doing such things. The received way is to
207
+ − 1349
start a ``data slot'', below called @{text MyThmsData}, generated by the functor
194
+ − 1350
@{text GenericDataFun}:
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1351
*}
133
+ − 1352
193
+ − 1353
ML {*structure MyThmsData = GenericDataFun
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1354
(type T = thm list
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1355
val empty = []
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1356
val extend = I
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1357
fun merge _ = Thm.merge_thms) *}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1358
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1359
text {*
207
+ − 1360
The type @{text "T"} of this data slot is @{ML_type "thm list"}.\footnote{FIXME: give a pointer
+ − 1361
to where data slots are explained properly.}
+ − 1362
To use this data slot, you only have to change @{ML my_thm_add} and
+ − 1363
@{ML my_thm_del} to:
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1364
*}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1365
207
+ − 1366
ML{*val my_thm_add = MyThmsData.map o Thm.add_thm
+ − 1367
val my_thm_del = MyThmsData.map o Thm.del_thm*}
193
+ − 1368
+ − 1369
text {*
194
+ − 1370
where @{ML MyThmsData.map} updates the data appropriately. The
+ − 1371
corresponding theorem addtributes are
193
+ − 1372
*}
+ − 1373
207
+ − 1374
ML{*val my_add = Thm.declaration_attribute my_thm_add
+ − 1375
val my_del = Thm.declaration_attribute my_thm_del *}
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1376
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1377
text {*
194
+ − 1378
and the setup is as follows
193
+ − 1379
*}
+ − 1380
207
+ − 1381
attribute_setup %gray my_thms2 = {* Attrib.add_del my_add my_del *}
193
+ − 1382
"properly maintaining a list of my_thms"
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1383
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1384
text {*
207
+ − 1385
Initially, the data slot is empty
193
+ − 1386
194
+ − 1387
@{ML_response_fake [display,gray]
+ − 1388
"MyThmsData.get (Context.Proof @{context})"
+ − 1389
"[]"}
193
+ − 1390
194
+ − 1391
but if you prove
+ − 1392
*}
+ − 1393
+ − 1394
lemma three[my_thms2]: "3 = Suc (Suc (Suc 0))" by simp
193
+ − 1395
+ − 1396
text {*
207
+ − 1397
then the lemma is recorded.
194
+ − 1398
+ − 1399
@{ML_response_fake [display,gray]
+ − 1400
"MyThmsData.get (Context.Proof @{context})"
+ − 1401
"[\"3 = Suc (Suc (Suc 0))\"]"}
+ − 1402
207
+ − 1403
With theorem attribute @{text my_thms2} you can also nicely see why it
+ − 1404
is important to
194
+ − 1405
store data in a ``data slot'' and \emph{not} in a reference. Backtrack
207
+ − 1406
to the point just before the lemma @{thm [source] three} was proved and
+ − 1407
check the the content of @{ML_struct "MyThmsData"}: it should be empty.
+ − 1408
The addition has been properly retracted. Now consider the proof:
194
+ − 1409
*}
+ − 1410
+ − 1411
lemma four[my_thms]: "4 = Suc (Suc (Suc (Suc 0)))" by simp
193
+ − 1412
194
+ − 1413
text {*
+ − 1414
Checking the content of @{ML my_thms} gives
+ − 1415
+ − 1416
@{ML_response_fake [display,gray]
+ − 1417
"!my_thms"
+ − 1418
"[\"4 = Suc (Suc (Suc (Suc 0)))\", \"True\"]"}
+ − 1419
207
+ − 1420
as expected, but if you backtrack before the lemma @{thm [source] four}, the
194
+ − 1421
content of @{ML my_thms} is unchanged. The backtracking mechanism
207
+ − 1422
of Isabelle is completely oblivious about what to do with references, but
+ − 1423
properly treats ``data slots''!
194
+ − 1424
207
+ − 1425
Since storing theorems in a list is such a common task, there is the special
194
+ − 1426
functor @{text NamedThmsFun}, which does most of the work for you. To obtain
207
+ − 1427
a named theorem lists, you just declare
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1428
*}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1429
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1430
ML{*structure FooRules = NamedThmsFun
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1431
(val name = "foo"
194
+ − 1432
val description = "Rules for foo") *}
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1433
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1434
text {*
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1435
and set up the @{ML_struct FooRules} with the command
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1436
*}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1437
177
+ − 1438
setup %gray {* FooRules.setup *}
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1439
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1440
text {*
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1441
This code declares a data slot where the theorems are stored,
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1442
an attribute @{text foo} (with the @{text add} and @{text del} options
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1443
for adding and deleting theorems) and an internal ML interface to retrieve and
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1444
modify the theorems.
133
+ − 1445
157
+ − 1446
Furthermore, the facts are made available on the user-level under the dynamic
156
+ − 1447
fact name @{text foo}. For example you can declare three lemmas to be of the kind
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1448
@{text foo} by:
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1449
*}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1450
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1451
lemma rule1[foo]: "A" sorry
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1452
lemma rule2[foo]: "B" sorry
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1453
lemma rule3[foo]: "C" sorry
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1454
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1455
text {* and undeclare the first one by: *}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1456
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1457
declare rule1[foo del]
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1458
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1459
text {* and query the remaining ones with:
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1460
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1461
\begin{isabelle}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1462
\isacommand{thm}~@{text "foo"}\\
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1463
@{text "> ?C"}\\
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1464
@{text "> ?B"}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1465
\end{isabelle}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1466
156
+ − 1467
On the ML-level the rules marked with @{text "foo"} can be retrieved
151
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1468
using the function @{ML FooRules.get}:
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1469
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1470
@{ML_response_fake [display,gray] "FooRules.get @{context}" "[\"?C\",\"?B\"]"}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1471
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1472
\begin{readmore}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1473
For more information see @{ML_file "Pure/Tools/named_thms.ML"} and also
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1474
the recipe in Section~\ref{recipe:storingdata} about storing arbitrary
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1475
data.
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1476
\end{readmore}
7e0bf13bf743
added more material to the attribute section; merged the recipe about named theorems into the main body; added a solution to an exercise in the conversion section
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1477
156
+ − 1478
(FIXME What are: @{text "theory_attributes"}, @{text "proof_attributes"}?)
133
+ − 1479
+ − 1480
+ − 1481
\begin{readmore}
207
+ − 1482
FIXME: @{ML_file "Pure/more_thm.ML"}; parsers for attributes is in
+ − 1483
@{ML_file "Pure/Isar/attrib.ML"}...also explained in the chapter about
+ − 1484
parsing.
133
+ − 1485
\end{readmore}
+ − 1486
*}
+ − 1487
127
+ − 1488
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1489
section {* Setups (TBD) *}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1490
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1491
text {*
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1492
In the previous section we used \isacommand{setup} in order to make
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1493
a theorem attribute known to Isabelle. What happens behind the scenes
202
+ − 1494
is that \isacommand{setup} expects a function of type
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1495
@{ML_type "theory -> theory"}: the input theory is the current theory and the
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1496
output the theory where the theory attribute has been stored.
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1497
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1498
This is a fundamental principle in Isabelle. A similar situation occurs
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1499
for example with declaring constants. The function that declares a
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1500
constant on the ML-level is @{ML Sign.add_consts_i}.
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1501
If you write\footnote{Recall that ML-code needs to be
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1502
enclosed in \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.}
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1503
*}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1504
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1505
ML{*Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] @{theory} *}
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1506
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1507
text {*
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1508
for declaring the constant @{text "BAR"} with type @{typ nat} and
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1509
run the code, then you indeed obtain a theory as result. But if you
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1510
query the constant on the Isabelle level using the command \isacommand{term}
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1511
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1512
\begin{isabelle}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1513
\isacommand{term}~@{text [quotes] "BAR"}\\
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1514
@{text "> \"BAR\" :: \"'a\""}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1515
\end{isabelle}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1516
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1517
you do not obtain a constant of type @{typ nat}, but a free variable (printed in
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1518
blue) of polymorphic type. The problem is that the ML-expression above did
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1519
not register the declaration with the current theory. This is what the command
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1520
\isacommand{setup} is for. The constant is properly declared with
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1521
*}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1522
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1523
setup %gray {* Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] *}
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1524
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1525
text {*
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1526
Now
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1527
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1528
\begin{isabelle}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1529
\isacommand{term}~@{text [quotes] "BAR"}\\
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1530
@{text "> \"BAR\" :: \"nat\""}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1531
\end{isabelle}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1532
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1533
returns a (black) constant with the type @{typ nat}.
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1534
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1535
A similar command is \isacommand{local\_setup}, which expects a function
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1536
of type @{ML_type "local_theory -> local_theory"}. Later on we will also
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1537
use the commands \isacommand{method\_setup} for installing methods in the
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1538
current theory and \isacommand{simproc\_setup} for adding new simprocs to
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1539
the current simpset.
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1540
*}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1541
153
+ − 1542
section {* Theories, Contexts and Local Theories (TBD) *}
123
+ − 1543
126
+ − 1544
text {*
+ − 1545
There are theories, proof contexts and local theories (in this order, if you
+ − 1546
want to order them).
+ − 1547
+ − 1548
In contrast to an ordinary theory, which simply consists of a type
+ − 1549
signature, as well as tables for constants, axioms and theorems, a local
202
+ − 1550
theory contains additional context information, such as locally fixed
126
+ − 1551
variables and local assumptions that may be used by the package. The type
+ − 1552
@{ML_type local_theory} is identical to the type of \emph{proof contexts}
+ − 1553
@{ML_type "Proof.context"}, although not every proof context constitutes a
+ − 1554
valid local theory.
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1555
*}
126
+ − 1556
153
+ − 1557
section {* Storing Theorems\label{sec:storing} (TBD) *}
123
+ − 1558
+ − 1559
text {* @{ML PureThy.add_thms_dynamic} *}
+ − 1560
100
+ − 1561
75
+ − 1562
126
+ − 1563
(* FIXME: some code below *)
89
+ − 1564
+ − 1565
(*<*)
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1566
(*
89
+ − 1567
setup {*
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1568
Sign.add_consts_i [(Binding"bar", @{typ "nat"},NoSyn)]
89
+ − 1569
*}
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1570
*)
89
+ − 1571
lemma "bar = (1::nat)"
+ − 1572
oops
+ − 1573
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1574
(*
89
+ − 1575
setup {*
+ − 1576
Sign.add_consts_i [("foo", @{typ "nat"},NoSyn)]
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1577
#> PureThy.add_defs false [((@{binding "foo_def"},
89
+ − 1578
Logic.mk_equals (Const ("FirstSteps.foo", @{typ "nat"}), @{term "1::nat"})), [])]
+ − 1579
#> snd
+ − 1580
*}
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1581
*)
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1582
(*
89
+ − 1583
lemma "foo = (1::nat)"
+ − 1584
apply(simp add: foo_def)
+ − 1585
done
+ − 1586
+ − 1587
thm foo_def
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1588
*)
89
+ − 1589
(*>*)
+ − 1590
153
+ − 1591
section {* Pretty-Printing (TBD) *}
+ − 1592
+ − 1593
text {*
210
+ − 1594
Isabelle has a pretty sphisticated pretty printing module.
+ − 1595
*}
+ − 1596
+ − 1597
text {*
163
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1598
@{ML Pretty.big_list},
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1599
@{ML Pretty.brk},
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1600
@{ML Pretty.block},
2319cff107f0
removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1601
@{ML Pretty.chunks}
153
+ − 1602
*}
+ − 1603
+ − 1604
section {* Misc (TBD) *}
92
+ − 1605
+ − 1606
ML {*DatatypePackage.get_datatype @{theory} "List.list"*}
+ − 1607
196
+ − 1608
end