| author | Christian Urban <urbanc@in.tum.de> | 
| Mon, 09 Feb 2009 04:18:14 +0000 | |
| changeset 107 | 258ce361ba1b | 
| parent 104 | 5dcad9348e4d | 
| child 108 | 8bea3f74889d | 
| permissions | -rw-r--r-- | 
| 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: 
21diff
changeset | 2 | imports Base | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 3 | begin | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 4 | |
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 5 | chapter {* First Steps *}
 | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 6 | |
| 42 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 7 | text {*
 | 
| 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 8 | |
| 54 
1783211b3494
tuned; added document antiquotation ML_response_fake_both
 Christian Urban <urbanc@in.tum.de> parents: 
52diff
changeset | 9 | Isabelle programming is done in ML. Just like lemmas and proofs, ML-code | 
| 89 | 10 | in Isabelle is part of a theory. If you want to follow the code given in | 
| 54 
1783211b3494
tuned; added document antiquotation ML_response_fake_both
 Christian Urban <urbanc@in.tum.de> parents: 
52diff
changeset | 11 | 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 | 12 | |
| 6 | 13 |   \begin{center}
 | 
| 5 
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
 Christian Urban <urbanc@in.tum.de> parents: 
2diff
changeset | 14 |   \begin{tabular}{@ {}l}
 | 
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 15 |   \isacommand{theory} FirstSteps\\
 | 
| 5 
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
 Christian Urban <urbanc@in.tum.de> parents: 
2diff
changeset | 16 |   \isacommand{imports} Main\\
 | 
| 
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
 Christian Urban <urbanc@in.tum.de> parents: 
2diff
changeset | 17 |   \isacommand{begin}\\
 | 
| 6 | 18 | \ldots | 
| 5 
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
 Christian Urban <urbanc@in.tum.de> parents: 
2diff
changeset | 19 |   \end{tabular}
 | 
| 6 | 20 |   \end{center}
 | 
| 14 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 21 | *} | 
| 5 
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
 Christian Urban <urbanc@in.tum.de> parents: 
2diff
changeset | 22 | |
| 20 | 23 | section {* Including ML-Code *}
 | 
| 14 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 24 | |
| 47 
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
 Christian Urban <urbanc@in.tum.de> parents: 
43diff
changeset | 25 | |
| 
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
 Christian Urban <urbanc@in.tum.de> parents: 
43diff
changeset | 26 | |
| 14 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 27 | text {*
 | 
| 5 
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
 Christian Urban <urbanc@in.tum.de> parents: 
2diff
changeset | 28 | The easiest and quickest way to include code in a theory is | 
| 89 | 29 |   by using the \isacommand{ML}-command. For example:
 | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 30 | |
| 75 | 31 | \begin{isabelle}
 | 
| 32 | \begin{graybox}
 | |
| 85 
b02904872d6b
better handling of {* and *}
 Christian Urban <urbanc@in.tum.de> parents: 
84diff
changeset | 33 | \isacommand{ML}~@{text "\<verbopen>"}\isanewline
 | 
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 34 | \hspace{5mm}@{ML "3 + 4"}\isanewline
 | 
| 85 
b02904872d6b
better handling of {* and *}
 Christian Urban <urbanc@in.tum.de> parents: 
84diff
changeset | 35 | @{text "\<verbclose>"}\isanewline
 | 
| 
b02904872d6b
better handling of {* and *}
 Christian Urban <urbanc@in.tum.de> parents: 
84diff
changeset | 36 | @{text "> 7"}\smallskip
 | 
| 75 | 37 | \end{graybox}
 | 
| 38 | \end{isabelle}
 | |
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 39 | |
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 40 |   Like normal Isabelle proof scripts, \isacommand{ML}-commands can be
 | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 41 | evaluated by using the advance and undo buttons of your Isabelle | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 42 |   environment. The code inside the \isacommand{ML}-command can also contain
 | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 43 | value and function bindings, and even those can be undone when the proof | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 44 | script is retracted. As mentioned earlier, we will drop the | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 45 |   \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"} scaffolding whenever we
 | 
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 46 |   show code. The lines prefixed with @{text [quotes] ">"} are not part of the
 | 
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 47 | code, rather they indicate what the response is when the code is evaluated. | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 48 | |
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 49 | Once a portion of code is relatively stable, you usually want to export it | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 50 | to a separate ML-file. Such files can then be included in a theory by using | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 51 |   the \isacommand{uses}-command in the header of the theory, like:
 | 
| 14 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 52 | |
| 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 53 |   \begin{center}
 | 
| 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 54 |   \begin{tabular}{@ {}l}
 | 
| 54 
1783211b3494
tuned; added document antiquotation ML_response_fake_both
 Christian Urban <urbanc@in.tum.de> parents: 
52diff
changeset | 55 |   \isacommand{theory} FirstSteps\\
 | 
| 14 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 56 |   \isacommand{imports} Main\\
 | 
| 58 | 57 |   \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: 
13diff
changeset | 58 |   \isacommand{begin}\\
 | 
| 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 59 | \ldots | 
| 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 60 |   \end{tabular}
 | 
| 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 61 |   \end{center}
 | 
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 62 | |
| 14 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 63 | *} | 
| 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 64 | |
| 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 65 | section {* Debugging and Printing *}
 | 
| 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 66 | |
| 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 67 | text {*
 | 
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 68 | |
| 50 | 69 | During development you might find it necessary to inspect some data | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 70 | in your code. This can be done in a ``quick-and-dirty'' fashion using | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 71 |   the function @{ML "warning"}. For example 
 | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 72 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 73 |   @{ML_response_fake [display,gray] "warning \"any string\"" "\"any string\""}
 | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 74 | |
| 58 | 75 |   will print out @{text [quotes] "any string"} inside the response buffer
 | 
| 76 | of Isabelle. This function expects a string as argument. If you develop under PolyML, | |
| 50 | 77 | then there is a convenient, though again ``quick-and-dirty'', method for | 
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 78 |   converting values into strings, namely the function @{ML makestring}:
 | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 79 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 80 |   @{ML_response_fake [display,gray] "warning (makestring 1)" "\"1\""} 
 | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 81 | |
| 65 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 82 |   However @{ML makestring} only works if the type of what is converted is monomorphic 
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 83 | and not a function. | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 84 | |
| 52 | 85 |   The function @{ML "warning"} should only be used for testing purposes, because any
 | 
| 86 | output this function generates will be overwritten as soon as an error is | |
| 50 | 87 | raised. For printing anything more serious and elaborate, the | 
| 54 
1783211b3494
tuned; added document antiquotation ML_response_fake_both
 Christian Urban <urbanc@in.tum.de> parents: 
52diff
changeset | 88 |   function @{ML tracing} is more appropriate. This function writes all output into
 | 
| 89 | 89 | 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: 
13diff
changeset | 90 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 91 |   @{ML_response_fake [display,gray] "tracing \"foo\"" "\"foo\""}
 | 
| 14 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 92 | |
| 58 | 93 |   It is also possible to redirect the ``channel'' where the string @{text "foo"} is 
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 94 | printed to a separate file, e.g.~to prevent ProofGeneral from choking on massive | 
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 95 | amounts of trace output. This redirection can be achieved with the code: | 
| 43 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 96 | *} | 
| 14 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 97 | |
| 69 
19106a9975c1
highligted the background of ML-code
 Christian Urban <urbanc@in.tum.de> parents: 
