ProgTutorial/FirstSteps.thy
author Christian Urban <urbanc@in.tum.de>
Mon, 28 Sep 2009 01:21:27 +0200
changeset 323 92f6a772e013
parent 322 2b7c08d90e2e
child 324 4172c0743cf2
permissions -rw-r--r--
some polishing
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
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
chapter {* First Steps *}
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
     8
text {*
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
     9
   \begin{flushright}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    10
  {\em
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    11
  ``We will most likely never realize the full importance of painting the Tower,\\ 
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    12
  that it is the essential element in the conservation of metal works and the\\ 
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    13
  more meticulous the paint job, the longer the Tower shall endure.''} \\[1ex]
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    14
  Gustave Eiffel, In The 300-Meter Tower.\footnote{The Eiffel Tower has been 
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    15
  re-painted 18 times since its initial construction, an average of once every 
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    16
  seven years. It takes more than a year for a team of 25 painters to paint the Tower 
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    17
  from top to bottom.}\\[1ex]
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    18
  \end{flushright}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
    19
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    20
  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
    21
  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
    22
  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
    23
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
    24
  \begin{quote}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    25
  \begin{tabular}{@ {}l}
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
    26
  \isacommand{theory} FirstSteps\\
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    27
  \isacommand{imports} Main\\
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    28
  \isacommand{begin}\\
6
007e09485351 some tuning
Christian Urban <urbanc@in.tum.de>
parents: 5
diff changeset
    29
  \ldots
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    30
  \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
    31
  \end{quote}
157
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
    32
76cdc8f562fc added more to the simplifier section
Christian Urban <urbanc@in.tum.de>
parents: 156
diff changeset
    33
  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
    34
  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
    35
*}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
    36
20
5ae6a1bb91c9 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 19
diff changeset
    37
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
    38
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    39
text {*
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    40
  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
    41
  \isacommand{ML}-command. For example:
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
  \begin{isabelle}
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    44
  \begin{graybox}
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    45
  \isacommand{ML}~@{text "\<verbopen>"}\isanewline
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    46
  \hspace{5mm}@{ML "3 + 4"}\isanewline
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    47
  @{text "\<verbclose>"}\isanewline
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    48
  @{text "> 7"}\smallskip
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    49
  \end{graybox}
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    50
  \end{isabelle}
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    51
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    52
  Like normal Isabelle scripts, \isacommand{ML}-commands can be evaluated by
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    53
  using the advance and undo buttons of your Isabelle environment. The code
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    54
  inside the \isacommand{ML}-command can also contain value and function
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    55
  bindings, for example
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    56
*}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    57
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    58
ML %gray {* 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    59
  val r = ref 0
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    60
  fun f n = n + 1 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    61
*}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    62
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
    63
