ProgTutorial/FirstSteps.thy
author Christian Urban <urbanc@in.tum.de>
Mon, 18 May 2009 05:21:40 +0200
changeset 251 cccb25ee1309
parent 250 ab9e09076462
child 252 f380b13b25a7
permissions -rw-r--r--
a few additions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
theory FirstSteps
25
e2f9f94b26d4 Antiquotation setup is now contained in theory Base.
berghofe
parents: 21
diff changeset
     2
imports Base
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
begin
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
chapter {* First Steps *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
     7
text {*
242
14d5f0cf91dc added note about @{type_names ...}
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
     8
  Isabelle programming is done in ML. Just like lemmas and proofs, ML-code
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
     9
  in Isabelle is part of a theory. If you want to follow the code given in
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
    10
  this chapter, we assume you are working inside the theory starting with
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
    12
  \begin{quote}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    13
  \begin{tabular}{@ {}l}
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    14
  \isacommand{theory} FirstSteps\\
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    15
  \isacommand{imports} Main\\
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    16
  \isacommand{begin}\\
6
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    17
  \ldots
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    18
  \end{tabular}
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
    19
  \end{quote}
157
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
    20
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
    21
  We also generally assume you are working with HOL. The given examples might
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    22
  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>
parents: 13
diff changeset
    23
*}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    24
20
5ae6a1bb91c9 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    25
section {* Including ML-Code *}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    26
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    27
text {*
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    28
  The easiest and quickest way to include code in a theory is
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
    29
  by using the \isacommand{ML}-command. For example:
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
    31
\begin{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
    32
\begin{graybox}
85
b02904872d6b better handling of {* and *}
Christian Urban <urbanc@in.tum.de>
parents: 84
diff changeset
    33
\isacommand{ML}~@{text "\<verbopen>"}\isanewline
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    34
\hspace{5mm}@{ML "3 + 4"}\isanewline
85
b02904872d6b better handling of {* and *}
Christian Urban <urbanc@in.tum.de>
parents: 84
diff changeset
    35
@{text "\<verbclose>"}\isanewline
b02904872d6b better handling of {* and *}
Christian Urban <urbanc@in.tum.de>
parents: 84
diff changeset
    36
@{text "> 7"}\smallskip
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
    37
\end{graybox}
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
    38
\end{isabelle}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
234
f84bc59cb5be polished
Christian Urban <urbanc@in.tum.de>
parents: 230
diff changeset
    40
  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>
parents: 101
diff changeset
    41
  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>
parents: 101
diff changeset
    42
  environment. The code inside the \isacommand{ML}-command can also contain
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    43
  value and function bindings, for example
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    44
*}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    45
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    46
ML %gray {* 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    47
  val r = ref 0
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    48
  fun f n = n + 1 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    49
*}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    50
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    51
text {*
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    52
  and even those can be undone when the proof
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    53
  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>
parents: 101
diff changeset
    54
  \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"} scaffolding whenever we
107
258ce361ba1b polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents: 104
diff changeset
    55
  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>
parents: 101
diff changeset
    56
  code, rather they indicate what the response is when the code is evaluated.
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    57
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
    58
  Once a portion of code is relatively stable, you usually want to export it
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    59
  to a separate ML-file. Such files can then be included somewhere inside a 
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    60
  theory by using the command \isacommand{use}. For example
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    61
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
    62
  \begin{quote}
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    63
  \begin{tabular}{@ {}l}
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    64
  \isacommand{theory} FirstSteps\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    65
  \isacommand{imports} Main\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    66
  \isacommand{uses}~@{text "(\"file_to_be_included.ML\")"} @{text "\<dots>"}\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    67
  \isacommand{begin}\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    68
  \ldots\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    69
  \isacommand{use}~@{text "\"file_to_be_included.ML\""}\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    70
  \ldots
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    71
  \end{tabular}
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
    72
  \end{quote}
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    73
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    74
  The \isacommand{uses}-command in the header of the theory is needed in order 
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    75
  to indicate the dependency of the theory on the ML-file. Alternatively, the 
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    76
  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>
parents: 13
diff changeset
    77
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
    78
  \begin{quote}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    79
  \begin{tabular}{@ {}l}
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
    80
  \isacommand{theory} FirstSteps\\
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    81
  \isacommand{imports} Main\\
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
    82
  \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>
parents: 13
diff changeset
    83
  \isacommand{begin}\\
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    84
  \ldots
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    85
  \end{tabular}
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
    86
  \end{quote}
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    87
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    88
  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>
parents: 13
diff changeset
    89
*}
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    90
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    91
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>
parents: 13
diff changeset
    92
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    93
text {*
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
    94
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
    95
  During development you might find it necessary to inspect some data
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    96
  in your code. This can be done in a ``quick-and-dirty'' fashion using 
239
b63c72776f03 replaced "warning" with "writeln"
Christian Urban <urbanc@in.tum.de>
parents: 235
diff changeset
    97
  the function @{ML "writeln"}. For example 
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    98
239
b63c72776f03 replaced "warning" with "writeln"
Christian Urban <urbanc@in.tum.de>
parents: 235
diff changeset
    99
  @{ML_response_fake [display,gray] "writeln \"any string\"" "\"any string\""}
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   100
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   101
  will print out @{text [quotes] "any string"} inside the response buffer
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   102
  of Isabelle.  This function expects a string as argument. If you develop under PolyML,
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
   103
  then there is a convenient, though again ``quick-and-dirty'', method for
240
d111f5988e49 replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents: 239
diff changeset
   104
  converting values into strings, namely the function @{ML PolyML.makestring}:
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   105
240
d111f5988e49 replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents: 239
diff changeset
   106
  @{ML_response_fake [display,gray] "writeln (PolyML.makestring 1)" "\"1\""} 
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   107
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   108
  However, @{ML makestring} only works if the type of what is converted is monomorphic 
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   109
  and not a function.
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   110
239
b63c72776f03 replaced "warning" with "writeln"
Christian Urban <urbanc@in.tum.de>
parents: 235
diff changeset
   111
  The function @{ML "writeln"} should only be used for testing purposes, because any
52
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
   112
  output this function generates will be overwritten as soon as an error is
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
   113
  raised. For printing anything more serious and elaborate, the
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
   114
  function @{ML tracing} is more appropriate. This function writes all output into
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   115
  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>
parents: 13
diff changeset
   116
161
83f36a1c62f2 rolled back the changes on the function warning and tracing
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
   117
  @{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>
parents: 13
diff changeset
   118
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   119
  It is also possible to redirect the ``channel'' where the string @{text "foo"} is 
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   120
  printed to a separate file, e.g., to prevent ProofGeneral from choking on massive 
107
258ce361ba1b polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents: 104
diff changeset
   121
  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>
parents: 42
diff changeset
   122
*}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   123
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   124
ML{*val strip_specials =
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   125
let
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   126
  fun strip ("\^A" :: _ :: cs) = strip cs
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   127
    | strip (c :: cs) = c :: strip cs
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   128
    | strip [] = [];
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   129
in implode o strip o explode end;
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   130
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   131
fun redirect_tracing stream =
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   132
 Output.tracing_fn := (fn s =>
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   133
    (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>
parents: 42
diff changeset
   134
     TextIO.output (stream, "\n");
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   135
     TextIO.flushOut stream)) *}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   136
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   137
text {*
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   138
  Calling @{ML "redirect_tracing"} with @{ML "(TextIO.openOut \"foo.bar\")"} 
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   139
  will cause that all tracing information is printed into the file @{text "foo.bar"}.
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   140
107
258ce361ba1b polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents: 104
diff changeset
   141
  You can print out error messages with the function @{ML error}; for example:
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   142
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   143
@{ML_response_fake [display,gray] "if 0=1 then true else (error \"foo\")" 
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   144
"Exception- ERROR \"foo\" raised
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   145
At command \"ML\"."}
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   146
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   147
  (FIXME Mention how to work with @{ML Toplevel.debug} and @{ML Toplevel.profiling}.)
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   148
*}
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   149
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   150
(*
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   151
ML {* reset Toplevel.debug *}
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   152
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   153
ML {* fun dodgy_fun () = (raise TYPE ("",[],[]); 1) *}
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   154
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   155
ML {* fun innocent () = dodgy_fun () *}
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   156
ML {* exception_trace (fn () => cterm_of @{theory} (Bound 0)) *}
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   157
ML {* exception_trace (fn () => innocent ()) *}
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   158
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   159
ML {* Toplevel.program (fn () => cterm_of @{theory} (Bound 0)) *}
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   160
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   161
ML {* Toplevel.program (fn () => innocent ()) *}
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   162
*)
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   163
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   164
text {*
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   165
  Most often you want to inspect data of type @{ML_type term}, @{ML_type cterm} 
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   166
  or @{ML_type thm}. Isabelle contains elaborate pretty-printing functions for printing 
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   167
  them (see Section \ref{sec:pretty}), 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   168
  but  for quick-and-dirty solutions they are far too unwieldy. A simple way to transform 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   169
  a term into a string is to use the function @{ML Syntax.string_of_term}.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   170
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   171
  @{ML_response_fake [display,gray]
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   172
  "Syntax.string_of_term @{context} @{term \"1::nat\"}"
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   173
  "\"\\^E\\^Fterm\\^E\\^E\\^Fconst\\^Fname=HOL.one_class.one\\^E1\\^E\\^F\\^E\\^E\\^F\\^E\""}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   174
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   175
  This produces a string with some additional information encoded in it. The string
239
b63c72776f03 replaced "warning" with "writeln"
Christian Urban <urbanc@in.tum.de>
parents: 235
diff changeset
   176
  can be properly printed by using the function @{ML writeln}.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   177
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   178
  @{ML_response_fake [display,gray]
239
b63c72776f03 replaced "warning" with "writeln"
Christian Urban <urbanc@in.tum.de>
parents: 235
diff changeset
   179
  "writeln (Syntax.string_of_term @{context} @{term \"1::nat\"})"
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   180
  "\"1\""}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   181
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   182
  A @{ML_type cterm} can be transformed into a string by the following function.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   183
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   184
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   185
ML{*fun string_of_cterm ctxt t =  
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   186
   Syntax.string_of_term ctxt (term_of t)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   187
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   188
text {*
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   189
  In this example the function @{ML term_of} extracts the @{ML_type term} from
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   190
  a @{ML_type cterm}.  If there are more than one @{ML_type cterm}s to be
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   191
  printed, you can use the function @{ML commas} to separate them.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   192
*} 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   193
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   194
ML{*fun string_of_cterms ctxt ts =  
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   195
   commas (map (string_of_cterm ctxt) ts)*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   196
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   197
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   198
  The easiest way to get the string of a theorem is to transform it
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   199
  into a @{ML_type cterm} using the function @{ML crep_thm}. 
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   200
*}
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   201
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   202
ML{*fun string_of_thm ctxt thm =
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   203
  string_of_cterm ctxt (#prop (crep_thm thm))*}
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   204
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   205
text {*
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   206
  Theorems also include schematic variables, such as @{text "?P"}, 
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   207
  @{text "?Q"} and so on. 
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   208
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   209
  @{ML_response_fake [display, gray]
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   210
  "writeln (string_of_thm @{context} @{thm conjI})"
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   211
  "\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q"}
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   212
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   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>
parents: 157
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>
parents: 157
diff changeset
   215
  function @{ML Variable.import_thms}.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   216
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   217
158
d7944bdf7b3f removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents: 157
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>
parents: 157
diff changeset
   219
let 
d7944bdf7b3f removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents: 157
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>
parents: 157
diff changeset
   221
in
d7944bdf7b3f removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   222
  thm'
d7944bdf7b3f removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   223
end
d7944bdf7b3f removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   224
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   225
fun string_of_thm_no_vars ctxt thm =
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   226
  string_of_cterm ctxt (#prop (crep_thm (no_vars ctxt thm)))*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   227
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   228
text {* 
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   229
  Theorem @{thm [source] conjI} is now printed as follows:
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   230
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   231
  @{ML_response_fake [display, gray]
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   232
  "writeln (string_of_thm_no_vars @{context} @{thm conjI})"
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   233
  "\<lbrakk>P; Q\<rbrakk> \<Longrightarrow> P \<and> Q"}
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   234
  
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   235
  Again the function @{ML commas} helps with printing more than one theorem. 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   236
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   237
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   238
ML{*fun string_of_thms ctxt thms =  
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   239
  commas (map (string_of_thm ctxt) thms)
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   240
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   241
fun string_of_thms_no_vars ctxt thms =  
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   242
  commas (map (string_of_thm_no_vars ctxt) thms) *}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   243
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   244
section {* Combinators\label{sec:combinators} *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   245
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   246
text {*
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   247
  For beginners perhaps the most puzzling parts in the existing code of Isabelle are
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   248
  the combinators. At first they seem to greatly obstruct the
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   249
  comprehension of the code, but after getting familiar with them, they
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   250
  actually ease the understanding and also the programming.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   251
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   252
  The simplest combinator is @{ML I}, which is just the identity function defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   253
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   254
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   255
ML{*fun I x = x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   256
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   257
text {* Another simple combinator is @{ML K}, defined as *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   258
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   259
ML{*fun K x = fn _ => x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   260
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   261
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   262
  @{ML K} ``wraps'' a function around the argument @{text "x"}. However, this 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   263
  function ignores its argument. As a result, @{ML K} defines a constant function 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   264
  always returning @{text x}.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   265
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   266
  The next combinator is reverse application, @{ML "|>"}, defined as: 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   267
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   268
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   269
ML{*fun x |> f = f x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   270
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   271
text {* While just syntactic sugar for the usual function application,
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   272
  the purpose of this combinator is to implement functions in a  
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   273
  ``waterfall fashion''. Consider for example the function *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   274
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   275
ML %linenosgray{*fun inc_by_five x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   276
  x |> (fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   277
    |> (fn x => (x, x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   278
    |> fst
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   279
    |> (fn x => x + 4)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   280
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   281
text {*
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   282
  which increments its argument @{text x} by 5. It proceeds by first incrementing 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   283
  the argument by 1 (Line 2); then storing the result in a pair (Line 3); taking 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   284
  the first component of the pair (Line 4) and finally incrementing the first 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   285
  component by 4 (Line 5). This kind of cascading manipulations of values is quite
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   286
  common when dealing with theories (for example by adding a definition, followed by
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   287
  lemmas and so on). The reverse application allows you to read what happens in 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   288
  a top-down manner. This kind of coding should also be familiar, 
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   289
  if you have been exposed to Haskell's {\it do}-notation. Writing the function 
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   290
  @{ML inc_by_five} using the reverse application is much clearer than writing
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   291
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   292
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   293
ML{*fun inc_by_five x = fst ((fn x => (x, x)) (x + 1)) + 4*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   294
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   295
text {* or *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   296
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   297
ML{*fun inc_by_five x = 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   298
       ((fn x => x + 4) o fst o (fn x => (x, x)) o (fn x => x + 1)) x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   299
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   300
text {* and typographically more economical than *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   301
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   302
ML{*fun inc_by_five x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   303
   let val y1 = x + 1
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   304
       val y2 = (y1, y1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   305
       val y3 = fst y2
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   306
       val y4 = y3 + 4
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   307
   in y4 end*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   308
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   309
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   310
  Another reason why the let-bindings in the code above are better to be
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   311
  avoided: it is more than easy to get the intermediate values wrong, not to 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   312
  mention the nightmares the maintenance of this code causes!
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   313
215
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   314
  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>
parents: 177
diff changeset
   315
  the waterfall fashion might be the following code:
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   316
*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   317
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   318
ML %linenosgray{*fun apply_fresh_args f ctxt =
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   319
    f |> fastype_of
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   320
      |> binder_types 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   321
      |> map (pair "z")
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   322
      |> Variable.variant_frees ctxt [f]
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   323
      |> map Free  
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   324
      |> (curry list_comb) f *}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   325
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   326
text {*
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   327
  This code extracts the argument types of a given function @{text "f"} and then generates 
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   328
  for each argument type a distinct variable; finally it applies the generated 
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   329
  variables to the function. For example:
183
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   330
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   331
  @{ML_response_fake [display,gray]
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   332
"apply_fresh_args @{term \"P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool\"} @{context} 
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   333
 |> Syntax.string_of_term @{context}
239
b63c72776f03 replaced "warning" with "writeln"
Christian Urban <urbanc@in.tum.de>
parents: 235
diff changeset
   334
 |> writeln"
183
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   335
  "P z za zb"}
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   336
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 183
diff changeset
   337
  You can read off this behaviour from how @{ML apply_fresh_args} is
183
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   338
  coded: in Line 2, the function @{ML fastype_of} calculates the type of the
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   339
  function; @{ML binder_types} in the next line produces the list of argument
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 183
diff changeset
   340
  types (in the case above the list @{text "[nat, int, unit]"}); Line 4 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 183
diff changeset
   341
  pairs up each type with the string  @{text "z"}; the
183
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   342
  function @{ML variant_frees in Variable} generates for each @{text "z"} a
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   343
  unique name avoiding the given @{text f}; the list of name-type pairs is turned
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 183
diff changeset
   344
  into a list of variable terms in Line 6, which in the last line is applied
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   345
  by the function @{ML list_comb} to the function. In this last step we have to 
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   346
  use the function @{ML curry}, because @{ML list_comb} expects the function and the
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 183
diff changeset
   347
  variables list as a pair.
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   348
  
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   349
  The combinator @{ML "#>"} is the reverse function composition. It can be
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   350
  used to define the following function
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   351
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   352
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   353
ML{*val inc_by_six = 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   354
      (fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   355
   #> (fn x => x + 2)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   356
   #> (fn x => x + 3)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   357
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   358
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   359
  which is the function composed of first the increment-by-one function and then
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   360
  increment-by-two, followed by increment-by-three. Again, the reverse function 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   361
  composition allows you to read the code top-down.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   362
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   363
  The remaining combinators described in this section add convenience for the
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   364
  ``waterfall method'' of writing functions. The combinator @{ML tap} allows
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   365
  you to get hold of an intermediate result (to do some side-calculations for
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   366
  instance). The function
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   367
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   368
 *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   369
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   370
ML %linenosgray{*fun inc_by_three x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   371
     x |> (fn x => x + 1)
240
d111f5988e49 replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents: 239
diff changeset
   372
       |> tap (fn x => tracing (PolyML.makestring x))
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   373
       |> (fn x => x + 2)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   374
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   375
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   376
  increments the argument first by @{text "1"} and then by @{text "2"}. In the
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   377
  middle (Line 3), however, it uses @{ML tap} for printing the ``plus-one''
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   378
  intermediate result inside the tracing buffer. The function @{ML tap} can
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   379
  only be used for side-calculations, because any value that is computed
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   380
  cannot be merged back into the ``main waterfall''. To do this, you can use
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   381
  the next combinator.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   382
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   383
  The combinator @{ML "`"} (a backtick) is similar to @{ML tap}, but applies a
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   384
  function to the value and returns the result together with the value (as a
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   385
  pair). For example the function 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   386
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   387
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   388
ML{*fun inc_as_pair x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   389
     x |> `(fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   390
       |> (fn (x, y) => (x, y + 1))*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   391
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   392
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   393
  takes @{text x} as argument, and then increments @{text x}, but also keeps
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   394
  @{text x}. The intermediate result is therefore the pair @{ML "(x + 1, x)"
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   395
  for x}. After that, the function increments the right-hand component of the
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   396
  pair. So finally the result will be @{ML "(x + 1, x + 1)" for x}.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   397
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   398
  The combinators @{ML "|>>"} and @{ML "||>"} are defined for 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   399
  functions manipulating pairs. The first applies the function to
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   400
  the first component of the pair, defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   401
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   402
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   403
ML{*fun (x, y) |>> f = (f x, y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   404
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   405
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   406
  and the second combinator to the second component, defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   407
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   408
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   409
ML{*fun (x, y) ||> f = (x, f y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   410
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   411
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   412
  With the combinator @{ML "|->"} you can re-combine the elements from a pair.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   413
  This combinator is defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   414
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   415
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   416
ML{*fun (x, y) |-> f = f x y*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   417
215
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   418
text {* 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   419
  and can be used to write the following roundabout version 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   420
  of the @{text double} function: 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   421
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   422
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   423
ML{*fun double x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   424
      x |>  (fn x => (x, x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   425
        |-> (fn x => fn y => x + y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   426
215
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   427
text {* 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   428
  The combinator @{ML ||>>} plays a central rôle whenever your task is to update a 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   429
  theory and the update also produces a side-result (for example a theorem). Functions 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   430
  for such tasks return a pair whose second component is the theory and the fist 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   431
  component is the side-result. Using @{ML ||>>}, you can do conveniently the update 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   432
  and also accumulate the side-results. Considder the following simple function. 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   433
*}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   434
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   435
ML %linenosgray{*fun acc_incs x =
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   436
    x |> (fn x => ("", x)) 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   437
      ||>> (fn x => (x, x + 1))
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   438
      ||>> (fn x => (x, x + 1))
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   439
      ||>> (fn x => (x, x + 1))*}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   440
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   441
text {*
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   442
  The purpose of Line 2 is to just pair up the argument with a dummy value (since
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   443
  @{ML "||>>"} operates on pairs). Each of the next three lines just increment 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   444
  the value by one, but also nest the intrermediate results to the left. For example 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   445
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   446
  @{ML_response [display,gray]
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   447
  "acc_incs 1"
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   448
  "((((\"\", 1), 2), 3), 4)"}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   449
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   450
  You can continue this chain with:
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   451
  
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   452
  @{ML_response [display,gray]
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   453
  "acc_incs 1 ||>> (fn x => (x, x + 2))"
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   454
  "(((((\"\", 1), 2), 3), 4), 6)"}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   455
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   456
  (FIXME: maybe give a ``real world'' example)
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   457
*}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   458
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   459
text {*
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   460
  Recall that @{ML "|>"} is the reverse function application. Recall also that
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   461
  the related 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   462
  reverse function composition is @{ML "#>"}. In fact all the combinators @{ML "|->"},
215
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   463
  @{ML "|>>"} , @{ML "||>"} and @{ML "||>>"} described above have related combinators for 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   464
  function composition, namely @{ML "#->"}, @{ML "#>>"}, @{ML "##>"} and @{ML "##>>"}. 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   465
  Using @{ML "#->"}, for example, the function @{text double} can also be written as:
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   466
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   467
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   468
ML{*val double =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   469
            (fn x => (x, x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   470
        #-> (fn x => fn y => x + y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   471
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   472
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   473
  
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   474
  (FIXME: find a good exercise for combinators)
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   475
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   476
  \begin{readmore}
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   477
  The most frequently used combinators are defined in the files @{ML_file
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   478
  "Pure/library.ML"}
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   479
  and @{ML_file "Pure/General/basics.ML"}. Also \isccite{sec:ML-linear-trans} 
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   480
  contains further information about combinators.
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   481
  \end{readmore}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   482
 
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   483
  (FIXME: say something about calling conventions)
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   484
*}
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   485
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   486
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   487
section {* Antiquotations *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   488
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   489
text {*
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   490
  The main advantage of embedding all code in a theory is that the code can
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   491
  contain references to entities defined on the logical level of Isabelle. By
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   492
  this we mean definitions, theorems, terms and so on. This kind of reference is
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   493
  realised with antiquotations.  For example, one can print out the name of the current
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   494
  theory by typing
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   495
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   496
  
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   497
  @{ML_response [display,gray] "Context.theory_name @{theory}" "\"FirstSteps\""}
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   498
 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   499
  where @{text "@{theory}"} is an antiquotation that is substituted with the
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   500
  current theory (remember that we assumed we are inside the theory 
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   501
  @{text FirstSteps}). The name of this theory can be extracted using
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   502
  the function @{ML "Context.theory_name"}. 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   503
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   504
  Note, however, that antiquotations are statically linked, that is their value is
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   505
  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>
parents: 42
diff changeset
   506
*}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   507
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   508
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>
parents: 42
diff changeset
   509
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   510
text {*
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   511
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   512
  does \emph{not} return the name of the current theory, if it is run in a 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   513
  different theory. Instead, the code above defines the constant function 
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   514
  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>
parents: 42
diff changeset
   515
  function is called. Operationally speaking,  the antiquotation @{text "@{theory}"} is 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   516
  \emph{not} replaced with code that will look up the current theory in 
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   517
  some data structure and return it. Instead, it is literally
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   518
  replaced with the value representing the theory name.
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   519
132
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   520
  In a similar way you can use antiquotations to refer to proved theorems: 
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   521
  @{text "@{thm \<dots>}"} for a single theorem
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   522
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   523
  @{ML_response_fake [display,gray] "@{thm allI}" "(\<And>x. ?P x) \<Longrightarrow> \<forall>x. ?P x"}
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   524
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   525
  and @{text "@{thms \<dots>}"} for more than one
132
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   526
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   527
@{ML_response_fake [display,gray] "@{thms conj_ac}" 
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   528
"(?P \<and> ?Q) = (?Q \<and> ?P)
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   529
(?P \<and> ?Q \<and> ?R) = (?Q \<and> ?P \<and> ?R)
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   530
((?P \<and> ?Q) \<and> ?R) = (?P \<and> ?Q \<and> ?R)"}  
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   531
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   532
  You can also refer to the current simpset. To illustrate this we implement the
132
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   533
  function that extracts the theorem names stored in a simpset.
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   534
*}
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   535
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   536
ML{*fun get_thm_names_from_ss simpset =
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   537
let
163
2319cff107f0 removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
parents: 162
diff changeset
   538
  val {simps,...} = MetaSimplifier.dest_ss simpset
70
bbb2d5f1d58d deleted the fixme about simpsets
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   539
in
163
2319cff107f0 removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
parents: 162
diff changeset
   540
  map #1 simps
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   541
end*}
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
   542
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   543
text {*
163
2319cff107f0 removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
parents: 162
diff changeset
   544
  The function @{ML dest_ss in MetaSimplifier} returns a record containing all
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   545
  information stored in the simpset, but we are only interested in the names of the
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   546
  simp-rules. Now you can feed in the current simpset into this function. 
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   547
  The current simpset can be referred to using the antiquotation @{text "@{simpset}"}.
81
8fda6b452f28 polished
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   548
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   549
  @{ML_response_fake [display,gray] 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   550
  "get_thm_names_from_ss @{simpset}" 
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   551
  "[\"Nat.of_nat_eq_id\", \"Int.of_int_eq_id\", \"Nat.One_nat_def\", \<dots>]"}
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   552
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   553
  Again, this way of referencing simpsets makes you independent from additions
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   554
  of lemmas to the simpset by the user that potentially cause loops.
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   555
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   556
  On the ML-level of Isabelle, you often have to work with qualified names.
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   557
  These are strings with some additional information, such as positional information
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   558
  and qualifiers. Such qualified names can be generated with the antiquotation 
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   559
  @{text "@{binding \<dots>}"}.
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   560
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   561
  @{ML_response [display,gray]
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   562
  "@{binding \"name\"}"
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   563
  "name"}
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   564
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   565
  An example where a binding is needed is the function @{ML define in
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   566
  LocalTheory}.  This function is below used to define the constant @{term
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   567
  "TrueConj"} as the conjunction
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   568
  @{term "True \<and> True"}.
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   569
*}
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   570
  
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   571
local_setup %gray {* 
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   572
  snd o LocalTheory.define Thm.internalK 
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   573
    ((@{binding "TrueConj"}, NoSyn), 
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   574
     (Attrib.empty_binding, @{term "True \<and> True"})) *}
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   575
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   576
text {* 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   577
  Now querying the definition you obtain:
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   578
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   579
  \begin{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   580
  \isacommand{thm}~@{text "TrueConj_def"}\\
225
7859fc59249a section for further material about simple inductive
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   581
  @{text "> "}~@{thm TrueConj_def}
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   582
  \end{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   583
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   584
  (FIXME give a better example why bindings are important; maybe
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   585
  give a pointer to \isacommand{local\_setup}; if not, then explain
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   586
  why @{ML snd} is needed)
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   587
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   588
  While antiquotations have many applications, they were originally introduced
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   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>
parents: 42
diff changeset
   590
*}
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   591
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   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>
parents: 42
diff changeset
   593
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   594
text {*
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   595
  Such bindings are difficult to maintain and can be overwritten by the
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   596
  user accidentally. This often broke Isabelle
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   597
  packages. Antiquotations solve this problem, since they are ``linked''
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   598
  statically at compile-time.  However, this static linkage also limits their
201
e1dbcccf970f passive of `build' = `built'
griff
parents: 200
diff changeset
   599
  usefulness in cases where data needs to be built up dynamically. In the
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   600
  course of this chapter you will learn more about antiquotations:
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   601
  they can simplify Isabelle programming since one can directly access all
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   602
  kinds of logical elements from the ML-level.
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
*}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   605
section {* Terms and Types *}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
text {*
197
2fe1877f705f used spell checker on `FirstSteps' + small changes
griff
parents: 196
diff changeset
   608
  One way to construct Isabelle terms, is by using the antiquotation 
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   609
  \mbox{@{text "@{term \<dots>}"}}. For example
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   611
  @{ML_response [display,gray] 
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   612
"@{term \"(a::nat) + b = c\"}" 
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   613
"Const (\"op =\", \<dots>) $ 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   614
  (Const (\"HOL.plus_class.plus\", \<dots>) $ \<dots> $ \<dots>) $ \<dots>"}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   615
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   616
  will show the term @{term "(a::nat) + b = c"}, but printed using the internal
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   617
  representation corresponding to the datatype @{ML_type "term"} defined as follows: 
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   618
*}  
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   619
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   620
ML_val{*datatype term =
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   621
  Const of string * typ 
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   622
| Free of string * typ 
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   623
| Var of indexname * typ 
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   624
| Bound of int 
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   625
| Abs of string * typ * term 
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   626
| $ of term * term *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   627
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   628
text {*
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   629
  Terms use the usual de Bruijn index mechanism---where
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   630
  bound variables are represented by the constructor @{ML Bound}.  For
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   631
  example in
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   632
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   633
  @{ML_response_fake [display, gray]
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   634
  "@{term \"\<lambda>x y. x y\"}"
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   635
  "Abs (\"x\", \"'a \<Rightarrow> 'b\", Abs (\"y\", \"'a\", Bound 1 $ Bound 0))"}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   636
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   637
  the indices refer to the number of Abstractions (@{ML Abs}) that we need to skip
197
2fe1877f705f used spell checker on `FirstSteps' + small changes
griff
parents: 196
diff changeset
   638
  until we hit the @{ML Abs} that binds the corresponding variable. Note that
2fe1877f705f used spell checker on `FirstSteps' + small changes
griff
parents: 196
diff changeset
   639
  the names of bound variables are kept at abstractions for printing purposes,
2fe1877f705f used spell checker on `FirstSteps' + small changes
griff
parents: 196
diff changeset
   640
  and so should be treated only as ``comments''.  Application in Isabelle is
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   641
  realised with the term-constructor @{ML $}. 
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   642
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   643
  Isabelle makes a distinction between \emph{free} variables (term-constructor @{ML Free})
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   644
  and variables (term-constructor @{ML Var}). The latter correspond to the schematic 
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   645
  variables that when printed show up with a question mark in front of them. Consider 
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   646
  the following two examples
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   647
  
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   648
  @{ML_response_fake [display, gray]
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   649
"let
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   650
  val v1 = Var ((\"x\", 3), @{typ bool})
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   651
  val v2 = Var ((\"x1\", 3), @{typ bool})
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   652
in
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   653
  writeln (Syntax.string_of_term @{context} v1);
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   654
  writeln (Syntax.string_of_term @{context} v2)
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   655
end"
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   656
"?x3
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   657
?x1.3"}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   658
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   659
  This is different from a free variable
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   660
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   661
  @{ML_response_fake [display, gray]
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   662
  "writeln (Syntax.string_of_term @{context} (Free (\"x\", @{typ bool})))"
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   663
  "x"}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   664
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   665
  When constructing terms, you are usually concerned with free variables (for example
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   666
  you cannot construct schematic variables using the antiquotation @{text "@{term \<dots>}"}). 
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   667
  If you deal with theorems, you have to observe the distinction. A similar
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   668
  distinction holds for types (see below).
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   669
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
  \begin{readmore}
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   671
  Terms and types are described in detail in \isccite{sec:terms}. Their
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   672
  definition and many useful operations are implemented in @{ML_file "Pure/term.ML"}.
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
  \end{readmore}
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   674
  
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   675
  Constructing terms via antiquotations has the advantage that only typable
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   676
  terms can be constructed. For example
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   677
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   678
  @{ML_response_fake_both [display,gray]
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   679
  "@{term \"x x\"}"
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   680
  "Type unification failed: Occurs check!"}
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   681
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   682
  raises a typing error, while it perfectly ok to construct the term
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   683
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   684
  @{ML_response_fake [display,gray] 
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   685
"let
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   686
  val omega = Free (\"x\", @{typ nat}) $ Free (\"x\", @{typ nat})
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   687
in 
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   688
  writeln (Syntax.string_of_term @{context} omega)
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   689
end"
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   690
  "x x"}
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   691
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   692
  with the raw ML-constructors.
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   693
  Sometimes the internal representation of terms can be surprisingly different
157
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
   694
  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>
parents: 43
diff changeset
   695
  parsing/type-checking/pretty printing can be quite elaborate. 
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   696
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   697
  \begin{exercise}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   698
  Look at the internal term representation of the following terms, and
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   699
  find out why they are represented like this:
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   700
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   701
  \begin{itemize}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   702
  \item @{term "case x of 0 \<Rightarrow> 0 | Suc y \<Rightarrow> y"}  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   703
  \item @{term "\<lambda>(x,y). P y x"}  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   704
  \item @{term "{ [x::int] | x. x \<le> -2 }"}  
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   705
  \end{itemize}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   706
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   707
  Hint: The third term is already quite big, and the pretty printer
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   708
  may omit parts of it by default. If you want to see all of it, you
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   709
  can use the following ML-function to set the printing depth to a higher 
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   710
  value:
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   711
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   712
  @{ML [display,gray] "print_depth 50"}
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   713
  \end{exercise}
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   714
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   715
  The antiquotation @{text "@{prop \<dots>}"} constructs terms of propositional type, 
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
   716
  inserting the invisible @{text "Trueprop"}-coercions whenever necessary. 
68
e7519207c2b7 added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
   717
  Consider for example the pairs
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   718
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   719
@{ML_response [display,gray] "(@{term \"P x\"}, @{prop \"P x\"})" 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   720
"(Free (\"P\", \<dots>) $ Free (\"x\", \<dots>),
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   721
 Const (\"Trueprop\", \<dots>) $ (Free (\"P\", \<dots>) $ Free (\"x\", \<dots>)))"}
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   722
 
108
8bea3f74889d added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
parents: 107
diff changeset
   723
  where a coercion is inserted in the second component and 
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   724
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   725
  @{ML_response [display,gray] "(@{term \"P x \<Longrightarrow> Q x\"}, @{prop \"P x \<Longrightarrow> Q x\"})" 
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   726
  "(Const (\"==>\", \<dots>) $ \<dots> $ \<dots>, Const (\"==>\", \<dots>) $ \<dots> $ \<dots>)"}
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   727
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   728
  where it is not (since it is already constructed by a meta-implication). 
19
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   729
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   730
  As already seen above, types can be constructed using the antiquotation 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   731
  @{text "@{typ \<dots>}"}. For example:
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   732
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   733
  @{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>
parents: 34
diff changeset
   734
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   735
  Their definition is
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   736
*}
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   737
  
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   738
ML_val{*datatype typ =
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   739
  Type  of string * typ list 
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   740
| TFree of string * sort 
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   741
| TVar  of indexname * sort *}
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   742
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   743
text {*
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   744
  Like with terms, there is the distinction between free type
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   745
  variables (term-constructor @{ML "TFree"} and schematic
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   746
  type variables (term-constructor @{ML "TVar"}). A type constant,
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   747
  like @{typ "int"} or @{typ bool}, are types with an empty list
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   748
  of argument types.
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   749
  
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   750
19
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   751
  \begin{readmore}
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   752
  Types are described in detail in \isccite{sec:types}. Their
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   753
  definition and many useful operations are implemented 
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
   754
  in @{ML_file "Pure/type.ML"}.
19
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   755
  \end{readmore}
47
4daf913fdbe1 hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents: 43
diff changeset
   756
*}
19
34b93dbf8c3c some tuning in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
   757
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   758
section {* Constructing Terms and Types Manually\label{sec:terms_types_manually} *} 
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   759
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   760
text {*
81
8fda6b452f28 polished
Christian Urban <urbanc@in.tum.de>
parents: 78
diff changeset
   761
  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>
parents: 101
diff changeset
   762
  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>
parents: 101
diff changeset
   763
  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>
parents: 185
diff changeset
   764
  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>
parents: 185
diff changeset
   765
  @{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>
parents: 101
diff changeset
   766
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   767
*}
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   768
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   769
ML{*fun make_imp P Q =
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   770
let
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   771
  val x = Free ("x", @{typ nat})
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   772
in 
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   773
  Logic.all x (Logic.mk_implies (P $ x, Q $ x))
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   774
end *}
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   775
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   776
text {*
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   777
  The reason is that you cannot pass the arguments @{term P} and @{term Q} 
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
   778
  into an antiquotation.\footnote{At least not at the moment.} For example 
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
   779
  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>
parents: 42
diff changeset
   780
*}
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   781
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   782
ML{*fun make_wrong_imp P Q = @{prop "\<And>(x::nat). P x \<Longrightarrow> Q x"} *}
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   783
43
02f76f1b6e7b added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   784
text {*
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   785
  To see this, apply @{text "@{term S}"} and @{text "@{term T}"} 
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   786
  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>
parents: 101
diff changeset
   787
  the given arguments
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   788
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   789
  @{ML_response [display,gray] "make_imp @{term S} @{term T}" 
162
3fb9f820a294 some additions to the simplifier section and general tuning
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   790
"Const \<dots> $ 
3fb9f820a294 some additions to the simplifier section and general tuning
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   791
  Abs (\"x\", Type (\"nat\",[]),
3fb9f820a294 some additions to the simplifier section and general tuning
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   792
    Const \<dots> $ (Free (\"S\",\<dots>) $ \<dots>) $ (Free (\"T\",\<dots>) $ \<dots>))"}
68
e7519207c2b7 added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
   793
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   794
  whereas with @{ML make_wrong_imp} you obtain a term involving the @{term "P"} 
68
e7519207c2b7 added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
   795
  and @{text "Q"} from the antiquotation.
e7519207c2b7 added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents: 66
diff changeset
   796
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   797
  @{ML_response [display,gray] "make_wrong_imp @{term S} @{term T}" 
162
3fb9f820a294 some additions to the simplifier section and general tuning
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   798
"Const \<dots> $ 
3fb9f820a294 some additions to the simplifier section and general tuning
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   799
  Abs (\"x\", \<dots>,
3fb9f820a294 some additions to the simplifier section and general tuning
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   800
    Const \<dots> $ (Const \<dots> $ (Free (\"P\",\<dots>) $ \<dots>)) $ 
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   801
                (Const \<dots> $ (Free (\"Q\",\<dots>) $ \<dots>)))"}
65
c8e9a4f97916 tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   802
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   803
  There are a number of handy functions that are frequently used for 
204
Christian Urban <urbanc@in.tum.de>
parents: 194 202
diff changeset
   804
  constructing terms. One is the function @{ML list_comb}, which takes a term
199
griff
parents: 198
diff changeset
   805
  and a list of terms as arguments, and produces as output the term
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   806
  list applied to the term. For example
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   807
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   808
@{ML_response_fake [display,gray]
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   809
"list_comb (@{term \"P::nat\"}, [@{term \"True\"}, @{term \"False\"}])"
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   810
"Free (\"P\", \"nat\") $ Const (\"True\", \"bool\") $ Const (\"False\", \"bool\")"}
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   811
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   812
  Another handy function is @{ML lambda}, which abstracts a variable 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   813
  in a term. For example
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   814
  
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   815
  @{ML_response_fake [display,gray]
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   816
  "lambda @{term \"x::nat\"} @{term \"(P::nat\<Rightarrow>bool) x\"}"
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   817
  "Abs (\"x\", \"nat\", Free (\"P\", \"bool \<Rightarrow> bool\") $ Bound 0)"}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   818
228
fe45fbb111c5 various additions
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
   819
  In this example, @{ML lambda} produces a de Bruijn index (i.e.~@{ML "Bound  0"}), 
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   820
  and an abstraction. It also records the type of the abstracted
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   821
  variable and for printing purposes also its name.  Note that because of the
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   822
  typing annotation on @{text "P"}, the variable @{text "x"} in @{text "P x"}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   823
  is of the same type as the abstracted variable. If it is of different type,
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   824
  as in
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   825
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   826
  @{ML_response_fake [display,gray]
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   827
  "lambda @{term \"x::nat\"} @{term \"(P::bool\<Rightarrow>bool) x\"}"
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   828
  "Abs (\"x\", \"nat\", Free (\"P\", \"bool \<Rightarrow> bool\") $ Free (\"x\", \"bool\"))"}
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   829
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   830
  then the variable @{text "Free (\"x\", \"bool\")"} is \emph{not} abstracted. 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   831
  This is a fundamental principle
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   832
  of Church-style typing, where variables with the same name still differ, if they 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   833
  have different type.
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   834
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   835
  There is also the function @{ML subst_free} with which terms can 
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   836
  be replaced by other terms. For example below, we will replace in  
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   837
  @{term "(f::nat\<Rightarrow>nat\<Rightarrow>nat) 0 x"} 
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   838
  the subterm @{term "(f::nat\<Rightarrow>nat\<Rightarrow>nat) 0"} by @{term y}, and @{term x} by @{term True}.
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   839
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   840
  @{ML_response_fake [display,gray]
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   841
"subst_free [(@{term \"(f::nat\<Rightarrow>nat\<Rightarrow>nat) 0\"}, @{term \"y::nat\<Rightarrow>nat\"}),
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   842
            (@{term \"x::nat\"}, @{term \"True\"})] 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   843
  @{term \"((f::nat\<Rightarrow>nat\<Rightarrow>nat) 0) x\"}"
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   844
  "Free (\"y\", \"nat \<Rightarrow> nat\") $ Const (\"True\", \"bool\")"}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   845
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   846
  As can be seen, @{ML subst_free} does not take typability into account.
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   847
  However it takes alpha-equivalence into account:
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   848
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   849
  @{ML_response_fake [display, gray]
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   850
  "subst_free [(@{term \"(\<lambda>y::nat. y)\"}, @{term \"x::nat\"})] 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   851
  @{term \"(\<lambda>x::nat. x)\"}"
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   852
  "Free (\"x\", \"nat\")"}
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   853
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   854
  \begin{readmore}
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   855
  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>
parents: 101
diff changeset
   856
  @{ML_file "HOL/Tools/hologic.ML"} that make such manual constructions of terms 
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   857
  and types easier.\end{readmore}
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   858
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   859
  Have a look at these files and try to solve the following two exercises:
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   860
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   861
  \begin{exercise}\label{fun:revsum}
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   862
  Write a function @{text "rev_sum : term -> term"} that takes a
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   863
  term of the form @{text "t\<^isub>1 + t\<^isub>2 + \<dots> + t\<^isub>n"} (whereby @{text "n"} might be zero)
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   864
  and returns the reversed sum @{text "t\<^isub>n + \<dots> + t\<^isub>2 + t\<^isub>1"}. Assume
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   865
  the @{text "t\<^isub>i"} can be arbitrary expressions and also note that @{text "+"} 
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
   866
  associates to the left. Try your function on some examples. 
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   867
  \end{exercise}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   868
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   869
  \begin{exercise}\label{fun:makesum}
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   870
  Write a function which takes two terms representing natural numbers
199
griff
parents: 198
diff changeset
   871
  in unary notation (like @{term "Suc (Suc (Suc 0))"}), and produces the
11
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   872
  number representing their sum.
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   873
  \end{exercise}
733614e236a3 tuned and updated antquote_setup.ML
Christian Urban <urbanc@in.tum.de>
parents: 10
diff changeset
   874
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   875
  There are a few subtle issues with constants. They usually crop up when
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   876
  pattern matching terms or types, or when constructing them. While it is perfectly ok
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   877
  to write the function @{text is_true} as follows
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   878
*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   879
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   880
ML{*fun is_true @{term True} = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   881
  | is_true _ = false*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   882
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   883
text {*
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   884
  this does not work for picking out @{text "\<forall>"}-quantified terms. Because
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   885
  the function 
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   886
*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   887
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   888
ML{*fun is_all (@{term All} $ _) = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   889
  | is_all _ = false*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   890
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   891
text {* 
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   892
  will not correctly match the formula @{prop "\<forall>x::nat. P x"}: 
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   893
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   894
  @{ML_response [display,gray] "is_all @{term \"\<forall>x::nat. P x\"}" "false"}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   895
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   896
  The problem is that the @{text "@term"}-antiquotation in the pattern 
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   897
  fixes the type of the constant @{term "All"} to be @{typ "('a \<Rightarrow> bool) \<Rightarrow> bool"} for 
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   898
  an arbitrary, but fixed type @{typ "'a"}. A properly working alternative 
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   899
  for this function is
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   900
*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   901
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   902
ML{*fun is_all (Const ("All", _) $ _) = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   903
  | is_all _ = false*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   904
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   905
text {* 
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   906
  because now
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   907
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   908
@{ML_response [display,gray] "is_all @{term \"\<forall>x::nat. P x\"}" "true"}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   909
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   910
  matches correctly (the first wildcard in the pattern matches any type and the
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   911
  second any term).
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   912
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   913
  However there is still a problem: consider the similar function that
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   914
  attempts to pick out @{text "Nil"}-terms:
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   915
*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   916
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   917
ML{*fun is_nil (Const ("Nil", _)) = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   918
  | is_nil _ = false *}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   919
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   920
text {* 
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   921
  Unfortunately, also this function does \emph{not} work as expected, since
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   922
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   923
  @{ML_response [display,gray] "is_nil @{term \"Nil\"}" "false"}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   924
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   925
  The problem is that on the ML-level the name of a constant is more
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   926
  subtle than you might expect. The function @{ML is_all} worked correctly,
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   927
  because @{term "All"} is such a fundamental constant, which can be referenced
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   928
  by @{ML "Const (\"All\", some_type)" for some_type}. However, if you look at
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   929
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   930
  @{ML_response [display,gray] "@{term \"Nil\"}" "Const (\"List.list.Nil\", \<dots>)"}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   931
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   932
  the name of the constant @{text "Nil"} depends on the theory in which the
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   933
  term constructor is defined (@{text "List"}) and also in which datatype
128
693711a0c702 polished
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   934
  (@{text "list"}). Even worse, some constants have a name involving
693711a0c702 polished
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   935
  type-classes. Consider for example the constants for @{term "zero"} and
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   936
  \mbox{@{text "(op *)"}}:
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   937
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   938
  @{ML_response [display,gray] "(@{term \"0::nat\"}, @{term \"op *\"})" 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   939
 "(Const (\"HOL.zero_class.zero\", \<dots>), 
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   940
 Const (\"HOL.times_class.times\", \<dots>))"}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   941
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   942
  While you could use the complete name, for example 
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   943
  @{ML "Const (\"List.list.Nil\", some_type)" for some_type}, for referring to or
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   944
  matching against @{text "Nil"}, this would make the code rather brittle. 
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   945
  The reason is that the theory and the name of the datatype can easily change. 
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   946
  To make the code more robust, it is better to use the antiquotation 
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   947
  @{text "@{const_name \<dots>}"}. With this antiquotation you can harness the 
200
f9b7bf8aad3e small changes
griff
parents: 199
diff changeset
   948
  variable parts of the constant's name. Therefore a function for 
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   949
  matching against constants that have a polymorphic type should 
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
   950
  be written as follows.
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   951
*}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   952
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   953
ML{*fun is_nil_or_all (Const (@{const_name "Nil"}, _)) = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   954
  | is_nil_or_all (Const (@{const_name "All"}, _) $ _) = true
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   955
  | is_nil_or_all _ = false *}
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   956
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   957
text {*
242
14d5f0cf91dc added note about @{type_names ...}
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
   958
  The antiquotation for properly referencing type constants is  is @{text "@{type_name \<dots>}"}.
14d5f0cf91dc added note about @{type_names ...}
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
   959
  For example
14d5f0cf91dc added note about @{type_names ...}
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
   960
14d5f0cf91dc added note about @{type_names ...}
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
   961
  @{ML_response [display,gray]
14d5f0cf91dc added note about @{type_names ...}
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
   962
  "@{type_name \"list\"}" "\"List.list\""}
14d5f0cf91dc added note about @{type_names ...}
Christian Urban <urbanc@in.tum.de>
parents: 240
diff changeset
   963
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   964
  (FIXME: Explain the following better.)
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   965
200
f9b7bf8aad3e small changes
griff
parents: 199
diff changeset
   966
  Occasionally you have to calculate what the ``base'' name of a given
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   967
  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>
parents: 162
diff changeset
   968
  @{ML Long_Name.base_name}. For example:
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   969
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   970
  @{ML_response [display,gray] "Sign.extern_const @{theory} \"List.list.Nil\"" "\"Nil\""}
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   971
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   972
  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>
parents: 162
diff changeset
   973
  the smallest name that is still unique, whereas @{ML base_name in Long_Name} always
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   974
  strips off all qualifiers.
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   975
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   976
  \begin{readmore}
162
3fb9f820a294 some additions to the simplifier section and general tuning
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   977
  Functions about naming are implemented in @{ML_file "Pure/General/name_space.ML"};
3fb9f820a294 some additions to the simplifier section and general tuning
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
   978
  functions about signatures in @{ML_file "Pure/sign.ML"}.
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   979
  \end{readmore}
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   980
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   981
  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>
parents: 185
diff changeset
   982
  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>
parents: 185
diff changeset
   983
  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>
parents: 185
diff changeset
   984
  type is as follows:
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   985
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   986
*} 
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   987
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   988
ML{*fun make_fun_type ty1 ty2 = Type ("fun", [ty1, ty2]) *}
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   989
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   990
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>
parents: 185
diff changeset
   991
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
   992
ML{*fun make_fun_type ty1 ty2 = ty1 --> ty2 *}
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   993
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   994
text {*
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   995
  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>
parents: 185
diff changeset
   996
  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>
parents: 185
diff changeset
   997
  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>
parents: 185
diff changeset
   998
*}
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
   999
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
  1000
ML{*fun nat_to_int t =
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
  1001
  (case t of
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
  1002
     @{typ nat} => @{typ int}
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
  1003
   | 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>
parents: 185
diff changeset
  1004
   | _ => t)*}
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
  1005
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
  1006
text {*
200
f9b7bf8aad3e small changes
griff
parents: 199
diff changeset
  1007
  Here is an example:
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
  1008
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
  1009
@{ML_response_fake [display,gray] 
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
  1010
"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>
parents: 185
diff changeset
  1011
"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>
parents: 185
diff changeset
  1012
           $ 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>
parents: 185
diff changeset
  1013
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1014
  If you want to obtain the list of free type-variables of a term, you
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1015
  can use the function @{ML Term.add_tfrees} (similarly @{ML Term.add_tvars}
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1016
  for the schematic type-variables). One would expect that such functions
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1017
  take a term as input and return a list of types. But their type is actually 
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1018
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1019
  @{text[display] "Term.term -> (string * Term.sort) list -> (string * Term.sort) list"}
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  1020
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1021
  that is they take, besides a term, also a list of type-variables as input. 
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1022
  So in order to obtain the list of type-variables of a term you have to 
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1023
  call them as follows
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1024
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1025
  @{ML_response [gray,display]
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1026
  "Term.add_tfrees @{term \"(a,b)\"} []"
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1027
  "[(\"'b\", [\"HOL.type\"]), (\"'a\", [\"HOL.type\"])]"}
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1028
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1029
  The reason for this definition is that @{ML Term.add_tfrees} can
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1030
  be easily folded over a list of terms. Similarly for all functions
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1031
  named @{text "add_"}some\_name in @{ML_file "Pure/term.ML"}.
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
  1032
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
  1033
*}
162
3fb9f820a294 some additions to the simplifier section and general tuning
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
  1034
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
  1035
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1036
section {* Type-Checking *}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1037
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1038
text {* 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1039
  
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
  1040
  You can freely construct and manipulate @{ML_type "term"}s and @{ML_type
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
  1041
  typ}es, since they are just arbitrary unchecked trees. However, you
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
  1042
  eventually want to see if a term is well-formed, or type-checks, relative to
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
  1043
  a theory.  Type-checking is done via the function @{ML cterm_of}, which
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
  1044
  converts a @{ML_type term} into a @{ML_type cterm}, a \emph{certified}
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
  1045
  term. Unlike @{ML_type term}s, which are just trees, @{ML_type "cterm"}s are
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
  1046
  abstract objects that are guaranteed to be type-correct, and they can only
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
  1047
  be constructed via ``official interfaces''.
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
  1048
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1049
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1050
  Type-checking is always relative to a theory context. For now we use
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1051
  the @{ML "@{theory}"} antiquotation to get hold of the current theory.
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1052
  For example you can write:
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1053
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
  1054
  @{ML_response_fake [display,gray] "cterm_of @{theory} @{term \"(a::nat) + b = c\"}" "a + b = c"}
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1055
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1056
  This can also be written with an antiquotation:
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1057
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1058
  @{ML_response_fake [display,gray] "@{cterm \"(a::nat) + b = c\"}" "a + b = c"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1059
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1060
  Attempting to obtain the certified term for
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1061
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1062
  @{ML_response_fake_both [display,gray] "@{cterm \"1 + True\"}" "Type unification failed \<dots>"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1063
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1064
  yields an error (since the term is not typable). A slightly more elaborate
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1065
  example that type-checks is:
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1066
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1067
@{ML_response_fake [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1068
"let
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1069
  val natT = @{typ \"nat\"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1070
  val zero = @{term \"0::nat\"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1071
in
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1072
  cterm_of @{theory} 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1073
      (Const (@{const_name plus}, natT --> natT --> natT) $ zero $ zero)
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1074
end" "0 + 0"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1075
186
371e4375c994 made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents: 185
diff changeset
  1076
  In Isabelle not just terms need to be certified, but also types. For example, 
198
195e7bcbf618 again spell-checker on FirstSteps
griff
parents: 197
diff changeset
  1077
  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>
parents: 177
diff changeset
  1078
  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>
parents: 158
diff changeset
  1079
cc9359bfacf4 redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
  1080
  @{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>
parents: 158
diff changeset
  1081
  "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>
parents: 158
diff changeset
  1082
  "nat \<Rightarrow> bool"}
cc9359bfacf4 redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
  1083
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1084
  or with the antiquotation:
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1085
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1086
  @{ML_response_fake [display,gray]
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1087
  "@{ctyp \"nat \<Rightarrow> bool\"}"
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1088
  "nat \<Rightarrow> bool"}
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1089
160
cc9359bfacf4 redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
  1090
  \begin{readmore}
cc9359bfacf4 redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
  1091
  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>
parents: 158
diff changeset
  1092
  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>
parents: 158
diff changeset
  1093
  \end{readmore}
cc9359bfacf4 redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
  1094
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1095
  \begin{exercise}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1096
  Check that the function defined in Exercise~\ref{fun:revsum} returns a
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1097
  result that type-checks.
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1098
  \end{exercise}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1099
200
f9b7bf8aad3e small changes
griff
parents: 199
diff changeset
  1100
  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>
parents: 177
diff changeset
  1101
  enough typing information (constants, free variables and abstractions all have typing 
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1102
  information) so that it is always clear what the type of a term is. 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1103
  Given a well-typed term, the function @{ML type_of} returns the 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1104
  type of a term. Consider for example:
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1105
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1106
  @{ML_response [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1107
  "type_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::nat\"})" "bool"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1108
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1109
  To calculate the type, this function traverses the whole term and will
197
2fe1877f705f used spell checker on `FirstSteps' + small changes
griff
parents: 196
diff changeset
  1110
  detect any typing inconsistency. For example changing the type of the variable 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
  1111
  @{term "x"} from @{typ "nat"} to @{typ "int"} will result in the error message: 
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1112
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1113
  @{ML_response_fake [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1114
  "type_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::int\"})" 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1115
  "*** Exception- TYPE (\"type_of: type mismatch in application\" \<dots>"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1116
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1117
  Since the complete traversal might sometimes be too costly and
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
  1118
  not necessary, there is the function @{ML fastype_of}, which 
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
  1119
  also returns the type of a term.
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1120
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1121
  @{ML_response [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1122
  "fastype_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::nat\"})" "bool"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1123
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
  1124
  However, efficiency is gained on the expense of skipping some tests. You 
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1125
  can see this in the following example
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1126
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1127
   @{ML_response [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1128
  "fastype_of (@{term \"f::nat \<Rightarrow> bool\"} $ @{term \"x::int\"})" "bool"}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1129
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
  1130
  where no error is detected.
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1131
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1132
  Sometimes it is a bit inconvenient to construct a term with 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1133
  complete typing annotations, especially in cases where the typing 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1134
  information is redundant. A short-cut is to use the ``place-holder'' 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1135
  type @{ML "dummyT"} and then let type-inference figure out the 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1136
  complete type. An example is as follows:
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1137
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1138
  @{ML_response_fake [display,gray] 
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1139
"let
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1140
  val c = Const (@{const_name \"plus\"}, dummyT) 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1141
  val o = @{term \"1::nat\"} 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1142
  val v = Free (\"x\", dummyT)
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1143
in   
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1144
  Syntax.check_term @{context} (c $ o $ v)
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1145
end"
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1146
"Const (\"HOL.plus_class.plus\", \"nat \<Rightarrow> nat \<Rightarrow> nat\") $
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1147
  Const (\"HOL.one_class.one\", \"nat\") $ Free (\"x\", \"nat\")"}
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1148
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1149
  Instead of giving explicitly the type for the constant @{text "plus"} and the free 
200
f9b7bf8aad3e small changes
griff
parents: 199
diff changeset
  1150
  variable @{text "x"}, type-inference fills in the missing information.
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1151
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1152
  \begin{readmore}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1153
  See @{ML_file "Pure/Syntax/syntax.ML"} where more functions about reading,
200
f9b7bf8aad3e small changes
griff
parents: 199
diff changeset
  1154
  checking and pretty-printing of terms are defined. Functions related to
f9b7bf8aad3e small changes
griff
parents: 199
diff changeset
  1155
  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>
parents: 158
diff changeset
  1156
  @{ML_file "Pure/type_infer.ML"}. 
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1157
  \end{readmore}
162
3fb9f820a294 some additions to the simplifier section and general tuning
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
  1158
3fb9f820a294 some additions to the simplifier section and general tuning
Christian Urban <urbanc@in.tum.de>
parents: 161
diff changeset
  1159
  (FIXME: say something about sorts)
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1160
*}
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1161
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1162
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1163
section {* Theorems *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1164
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1165
text {*
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
  1166
  Just like @{ML_type cterm}s, theorems are abstract objects of type @{ML_type thm} 
201
e1dbcccf970f passive of `build' = `built'
griff
parents: 200
diff changeset
  1167
  that can only be built by going through interfaces. As a consequence, every proof 
124
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1168
  in Isabelle is correct by construction. This follows the tradition of the LCF approach
0b9fa606a746 added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
  1169
  \cite{GordonMilnerWadsworth79}.
107
258ce361ba1b polished and more material in the tactic chapter
Christian Urban <urbanc@in.tum.de>
parents: 104
diff changeset
  1170
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1171
78
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
  1172
  To see theorems in ``action'', let us give a proof on the ML-level for the following 
ef778679d3e0 added a section about combinators
Christian Urban <urbanc@in.tum.de>
parents: 75
diff changeset
  1173
  statement:
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1174
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1175
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1176
  lemma 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1177
   assumes assm\<^isub>1: "\<And>(x::nat). P x \<Longrightarrow> Q x" 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1178
   and     assm\<^isub>2: "P t"
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
  1179
   shows "Q t" (*<*)oops(*>*) 
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1180
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1181
text {*
185
043ef82000b4 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
  1182
  The corresponding ML-code is as follows:
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1183
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
  1184
@{ML_response_fake [display,gray]
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
  1185
"let
138
e4d8dfb7e34a included comment from Sascha
Christian Urban <urbanc@in.tum.de>
parents: 136
diff changeset
  1186
  val assm1 = @{cprop \"\<And>(x::nat). P x \<Longrightarrow> Q x\"}
e4d8dfb7e34a included comment from Sascha
Christian Urban <urbanc@in.tum.de>
parents: 136
diff changeset
  1187
  val assm2 = @{cprop \"(P::nat\<Rightarrow>bool) t\"}
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1188
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1189
  val Pt_implies_Qt = 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1190
        assume assm1
138
e4d8dfb7e34a included comment from Sascha
Christian Urban <urbanc@in.tum.de>
parents: 136
diff changeset
  1191
        |> forall_elim @{cterm \"t::nat\"};
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1192
  
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1193
  val Qt = implies_elim Pt_implies_Qt (assume assm2);
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1194
in
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1195
  Qt 
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1196
  |> implies_intr assm2
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1197
  |> implies_intr assm1
48
609f9ef73494 fixed FIXME's in fake responses
Christian Urban <urbanc@in.tum.de>
parents: 47
diff changeset
  1198
end" "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
  1199
21
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1200
  This code-snippet constructs the following proof:
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1201
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1202
  \[
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1203
  \infer[(@{text "\<Longrightarrow>"}$-$intro)]{\vdash @{prop "(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> P t \<Longrightarrow> Q t"}}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1204
    {\infer[(@{text "\<Longrightarrow>"}$-$intro)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1205
       {\infer[(@{text "\<Longrightarrow>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"}, @{prop "P t"} \vdash @{prop "Q t"}}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1206
          {\infer[(@{text "\<And>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1207
                 {\infer[(assume)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "\<And>x. P x \<Longrightarrow> Q x"}}{}}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1208
                 &
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1209
           \infer[(assume)]{@{prop "P t"} \vdash @{prop "P t"}}{}
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1210
          }
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1211
       }
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1212
    }
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1213
  \]
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1214
102
5e309df58557 general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents: 101
diff changeset
  1215
  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>
parents: 101
diff changeset
  1216
  yet stored in Isabelle's theorem database. So it cannot be referenced later
128
693711a0c702 polished
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
  1217
  on. How to store theorems will be explained in Section~\ref{sec:storing}.
21
2356e5c70d98 added a proof and tuned the rest
Christian Urban <urbanc@in.tum.de>
parents: 20
diff changeset
  1218
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
  1219
  \begin{readmore}
50
Christian Urban <urbanc@in.tum.de>
parents: 49
diff changeset
  1220
  For the functions @{text "assume"}, @{text "forall_elim"} etc 
13
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
  1221
  see \isccite{sec:thms}. The basic functions for theorems are defined in
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
  1222
  @{ML_file "Pure/thm.ML"}. 
2b07da8b310d polished and added a subdirectory for the recipes
Christian Urban <urbanc@in.tum.de>
parents: 12
diff changeset
  1223
  \end{readmore}
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
  1224
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1225
  (FIXME: handy functions working on theorems, like @{ML ObjectLogic.rulify} and so on) 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1226
230
8def50824320 added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents: 229
diff changeset
  1227
  (FIXME: how to add case-names to goal states - maybe in the 
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1228
  next section)
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1229
*}
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
  1230
243
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1231
section {* Setups (TBD) *}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1232
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1233
text {*
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1234
  In the previous section we used \isacommand{setup} in order to make
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1235
  a theorem attribute known to Isabelle. What happens behind the scenes
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1236
  is that \isacommand{setup} expects a function of type 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1237
  @{ML_type "theory -> theory"}: the input theory is the current theory and the 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1238
  output the theory where the theory attribute has been stored.
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1239
  
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1240
  This is a fundamental principle in Isabelle. A similar situation occurs 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1241
  for example with declaring constants. The function that declares a 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1242
  constant on the ML-level is @{ML Sign.add_consts_i}. 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1243
  If you write\footnote{Recall that ML-code  needs to be 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1244
  enclosed in \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.} 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1245
*}  
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1246
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1247
ML{*Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] @{theory} *}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1248
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1249
text {*
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1250
  for declaring the constant @{text "BAR"} with type @{typ nat} and 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1251
  run the code, then you indeed obtain a theory as result. But if you 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1252
  query the constant on the Isabelle level using the command \isacommand{term}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1253
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1254
  \begin{isabelle}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1255
  \isacommand{term}~@{text [quotes] "BAR"}\\
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1256
  @{text "> \"BAR\" :: \"'a\""}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1257
  \end{isabelle}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1258
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1259
  you do not obtain a constant of type @{typ nat}, but a free variable (printed in 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1260
  blue) of polymorphic type. The problem is that the ML-expression above did 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1261
  not register the declaration with the current theory. This is what the command
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1262
  \isacommand{setup} is for. The constant is properly declared with
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1263
*}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1264
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1265
setup %gray {* Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] *}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1266
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1267
text {* 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1268
  Now 
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1269
  
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1270
  \begin{isabelle}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1271
  \isacommand{term}~@{text [quotes] "BAR"}\\
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1272
  @{text "> \"BAR\" :: \"nat\""}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1273
  \end{isabelle}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1274
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1275
  returns a (black) constant with the type @{typ nat}.
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1276
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1277
  A similar command is \isacommand{local\_setup}, which expects a function
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1278
  of type @{ML_type "local_theory -> local_theory"}. Later on we will also
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1279
  use the commands \isacommand{method\_setup} for installing methods in the
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1280
  current theory and \isacommand{simproc\_setup} for adding new simprocs to
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1281
  the current simpset.
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1282
*}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1283
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
  1284
section {* Theorem Attributes *}
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
  1285
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
  1286
text {*
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1287
  Theorem attributes are @{text "[symmetric]"}, @{text "[THEN \<dots>]"}, @{text
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1288
  "[simp]"} and so on. Such attributes are \emph{neither} tags \emph{nor} flags
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1289
  annotated to theorems, but functions that do further processing once a
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1290
  theorem is proved. In particular, it is not possible to find out
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1291
  what are all theorems that have a given attribute in common, unless of course
197
2fe1877f705f used spell checker on `FirstSteps' + small changes
griff
parents: 196
diff changeset
  1292
  the function behind the attribute stores the theorems in a retrievable 
2fe1877f705f used spell checker on `FirstSteps' + small changes
griff
parents: 196
diff changeset
  1293
  data structure. 
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
  1294
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1295
  If you want to print out all currently known attributes a theorem can have, 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1296
  you can use the Isabelle command
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
  1297
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1298
  \begin{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1299
  \isacommand{print\_attributes}\\
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1300
  @{text "> COMP:  direct composition with rules (no lifting)"}\\
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1301
  @{text "> HOL.dest:  declaration of Classical destruction rule"}\\
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1302
  @{text "> HOL.elim:  declaration of Classical elimination rule"}\\
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1303
  @{text "> \<dots>"}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1304
  \end{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1305
  
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1306
  The theorem attributes fall roughly into two categories: the first category manipulates
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1307
  the proved theorem (for example @{text "[symmetric]"} and @{text "[THEN \<dots>]"}), and the second
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1308
  stores the proved theorem somewhere as data (for example @{text "[simp]"}, which adds
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1309
  the theorem to the current simpset).
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
  1310
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1311
  To explain how to write your own attribute, let us start with an extremely simple 
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1312
  version of the attribute @{text "[symmetric]"}. The purpose of this attribute is
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1313
  to produce the ``symmetric'' version of an equation. The main function behind 
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1314
  this attribute is
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
  1315
*}
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
  1316
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1317
ML{*val my_symmetric = Thm.rule_attribute (fn _ => fn thm => thm RS @{thm sym})*}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1318
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1319
text {* 
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1320
  where the function @{ML "Thm.rule_attribute"} expects a function taking a 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
  1321
  context (which we ignore in the code above) and a theorem (@{text thm}), and 
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1322
  returns another theorem (namely @{text thm} resolved with the theorem 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1323
  @{thm [source] sym}: @{thm sym[no_vars]}).\footnote{The function @{ML RS} is explained
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1324
  later on in Section~\ref{sec:simpletacs}.} The function 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1325
  @{ML "Thm.rule_attribute"} then returns 
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1326
  an attribute.
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1327
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1328
  Before we can use the attribute, we need to set it up. This can be done
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1329
  using the Isabelle command \isacommand{attribute\_setup} as follows:
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1330
*}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1331
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1332
attribute_setup %gray my_sym = {* Scan.succeed my_symmetric *} 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1333
  "applying the sym rule"
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1334
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1335
text {*
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1336
  Inside the @{text "\<verbopen> \<dots> \<verbclose>"}, we have to specify a parser
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1337
  for the theorem attribute. Since the attribute does not expect any further 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1338
  arguments (unlike @{text "[THEN \<dots>]"}, for example), we use the parser @{ML
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1339
  Scan.succeed}. Later on we will also consider attributes taking further
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1340
  arguments. An example for the attribute @{text "[my_sym]"} is the proof
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1341
*} 
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1342
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1343
lemma test[my_sym]: "2 = Suc (Suc 0)" by simp
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1344
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1345
text {*
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1346
  which stores the theorem @{thm test} under the name @{thm [source] test}. You
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1347
  can see this, if you query the lemma: 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1348
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1349
  \begin{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1350
  \isacommand{thm}~@{text "test"}\\
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1351
  @{text "> "}~@{thm test}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1352
  \end{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1353
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1354
  We can also use the attribute when referring to this theorem:
136
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1355
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1356
  \begin{isabelle}
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1357
  \isacommand{thm}~@{text "test[my_sym]"}\\
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1358
  @{text "> "}~@{thm test[my_sym]}
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1359
  \end{isabelle}
58277de8493c to be in sync with Sascha
Christian Urban <urbanc@in.tum.de>
parents: 134
diff changeset
  1360
243
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1361
  An alternative for setting up an attribute is the function @{ML Attrib.setup}.
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1362
  So instead of using \isacommand{attribute\_setup}, you can also set up the
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1363
  attribute as follows:
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1364
*}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1365
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1366
ML{*Attrib.setup @{binding "my_sym"} (Scan.succeed my_symmetric)
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1367
  "applying the sym rule" *}
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1368
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1369
text {*
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1370
  This gives a function from @{ML_type "Context.theory -> Context.theory"}, which
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1371
  can be used for example with \isacommand{setup}.
6e0f56764ff8 slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 242
diff changeset
  1372
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1373
  As an example of a slightly more complicated theorem attribute, we implement 
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1374
  our own version of @{text "[THEN \<dots>]"}. This attribute will take a list of theorems
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1375
  as argument and resolve the proved theorem with this list (one theorem 
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1376
  after another). The code for this attribute is
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1377
*}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1378
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1379
ML{*fun MY_THEN thms = 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1380
  Thm.rule_attribute (fn _ => fn thm => foldl ((op RS) o swap) thm thms)*}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1381
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1382
text {* 
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1383
  where @{ML swap} swaps the components of a pair. The setup of this theorem
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1384
  attribute uses the parser @{ML Attrib.thms}, which parses a list of
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1385
  theorems. 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1386
*}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1387
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1388
attribute_setup %gray MY_THEN = {* Attrib.thms >> MY_THEN *} 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1389
  "resolving the list of theorems with the proved theorem"
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1390
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1391
text {* 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1392
  You can, for example, use this theorem attribute to turn an equation into a 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1393
  meta-equation:
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1394
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1395
  \begin{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1396
  \isacommand{thm}~@{text "test[MY_THEN eq_reflection]"}\\
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1397
  @{text "> "}~@{thm test[MY_THEN eq_reflection]}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1398
  \end{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1399
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1400
  If you need the symmetric version as a meta-equation, you can write
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1401
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1402
  \begin{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1403
  \isacommand{thm}~@{text "test[MY_THEN sym eq_reflection]"}\\
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1404
  @{text "> "}~@{thm test[MY_THEN sym eq_reflection]}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1405
  \end{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1406
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1407
  It is also possible to combine different theorem attributes, as in:
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1408
  
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1409
  \begin{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1410
  \isacommand{thm}~@{text "test[my_sym, MY_THEN eq_reflection]"}\\
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1411
  @{text "> "}~@{thm test[my_sym, MY_THEN eq_reflection]}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1412
  \end{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1413
  
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1414
  However, here also a weakness of the concept
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1415
  of theorem attributes shows through: since theorem attributes can be
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1416
  arbitrary functions, they do not in general commute. If you try
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1417
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1418
  \begin{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1419
  \isacommand{thm}~@{text "test[MY_THEN eq_reflection, my_sym]"}\\
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1420
  @{text "> "}~@{text "exception THM 1 raised: RSN: no unifiers"}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1421
  \end{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1422
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1423
  you get an exception indicating that the theorem @{thm [source] sym}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1424
  does not resolve with meta-equations. 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1425
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>
parents: 149
diff changeset
  1426
  The purpose of @{ML Thm.rule_attribute} is to directly manipulate theorems.
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1427
  Another usage of theorem attributes is to add and delete theorems from stored data.
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1428
  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>
parents: 149
diff changeset
  1429
  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>
parents: 149
diff changeset
  1430
  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>
parents: 149
diff changeset
  1431
  of theorems.
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1432
*}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1433
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1434
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>
parents: 149
diff changeset
  1435
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>
parents: 149
diff changeset
  1436
text {* 
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1437
  The purpose of this reference is that we are going to add and delete theorems
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1438
  to the referenced list. However, a word of warning: such references must not 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1439
  be used in any code that is meant to be more than just for testing purposes! 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1440
  Here it is only used to illustrate matters. We will show later how to store 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1441
  data properly without using references.
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1442
 
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1443
  We need to provide two functions that add and delete theorems from this list. 
153
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1444
  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>
parents: 149
diff changeset
  1445
*}
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>
parents: 149
diff changeset
  1446
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1447
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>
parents: 149
diff changeset
  1448
  (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>
parents: 149
diff changeset
  1449
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1450
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>
parents: 149
diff changeset
  1451
  (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>
parents: 149
diff changeset
  1452
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1453
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>
parents: 149
diff changeset
  1454
  These functions take a theorem and a context and, for what we are explaining
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1455
  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>
parents: 149
diff changeset
  1456
  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>
parents: 149
diff changeset
  1457
  adds a theorem if it is not already included in the list, and @{ML
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1458
  Thm.del_thm} deletes one (both functions use the predicate @{ML
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1459
  Thm.eq_thm_prop}, which compares theorems according to their proved
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1460
  propositions modulo alpha-equivalence).
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1461
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1462
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1463
  You can turn functions @{ML my_thm_add} and @{ML my_thm_del} into 
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1464
  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>
parents: 149
diff changeset
  1465
*}
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>
parents: 149
diff changeset
  1466
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1467
ML{*val my_add = Thm.declaration_attribute my_thm_add
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1468
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>
parents: 149
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>
parents: 149
diff changeset
  1470
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>
parents: 149
diff changeset
  1471
  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>
parents: 149
diff changeset
  1472
*}
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>
parents: 149
diff changeset
  1473
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1474
attribute_setup %gray my_thms = {* Attrib.add_del my_add my_del *} 
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1475
  "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>
parents: 149
diff changeset
  1476
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>
parents: 149
diff changeset
  1477
text {*
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1478
  The parser @{ML Attrib.add_del} is a pre-defined parser for 
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1479
  adding and deleting lemmas. Now if you prove the next lemma 
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1480
  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>
parents: 149
diff changeset
  1481
*}
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>
parents: 149
diff changeset
  1482
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>
parents: 149
diff changeset
  1483
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>
parents: 149
diff changeset
  1484
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>
parents: 149
diff changeset
  1485
text {*
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1486
  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>
parents: 149
diff changeset
  1487
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>
parents: 149
diff changeset
  1488
  @{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>
parents: 149
diff changeset
  1489
  "!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>
parents: 149
diff changeset
  1490
160
cc9359bfacf4 redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
  1491
  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>
parents: 149
diff changeset
  1492
*}
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>
parents: 149
diff changeset
  1493
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1494
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>
parents: 149
diff changeset
  1495
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>
parents: 149
diff changeset
  1496
text {*
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1497
  With this attribute, the @{text "add"} operation is the default and does 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1498
  not need to be explicitly given. These three declarations will cause the 
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1499
  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>
parents: 149
diff changeset
  1500
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>
parents: 149
diff changeset
  1501
  @{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>
parents: 149
diff changeset
  1502
  "!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>
parents: 149
diff changeset
  1503
  "[\"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>
parents: 149
diff changeset
  1504
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>
parents: 149
diff changeset
  1505
  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>
parents: 149
diff changeset
  1506
  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>
parents: 149
diff changeset
  1507
  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>
parents: 149
diff changeset
  1508
*}
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>
parents: 149
diff changeset
  1509
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>
parents: 149
diff changeset
  1510
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>
parents: 149
diff changeset
  1511
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1512
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>
parents: 149
diff changeset
  1513
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>
parents: 149
diff changeset
  1514
  @{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>
parents: 149
diff changeset
  1515
  "!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>
parents: 149
diff changeset
  1516
  "[\"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>
parents: 149
diff changeset
  1517
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>
parents: 149
diff changeset
  1518
  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>
parents: 149
diff changeset
  1519
  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>
parents: 149
diff changeset
  1520
  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>
parents: 149
diff changeset
  1521
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1522
  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>
parents: 149
diff changeset
  1523
  \emph{not} the received way of doing such things. The received way is to 
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1524
  start a ``data slot'', below called @{text MyThmsData}, generated by the functor 
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1525
  @{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>
parents: 149
diff changeset
  1526
*}
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1527
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1528
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>
parents: 149
diff changeset
  1529
 (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>
parents: 149
diff changeset
  1530
  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>
parents: 149
diff changeset
  1531
  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>
parents: 149
diff changeset
  1532
  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>
parents: 149
diff changeset
  1533
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>
parents: 149
diff changeset
  1534
text {*
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1535
  The type @{text "T"} of this data slot is @{ML_type "thm list"}.\footnote{FIXME: give a pointer
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1536
  to where data slots are explained properly.}
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1537
  To use this data slot, you only have to change @{ML my_thm_add} and
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1538
  @{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>
parents: 149
diff changeset
  1539
*}
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>
parents: 149
diff changeset
  1540
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1541
ML{*val my_thm_add = MyThmsData.map o Thm.add_thm
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1542
val my_thm_del = MyThmsData.map o Thm.del_thm*}
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1543
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1544
text {*
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1545
  where @{ML MyThmsData.map} updates the data appropriately. The
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1546
  corresponding theorem addtributes are
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1547
*}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1548
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1549
ML{*val my_add = Thm.declaration_attribute my_thm_add
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1550
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>
parents: 149
diff changeset
  1551
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>
parents: 149
diff changeset
  1552
text {*
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1553
  and the setup is as follows
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1554
*}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1555
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1556
attribute_setup %gray my_thms2 = {* Attrib.add_del my_add my_del *} 
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1557
  "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>
parents: 149
diff changeset
  1558
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>
parents: 149
diff changeset
  1559
text {*
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1560
  Initially, the data slot is empty 
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1561
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1562
  @{ML_response_fake [display,gray]
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1563
  "MyThmsData.get (Context.Proof @{context})"
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1564
  "[]"}
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1565
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1566
  but if you prove
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1567
*}
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1568
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1569
lemma three[my_thms2]: "3 = Suc (Suc (Suc 0))" by simp
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1570
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1571
text {*
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1572
  then the lemma is recorded. 
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1573
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1574
  @{ML_response_fake [display,gray]
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1575
  "MyThmsData.get (Context.Proof @{context})"
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1576
  "[\"3 = Suc (Suc (Suc 0))\"]"}
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1577
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1578
  With theorem attribute @{text my_thms2} you can also nicely see why it 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1579
  is important to 
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1580
  store data in a ``data slot'' and \emph{not} in a reference. Backtrack
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1581
  to the point just before the lemma @{thm [source] three} was proved and 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1582
  check the the content of @{ML_struct "MyThmsData"}: it should be empty. 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1583
  The addition has been properly retracted. Now consider the proof:
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1584
*}
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1585
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1586
lemma four[my_thms]: "4 = Suc (Suc (Suc (Suc 0)))" by simp
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
  1587
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1588
text {*
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1589
  Checking the content of @{ML my_thms} gives
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1590
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1591
  @{ML_response_fake [display,gray]
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1592
  "!my_thms"
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1593
  "[\"4 = Suc (Suc (Suc (Suc 0)))\", \"True\"]"}
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1594
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1595
  as expected, but if you backtrack before the lemma @{thm [source] four}, the
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1596
  content of @{ML my_thms} is unchanged. The backtracking mechanism
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1597
  of Isabelle is completely oblivious about what to do with references, but
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1598
  properly treats ``data slots''!
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1599
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1600
  Since storing theorems in a list is such a common task, there is the special
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1601
  functor @{text NamedThmsFun}, which does most of the work for you. To obtain
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1602
  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>
parents: 149
diff changeset
  1603
*}
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>
parents: 149
diff changeset
  1604
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>
parents: 149
diff changeset
  1605
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>
parents: 149
diff changeset
  1606
 (val name = "foo" 
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1607
  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>
parents: 149
diff changeset
  1608
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>
parents: 149
diff changeset
  1609
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>
parents: 149
diff changeset
  1610
  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>
parents: 149
diff changeset
  1611
*}
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>
parents: 149
diff changeset
  1612
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
  1613
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>
parents: 149
diff changeset
  1614
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>
parents: 149
diff changeset
  1615
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>
parents: 149
diff changeset
  1616
  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>
parents: 149
diff changeset
  1617
  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>
parents: 149
diff changeset
  1618
  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>
parents: 149
diff changeset
  1619
  modify the theorems.
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1620
157
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
  1621
  Furthermore, the facts are made available on the user-level under the dynamic 
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1622
  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>
parents: 149
diff changeset
  1623
  @{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>
parents: 149
diff changeset
  1624
*}
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>
parents: 149
diff changeset
  1625
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>
parents: 149
diff changeset
  1626
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>
parents: 149
diff changeset
  1627
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>
parents: 149
diff changeset
  1628
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>
parents: 149
diff changeset
  1629
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>
parents: 149
diff changeset
  1630
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>
parents: 149
diff changeset
  1631
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>
parents: 149
diff changeset
  1632
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>
parents: 149
diff changeset
  1633
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>
parents: 149
diff changeset
  1634
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>
parents: 149
diff changeset
  1635
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>
parents: 149
diff changeset
  1636
  \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>
parents: 149
diff changeset
  1637
  \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>
parents: 149
diff changeset
  1638
  @{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>
parents: 149
diff changeset
  1639
  @{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>
parents: 149
diff changeset
  1640
  \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>
parents: 149
diff changeset
  1641
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1642
  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>
parents: 149
diff changeset
  1643
  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>
parents: 149
diff changeset
  1644
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>
parents: 149
diff changeset
  1645
  @{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>
parents: 149
diff changeset
  1646
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>
parents: 149
diff changeset
  1647
  \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>
parents: 149
diff changeset
  1648
  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>
parents: 149
diff changeset
  1649
  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>
parents: 149
diff changeset
  1650
  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>
parents: 149
diff changeset
  1651
  \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>
parents: 149
diff changeset
  1652
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
  1653
  (FIXME What are: @{text "theory_attributes"}, @{text "proof_attributes"}?)
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1654
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1655
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1656
  \begin{readmore}
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1657
  FIXME: @{ML_file "Pure/more_thm.ML"}; parsers for attributes is in 
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1658
  @{ML_file "Pure/Isar/attrib.ML"}...also explained in the chapter about
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
  1659
  parsing.
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1660
  \end{readmore}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1661
*}
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
  1662
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
  1663
178
fb8f22dd8ad0 adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents: 177
diff changeset
  1664
153
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1665
section {* Theories, Contexts and Local Theories (TBD) *}
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
  1666
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1667
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1668
  There are theories, proof contexts and local theories (in this order, if you
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1669
  want to order them). 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1670
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1671
  In contrast to an ordinary theory, which simply consists of a type
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1672
  signature, as well as tables for constants, axioms and theorems, a local
202
16ec70218d26 tiny changes
griff
parents: 201
diff changeset
  1673
  theory contains additional context information, such as locally fixed
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1674
  variables and local assumptions that may be used by the package. The type
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1675
  @{ML_type local_theory} is identical to the type of \emph{proof contexts}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1676
  @{ML_type "Proof.context"}, although not every proof context constitutes a
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1677
  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>
parents: 177
diff changeset
  1678
*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1679
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
  1680
(*
229
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1681
ML{*signature UNIVERSAL_TYPE =
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1682
sig
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1683
  type t
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1684
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1685
  val embed: unit -> ('a -> t) * (t -> 'a option)
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1686
end*}
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1687
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1688
ML{*structure U:> UNIVERSAL_TYPE =
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1689
   struct
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1690
      type t = exn
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1691
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1692
      fun 'a embed () =
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1693
         let
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1694
            exception E of 'a
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1695
            fun project (e: t): 'a option =
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1696
               case e of
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1697
                  E a => SOME a
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1698
                | _ => NONE
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1699
         in
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1700
            (E, project)
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1701
         end
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1702
   end*}
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1703
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1704
text {*
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1705
  The idea is that type t is the universal type and that each call to embed
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1706
  returns a new pair of functions (inject, project), where inject embeds a
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1707
  value into the universal type and project extracts the value from the
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1708
  universal type. A pair (inject, project) returned by embed works together in
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1709
  that project u will return SOME v if and only if u was created by inject
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1710
  v. If u was created by a different function inject', then project returns
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1711
  NONE.
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1712
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1713
  in library.ML
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1714
*}
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1715
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1716
ML_val{*structure Object = struct type T = exn end; *}
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1717
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1718
ML{*functor Test (U: UNIVERSAL_TYPE): sig end =
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1719
   struct
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1720
      val (intIn: int -> U.t, intOut) = U.embed ()
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1721
      val r: U.t ref = ref (intIn 13)
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1722
      val s1 =
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1723
         case intOut (!r) of
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1724
            NONE => "NONE"
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1725
          | SOME i => Int.toString i
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1726
      val (realIn: real -> U.t, realOut) = U.embed ()
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1727
      val () = r := realIn 13.0
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1728
      val s2 =
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1729
         case intOut (!r) of
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1730
            NONE => "NONE"
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1731
          | SOME i => Int.toString i
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1732
      val s3 =
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1733
         case realOut (!r) of
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1734
            NONE => "NONE"
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1735
          | SOME x => Real.toString x
239
b63c72776f03 replaced "warning" with "writeln"
Christian Urban <urbanc@in.tum.de>
parents: 235
diff changeset
  1736
      val () = writeln (concat [s1, " ", s2, " ", s3, "\n"])
229
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1737
   end*}
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1738
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1739
ML_val{*structure t = Test(U) *} 
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1740
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1741
ML_val{*structure Datatab = TableFun(type key = int val ord = int_ord);*}
abc7f90188af permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents: 228
diff changeset
  1742
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
  1743
ML {* LocalTheory.restore *}
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
  1744
ML {* LocalTheory.set_group *}
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
  1745
*)
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
  1746
153
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1747
section {* Storing Theorems\label{sec:storing} (TBD) *}
123
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
  1748
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
  1749
text {* @{ML PureThy.add_thms_dynamic} *}
460bc2ee14e9 some polishing in the first-steps chapter
Christian Urban <urbanc@in.tum.de>
parents: 122
diff changeset
  1750
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  1751
local_setup {* 
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  1752
  LocalTheory.note Thm.theoremK 
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  1753
    ((@{binding "allI_alt"}, []), [@{thm allI}]) #> snd *}
100
dd8eebae11ec polished
Christian Urban <urbanc@in.tum.de>
parents: 92
diff changeset
  1754
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
  1755
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
  1756
(* FIXME: some code below *)
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1757
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1758
(*<*)
163
2319cff107f0 removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
parents: 162
diff changeset
  1759
(*
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1760
setup {*
163
2319cff107f0 removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
parents: 162
diff changeset
  1761
 Sign.add_consts_i [(Binding"bar", @{typ "nat"},NoSyn)] 
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1762
*}
163
2319cff107f0 removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
parents: 162
diff changeset
  1763
*)
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1764
lemma "bar = (1::nat)"
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1765
  oops
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1766
163
2319cff107f0 removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
parents: 162
diff changeset
  1767
(*
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1768
setup {*   
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1769
  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>
parents: 185
diff changeset
  1770
 #> PureThy.add_defs false [((@{binding "foo_def"}, 
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1771
       Logic.mk_equals (Const ("FirstSteps.foo", @{typ "nat"}), @{term "1::nat"})), [])] 
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1772
 #> snd
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1773
*}
163
2319cff107f0 removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
parents: 162
diff changeset
  1774
*)
2319cff107f0 removed rep_ss, and used dest_ss instead; some very slight changes to simple_inductive
Christian Urban <urbanc@in.tum.de>
parents: 162
diff changeset
  1775
(*
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1776
lemma "foo = (1::nat)"
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1777
  apply(simp add: foo_def)
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1778
  done
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1779
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1780
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>
parents: 162
diff changeset
  1781
*)
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1782
(*>*)
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
  1783
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1784
section {* Pretty-Printing\label{sec:pretty} *}
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1785
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1786
text {*
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1787
  So far we printed out only plain strings without any formatting except for
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1788
  occasional explicit linebreaks using @{text [quotes] "\\n"}. This is
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1789
  sufficient for ``quick-and-dirty'' printouts. For something more
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1790
  sophisticated, Isabelle includes an infrastructure for properly formatting text.
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1791
  This infrastructure is loosely based on a paper by Oppen~\cite{Oppen80}. Most of
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1792
  its functions do not operate on @{ML_type string}s, but on instances of the
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1793
  type:
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1794
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1795
  @{ML_type [display, gray] "Pretty.T"}
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1796
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1797
  The function @{ML "Pretty.str"} transforms a (plain) string into such a pretty 
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1798
  type. For example
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1799
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1800
  @{ML_response_fake [display,gray]
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1801
  "Pretty.str \"test\"" "String (\"test\", 4)"}
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1802
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1803
  where the result indicates that we transformed a string with length 4. Once
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1804
  you have a pretty type, you can, for example, control where linebreaks may
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1805
  occur in case the text wraps over a line, or with how much indentation a
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1806
  text should be printed.  However, if you want to actually output the
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1807
  formatted text, you have to transform the pretty type back into a @{ML_type
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1808
  string}. This can be done with the function @{ML Pretty.string_of}. In what
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1809
  follows we will use the following wrapper function for printing a pretty
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1810
  type:
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1811
*}
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1812
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1813
ML{*fun pprint prt = writeln (Pretty.string_of prt)*}
153
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1814
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  1815
text {*
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1816
  The point of the pretty-printing infrastructure is to give hints about how to
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1817
  layout text and let Isabelle do the actual layout. Let us first explain
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1818
  how you can insert places where a linebreak can occur. For this assume the
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1819
  following function that replicates a string n times:
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1820
*}
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1821
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1822
ML{*fun rep n str = implode (replicate n str) *}
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1823
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1824
text {*
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1825
  and suppose we want to print out the string:
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1826
*}
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1827
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1828
ML{*val test_str = rep 8 "fooooooooooooooobaaaaaaaaaaaar "*}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1829
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1830
text {*
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1831
  We deliberately chosed a large string so that is spans over more than one line. 
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1832
  If we print out the string using the usual ``quick-and-dirty'' method, then
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1833
  we obtain the ugly output:
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1834
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1835
@{ML_response_fake [display,gray]
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1836
"writeln test_str"
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1837
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar foooooooooo
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1838
ooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaa
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1839
aaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fo
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1840
oooooooooooooobaaaaaaaaaaaar"}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1841
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1842
  We obtain the same if we just use
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1843
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1844
@{ML_response_fake [display,gray]
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1845
"pprint (Pretty.str test_str)"
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1846
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar foooooooooo
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1847
ooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaa
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1848
aaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fo
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1849
oooooooooooooobaaaaaaaaaaaar"}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1850
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1851
  However by using pretty types you have the ability to indicate a possible
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1852
  linebreak for example at each space. You can achieve this with the function
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1853
  @{ML Pretty.breaks}, which expects a list of pretty types and inserts a
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1854
  possible linebreak in between every two elements in this list. To print
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1855
  this list of pretty types as a single string, we concatenate them 
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1856
  with the function @{ML Pretty.blk} as follows:
210
db8e302f44c8 more work on the simple inductive section
Christian Urban <urbanc@in.tum.de>
parents: 207
diff changeset
  1857
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1858
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1859
@{ML_response_fake [display,gray]
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1860
"let
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1861
  val ptrs = map Pretty.str (space_explode \" \" test_str)
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1862
in
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1863
  pprint (Pretty.blk (0, Pretty.breaks ptrs))
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1864
end"
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1865
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1866
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1867
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1868
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1869
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1870
  Here the layout of @{ML test_str} is much more pleasing to the 
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1871
  eye. The @{ML "0"} in @{ML Pretty.blk} stands for no indentation
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1872
  of the printed string. You can increase the indentation and obtain
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1873
  
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1874
@{ML_response_fake [display,gray]
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1875
"let
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1876
  val ptrs = map Pretty.str (space_explode \" \" test_str)
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1877
in
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1878
  pprint (Pretty.blk (3, Pretty.breaks ptrs))
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1879
end"
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1880
"fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1881
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1882
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1883
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1884
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1885
  where starting from the second line the indent is 3. If you want
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1886
  that every line starts with the same indent, you can use the
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1887
  function @{ML Pretty.indent} as follows:
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1888
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1889
@{ML_response_fake [display,gray]
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1890
"let
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1891
  val ptrs = map Pretty.str (space_explode \" \" test_str)
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1892
in
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1893
  pprint (Pretty.indent 10 (Pretty.blk (0, Pretty.breaks ptrs)))
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1894
end"
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1895
"          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1896
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1897
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1898
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar"}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1899
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1900
  If you want to print out a list of items separated by commas and 
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1901
  have the linebreaks handled properly, you can use the function 
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1902
  @{ML Pretty.commas}. For example
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1903
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1904
@{ML_response_fake [display,gray]
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1905
"let
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1906
  val ptrs = map (Pretty.str o string_of_int) (99998 upto 100020)
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1907
in
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1908
  pprint (Pretty.blk (0, Pretty.commas ptrs))
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1909
end"
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1910
"99998, 99999, 100000, 100001, 100002, 100003, 100004, 100005, 100006, 
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1911
100007, 100008, 100009, 100010, 100011, 100012, 100013, 100014, 100015, 
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1912
100016, 100017, 100018, 100019, 100020"}
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1913
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1914
  where @{ML upto} generates a list of integers. You can print this
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1915
  list out as an ``set'', that means enclosed inside @{text [quotes] "{"} and
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1916
  @{text [quotes] "}"}, and separated by commas using the function
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1917
  @{ML Pretty.enum}. For example
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1918
*}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1919
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1920
text {*
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1921
  
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1922
@{ML_response_fake [display,gray]
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1923
"let
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1924
  val ptrs = map (Pretty.str o string_of_int) (99998 upto 100020)
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1925
in
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1926
  pprint (Pretty.enum \",\" \"{\" \"}\" ptrs)
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1927
end"
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1928
"{99998, 99999, 100000, 100001, 100002, 100003, 100004, 100005, 100006, 
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1929
  100007, 100008, 100009, 100010, 100011, 100012, 100013, 100014, 100015, 
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1930
  100016, 100017, 100018, 100019, 100020}"}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1931
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1932
  As can be seen, this function prints out the ``set'' so that starting 
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1933
  from the second each new line as an indentation of 2.
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1934
  
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1935
  If you print something out which goes beyond the capabilities of the
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1936
  standard function, you can do realatively easily the formating
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1937
  yourself. Assume you want to print out a list of items where like in ``English''
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1938
  the last two items are separated by @{text [quotes] "and"}. For this you can
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1939
  write the function
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1940
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1941
*}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1942
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1943
ML %linenosgray{*fun and_list [] = []
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1944
  | and_list [x] = [x]
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1945
  | and_list xs = 
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1946
      let 
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1947
        val (front, last) = split_last xs
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1948
      in
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1949
        (Pretty.commas front) @ 
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1950
        [Pretty.brk 1, Pretty.str "and", Pretty.brk 1, last]
247
afa2d9c6b3b7 added some text to the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 245
diff changeset
  1951
      end *}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1952
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1953
text {*
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1954
  where in Line 7 we print the beginning of the list and in Line
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1955
  8 the last item. We have to use @{ML "Pretty.brk 1"} in order
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1956
  to insert a space (of length 1) before the 
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1957
  @{text [quotes] "and"}. This space is also a pleace where a linebreak 
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1958
  can occur. We do the same ater the @{text [quotes] "and"}. This gives you
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1959
  for example
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1960
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1961
@{ML_response_fake [display,gray]
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1962
"let
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1963
  val ptrs = map (Pretty.str o string_of_int) (1 upto 22)
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1964
in
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1965
  pprint (Pretty.blk (0, and_list ptrs))
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1966
end"
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1967
"1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1968
and 22"}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1969
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1970
  Next we like to pretty-print a term and its type. For this we use the
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1971
  function @{text "tell_type"}.
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1972
*}
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1973
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1974
ML %linenosgray{*fun tell_type ctxt t = 
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1975
let
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1976
  fun pstr s = Pretty.breaks (map Pretty.str (space_explode " " s))
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1977
  val ptrm = Pretty.quote (Syntax.pretty_term ctxt t)
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1978
  val pty  = Pretty.quote (Syntax.pretty_typ ctxt (fastype_of t))
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1979
in
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1980
  pprint (Pretty.blk (0, 
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1981
    (pstr "The term " @ [ptrm] @ pstr " has type " 
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1982
      @ [pty, Pretty.str "."])))
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1983
end*}
245
53112deda119 Jasmin and Christian added examples for the pretty-printing section
Christian Urban <urbanc@in.tum.de>
parents: 243
diff changeset
  1984
210
db8e302f44c8 more work on the simple inductive section
Christian Urban <urbanc@in.tum.de>
parents: 207
diff changeset
  1985
text {*
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1986
  In Line 3 we define a function that inserts according to spaces 
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1987
  possible linebreaks into an (English). In Lines 4 and 5 we pretty-print
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1988
  the term and its type using the functions @{ML Syntax.pretty_term} and
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1989
  @{ML Syntax.pretty_typ}. We also use the function @{ML Pretty.quote}
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1990
  in order to enclose the term and type inside quotation marks. In Line 
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1991
  9 we add a period right after the type without the possibility of a 
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  1992
  linebreak, because we do not want that a linebreak occurs there.
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1993
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1994
  Now you can write
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1995
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1996
  @{ML_response_fake [display,gray]
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1997
  "tell_type @{context} @{term \"min (Suc 0)\"}"
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1998
  "The term \"min (Suc 0)\" has type \"nat \<Rightarrow> nat\"."}
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  1999
  
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  2000
  To see the proper linebreaking, you can try out the function on a bigger term 
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2001
  and type. For example:
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2002
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2003
  @{ML_response_fake [display,gray]
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2004
  "tell_type @{context} @{term \"op = (op = (op = (op = (op = op =))))\"}"
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2005
  "The term \"op = (op = (op = (op = (op = op =))))\" has type 
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2006
\"((((('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool\"."}
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2007
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2008
*}
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2009
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2010
ML {* pprint (Pretty.big_list "header"  (map (Pretty.str o string_of_int) (4 upto 10))) *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2011
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2012
text {*
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2013
chunks inserts forced breaks (unlike blk where you have to do this yourself)
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2014
*}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2015
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2016
ML {* (Pretty.chunks [Pretty.str "a", Pretty.str "b"], 
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2017
       Pretty.blk (0, [Pretty.str "a", Pretty.str "b"])) *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2018
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2019
ML {*
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2020
fun setmp_show_all_types f =
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2021
   setmp show_all_types
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2022
         (! show_types orelse ! show_sorts orelse ! show_all_types) f;
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2023
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2024
val ctxt = @{context};
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2025
val t1 = @{term "undefined::nat"};
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2026
val t2 = @{term "Suc y"};
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2027
val pty =        Pretty.block (Pretty.breaks
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2028
             [(setmp show_question_marks false o setmp_show_all_types)
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2029
                  (Syntax.pretty_term ctxt) t1,
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2030
              Pretty.str "=", Syntax.pretty_term ctxt t2]);
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2031
pty |> Pretty.string_of |> priority
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2032
*}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2033
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2034
text {* the infrastructure of Syntax-pretty-term makes sure it is printed nicely  *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2035
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2036
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2037
ML {* Pretty.mark Markup.hilite (Pretty.str "foo") |> Pretty.string_of  |> writeln *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2038
ML {* (Pretty.str "bar") |> Pretty.string_of |> writeln *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2039
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2040
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2041
ML {* Pretty.mark Markup.subgoal (Pretty.str "foo") |> Pretty.string_of  |> writeln *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2042
ML {* (Pretty.str "bar") |> Pretty.string_of |> writeln *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2043
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2044
text {*
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2045
  Still to be done:
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2046
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2047
  What happens with big formulae?
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2048
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2049
  \begin{readmore}
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  2050
  The general infrastructure for pretty-printing is implemented in the file
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2051
  @{ML_file "Pure/General/pretty.ML"}. The file @{ML_file "Pure/Syntax/syntax.ML"}
249
5c211dd7e5ad slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 248
diff changeset
  2052
  contains pretty-printing functions for terms, types, theorems and so on.
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2053
  
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2054
  @{ML_file "Pure/General/markup.ML"}
248
11851b20fb78 added more to the pretty section and updated the acknowledgements
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
  2055
  \end{readmore}
153
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  2056
*}
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  2057
250
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2058
text {*
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2059
  printing into the goal buffer as part of the proof state
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2060
*}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2061
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2062
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2063
ML {* Pretty.mark Markup.hilite (Pretty.str "foo") |> Pretty.string_of  |> writeln *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2064
ML {* (Pretty.str "bar") |> Pretty.string_of |> writeln *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2065
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2066
text {* writing into the goal buffer *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2067
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2068
ML {* fun my_hook interactive state =
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2069
         (interactive ? Proof.goal_message (fn () => Pretty.str  
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2070
"foo")) state
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2071
*}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2072
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2073
setup {* Context.theory_map (Specification.add_theorem_hook my_hook) *}
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2074
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2075
lemma "False"
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2076
oops
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2077
ab9e09076462 some polishing; added together with Jasmin more examples to the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 249
diff changeset
  2078
153
c22b507e1407 general polishing; added versioning to the document
Christian Urban <urbanc@in.tum.de>
parents: 151
diff changeset
  2079
section {* Misc (TBD) *}
92
Christian Urban <urbanc@in.tum.de>
parents: 89
diff changeset
  2080
Christian Urban <urbanc@in.tum.de>
parents: 89
diff changeset
  2081
ML {*DatatypePackage.get_datatype @{theory} "List.list"*}
Christian Urban <urbanc@in.tum.de>
parents: 89
diff changeset
  2082
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
  2083
end