ProgTutorial/First_Steps.thy
author Christian Urban <urbanc@in.tum.de>
Thu, 17 Nov 2011 16:33:49 +0000
changeset 502 615780a701b6
parent 496 80eb66aefc66
child 504 1d1165432c9f
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
     1
theory First_Steps
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
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
     5
(*<*)
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
     6
setup{*
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
     7
open_file_with_prelude 
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
     8
  "First_Steps_Code.thy"
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
     9
  ["theory First_Steps", "imports Main", "begin"]
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
    10
*}
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
    11
(*>*)
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
    12
421
620a24bf954a added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
    13
chapter {* First Steps\label{chp:firststeps} *}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
    15
text {*
489
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
    16
  \begin{flushright}
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
    17
  {\em ``The most effective debugging tool is still careful thought,\\ 
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
    18
  coupled with judiciously placed print statements.''} \\[1ex]
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
    19
  Brian Kernighan, in {\em Unix for Beginners}, 1979
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    20
  \end{flushright}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    21
324
4172c0743cf2 updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    22
  \medskip
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    23
  Isabelle programming is done in ML. Just like lemmas and proofs, ML-code for
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    24
  Isabelle must be 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
    25
  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
    26
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
    27
  \begin{quote}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    28
  \begin{tabular}{@ {}l}
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
    29
  \isacommand{theory} First\_Steps\\
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    30
  \isacommand{imports} Main\\
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    31
  \isacommand{begin}\\
6
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    32
  \ldots
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    33
  \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
    34
  \end{quote}
157
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
    35
324
4172c0743cf2 updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    36
  We also generally assume you are working with the logic HOL. The examples
4172c0743cf2 updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
    37
  that will be given might 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
    38
*}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    39
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
    40