68diff
changeset | 98 | ML{*val strip_specials =
 | 
| 42 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 99 | let | 
| 43 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 100 |   fun strip ("\^A" :: _ :: cs) = strip cs
 | 
| 42 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 101 | | strip (c :: cs) = c :: strip cs | 
| 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 102 | | strip [] = []; | 
| 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 103 | in implode o strip o explode end; | 
| 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 104 | |
| 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 105 | fun redirect_tracing stream = | 
| 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 106 | 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: 
13diff
changeset | 107 | (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: 
42diff
changeset | 108 | TextIO.output (stream, "\n"); | 
| 69 
19106a9975c1
highligted the background of ML-code
 Christian Urban <urbanc@in.tum.de> parents: 
68diff
changeset | 109 | TextIO.flushOut stream)) *} | 
| 14 
1c17e99f6f66
added a paragraph about "uses" and started a paragraph about tracing
 Christian Urban <urbanc@in.tum.de> parents: 
13diff
changeset | 110 | |
| 43 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 111 | text {*
 | 
| 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 112 |   Calling @{ML "redirect_tracing"} with @{ML "(TextIO.openOut \"foo.bar\")"} 
 | 
| 58 | 113 |   will cause that all tracing information is printed into the file @{text "foo.bar"}.
 | 
| 75 | 114 | |
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 115 |   You can print out error messages with the function @{ML error}; for example:
 | 
| 75 | 116 | |
| 117 |   @{ML_response_fake [display,gray] "if 0=1 then 1 else (error \"foo\")" "\"foo\""}
 | |
| 118 | ||
| 104 | 119 |   Section~\ref{sec:printing} will give more information about printing 
 | 
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 120 |   the main data structures of Isabelle, namely @{ML_type term}, @{ML_type cterm} 
 | 
| 104 | 121 |   and @{ML_type thm}.
 | 
| 15 
9da9ba2b095b
added a solution section and some other minor additions
 Christian Urban <urbanc@in.tum.de> parents: 
14diff
changeset | 122 | *} | 
| 
9da9ba2b095b
added a solution section and some other minor additions
 Christian Urban <urbanc@in.tum.de> parents: 