text {*
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    64
  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
    65
  mentioned in the Introduction, we will drop the \isacommand{ML}~@{text
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    66
  "\<verbopen> \<dots> \<verbclose>"} scaffolding whenever we show code. The lines
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    67
  prefixed with @{text [quotes] ">"} are not part of the code, rather they
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    68
  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
    69
  the commands \isacommand{ML\_val} and \isacommand{ML\_prf} for including
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    70
  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
    71
  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
    72
  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
    73
254
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    74
  \begin{quote}
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    75
  \begin{isabelle}
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    76
  \isacommand{lemma}~@{text "test:"}\isanewline
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    77
  \isacommand{shows}~@{text [quotes] "True"}\isanewline
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    78
  \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
    79
  \isacommand{oops}
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    80
  \end{isabelle}
cb86bf5658e4 some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 253
diff changeset
    81
  \end{quote}
253
3cfd9a8a6de1 added comments about ML_prf and ML_val
Christian Urban <urbanc@in.tum.de>
parents: 252
diff changeset
    82
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    83
  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
    84
  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
    85
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
    86
  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
    87
  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
    88
  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
    89
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
    90
  \begin{quote}
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    91
  \begin{tabular}{@ {}l}
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    92
  \isacommand{theory} FirstSteps\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    93
  \isacommand{imports} Main\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    94
  \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
    95
  \isacommand{begin}\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    96
  \ldots\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    97
  \isacommand{use}~@{text "\"file_to_be_included.ML\""}\\
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    98
  \ldots
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
    99
  \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
   100
  \end{quote}
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   101
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   102
  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
   103
  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
   104
  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
   105
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
   106
  \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
   107
  \begin{tabular}{@ {}l}
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
   108
  \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
   109
  \isacommand{imports} Main\\
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   110
  \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
   111
  \isacommand{begin}\\
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   112
  \ldots
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   113
  \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
   114
  \end{quote}
235
dc955603d813 explained uses and use commands
Christian Urban <urbanc@in.tum.de>
parents: 234
diff changeset
   115
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   116
  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
   117
  ML-file should not contain any \isacommand{use} itself. Otherwise Isabelle
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   118
  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
   119
  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
   120
*}
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   121
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   122
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
   123
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   124
text {*
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   125
  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
   126
  code. This can be done in a ``quick-and-dirty'' fashion using the function
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   127
  @{ML_ind  "writeln"}. For example
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   128
317
d69214e47ef9 added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   129
  @{ML_response_eq [display,gray] "writeln \"any string\"" "\"any string\"" with "(op =)"}
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   130
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   131
  will print out @{text [quotes] "any string"} inside the response buffer of
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   132
  Isabelle.  This function expects a string as argument. If you develop under
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   133
  PolyML, then there is a convenient, though again ``quick-and-dirty'', method
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   134
  for converting values into strings, namely the function 
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   135
  @{ML_ind  makestring in PolyML}:
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   136
317
d69214e47ef9 added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   137
  @{ML_response_eq [display,gray] "writeln (PolyML.makestring 1)" "\"1\"" with "(op =)"} 
12
2f1736cb8f26 various changes by Alex and Christian
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
   138
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   139
  However, @{ML makestring in PolyML} only works if the type of what
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   140
  is converted is monomorphic and not a function.
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   141
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   142
  The function @{ML "writeln"} should only be used for testing purposes,
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   143
  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
   144
  error is raised. For printing anything more serious and elaborate, the
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   145
  function @{ML_ind  tracing} is more appropriate. This function writes all
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   146
  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
   147
317
d69214e47ef9 added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   148
  @{ML_response_eq [display,gray] "tracing \"foo\"" "\"foo\"" with "(op =)"}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   149
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   150
  It is also possible to redirect the ``channel'' where the string @{text
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   151
  "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
   152
  choking on massive amounts of trace output. This redirection can be achieved
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   153
  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
   154
*}
14
1c17e99f6f66 added a paragraph about "uses" and started a paragraph about tracing
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   155
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   156
ML{*val strip_specials =
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   157
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
   158
  fun strip ("\^A" :: _ :: cs) = strip cs
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   159
    | strip (c :: cs) = c :: strip cs
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   160
    | strip [] = [];
306
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   161
in 
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   162
  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
   163
end
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   164
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   165
fun redirect_tracing stream =
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 41
diff changeset
   166
 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
   167
    (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
   168
     TextIO.output (stream, "\n");
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   169
     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
   170
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
   171
text {*
307
f4fa6540e280 suggestions by Peter Homeier
Christian Urban <urbanc@in.tum.de>
parents: 306
diff changeset
   172
  Calling now
f4fa6540e280 suggestions by Peter Homeier
Christian Urban <urbanc@in.tum.de>
parents: 306
diff changeset
   173
f4fa6540e280 suggestions by Peter Homeier
Christian Urban <urbanc@in.tum.de>
parents: 306
diff changeset
   174
  @{ML [display,gray] "redirect_tracing (TextIO.openOut \"foo.bar\")"}
f4fa6540e280 suggestions by Peter Homeier
Christian Urban <urbanc@in.tum.de>
parents: 306
diff changeset
   175
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   176
  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
   177
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   178
  You can print out error messages with the function @{ML_ind  error}; for 
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   179
  example:
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   180
122
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   181
@{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
   182
"Exception- ERROR \"foo\" raised
79696161ae16 polished
Christian Urban <urbanc@in.tum.de>
parents: 120
diff changeset
   183
At command \"ML\"."}
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   184
307
f4fa6540e280 suggestions by Peter Homeier
Christian Urban <urbanc@in.tum.de>
parents: 306
diff changeset
   185
  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
   186
  be displayed by the infrastructure.
14173c0e8688 polished comment for error function
Christian Urban <urbanc@in.tum.de>
parents: 301
diff changeset
   187
14173c0e8688 polished comment for error function
Christian Urban <urbanc@in.tum.de>
parents: 301
diff changeset
   188
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   189
  (FIXME Mention how to work with @{ML_ind  debug in Toplevel} and 
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   190
  @{ML_ind  profiling in Toplevel}.)
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   191
*}
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   192
322
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   193
(* FIXME
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   194
ML {* reset Toplevel.debug *}
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   195
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   196
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
   197
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   198
ML {* fun innocent () = dodgy_fun () *}
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   199
ML {* exception_trace (fn () => cterm_of @{theory} (Bound 0)) *}
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   200
ML {* exception_trace (fn () => innocent ()) *}
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   201
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   202
ML {* Toplevel.program (fn () => cterm_of @{theory} (Bound 0)) *}
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   203
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   204
ML {* Toplevel.program (fn () => innocent ()) *}
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   205
*)
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   206
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   207
text {*
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   208
  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
   209
  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
   210
  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
   211
  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
   212
  are a bit unwieldy. One way to transform a term into a string is to use the
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   213
  function @{ML_ind  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
   214
  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
   215
*}
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   216
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   217
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
   218
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   219
text {*
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   220
  It can now be used as follows
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   221
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   222
  @{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
   223
  "string_of_term @{context} @{term \"1::nat\"}"
317
d69214e47ef9 added an experimental antiquotation to replace eventually ML_response_fake
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   224
  "\"\\^E\\^Fterm\\^E\\^E\\^Fconst\\^Fname=HOL.one_class.one\\^E1\\^E\\^F\\^E\\^E\\^F\\^E\""} 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   225
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   226
  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
   227
  additional information encoded in it. The string can be properly printed by
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   228
  using either the function @{ML_ind  writeln} or @{ML_ind  tracing}:
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   229
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   230
  @{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
   231
  "writeln (string_of_term @{context} @{term \"1::nat\"})"
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   232
  "\"1\""}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   233
308
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   234
  or
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   235
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   236
  @{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
   237
  "tracing (string_of_term @{context} @{term \"1::nat\"})"
308
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   238
  "\"1\""}
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   239
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   240
  If there are more than one @{ML_type term}s to be printed, you can use the 
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   241
  function @{ML_ind  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
   242
*} 
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   243
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   244
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
   245
  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
   246
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   247
text {*
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   248
  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
   249
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   250
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   251
ML{*fun string_of_cterm ctxt ct =  
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   252
  string_of_term ctxt (term_of ct)*}
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 {*
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   255
  In this example the function @{ML_ind  term_of} extracts the @{ML_type
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   256
  term} from a @{ML_type cterm}.  More than one @{ML_type cterm}s can again be
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   257
  printed with @{ML_ind  commas}.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   258
*} 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   259
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   260
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
   261
  commas (map (string_of_cterm ctxt) cts)*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   262
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   263
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   264
  The easiest way to get the string of a theorem is to transform it
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   265
  into a @{ML_type cterm} using the function @{ML_ind  crep_thm}. 
190
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   266
*}
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   267
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
   268
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
   269
  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
   270
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   271
text {*
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   272
  Theorems also include schematic variables, such as @{text "?P"}, 
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   273
  @{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
   274
  instantiate theorems when they are applied. For example the theorem 
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   275
  @{thm [source] conjI} shown below can be used for any (typable) 
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   276
  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
   277
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   278
  @{ML_response_fake [display, gray]
301
2728e8daebc0 replaced "writeln" with "tracing"
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   279
  "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
   280
  "\<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
   281
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   282
  However, in order to improve the readability when printing theorems, we
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   283
  convert these schematic variables into free variables using the function
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   284
  @{ML_ind  import in Variable}. This is similar to statements like @{text
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   285
  "conjI[no_vars]"} from Isabelle's user-level.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   286
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   287
158
d7944bdf7b3f removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   288
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
   289
let 
263
195c4444dff7 added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 262
diff changeset
   290
  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
   291
in
d7944bdf7b3f removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   292
  thm'
d7944bdf7b3f removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   293
end
d7944bdf7b3f removed infix_conv and moved function no_vars into the FirstSteps chapter
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
   294
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
   295
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
   296
  string_of_cterm ctxt (#prop (crep_thm (no_vars ctxt thm)))*}
126
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
text {* 
207
d3cd633e8240 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 204
diff changeset
   299
  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
   300
ca0ac2e75f6d more one the simple-inductive chapter
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
   301
  @{ML_response_fake [display, gray]
301
2728e8daebc0 replaced "writeln" with "tracing"
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   302
  "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
   303
  "\<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
   304
  
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   305
  Again the function @{ML commas} helps with printing more than one theorem. 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   306
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   307
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
   308
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
   309
  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
   310
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
   311
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
   312
  commas (map (string_of_thm_no_vars ctxt) thms) *}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   313
305
2ac9dc1a95b4 added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents: 304
diff changeset
   314
text {*
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   315
  Note, that when printing out several parcels of information that
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   316
  semantically belong together, like a warning message consisting for example
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   317
  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
   318
  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
   319
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   320
@{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
   321
"tracing \"First half,\"; 
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   322
tracing \"and second half.\""
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   323
"First half,
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   324
and second half."}
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   325
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   326
  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
   327
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   328
@{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
   329
"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
   330
"First half,
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   331
and second half."}
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   332
  
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   333
  To ease this kind of string manipulations, there are a number
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   334
  of library functions. For example,  the function @{ML_ind  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
   335
  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
   336
2ac9dc1a95b4 added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents: 304
diff changeset
   337
  @{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
   338
  "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
   339
  "\"foo\\nbar\""}
306
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   340
fe732e890d87 tuned the section about printing several bits of inormation
Christian Urban <urbanc@in.tum.de>
parents: 305
diff changeset
   341
  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
   342
  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
   343
*}
2ac9dc1a95b4 added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents: 304
diff changeset
   344
2ac9dc1a95b4 added a comment for printing out information and tuned some examples accordingly
Christian Urban <urbanc@in.tum.de>
parents: 304
diff changeset
   345
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   346
section {* Combinators\label{sec:combinators} *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   347
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   348
text {*
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   349
  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
   350
  the combinators. At first they seem to greatly obstruct the
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   351
  comprehension of the code, but after getting familiar with them, they
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   352
  actually ease the understanding and also the programming.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   353
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   354
  The simplest combinator is @{ML_ind  I}, which is just the identity function defined as
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   355
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   356
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   357
ML{*fun I x = x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   358
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   359
text {* Another simple combinator is @{ML_ind  K}, 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
ML{*fun K x = fn _ => x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   362
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   363
text {*
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   364
  @{ML_ind  K} ``wraps'' a function around the argument @{text "x"}. However, this 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   365
  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
   366
  always returning @{text x}.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   367
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   368
  The next combinator is reverse application, @{ML_ind  "|>"}, defined as: 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   369
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   370
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   371
ML{*fun x |> f = f x*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   372
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   373
text {* While just syntactic sugar for the usual function application,
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   374
  the purpose of this combinator is to implement functions in a  
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   375
  ``waterfall fashion''. Consider for example the function *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   376
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   377
ML %linenosgray{*fun inc_by_five x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   378
  x |> (fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   379
    |> (fn x => (x, x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   380
    |> fst
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   381
    |> (fn x => x + 4)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   382
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   383
text {*
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   384
  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
   385
  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
   386
  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
   387
  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
   388
  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
   389
  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
   390
  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
   391
  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
   392
  @{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
   393
*}
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 = fst ((fn x => (x, x)) (x + 1)) + 4*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   396
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   397
text {* or *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   398
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   399
ML{*fun inc_by_five x = 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   400
       ((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
   401
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   402
text {* and typographically more economical than *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   403
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   404
ML{*fun inc_by_five x =
257
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   405
let val y1 = x + 1
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   406
    val y2 = (y1, y1)
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   407
    val y3 = fst y2
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   408
    val y4 = y3 + 4
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   409
in y4 end*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   410
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   411
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   412
  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
   413
  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
   414
  mention the nightmares the maintenance of this code causes!
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   415
215
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   416
  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
   417
  the waterfall fashion might be the following code:
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   418
*}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   419
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   420
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
   421
    f |> fastype_of
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   422
      |> binder_types 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   423
      |> map (pair "z")
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   424
      |> Variable.variant_frees ctxt [f]
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   425
      |> map Free  
257
ce0f60d0351e corrected index references and minor polishing
Christian Urban <urbanc@in.tum.de>
parents: 256
diff changeset
   426
      |> curry list_comb f *}
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   427
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   428
text {*
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   429
  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
   430
  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
   431
  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
   432
  @{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
   433
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   434
  @{ML_response_fake [display,gray]
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   435
"let
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   436
  val t = @{term \"P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool\"}
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   437
  val ctxt = @{context}
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   438
in 
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   439
  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
   440
   |> string_of_term ctxt
301
2728e8daebc0 replaced "writeln" with "tracing"
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   441
   |> tracing
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   442
end" 
183
8bb4eaa2ec92 a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents: 178
diff changeset
   443
  "P z za zb"}
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   444
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 183
diff changeset
   445
  You can read off this behaviour from how @{ML apply_fresh_args} is
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   446
  coded: in Line 2, the function @{ML_ind  fastype_of} calculates the type of the
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   447
  term; @{ML_ind  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
   448
  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
   449
  pairs up each type with the string  @{text "z"}; the
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   450
  function @{ML_ind  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
   451
  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
   452
  into a list of variable terms in Line 6, which in the last line is applied
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   453
  by the function @{ML_ind  list_comb} to the term. In this last step we have to 
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   454
  use the function @{ML_ind  curry}, because @{ML_ind  list_comb} expects the 
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
   455
  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
   456
  constructing terms with fresh variables. The infrastructure helps tremendously 
8057d65607eb some general polishing
Christian Urban <urbanc@in.tum.de>
parents: 293
diff changeset
   457
  to avoid any name clashes. Consider for example: 
252
f380b13b25a7 added an example
Christian Urban <urbanc@in.tum.de>
parents: 251
diff changeset
   458
f380b13b25a7 added an example
Christian Urban <urbanc@in.tum.de>
parents: 251
diff changeset
   459
   @{ML_response_fake [display,gray]
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   460
"let
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   461
  val t = @{term \"za::'a \<Rightarrow> 'b \<Rightarrow> 'c\"}
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   462
  val ctxt = @{context}
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   463
in
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   464
  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
   465
   |> string_of_term ctxt
301
2728e8daebc0 replaced "writeln" with "tracing"
Christian Urban <urbanc@in.tum.de>
parents: 299
diff changeset
   466
   |> tracing
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   467
end"
252
f380b13b25a7 added an example
Christian Urban <urbanc@in.tum.de>
parents: 251
diff changeset
   468
  "za z zb"}
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   469
  
266
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   470
  where the @{text "za"} is correctly avoided.
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   471
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   472
  The combinator @{ML_ind  "#>"} is the reverse function composition. It can be
177
4e2341f6599d polishing
Christian Urban <urbanc@in.tum.de>
parents: 171
diff changeset
   473
  used to define the following function
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   474
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   475
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   476
ML{*val inc_by_six = 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   477
      (fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   478
   #> (fn x => x + 2)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   479
   #> (fn x => x + 3)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   480
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   481
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   482
  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
   483
  increment-by-two, followed by increment-by-three. Again, the reverse function 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   484
  composition allows you to read the code top-down.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   485
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   486
  The remaining combinators described in this section add convenience for the
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   487
  ``waterfall method'' of writing functions. The combinator @{ML_ind  tap} allows
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   488
  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
   489
  instance). The function
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   490
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   491
 *}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   492
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   493
ML %linenosgray{*fun inc_by_three x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   494
     x |> (fn x => x + 1)
240
d111f5988e49 replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents: 239
diff changeset
   495
       |> tap (fn x => tracing (PolyML.makestring x))
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   496
       |> (fn x => x + 2)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   497
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   498
text {* 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   499
  increments the argument first by @{text "1"} and then by @{text "2"}. In the
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   500
  middle (Line 3), however, it uses @{ML_ind  tap} for printing the ``plus-one''
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   501
  intermediate result inside the tracing buffer. The function @{ML_ind  tap} can
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   502
  only be used for side-calculations, because any value that is computed
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   503
  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
   504
  the next combinator.
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   505
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   506
  The combinator @{ML_ind  "`"} (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
   507
  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
   508
  pair). For example the function 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   509
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   510
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   511
ML{*fun inc_as_pair x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   512
     x |> `(fn x => x + 1)
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   513
       |> (fn (x, y) => (x, y + 1))*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   514
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   515
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   516
  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
   517
  @{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
   518
  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
   519
  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
   520
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   521
  The combinators @{ML_ind  "|>>"} and @{ML_ind  "||>"} are defined for 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   522
  functions manipulating pairs. The first applies the function to
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   523
  the first component of the pair, defined as
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
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   526
ML{*fun (x, y) |>> f = (f x, y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   527
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   528
text {*
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   529
  and the second combinator to the second component, defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   530
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   531
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   532
ML{*fun (x, y) ||> f = (x, f y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   533
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   534
text {*
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   535
  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
   536
  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
   537
  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
   538
  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
   539
  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
   540
  implemented as
308
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
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   543
ML{*fun separate i [] = ([], [])
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   544
  | separate i (x::xs) =
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   545
      let 
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   546
        val (los, grs) = separate i xs
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   547
      in
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   548
        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
   549
      end*}
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   550
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   551
text {*
309
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   552
  where however the return value of the recursive call is bound explicitly to
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   553
  the pair @{ML "(los, grs)" for los grs}. You can implement this function
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   554
  more concisely as
308
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   555
*}
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   556
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   557
ML{*fun separate i [] = ([], [])
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   558
  | separate i (x::xs) =
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   559
      if i <= x 
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   560
      then separate i xs ||> cons x
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   561
      else separate i xs |>> cons x*}
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   562
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   563
text {*
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   564
  avoiding the explicit @{text "let"}. While in this example the gain in
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   565
  conciseness is only small, in more complicated situations the benefit of
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   566
  avoiding @{text "lets"} can be substantial.
308
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   567
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   568
  With the combinator @{ML_ind  "|->"} you can re-combine the elements from a pair.
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   569
  This combinator is defined as
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   570
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   571
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   572
ML{*fun (x, y) |-> f = f 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 {* 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   575
  and can be used to write the following roundabout version 
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   576
  of the @{text double} function: 
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   577
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   578
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   579
ML{*fun double x =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   580
      x |>  (fn x => (x, x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   581
        |-> (fn x => fn y => x + y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   582
215
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   583
text {* 
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   584
  The combinator @{ML_ind  ||>>} 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
   585
  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
   586
  for such tasks return a pair whose second component is the theory and the fist 
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   587
  component is the side-result. Using @{ML_ind  ||>>}, you can do conveniently the update 
280
a63ca3a9b44a spell checked
griff
parents: 279
diff changeset
   588
  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
   589
*}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   590
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   591
ML %linenosgray{*fun acc_incs x =
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   592
    x |> (fn x => ("", x)) 
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   593
      ||>> (fn x => (x, x + 1))
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   594
      ||>> (fn x => (x, x + 1))
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   595
      ||>> (fn x => (x, x + 1))*}
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
text {*
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   598
  The purpose of Line 2 is to just pair up the argument with a dummy value (since
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   599
  @{ML_ind  "||>>"} operates on pairs). Each of the next three lines just increment 
280
a63ca3a9b44a spell checked
griff
parents: 279
diff changeset
   600
  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
   601
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   602
  @{ML_response [display,gray]
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   603
  "acc_incs 1"
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   604
  "((((\"\", 1), 2), 3), 4)"}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   605
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   606
  You can continue this chain with:
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   607
  
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   608
  @{ML_response [display,gray]
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   609
  "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
   610
  "(((((\"\", 1), 2), 3), 4), 6)"}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   611
298
8057d65607eb some general polishing
Christian Urban <urbanc@in.tum.de>
parents: 293
diff changeset
   612
  (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
   613
*}
8d1a344a621e more work on the inductive package
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   614
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   615
text {*
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   616
  Recall that @{ML_ind  "|>"} is the reverse function application. Recall also that
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   617
  the related 
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   618
  reverse function composition is @{ML_ind  "#>"}. In fact all the combinators 
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   619
  @{ML_ind  "|->"}, @{ML_ind  "|>>"} , @{ML_ind  "||>"} and @{ML_ind  
256
1fb8d62c88a0 added some first index-information
Christian Urban <urbanc@in.tum.de>
parents: 255
diff changeset
   620
  "||>>"} described above have related combinators for 
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   621
  function composition, namely @{ML_ind  "#->"}, @{ML_ind  "#>>"}, 
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   622
  @{ML_ind  "##>"} and @{ML_ind  "##>>"}. 
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   623
  Using @{ML_ind  "#->"}, for example, the function @{text double} can also be written as:
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   624
*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   625
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   626
ML{*val double =
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   627
            (fn x => (x, x))
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   628
        #-> (fn x => fn y => x + y)*}
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   629
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   630
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   631
text {* 
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   632
  When using combinators for writing functions in waterfall fashion, it is
311
ee864694315b polished
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   633
  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
   634
  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
   635
  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
   636
  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
   637
  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
   638
  but one calculates only with a single entity. An example is the function 
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   639
  @{ML_ind  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
   640
  of terms.
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   641
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   642
  @{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
   643
  "let
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   644
  val ctxt = @{context}
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   645
in
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   646
  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
   647
  |> Syntax.check_terms ctxt
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   648
  |> string_of_terms ctxt
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   649
  |> tracing
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   650
end"
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   651
  "m + n, m - n"}
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
126
fcc0e6e54dca polished
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   654
text {*
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   655
  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
   656
  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
   657
  adapted. This can be done with the ``plumbing'' function @{ML
314
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   658
  singleton}.\footnote{There is already a function @{ML check_term in Syntax}
79202e2eab6a polished
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   659
  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
   660
  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
   661
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   662
  @{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
   663
  "let 
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   664
  val ctxt = @{context}
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   665
in
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   666
  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
   667
  |> 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
   668
  |> string_of_term ctxt
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   669
  |> tracing
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   670
end"
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   671
  "m - n"}
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   672
   
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   673
  \begin{readmore}
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   674
  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
   675
  "Pure/library.ML"}
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   676
  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
   677
  contains further information about combinators.
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
   678
  \end{readmore}
310
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   679
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   680
  (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
   681
007922777ff1 added some rudimentary inrastructure for producing the ML-code
Christian Urban <urbanc@in.tum.de>
parents: 309
diff changeset
   682
  (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
   683
*}
9da9ba2b095b added a solution section and some other minor additions
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   684
10
df09e49b19bf many changes in the FirstSteps section
Christian Urban <urbanc@in.tum.de>
parents: 6
diff changeset
   685
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   686
section {* ML-Antiquotations *}
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   687
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   688
text {*
49
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   689
  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
   690
  contain references to entities defined on the logical level of Isabelle. By
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   691
  this we mean definitions, theorems, terms and so on. This kind of reference
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   692
  is realised with ML-antiquotations, often also referred to as just
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   693
  antiquotations.\footnote{There are two kinds of antiquotations in Isabelle,
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   694
  which have very different purpose and infrastructures. The first kind,
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   695
  described in this section, are \emph{ML-antiquotations}. They are used to
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   696
  refer to entities (like terms, types etc) from Isabelle's logic layer inside
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   697
  ML-code. The other kind of antiquotations are \emph{document} antiquotations. They
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   698
  are used only in the text parts of Isabelle and their purpose is to print
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   699
  logical entities inside \LaTeX-documents. They are part of the user level
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   700
  and therefore we are not interested in them in this Tutorial, except in
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   701
  Appendix \ref{rec:docantiquotations} where we show how to implement your own
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   702
  document antiquotations.}
298
8057d65607eb some general polishing
Christian Urban <urbanc@in.tum.de>
parents: 293
diff changeset
   703
  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
   704
  theory by typing
a0edabf14457 added more material
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
   705
39
631d12c25bde substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   706
  
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   707
  @{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
   708
 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   709
  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
   710
  current theory (remember that we assumed we are inside the theory 
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   711
  @{text FirstSteps}). The name of this theory can be extracted using
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   712
  the function @{ML_ind  theory_name in Context}. 
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   713
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   714
  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
   715
  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
   716
*}
5
e91f54791e14 minor modifiations to the Intro and FirstSteps chapters
Christian Urban <urbanc@in.tum.de>
parents: 2
diff changeset
   717
69
19106a9975c1 highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   718
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
   719
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
   720
text {*
2
978a3c2ed7ce split the document into smaller pieces;
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   721
89
fee4942c4770 polishing
Christian Urban <urbanc@in.tum.de>
parents: 87
diff changeset
   722
  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
   723
  different theory. Instead, the code above defines the constant function 
58
f3794c231898 fixed typos
Christian Urban <urbanc@in.tum.de>
parents: 54
diff changeset
   724
  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
   725
  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
   726
  \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
   727
  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
   728
  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
   729
132
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   730
  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
   731
  @{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
   732
72
7b8c4fe235aa added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   733
  @{ML_response_fake [display,gray] "@{thm allI}" "(\<And>x. ?P x) \<Longrightarrow> \<forall>x. ?P x"}
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   734
133
3e94ccc0f31e polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents: 132
diff changeset
   735
  and @{text "@{thms \<dots>}"} for more than one
132
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   736
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   737
@{ML_response_fake [display,gray] "@{thms conj_ac}" 
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   738
"(?P \<and> ?Q) = (?Q \<and> ?P)
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   739
(?P \<and> ?Q \<and> ?R) = (?Q \<and> ?P \<and> ?R)
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   740
((?P \<and> ?Q) \<and> ?R) = (?P \<and> ?Q \<and> ?R)"}  
2d9198bcb850 polished
Christian Urban <urbanc@in.tum.de>
parents: 131
diff changeset
   741
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   742
  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
   743
  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
   744
  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
   745
  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
   746
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   747
  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
   748
  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
   749
  simpset.
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   750
*}
75
Christian Urban <urbanc@in.tum.de>
parents: 73
diff changeset
   751
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   752
ML{*fun get_thm_names_from_ss simpset =
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   753
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
   754
  val {simps,...} = MetaSimplifier.dest_ss simpset
70
bbb2d5f1d58d deleted the fixme about simpsets
Christian Urban <urbanc@in.tum.de>
parents: 69
diff changeset
   755
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
   756
  map #1 simps
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   757
end*}
54
1783211b3494 tuned; added document antiquotation ML_response_fake_both
Christian Urban <urbanc@in.tum.de>
parents: 52
diff changeset
   758
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   759
text {*
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   760
  The function @{ML_ind  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
   761
  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
   762
  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
   763
  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
   764
131
8db9195bb3e9 polished
Christian Urban <urbanc@in.tum.de>
parents: 128
diff changeset
   765
  @{ML_response_fake [display,gray] 
149
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   766
  "get_thm_names_from_ss @{simpset}" 
253ea99c1441 polished
Christian Urban <urbanc@in.tum.de>
parents: 138
diff changeset
   767
  "[\"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
   768
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   769
  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
   770
  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
   771
  code.
156
e8f11280c762 polished
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   772
251
cccb25ee1309 a few additions
Christian Urban <urbanc@in.tum.de>
parents: 250
diff changeset
   773
  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
   774
  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
   775
  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
   776
  antiquotation @{text "@{binding \<dots>}"}. For example
192
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   777
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   778
  @{ML_response [display,gray]
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   779
  "@{binding \"name\"}"
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   780
  "name"}
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   781
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   782
  An example where a qualified name is needed is the function 
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   783
  @{ML_ind  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
   784
  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
   785
*}
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   786
  
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   787
local_setup %gray {* 
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   788
  snd o LocalTheory.define Thm.internalK 
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   789
    ((@{binding "TrueConj"}, NoSyn), 
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   790
     (Attrib.empty_binding, @{term "True \<and> True"})) *}
2fff636e1fa0 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   791
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   792
text {* 
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   793
  Now querying the definition you obtain:
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   794
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   795
  \begin{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   796
  \isacommand{thm}~@{text "TrueConj_def"}\\
225
7859fc59249a section for further material about simple inductive
Christian Urban <urbanc@in.tum.de>
parents: 215
diff changeset
   797
  @{text "> "}~@{thm TrueConj_def}
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   798
  \end{isabelle}
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   799
194
8cd51a25a7ca some polishing
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   800
  (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
   801
  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
   802
  why @{ML snd} is needed)
193
ffd93dcc269d polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents: 192
diff changeset
   803
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   804
  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
   805
  exercise care when introducing new ones, as they can also make your code
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   806
  also difficult to read. In the next chapter we will see how the (build in)
315
de49d5780f57 simplified a bit the index generation
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
   807
  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
   808
  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
   809
  schematic variables. If you want to have an antiquotation that does not have
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   810
  this restriction, you can implement your own using the function @{ML_ind
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   811
  ML_Antiquote.inline}. The code for the antiquotation @{text "term_pat"} is
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   812
  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
   813
*}
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
   814
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   815
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
   816
  (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
   817
     (fn (ctxt, s) =>
264
311830b43f8f minor tuning
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
   818
       s |> ProofContext.read_term_pattern ctxt
311830b43f8f minor tuning
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
   819
         |> ML_Syntax.print_term
311830b43f8f minor tuning
Christian Urban <urbanc@in.tum.de>
parents: 263
diff changeset
   820
         |> 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
   821
195c4444dff7 added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 262
diff changeset
   822
text {*
308
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   823
  The parser in Line 2 provides us with a context and a string; this string is
316
74f0a06f751f further polishing of index generation
Christian Urban <urbanc@in.tum.de>
parents: 315
diff changeset
   824
  transformed into a term using the function @{ML_ind  read_term_pattern in
308
c90f4ec30d43 improvements from the workshop
Christian Urban <urbanc@in.tum.de>
parents: 307
diff changeset
   825
  ProofContext} (Line 4); the next two lines print the term so that the
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   826
  ML-system can understand them. The function @{ML atomic in ML_Syntax}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   827
  just encloses the term in parentheses. An example for the usage of this 
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   828
  antiquotation is:
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   829
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   830
  @{ML_response_fake [display,gray]
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   831
  "@{term_pat \"Suc (?x::nat)\"}"
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   832
  "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
   833
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   834
  which shows the internal representation of the term @{text "Suc ?x"}.
298
8057d65607eb some general polishing
Christian Urban <urbanc@in.tum.de>
parents: 293
diff changeset
   835
263
195c4444dff7 added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 262
diff changeset
   836
  \begin{readmore}
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   837
  The file @{ML_file "Pure/ML/ml_antiquote.ML"} contains the the definitions
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   838
  for most antiquotations. Most of the basic operations on ML-syntax are implemented 
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   839
  in @{ML_file "Pure/ML/ml_syntax.ML"}.
263
195c4444dff7 added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 262
diff changeset
   840
  \end{readmore}
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   841
*}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   842
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   843
section {* Storing Data (TBD) *}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   844
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   845
text {*
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   846
  Isabelle provides a mechanism to store (and retrieve) arbitrary data. Before we 
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   847
  explain this mechanism. 
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   848
*}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   849
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   850
ML{*signature UNIVERSAL_TYPE =
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   851
sig
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   852
  type t
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   853
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   854
  val embed: unit -> ('a -> t) * (t -> 'a option)
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   855
end*}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   856
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   857
ML{*structure U:> UNIVERSAL_TYPE =
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   858
   struct
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   859
      type t = exn
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   860
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   861
      fun 'a embed () =
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   862
         let
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   863
            exception E of 'a
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   864
            fun project (e: t): 'a option =
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   865
               case e of
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   866
                  E a => SOME a
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   867
                | _ => NONE
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   868
         in
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   869
            (E, project)
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   870
         end
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   871
   end*}
292
41a802bbb7df added more to the ML-antiquotation section
Christian Urban <urbanc@in.tum.de>
parents: 290
diff changeset
   872
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   873
text {*
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   874
  The idea is that type t is the universal type and that each call to embed
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   875
  returns a new pair of functions (inject, project), where inject embeds a
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   876
  value into the universal type and project extracts the value from the
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   877
  universal type. A pair (inject, project) returned by embed works together in
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   878
  that project u will return SOME v if and only if u was created by inject
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   879
  v. If u was created by a different function inject', then project returns
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   880
  NONE.
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   881
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   882
  in library.ML
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   883
*}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   884
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   885
ML_val{*structure Object = struct type T = exn end; *}
298
8057d65607eb some general polishing
Christian Urban <urbanc@in.tum.de>
parents: 293
diff changeset
   886
323
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   887
ML{*functor Test (U: UNIVERSAL_TYPE): sig end =
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   888
   struct
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   889
      val (intIn: int -> U.t, intOut) = U.embed ()
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   890
      val r: U.t ref = ref (intIn 13)
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   891
      val s1 =
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   892
         case intOut (!r) of
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   893
            NONE => "NONE"
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   894
          | SOME i => Int.toString i
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   895
      val (realIn: real -> U.t, realOut) = U.embed ()
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   896
      val _ = r := realIn 13.0
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   897
      val s2 =
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   898
         case intOut (!r) of
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   899
            NONE => "NONE"
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   900
          | SOME i => Int.toString i
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   901
      val s3 =
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   902
         case realOut (!r) of
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   903
            NONE => "NONE"
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   904
          | SOME x => Real.toString x
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   905
      val _ = tracing (implode [s1, " ", s2, " ", s3, "\n"])
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   906
   end*}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   907
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   908
ML_val{*structure t = Test(U) *} 
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   909
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   910
ML_val{*structure Datatab = Table(type key = int val ord = int_ord);*}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   911
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   912
ML {* LocalTheory.restore *}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   913
ML {* LocalTheory.set_group *}
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   914
92f6a772e013 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 322
diff changeset
   915
263
195c4444dff7 added section about code maintenance and added an example for antiquotations
Christian Urban <urbanc@in.tum.de>
parents: 262
diff changeset
   916
322
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   917
(*
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   918
section {* Do Not Try This At Home! *}
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   919
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   920
ML {* val my_thms = ref ([]: thm list) *}
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   921
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   922
attribute_setup my_thm_bad =
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   923
  {* Scan.succeed (Thm.declaration_attribute (fn th => fn ctxt =>
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   924
      (my_thms := th :: ! my_thms; ctxt))) *} "bad attribute"
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   925
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   926
declare sym [my_thm_bad]
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   927
declare refl [my_thm_bad]
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   928
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   929
ML "!my_thms"
2b7c08d90e2e included some tests
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   930
*)
196
840b49bfb1cf fixed `str_of_thms' output in example + small changes
griff
parents: 192
diff changeset
   931
end