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