14diff
changeset | 123 | |
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 124 | |
| 75 | 125 | |
| 126 | ||
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 127 | section {* Antiquotations *}
 | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 128 | |
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 129 | text {*
 | 
| 49 | 130 | The main advantage of embedding all code in a theory is that the code can | 
| 58 | 131 | contain references to entities defined on the logical level of Isabelle. By | 
| 132 | this we mean definitions, theorems, terms and so on. This kind of reference is | |
| 133 | realised with antiquotations. For example, one can print out the name of the current | |
| 49 | 134 | theory by typing | 
| 135 | ||
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 136 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 137 |   @{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: 
34diff
changeset | 138 | |
| 5 
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
 Christian Urban <urbanc@in.tum.de> parents: 
2diff
changeset | 139 |   where @{text "@{theory}"} is an antiquotation that is substituted with the
 | 
| 49 | 140 | current theory (remember that we assumed we are inside the theory | 
| 89 | 141 |   @{text FirstSteps}). The name of this theory can be extracted using
 | 
| 49 | 142 |   the function @{ML "Context.theory_name"}. 
 | 
| 5 
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
 Christian Urban <urbanc@in.tum.de> parents: 
2diff
changeset | 143 | |
| 89 | 144 | 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: 
11diff
changeset | 145 | 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: 
42diff
changeset | 146 | *} | 
| 5 
e91f54791e14
minor modifiations to the Intro and FirstSteps chapters
 Christian Urban <urbanc@in.tum.de> parents: 
2diff
changeset | 147 | |
| 69 
19106a9975c1
highligted the background of ML-code
 Christian Urban <urbanc@in.tum.de> parents: 
68diff
changeset | 148 | 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: 
42diff
changeset | 149 | |
| 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 150 | text {*
 | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 151 | |
| 89 | 152 |   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: 
2diff
changeset | 153 | different theory. Instead, the code above defines the constant function | 
| 58 | 154 |   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: 
42diff
changeset | 155 |   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: 
2diff
changeset | 156 |   \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: 
2diff
changeset | 157 | 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: 
2diff
changeset | 158 | 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 | 159 | |
| 81 | 160 | In a similar way you can use antiquotations to refer to proved theorems: | 
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 161 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 162 |   @{ML_response_fake [display,gray] "@{thm allI}" "(\<And>x. ?P x) \<Longrightarrow> \<forall>x. ?P x"}
 | 
| 75 | 163 | |
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 164 | and simpsets: | 
| 75 | 165 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 166 |   @{ML_response_fake [display,gray] 
 | 
| 70 
bbb2d5f1d58d
deleted the fixme about simpsets
 Christian Urban <urbanc@in.tum.de> parents: 
69diff
changeset | 167 | "let | 
| 89 | 168 |   val ({rules,...}, _) = MetaSimplifier.rep_ss @{simpset}
 | 
| 70 
bbb2d5f1d58d
deleted the fixme about simpsets
 Christian Urban <urbanc@in.tum.de> parents: 
69diff
changeset | 169 | in | 
| 
bbb2d5f1d58d
deleted the fixme about simpsets
 Christian Urban <urbanc@in.tum.de> parents: 
69diff
changeset | 170 | map #name (Net.entries rules) | 
| 
bbb2d5f1d58d
deleted the fixme about simpsets
 Christian Urban <urbanc@in.tum.de> parents: 
69diff
changeset | 171 | end" "[\"Nat.of_nat_eq_id\", \"Int.of_int_eq_id\", \"Nat.One_nat_def\", \<dots>]"} | 
| 54 
1783211b3494
tuned; added document antiquotation ML_response_fake_both
 Christian Urban <urbanc@in.tum.de> parents: 
52diff
changeset | 172 | |
| 89 | 173 | The code about simpsets extracts the theorem names stored in the | 
| 81 | 174 | current simpset. We get hold of the current simpset with the antiquotation | 
| 175 |   @{text "@{simpset}"}.  The function @{ML rep_ss in MetaSimplifier} returns a record
 | |
| 176 | containing all information about the simpset. The rules of a simpset are | |
| 104 | 177 |   stored in a \emph{discrimination net} (a data structure for fast
 | 
| 81 | 178 |   indexing). From this net we can extract the entries using the function @{ML
 | 
| 179 | Net.entries}. | |
| 180 | ||
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 181 | |
| 75 | 182 |   \begin{readmore}
 | 
| 81 | 183 |   The infrastructure for simpsets is implemented in @{ML_file "Pure/meta_simplifier.ML"}
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 184 |   and @{ML_file "Pure/simplifier.ML"}. Discrimination nets are implemented
 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 185 |   in @{ML_file "Pure/net.ML"}.
 | 
| 75 | 186 |   \end{readmore}
 | 
| 187 | ||
| 188 | While antiquotations have many applications, they were originally introduced in order | |
| 89 | 189 | to avoid explicit bindings for theorems such as: | 
| 43 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 190 | *} | 
| 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 191 | |
| 69 
19106a9975c1
highligted the background of ML-code
 Christian Urban <urbanc@in.tum.de> parents: 
68diff
changeset | 192 | ML{*val allI = thm "allI" *}
 | 
| 43 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 193 | |
| 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 194 | text {*
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 195 | These bindings are difficult to maintain and also can be accidentally | 
| 89 | 196 | overwritten by the user. This often breakes Isabelle | 
| 49 | 197 | packages. Antiquotations solve this problem, since they are ``linked'' | 
| 89 | 198 | statically at compile-time. However, this static linkage also limits their | 
| 199 | usefulness in cases where data needs to be build up dynamically. In the | |
| 200 | course of this introduction, we will learn more about these antiquotations: | |
| 201 | they greatly simplify Isabelle programming since one can directly access all | |
| 202 | kinds of logical elements from th ML-level. | |
| 49 | 203 | |
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 204 | *} | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 205 | |
| 15 
9da9ba2b095b
added a solution section and some other minor additions
 Christian Urban <urbanc@in.tum.de> parents: 
14diff
changeset | 206 | section {* Terms and Types *}
 | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 207 | |
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 208 | text {*
 | 
| 49 | 209 | One way to construct terms of Isabelle on the ML-level is by using the antiquotation | 
| 89 | 210 |   \mbox{@{text "@{term \<dots>}"}}. For example:
 | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 211 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 212 |   @{ML_response [display,gray] 
 | 
| 75 | 213 | "@{term \"(a::nat) + b = c\"}" 
 | 
| 214 | "Const (\"op =\", \<dots>) $ | |
| 215 | (Const (\"HOL.plus_class.plus\", \<dots>) $ \<dots> $ \<dots>) $ \<dots>"} | |
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 216 | |
| 50 | 217 |   This will show the term @{term "(a::nat) + b = c"}, but printed using the internal
 | 
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 218 | representation of this term. This internal representation corresponds to the | 
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 219 |   datatype @{ML_type "term"}.
 | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 220 | |
| 34 | 221 | The internal representation of terms uses the usual de Bruijn index mechanism where bound | 
| 11 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 222 |   variables are represented by the constructor @{ML Bound}. The index in @{ML Bound} refers to
 | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 223 |   the number of Abstractions (@{ML Abs}) we have to skip until we hit the @{ML Abs} that
 | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 224 | binds the corresponding variable. However, in Isabelle the names of bound variables are | 
| 11 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 225 | kept at abstractions for printing purposes, and so should be treated only as comments. | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 226 | |
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 227 |   \begin{readmore}
 | 
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 228 |   Terms are described in detail in \isccite{sec:terms}. Their
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 229 |   definition and many useful operations are implemented in @{ML_file "Pure/term.ML"}.
 | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 230 |   \end{readmore}
 | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 231 | |
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 232 | Sometimes the internal representation of terms can be surprisingly different | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 233 | from what you see at the user level, because the layers of | 
| 47 
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
 Christian Urban <urbanc@in.tum.de> parents: 
43diff
changeset | 234 | parsing/type-checking/pretty printing can be quite elaborate. | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 235 | |
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 236 |   \begin{exercise}
 | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 237 | Look at the internal term representation of the following terms, and | 
| 89 | 238 | find out why they are represented like this: | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 239 | |
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 240 |   \begin{itemize}
 | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 241 |   \item @{term "case x of 0 \<Rightarrow> 0 | Suc y \<Rightarrow> y"}  
 | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 242 |   \item @{term "\<lambda>(x,y). P y x"}  
 | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 243 |   \item @{term "{ [x::int] | x. x \<le> -2 }"}  
 | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 244 |   \end{itemize}
 | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 245 | |
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 246 | Hint: The third term is already quite big, and the pretty printer | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 247 | may omit parts of it by default. If you want to see all of it, you | 
| 104 | 248 | can use the following ML-function to set the limit to a value high | 
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 249 | enough: | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 250 | |
| 75 | 251 |   @{ML [display,gray] "print_depth 50"}
 | 
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 252 |   \end{exercise}
 | 
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 253 | |
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 254 |   The antiquotation @{text "@{prop \<dots>}"} constructs terms of propositional type, 
 | 
| 50 | 255 |   inserting the invisible @{text "Trueprop"}-coercions whenever necessary. 
 | 
| 68 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 256 | Consider for example the pairs | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 257 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 258 |   @{ML_response [display,gray] "(@{term \"P x\"}, @{prop \"P x\"})" "(Free (\"P\", \<dots>) $ Free (\"x\", \<dots>),
 | 
| 65 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 259 | Const (\"Trueprop\", \<dots>) $ (Free (\"P\", \<dots>) $ Free (\"x\", \<dots>)))"} | 
| 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 260 | |
| 68 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 261 | where an coercion is inserted in the second component and | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 262 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 263 |   @{ML_response [display,gray] "(@{term \"P x \<Longrightarrow> Q x\"}, @{prop \"P x \<Longrightarrow> Q x\"})" 
 | 
| 65 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 264 | "(Const (\"==>\", \<dots>) $ \<dots> $ \<dots>, Const (\"==>\", \<dots>) $ \<dots> $ \<dots>)"} | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 265 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 266 | where it is not (since it is already constructed by a meta-implication). | 
| 19 
34b93dbf8c3c
some tuning in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
15diff
changeset | 267 | |
| 89 | 268 |   Types can be constructed using the antiquotation @{text "@{typ \<dots>}"}. For example:
 | 
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 269 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 270 |   @{ML_response_fake [display,gray] "@{typ \"bool \<Rightarrow> nat\"}" "bool \<Rightarrow> nat"}
 | 
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 271 | |
| 19 
34b93dbf8c3c
some tuning in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
15diff
changeset | 272 |   \begin{readmore}
 | 
| 
34b93dbf8c3c
some tuning in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
15diff
changeset | 273 |   Types are described in detail in \isccite{sec:types}. Their
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 274 | definition and many useful operations are implemented | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 275 |   in @{ML_file "Pure/type.ML"}.
 | 
| 19 
34b93dbf8c3c
some tuning in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
15diff
changeset | 276 |   \end{readmore}
 | 
| 47 
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
 Christian Urban <urbanc@in.tum.de> parents: 
43diff
changeset | 277 | *} | 
| 19 
34b93dbf8c3c
some tuning in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
15diff
changeset | 278 | |
| 
34b93dbf8c3c
some tuning in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
15diff
changeset | 279 | |
| 15 
9da9ba2b095b
added a solution section and some other minor additions
 Christian Urban <urbanc@in.tum.de> parents: 
14diff
changeset | 280 | section {* Constructing Terms and Types Manually *} 
 | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 281 | |
| 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 282 | text {*
 | 
| 81 | 283 | While antiquotations are very convenient for constructing terms, they can | 
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 284 | only construct fixed terms (remember they are ``linked'' at compile-time). | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 285 | However, you often need to construct terms dynamically. For example, a | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 286 |   function that returns the implication @{text "\<And>(x::\<tau>). P x \<Longrightarrow> Q x"} taking
 | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 287 |   @{term P}, @{term Q} and the type @{term "\<tau>"} as arguments can only be
 | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 288 | written as: | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 289 | |
| 43 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 290 | *} | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 291 | |
| 69 
19106a9975c1
highligted the background of ML-code
 Christian Urban <urbanc@in.tum.de> parents: 
68diff
changeset | 292 | ML{*fun make_imp P Q tau =
 | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 293 | let | 
| 43 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 294 |     val x = Free ("x",tau)
 | 
| 75 | 295 | in | 
| 296 | Logic.all x (Logic.mk_implies (P $ x, Q $ x)) | |
| 69 
19106a9975c1
highligted the background of ML-code
 Christian Urban <urbanc@in.tum.de> parents: 
68diff
changeset | 297 | end *} | 
| 43 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 298 | |
| 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 299 | text {*
 | 
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 300 |   The reason is that you cannot pass the arguments @{term P}, @{term Q} and 
 | 
| 104 | 301 |   @{term "tau"} into an antiquotation. For example the following does \emph{not} work.
 | 
| 43 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 302 | *} | 
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 303 | |
| 69 
19106a9975c1
highligted the background of ML-code
 Christian Urban <urbanc@in.tum.de> parents: 
68diff
changeset | 304 | ML{*fun make_wrong_imp P Q tau = @{prop "\<And>x. P x \<Longrightarrow> Q x"} *}
 | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 305 | |
| 43 
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
 Christian Urban <urbanc@in.tum.de> parents: 
42diff
changeset | 306 | text {*
 | 
| 19 
34b93dbf8c3c
some tuning in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
15diff
changeset | 307 |   To see this apply @{text "@{term S}"}, @{text "@{term T}"} and @{text "@{typ nat}"} 
 | 
| 75 | 308 |   to both functions. With @{ML make_imp} we obtain the intended term involving 
 | 
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 309 | the given arguments | 
| 65 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 310 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 311 |   @{ML_response [display,gray] "make_imp @{term S} @{term T} @{typ nat}" 
 | 
| 65 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 312 | "Const \<dots> $ | 
| 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 313 | Abs (\"x\", Type (\"nat\",[]), | 
| 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 314 | Const \<dots> $ (Free (\"S\",\<dots>) $ \<dots>) $ | 
| 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 315 | (Free (\"T\",\<dots>) $ \<dots>))"} | 
| 68 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 316 | |
| 81 | 317 |   whereas with @{ML make_wrong_imp} we obtain a term involving the @{term "P"} 
 | 
| 68 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 318 |   and @{text "Q"} from the antiquotation.
 | 
| 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 319 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 320 |   @{ML_response [display,gray] "make_wrong_imp @{term S} @{term T} @{typ nat}" 
 | 
| 65 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 321 | "Const \<dots> $ | 
| 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 322 | Abs (\"x\", \<dots>, | 
| 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 323 | Const \<dots> $ (Const \<dots> $ (Free (\"P\",\<dots>) $ \<dots>)) $ | 
| 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 324 | (Const \<dots> $ (Free (\"Q\",\<dots>) $ \<dots>)))"} | 
| 
c8e9a4f97916
tuned and added a section about creating keyword files
 Christian Urban <urbanc@in.tum.de> parents: 
60diff
changeset | 325 | |
| 81 | 326 | (FIXME: expand the following point) | 
| 327 | ||
| 68 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 328 | One tricky point in constructing terms by hand is to obtain the fully | 
| 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 329 |   qualified name for constants. For example the names for @{text "zero"} and
 | 
| 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 330 |   @{text "+"} are more complex than one first expects, namely
 | 
| 19 
34b93dbf8c3c
some tuning in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
15diff
changeset | 331 | |
| 15 
9da9ba2b095b
added a solution section and some other minor additions
 Christian Urban <urbanc@in.tum.de> parents: 
14diff
changeset | 332 | |
| 
9da9ba2b095b
added a solution section and some other minor additions
 Christian Urban <urbanc@in.tum.de> parents: 
14diff
changeset | 333 |   \begin{center}
 | 
| 58 | 334 |   @{text "HOL.zero_class.zero"} and @{text "HOL.plus_class.plus"}. 
 | 
| 15 
9da9ba2b095b
added a solution section and some other minor additions
 Christian Urban <urbanc@in.tum.de> parents: 
14diff
changeset | 335 |   \end{center}
 | 
| 
9da9ba2b095b
added a solution section and some other minor additions
 Christian Urban <urbanc@in.tum.de> parents: 
14diff
changeset | 336 | |
| 68 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 337 |   The extra prefixes @{text zero_class} and @{text plus_class} are present
 | 
| 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 338 |   because these constants are defined within type classes; the prefix @{text
 | 
| 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 339 | "HOL"} indicates in which theory they are defined. Guessing such internal | 
| 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 340 | names can sometimes be quite hard. Therefore Isabelle provides the | 
| 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 341 |   antiquotation @{text "@{const_name \<dots>}"} which does the expansion
 | 
| 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 342 | automatically, for example: | 
| 49 | 343 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 344 |   @{ML_response_fake [display,gray] "@{const_name \"Nil\"}" "List.list.Nil"}
 | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 345 | |
| 19 
34b93dbf8c3c
some tuning in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
15diff
changeset | 346 |   (FIXME: Is it useful to explain @{text "@{const_syntax}"}?)
 | 
| 11 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 347 | |
| 104 | 348 | Although to some extend types of terms can be inferred, there are many | 
| 349 | situations where you need to construct types manually, especially | |
| 350 | when defining constants. For example the function returning a function | |
| 351 | type is as follows: | |
| 49 | 352 | |
| 353 | *} | |
| 354 | ||
| 69 
19106a9975c1
highligted the background of ML-code
 Christian Urban <urbanc@in.tum.de> parents: 
68diff
changeset | 355 | ML{*fun make_fun_type tau1 tau2 = Type ("fun",[tau1,tau2]) *}
 | 
| 49 | 356 | |
| 104 | 357 | text {* This can be equally written as: *}
 | 
| 49 | 358 | |
| 69 
19106a9975c1
highligted the background of ML-code
 Christian Urban <urbanc@in.tum.de> parents: 
68diff
changeset | 359 | ML{*fun make_fun_type tau1 tau2 = tau1 --> tau2 *}
 | 
| 49 | 360 | |
| 361 | text {*
 | |
| 20 | 362 | |
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 363 |   \begin{readmore}
 | 
| 89 | 364 |   There are many functions in @{ML_file "Pure/term.ML"}, @{ML_file "Pure/logic.ML"} and
 | 
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 365 |   @{ML_file "HOL/Tools/hologic.ML"} that make such manual constructions of terms 
 | 
| 49 | 366 |   and types easier.\end{readmore}
 | 
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 367 | |
| 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 368 | Have a look at these files and try to solve the following two exercises: | 
| 11 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 369 | |
| 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 370 | *} | 
| 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 371 | |
| 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 372 | text {*
 | 
| 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 373 | |
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 374 |   \begin{exercise}\label{fun:revsum}
 | 
| 58 | 375 |   Write a function @{text "rev_sum : term -> term"} that takes a
 | 
| 11 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 376 |   term of the form @{text "t\<^isub>1 + t\<^isub>2 + \<dots> + t\<^isub>n"} (whereby @{text "i"} might be zero)
 | 
| 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 377 |   and returns the reversed sum @{text "t\<^isub>n + \<dots> + t\<^isub>2 + t\<^isub>1"}. Assume
 | 
| 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 378 |   the @{text "t\<^isub>i"} can be arbitrary expressions and also note that @{text "+"} 
 | 
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 379 | associates to the left. Try your function on some examples. | 
| 11 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 380 |   \end{exercise}
 | 
| 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 381 | |
| 15 
9da9ba2b095b
added a solution section and some other minor additions
 Christian Urban <urbanc@in.tum.de> parents: 
14diff
changeset | 382 |   \begin{exercise}\label{fun:makesum}
 | 
| 11 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 383 | Write a function which takes two terms representing natural numbers | 
| 75 | 384 |   in unary notation (like @{term "Suc (Suc (Suc 0))"}), and produce the
 | 
| 11 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 385 | number representing their sum. | 
| 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 386 |   \end{exercise}
 | 
| 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 387 | |
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 388 |   A handy function for manipulating terms is @{ML map_types}: it takes a 
 | 
| 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 389 | function and applies it to every type in the term. You can, for example, | 
| 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 390 |   change every @{typ nat} into an @{typ int} using the function
 | 
| 89 | 391 | *} | 
| 392 | ||
| 393 | ML{*fun nat_to_int t =
 | |
| 394 | (case t of | |
| 395 |      @{typ nat} => @{typ int}
 | |
| 396 | | Type (s, ts) => Type (s, map nat_to_int ts) | |
| 397 | | _ => t)*} | |
| 398 | ||
| 399 | text {*
 | |
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 400 | an then apply it as follows: | 
| 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 401 | |
| 89 | 402 | |
| 403 | @{ML_response_fake [display,gray] 
 | |
| 404 | "map_types nat_to_int @{term \"a = (1::nat)\"}" 
 | |
| 405 | "Const (\"op =\", \"int \<Rightarrow> int \<Rightarrow> bool\") | |
| 406 | $ Free (\"a\", \"int\") $ Const (\"HOL.one_class.one\", \"int\")"} | |
| 407 | ||
| 11 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 408 | *} | 
| 
733614e236a3
tuned and updated antquote_setup.ML
 Christian Urban <urbanc@in.tum.de> parents: 
10diff
changeset | 409 | |
| 49 | 410 | section {* Type-Checking *}
 | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 411 | |
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 412 | text {* 
 | 
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 413 | |
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 414 | You can freely construct and manipulate terms, since they are just | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 415 | arbitrary unchecked trees. However, you eventually want to see if a | 
| 54 
1783211b3494
tuned; added document antiquotation ML_response_fake_both
 Christian Urban <urbanc@in.tum.de> parents: 
52diff
changeset | 416 | term is well-formed, or type-checks, relative to a theory. | 
| 50 | 417 |   Type-checking is done via the function @{ML cterm_of}, which converts 
 | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 418 |   a @{ML_type term} into a  @{ML_type cterm}, a \emph{certified} term. 
 | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 419 |   Unlike @{ML_type term}s, which are just trees, @{ML_type
 | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 420 | "cterm"}s are abstract objects that are guaranteed to be | 
| 81 | 421 | type-correct, and they can only be constructed via ``official | 
| 50 | 422 | interfaces''. | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 423 | |
| 54 
1783211b3494
tuned; added document antiquotation ML_response_fake_both
 Christian Urban <urbanc@in.tum.de> parents: 
52diff
changeset | 424 | Type-checking is always relative to a theory context. For now we use | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 425 |   the @{ML "@{theory}"} antiquotation to get hold of the current theory.
 | 
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 426 | For example you can write: | 
| 47 
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
 Christian Urban <urbanc@in.tum.de> parents: 
43diff
changeset | 427 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 428 |   @{ML_response_fake [display,gray] "cterm_of @{theory} @{term \"a + b = c\"}" "a + b = c"}
 | 
| 47 
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
 Christian Urban <urbanc@in.tum.de> parents: 
43diff
changeset | 429 | |
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 430 | This can also be written with an antiquotation: | 
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 431 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 432 |   @{ML_response_fake [display,gray] "@{cterm \"(a::nat) + b = c\"}" "a + b = c"}
 | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 433 | |
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 434 | Attempting to obtain the certified term for | 
| 54 
1783211b3494
tuned; added document antiquotation ML_response_fake_both
 Christian Urban <urbanc@in.tum.de> parents: 
52diff
changeset | 435 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 436 |   @{ML_response_fake_both [display,gray] "@{cterm \"1 + True\"}" "Type unification failed \<dots>"}
 | 
| 54 
1783211b3494
tuned; added document antiquotation ML_response_fake_both
 Christian Urban <urbanc@in.tum.de> parents: 
52diff
changeset | 437 | |
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 438 | yields an error (since the term is not typable). A slightly more elaborate | 
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 439 | example that type-checks is: | 
| 20 | 440 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 441 | @{ML_response_fake [display,gray] 
 | 
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 442 | "let | 
| 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 443 |   val natT = @{typ \"nat\"}
 | 
| 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 444 |   val zero = @{term \"0::nat\"}
 | 
| 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 445 | in | 
| 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 446 |   cterm_of @{theory} 
 | 
| 75 | 447 |       (Const (@{const_name plus}, natT --> natT --> natT) $ zero $ zero)
 | 
| 41 
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
 Christian Urban <urbanc@in.tum.de> parents: 
40diff
changeset | 448 | end" "0 + 0"} | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 449 | |
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 450 |   \begin{exercise}
 | 
| 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 451 |   Check that the function defined in Exercise~\ref{fun:revsum} returns a
 | 
| 50 | 452 | result that type-checks. | 
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 453 |   \end{exercise}
 | 
| 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 454 | |
| 89 | 455 |   (FIXME: @{text "ctyp_of"}, @{ML fastype_of}, @{text dummyT})
 | 
| 86 | 456 | |
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 457 | *} | 
| 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 458 | |
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 459 | section {* Theorems *}
 | 
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 460 | |
| 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 461 | text {*
 | 
| 50 | 462 |   Just like @{ML_type cterm}s, theorems are abstract objects of type @{ML_type thm} 
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 463 | that can only be built by going through interfaces. As a consequence, every proof | 
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 464 | in Isabelle is correct by construction. | 
| 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 465 | |
| 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 466 | (FIXME reference LCF-philosophy) | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 467 | |
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 468 | To see theorems in ``action'', let us give a proof on the ML-level for the following | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 469 | statement: | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 470 | *} | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 471 | |
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 472 | lemma | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 473 | assumes assm\<^isub>1: "\<And>(x::nat). P x \<Longrightarrow> Q x" | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 474 | and assm\<^isub>2: "P t" | 
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 475 | shows "Q t" (*<*)oops(*>*) | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 476 | |
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 477 | text {*
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 478 |   The corresponding ML-code is as follows:\footnote{Note that @{text "|>"} is reverse
 | 
| 75 | 479 |   application. See Section~\ref{sec:combinators}.}
 | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 480 | |
| 72 
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
 Christian Urban <urbanc@in.tum.de> parents: 
71diff
changeset | 481 | @{ML_response_fake [display,gray]
 | 
| 42 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 482 | "let | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 483 |   val thy = @{theory}
 | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 484 | |
| 42 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 485 |   val assm1 = cterm_of thy @{prop \"\<And>(x::nat). P x \<Longrightarrow> Q x\"}
 | 
| 49 | 486 |   val assm2 = cterm_of thy @{prop \"(P::nat\<Rightarrow>bool) t\"}
 | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 487 | |
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 488 | val Pt_implies_Qt = | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 489 | assume assm1 | 
| 42 
cd612b489504
tuned mostly antiquotation and text
 Christian Urban <urbanc@in.tum.de> parents: 
41diff
changeset | 490 |         |> forall_elim (cterm_of thy @{term \"t::nat\"});
 | 
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 491 | |
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 492 | val Qt = implies_elim Pt_implies_Qt (assume assm2); | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 493 | in | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 494 | Qt | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 495 | |> implies_intr assm2 | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 496 | |> implies_intr assm1 | 
| 48 
609f9ef73494
fixed FIXME's in fake responses
 Christian Urban <urbanc@in.tum.de> parents: 
47diff
changeset | 497 | end" "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"} | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 498 | |
| 21 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 499 | This code-snippet constructs the following proof: | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 500 | |
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 501 | \[ | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 502 |   \infer[(@{text "\<Longrightarrow>"}$-$intro)]{\vdash @{prop "(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> P t \<Longrightarrow> Q t"}}
 | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 503 |     {\infer[(@{text "\<Longrightarrow>"}$-$intro)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
 | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 504 |        {\infer[(@{text "\<Longrightarrow>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"}, @{prop "P t"} \vdash @{prop "Q t"}}
 | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 505 |           {\infer[(@{text "\<And>"}$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
 | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 506 |                  {\infer[(assume)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "\<And>x. P x \<Longrightarrow> Q x"}}{}}
 | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 507 | & | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 508 |            \infer[(assume)]{@{prop "P t"} \vdash @{prop "P t"}}{}
 | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 509 | } | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 510 | } | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 511 | } | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 512 | \] | 
| 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 513 | |
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 514 | However, while we obtained a theorem as result, this theorem is not | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 515 | yet stored in Isabelle's theorem database. So it cannot be referenced later | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 516 | on. How to store theorems will be explained in the next section. | 
| 21 
2356e5c70d98
added a proof and tuned the rest
 Christian Urban <urbanc@in.tum.de> parents: 
20diff
changeset | 517 | |
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 518 |   \begin{readmore}
 | 
| 50 | 519 |   For the functions @{text "assume"}, @{text "forall_elim"} etc 
 | 
| 13 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 520 |   see \isccite{sec:thms}. The basic functions for theorems are defined in
 | 
| 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 521 |   @{ML_file "Pure/thm.ML"}. 
 | 
| 
2b07da8b310d
polished and added a subdirectory for the recipes
 Christian Urban <urbanc@in.tum.de> parents: 
12diff
changeset | 522 |   \end{readmore}
 | 
| 12 
2f1736cb8f26
various changes by Alex and Christian
 Christian Urban <urbanc@in.tum.de> parents: 
11diff
changeset | 523 | |
| 10 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 524 | *} | 
| 
df09e49b19bf
many changes in the FirstSteps section
 Christian Urban <urbanc@in.tum.de> parents: 
6diff
changeset | 525 | |
| 20 | 526 | section {* Storing Theorems *}
 | 
| 527 | ||
| 528 | section {* Theorem Attributes *}
 | |
| 529 | ||
| 104 | 530 | section {* Printing Terms and Theorems\label{sec:printing} *}
 | 
| 100 | 531 | |
| 532 | text {* 
 | |
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 533 |   During development, you often want to inspect date of type @{ML_type term}, @{ML_type cterm} 
 | 
| 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 534 |   or @{ML_type thm}. Isabelle contains elaborate pretty-printing functions for printing them, 
 | 
| 104 | 535 | but for quick-and-dirty solutions they are far too unwieldy. A simple way to transform | 
| 101 | 536 |   a term into a string is to use the function @{ML Syntax.string_of_term}.
 | 
| 100 | 537 | |
| 538 |   @{ML_response_fake [display,gray]
 | |
| 539 |   "Syntax.string_of_term @{context} @{term \"1::nat\"}"
 | |
| 540 | "\"\\^E\\^Fterm\\^E\\^E\\^Fconst\\^Fname=HOL.one_class.one\\^E1\\^E\\^F\\^E\\^E\\^F\\^E\""} | |
| 541 | ||
| 104 | 542 | This produces a string with some printing directions encoded in it. The string | 
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 543 |   can be properly printed by using the function @{ML warning}.
 | 
| 100 | 544 | |
| 545 |   @{ML_response_fake [display,gray]
 | |
| 546 |   "warning (Syntax.string_of_term @{context} @{term \"1::nat\"})"
 | |
| 547 | "\"1\""} | |
| 548 | ||
| 101 | 549 |   A @{ML_type cterm} can be transformed into a string by the following function.
 | 
| 100 | 550 | *} | 
| 551 | ||
| 552 | ML{*fun str_of_cterm ctxt t =  
 | |
| 553 | Syntax.string_of_term ctxt (term_of t)*} | |
| 554 | ||
| 555 | text {*
 | |
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 556 |   If there are more than one @{ML_type cterm}s to be printed, you can use the 
 | 
| 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 557 |   function @{ML commas} to separate them.
 | 
| 100 | 558 | *} | 
| 559 | ||
| 560 | ML{*fun str_of_cterms ctxt ts =  
 | |
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 561 | commas (map (str_of_cterm ctxt) ts)*} | 
| 100 | 562 | |
| 563 | text {*
 | |
| 101 | 564 | The easiest way to get the string of a theorem is to transform it | 
| 100 | 565 |   into a @{ML_type cterm} using the function @{ML crep_thm}.
 | 
| 566 | *} | |
| 567 | ||
| 568 | ML{*fun str_of_thm ctxt thm =
 | |
| 569 | let | |
| 570 |     val {prop, ...} = crep_thm thm
 | |
| 571 | in | |
| 572 | str_of_cterm ctxt prop | |
| 573 | end*} | |
| 574 | ||
| 575 | text {* 
 | |
| 101 | 576 |   Again the function @{ML commas} helps with printing more than one theorem. 
 | 
| 100 | 577 | *} | 
| 578 | ||
| 579 | ML{*fun str_of_thms ctxt thms =  
 | |
| 580 | commas (map (str_of_thm ctxt) thms)*} | |
| 581 | ||
| 582 | ||
| 75 | 583 | section {* Operations on Constants (Names) *}
 | 
| 39 
631d12c25bde
substantial changes to the antiquotations (preliminary version)
 Christian Urban <urbanc@in.tum.de> parents: 
34diff
changeset | 584 | |
| 68 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 585 | text {*
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 586 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 587 | @{ML_response [display] "Sign.base_name \"List.list.Nil\"" "\"Nil\""}
 | 
| 86 | 588 | |
| 92 | 589 | authentic syntax? | 
| 590 | ||
| 591 | *} | |
| 592 | ||
| 593 | ML {* @{const_name lfp} *}
 | |
| 594 | ||
| 595 | text {*
 | |
| 596 | constants in case-patterns? | |
| 597 | ||
| 598 | In the meantime, lfp has been moved to the Inductive theory, so it is | |
| 599 |   no longer called Lfp.lfp. If a @{text "@{const_name}"} antiquotation had been  
 | |
| 600 | used, we would have gotten an error for this. Another advantage of the | |
| 601 |   antiquotation is that we can then just write @{text "@{const_name lfp}"} rather  
 | |
| 602 |   than @{text "@{const_name Lfp.lfp}"} or whatever, and it expands to the correct  
 | |
| 603 | name. | |
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 604 | |
| 75 | 605 | *} | 
| 606 | ||
| 607 | section {* Combinators\label{sec:combinators} *}
 | |
| 608 | ||
| 609 | text {*
 | |
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 610 | For beginners, perhaps the most puzzling parts in the existing code of Isabelle are | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 611 | the combinators. At first they seem to greatly obstruct the | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 612 | comprehension of the code, but after getting familiar with them, they | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 613 | actually ease the understanding and also the programming. | 
| 73 
bcbcf5c839ae
used newly exported break reference in ThyOutput for writing separate output_list function
 Christian Urban <urbanc@in.tum.de> parents: 
72diff
changeset | 614 | |
| 
bcbcf5c839ae
used newly exported break reference in ThyOutput for writing separate output_list function
 Christian Urban <urbanc@in.tum.de> parents: 
72diff
changeset | 615 |   \begin{readmore}
 | 
| 75 | 616 |   The most frequently used combinator are defined in the files @{ML_file "Pure/library.ML"}
 | 
| 87 | 617 |   and @{ML_file "Pure/General/basics.ML"}. Also \isccite{sec:ML-linear-trans} 
 | 
| 104 | 618 | contains further information about combinators. | 
| 73 
bcbcf5c839ae
used newly exported break reference in ThyOutput for writing separate output_list function
 Christian Urban <urbanc@in.tum.de> parents: 
72diff
changeset | 619 |   \end{readmore}
 | 
| 
bcbcf5c839ae
used newly exported break reference in ThyOutput for writing separate output_list function
 Christian Urban <urbanc@in.tum.de> parents: 
72diff
changeset | 620 | |
| 104 | 621 |   The simplest combinator is @{ML I}, which is just the identity function defined as
 | 
| 73 
bcbcf5c839ae
used newly exported break reference in ThyOutput for writing separate output_list function
 Christian Urban <urbanc@in.tum.de> parents: 
72diff
changeset | 622 | *} | 
| 
bcbcf5c839ae
used newly exported break reference in ThyOutput for writing separate output_list function
 Christian Urban <urbanc@in.tum.de> parents: 
72diff
changeset | 623 | |
| 
bcbcf5c839ae
used newly exported break reference in ThyOutput for writing separate output_list function
 Christian Urban <urbanc@in.tum.de> parents: 
72diff
changeset | 624 | ML{*fun I x = x*}
 | 
| 
bcbcf5c839ae
used newly exported break reference in ThyOutput for writing separate output_list function
 Christian Urban <urbanc@in.tum.de> parents: 
72diff
changeset | 625 | |
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 626 | text {* Another simple combinator is @{ML K}, defined as *}
 | 
| 75 | 627 | |
| 628 | ML{*fun K x = fn _ => x*}
 | |
| 629 | ||
| 73 
bcbcf5c839ae
used newly exported break reference in ThyOutput for writing separate output_list function
 Christian Urban <urbanc@in.tum.de> parents: 
72diff
changeset | 630 | text {*
 | 
| 84 | 631 |   @{ML K} ``wraps'' a function around the argument @{text "x"}. However, this 
 | 
| 101 | 632 |   function ignores its argument. As a result, @{ML K} defines a constant function 
 | 
| 104 | 633 |   always returning @{text x}.
 | 
| 73 
bcbcf5c839ae
used newly exported break reference in ThyOutput for writing separate output_list function
 Christian Urban <urbanc@in.tum.de> parents: 
72diff
changeset | 634 | |
| 101 | 635 |   The next combinator is reverse application, @{ML "|>"}, defined as: 
 | 
| 68 
e7519207c2b7
added more to the "new command section" and tuning
 Christian Urban <urbanc@in.tum.de> parents: 
66diff
changeset | 636 | *} | 
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 637 | |
| 75 | 638 | ML{*fun x |> f = f x*}
 | 
| 639 | ||
| 81 | 640 | text {* While just syntactic sugar for the usual function application,
 | 
| 641 | the purpose of this combinator is to implement functions in a | |
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 642 | ``waterfall fashion''. Consider for example the function *} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 643 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 644 | ML %linenumbers{*fun inc_by_five x =
 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 645 | x |> (fn x => x + 1) | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 646 | |> (fn x => (x, x)) | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 647 | |> fst | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 648 | |> (fn x => x + 4)*} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 649 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 650 | text {*
 | 
| 104 | 651 |   which increments its argument @{text x} by 5. It does this by first incrementing 
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 652 | the argument by 1 (Line 2); then storing the result in a pair (Line 3); taking | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 653 | the first component of the pair (Line 4) and finally incrementing the first | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 654 | component by 4 (Line 5). This kind of cascading manipulations of values is quite | 
| 81 | 655 | common when dealing with theories (for example by adding a definition, followed by | 
| 101 | 656 | lemmas and so on). The reverse application allows you to read what happens in | 
| 657 | a top-down manner. This kind of coding should also be familiar, | |
| 100 | 658 |   if you used Haskell's do-notation. Writing the function @{ML inc_by_five} using 
 | 
| 659 | the reverse application is much clearer than writing | |
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 660 | *} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 661 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 662 | ML{*fun inc_by_five x = fst ((fn x => (x, x)) (x + 1)) + 4*}
 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 663 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 664 | text {* or *}
 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 665 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 666 | ML{*fun inc_by_five x = 
 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 667 | ((fn x => x + 4) o fst o (fn x => (x, x)) o (fn x => x + 1)) x*} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 668 | |
| 81 | 669 | text {* and typographically more economical than *}
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 670 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 671 | ML{*fun inc_by_five x =
 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 672 | let val y1 = x + 1 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 673 | val y2 = (y1, y1) | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 674 | val y3 = fst y2 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 675 | val y4 = y3 + 4 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 676 | in y4 end*} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 677 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 678 | text {* 
 | 
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 679 | Another reason why the let-bindings in the code above are better to be | 
| 84 | 680 | avoided: it is more than easy to get the intermediate values wrong, not to | 
| 681 | mention the nightmares the maintenance of this code causes! | |
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 682 | |
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 683 | |
| 81 | 684 | (FIXME: give a real world example involving theories) | 
| 685 | ||
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 686 |   Similarly, the combinator @{ML "#>"} is the reverse function 
 | 
| 86 | 687 | composition. It can be used to define the following function | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 688 | *} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 689 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 690 | ML{*val inc_by_six = 
 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 691 | (fn x => x + 1) | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 692 | #> (fn x => x + 2) | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 693 | #> (fn x => x + 3)*} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 694 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 695 | text {* 
 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 696 | which is the function composed of first the increment-by-one function and then | 
| 84 | 697 | increment-by-two, followed by increment-by-three. Again, the reverse function | 
| 698 | composition allows one to read the code top-down. | |
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 699 | |
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 700 | The remaining combinators described in this section add convenience for the | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 701 |   ``waterfall method'' of writing functions. The combinator @{ML tap} allows
 | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 702 | one to get hold of an intermediate result (to do some side-calculations for | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 703 | instance). The function | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 704 | |
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 705 | *} | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 706 | |
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 707 | ML %linenumbers{*fun inc_by_three x =
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 708 | x |> (fn x => x + 1) | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 709 | |> tap (fn x => tracing (makestring x)) | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 710 | |> (fn x => x + 2)*} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 711 | |
| 84 | 712 | text {* increments the argument first by one and then by two. In the middle (Line 3),
 | 
| 81 | 713 |   however, it uses @{ML tap} for printing the ``plus-one'' intermediate 
 | 
| 84 | 714 |   result inside the tracing buffer. The function @{ML tap} can only
 | 
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 715 | be used for side-calculations, because any value that is computed cannot | 
| 100 | 716 | be merged back into the ``main waterfall''. To do this, you can use the next | 
| 717 | combinator. | |
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 718 | |
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 719 |   The combinator @{ML "`"} is similar to @{ML tap}, but applies a function to the value
 | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 720 | and returns the result together with the value (as a pair). For example | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 721 | the function | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 722 | *} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 723 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 724 | ML{*fun inc_as_pair x =
 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 725 | x |> `(fn x => x + 1) | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 726 | |> (fn (x, y) => (x, y + 1))*} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 727 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 728 | text {*
 | 
| 100 | 729 |   takes @{text x} as argument, and then increments @{text x}, but also keeps
 | 
| 730 |   @{text x}. The intermediate result is therefore the pair @{ML "(x + 1, x)"
 | |
| 731 | for x}. After that, the function increments the right-hand component of the | |
| 732 |   pair. So finally the result will be @{ML "(x + 1, x + 1)" for x}.
 | |
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 733 | |
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 734 |   The combinators @{ML "|>>"} and @{ML "||>"} are defined for 
 | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 735 | functions manipulating pairs. The first applies the function to | 
| 102 
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
 Christian Urban <urbanc@in.tum.de> parents: 
101diff
changeset | 736 | the first component of the pair, defined as | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 737 | *} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 738 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 739 | ML{*fun (x, y) |>> f = (f x, y)*}
 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 740 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 741 | text {*
 | 
| 81 | 742 | and the second combinator to the second component, defined as | 
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 743 | *} | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 744 | |
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 745 | ML{*fun (x, y) ||> f = (x, f y)*}
 | 
| 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 746 | |
| 81 | 747 | text {*
 | 
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 748 |   With the combinator @{ML "|->"} you can re-combine the elements from a pair.
 | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 749 | This combinator is defined as | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 750 | *} | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 751 | |
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 752 | ML{*fun (x, y) |-> f = f x y*}
 | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 753 | |
| 107 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 754 | text {* and can be used to write the following roundabout version 
 | 
| 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 755 |   of the @{text double} function 
 | 
| 
258ce361ba1b
polished and more material in the tactic chapter
 Christian Urban <urbanc@in.tum.de> parents: 
104diff
changeset | 756 | *} | 
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 757 | |
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 758 | ML{*fun double x =
 | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 759 | x |> (fn x => (x, x)) | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 760 | |-> (fn x => fn y => x + y)*} | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 761 | |
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 762 | text {*
 | 
| 86 | 763 |   Recall that @{ML "|>"} is the reverse function applications. Recall also that the related 
 | 
| 764 |   reverse function composition is @{ML "#>"}. In fact all the combinators @{ML "|->"},
 | |
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 765 |   @{ML "|>>"} and @{ML "||>"} described above have related combinators for function
 | 
| 86 | 766 |   composition, namely @{ML "#->"}, @{ML "#>>"} and @{ML "##>"}. Using @{ML "#->"}, 
 | 
| 767 |   for example, the function @{text double} can also be written as
 | |
| 82 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 768 | *} | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 769 | |
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 770 | ML{*val double =
 | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 771 | (fn x => (x, x)) | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 772 | #-> (fn x => fn y => x + y)*} | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 773 | |
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 774 | text {*
 | 
| 
6dfe6975bda0
polished the combinator section
 Christian Urban <urbanc@in.tum.de> parents: 
81diff
changeset | 775 | |
| 81 | 776 | (FIXME: find a good exercise for combinators) | 
| 777 | *} | |
| 78 
ef778679d3e0
added a section about combinators
 Christian Urban <urbanc@in.tum.de> parents: 
75diff
changeset | 778 | |
| 89 | 779 | |
| 780 | (*<*) | |
| 781 | setup {*
 | |
| 782 |  Sign.add_consts_i [("bar", @{typ "nat"},NoSyn)] 
 | |
| 783 | *} | |
| 784 | ||
| 785 | lemma "bar = (1::nat)" | |
| 786 | oops | |
| 787 | ||
| 788 | setup {*   
 | |
| 789 |   Sign.add_consts_i [("foo", @{typ "nat"},NoSyn)] 
 | |
| 790 | #> PureThy.add_defs false [((Binding.name "foo_def", | |
| 791 |        Logic.mk_equals (Const ("FirstSteps.foo", @{typ "nat"}), @{term "1::nat"})), [])] 
 | |
| 792 | #> snd | |
| 793 | *} | |
| 794 | ||
| 795 | lemma "foo = (1::nat)" | |
| 796 | apply(simp add: foo_def) | |
| 797 | done | |
| 798 | ||
| 799 | thm foo_def | |
| 800 | (*>*) | |
| 801 | ||
| 92 | 802 | section {* Misc *}
 | 
| 803 | ||
| 804 | ML {*DatatypePackage.get_datatype @{theory} "List.list"*}
 | |
| 805 | ||
| 2 
978a3c2ed7ce
split the document into smaller pieces;
 Christian Urban <urbanc@in.tum.de> parents: diff
changeset | 806 | end |