author | Christian Urban <urbanc@in.tum.de> |
Thu, 27 May 2010 10:39:07 +0200 | |
changeset 426 | d94755882e36 |
parent 424 | 5e0a2b50707e |
child 445 | 2f39df9ceb63 |
permissions | -rw-r--r-- |
4
2a69b119cdee
added verbatim the notes by Jeremy
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
1 |
theory Parsing |
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
2 |
imports Base "Helper/Command/Command" "Package/Simple_Inductive_Package" |
4
2a69b119cdee
added verbatim the notes by Jeremy
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
3 |
begin |
2a69b119cdee
added verbatim the notes by Jeremy
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
4 |
|
346
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
5 |
(*<*) |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
6 |
setup {* |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
7 |
open_file_with_prelude |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
8 |
"Parsing_Code.thy" |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
9 |
["theory Parsing", |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
10 |
"imports Base \"Package/Simple_Inductive_Package\"", |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
11 |
"begin"] |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
12 |
*} |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
13 |
(*>*) |
0fea8b7a14a1
tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents:
344
diff
changeset
|
14 |
|
414 | 15 |
chapter {* Parsing\label{chp:parsing} *} |
4
2a69b119cdee
added verbatim the notes by Jeremy
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
16 |
|
2a69b119cdee
added verbatim the notes by Jeremy
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
17 |
text {* |
421
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
18 |
\begin{flushright} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
19 |
{\em An important principle underlying the success and popularity of Unix\\ is |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
20 |
the philosophy of building on the work of others.} \\[1ex] |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
21 |
Linus Torwalds in the email exchange\\ with Andrew S.~Tannenbaum |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
22 |
\end{flushright} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
23 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
24 |
|
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
25 |
Isabelle distinguishes between \emph{outer} and \emph{inner} |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
26 |
syntax. Commands, such as \isacommand{definition}, \isacommand{inductive} |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
27 |
and so on, belong to the outer syntax, whereas terms, types and so on belong |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
28 |
to the inner syntax. For parsing inner syntax, Isabelle uses a rather |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
29 |
general and sophisticated algorithm, which is driven by priority |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
30 |
grammars. Parsers for outer syntax are built up by functional parsing |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
31 |
combinators. These combinators are a well-established technique for parsing, |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
32 |
which has, for example, been described in Paulson's classic ML-book |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
33 |
\cite{paulson-ml2}. Isabelle developers are usually concerned with writing |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
34 |
these outer syntax parsers, either for new definitional packages or for |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
35 |
calling methods with specific arguments. |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
36 |
|
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
37 |
\begin{readmore} |
236 | 38 |
The library for writing parser combinators is split up, roughly, into two |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
39 |
parts: The first part consists of a collection of generic parser combinators |
236 | 40 |
defined in the structure @{ML_struct Scan} in the file @{ML_file |
41 |
"Pure/General/scan.ML"}. The second part of the library consists of |
|
42 |
combinators for dealing with specific token types, which are defined in the |
|
426 | 43 |
structure @{ML_struct Parse} in the file @{ML_file |
424 | 44 |
"Pure/Isar/parse.ML"}. In addition specific parsers for packages are |
45 |
defined in @{ML_file "Pure/Isar/parse_spec.ML"}. Parsers for method arguments |
|
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
46 |
are defined in @{ML_file "Pure/Isar/args.ML"}. |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
47 |
\end{readmore} |
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
48 |
|
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
49 |
*} |
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
50 |
|
49 | 51 |
section {* Building Generic Parsers *} |
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
52 |
|
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
53 |
text {* |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
54 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
55 |
Let us first have a look at parsing strings using generic parsing |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
56 |
combinators. The function @{ML_ind "$$" in Scan} takes a string as argument and will |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
57 |
``consume'' this string from a given input list of strings. ``Consume'' in |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
58 |
this context means that it will return a pair consisting of this string and |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
59 |
the rest of the input list. For example: |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
60 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
61 |
@{ML_response [display,gray] |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
62 |
"($$ \"h\") (Symbol.explode \"hello\")" "(\"h\", [\"e\", \"l\", \"l\", \"o\"])"} |
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
63 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
64 |
@{ML_response [display,gray] |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
65 |
"($$ \"w\") (Symbol.explode \"world\")" "(\"w\", [\"o\", \"r\", \"l\", \"d\"])"} |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
66 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
67 |
The function @{ML "$$"} will either succeed (as in the two examples above) |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
68 |
or raise the exception @{text "FAIL"} if no string can be consumed. For |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
69 |
example trying to parse |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
70 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
71 |
@{ML_response_fake [display,gray] |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
72 |
"($$ \"x\") (Symbol.explode \"world\")" |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
73 |
"Exception FAIL raised"} |
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
74 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
75 |
will raise the exception @{text "FAIL"}. There are three exceptions used in |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
76 |
the parsing combinators: |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
77 |
|
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
78 |
\begin{itemize} |
58 | 79 |
\item @{text "FAIL"} is used to indicate that alternative routes of parsing |
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
80 |
might be explored. |
58 | 81 |
\item @{text "MORE"} indicates that there is not enough input for the parser. For example |
82 |
in @{text "($$ \"h\") []"}. |
|
60
5b9c6010897b
doem tuning and made the cookbook work again with recent changes (CookBook/Package/Ind_Interface.thy needs to be looked at to see what the problem with the new parser type is)
Christian Urban <urbanc@in.tum.de>
parents:
59
diff
changeset
|
83 |
\item @{text "ABORT"} is the exception that is raised when a dead end is reached. |
108
8bea3f74889d
added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
parents:
105
diff
changeset
|
84 |
It is used for example in the function @{ML "!!"} (see below). |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
85 |
\end{itemize} |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
86 |
|
50 | 87 |
However, note that these exceptions are private to the parser and cannot be accessed |
49 | 88 |
by the programmer (for example to handle them). |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
89 |
|
357
80b56d9b322f
included a comment from Tim Bourke
Christian Urban <urbanc@in.tum.de>
parents:
346
diff
changeset
|
90 |
In the examples above we use the function @{ML_ind explode in Symbol} from the |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
91 |
structure @{ML_struct Symbol}, instead of the more standard library function |
369 | 92 |
@{ML_ind explode in String}, for obtaining an input list for the parser. The reason is |
93 |
that @{ML explode} in @{ML_struct Symbol} is aware of character |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
94 |
sequences, for example @{text "\<foo>"}, that have a special meaning in |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
95 |
Isabelle. To see the difference consider |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
96 |
|
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
97 |
@{ML_response_fake [display,gray] |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
98 |
"let |
261 | 99 |
val input = \"\<foo> bar\" |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
100 |
in |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
101 |
(explode input, Symbol.explode input) |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
102 |
end" |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
103 |
"([\"\\\", \"<\", \"f\", \"o\", \"o\", \">\", \" \", \"b\", \"a\", \"r\"], |
261 | 104 |
[\"\<foo>\", \" \", \"b\", \"a\", \"r\"])"} |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
105 |
|
256
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
250
diff
changeset
|
106 |
Slightly more general than the parser @{ML "$$"} is the function |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
107 |
@{ML_ind one in Scan}, in that it takes a predicate as argument and |
256
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
250
diff
changeset
|
108 |
then parses exactly |
52 | 109 |
one item from the input list satisfying this predicate. For example the |
58 | 110 |
following parser either consumes an @{text [quotes] "h"} or a @{text |
49 | 111 |
[quotes] "w"}: |
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
112 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
113 |
@{ML_response [display,gray] |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
114 |
"let |
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
115 |
val hw = Scan.one (fn x => x = \"h\" orelse x = \"w\") |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
116 |
val input1 = Symbol.explode \"hello\" |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
117 |
val input2 = Symbol.explode \"world\" |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
118 |
in |
236 | 119 |
(hw input1, hw input2) |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
120 |
end" |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
121 |
"((\"h\", [\"e\", \"l\", \"l\", \"o\"]),(\"w\", [\"o\", \"r\", \"l\", \"d\"]))"} |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
122 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
123 |
Two parsers can be connected in sequence by using the function @{ML_ind "--" in Scan}. |
220 | 124 |
For example parsing @{text "h"}, @{text "e"} and @{text "l"} (in this |
125 |
order) you can achieve by: |
|
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
126 |
|
236 | 127 |
@{ML_response [display,gray] |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
128 |
"($$ \"h\" -- $$ \"e\" -- $$ \"l\") (Symbol.explode \"hello\")" |
236 | 129 |
"(((\"h\", \"e\"), \"l\"), [\"l\", \"o\"])"} |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
130 |
|
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
131 |
Note how the result of consumed strings builds up on the left as nested pairs. |
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
132 |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
133 |
If, as in the previous example, you want to parse a particular string, |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
134 |
then you can use the function @{ML_ind this_string in Scan}. |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
135 |
|
236 | 136 |
@{ML_response [display,gray] |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
137 |
"Scan.this_string \"hell\" (Symbol.explode \"hello\")" |
236 | 138 |
"(\"hell\", [\"o\"])"} |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
139 |
|
256
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
250
diff
changeset
|
140 |
Parsers that explore alternatives can be constructed using the function |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
141 |
@{ML_ind "||" in Scan}. The parser @{ML "(p || q)" for p q} returns the |
58 | 142 |
result of @{text "p"}, in case it succeeds, otherwise it returns the |
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
143 |
result of @{text "q"}. For example: |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
144 |
|
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
145 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
146 |
@{ML_response [display,gray] |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
147 |
"let |
236 | 148 |
val hw = $$ \"h\" || $$ \"w\" |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
149 |
val input1 = Symbol.explode \"hello\" |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
150 |
val input2 = Symbol.explode \"world\" |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
151 |
in |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
152 |
(hw input1, hw input2) |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
153 |
end" |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
154 |
"((\"h\", [\"e\", \"l\", \"l\", \"o\"]), (\"w\", [\"o\", \"r\", \"l\", \"d\"]))"} |
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
155 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
156 |
The functions @{ML_ind "|--" in Scan} and @{ML_ind "--|" in Scan} work like the sequencing |
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
157 |
function for parsers, except that they discard the item being parsed by the |
357
80b56d9b322f
included a comment from Tim Bourke
Christian Urban <urbanc@in.tum.de>
parents:
346
diff
changeset
|
158 |
first (respectively second) parser. That means the item being dropped is the |
80b56d9b322f
included a comment from Tim Bourke
Christian Urban <urbanc@in.tum.de>
parents:
346
diff
changeset
|
159 |
one that @{ML_ind "|--" in Scan} and @{ML_ind "--|" in Scan} ``point'' away. |
80b56d9b322f
included a comment from Tim Bourke
Christian Urban <urbanc@in.tum.de>
parents:
346
diff
changeset
|
160 |
For example: |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
161 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
162 |
@{ML_response [display,gray] |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
163 |
"let |
236 | 164 |
val just_e = $$ \"h\" |-- $$ \"e\" |
165 |
val just_h = $$ \"h\" --| $$ \"e\" |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
166 |
val input = Symbol.explode \"hello\" |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
167 |
in |
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents:
44
diff
changeset
|
168 |
(just_e input, just_h input) |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
169 |
end" |
241 | 170 |
"((\"e\", [\"l\", \"l\", \"o\"]), (\"h\", [\"l\", \"l\", \"o\"]))"} |
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
171 |
|
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
172 |
The parser @{ML "Scan.optional p x" for p x} returns the result of the parser |
58 | 173 |
@{text "p"}, if it succeeds; otherwise it returns |
104 | 174 |
the default value @{text "x"}. For example: |
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
175 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
176 |
@{ML_response [display,gray] |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
177 |
"let |
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
178 |
val p = Scan.optional ($$ \"h\") \"x\" |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
179 |
val input1 = Symbol.explode \"hello\" |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
180 |
val input2 = Symbol.explode \"world\" |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
181 |
in |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
182 |
(p input1, p input2) |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
183 |
end" |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
184 |
"((\"h\", [\"e\", \"l\", \"l\", \"o\"]), (\"x\", [\"w\", \"o\", \"r\", \"l\", \"d\"]))"} |
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
185 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
186 |
The function @{ML_ind option in Scan} works similarly, except no default value can |
50 | 187 |
be given. Instead, the result is wrapped as an @{text "option"}-type. For example: |
188 |
||
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
189 |
@{ML_response [display,gray] |
50 | 190 |
"let |
191 |
val p = Scan.option ($$ \"h\") |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
192 |
val input1 = Symbol.explode \"hello\" |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
193 |
val input2 = Symbol.explode \"world\" |
50 | 194 |
in |
195 |
(p input1, p input2) |
|
196 |
end" "((SOME \"h\", [\"e\", \"l\", \"l\", \"o\"]), (NONE, [\"w\", \"o\", \"r\", \"l\", \"d\"]))"} |
|
49 | 197 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
198 |
The function @{ML_ind ahead in Scan} parses some input, but leaves the original |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
199 |
input unchanged. For example: |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
200 |
|
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
201 |
@{ML_response [display,gray] |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
202 |
"Scan.ahead (Scan.this_string \"foo\") (Symbol.explode \"foo\")" |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
203 |
"(\"foo\", [\"f\", \"o\", \"o\"])"} |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
204 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
205 |
The function @{ML_ind "!!" in Scan} helps with producing appropriate error messages |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
206 |
during parsing. For example if you want to parse @{text p} immediately |
58 | 207 |
followed by @{text q}, or start a completely different parser @{text r}, |
104 | 208 |
you might write: |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
209 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
210 |
@{ML [display,gray] "(p -- q) || r" for p q r} |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
211 |
|
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
212 |
However, this parser is problematic for producing a useful error |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
213 |
message, if the parsing of @{ML "(p -- q)" for p q} fails. Because with the |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
214 |
parser above you lose the information that @{text p} should be followed by @{text q}. |
220 | 215 |
To see this assume that @{text p} is present in the input, but it is not |
216 |
followed by @{text q}. That means @{ML "(p -- q)" for p q} will fail and |
|
217 |
hence the alternative parser @{text r} will be tried. However, in many |
|
236 | 218 |
circumstances this will be the wrong parser for the input ``@{text "p"}-followed-by-something'' |
220 | 219 |
and therefore will also fail. The error message is then caused by the failure |
220 |
of @{text r}, not by the absence of @{text q} in the input. This kind of |
|
221 |
situation can be avoided when using the function @{ML "!!"}. This function |
|
222 |
aborts the whole process of parsing in case of a failure and prints an error |
|
223 |
message. For example if you invoke the parser |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
224 |
|
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
225 |
|
236 | 226 |
@{ML [display,gray] "!! (fn _ => \"foo\") ($$ \"h\")"} |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
227 |
|
58 | 228 |
on @{text [quotes] "hello"}, the parsing succeeds |
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
parents:
38
diff
changeset
|
229 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
230 |
@{ML_response [display,gray] |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
231 |
"(!! (fn _ => \"foo\") ($$ \"h\")) (Symbol.explode \"hello\")" |
236 | 232 |
"(\"h\", [\"e\", \"l\", \"l\", \"o\"])"} |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
233 |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
234 |
but if you invoke it on @{text [quotes] "world"} |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
235 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
236 |
@{ML_response_fake [display,gray] "(!! (fn _ => \"foo\") ($$ \"h\")) (Symbol.explode \"world\")" |
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
237 |
"Exception ABORT raised"} |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
238 |
|
108
8bea3f74889d
added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
parents:
105
diff
changeset
|
239 |
then the parsing aborts and the error message @{text "foo"} is printed. In order to |
120
c39f83d8daeb
some polishing; split up the file External Solver into two
Christian Urban <urbanc@in.tum.de>
parents:
116
diff
changeset
|
240 |
see the error message properly, you need to prefix the parser with the function |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
241 |
@{ML_ind error in Scan}. For example: |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
242 |
|
236 | 243 |
@{ML_response_fake [display,gray] |
244 |
"Scan.error (!! (fn _ => \"foo\") ($$ \"h\"))" |
|
245 |
"Exception Error \"foo\" raised"} |
|
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
246 |
|
426 | 247 |
This ``prefixing'' is usually done by wrappers such as @{ML_ind local_theory in Outer_Syntax} |
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
248 |
(see Section~\ref{sec:newcommand} which explains this function in more detail). |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
249 |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
250 |
Let us now return to our example of parsing @{ML "(p -- q) || r" for p q |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
251 |
r}. If you want to generate the correct error message for failure |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
252 |
of parsing @{text "p"}-followed-by-@{text "q"}, then you have to write: |
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
253 |
*} |
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
254 |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
255 |
ML{*fun p_followed_by_q p q r = |
133
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
256 |
let |
236 | 257 |
val err_msg = fn _ => p ^ " is not followed by " ^ q |
133
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
258 |
in |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
259 |
($$ p -- (!! err_msg ($$ q))) || ($$ r -- $$ r) |
3e94ccc0f31e
polishing and start of the section about attributes
Christian Urban <urbanc@in.tum.de>
parents:
132
diff
changeset
|
260 |
end *} |
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
261 |
|
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
262 |
|
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
263 |
text {* |
220 | 264 |
Running this parser with the arguments |
265 |
@{text [quotes] "h"}, @{text [quotes] "e"} and @{text [quotes] "w"}, and |
|
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
266 |
the input @{text [quotes] "holle"} |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
267 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
268 |
@{ML_response_fake [display,gray] "Scan.error (p_followed_by_q \"h\" \"e\" \"w\") (Symbol.explode \"holle\")" |
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
269 |
"Exception ERROR \"h is not followed by e\" raised"} |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
270 |
|
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
271 |
produces the correct error message. Running it with |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
272 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
273 |
@{ML_response [display,gray] "Scan.error (p_followed_by_q \"h\" \"e\" \"w\") (Symbol.explode \"wworld\")" |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
274 |
"((\"w\", \"w\"), [\"o\", \"r\", \"l\", \"d\"])"} |
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
275 |
|
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
276 |
yields the expected parsing. |
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
277 |
|
58 | 278 |
The function @{ML "Scan.repeat p" for p} will apply a parser @{text p} as |
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
279 |
often as it succeeds. For example: |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
280 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
281 |
@{ML_response [display,gray] "Scan.repeat ($$ \"h\") (Symbol.explode \"hhhhello\")" |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
282 |
"([\"h\", \"h\", \"h\", \"h\"], [\"e\", \"l\", \"l\", \"o\"])"} |
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
283 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
284 |
Note that @{ML_ind repeat in Scan} stores the parsed items in a list. The function |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
285 |
@{ML_ind repeat1 in Scan} is similar, but requires that the parser @{text "p"} |
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
286 |
succeeds at least once. |
48
609f9ef73494
fixed FIXME's in fake responses
Christian Urban <urbanc@in.tum.de>
parents:
47
diff
changeset
|
287 |
|
58 | 288 |
Also note that the parser would have aborted with the exception @{text MORE}, if |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
289 |
you had it run with the string @{text [quotes] "hhhh"}. This can be avoided by using |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
290 |
the wrapper @{ML_ind finite in Scan} and the ``stopper-token'' |
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
291 |
@{ML_ind stopper in Symbol}. With them you can write: |
49 | 292 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
293 |
@{ML_response [display,gray] "Scan.finite Symbol.stopper (Scan.repeat ($$ \"h\")) (Symbol.explode \"hhhh\")" |
49 | 294 |
"([\"h\", \"h\", \"h\", \"h\"], [])"} |
295 |
||
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
296 |
The function @{ML stopper in Symbol} is the ``end-of-input'' indicator for parsing strings; |
128 | 297 |
other stoppers need to be used when parsing, for example, tokens. However, this kind of |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
298 |
manually wrapping is often already done by the surrounding infrastructure. |
49 | 299 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
300 |
The function @{ML_ind repeat in Scan} can be used with @{ML_ind one in Scan} to read any |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
301 |
string as in |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
302 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
303 |
@{ML_response [display,gray] |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
304 |
"let |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
305 |
val p = Scan.repeat (Scan.one Symbol.not_eof) |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
306 |
val input = Symbol.explode \"foo bar foo\" |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
307 |
in |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
308 |
Scan.finite Symbol.stopper p input |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
309 |
end" |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
310 |
"([\"f\", \"o\", \"o\", \" \", \"b\", \"a\", \"r\", \" \", \"f\", \"o\", \"o\"], [])"} |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
311 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
312 |
where the function @{ML_ind not_eof in Symbol} ensures that we do not read beyond the |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
313 |
end of the input string (i.e.~stopper symbol). |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
314 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
315 |
The function @{ML_ind unless in Scan} takes two parsers: if the first one can |
60
5b9c6010897b
doem tuning and made the cookbook work again with recent changes (CookBook/Package/Ind_Interface.thy needs to be looked at to see what the problem with the new parser type is)
Christian Urban <urbanc@in.tum.de>
parents:
59
diff
changeset
|
316 |
parse the input, then the whole parser fails; if not, then the second is tried. Therefore |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
317 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
318 |
@{ML_response_fake_both [display,gray] "Scan.unless ($$ \"h\") ($$ \"w\") (Symbol.explode \"hello\")" |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
319 |
"Exception FAIL raised"} |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
320 |
|
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
321 |
fails, while |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
322 |
|
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
323 |
@{ML_response [display,gray] "Scan.unless ($$ \"h\") ($$ \"w\") (Symbol.explode \"world\")" |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
324 |
"(\"w\",[\"o\", \"r\", \"l\", \"d\"])"} |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
325 |
|
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
326 |
succeeds. |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
327 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
328 |
The functions @{ML_ind repeat in Scan} and @{ML_ind unless in Scan} can |
256
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
250
diff
changeset
|
329 |
be combined to read any input until a certain marker symbol is reached. In the |
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
250
diff
changeset
|
330 |
example below the marker symbol is a @{text [quotes] "*"}. |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
331 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
332 |
@{ML_response [display,gray] |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
333 |
"let |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
334 |
val p = Scan.repeat (Scan.unless ($$ \"*\") (Scan.one Symbol.not_eof)) |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
335 |
val input1 = Symbol.explode \"fooooo\" |
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
336 |
val input2 = Symbol.explode \"foo*ooo\" |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
337 |
in |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
338 |
(Scan.finite Symbol.stopper p input1, |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
339 |
Scan.finite Symbol.stopper p input2) |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
340 |
end" |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
341 |
"(([\"f\", \"o\", \"o\", \"o\", \"o\", \"o\"], []), |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
342 |
([\"f\", \"o\", \"o\"], [\"*\", \"o\", \"o\", \"o\"]))"} |
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
343 |
|
256
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
250
diff
changeset
|
344 |
|
220 | 345 |
After parsing is done, you almost always want to apply a function to the parsed |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
346 |
items. One way to do this is the function @{ML_ind ">>" in Scan} where |
256
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
250
diff
changeset
|
347 |
@{ML "(p >> f)" for p f} runs |
58 | 348 |
first the parser @{text p} and upon successful completion applies the |
349 |
function @{text f} to the result. For example |
|
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
350 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
351 |
@{ML_response [display,gray] |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
352 |
"let |
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
353 |
fun double (x, y) = (x ^ x, y ^ y) |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
354 |
val parser = $$ \"h\" -- $$ \"e\" |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
355 |
in |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
356 |
(parser >> double) (Symbol.explode \"hello\") |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
357 |
end" |
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
358 |
"((\"hh\", \"ee\"), [\"l\", \"l\", \"o\"])"} |
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
359 |
|
104 | 360 |
doubles the two parsed input strings; or |
59 | 361 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
362 |
@{ML_response [display,gray] |
59 | 363 |
"let |
104 | 364 |
val p = Scan.repeat (Scan.one Symbol.not_eof) |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
365 |
val input = Symbol.explode \"foo bar foo\" |
59 | 366 |
in |
104 | 367 |
Scan.finite Symbol.stopper (p >> implode) input |
59 | 368 |
end" |
369 |
"(\"foo bar foo\",[])"} |
|
370 |
||
60
5b9c6010897b
doem tuning and made the cookbook work again with recent changes (CookBook/Package/Ind_Interface.thy needs to be looked at to see what the problem with the new parser type is)
Christian Urban <urbanc@in.tum.de>
parents:
59
diff
changeset
|
371 |
where the single-character strings in the parsed output are transformed |
59 | 372 |
back into one string. |
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
parents:
54
diff
changeset
|
373 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
374 |
The function @{ML_ind lift in Scan} takes a parser and a pair as arguments. This function applies |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
375 |
the given parser to the second component of the pair and leaves the first component |
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
376 |
untouched. For example |
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
377 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
378 |
@{ML_response [display,gray] |
240
d111f5988e49
replaced explode by Symbol.explode
Christian Urban <urbanc@in.tum.de>
parents:
236
diff
changeset
|
379 |
"Scan.lift ($$ \"h\" -- $$ \"e\") (1, Symbol.explode \"hello\")" |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
380 |
"((\"h\", \"e\"), (1, [\"l\", \"l\", \"o\"]))"} |
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
381 |
|
390
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
382 |
\footnote{\bf FIXME: In which situations is @{text "lift"} useful? Give examples.} |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
383 |
|
397 | 384 |
Be aware of recursive parsers. Suppose you want to read strings separated by |
385 |
commas and by parentheses into a tree datastructure; for example, generating |
|
386 |
the tree corresponding to the string @{text [quotes] "(A, A), (A, A)"} where |
|
387 |
the @{text "A"}s will be the leaves. We assume the trees are represented by the |
|
388 |
datatype: |
|
390
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
389 |
*} |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
390 |
|
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
391 |
ML{*datatype tree = |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
392 |
Lf of string |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
393 |
| Br of tree * tree*} |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
394 |
|
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
395 |
text {* |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
396 |
Since nested parentheses should be treated in a meaningful way---for example |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
397 |
the string @{text [quotes] "((A))"} should be read into a single |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
398 |
leaf---you might implement the following parser. |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
399 |
*} |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
400 |
|
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
401 |
ML{*fun parse_basic s = |
397 | 402 |
$$ s >> Lf || $$ "(" |-- parse_tree s --| $$ ")" |
403 |
||
390
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
404 |
and parse_tree s = |
397 | 405 |
parse_basic s --| $$ "," -- parse_tree s >> Br || parse_basic s*} |
390
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
406 |
|
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
407 |
text {* |
397 | 408 |
This parser corrsponds to the grammar: |
409 |
||
410 |
\begin{center} |
|
411 |
\begin{tabular}{lcl} |
|
412 |
@{text "<Basic>"} & @{text "::="} & @{text "<String> | (<Tree>)"}\\ |
|
413 |
@{text "<Tree>"} & @{text "::="} & @{text "<Basic>, <Tree> | <Basic>"}\\ |
|
414 |
\end{tabular} |
|
415 |
\end{center} |
|
416 |
||
390
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
417 |
The parameter @{text "s"} is the string over which the tree is parsed. The |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
418 |
parser @{ML parse_basic} reads either a leaf or a tree enclosed in |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
419 |
parentheses. The parser @{ML parse_tree} reads either a pair of trees |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
420 |
separated by a comma, or acts like @{ML parse_basic}. Unfortunately, |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
421 |
because of the mutual recursion, this parser will immediately run into a |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
422 |
loop, even if it is called without any input. For example |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
423 |
|
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
424 |
@{ML_response_fake_both [display, gray] |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
425 |
"parse_tree \"A\"" |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
426 |
"*** Exception- TOPLEVEL_ERROR raised"} |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
427 |
|
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
428 |
raises an exception indicating that the stack limit is reached. Such |
392
47e5b71c7f6c
modified the typ-pretty-printer and added parser exercise
Christian Urban <urbanc@in.tum.de>
parents:
391
diff
changeset
|
429 |
looping parser are not useful, because of ML's strict evaluation of |
390
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
430 |
arguments. Therefore we need to delay the execution of the |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
431 |
parser until an input is given. This can be done by adding the parsed |
397 | 432 |
string as an explicit argument. So the parser above should be implemented |
433 |
as follows. |
|
390
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
434 |
*} |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
435 |
|
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
436 |
ML{*fun parse_basic s xs = |
397 | 437 |
($$ s >> Lf || $$ "(" |-- parse_tree s --| $$ ")") xs |
438 |
||
390
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
439 |
and parse_tree s xs = |
397 | 440 |
(parse_basic s --| $$ "," -- parse_tree s >> Br || parse_basic s) xs*} |
390
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
441 |
|
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
442 |
text {* |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
443 |
While the type of the parser is unchanged by the addition, its behaviour |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
444 |
changed: with this version of the parser the execution is delayed until |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
445 |
some string is applied for the argument @{text "xs"}. This gives us |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
446 |
exactly the parser what we wanted. An example is as follows: |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
447 |
|
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
448 |
@{ML_response [display, gray] |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
449 |
"let |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
450 |
val input = Symbol.explode \"(A,((A))),A\" |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
451 |
in |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
452 |
Scan.finite Symbol.stopper (parse_tree \"A\") input |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
453 |
end" |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
454 |
"(Br (Br (Lf \"A\", Lf \"A\"), Lf \"A\"), [])"} |
8ad407e77ea0
added example by Lukas Bulwahn
Christian Urban <urbanc@in.tum.de>
parents:
376
diff
changeset
|
455 |
|
149 | 456 |
|
457 |
\begin{exercise}\label{ex:scancmts} |
|
458 |
Write a parser that parses an input string so that any comment enclosed |
|
220 | 459 |
within @{text "(*\<dots>*)"} is replaced by the same comment but enclosed within |
149 | 460 |
@{text "(**\<dots>**)"} in the output string. To enclose a string, you can use the |
461 |
function @{ML "enclose s1 s2 s" for s1 s2 s} which produces the string @{ML |
|
236 | 462 |
"s1 ^ s ^ s2" for s1 s2 s}. Hint: To simplify the task ignore the proper |
463 |
nesting of comments. |
|
149 | 464 |
\end{exercise} |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
465 |
*} |
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
466 |
|
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
467 |
section {* Parsing Theory Syntax *} |
38
e21b2f888fa2
added a preliminary section about parsing
Christian Urban <urbanc@in.tum.de>
parents:
16
diff
changeset
|
468 |
|
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
469 |
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:
101
diff
changeset
|
470 |
Most of the time, however, Isabelle developers have to deal with parsing |
156 | 471 |
tokens, not strings. These token parsers have the type: |
128 | 472 |
*} |
473 |
||
426 | 474 |
ML{*type 'a parser = Token.T list -> 'a * Token.T list*} |
128 | 475 |
|
476 |
text {* |
|
149 | 477 |
The reason for using token parsers is that theory syntax, as well as the |
128 | 478 |
parsers for the arguments of proof methods, use the type @{ML_type |
426 | 479 |
Token.T}. |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
480 |
|
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
481 |
\begin{readmore} |
40
35e1dff0d9bb
more on the parsing section
Christian Urban <urbanc@in.tum.de>
parents:
39
diff
changeset
|
482 |
The parser functions for the theory syntax are contained in the structure |
426 | 483 |
@{ML_struct Parse} defined in the file @{ML_file "Pure/Isar/parse.ML"}. |
484 |
The definition for tokens is in the file @{ML_file "Pure/Isar/token.ML"}. |
|
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
485 |
\end{readmore} |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
486 |
|
426 | 487 |
The structure @{ML_struct Token} defines several kinds of tokens (for |
488 |
example @{ML_ind Ident in Token} for identifiers, @{ML Keyword in |
|
489 |
Token} for keywords and @{ML_ind Command in Token} for commands). Some |
|
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
490 |
token parsers take into account the kind of tokens. The first example shows |
256
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
250
diff
changeset
|
491 |
how to generate a token list out of a string using the function |
426 | 492 |
@{ML_ind scan in Outer_Syntax}. It is given the argument |
256
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
250
diff
changeset
|
493 |
@{ML "Position.none"} since, |
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
494 |
at the moment, we are not interested in generating precise error |
376
76b1b679e845
removed comment about compiler bug
Christian Urban <urbanc@in.tum.de>
parents:
374
diff
changeset
|
495 |
messages. The following code |
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
496 |
|
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
497 |
|
426 | 498 |
@{ML_response_fake [display,gray] "Outer_Syntax.scan Position.none \"hello world\"" |
50 | 499 |
"[Token (\<dots>,(Ident, \"hello\"),\<dots>), |
500 |
Token (\<dots>,(Space, \" \"),\<dots>), |
|
501 |
Token (\<dots>,(Ident, \"world\"),\<dots>)]"} |
|
502 |
||
503 |
produces three tokens where the first and the last are identifiers, since |
|
58 | 504 |
@{text [quotes] "hello"} and @{text [quotes] "world"} do not match any |
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
505 |
other syntactic category. The second indicates a space. |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
506 |
|
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
507 |
We can easily change what is recognised as a keyword with the function |
426 | 508 |
@{ML_ind keyword in Keyword}. For example calling it with |
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
509 |
*} |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
510 |
|
426 | 511 |
ML{*val _ = Keyword.keyword "hello"*} |
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
512 |
|
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
513 |
text {* |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
514 |
then lexing @{text [quotes] "hello world"} will produce |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
515 |
|
426 | 516 |
@{ML_response_fake [display,gray] "Outer_Syntax.scan Position.none \"hello world\"" |
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
517 |
"[Token (\<dots>,(Keyword, \"hello\"),\<dots>), |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
518 |
Token (\<dots>,(Space, \" \"),\<dots>), |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
519 |
Token (\<dots>,(Ident, \"world\"),\<dots>)]"} |
50 | 520 |
|
241 | 521 |
Many parsing functions later on will require white space, comments and the like |
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
522 |
to have already been filtered out. So from now on we are going to use the |
426 | 523 |
functions @{ML filter} and @{ML_ind is_proper in Token} to do this. |
256
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
250
diff
changeset
|
524 |
For example: |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
525 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
526 |
@{ML_response_fake [display,gray] |
50 | 527 |
"let |
426 | 528 |
val input = Outer_Syntax.scan Position.none \"hello world\" |
50 | 529 |
in |
426 | 530 |
filter Token.is_proper input |
50 | 531 |
end" |
532 |
"[Token (\<dots>,(Ident, \"hello\"), \<dots>), Token (\<dots>,(Ident, \"world\"), \<dots>)]"} |
|
533 |
||
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
534 |
For convenience we define the function: |
50 | 535 |
*} |
536 |
||
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
537 |
ML{*fun filtered_input str = |
426 | 538 |
filter Token.is_proper (Outer_Syntax.scan Position.none str) *} |
50 | 539 |
|
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
540 |
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:
101
diff
changeset
|
541 |
If you now parse |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
542 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
543 |
@{ML_response_fake [display,gray] |
50 | 544 |
"filtered_input \"inductive | for\"" |
545 |
"[Token (\<dots>,(Command, \"inductive\"),\<dots>), |
|
546 |
Token (\<dots>,(Keyword, \"|\"),\<dots>), |
|
547 |
Token (\<dots>,(Keyword, \"for\"),\<dots>)]"} |
|
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
548 |
|
221 | 549 |
you obtain a list consisting of only one command and two keyword tokens. |
241 | 550 |
If you want to see which keywords and commands are currently known to Isabelle, |
551 |
type: |
|
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents:
44
diff
changeset
|
552 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
553 |
@{ML_response_fake [display,gray] |
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents:
44
diff
changeset
|
554 |
"let |
426 | 555 |
val (keywords, commands) = Keyword.get_lexicons () |
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents:
44
diff
changeset
|
556 |
in |
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents:
44
diff
changeset
|
557 |
(Scan.dest_lexicon commands, Scan.dest_lexicon keywords) |
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
parents:
44
diff
changeset
|
558 |
end" |
132 | 559 |
"([\"}\", \"{\", \<dots>], [\"\<rightleftharpoons>\", \"\<leftharpoondown>\", \<dots>])"} |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
560 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
561 |
You might have to adjust the @{ML_ind print_depth} in order to |
241 | 562 |
see the complete list. |
563 |
||
426 | 564 |
The parser @{ML_ind "$$$" in Parse} parses a single keyword. For example: |
50 | 565 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
566 |
@{ML_response [display,gray] |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
567 |
"let |
50 | 568 |
val input1 = filtered_input \"where for\" |
569 |
val input2 = filtered_input \"| in\" |
|
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
570 |
in |
426 | 571 |
(Parse.$$$ \"where\" input1, Parse.$$$ \"|\" input2) |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
572 |
end" |
128 | 573 |
"((\"where\",\<dots>), (\"|\",\<dots>))"} |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
574 |
|
426 | 575 |
Any non-keyword string can be parsed with the function @{ML_ind reserved in Parse}. |
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
576 |
For example: |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
577 |
|
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
578 |
@{ML_response [display,gray] |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
579 |
"let |
426 | 580 |
val p = Parse.reserved \"bar\" |
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
581 |
val input = filtered_input \"bar\" |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
582 |
in |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
583 |
p input |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
584 |
end" |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
585 |
"(\"bar\",[])"} |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
586 |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
587 |
Like before, you can sequentially connect parsers with @{ML "--"}. For example: |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
588 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
589 |
@{ML_response [display,gray] |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
590 |
"let |
50 | 591 |
val input = filtered_input \"| in\" |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
592 |
in |
426 | 593 |
(Parse.$$$ \"|\" -- Parse.$$$ \"in\") input |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
594 |
end" |
183
8bb4eaa2ec92
a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents:
181
diff
changeset
|
595 |
"((\"|\", \"in\"), [])"} |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
596 |
|
426 | 597 |
The parser @{ML "Parse.enum s p" for s p} parses a possibly empty |
58 | 598 |
list of items recognised by the parser @{text p}, where the items being parsed |
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
599 |
are separated by the string @{text s}. For example: |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
600 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
601 |
@{ML_response [display,gray] |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
602 |
"let |
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
603 |
val input = filtered_input \"in | in | in foo\" |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
604 |
in |
426 | 605 |
(Parse.enum \"|\" (Parse.$$$ \"in\")) input |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
606 |
end" |
183
8bb4eaa2ec92
a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents:
181
diff
changeset
|
607 |
"([\"in\", \"in\", \"in\"], [\<dots>])"} |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
608 |
|
426 | 609 |
The function @{ML_ind enum1 in Parse} works similarly, except that the |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
610 |
parsed list must be non-empty. Note that we had to add a string @{text |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
611 |
[quotes] "foo"} at the end of the parsed string, otherwise the parser would |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
612 |
have consumed all tokens and then failed with the exception @{text |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
613 |
"MORE"}. Like in the previous section, we can avoid this exception using the |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
614 |
wrapper @{ML Scan.finite}. This time, however, we have to use the |
426 | 615 |
``stopper-token'' @{ML Token.stopper}. We can write: |
49 | 616 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
617 |
@{ML_response [display,gray] |
49 | 618 |
"let |
50 | 619 |
val input = filtered_input \"in | in | in\" |
426 | 620 |
val p = Parse.enum \"|\" (Parse.$$$ \"in\") |
49 | 621 |
in |
426 | 622 |
Scan.finite Token.stopper p input |
49 | 623 |
end" |
183
8bb4eaa2ec92
a simplification suggested by Stefan and some polishing
Christian Urban <urbanc@in.tum.de>
parents:
181
diff
changeset
|
624 |
"([\"in\", \"in\", \"in\"], [])"} |
49 | 625 |
|
75 | 626 |
The following function will help to run examples. |
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
627 |
*} |
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
628 |
|
426 | 629 |
ML{*fun parse p input = Scan.finite Token.stopper (Scan.error p) input *} |
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
630 |
|
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
631 |
text {* |
426 | 632 |
The function @{ML_ind "!!!" in Parse} can be used to force termination |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
633 |
of the parser in case of a dead end, just like @{ML "Scan.!!"} (see previous |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
634 |
section). A difference, however, is that the error message of @{ML |
426 | 635 |
"Parse.!!!"} is fixed to be @{text [quotes] "Outer syntax error"} |
221 | 636 |
together with a relatively precise description of the failure. For example: |
49 | 637 |
|
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
638 |
@{ML_response_fake [display,gray] |
49 | 639 |
"let |
50 | 640 |
val input = filtered_input \"in |\" |
426 | 641 |
val parse_bar_then_in = Parse.$$$ \"|\" -- Parse.$$$ \"in\" |
49 | 642 |
in |
426 | 643 |
parse (Parse.!!! parse_bar_then_in) input |
49 | 644 |
end" |
645 |
"Exception ERROR \"Outer syntax error: keyword \"|\" expected, |
|
646 |
but keyword in was found\" raised" |
|
647 |
} |
|
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
648 |
|
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
649 |
\begin{exercise} (FIXME) |
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
650 |
A type-identifier, for example @{typ "'a"}, is a token of |
426 | 651 |
kind @{ML_ind Keyword in Token}. It can be parsed using |
652 |
the function @{ML type_ident in Parse}. |
|
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
653 |
\end{exercise} |
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
654 |
|
104 | 655 |
(FIXME: or give parser for numbers) |
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents:
52
diff
changeset
|
656 |
|
125 | 657 |
Whenever there is a possibility that the processing of user input can fail, |
221 | 658 |
it is a good idea to give all available information about where the error |
220 | 659 |
occurred. For this Isabelle can attach positional information to tokens |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
660 |
and then thread this information up the ``processing chain''. To see this, |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
661 |
modify the function @{ML filtered_input}, described earlier, as follows |
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
662 |
*} |
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
663 |
|
125 | 664 |
ML{*fun filtered_input' str = |
426 | 665 |
filter Token.is_proper (Outer_Syntax.scan (Position.line 7) str) *} |
49 | 666 |
|
667 |
text {* |
|
125 | 668 |
where we pretend the parsed string starts on line 7. An example is |
49 | 669 |
|
125 | 670 |
@{ML_response_fake [display,gray] |
671 |
"filtered_input' \"foo \\n bar\"" |
|
672 |
"[Token ((\"foo\", ({line=7, end_line=7}, {line=7})), (Ident, \"foo\"), \<dots>), |
|
673 |
Token ((\"bar\", ({line=8, end_line=8}, {line=8})), (Ident, \"bar\"), \<dots>)]"} |
|
674 |
||
675 |
in which the @{text [quotes] "\\n"} causes the second token to be in |
|
676 |
line 8. |
|
677 |
||
426 | 678 |
By using the parser @{ML position in Parse} you can access the token |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
679 |
position and return it as part of the parser result. For example |
125 | 680 |
|
681 |
@{ML_response_fake [display,gray] |
|
682 |
"let |
|
241 | 683 |
val input = filtered_input' \"where\" |
125 | 684 |
in |
426 | 685 |
parse (Parse.position (Parse.$$$ \"where\")) input |
125 | 686 |
end" |
687 |
"((\"where\", {line=7, end_line=7}), [])"} |
|
688 |
||
689 |
\begin{readmore} |
|
690 |
The functions related to positions are implemented in the file |
|
691 |
@{ML_file "Pure/General/position.ML"}. |
|
692 |
\end{readmore} |
|
49 | 693 |
|
391 | 694 |
\begin{exercise}\label{ex:contextfree} |
695 |
Write a parser for the context-free grammar representing arithmetic |
|
696 |
expressions with addition and multiplication. As usual, multiplication |
|
697 |
binds stronger than addition, and both of them nest to the right. |
|
698 |
The context-free grammar is defined as: |
|
699 |
||
700 |
\begin{center} |
|
701 |
\begin{tabular}{lcl} |
|
702 |
@{text "<Basic>"} & @{text "::="} & @{text "<Number> | (<Expr>)"}\\ |
|
703 |
@{text "<Factor>"} & @{text "::="} & @{text "<Basic> * <Factor> | <Basic>"}\\ |
|
704 |
@{text "<Expr>"} & @{text "::="} & @{text "<Factor> + <Expr> | <Factor>"}\\ |
|
705 |
\end{tabular} |
|
706 |
\end{center} |
|
707 |
||
708 |
Hint: Be careful with recursive parsers. |
|
709 |
\end{exercise} |
|
49 | 710 |
*} |
711 |
||
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
712 |
section {* Parsers for ML-Code (TBD) *} |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
713 |
|
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
714 |
text {* |
426 | 715 |
@{ML_ind ML_source in Parse} |
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
716 |
*} |
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
717 |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
718 |
section {* Context Parser (TBD) *} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
719 |
|
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
720 |
text {* |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
721 |
@{ML_ind Args.context} |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
722 |
*} |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
723 |
(* |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
724 |
ML {* |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
725 |
let |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
726 |
val parser = Args.context -- Scan.lift Args.name_source |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
727 |
|
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
728 |
fun term_pat (ctxt, str) = |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
729 |
str |> Syntax.read_prop ctxt |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
730 |
in |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
731 |
(parser >> term_pat) (Context.Proof @{context}, filtered_input "f (a::nat)") |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
732 |
|> fst |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
733 |
end |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
734 |
*} |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
735 |
*) |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
736 |
|
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
737 |
text {* |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
738 |
@{ML_ind Args.context} |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
739 |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
740 |
Used for example in \isacommand{attribute\_setup} and \isacommand{method\_setup}. |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
741 |
*} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
742 |
|
207 | 743 |
section {* Argument and Attribute Parsers (TBD) *} |
744 |
||
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
745 |
section {* Parsing Inner Syntax *} |
42
cd612b489504
tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents:
41
diff
changeset
|
746 |
|
125 | 747 |
text {* |
748 |
There is usually no need to write your own parser for parsing inner syntax, that is |
|
285 | 749 |
for terms and types: you can just call the predefined parsers. Terms can |
426 | 750 |
be parsed using the function @{ML_ind term in Parse}. For example: |
125 | 751 |
|
752 |
@{ML_response [display,gray] |
|
753 |
"let |
|
426 | 754 |
val input = Outer_Syntax.scan Position.none \"foo\" |
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
parents:
43
diff
changeset
|
755 |
in |
426 | 756 |
Parse.term input |
125 | 757 |
end" |
758 |
"(\"\\^E\\^Ftoken\\^Efoo\\^E\\^F\\^E\", [])"} |
|
759 |
||
426 | 760 |
The function @{ML_ind prop in Parse} is similar, except that it gives a different |
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
761 |
error message, when parsing fails. As you can see, the parser not just returns |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
762 |
the parsed string, but also some encoded information. You can decode the |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
763 |
information with the function @{ML_ind parse in YXML} in @{ML_struct YXML}. For example |
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
764 |
|
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
765 |
@{ML_response [display,gray] |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
766 |
"YXML.parse \"\\^E\\^Ftoken\\^Efoo\\^E\\^F\\^E\"" |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
767 |
"XML.Elem (\"token\", [], [XML.Text \"foo\"])"} |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
768 |
|
149 | 769 |
The result of the decoding is an XML-tree. You can see better what is going on if |
131 | 770 |
you replace @{ML Position.none} by @{ML "Position.line 42"}, say: |
101 | 771 |
|
125 | 772 |
@{ML_response [display,gray] |
773 |
"let |
|
426 | 774 |
val input = Outer_Syntax.scan (Position.line 42) \"foo\" |
125 | 775 |
in |
426 | 776 |
YXML.parse (fst (Parse.term input)) |
125 | 777 |
end" |
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
778 |
"XML.Elem (\"token\", [(\"line\", \"42\"), (\"end_line\", \"42\")], [XML.Text \"foo\"])"} |
125 | 779 |
|
149 | 780 |
The positional information is stored as part of an XML-tree so that code |
781 |
called later on will be able to give more precise error messages. |
|
125 | 782 |
|
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
783 |
\begin{readmore} |
128 | 784 |
The functions to do with input and output of XML and YXML are defined |
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
785 |
in @{ML_file "Pure/General/xml.ML"} and @{ML_file "Pure/General/yxml.ML"}. |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
786 |
\end{readmore} |
160
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
parents:
156
diff
changeset
|
787 |
|
361 | 788 |
FIXME: |
789 |
@{ML_ind parse_term in Syntax} @{ML_ind check_term in Syntax} |
|
790 |
@{ML_ind parse_typ in Syntax} @{ML_ind check_typ in Syntax} |
|
374 | 791 |
@{ML_ind read_term in Syntax} @{ML_ind read_term in Syntax} |
792 |
||
361 | 793 |
|
125 | 794 |
*} |
101 | 795 |
|
116
c9ff326e3ce5
more changes to the package chapter
Christian Urban <urbanc@in.tum.de>
parents:
114
diff
changeset
|
796 |
section {* Parsing Specifications\label{sec:parsingspecs} *} |
101 | 797 |
|
798 |
text {* |
|
121 | 799 |
There are a number of special purpose parsers that help with parsing |
156 | 800 |
specifications of function definitions, inductive predicates and so on. In |
220 | 801 |
Chapter~\ref{chp:package}, for example, we will need to parse specifications |
121 | 802 |
for inductive predicates of the form: |
803 |
*} |
|
101 | 804 |
|
121 | 805 |
simple_inductive |
806 |
even and odd |
|
807 |
where |
|
808 |
even0: "even 0" |
|
809 |
| evenS: "odd n \<Longrightarrow> even (Suc n)" |
|
810 |
| oddS: "even n \<Longrightarrow> odd (Suc n)" |
|
101 | 811 |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
812 |
|
101 | 813 |
text {* |
121 | 814 |
For this we are going to use the parser: |
101 | 815 |
*} |
816 |
||
121 | 817 |
ML %linenosgray{*val spec_parser = |
426 | 818 |
Parse.fixes -- |
126 | 819 |
Scan.optional |
426 | 820 |
(Parse.$$$ "where" |-- |
821 |
Parse.!!! |
|
822 |
(Parse.enum1 "|" |
|
823 |
(Parse_Spec.opt_thm_name ":" -- Parse.prop))) []*} |
|
120
c39f83d8daeb
some polishing; split up the file External Solver into two
Christian Urban <urbanc@in.tum.de>
parents:
116
diff
changeset
|
824 |
|
101 | 825 |
text {* |
241 | 826 |
Note that the parser must not parse the keyword \simpleinductive, even if it is |
126 | 827 |
meant to process definitions as shown above. The parser of the keyword |
128 | 828 |
will be given by the infrastructure that will eventually call @{ML spec_parser}. |
126 | 829 |
|
830 |
||
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
831 |
To see what the parser returns, let us parse the string corresponding to the |
121 | 832 |
definition of @{term even} and @{term odd}: |
833 |
||
101 | 834 |
@{ML_response [display,gray] |
835 |
"let |
|
836 |
val input = filtered_input |
|
837 |
(\"even and odd \" ^ |
|
838 |
\"where \" ^ |
|
839 |
\" even0[intro]: \\\"even 0\\\" \" ^ |
|
840 |
\"| evenS[intro]: \\\"odd n \<Longrightarrow> even (Suc n)\\\" \" ^ |
|
841 |
\"| oddS[intro]: \\\"even n \<Longrightarrow> odd (Suc n)\\\"\") |
|
842 |
in |
|
120
c39f83d8daeb
some polishing; split up the file External Solver into two
Christian Urban <urbanc@in.tum.de>
parents:
116
diff
changeset
|
843 |
parse spec_parser input |
101 | 844 |
end" |
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents:
183
diff
changeset
|
845 |
"(([(even, NONE, NoSyn), (odd, NONE, NoSyn)], |
101 | 846 |
[((even0,\<dots>), \"\\^E\\^Ftoken\\^Eeven 0\\^E\\^F\\^E\"), |
847 |
((evenS,\<dots>), \"\\^E\\^Ftoken\\^Eodd n \<Longrightarrow> even (Suc n)\\^E\\^F\\^E\"), |
|
848 |
((oddS,\<dots>), \"\\^E\\^Ftoken\\^Eeven n \<Longrightarrow> odd (Suc n)\\^E\\^F\\^E\")]), [])"} |
|
121 | 849 |
|
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents:
183
diff
changeset
|
850 |
As you see, the result is a pair consisting of a list of |
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents:
183
diff
changeset
|
851 |
variables with optional type-annotation and syntax-annotation, and a list of |
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents:
183
diff
changeset
|
852 |
rules where every rule has optionally a name and an attribute. |
121 | 853 |
|
426 | 854 |
The function @{ML_ind "fixes" in Parse} in Line 2 of the parser reads an |
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents:
183
diff
changeset
|
855 |
\isacommand{and}-separated |
124
0b9fa606a746
added to the first-steps section
Christian Urban <urbanc@in.tum.de>
parents:
122
diff
changeset
|
856 |
list of variables that can include optional type annotations and syntax translations. |
121 | 857 |
For example:\footnote{Note that in the code we need to write |
858 |
@{text "\\\"int \<Rightarrow> bool\\\""} in order to properly escape the double quotes |
|
859 |
in the compound type.} |
|
860 |
||
861 |
@{ML_response [display,gray] |
|
862 |
"let |
|
863 |
val input = filtered_input |
|
864 |
\"foo::\\\"int \<Rightarrow> bool\\\" and bar::nat (\\\"BAR\\\" 100) and blonk\" |
|
865 |
in |
|
426 | 866 |
parse Parse.fixes input |
121 | 867 |
end" |
868 |
"([(foo, SOME \"\\^E\\^Ftoken\\^Eint \<Rightarrow> bool\\^E\\^F\\^E\", NoSyn), |
|
869 |
(bar, SOME \"\\^E\\^Ftoken\\^Enat\\^E\\^F\\^E\", Mixfix (\"BAR\", [], 100)), |
|
870 |
(blonk, NONE, NoSyn)],[])"} |
|
50 | 871 |
*} |
872 |
||
121 | 873 |
text {* |
156 | 874 |
Whenever types are given, they are stored in the @{ML SOME}s. The types are |
875 |
not yet used to type the variables: this must be done by type-inference later |
|
149 | 876 |
on. Since types are part of the inner syntax they are strings with some |
241 | 877 |
encoded information (see previous section). If a mixfix-syntax is |
369 | 878 |
present for a variable, then it is stored in the |
371
e6f583366779
solved problem with mixfix.
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
879 |
@{ML Mixfix} data structure; no syntax translation is indicated by @{ML_ind NoSyn in Syntax}. |
121 | 880 |
|
881 |
\begin{readmore} |
|
371
e6f583366779
solved problem with mixfix.
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
882 |
The data structure for mixfix annotations are implemented in |
e6f583366779
solved problem with mixfix.
Christian Urban <urbanc@in.tum.de>
parents:
369
diff
changeset
|
883 |
@{ML_file "Pure/Syntax/mixfix.ML"} and @{ML_file "Pure/Syntax/syntax.ML"}. |
121 | 884 |
\end{readmore} |
885 |
||
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents:
183
diff
changeset
|
886 |
Lines 3 to 7 in the function @{ML spec_parser} implement the parser for a |
219
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
887 |
list of introduction rules, that is propositions with theorem annotations |
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
888 |
such as rule names and attributes. The introduction rules are propositions |
426 | 889 |
parsed by @{ML_ind prop in Parse}. However, they can include an optional |
219
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
890 |
theorem name plus some attributes. For example |
121 | 891 |
|
892 |
@{ML_response [display,gray] "let |
|
893 |
val input = filtered_input \"foo_lemma[intro,dest!]:\" |
|
426 | 894 |
val ((name, attrib), _) = parse (Parse_Spec.thm_name \":\") input |
121 | 895 |
in |
896 |
(name, map Args.dest_src attrib) |
|
897 |
end" "(foo_lemma, [((\"intro\", []), \<dots>), ((\"dest\", [\<dots>]), \<dots>)])"} |
|
898 |
||
426 | 899 |
The function @{ML_ind opt_thm_name in Parse_Spec} is the ``optional'' variant of |
900 |
@{ML_ind thm_name in Parse_Spec}. Theorem names can contain attributes. The name |
|
131 | 901 |
has to end with @{text [quotes] ":"}---see the argument of |
426 | 902 |
the function @{ML Parse_Spec.opt_thm_name} in Line 7. |
121 | 903 |
|
904 |
\begin{readmore} |
|
905 |
Attributes and arguments are implemented in the files @{ML_file "Pure/Isar/attrib.ML"} |
|
906 |
and @{ML_file "Pure/Isar/args.ML"}. |
|
907 |
\end{readmore} |
|
101 | 908 |
*} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
909 |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
910 |
text_raw {* |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
911 |
\begin{exercise} |
426 | 912 |
Have a look at how the parser @{ML Parse_Spec.where_alt_specs} is implemented |
424 | 913 |
in file @{ML_file "Pure/Isar/parse_spec.ML"}. This parser corresponds |
207 | 914 |
to the ``where-part'' of the introduction rules given above. Below |
426 | 915 |
we paraphrase the code of @{ML_ind where_alt_specs in Parse_Spec} adapted to our |
207 | 916 |
purposes. |
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
917 |
\begin{isabelle} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
918 |
*} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
919 |
ML %linenosgray{*val spec_parser' = |
426 | 920 |
Parse.fixes -- |
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
921 |
Scan.optional |
426 | 922 |
(Parse.$$$ "where" |-- |
923 |
Parse.!!! |
|
924 |
(Parse.enum1 "|" |
|
925 |
((Parse_Spec.opt_thm_name ":" -- Parse.prop) --| |
|
926 |
Scan.option (Scan.ahead (Parse.name || |
|
927 |
Parse.$$$ "[") -- |
|
928 |
Parse.!!! (Parse.$$$ "|"))))) [] *} |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
929 |
text_raw {* |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
930 |
\end{isabelle} |
284 | 931 |
Both parsers accept the same input% that's not true: |
932 |
% spec_parser accepts input that is refuted by spec_parser' |
|
933 |
, but if you look closely, you can notice |
|
207 | 934 |
an additional ``tail'' (Lines 8 to 10) in @{ML spec_parser'}. What is the purpose of |
935 |
this additional ``tail''? |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
936 |
\end{exercise} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
937 |
*} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
938 |
|
229
abc7f90188af
permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents:
228
diff
changeset
|
939 |
text {* |
426 | 940 |
(FIXME: @{ML Parse.type_args}, @{ML Parse.typ}, @{ML Parse.opt_mixfix}) |
229
abc7f90188af
permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents:
228
diff
changeset
|
941 |
*} |
abc7f90188af
permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents:
228
diff
changeset
|
942 |
|
abc7f90188af
permutation example uses now recent infrastructure
Christian Urban <urbanc@in.tum.de>
parents:
228
diff
changeset
|
943 |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
944 |
section {* New Commands and Keyword Files\label{sec:newcommand} *} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
945 |
|
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
946 |
text {* |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
947 |
Often new commands, for example for providing new definitional principles, |
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
948 |
need to be implemented. While this is not difficult on the ML-level, |
66 | 949 |
new commands, in order to be useful, need to be recognised by |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
950 |
ProofGeneral. This results in some subtle configuration issues, which we |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
951 |
will explain in this section. |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
952 |
|
74 | 953 |
To keep things simple, let us start with a ``silly'' command that does nothing |
954 |
at all. We shall name this command \isacommand{foobar}. On the ML-level it can be |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
955 |
defined as: |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
956 |
*} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
957 |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
958 |
ML{*let |
394 | 959 |
val do_nothing = Scan.succeed (Local_Theory.theory I) |
426 | 960 |
val kind = Keyword.thy_decl |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
961 |
in |
426 | 962 |
Outer_Syntax.local_theory "foobar" "description of foobar" kind do_nothing |
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
963 |
end *} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
964 |
|
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
965 |
text {* |
426 | 966 |
The crucial function @{ML_ind local_theory in Outer_Syntax} expects a name for the command, a |
219
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
967 |
short description, a kind indicator (which we will explain later more thoroughly) and a |
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
968 |
parser producing a local theory transition (its purpose will also explained |
66 | 969 |
later). |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
970 |
|
101 | 971 |
While this is everything you have to do on the ML-level, you need a keyword |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
972 |
file that can be loaded by ProofGeneral. This is to enable ProofGeneral to |
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
973 |
recognise \isacommand{foobar} as a command. Such a keyword file can be |
74 | 974 |
generated with the command-line: |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
975 |
|
74 | 976 |
@{text [display] "$ isabelle keywords -k foobar some_log_files"} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
977 |
|
74 | 978 |
The option @{text "-k foobar"} indicates which postfix the name of the keyword file |
80 | 979 |
will be assigned. In the case above the file will be named @{text |
86 | 980 |
"isar-keywords-foobar.el"}. This command requires log files to be |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
981 |
present (in order to extract the keywords from them). To generate these log |
101 | 982 |
files, you first need to package the code above into a separate theory file named |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
983 |
@{text "Command.thy"}, say---see Figure~\ref{fig:commandtheory} for the |
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
984 |
complete code. |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
985 |
|
66 | 986 |
|
987 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
988 |
\begin{figure}[t] |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
989 |
\begin{graybox}\small |
66 | 990 |
\isacommand{theory}~@{text Command}\\ |
991 |
\isacommand{imports}~@{text Main}\\ |
|
992 |
\isacommand{begin}\\ |
|
85
b02904872d6b
better handling of {* and *}
Christian Urban <urbanc@in.tum.de>
parents:
81
diff
changeset
|
993 |
\isacommand{ML}~@{text "\<verbopen>"}\\ |
66 | 994 |
@{ML |
995 |
"let |
|
394 | 996 |
val do_nothing = Scan.succeed (Local_Theory.theory I) |
426 | 997 |
val kind = Keyword.thy_decl |
66 | 998 |
in |
426 | 999 |
Outer_Syntax.local_theory \"foobar\" \"description of foobar\" kind do_nothing |
66 | 1000 |
end"}\\ |
85
b02904872d6b
better handling of {* and *}
Christian Urban <urbanc@in.tum.de>
parents:
81
diff
changeset
|
1001 |
@{text "\<verbclose>"}\\ |
66 | 1002 |
\isacommand{end} |
80 | 1003 |
\end{graybox} |
241 | 1004 |
\caption{This file can be used to generate a log file. This log file in turn can |
1005 |
be used to generate a keyword file containing the command \isacommand{foobar}. |
|
1006 |
\label{fig:commandtheory}} |
|
66 | 1007 |
\end{figure} |
1008 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
1009 |
||
75 | 1010 |
For our purposes it is sufficient to use the log files of the theories |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1011 |
@{text "Pure"}, @{text "HOL"} and @{text "Pure-ProofGeneral"}, as well as |
75 | 1012 |
the log file for the theory @{text "Command.thy"}, which contains the new |
1013 |
\isacommand{foobar}-command. If you target other logics besides HOL, such |
|
74 | 1014 |
as Nominal or ZF, then you need to adapt the log files appropriately. |
104 | 1015 |
|
74 | 1016 |
@{text Pure} and @{text HOL} are usually compiled during the installation of |
1017 |
Isabelle. So log files for them should be already available. If not, then |
|
75 | 1018 |
they can be conveniently compiled with the help of the build-script from the Isabelle |
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
1019 |
distribution. |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1020 |
|
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1021 |
@{text [display] |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1022 |
"$ ./build -m \"Pure\" |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1023 |
$ ./build -m \"HOL\""} |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1024 |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
1025 |
The @{text "Pure-ProofGeneral"} theory needs to be compiled with: |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1026 |
|
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1027 |
@{text [display] "$ ./build -m \"Pure-ProofGeneral\" \"Pure\""} |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1028 |
|
101 | 1029 |
For the theory @{text "Command.thy"}, you first need to create a ``managed'' subdirectory |
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
1030 |
with: |
66 | 1031 |
|
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1032 |
@{text [display] "$ isabelle mkdir FoobarCommand"} |
66 | 1033 |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
1034 |
This generates a directory containing the files: |
66 | 1035 |
|
1036 |
@{text [display] |
|
1037 |
"./IsaMakefile |
|
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1038 |
./FoobarCommand/ROOT.ML |
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1039 |
./FoobarCommand/document |
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1040 |
./FoobarCommand/document/root.tex"} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1041 |
|
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1042 |
|
101 | 1043 |
You need to copy the file @{text "Command.thy"} into the directory @{text "FoobarCommand"} |
66 | 1044 |
and add the line |
1045 |
||
207 | 1046 |
@{text [display] "no_document use_thy \"Command\";"} |
66 | 1047 |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
1048 |
to the file @{text "./FoobarCommand/ROOT.ML"}. You can now compile the theory by just typing: |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1049 |
|
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1050 |
@{text [display] "$ isabelle make"} |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1051 |
|
101 | 1052 |
If the compilation succeeds, you have finally created all the necessary log files. |
1053 |
They are stored in the directory |
|
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1054 |
|
241 | 1055 |
@{text [display] "~/.isabelle/heaps/Isabelle2009/polyml-5.2.1_x86-linux/log"} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1056 |
|
74 | 1057 |
or something similar depending on your Isabelle distribution and architecture. |
1058 |
One quick way to assign a shell variable to this directory is by typing |
|
66 | 1059 |
|
1060 |
@{text [display] "$ ISABELLE_LOGS=\"$(isabelle getenv -b ISABELLE_OUTPUT)\"/log"} |
|
1061 |
||
156 | 1062 |
on the Unix prompt. If you now type @{text "ls $ISABELLE_LOGS"}, then the |
128 | 1063 |
directory should include the files: |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1064 |
|
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1065 |
@{text [display] |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1066 |
"Pure.gz |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1067 |
HOL.gz |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1068 |
Pure-ProofGeneral.gz |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1069 |
HOL-FoobarCommand.gz"} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1070 |
|
101 | 1071 |
From them you can create the keyword files. Assuming the name |
75 | 1072 |
of the directory is in @{text "$ISABELLE_LOGS"}, |
74 | 1073 |
then the Unix command for creating the keyword file is: |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1074 |
|
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1075 |
@{text [display] |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1076 |
"$ isabelle keywords -k foobar |
80 | 1077 |
$ISABELLE_LOGS/{Pure.gz,HOL.gz,Pure-ProofGeneral.gz,HOL-FoobarCommand.gz}"} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1078 |
|
80 | 1079 |
The result is the file @{text "isar-keywords-foobar.el"}. It should contain |
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1080 |
the string @{text "foobar"} twice.\footnote{To see whether things are fine, |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1081 |
check that @{text "grep foobar"} on this file returns something non-empty.} |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1082 |
This keyword file needs to be copied into the directory @{text |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1083 |
"~/.isabelle/etc"}. To make ProofGeneral aware of it, you have to start |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1084 |
Isabelle with the option @{text "-k foobar"}, that is: |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1085 |
|
80 | 1086 |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
1087 |
@{text [display] "$ isabelle emacs -k foobar a_theory_file"} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1088 |
|
101 | 1089 |
If you now build a theory on top of @{text "Command.thy"}, |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
1090 |
then you can use the command \isacommand{foobar}. You can just write |
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1091 |
*} |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1092 |
|
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1093 |
foobar |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1094 |
|
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1095 |
text {* |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1096 |
but you will not see any action as we chose to implement this command to do |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1097 |
nothing. The point of this command is only to show the procedure of how |
326
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
1098 |
to interact with ProofGeneral. A similar procedure has to be done with any |
f76135c6c527
more work on the tutorial
Christian Urban <urbanc@in.tum.de>
parents:
324
diff
changeset
|
1099 |
other new command, and also any new keyword that is introduced with |
426 | 1100 |
the function @{ML_ind keyword in Keyword}. For example: |
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
1101 |
*} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1102 |
|
426 | 1103 |
ML{*val _ = Keyword.keyword "blink" *} |
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1104 |
|
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
parents:
229
diff
changeset
|
1105 |
text {* |
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1106 |
At the moment the command \isacommand{foobar} is not very useful. Let us |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1107 |
refine it a bit next by letting it take a proposition as argument and |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1108 |
printing this proposition inside the tracing buffer. |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1109 |
|
75 | 1110 |
The crucial part of a command is the function that determines the behaviour |
1111 |
of the command. In the code above we used a ``do-nothing''-function, which |
|
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
1112 |
because of @{ML_ind succeed in Scan} does not parse any argument, but immediately |
394 | 1113 |
returns the simple function @{ML "Local_Theory.theory I"}. We can |
75 | 1114 |
replace this code by a function that first parses a proposition (using the |
426 | 1115 |
parser @{ML Parse.prop}), then prints out the tracing |
219
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
1116 |
information (using a new function @{text trace_prop}) and |
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
1117 |
finally does nothing. For this you can write: |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1118 |
*} |
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1119 |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
1120 |
ML{*let |
219
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
1121 |
fun trace_prop str = |
394 | 1122 |
Local_Theory.theory (fn ctxt => (tracing str; ctxt)) |
75 | 1123 |
|
426 | 1124 |
val kind = Keyword.thy_decl |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1125 |
in |
426 | 1126 |
Outer_Syntax.local_theory "foobar_trace" "traces a proposition" |
1127 |
kind (Parse.prop >> trace_prop) |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
1128 |
end *} |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1129 |
|
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1130 |
text {* |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1131 |
The command is now \isacommand{foobar\_trace} and can be used to |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1132 |
see the proposition in the tracing buffer. |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1133 |
*} |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1134 |
|
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1135 |
foobar_trace "True \<and> False" |
218
7ff7325e3b4e
started to adapt the rest of chapter 5 to the simplified version without parameters (they will be described in the extension section)
Christian Urban <urbanc@in.tum.de>
parents:
211
diff
changeset
|
1136 |
|
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1137 |
text {* |
426 | 1138 |
Note that so far we used @{ML_ind thy_decl in Keyword} as the kind |
219
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
1139 |
indicator for the command. This means that the command finishes as soon as |
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
1140 |
the arguments are processed. Examples of this kind of commands are |
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
1141 |
\isacommand{definition} and \isacommand{declare}. In other cases, commands |
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
1142 |
are expected to parse some arguments, for example a proposition, and then |
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
1143 |
``open up'' a proof in order to prove the proposition (for example |
86 | 1144 |
\isacommand{lemma}) or prove some other properties (for example |
219
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
1145 |
\isacommand{function}). To achieve this kind of behaviour, you have to use |
426 | 1146 |
the kind indicator @{ML_ind thy_goal in Keyword} and the function @{ML |
1147 |
"local_theory_to_proof" in Outer_Syntax} to set up the command. Note, |
|
219
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
1148 |
however, once you change the ``kind'' of a command from @{ML thy_decl in |
426 | 1149 |
Keyword} to @{ML thy_goal in Keyword} then the keyword file needs |
219
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
1150 |
to be re-created! |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1151 |
|
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1152 |
Below we show the command \isacommand{foobar\_goal} which takes a |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1153 |
proposition as argument and then starts a proof in order to prove |
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1154 |
it. Therefore in Line 9, we set the kind indicator to @{ML thy_goal in |
426 | 1155 |
Keyword}. |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1156 |
*} |
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1157 |
|
114
13fd0a83d3c3
properly handled linenumbers in ML-text and Isar-proofs
Christian Urban <urbanc@in.tum.de>
parents:
108
diff
changeset
|
1158 |
ML%linenosgray{*let |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1159 |
fun goal_prop str lthy = |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1160 |
let |
241 | 1161 |
val prop = Syntax.read_prop lthy str |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1162 |
in |
422 | 1163 |
Proof.theorem NONE (K I) [[(prop, [])]] lthy |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1164 |
end |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1165 |
|
426 | 1166 |
val kind = Keyword.thy_goal |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1167 |
in |
426 | 1168 |
Outer_Syntax.local_theory_to_proof "foobar_goal" "proves a proposition" |
1169 |
kind (Parse.prop >> goal_prop) |
|
69
19106a9975c1
highligted the background of ML-code
Christian Urban <urbanc@in.tum.de>
parents:
68
diff
changeset
|
1170 |
end *} |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1171 |
|
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1172 |
text {* |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1173 |
The function @{text goal_prop} in Lines 2 to 7 takes a string (the proposition to be |
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
1174 |
proved) and a context as argument. The context is necessary in order to be able to use |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
1175 |
@{ML_ind read_prop in Syntax}, which converts a string into a proper proposition. |
422 | 1176 |
In Line 6 the function @{ML_ind theorem in Proof} starts the proof for the |
75 | 1177 |
proposition. Its argument @{ML NONE} stands for a locale (which we chose to |
1178 |
omit); the argument @{ML "(K I)"} stands for a function that determines what |
|
1179 |
should be done with the theorem once it is proved (we chose to just forget |
|
219
98d43270024f
more work on the simple inductive chapter
Christian Urban <urbanc@in.tum.de>
parents:
218
diff
changeset
|
1180 |
about it). Line 9 contains the parser for the proposition. |
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
101
diff
changeset
|
1181 |
|
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1182 |
If you now type \isacommand{foobar\_goal}~@{text [quotes] "True \<and> True"}, |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1183 |
you obtain the following proof state |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1184 |
*} |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1185 |
|
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1186 |
foobar_goal "True \<and> True" |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1187 |
txt {* |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1188 |
\begin{minipage}{\textwidth} |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1189 |
@{subgoals [display]} |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1190 |
\end{minipage}\medskip |
68
e7519207c2b7
added more to the "new command section" and tuning
Christian Urban <urbanc@in.tum.de>
parents:
67
diff
changeset
|
1191 |
|
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1192 |
and can prove the proposition as follows. |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1193 |
*} |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1194 |
apply(rule conjI) |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1195 |
apply(rule TrueI)+ |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1196 |
done |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1197 |
|
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1198 |
text {* |
327
ce754ad78bc9
more work on the storing section
Christian Urban <urbanc@in.tum.de>
parents:
326
diff
changeset
|
1199 |
{\bf TBD below} |
74 | 1200 |
|
394 | 1201 |
(FIXME: read a name and show how to store theorems; see @{ML_ind note in Local_Theory}) |
241 | 1202 |
|
65
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1203 |
*} |
c8e9a4f97916
tuned and added a section about creating keyword files
Christian Urban <urbanc@in.tum.de>
parents:
60
diff
changeset
|
1204 |
|
328
c0cae24b9d46
updated to new Isabelle; more work on the data section
Christian Urban <urbanc@in.tum.de>
parents:
327
diff
changeset
|
1205 |
ML_val{*val r = Unsynchronized.ref (NONE:(unit -> term) option)*} |
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1206 |
ML{*let |
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
322
diff
changeset
|
1207 |
fun after_qed thm_name thms lthy = |
394 | 1208 |
Local_Theory.note (thm_name, (flat thms)) lthy |> snd |
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
322
diff
changeset
|
1209 |
|
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
322
diff
changeset
|
1210 |
fun setup_proof (thm_name, (txt, pos)) lthy = |
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1211 |
let |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1212 |
val trm = ML_Context.evaluate lthy true ("r", r) txt |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1213 |
in |
422 | 1214 |
Proof.theorem NONE (after_qed thm_name) [[(trm,[])]] lthy |
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
322
diff
changeset
|
1215 |
end |
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1216 |
|
426 | 1217 |
val parser = Parse_Spec.opt_thm_name ":" -- Parse.ML_source |
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1218 |
in |
426 | 1219 |
Outer_Syntax.local_theory_to_proof "foobar_prove" "proving a proposition" |
1220 |
Keyword.thy_goal (parser >> setup_proof) |
|
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1221 |
end*} |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1222 |
|
324
4172c0743cf2
updated foobar_proof example
Christian Urban <urbanc@in.tum.de>
parents:
322
diff
changeset
|
1223 |
foobar_prove test: {* @{prop "True"} *} |
321
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1224 |
apply(rule TrueI) |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1225 |
done |
e450fa467e3f
polished the commands section
Christian Urban <urbanc@in.tum.de>
parents:
319
diff
changeset
|
1226 |
|
322 | 1227 |
(* |
1228 |
ML {* |
|
1229 |
structure TacticData = ProofDataFun |
|
1230 |
( |
|
1231 |
type T = thm list -> tactic; |
|
1232 |
fun init _ = undefined; |
|
366 | 1233 |
) |
322 | 1234 |
|
1235 |
val set_tactic = TacticData.put; |
|
1236 |
*} |
|
1237 |
||
1238 |
ML {* |
|
1239 |
TacticData.get @{context} |
|
1240 |
*} |
|
1241 |
||
1242 |
ML {* Method.set_tactic *} |
|
1243 |
ML {* fun tactic (facts: thm list) : tactic = (atac 1) *} |
|
1244 |
ML {* Context.map_proof *} |
|
1245 |
ML {* ML_Context.expression *} |
|
1246 |
ML {* METHOD *} |
|
1247 |
||
1248 |
||
1249 |
ML {* |
|
1250 |
fun myexpression pos bind body txt = |
|
1251 |
let |
|
1252 |
val _ = tracing ("bind)" ^ bind) |
|
1253 |
val _ = tracing ("body)" ^ body) |
|
1254 |
val _ = tracing ("txt)" ^ txt) |
|
1255 |
val _ = tracing ("result) " ^ "Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^ |
|
1256 |
" end (ML_Context.the_generic_context ())));") |
|
1257 |
in |
|
1258 |
ML_Context.exec (fn () => ML_Context.eval false pos |
|
1259 |
("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^ |
|
1260 |
" end (ML_Context.the_generic_context ())));")) |
|
1261 |
end |
|
1262 |
*} |
|
319
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
316
diff
changeset
|
1263 |
|
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
316
diff
changeset
|
1264 |
|
322 | 1265 |
ML {* |
1266 |
fun ml_tactic (txt, pos) ctxt = |
|
1267 |
let |
|
1268 |
val ctxt' = ctxt |> Context.proof_map |
|
1269 |
(myexpression pos |
|
1270 |
"fun tactic (facts: thm list) : tactic" |
|
1271 |
"Context.map_proof (Method.set_tactic tactic)" txt); |
|
1272 |
in |
|
1273 |
Context.setmp_thread_data (SOME (Context.Proof ctxt)) (TacticData.get ctxt') |
|
1274 |
end; |
|
1275 |
*} |
|
1276 |
||
1277 |
ML {* |
|
1278 |
fun tactic3 (txt, pos) ctxt = |
|
1279 |
let |
|
1280 |
val _ = tracing ("1) " ^ txt ) |
|
1281 |
in |
|
1282 |
METHOD (ml_tactic (txt, pos) ctxt; K (atac 1)) |
|
1283 |
end |
|
1284 |
*} |
|
1285 |
||
1286 |
setup {* |
|
426 | 1287 |
Method.setup (Binding.name "tactic3") (Scan.lift (Parse.position Args.name) |
322 | 1288 |
>> tactic3) |
1289 |
"ML tactic as proof method" |
|
1290 |
*} |
|
1291 |
||
1292 |
lemma "A \<Longrightarrow> A" |
|
1293 |
apply(tactic3 {* (atac 1) *}) |
|
1294 |
done |
|
1295 |
||
1296 |
ML {* |
|
1297 |
(ML_Context.the_generic_context ()) |
|
1298 |
*} |
|
1299 |
||
1300 |
ML {* |
|
1301 |
Context.set_thread_data; |
|
1302 |
ML_Context.the_generic_context |
|
1303 |
*} |
|
1304 |
||
1305 |
lemma "A \<Longrightarrow> A" |
|
1306 |
ML_prf {* |
|
1307 |
Context.set_thread_data (SOME (let fun tactic (facts: thm list) : tactic = (atac 1) in Context.map_proof (Method.set_tactic tactic) end (ML_Context.the_generic_context ()))); |
|
1308 |
*} |
|
1309 |
||
1310 |
ML {* |
|
1311 |
Context.set_thread_data (SOME ((let fun tactic (facts: thm list) : tactic = (atac 1) in 3 end) (ML_Context.the_generic_context ()))); |
|
1312 |
*} |
|
1313 |
||
1314 |
ML {* |
|
1315 |
Context.set_thread_data (SOME (let |
|
1316 |
fun tactic (facts: thm list) : tactic = (atac 1) |
|
1317 |
in |
|
1318 |
Context.map_proof (Method.set_tactic tactic) |
|
1319 |
end |
|
1320 |
(ML_Context.the_generic_context ()))); |
|
1321 |
*} |
|
1322 |
||
1323 |
||
1324 |
ML {* |
|
1325 |
let |
|
1326 |
fun tactic (facts: thm list) : tactic = atac |
|
1327 |
in |
|
1328 |
Context.map_proof (Method.set_tactic tactic) |
|
1329 |
end *} |
|
1330 |
||
1331 |
end *} |
|
1332 |
||
1333 |
ML {* Toplevel.program (fn () => |
|
1334 |
(ML_Context.expression Position.none "val plus : int" "3 + 4" "1" (Context.Proof @{context})))*} |
|
1335 |
||
1336 |
||
1337 |
ML {* |
|
1338 |
fun ml_tactic (txt, pos) ctxt = |
|
1339 |
let |
|
1340 |
val ctxt' = ctxt |> Context.proof_map |
|
1341 |
(ML_Context.expression pos |
|
1342 |
"fun tactic (facts: thm list) : tactic" |
|
1343 |
"Context.map_proof (Method.set_tactic tactic)" txt); |
|
1344 |
in Context.setmp_thread_data (SOME (Context.Proof ctxt)) (TacticData.get ctxt') end; |
|
1345 |
||
1346 |
*} |
|
1347 |
||
1348 |
ML {* |
|
1349 |
Context.set_thread_data (SOME (let fun tactic (facts: thm list) : tactic = (atac 1) in Context.map_proof (Method.set_tactic tactic) end (ML_Context.the_generic_context ()))); |
|
1350 |
*} |
|
1351 |
*) |
|
319
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
316
diff
changeset
|
1352 |
|
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
parents:
207
diff
changeset
|
1353 |
section {* Methods (TBD) *} |
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1354 |
|
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1355 |
text {* |
207 | 1356 |
(FIXME: maybe move to after the tactic section) |
1357 |
||
221 | 1358 |
Methods are central to Isabelle. They are the ones you use for example |
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents:
183
diff
changeset
|
1359 |
in \isacommand{apply}. To print out all currently known methods you can use the |
192 | 1360 |
Isabelle command: |
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1361 |
|
207 | 1362 |
\begin{isabelle} |
1363 |
\isacommand{print\_methods}\\ |
|
1364 |
@{text "> methods:"}\\ |
|
1365 |
@{text "> -: do nothing (insert current facts only)"}\\ |
|
1366 |
@{text "> HOL.default: apply some intro/elim rule (potentially classical)"}\\ |
|
1367 |
@{text "> ..."} |
|
1368 |
\end{isabelle} |
|
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1369 |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
1370 |
An example of a very simple method is: |
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1371 |
*} |
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1372 |
|
244
dc95a56b1953
fixed the problem with double definition of even and odd
Christian Urban <urbanc@in.tum.de>
parents:
241
diff
changeset
|
1373 |
method_setup %gray foo = |
181
5baaabe1ab92
updated to new method_setup
Christian Urban <urbanc@in.tum.de>
parents:
178
diff
changeset
|
1374 |
{* Scan.succeed |
5baaabe1ab92
updated to new method_setup
Christian Urban <urbanc@in.tum.de>
parents:
178
diff
changeset
|
1375 |
(K (SIMPLE_METHOD ((etac @{thm conjE} THEN' rtac @{thm conjI}) 1))) *} |
244
dc95a56b1953
fixed the problem with double definition of even and odd
Christian Urban <urbanc@in.tum.de>
parents:
241
diff
changeset
|
1376 |
"foo method for conjE and conjI" |
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1377 |
|
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1378 |
text {* |
286 | 1379 |
It defines the method @{text foo}, which takes no arguments (therefore the |
207 | 1380 |
parser @{ML Scan.succeed}) and only applies a single tactic, namely the tactic which |
256
1fb8d62c88a0
added some first index-information
Christian Urban <urbanc@in.tum.de>
parents:
250
diff
changeset
|
1381 |
applies @{thm [source] conjE} and then @{thm [source] conjI}. The function |
344
83d5bca38bec
added structures in the index
Christian Urban <urbanc@in.tum.de>
parents:
328
diff
changeset
|
1382 |
@{ML_ind SIMPLE_METHOD in Method} |
287 | 1383 |
turns such a tactic into a method. The method @{text "foo"} can be used as follows |
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1384 |
*} |
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1385 |
|
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1386 |
lemma shows "A \<and> B \<Longrightarrow> C \<and> D" |
244
dc95a56b1953
fixed the problem with double definition of even and odd
Christian Urban <urbanc@in.tum.de>
parents:
241
diff
changeset
|
1387 |
apply(foo) |
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1388 |
txt {* |
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1389 |
where it results in the goal state |
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1390 |
|
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1391 |
\begin{minipage}{\textwidth} |
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1392 |
@{subgoals} |
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1393 |
\end{minipage} *} |
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1394 |
(*<*)oops(*>*) |
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1395 |
|
421
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1396 |
method_setup test = {* Scan.lift (Scan.succeed (K Method.succeed)) *} {* bla *} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1397 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1398 |
lemma "True" |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1399 |
apply(test) |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1400 |
oops |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1401 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1402 |
method_setup joker = {* Scan.lift (Scan.succeed (fn ctxt => Method.cheating true ctxt)) *} {* bla *} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1403 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1404 |
lemma "False" |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1405 |
apply(joker) |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1406 |
oops |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1407 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1408 |
text {* if true is set then always works *} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1409 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1410 |
ML {* atac *} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1411 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1412 |
method_setup first_atac = {* Scan.lift (Scan.succeed (K (SIMPLE_METHOD (atac 1)))) *} {* bla *} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1413 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1414 |
ML {* HEADGOAL *} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1415 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1416 |
lemma "A \<Longrightarrow> A" |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1417 |
apply(first_atac) |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1418 |
oops |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1419 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1420 |
method_setup my_atac = {* Scan.lift (Scan.succeed (K (SIMPLE_METHOD' atac))) *} {* bla *} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1421 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1422 |
lemma "A \<Longrightarrow> A" |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1423 |
apply(my_atac) |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1424 |
oops |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1425 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1426 |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
1427 |
|
319
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
316
diff
changeset
|
1428 |
|
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
316
diff
changeset
|
1429 |
|
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
316
diff
changeset
|
1430 |
|
6bce4acf7f2a
added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents:
316
diff
changeset
|
1431 |
|
193
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
1432 |
(* |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
1433 |
ML {* SIMPLE_METHOD *} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
1434 |
ML {* METHOD *} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
1435 |
ML {* K (SIMPLE_METHOD ((etac @{thm conjE} THEN' rtac @{thm conjI}) 1)) *} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
1436 |
ML {* Scan.succeed *} |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
1437 |
*) |
ffd93dcc269d
polishing to the theorem attributes section
Christian Urban <urbanc@in.tum.de>
parents:
192
diff
changeset
|
1438 |
|
421
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1439 |
ML {* resolve_tac *} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1440 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1441 |
method_setup myrule = |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1442 |
{* Scan.lift (Scan.succeed (K (METHOD (fn thms => resolve_tac thms 1)))) *} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1443 |
{* bla *} |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1444 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1445 |
lemma |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1446 |
assumes a: "A \<Longrightarrow> B \<Longrightarrow> C" |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1447 |
shows "C" |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1448 |
using a |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1449 |
apply(myrule) |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1450 |
oops |
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1451 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1452 |
|
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1453 |
|
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents:
183
diff
changeset
|
1454 |
text {* |
421
620a24bf954a
added a section to the introduction; described @{make_string}
Christian Urban <urbanc@in.tum.de>
parents:
414
diff
changeset
|
1455 |
(********************************************************) |
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents:
183
diff
changeset
|
1456 |
(FIXME: explain a version of rule-tac) |
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
parents:
183
diff
changeset
|
1457 |
*} |
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
parents:
177
diff
changeset
|
1458 |
|
220 | 1459 |
end |