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