section {* Including ML-Code\label{sec:include} *}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    41
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    42
text {*
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    43
  The easiest and quickest way to include code in a theory is by using the
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    44
  \isacommand{ML}-command. For example:
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    45
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    46
  \begin{isabelle}
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    47
  \begin{graybox}
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    48
  \isacommand{ML}~@{text "\<verbopen>"}\isanewline
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    49
  \hspace{5mm}@{ML "3 + 4"}\isanewline
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    50
  @{text "\<verbclose>"}\isanewline
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    51
  @{text "> 7"}\smallskip
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    52
  \end{graybox}
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    53
  \end{isabelle}
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    54
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    55
  If you work with ProofGeneral then like normal Isabelle scripts
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    56
  \isacommand{ML}-commands can be evaluated by using the advance and
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    57
  undo buttons of your Isabelle environment.  If you work with the
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    58
  Jedit GUI, then you just have to hover the cursor over the code 
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    59
  and you see the evaluated result in the ``Output'' window.
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    60
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    61
  As mentioned in the Introduction, we will drop the \isacommand{ML}~@{text
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    62
  "\<verbopen> \<dots> \<verbclose>"} scaffolding whenever we show code. The lines
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    63
  prefixed with @{text [quotes] ">"} are not part of the code, rather they
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    64
  indicate what the response is when the code is evaluated.  There are also
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    65
  the commands \isacommand{ML\_val} and \isacommand{ML\_prf} for including
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    66
  ML-code. The first evaluates the given code, but any effect on the theory,
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    67
  in which the code is embedded, is suppressed. The second needs to be used if
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    68
  ML-code is defined inside a proof. For example
253
3cfd9a8a6de1 added comments about ML_prf and ML_val
Christian Urban <urbanc@in.tum.de>
parents: 252
diff changeset
    69
254
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    70
  \begin{quote}
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    71
  \begin{isabelle}
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    72
  \isacommand{lemma}~@{text "test:"}\isanewline
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    73
  \isacommand{shows}~@{text [quotes] "True"}\isanewline
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    74
  \isacommand{ML\_prf}~@{text "\<verbopen>"}~@{ML "writeln \"Trivial!\""}~@{text "\<verbclose>"}\isanewline
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    75
  \isacommand{oops}
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    76
  \end{isabelle}
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    77
  \end{quote}
253
3cfd9a8a6de1 added comments about ML_prf and ML_val
Christian Urban <urbanc@in.tum.de>
parents: 252
diff changeset
    78
502
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
    79
  However, both commands will only play minor roles in this tutorial (we most of
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
    80
  the time make sure that the ML-code is defined outside proofs).
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
    81
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
    82
  
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
    83
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
    84
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
    85
  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
    86
  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
    87
  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
    88
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
    89
  \begin{quote}
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    90
  \begin{tabular}{@ {}l}
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
    91
  \isacommand{theory} First\_Steps\\
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    92
  \isacommand{imports} Main\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    93
  \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
    94
  \isacommand{begin}\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    95
  \ldots\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    96
  \isacommand{use}~@{text "\"file_to_be_included.ML\""}\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    97
  \ldots
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    98
  \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
    99
  \end{quote}
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   100
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   101
  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
   102
  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
   103
  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
   104
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
   105
  \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
   106
  \begin{tabular}{@ {}l}
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   107
  \isacommand{theory} First\_Steps\\
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   108
  \isacommand{imports} Main\\
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   109
  \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
   110
  \isacommand{begin}\\
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   111
  \ldots
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   112
  \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
   113
  \end{quote}
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   114
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   115
  Note that no parentheses are given in this case. Note also that the included
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   116
  ML-file should not contain any \isacommand{use} itself. Otherwise Isabelle
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   117
  is unable to record all file dependencies, which is a nuisance if you have
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   118
  to track down errors.
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   119
*}
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   120
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   121
section {* Printing and Debugging\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
   122
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   123
text {*
374
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   124
  During development you might find it necessary to inspect data in your
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   125
  code. This can be done in a ``quick-and-dirty'' fashion using the function
369
74ba778b09c9 tuned index
Christian Urban <urbanc@in.tum.de>
parents: 361
diff changeset
   126
  @{ML_ind writeln in Output}. For example
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   127
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   128
  @{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
   129
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   130
  will print out @{text [quotes] "any string"} inside the response buffer.  
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   131
  This function expects a string as argument. If you develop under
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   132
  PolyML, then there is a convenient, though again ``quick-and-dirty'', method
421
620a24bf954a added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
   133
  for converting values into strings, namely the antiquotation 
620a24bf954a added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
   134
  @{text "@{make_string}"}:
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   135
421
620a24bf954a added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
   136
  @{ML_response_fake [display,gray] "writeln (@{make_string} 1)" "\"1\""} 
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   137
421
620a24bf954a added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
   138
  However, @{text "@{makes_tring}"} only works if the type of what
466
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   139
  is converted is monomorphic and not a function. 
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   140
466
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   141
  You can print out error messages with the function @{ML_ind error in Library}; 
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   142
  for example:
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   143
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
   144
  @{ML_response_fake [display,gray] 
483
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   145
  "if 0 = 1 then true else (error \"foo\")" 
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   146
"Exception- ERROR \"foo\" raised
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   147
At command \"ML\"."}
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   148
307
f4fa6540e280 suggestions by Peter Homeier
Christian Urban <urbanc@in.tum.de>
parents: 306
diff changeset
   149
  This function raises the exception @{text ERROR}, which will then 
420
0bcd598d2587 added something about ERROR and Fail
Christian Urban <urbanc@in.tum.de>
parents: 419
diff changeset
   150
  be displayed by the infrastructure. Note that this exception is meant 
466
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   151
  for ``user-level'' error messages seen by the ``end-user''. 
420
0bcd598d2587 added something about ERROR and Fail
Christian Urban <urbanc@in.tum.de>
parents: 419
diff changeset
   152
  For messages where you want to indicate a genuine program error, then
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   153
  use the exception @{text Fail}. 
304
14173c0e8688 polished comment for error function
Christian Urban <urbanc@in.tum.de>
parents: 301
diff changeset
   154
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   155
  Most often you want to inspect data of Isabelle's basic data structures,
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   156
  namely @{ML_type term}, @{ML_type typ}, @{ML_type cterm}, @{ML_type ctyp}
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   157
  and @{ML_type thm}. Isabelle contains elaborate pretty-printing functions,
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   158
  which we will explain in more detail in Section \ref{sec:pretty}. For now
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   159
  we just use the functions @{ML_ind writeln in Pretty}  from the structure
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   160
  @{ML_struct Pretty} and @{ML_ind pretty_term in Syntax} from the structure
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   161
  @{ML_struct Syntax}. For more convenience, we bind them to the toplevel.
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   162
*}
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   163
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   164
ML{*val pretty_term = Syntax.pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   165
val pwriteln = Pretty.writeln*}
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   166
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   167
text {*
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   168
  They can now be used as follows
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   169
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   170
  @{ML_response_fake [display,gray]
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   171
  "pwriteln (pretty_term @{context} @{term \"1::nat\"})"
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   172
  "\"1\""}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   173
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   174
  If there is more than one term to be printed, you can use the 
446
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   175
  function @{ML_ind commas in Pretty} and @{ML_ind block in Pretty} 
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   176
  to separate them.
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   177
*} 
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   178
467
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
   179
ML{*fun pretty_terms ctxt trms =  
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
   180
  Pretty.block (Pretty.commas (map (pretty_term ctxt) trms))*}
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   181
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   182
text {*
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   183
  You can also print out terms together with their typing information.
451
fc074e669f9f disabled foobar_prove; updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 450
diff changeset
   184
  For this you need to set the configuration value 
fc074e669f9f disabled foobar_prove; updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 450
diff changeset
   185
  @{ML_ind show_types in Syntax} to @{ML true}.
370
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   186
*}
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   187
451
fc074e669f9f disabled foobar_prove; updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 450
diff changeset
   188
ML{*val show_types_ctxt = Config.put show_types true @{context}*}
370
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   189
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   190
text {*
451
fc074e669f9f disabled foobar_prove; updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 450
diff changeset
   191
  Now by using this context @{ML pretty_term} prints out
370
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   192
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   193
  @{ML_response_fake [display, gray]
451
fc074e669f9f disabled foobar_prove; updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 450
diff changeset
   194
  "pwriteln (pretty_term show_types_ctxt @{term \"(1::nat, x)\"})"
370
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   195
  "(1::nat, x::'a)"}
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   196
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   197
  where @{text 1} and @{text x} are displayed with their inferred type.
370
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   198
  Even more type information can be printed by setting 
374
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   199
  the reference @{ML_ind show_all_types in Syntax} to @{ML true}. 
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   200
  In this case we obtain
370
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   201
*}
451
fc074e669f9f disabled foobar_prove; updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 450
diff changeset
   202
370
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   203
text {*
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   204
  @{ML_response_fake [display, gray]
466
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   205
  "let 
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   206
  val show_all_types_ctxt = Config.put show_all_types true @{context}
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   207
in
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   208
  pwriteln (pretty_term show_all_types_ctxt @{term \"(1::nat, x)\"})
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   209
end"
370
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   210
  "(Pair::nat \<Rightarrow> 'a \<Rightarrow> nat \<times> 'a) (1::nat) (x::'a)"}
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   211
466
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   212
  where now even @{term Pair} is written with its type (@{term Pair} is the
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   213
  term-constructor for products). Other configuration values that influence
502
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   214
  printing of terms include 
451
fc074e669f9f disabled foobar_prove; updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 450
diff changeset
   215
502
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   216
  \begin{itemize}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   217
  \item @{ML_ind show_brackets in Syntax} 
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   218
  \item @{ML_ind show_sorts in Syntax}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   219
  \item @{ML_ind eta_contract in Syntax}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   220
  \end{itemize}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   221
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   222
  A @{ML_type cterm} can be printed with the following function.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   223
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   224
467
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
   225
ML{*fun pretty_cterm ctxt ctrm =  
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
   226
  pretty_term ctxt (term_of ctrm)*}
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 {*
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   229
  Here the function @{ML_ind term_of in Thm} extracts the @{ML_type
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   230
  term} from a @{ML_type cterm}.  More than one @{ML_type cterm}s can be
446
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   231
  printed again with @{ML commas in Pretty}.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   232
*} 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   233
467
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
   234
ML{*fun pretty_cterms ctxt ctrms =  
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
   235
  Pretty.block (Pretty.commas (map (pretty_cterm ctxt) ctrms))*}
126
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
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   238
  The easiest way to get the string of a theorem is to transform it
369
74ba778b09c9 tuned index
Christian Urban <urbanc@in.tum.de>
parents: 361
diff changeset
   239
  into a @{ML_type term} using the function @{ML_ind prop_of in Thm}. 
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   240
*}
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   241
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   242
ML{*fun pretty_thm ctxt thm =
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   243
  pretty_term ctxt (prop_of thm)*}
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   244
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   245
text {*
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   246
  Theorems include schematic variables, such as @{text "?P"}, 
343
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   247
  @{text "?Q"} and so on. They are needed in Isabelle in order to able to
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   248
  instantiate theorems when they are applied. For example the theorem 
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   249
  @{thm [source] conjI} shown below can be used for any (typable) 
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
   250
  instantiation of @{text "?P"} and @{text "?Q"}. 
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   251
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   252
  @{ML_response_fake [display, gray]
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   253
  "pwriteln (pretty_thm @{context} @{thm conjI})"
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   254
  "\<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
   255
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   256
  However, in order to improve the readability when printing theorems, we
466
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   257
  can switch off the question marks as follows:
126
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
466
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   260
ML{*fun pretty_thm_no_vars ctxt thm =
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   261
let
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   262
  val ctxt' = Config.put show_question_marks false ctxt
158
d7944bdf7b3f removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   263
in
466
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   264
  pretty_term ctxt' (prop_of thm)
26d2f91608ed a little polishing
Christian Urban <urbanc@in.tum.de>
parents: 462
diff changeset
   265
end*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   266
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   267
text {* 
374
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   268
  With this function, 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
   269
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   270
  @{ML_response_fake [display, gray]
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   271
  "pwriteln (pretty_thm_no_vars @{context} @{thm conjI})"
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   272
  "\<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
   273
  
467
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
   274
  Again the functions @{ML commas} and @{ML block in Pretty} help 
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
   275
  with printing more than one theorem. 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   276
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   277
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   278
ML{*fun pretty_thms ctxt thms =  
446
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   279
  Pretty.block (Pretty.commas (map (pretty_thm ctxt) thms))
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   280
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   281
fun pretty_thms_no_vars ctxt thms =  
446
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   282
  Pretty.block (Pretty.commas (map (pretty_thm_no_vars ctxt) thms))*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   283
305
2ac9dc1a95b4 added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents: 304
diff changeset
   284
text {*
476
0fb910f62bf9 minor things
Christian Urban <urbanc@in.tum.de>
parents: 475
diff changeset
   285
  Printing functions for @{ML_type typ} are
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   286
*}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   287
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   288
ML{*fun pretty_typ ctxt ty = Syntax.pretty_typ ctxt ty
446
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   289
fun pretty_typs ctxt tys = 
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   290
  Pretty.block (Pretty.commas (map (pretty_typ ctxt) tys))*}
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   291
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   292
text {*
476
0fb910f62bf9 minor things
Christian Urban <urbanc@in.tum.de>
parents: 475
diff changeset
   293
  respectively @{ML_type ctyp}
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   294
*}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   295
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   296
ML{*fun pretty_ctyp ctxt cty = pretty_typ ctxt (typ_of cty)
446
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   297
fun pretty_ctyps ctxt ctys = 
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   298
  Pretty.block (Pretty.commas (map (pretty_ctyp ctxt) ctys))*}
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   299
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   300
text {*
370
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   301
  \begin{readmore}
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   302
  The simple conversion functions from Isabelle's main datatypes to 
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   303
  @{ML_type string}s are implemented in @{ML_file "Pure/Syntax/syntax.ML"}. 
467
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
   304
  The configuration values that change the printing information are declared in 
370
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   305
  @{ML_file "Pure/Syntax/printer.ML"}.
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   306
  \end{readmore}
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   307
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   308
  Note that for printing out several ``parcels'' of information that belong
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   309
  together, like a warning message consisting of a term and its type, you
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   310
  should try to print these parcels together in a single string. Therefore do
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   311
  \emph{not} print out information as
306
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   312
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   313
@{ML_response_fake [display,gray]
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   314
"pwriteln (Pretty.str \"First half,\"); 
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   315
pwriteln (Pretty.str \"and second half.\")"
306
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   316
"First half,
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   317
and second half."}
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   318
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   319
  but as a single string with appropriate formatting. For example
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   320
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   321
@{ML_response_fake [display,gray]
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   322
"pwriteln (Pretty.str (\"First half,\" ^ \"\\n\" ^ \"and second half.\"))"
306
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   323
"First half,
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   324
and second half."}
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   325
  
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   326
  To ease this kind of string manipulations, there are a number
374
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   327
  of library functions in Isabelle. For example,  the function 
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   328
  @{ML_ind cat_lines in Library} concatenates a list of strings 
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   329
  and inserts newlines in between each element. 
305
2ac9dc1a95b4 added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents: 304
diff changeset
   330
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   331
  @{ML_response_fake [display, gray]
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   332
  "pwriteln (Pretty.str (cat_lines [\"foo\", \"bar\"]))"
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   333
  "foo
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   334
bar"}
306
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   335
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   336
  Section \ref{sec:pretty} will explain the infrastructure that Isabelle 
374
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   337
  provides for more elaborate pretty printing. 
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   338
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   339
  \begin{readmore}
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   340
  Most of the basic string functions of Isabelle are defined in 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   341
  @{ML_file "Pure/library.ML"}.
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   342
  \end{readmore}
305
2ac9dc1a95b4 added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents: 304
diff changeset
   343
*}
2ac9dc1a95b4 added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents: 304
diff changeset
   344
2ac9dc1a95b4 added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents: 304
diff changeset
   345
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   346
section {* Combinators\label{sec:combinators} *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   347
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   348
text {*
413
95461cf6fd07 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   349
  For beginners perhaps the most puzzling parts in the existing code of
95461cf6fd07 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   350
  Isabelle are the combinators. At first they seem to greatly obstruct the
95461cf6fd07 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   351
  comprehension of code, but after getting familiar with them and handled with
95461cf6fd07 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   352
  care, they actually ease the understanding and also the programming.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   353
373
28a49fe024c9 added structure index
Christian Urban <urbanc@in.tum.de>
parents: 372
diff changeset
   354
  The simplest combinator is @{ML_ind I in Library}, which is just the 
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   355
  identity function defined as
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   356
*}
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
ML{*fun I x = x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   359
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   360
text {* 
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   361
  Another simple combinator is @{ML_ind K in Library}, defined as 
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   362
*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   363
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   364
ML{*fun K x = fn _ => x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   365
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   366
text {*
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   367
  @{ML K} ``wraps'' a function around @{text "x"} that ignores its argument. As a 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   368
  result, @{ML K} defines a constant function always returning @{text x}.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   369
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   370
  The next combinator is reverse application, @{ML_ind  "|>" in Basics}, defined as: 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   371
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   372
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   373
ML{*fun x |> f = f x*}
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 {* While just syntactic sugar for the usual function application,
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   376
  the purpose of this combinator is to implement functions in a  
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   377
  ``waterfall fashion''. Consider for example the function *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   378
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   379
ML %linenosgray{*fun inc_by_five x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   380
  x |> (fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   381
    |> (fn x => (x, x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   382
    |> fst
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   383
    |> (fn x => x + 4)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   384
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   385
text {*
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   386
  which increments its argument @{text x} by 5. It does this by first
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   387
  incrementing the argument by 1 (Line 2); then storing the result in a pair
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   388
  (Line 3); taking the first component of the pair (Line 4) and finally
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   389
  incrementing the first component by 4 (Line 5). This kind of cascading
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   390
  manipulations of values is quite common when dealing with theories. The
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   391
  reverse application allows you to read what happens in a top-down
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   392
  manner. This kind of coding should be familiar, if you have been exposed to
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   393
  Haskell's {\it do}-notation. Writing the function @{ML inc_by_five} using
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   394
  the reverse application is much clearer than writing
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   395
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   396
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   397
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
   398
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   399
text {* or *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   400
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   401
ML{*fun inc_by_five x = 
324
4172c0743cf2 updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   402
  ((fn x => x + 4) o fst o (fn x => (x, x)) o (fn x => x + 1)) x*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   403
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   404
text {* and typographically more economical than *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   405
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   406
ML{*fun inc_by_five x =
257
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   407
let val y1 = x + 1
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   408
    val y2 = (y1, y1)
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   409
    val y3 = fst y2
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   410
    val y4 = y3 + 4
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   411
in y4 end*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   412
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   413
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   414
  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
   415
  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
   416
  mention the nightmares the maintenance of this code causes!
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   417
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   418
  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
   419
  the waterfall fashion might be the following code:
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   420
*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   421
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   422
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
   423
    f |> fastype_of
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   424
      |> binder_types 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   425
      |> map (pair "z")
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   426
      |> Variable.variant_frees ctxt [f]
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   427
      |> map Free  
257
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   428
      |> curry list_comb f *}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   429
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   430
text {*
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   431
  This function takes a term and a context as argument. If the term is of function
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   432
  type, then @{ML "apply_fresh_args"} returns the term with distinct variables 
343
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
   433
  applied to it. For example below three variables are applied to the term 
298
8057d65607eb some general polishing
Christian Urban <urbanc@in.tum.de>
parents: 293
diff changeset
   434
  @{term [show_types] "P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool"}:
183
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   435
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   436
  @{ML_response_fake [display,gray]
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   437
"let
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   438
  val trm = @{term \"P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool\"}
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   439
  val ctxt = @{context}
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   440
in 
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   441
  apply_fresh_args trm ctxt
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   442
   |> pretty_term ctxt
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   443
   |> pwriteln
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   444
end" 
183
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   445
  "P z za zb"}
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   446
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   447
  You can read off this behaviour from how @{ML apply_fresh_args} is coded: in
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   448
  Line 2, the function @{ML_ind fastype_of in Term} calculates the type of the
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   449
  term; @{ML_ind binder_types in Term} in the next line produces the list of
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   450
  argument types (in the case above the list @{text "[nat, int, unit]"}); Line
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   451
  4 pairs up each type with the string @{text "z"}; the function @{ML_ind
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   452
  variant_frees in Variable} generates for each @{text "z"} a unique name
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   453
  avoiding the given @{text f}; the list of name-type pairs is turned into a
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   454
  list of variable terms in Line 6, which in the last line is applied by the
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   455
  function @{ML_ind list_comb in Term} to the original term. In this last step we have
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   456
  to use the function @{ML_ind curry in Library}, because @{ML list_comb}
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   457
  expects the function and the variables list as a pair. 
374
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   458
  
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   459
  Functions like @{ML apply_fresh_args} are often needed when constructing
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   460
  terms involving fresh variables. For this the infrastructure helps
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   461
  tremendously to avoid any name clashes. Consider for example:
252
f380b13b25a7 added an example
Christian Urban <urbanc@in.tum.de>
parents: 251
diff changeset
   462
f380b13b25a7 added an example
Christian Urban <urbanc@in.tum.de>
parents: 251
diff changeset
   463
   @{ML_response_fake [display,gray]
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   464
"let
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   465
  val trm = @{term \"za::'a \<Rightarrow> 'b \<Rightarrow> 'c\"}
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   466
  val ctxt = @{context}
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   467
in
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   468
  apply_fresh_args trm ctxt 
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   469
   |> pretty_term ctxt
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   470
   |> pwriteln
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   471
end"
252
f380b13b25a7 added an example
Christian Urban <urbanc@in.tum.de>
parents: 251
diff changeset
   472
  "za z zb"}
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   473
  
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   474
  where the @{text "za"} is correctly avoided.
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   475
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   476
  The combinator @{ML_ind "#>" in Basics} is the reverse function composition. 
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   477
  It can be used to define the following function
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   478
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   479
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   480
ML{*val inc_by_six = 
374
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   481
  (fn x => x + 1) #> 
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   482
  (fn x => x + 2) #> 
Christian Urban <urbanc@in.tum.de>
parents: 373
diff changeset
   483
  (fn x => x + 3)*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   484
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   485
text {* 
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   486
  which is the function composed of first the increment-by-one function and
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   487
  then increment-by-two, followed by increment-by-three. Again, the reverse
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   488
  function composition allows you to read the code top-down. This combinator
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   489
  is often used for setup functions inside the
478
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   490
  \isacommand{setup}- or \isacommand{local\_setup}-command. These functions 
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   491
  have to be of type @{ML_type "theory -> theory"}, respectively 
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   492
  @{ML_type "local_theory -> local_theory"}. More than one such setup function 
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   493
  can be composed with @{ML "#>"}. Consider for example the following code, 
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   494
  where we store the theorems @{thm [source] conjI}, @{thm [source] conjunct1} 
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   495
  and @{thm [source] conjunct2} under alternative names.
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   496
*}  
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   497
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   498
local_setup %graylinenos {* 
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   499
let  
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   500
  fun my_note name thm = Local_Theory.note ((name, []), [thm]) #> snd
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   501
in
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   502
  my_note @{binding "foo_conjI"} @{thm conjI} #>
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   503
  my_note @{binding "bar_conjunct1"} @{thm conjunct1} #>
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   504
  my_note @{binding "bar_conjunct2"} @{thm conjunct2}
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   505
end *}
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   506
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   507
text {*
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   508
  The function @{ML_text "my_note"} in line 3 is just a wrapper for the function 
482
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   509
  @{ML_ind note in Local_Theory} in the structure @{ML_struct Local_Theory}; 
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   510
  its purpose is to store a theorem under a name. 
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   511
  In lines 5 to 6 we call this function to give alternative names for the three
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   512
  theorems. The point of @{ML "#>"} is that you can sequence such function calls. 
478
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   513
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   514
  The remaining combinators we describe in this section add convenience for
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   515
  the ``waterfall method'' of writing functions. The combinator @{ML_ind tap
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   516
  in Basics} allows you to get hold of an intermediate result (to do some
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   517
  side-calculations or print out an intermediate result, for instance). The
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   518
  function
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   519
 *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   520
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   521
ML %linenosgray{*fun inc_by_three x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   522
     x |> (fn x => x + 1)
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   523
       |> tap (fn x => pwriteln (Pretty.str (@{make_string} x)))
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   524
       |> (fn x => x + 2)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   525
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   526
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   527
  increments the argument first by @{text "1"} and then by @{text "2"}. In the
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   528
  middle (Line 3), however, it uses @{ML tap} for printing the ``plus-one''
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   529
  intermediate result. The function @{ML tap} can only be used for
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   530
  side-calculations, because any value that is computed cannot be merged back
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   531
  into the ``main waterfall''. To do this, you can use the next combinator.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   532
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   533
  The combinator @{ML_ind "`" in Basics} (a backtick) is similar to @{ML tap},
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   534
  but applies a function to the value and returns the result together with the
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   535
  value (as a pair). It is defined as
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   536
*}
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   537
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   538
ML{*fun `f = fn x => (f x, x)*}
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   539
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   540
text {*
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   541
  An example for this combinator is the function
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   542
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   543
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   544
ML{*fun inc_as_pair x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   545
     x |> `(fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   546
       |> (fn (x, y) => (x, y + 1))*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   547
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   548
text {*
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   549
  which takes @{text x} as argument, and then increments @{text x}, but also keeps
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   550
  @{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
   551
  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
   552
  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
   553
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   554
  The combinators @{ML_ind "|>>" in Basics} and @{ML_ind "||>" in Basics} are
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   555
  defined for functions manipulating pairs. The first applies the function to
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   556
  the first component of the pair, defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   557
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   558
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   559
ML{*fun (x, y) |>> f = (f x, y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   560
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   561
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   562
  and the second combinator to the second component, defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   563
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   564
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   565
ML{*fun (x, y) ||> f = (x, f y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   566
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   567
text {*
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   568
  These two functions can, for example, be used to avoid explicit @{text "lets"} for
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   569
  intermediate values in functions that return pairs. As an example, suppose you
308
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   570
  want to separate a list of integers into two lists according to a
417
5f00958e3c7b typos fixed by Michael Norrish
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
   571
  threshold. If the threshold is @{ML "5"}, the list @{ML "[1,6,2,5,3,4]"}
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   572
  should be separated as @{ML "([1,2,3,4], [6,5])"}.  Such a function can be
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   573
  implemented as
308
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   574
*}
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   575
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   576
ML{*fun separate i [] = ([], [])
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   577
  | separate i (x::xs) =
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   578
      let 
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   579
        val (los, grs) = separate i xs
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   580
      in
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   581
        if i <= x then (los, x::grs) else (x::los, grs)
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   582
      end*}
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   583
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   584
text {*
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   585
  where the return value of the recursive call is bound explicitly to
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   586
  the pair @{ML "(los, grs)" for los grs}. However, this function
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   587
  can be implemented more concisely as
308
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   588
*}
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   589
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   590
ML{*fun separate i [] = ([], [])
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   591
  | separate i (x::xs) =
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   592
      if i <= x 
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   593
      then separate i xs ||> cons x
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   594
      else separate i xs |>> cons x*}
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   595
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   596
text {*
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   597
  avoiding the explicit @{text "let"}. While in this example the gain in
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   598
  conciseness is only small, in more complicated situations the benefit of
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   599
  avoiding @{text "lets"} can be substantial.
308
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   600
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   601
  With the combinator @{ML_ind "|->" in Basics} you can re-combine the 
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   602
  elements from a pair. This combinator is defined as
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   603
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   604
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   605
ML{*fun (x, y) |-> f = f x y*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   606
215
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   607
text {* 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   608
  and can be used to write the following roundabout version 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   609
  of the @{text double} function: 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   610
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   611
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   612
ML{*fun double x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   613
      x |>  (fn x => (x, x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   614
        |-> (fn x => fn y => x + y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   615
215
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   616
text {* 
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   617
  The combinator @{ML_ind ||>> in Basics} plays a central rôle whenever your
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   618
  task is to update a theory and the update also produces a side-result (for
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   619
  example a theorem). Functions for such tasks return a pair whose second
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   620
  component is the theory and the fist component is the side-result. Using
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   621
  @{ML ||>>}, you can do conveniently the update and also
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   622
  accumulate the side-results. Consider the following simple function.
215
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   623
*}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   624
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   625
ML %linenosgray{*fun acc_incs x =
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   626
    x |> (fn x => ("", x)) 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   627
      ||>> (fn x => (x, x + 1))
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   628
      ||>> (fn x => (x, x + 1))
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   629
      ||>> (fn x => (x, x + 1))*}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   630
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   631
text {*
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   632
  The purpose of Line 2 is to just pair up the argument with a dummy value (since
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   633
  @{ML ||>>} operates on pairs). Each of the next three lines just increment 
280
a63ca3a9b44a spell checked
griff
parents: 279
diff changeset
   634
  the value by one, but also nest the intermediate results to the left. For example 
215
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   635
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   636
  @{ML_response [display,gray]
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   637
  "acc_incs 1"
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   638
  "((((\"\", 1), 2), 3), 4)"}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   639
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   640
  You can continue this chain with:
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   641
  
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   642
  @{ML_response [display,gray]
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   643
  "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
   644
  "(((((\"\", 1), 2), 3), 4), 6)"}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   645
483
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   646
  An example where this combinator is useful is as follows
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   647
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   648
  @{ML_response_fake [display, gray]
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   649
  "let
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   650
  val ((names1, names2), _) =
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   651
    @{context}
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   652
    |> Variable.variant_fixes (replicate 4 \"x\")
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   653
    ||>> Variable.variant_fixes (replicate 5 \"x\")
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   654
in
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   655
  (names1, names2)
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   656
end"
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   657
  "([\"x\", \"xa\", \"xb\", \"xc\"], [\"xd\", \"xe\", \"xf\", \"xg\", \"xh\"])"}
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   658
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   659
  Its purpose is to create nine variants of the string @{ML "\"x\""} so
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   660
  that no variant will clash with another. Suppose for some reason we want
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   661
  to bind four variants to the lists @{ML_text "name1"} and the
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   662
  rest to @{ML_text "name2"}. In order to obtain non-clashing
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   663
  variants we have to thread the context through the function calls
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   664
  (the context records which variants have been previously created).
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   665
  For the first call we can use @{ML "|>"}, but in the
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   666
  second and any further call to @{ML_ind variant_fixes in Variable} we 
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   667
  have to use @{ML "||>>"} in order to account for the result(s) 
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   668
  obtained by previous calls.
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   669
  
479
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   670
  A more realistic example for this combinator is the following code
478
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   671
*}
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   672
479
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   673
ML{*val (((one_def, two_def), three_def), ctxt') = 
478
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   674
  @{context}
496
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   675
  |> Local_Defs.add_def ((@{binding "One"}, NoSyn), @{term "1::nat"}) 
478
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   676
  ||>> Local_Defs.add_def ((@{binding "Two"}, NoSyn), @{term "2::nat"})
479
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   677
  ||>> Local_Defs.add_def ((@{binding "Three"}, NoSyn), @{term "3::nat"})*}
478
dfbd535cd1fd some tests
Christian Urban <urbanc@in.tum.de>
parents: 477
diff changeset
   678
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   679
text {*
496
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   680
  where we make three definitions, namely @{term "One \<equiv> 1::nat"}, @{term "Two \<equiv> 2::nat"}
Christian Urban <urbanc@in.tum.de>
parents: 489
diff changeset
   681
  and @{term "Three \<equiv> 3::nat"}. The point of this code is that we augment the initial
479
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   682
  context with the definitions. The result we are interested in is the
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   683
  augmented context, that is @{ML_text "ctxt'"}, but also the side-results containing 
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   684
  information about the definitions---the function @{ML_ind add_def in Local_Defs} returns
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   685
  both as pairs. We can use this information for example to print out the definiens and 
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   686
  the theorem corresponding to the definitions. For example for the first definition:
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   687
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   688
  @{ML_response_fake [display, gray]
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   689
  "let 
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   690
  val (one_trm, one_thm) = one_def
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   691
in
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   692
  pwriteln (pretty_term ctxt' one_trm);
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   693
  pwriteln (pretty_thm ctxt' one_thm)
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   694
end"
502
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   695
  "One
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   696
One \<equiv> 1"}
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   697
  Recall that @{ML "|>"} is the reverse function application. Recall also that
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   698
  the related reverse function composition is @{ML "#>"}. In fact all the
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   699
  combinators @{ML "|->"}, @{ML "|>>"} , @{ML "||>"} and @{ML "||>>"}
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   700
  described above have related combinators for function composition, namely
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   701
  @{ML_ind "#->" in Basics}, @{ML_ind "#>>" in Basics}, @{ML_ind "##>" in
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   702
  Basics} and @{ML_ind "##>>" in Basics}. Using @{ML "#->"}, for example, the
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   703
  function @{text double} can also be written as:
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   704
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   705
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   706
ML{*val double =
502
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   707
   (fn x => (x, x)) #-> 
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   708
   (fn x => fn y => x + y)*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   709
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   710
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   711
text {* 
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   712
  When using combinators for writing functions in waterfall fashion, it is
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   713
  sometimes necessary to do some ``plumbing'' in order to fit functions
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   714
  together. We have already seen such plumbing in the function @{ML
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   715
  apply_fresh_args}, where @{ML curry} is needed for making the function @{ML
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   716
  list_comb}, which works over pairs, to fit with the combinator @{ML "|>"}. Such 
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   717
  plumbing is also needed in situations where a function operates over lists, 
325
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
   718
  but one calculates only with a single element. An example is the function 
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   719
  @{ML_ind check_terms in Syntax}, whose purpose is to simultaneously type-check 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   720
  a list of terms. Consider the code:
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   721
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   722
  @{ML_response_fake [display, gray]
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   723
  "let
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   724
  val ctxt = @{context}
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   725
in
324
4172c0743cf2 updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   726
  map (Syntax.parse_term ctxt) [\"m + n\", \"m * n\", \"m - (n::nat)\"] 
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   727
  |> Syntax.check_terms ctxt
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   728
  |> pretty_terms ctxt
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   729
  |> pwriteln
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   730
end"
324
4172c0743cf2 updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   731
  "m + n, m * n, m - n"}
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   732
*}
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   733
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   734
text {*
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   735
  In this example we obtain three terms (using the function 
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   736
  @{ML_ind parse_term in Syntax}) whose variables @{text m} and @{text n} 
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   737
  are of type @{typ "nat"}. If you have only a single term, then @{ML
324
4172c0743cf2 updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   738
  check_terms in Syntax} needs plumbing. This can be done with the function
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   739
  @{ML_ind singleton in Library}.\footnote{There is already a function @{ML check_term in
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   740
  Syntax} in the file @{ML_file "Pure/Syntax/syntax.ML"} that is implemented 
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   741
  in terms of @{ML singleton} and @{ML check_terms in Syntax}.} For example
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   742
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   743
  @{ML_response_fake [display, gray, linenos]
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   744
  "let 
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   745
  val ctxt = @{context}
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   746
in
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   747
  Syntax.parse_term ctxt \"m - (n::nat)\" 
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   748
  |> singleton (Syntax.check_terms ctxt)
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   749
  |> pretty_term ctxt
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   750
  |> pwriteln
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   751
end"
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   752
  "m - n"}
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   753
   
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   754
  where in Line 5, the function operating over lists fits with the
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   755
  single term generated in Line 4.
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   756
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   757
  \begin{readmore}
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   758
  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
   759
  "Pure/library.ML"}
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   760
  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
   761
  contains further information about combinators.
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   762
  \end{readmore}
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   763
421
620a24bf954a added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
   764
  \begin{exercise}
620a24bf954a added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
   765
  Find out what the combinator @{ML "K I"} does.
620a24bf954a added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
   766
  \end{exercise}
15
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   767
*}
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   768
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   769
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   770
section {* ML-Antiquotations\label{sec:antiquote} *}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   771
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   772
text {*
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   773
  Recall from Section \ref{sec:include} that code in Isabelle is always
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   774
  embedded in a theory.  The main advantage of this is that the code can
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   775
  contain references to entities defined on the logical level of Isabelle. By
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   776
  this we mean references to definitions, theorems, terms and so on. These
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   777
  reference are realised in Isabelle with ML-antiquotations, often just called
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   778
  antiquotations.\footnote{Note that there are two kinds of antiquotations in
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   779
  Isabelle, which have very different purposes and infrastructures. The first
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   780
  kind, described in this section, are \emph{\index*{ML-antiquotation}}. They
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   781
  are used to refer to entities (like terms, types etc) from Isabelle's logic
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   782
  layer inside ML-code. The other kind of antiquotations are
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   783
  \emph{document}\index{document antiquotation} antiquotations. They are used
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   784
  only in the text parts of Isabelle and their purpose is to print logical
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   785
  entities inside \LaTeX-documents. Document antiquotations are part of the
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   786
  user level and therefore we are not interested in them in this Tutorial,
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   787
  except in Appendix \ref{rec:docantiquotations} where we show how to
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   788
  implement your own document antiquotations.}  Syntactically antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   789
  are indicated by the @{ML_text @}-sign followed by text wrapped in @{text
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   790
  "{\<dots>}"}.  For example, one can print out the name of the current theory with
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   791
  the code
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   792
  
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   793
  @{ML_response [display,gray] "Context.theory_name @{theory}" "\"First_Steps\""}
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   794
 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   795
  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
   796
  current theory (remember that we assumed we are inside the theory 
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   797
  @{text First_Steps}). The name of this theory can be extracted using
344
83d5bca38bec added structures in the index
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   798
  the function @{ML_ind theory_name in Context}. 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   799
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   800
  Note, however, that antiquotations are statically linked, that is their value is
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   801
  determined at ``compile-time'', not at ``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
   802
*}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   803
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   804
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
   805
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
   806
text {*
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   807
  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
   808
  different theory. Instead, the code above defines the constant function 
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   809
  that always returns the string @{text [quotes] "First_Steps"}, 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
   810
  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
   811
  \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
   812
  some data structure and return it. Instead, it is literally
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   813
  replaced with the value representing the theory.
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   814
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   815
  Another important antiquotation is @{text "@{context}"}. (What the
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   816
  difference between a theory and a context is will be described in Chapter
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   817
  \ref{chp:advanced}.) A context is for example needed in order to use the
475
25371f74c768 updated to post-2011-1 Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   818
  function @{ML print_abbrevs in Proof_Context} that list of all currently
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   819
  defined abbreviations.
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   820
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   821
  @{ML_response_fake [display, gray]
475
25371f74c768 updated to post-2011-1 Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   822
  "Proof_Context.print_abbrevs @{context}"
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   823
"Code_Evaluation.valtermify \<equiv> \<lambda>x. (x, \<lambda>u. Code_Evaluation.termify x)
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   824
INTER \<equiv> INFI
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   825
Inter \<equiv> Inf
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   826
\<dots>"}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   827
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   828
  You can also 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
   829
  @{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
   830
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   831
  @{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
   832
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   833
  and @{text "@{thms \<dots>}"} for more than one
132
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   834
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   835
@{ML_response_fake [display,gray] 
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   836
  "@{thms conj_ac}" 
132
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   837
"(?P \<and> ?Q) = (?Q \<and> ?P)
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   838
(?P \<and> ?Q \<and> ?R) = (?Q \<and> ?P \<and> ?R)
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   839
((?P \<and> ?Q) \<and> ?R) = (?P \<and> ?Q \<and> ?R)"}  
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   840
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   841
  The thm-antiquotations can also be used for manipulating theorems. For 
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   842
  example, if you need the version of the theorem @{thm [source] refl} that 
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   843
  has a meta-equality instead of an equality, you can write
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   844
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   845
@{ML_response_fake [display,gray] 
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   846
  "@{thm refl[THEN eq_reflection]}"
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   847
  "?x \<equiv> ?x"}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   848
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   849
  The point of these antiquotations is that referring to theorems in this way
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   850
  makes your code independent from what theorems the user might have stored
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   851
  under this name (this becomes especially important when you deal with
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   852
  theorem lists; see Section \ref{sec:storing}).
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   853
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   854
  It is also possible to prove lemmas with the antiquotation @{text "@{lemma \<dots> by \<dots>}"}
400
Christian Urban <urbanc@in.tum.de>
parents: 394
diff changeset
   855
  whose first argument is a statement (possibly many of them separated by @{text "and"})
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   856
  and the second is a proof. For example
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   857
*}
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   858
412
73f716b9201a polised
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
   859
ML{*val foo_thm = @{lemma "True" and "False \<Longrightarrow> P" by simp_all} *}
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   860
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   861
text {*
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   862
  The result can be printed out as follows.
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   863
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   864
  @{ML_response_fake [gray,display]
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   865
"foo_thm |> pretty_thms_no_vars @{context}
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 423
diff changeset
   866
        |> pwriteln"
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   867
  "True, False \<Longrightarrow> P"}
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   868
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   869
  You can also refer to the current simpset via an antiquotation. To illustrate 
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   870
  this we implement the function that extracts the theorem names stored in a 
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   871
  simpset.
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   872
*}
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   873
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   874
ML{*fun get_thm_names_from_ss simpset =
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   875
let
458
242e81f4d461 updated to post-2011 Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 455
diff changeset
   876
  val {simps,...} = Raw_Simplifier.dest_ss simpset
70
bbb2d5f1d58d deleted the fixme about simpsets
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   877
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
   878
  map #1 simps
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   879
end*}
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
   880
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   881
text {*
458
242e81f4d461 updated to post-2011 Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 455
diff changeset
   882
  The function @{ML_ind dest_ss in Raw_Simplifier} returns a record containing all
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   883
  information stored in the simpset, but here 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
   884
  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
   885
  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
   886
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   887
  @{ML_response_fake [display,gray] 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   888
  "get_thm_names_from_ss @{simpset}" 
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   889
  "[\"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
   890
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   891
  Again, this way of referencing simpsets makes you independent from additions
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   892
  of lemmas to the simpset by the user, which can potentially cause loops in your
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   893
  code.
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   894
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   895
  It is also possible to define your own antiquotations. But you should
315
de49d5780f57 simplified a bit the index generation
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   896
  exercise care when introducing new ones, as they can also make your code
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   897
  also difficult to read. In the next chapter we describe how to construct
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   898
  terms with the (build in) antiquotation @{text "@{term \<dots>}"}.  A restriction
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   899
  of this antiquotation is that it does not allow you to use schematic
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   900
  variables in terms. If you want to have an antiquotation that does not have
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   901
  this restriction, you can implement your own using the function @{ML_ind
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   902
  inline in ML_Antiquote} from the structure @{ML_struct ML_Antiquote}. The code
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   903
  for the antiquotation @{text "term_pat"} is as follows.
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
   904
*}
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
   905
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 468
diff changeset
   906
ML %linenosgray{*val term_pat_setup = 
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 468
diff changeset
   907
let
325
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
   908
  val parser = Args.context -- Scan.lift Args.name_source
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
   909
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
   910
  fun term_pat (ctxt, str) =
475
25371f74c768 updated to post-2011-1 Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   911
     str |> Proof_Context.read_term_pattern ctxt
264
311830b43f8f minor tuning
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
   912
         |> ML_Syntax.print_term
325
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
   913
         |> ML_Syntax.atomic
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
   914
in
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 468
diff changeset
   915
  ML_Antiquote.inline @{binding "term_pat"} (parser >> term_pat)
325
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
   916
end*}
263
195c4444dff7 added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 262
diff changeset
   917
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   918
text {*
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   919
  To use it you also have to install it using \isacommand{setup} like so
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   920
*}
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   921
479
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   922
setup %gray {* term_pat_setup *}
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 468
diff changeset
   923
263
195c4444dff7 added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 262
diff changeset
   924
text {*
308
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   925
  The parser in Line 2 provides us with a context and a string; this string is
324
4172c0743cf2 updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents: 323
diff changeset
   926
  transformed into a term using the function @{ML_ind read_term_pattern in
475
25371f74c768 updated to post-2011-1 Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   927
  Proof_Context} (Line 5); the next two lines transform the term into a string
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   928
  so that the ML-system can understand it. (All these functions will be explained
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   929
  in more detail in later sections.) An example for this antiquotation is:
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   930
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   931
  @{ML_response_fake [display,gray]
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   932
  "@{term_pat \"Suc (?x::nat)\"}"
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   933
  "Const (\"Suc\", \"nat \<Rightarrow> nat\") $ Var ((\"x\", 0), \"nat\")"}
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   934
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   935
  which shows the internal representation of the term @{text "Suc ?x"}. Similarly
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   936
  we can write an antiquotation for type patterns. Its code is
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   937
*}
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   938
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 468
diff changeset
   939
ML{*val type_pat_setup = 
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 468
diff changeset
   940
let
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   941
  val parser = Args.context -- Scan.lift Args.name_source
298
8057d65607eb some general polishing
Christian Urban <urbanc@in.tum.de>
parents: 293
diff changeset
   942
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   943
  fun typ_pat (ctxt, str) =
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   944
     str |> Syntax.parse_typ ctxt
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   945
         |> ML_Syntax.print_typ
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   946
         |> ML_Syntax.atomic
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   947
in
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 468
diff changeset
   948
  ML_Antiquote.inline @{binding "typ_pat"} (parser >> typ_pat)
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   949
end*}
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   950
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   951
text {*
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   952
  which can be installed with
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   953
*}
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   954
479
7a84649d8839 a few things added First_Steps
Christian Urban <urbanc@in.tum.de>
parents: 478
diff changeset
   955
setup %gray {* type_pat_setup *}
471
f65b9f14d5de updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 468
diff changeset
   956
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
   957
text {*
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   958
  However, a word of warning is in order: Introducing new antiquotations
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   959
  should be done only after careful deliberations. They can make your 
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   960
  code harder to read, than making it easier. 
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   961
263
195c4444dff7 added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 262
diff changeset
   962
  \begin{readmore}
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   963
  The file @{ML_file "Pure/ML/ml_antiquote.ML"} contains the the definitions
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   964
  for most antiquotations. Most of the basic operations on ML-syntax are implemented 
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   965
  in @{ML_file "Pure/ML/ml_syntax.ML"}.
263
195c4444dff7 added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 262
diff changeset
   966
  \end{readmore}
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   967
*}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   968
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
   969
section {* Storing Data in Isabelle\label{sec:storing} *}
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   970
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   971
text {*
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   972
  Isabelle provides mechanisms for storing (and retrieving) arbitrary
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   973
  data. Before we delve into the details, let us digress a bit. Conventional
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   974
  wisdom has it that the type-system of ML ensures that  an
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   975
  @{ML_type "'a list"}, say, can only hold elements of the same type, namely
467
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
   976
  @{ML_type "'a"} (or whatever is substitued for it). Despite this common 
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
   977
  wisdom, however, it is possible to implement a
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   978
  universal type in ML, although by some arguably accidental features of ML.
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   979
  This universal type can be used to store data of different type into a single list.
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   980
  In fact, it allows one to inject and to project data of \emph{arbitrary} type. This is
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   981
  in contrast to datatypes, which only allow injection and projection of data for
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
   982
  some \emph{fixed} collection of types. In light of the conventional wisdom cited
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   983
  above it is important to keep in mind that the universal type does not
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   984
  destroy type-safety of ML: storing and accessing the data can only be done
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   985
  in a type-safe manner...though run-time checks are needed for that.
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   986
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   987
  \begin{readmore}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   988
  In Isabelle the universal type is implemented as the type @{ML_type
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   989
  Universal.universal} in the file
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   990
  @{ML_file "Pure/ML-Systems/universal.ML"}.
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   991
  \end{readmore}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   992
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   993
  We will show the usage of the universal type by storing an integer and
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   994
  a boolean into a single list. Let us first define injection and projection 
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   995
  functions for booleans and integers into and from the type @{ML_type Universal.universal}.
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   996
*}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   997
325
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
   998
ML{*local
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
   999
  val fn_int  = Universal.tag () : int  Universal.tag
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
  1000
  val fn_bool = Universal.tag () : bool Universal.tag
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
  1001
in
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
  1002
  val inject_int   = Universal.tagInject fn_int;
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
  1003
  val inject_bool  = Universal.tagInject fn_bool;
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
  1004
  val project_int  = Universal.tagProject fn_int;
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
  1005
  val project_bool = Universal.tagProject fn_bool
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
  1006
end*}
298
8057d65607eb some general polishing
Christian Urban <urbanc@in.tum.de>
parents: 293
diff changeset
  1007
325
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
  1008
text {*
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1009
  Using the injection functions, we can inject the integer @{ML_text "13"} 
330
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1010
  and the boolean value @{ML_text "true"} into @{ML_type Universal.universal}, and
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1011
  then store them in a @{ML_type "Universal.universal list"} as follows:
325
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
  1012
*}
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
  1013
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1014
ML{*val foo_list = 
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1015
let
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
  1016
  val thirteen = inject_int 13
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1017
  val truth_val = inject_bool true
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1018
in
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1019
  [thirteen, truth_val]
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1020
end*}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1021
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1022
text {*
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1023
  The data can be retrieved with the projection functions defined above.
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1024
  
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1025
  @{ML_response_fake [display, gray]
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1026
"project_int (nth foo_list 0); 
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1027
project_bool (nth foo_list 1)" 
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1028
"13
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1029
true"}
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1030
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1031
  Notice that we access the integer as an integer and the boolean as
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1032
  a boolean. If we attempt to access the integer as a boolean, then we get 
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1033
  a runtime error. 
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1034
  
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1035
  @{ML_response_fake [display, gray]
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1036
"project_bool (nth foo_list 0)"  
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1037
"*** Exception- Match raised"}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1038
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1039
  This runtime error is the reason why ML is still type-sound despite
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1040
  containing a universal type.
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1041
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1042
  Now, Isabelle heavily uses this mechanism for storing all sorts of
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1043
  data: theorem lists, simpsets, facts etc.  Roughly speaking, there are two
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1044
  places where data can be stored in Isabelle: in \emph{theories} and in \emph{proof
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1045
  contexts}. Data such as simpsets are ``global'' and therefore need to be stored
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1046
  in a theory (simpsets need to be maintained across proofs and even across
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1047
  theories).  On the other hand, data such as facts change inside a proof and
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1048
  are only relevant to the proof at hand. Therefore such data needs to be 
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1049
  maintained inside a proof context, which represents ``local'' data.
467
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
  1050
  You can think of a theory as the ``longterm memory'' of Isabelle (nothing will
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
  1051
  be deleted from it), and a proof-context as a ``shortterm memory'' (it dynamically
e11fe0de19a5 soem polishing
Christian Urban <urbanc@in.tum.de>
parents: 466
diff changeset
  1052
  changes according to what is needed at the time).
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1053
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1054
  For theories and proof contexts there are, respectively, the functors 
385
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1055
  @{ML_funct_ind Theory_Data} and @{ML_funct_ind Proof_Data} that help
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1056
  with the data storage. Below we show how to implement a table in which you
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1057
  can store theorems and look them up according to a string key. The
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1058
  intention in this example is to be able to look up introduction rules for logical
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1059
  connectives. Such a table might be useful in an automatic proof procedure
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1060
  and therefore it makes sense to store this data inside a theory.  
385
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1061
  Consequently we use the functor @{ML_funct Theory_Data}.
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1062
  The code for the table is:
325
352e31d9dacc started section about storing data
Christian Urban <urbanc@in.tum.de>
parents: 324
diff changeset
  1063
*}
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
  1064
385
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1065
ML %linenosgray{*structure Data = Theory_Data
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1066
  (type T = thm Symtab.table
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1067
   val empty = Symtab.empty
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1068
   val extend = I
385
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1069
   val merge = Symtab.merge (K true))*}
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1070
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1071
text {*
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1072
  In order to store data in a theory, we have to specify the type of the data
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1073
  (Line 2). In this case we specify the type @{ML_type "thm Symtab.table"},
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1074
  which stands for a table in which @{ML_type string}s can be looked up
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1075
  producing an associated @{ML_type thm}. We also have to specify four
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1076
  functions to use this functor: namely how to initialise the data storage
385
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1077
  (Line 3), how to extend it (Line 4) and how two
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1078
  tables should be merged (Line 5). These functions correspond roughly to the
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1079
  operations performed on theories and we just give some sensible 
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1080
  defaults.\footnote{\bf FIXME: Say more about the
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1081
  assumptions of these operations.} The result structure @{ML_text Data}
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1082
  contains functions for accessing the table (@{ML Data.get}) and for updating
385
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1083
  it (@{ML Data.map}). There is also the functions @{ML Data.put}, which however is 
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1084
  not relevant here. Below we define two
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1085
  auxiliary functions, which help us with accessing the table.
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1086
*}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1087
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1088
ML{*val lookup = Symtab.lookup o Data.get
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1089
fun update k v = Data.map (Symtab.update (k, v))*}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1090
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1091
text {*
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1092
  Since we want to store introduction rules associated with their 
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1093
  logical connective, we can fill the table as follows.
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1094
*}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1095
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1096
setup %gray {*
450
102dc5cc1aed a bit closer to the new conventions of naming HOL-constants
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1097
  update "conj" @{thm conjI} #>
102dc5cc1aed a bit closer to the new conventions of naming HOL-constants
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1098
  update "imp"  @{thm impI}  #>
102dc5cc1aed a bit closer to the new conventions of naming HOL-constants
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1099
  update "all"  @{thm allI}
326
f76135c6c527 more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
  1100
*}
f76135c6c527 more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
  1101
f76135c6c527 more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
  1102
text {*
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1103
  The use of the command \isacommand{setup} makes sure the table in the 
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1104
  \emph{current} theory is updated (this is explained further in 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1105
  section~\ref{sec:theories}). The lookup can now be performed as follows.
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1106
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1107
  @{ML_response_fake [display, gray]
450
102dc5cc1aed a bit closer to the new conventions of naming HOL-constants
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1108
"lookup @{theory} \"conj\""
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1109
"SOME \"\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q\""}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1110
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1111
  An important point to note is that these tables (and data in general)
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1112
  need to be treated in a purely functional fashion. Although
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1113
  we can update the table as follows
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1114
*}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1115
450
102dc5cc1aed a bit closer to the new conventions of naming HOL-constants
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1116
setup %gray {* update "conj" @{thm TrueI} *}
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1117
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1118
text {*
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1119
  and accordingly, @{ML lookup} now produces the introduction rule for @{term "True"}
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1120
  
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1121
@{ML_response_fake [display, gray]
450
102dc5cc1aed a bit closer to the new conventions of naming HOL-constants
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1122
"lookup @{theory} \"conj\""
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1123
"SOME \"True\""}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1124
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1125
  there are no references involved. This is one of the most fundamental
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1126
  coding conventions for programming in Isabelle. References  
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1127
  interfere with the multithreaded execution model of Isabelle and also
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1128
  defeat its undo-mechanism. To see the latter, consider the 
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1129
  following data container where we maintain a reference to a list of 
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1130
  integers.
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1131
*}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1132
385
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1133
ML{*structure WrongRefData = Theory_Data
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1134
  (type T = (int list) Unsynchronized.ref
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1135
   val empty = Unsynchronized.ref []
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1136
   val extend = I
385
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1137
   val merge = fst)*}
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1138
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1139
text {*
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1140
  We initialise the reference with the empty list. Consequently a first 
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1141
  lookup produces @{ML "ref []" in Unsynchronized}.
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1142
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1143
  @{ML_response_fake [display,gray]
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1144
  "WrongRefData.get @{theory}"
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1145
  "ref []"}
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1146
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1147
  For updating the reference we use the following function
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1148
*}
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1149
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1150
ML{*fun ref_update n = WrongRefData.map 
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1151
      (fn r => let val _ = r := n::(!r) in r end)*}
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1152
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1153
text {*
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1154
  which takes an integer and adds it to the content of the reference.
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1155
  As before, we update the reference with the command 
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1156
  \isacommand{setup}. 
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1157
*}
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1158
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1159
setup %gray {* ref_update 1 *}
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1160
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1161
text {*
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1162
  A lookup in the current theory gives then the expected list 
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1163
  @{ML "ref [1]" in Unsynchronized}.
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1164
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1165
  @{ML_response_fake [display,gray]
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1166
  "WrongRefData.get @{theory}"
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1167
  "ref [1]"}
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1168
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1169
  So far everything is as expected. But, the trouble starts if we attempt to
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1170
  backtrack to the ``point'' before the \isacommand{setup}-command. There, we
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1171
  would expect that the list is empty again. But since it is stored in a
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1172
  reference, Isabelle has no control over it. So it is not empty, but still
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1173
  @{ML "ref [1]" in Unsynchronized}. Adding to the trouble, if we execute the
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1174
  \isacommand{setup}-command again, we do not obtain @{ML "ref [1]" in
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1175
  Unsynchronized}, but
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1176
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1177
  @{ML_response_fake [display,gray]
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1178
  "WrongRefData.get @{theory}"
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1179
  "ref [1, 1]"}
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1180
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
  1181
  Now imagine how often you go backwards and forwards in your proof 
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
  1182
  scripts.\footnote{The same problem can be triggered in the Jedit GUI by
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
  1183
  making the parser to go over and over again over the \isacommand{setup} command.} 
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1184
  By using references in Isabelle code, you are bound to cause all
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1185
  hell to break loose. Therefore observe the coding convention: 
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1186
  Do not use references for storing data!
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1187
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1188
  \begin{readmore}
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1189
  The functors for data storage are defined in @{ML_file "Pure/context.ML"}.
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1190
  Isabelle contains implementations of several container data structures,
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1191
  including association lists in @{ML_file "Pure/General/alist.ML"},
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1192
  directed graphs in @{ML_file "Pure/General/graph.ML"}, and 
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1193
  tables and symtables in @{ML_file "Pure/General/table.ML"}.
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1194
  \end{readmore}
327
ce754ad78bc9 more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
  1195
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1196
  Storing data in a proof context is done in a similar fashion. As mentioned
385
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1197
  before, the corresponding functor is @{ML_funct_ind Proof_Data}. With the
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1198
  following code we can store a list of terms in a proof context.
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1199
*}
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1200
385
78c91a629602 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
  1201
ML{*structure Data = Proof_Data
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1202
  (type T = term list
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1203
   fun init _ = [])*}
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1204
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1205
text {*
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
  1206
  The init-function we have to specify must produce a list for when a context 
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1207
  is initialised (possibly taking the theory into account from which the 
372
Christian Urban <urbanc@in.tum.de>
parents: 371
diff changeset
  1208
  context is derived). We choose here to just return the empty list. Next 
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1209
  we define two auxiliary functions for updating the list with a given
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1210
  term and printing the list. 
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1211
*}
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1212
330
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1213
ML{*fun update trm = Data.map (fn trms => trm::trms)
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1214
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1215
fun print ctxt =
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1216
  case (Data.get ctxt) of
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
  1217
    [] => pwriteln (Pretty.str "Empty!")
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
  1218
  | trms => pwriteln (pretty_terms ctxt trms)*}
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1219
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1220
text {*
330
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1221
  Next we start with the context generated by the antiquotation 
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1222
  @{text "@{context}"} and update it in various ways. 
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1223
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1224
  @{ML_response_fake [display,gray]
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1225
"let
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1226
  val ctxt0 = @{context}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1227
  val ctxt1 = ctxt0 |> update @{term \"False\"}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1228
                    |> update @{term \"True \<and> True\"} 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1229
  val ctxt2 = ctxt0 |> update @{term \"1::nat\"}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1230
  val ctxt3 = ctxt2 |> update @{term \"2::nat\"}
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1231
in
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1232
  print ctxt0; 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1233
  print ctxt1;
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1234
  print ctxt2;
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1235
  print ctxt3
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1236
end"
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1237
"Empty!
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1238
True \<and> True, False
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1239
1
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1240
2, 1"}
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1241
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1242
  Many functions in Isabelle manage and update data in a similar
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
  1243
  fashion. Consequently, such calculations with contexts occur frequently in
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1244
  Isabelle code, although the ``context flow'' is usually only linear.
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1245
  Note also that the calculation above has no effect on the underlying
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1246
  theory. Once we throw away the contexts, we have no access to their
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
  1247
  associated data. This is different for theories, where the command 
328
c0cae24b9d46 updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents: 327
diff changeset
  1248
  \isacommand{setup} registers the data with the current and future 
330
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1249
  theories, and therefore one can access the data potentially 
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1250
  indefinitely.
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1251
484
490fe9483c0d more material
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
  1252
  Move elsewhere
490fe9483c0d more material
Christian Urban <urbanc@in.tum.de>
parents: 483
diff changeset
  1253
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1254
  For convenience there is an abstract layer, namely the type @{ML_type Context.generic}, 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1255
  for treating theories and proof contexts more uniformly. This type is defined as follows
330
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1256
*}
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1257
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1258
ML_val{*datatype generic = 
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1259
  Theory of theory 
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1260
| Proof of proof*}
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1261
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1262
text {*
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1263
  \footnote{\bf FIXME: say more about generic contexts.}
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1264
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1265
  There are two special instances of the data storage mechanism described 
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1266
  above. The first instance implements named theorem lists using the functor
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1267
  @{ML_funct_ind Named_Thms}. This is because storing theorems in a list 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1268
  is such a common task.  To obtain a named theorem list, you just declare
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1269
*}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1270
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1271
ML{*structure FooRules = Named_Thms
481
32323727af96 updated
Christian Urban <urbanc@in.tum.de>
parents: 480 479
diff changeset
  1272
  (val name = @{binding "foo"} 
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1273
   val description = "Theorems for foo") *}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1274
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1275
text {*
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1276
  and set up the @{ML_struct FooRules} with the command
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1277
*}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1278
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1279
setup %gray {* FooRules.setup *}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1280
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1281
text {*
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1282
  This code declares a data container where the theorems are stored,
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1283
  an attribute @{text foo} (with the @{text add} and @{text del} options
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
  1284
  for adding and deleting theorems) and an internal ML-interface for retrieving and 
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 376
diff changeset
  1285
  modifying the theorems.
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1286
  Furthermore, the theorems are made available on the user-level under the name 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1287
  @{text foo}. For example you can declare three lemmas to be a member of the 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1288
  theorem list @{text foo} by:
326
f76135c6c527 more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
  1289
*}
f76135c6c527 more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents: 325
diff changeset
  1290
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1291
lemma rule1[foo]: "A" sorry
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1292
lemma rule2[foo]: "B" sorry
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1293
lemma rule3[foo]: "C" sorry
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1294
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1295
text {* and undeclare the first one by: *}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1296
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1297
declare rule1[foo del]
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1298
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1299
text {* You can query the remaining ones with:
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1300
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1301
  \begin{isabelle}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1302
  \isacommand{thm}~@{text "foo"}\\
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1303
  @{text "> ?C"}\\
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1304
  @{text "> ?B"}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1305
  \end{isabelle}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1306
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1307
  On the ML-level, we can add theorems to the list with @{ML FooRules.add_thm}:
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1308
*} 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1309
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1310
setup %gray {* Context.theory_map (FooRules.add_thm @{thm TrueI}) *}
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1311
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1312
text {*
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1313
  The rules in the list can be retrieved using the function 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1314
  @{ML FooRules.get}:
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1315
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1316
  @{ML_response_fake [display,gray] 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1317
  "FooRules.get @{context}" 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1318
  "[\"True\", \"?C\",\"?B\"]"}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1319
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1320
  Note that this function takes a proof context as argument. This might be 
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1321
  confusing, since the theorem list is stored as theory data. It becomes clear by knowing
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1322
  that the proof context contains the information about the current theory and so the function
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1323
  can access the theorem list in the theory via the context. 
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1324
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1325
  \begin{readmore}
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1326
  For more information about named theorem lists see 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1327
  @{ML_file "Pure/Tools/named_thms.ML"}.
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1328
  \end{readmore}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1330
  The second special instance of the data storage mechanism are configuration
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1331
  values. They are used to enable users to configure tools without having to
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1332
  resort to the ML-level (and also to avoid references). Assume you want the
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1333
  user to control three values, say @{text bval} containing a boolean, @{text
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1334
  ival} containing an integer and @{text sval} containing a string. These
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1335
  values can be declared by
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1336
*}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1337
462
1d1e795bc3ad updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
  1338
ML{*val bval = Attrib.setup_config_bool @{binding "bval"} (K false)
1d1e795bc3ad updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
  1339
val ival = Attrib.setup_config_int @{binding "ival"} (K 0)
1d1e795bc3ad updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
  1340
val sval = Attrib.setup_config_string @{binding "sval"} (K "some string") *}
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1341
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1342
text {* 
462
1d1e795bc3ad updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
  1343
  where each value needs to be given a default. 
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1344
  The user can now manipulate the values from the user-level of Isabelle 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1345
  with the command
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1346
*}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1347
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1348
declare [[bval = true, ival = 3]]
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1349
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1350
text {*
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1351
  On the ML-level these values can be retrieved using the 
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1352
  function @{ML_ind get in Config} from a proof context
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1353
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1354
  @{ML_response [display,gray] 
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1355
  "Config.get @{context} bval" 
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1356
  "true"}
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1357
423
0a25b35178c3 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 421
diff changeset
  1358
  or directly from a theory using the function @{ML_ind get_global in Config}
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1359
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1360
  @{ML_response [display,gray] 
423
0a25b35178c3 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 421
diff changeset
  1361
  "Config.get_global @{theory} bval" 
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1362
  "true"}
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1363
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1364
  It is also possible to manipulate the configuration values
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1365
  from the ML-level with the functions @{ML_ind put in Config}
423
0a25b35178c3 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 421
diff changeset
  1366
  and @{ML_ind put_global in Config}. For example
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1367
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1368
  @{ML_response [display,gray]
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1369
  "let
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1370
  val ctxt = @{context}
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1371
  val ctxt' = Config.put sval \"foo\" ctxt
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1372
  val ctxt'' = Config.put sval \"bar\" ctxt'
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1373
in
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1374
  (Config.get ctxt sval, 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1375
   Config.get ctxt' sval, 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1376
   Config.get ctxt'' sval)
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1377
end"
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1378
  "(\"some string\", \"foo\", \"bar\")"}
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1379
468
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1380
  A concrete example for a configuration value is 
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1381
  @{ML_ind simp_trace in Raw_Simplifier}, which switches on trace information
474
683fb6e468b1 a few things updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
  1382
  in the simplifier. This can be used for example in the following proof
468
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1383
*}
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1384
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1385
lemma
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1386
  shows "(False \<or> True) \<and> True"
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1387
proof (rule conjI)
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1388
  show "False \<or> True" using [[simp_trace = true]] by simp
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1389
next
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1390
  show "True" by simp
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1391
qed
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1392
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1393
text {*
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1394
  in order to inspect how the simplifier solves the first subgoal.
00921ae66622 more polishing
Christian Urban <urbanc@in.tum.de>
parents: 467
diff changeset
  1395
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1396
  \begin{readmore}
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1397
  For more information about configuration values see 
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1398
  the files @{ML_file "Pure/Isar/attrib.ML"} and 
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 344
diff changeset
  1399
  @{ML_file "Pure/config.ML"}.
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1400
  \end{readmore}
343
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1401
*}
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1402
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1403
section {* Summary *}
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1404
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1405
text {*
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1406
  This chapter describes the combinators that are used in Isabelle, as well
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1407
  as a simple printing infrastructure for @{ML_type term}, @{ML_type cterm}
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1408
  and @{ML_type thm}. The section on ML-antiquotations shows how to refer 
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1409
  statically to entities from the logic level of Isabelle. Isabelle also
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1410
  contains mechanisms for storing arbitrary data in theory and proof 
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1411
  contexts.
8f73e80c8c6f polished first chapter
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1412
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1413
  \begin{conventions}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1414
  \begin{itemize}
370
2494b5b7a85d added something about show_types references
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
  1415
  \item Print messages that belong together in a single string.
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1416
  \item Do not use references in Isabelle code.
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1417
  \end{itemize}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1418
  \end{conventions}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1419
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1420
*}
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
  1421
end