4
+ − 1
theory Parsing
121
+ − 2
imports Base "Package/Simple_Inductive_Package"
4
+ − 3
begin
+ − 4
+ − 5
chapter {* Parsing *}
+ − 6
+ − 7
text {*
+ − 8
38
+ − 9
Isabelle distinguishes between \emph{outer} and \emph{inner} syntax.
236
+ − 10
Commands, such as \isacommand{definition}, \isacommand{inductive} and so
+ − 11
on, belong to the outer syntax, whereas terms, types and so on belong to the
+ − 12
inner syntax. For parsing inner syntax,
156
+ − 13
Isabelle uses a rather general and sophisticated algorithm, which
38
+ − 14
is driven by priority grammars. Parsers for outer syntax are built up by functional
+ − 15
parsing combinators. These combinators are a well-established technique for parsing,
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 16
which has, for example, been described in Paulson's classic ML-book \cite{paulson-ml2}.
42
+ − 17
Isabelle developers are usually concerned with writing these outer syntax parsers,
193
+ − 18
either for new definitional packages or for calling methods with specific arguments.
42
+ − 19
+ − 20
\begin{readmore}
236
+ − 21
The library for writing parser combinators is split up, roughly, into two
+ − 22
parts. The first part consists of a collection of generic parser combinators
+ − 23
defined in the structure @{ML_struct Scan} in the file @{ML_file
+ − 24
"Pure/General/scan.ML"}. The second part of the library consists of
+ − 25
combinators for dealing with specific token types, which are defined in the
+ − 26
structure @{ML_struct OuterParse} in the file @{ML_file
+ − 27
"Pure/Isar/outer_parse.ML"}. Specific parsers for packages are defined
+ − 28
in @{ML_file "Pure/Isar/spec_parse.ML"}. Parsers for method arguments
+ − 29
are defined in @{ML_file "Pure/Isar/args.ML"}.
42
+ − 30
\end{readmore}
38
+ − 31
+ − 32
*}
+ − 33
49
+ − 34
section {* Building Generic Parsers *}
38
+ − 35
+ − 36
text {*
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 37
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 38
Let us first have a look at parsing strings using generic parsing combinators.
108
8bea3f74889d
added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 39
The function @{ML "$$"} takes a string as argument and will ``consume'' this string from
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 40
a given input list of strings. ``Consume'' in this context means that it will
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 41
return a pair consisting of this string and the rest of the input list.
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 42
For example:
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 43
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 44
@{ML_response [display,gray] "($$ \"h\") (explode \"hello\")" "(\"h\", [\"e\", \"l\", \"l\", \"o\"])"}
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 45
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 46
@{ML_response [display,gray] "($$ \"w\") (explode \"world\")" "(\"w\", [\"o\", \"r\", \"l\", \"d\"])"}
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 47
149
+ − 48
The function @{ML "$$"} will either succeed (as in the two examples above) or raise the exception
58
+ − 49
@{text "FAIL"} if no string can be consumed. For example trying to parse
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 50
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 51
@{ML_response_fake [display,gray] "($$ \"x\") (explode \"world\")"
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 52
"Exception FAIL raised"}
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 53
58
+ − 54
will raise the exception @{text "FAIL"}.
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 55
There are three exceptions used in the parsing combinators:
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 56
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 57
\begin{itemize}
58
+ − 58
\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>
diff
changeset
+ − 59
might be explored.
58
+ − 60
\item @{text "MORE"} indicates that there is not enough input for the parser. For example
+ − 61
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>
diff
changeset
+ − 62
\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>
diff
changeset
+ − 63
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>
diff
changeset
+ − 64
\end{itemize}
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 65
50
+ − 66
However, note that these exceptions are private to the parser and cannot be accessed
49
+ − 67
by the programmer (for example to handle them).
+ − 68
108
8bea3f74889d
added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 69
Slightly more general than the parser @{ML "$$"} is the function @{ML
49
+ − 70
Scan.one}, in that it takes a predicate as argument and then parses exactly
52
+ − 71
one item from the input list satisfying this predicate. For example the
58
+ − 72
following parser either consumes an @{text [quotes] "h"} or a @{text
49
+ − 73
[quotes] "w"}:
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 74
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 75
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 76
@{ML_response [display,gray]
40
+ − 77
"let
+ − 78
val hw = Scan.one (fn x => x = \"h\" orelse x = \"w\")
236
+ − 79
val input1 = explode \"hello\"
+ − 80
val input2 = explode \"world\"
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 81
in
236
+ − 82
(hw input1, hw input2)
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 83
end"
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 84
"((\"h\", [\"e\", \"l\", \"l\", \"o\"]),(\"w\", [\"o\", \"r\", \"l\", \"d\"]))"}
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 85
220
+ − 86
Two parsers can be connected in sequence by using the function @{ML "--"}.
+ − 87
For example parsing @{text "h"}, @{text "e"} and @{text "l"} (in this
+ − 88
order) you can achieve by:
38
+ − 89
236
+ − 90
@{ML_response [display,gray]
+ − 91
"($$ \"h\" -- $$ \"e\" -- $$ \"l\") (explode \"hello\")"
+ − 92
"(((\"h\", \"e\"), \"l\"), [\"l\", \"o\"])"}
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 93
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 94
Note how the result of consumed strings builds up on the left as nested pairs.
38
+ − 95
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 96
If, as in the previous example, you want to parse a particular string,
128
+ − 97
then you should use the function @{ML Scan.this_string}:
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 98
236
+ − 99
@{ML_response [display,gray]
+ − 100
"Scan.this_string \"hell\" (explode \"hello\")"
+ − 101
"(\"hell\", [\"o\"])"}
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 102
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 103
Parsers that explore alternatives can be constructed using the function @{ML
220
+ − 104
"||"}. The parser @{ML "(p || q)" for p q} returns the
58
+ − 105
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>
diff
changeset
+ − 106
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>
diff
changeset
+ − 107
38
+ − 108
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 109
@{ML_response [display,gray]
40
+ − 110
"let
236
+ − 111
val hw = $$ \"h\" || $$ \"w\"
+ − 112
val input1 = explode \"hello\"
+ − 113
val input2 = explode \"world\"
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 114
in
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 115
(hw input1, hw input2)
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 116
end"
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 117
"((\"h\", [\"e\", \"l\", \"l\", \"o\"]), (\"w\", [\"o\", \"r\", \"l\", \"d\"]))"}
38
+ − 118
108
8bea3f74889d
added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 119
The functions @{ML "|--"} and @{ML "--|"} work like the sequencing function
50
+ − 120
for parsers, except that they discard the item being parsed by the first (respectively second)
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 121
parser. For example:
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 122
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 123
@{ML_response [display,gray]
40
+ − 124
"let
236
+ − 125
val just_e = $$ \"h\" |-- $$ \"e\"
+ − 126
val just_h = $$ \"h\" --| $$ \"e\"
+ − 127
val input = explode \"hello\"
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 128
in
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 129
(just_e input, just_h input)
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 130
end"
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 131
"((\"e\", [\"l\", \"l\", \"o\"]),(\"h\", [\"l\", \"l\", \"o\"]))"}
38
+ − 132
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 133
The parser @{ML "Scan.optional p x" for p x} returns the result of the parser
58
+ − 134
@{text "p"}, if it succeeds; otherwise it returns
104
+ − 135
the default value @{text "x"}. For example:
38
+ − 136
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 137
@{ML_response [display,gray]
40
+ − 138
"let
+ − 139
val p = Scan.optional ($$ \"h\") \"x\"
236
+ − 140
val input1 = explode \"hello\"
+ − 141
val input2 = explode \"world\"
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 142
in
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 143
(p input1, p input2)
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 144
end"
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 145
"((\"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>
diff
changeset
+ − 146
49
+ − 147
The function @{ML Scan.option} works similarly, except no default value can
50
+ − 148
be given. Instead, the result is wrapped as an @{text "option"}-type. For example:
+ − 149
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 150
@{ML_response [display,gray]
50
+ − 151
"let
+ − 152
val p = Scan.option ($$ \"h\")
236
+ − 153
val input1 = explode \"hello\"
+ − 154
val input2 = explode \"world\"
50
+ − 155
in
+ − 156
(p input1, p input2)
+ − 157
end" "((SOME \"h\", [\"e\", \"l\", \"l\", \"o\"]), (NONE, [\"w\", \"o\", \"r\", \"l\", \"d\"]))"}
49
+ − 158
108
8bea3f74889d
added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 159
The function @{ML "!!"} helps to produce appropriate error messages
220
+ − 160
for parsing. For example if you want to parse @{text p} immediately
58
+ − 161
followed by @{text q}, or start a completely different parser @{text r},
104
+ − 162
you might write:
40
+ − 163
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 164
@{ML [display,gray] "(p -- q) || r" for p q r}
40
+ − 165
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 166
However, this parser is problematic for producing an appropriate error
220
+ − 167
message, if the parsing of @{ML "(p -- q)" for p q} fails. Because in that
+ − 168
case you lose the information that @{text p} should be followed by @{text q}.
+ − 169
To see this assume that @{text p} is present in the input, but it is not
+ − 170
followed by @{text q}. That means @{ML "(p -- q)" for p q} will fail and
+ − 171
hence the alternative parser @{text r} will be tried. However, in many
236
+ − 172
circumstances this will be the wrong parser for the input ``@{text "p"}-followed-by-something''
220
+ − 173
and therefore will also fail. The error message is then caused by the failure
+ − 174
of @{text r}, not by the absence of @{text q} in the input. This kind of
+ − 175
situation can be avoided when using the function @{ML "!!"}. This function
+ − 176
aborts the whole process of parsing in case of a failure and prints an error
+ − 177
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>
diff
changeset
+ − 178
40
+ − 179
236
+ − 180
@{ML [display,gray] "!! (fn _ => \"foo\") ($$ \"h\")"}
40
+ − 181
58
+ − 182
on @{text [quotes] "hello"}, the parsing succeeds
39
631d12c25bde
substantial changes to the antiquotations (preliminary version)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 183
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 184
@{ML_response [display,gray]
236
+ − 185
"(!! (fn _ => \"foo\") ($$ \"h\")) (explode \"hello\")"
+ − 186
"(\"h\", [\"e\", \"l\", \"l\", \"o\"])"}
40
+ − 187
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 188
but if you invoke it on @{text [quotes] "world"}
40
+ − 189
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 190
@{ML_response_fake [display,gray] "(!! (fn _ => \"foo\") ($$ \"h\")) (explode \"world\")"
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 191
"Exception ABORT raised"}
40
+ − 192
108
8bea3f74889d
added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 193
then the parsing aborts and the error message @{text "foo"} is printed. In order to
120
+ − 194
see the error message properly, you need to prefix the parser with the function
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 195
@{ML "Scan.error"}. For example:
40
+ − 196
236
+ − 197
@{ML_response_fake [display,gray]
+ − 198
"Scan.error (!! (fn _ => \"foo\") ($$ \"h\"))"
+ − 199
"Exception Error \"foo\" raised"}
40
+ − 200
219
+ − 201
This ``prefixing'' is usually done by wrappers such as @{ML "OuterSyntax.local_theory"}
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 202
(see Section~\ref{sec:newcommand} which explains this function in more detail).
40
+ − 203
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 204
Let us now return to our example of parsing @{ML "(p -- q) || r" for p q
236
+ − 205
r}. If you want to generate the correct error message for
+ − 206
@{text "p"}-followed-by-@{text "q"}, then you have to write:
38
+ − 207
*}
+ − 208
69
+ − 209
ML{*fun p_followed_by_q p q r =
133
+ − 210
let
236
+ − 211
val err_msg = fn _ => p ^ " is not followed by " ^ q
133
+ − 212
in
+ − 213
($$ p -- (!! err_msg ($$ q))) || ($$ r -- $$ r)
+ − 214
end *}
38
+ − 215
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 216
40
+ − 217
text {*
220
+ − 218
Running this parser with the arguments
+ − 219
@{text [quotes] "h"}, @{text [quotes] "e"} and @{text [quotes] "w"}, and
65
+ − 220
the input @{text [quotes] "holle"}
40
+ − 221
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 222
@{ML_response_fake [display,gray] "Scan.error (p_followed_by_q \"h\" \"e\" \"w\") (explode \"holle\")"
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 223
"Exception ERROR \"h is not followed by e\" raised"}
40
+ − 224
65
+ − 225
produces the correct error message. Running it with
40
+ − 226
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 227
@{ML_response [display,gray] "Scan.error (p_followed_by_q \"h\" \"e\" \"w\") (explode \"wworld\")"
40
+ − 228
"((\"w\", \"w\"), [\"o\", \"r\", \"l\", \"d\"])"}
+ − 229
+ − 230
yields the expected parsing.
38
+ − 231
58
+ − 232
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>
diff
changeset
+ − 233
often as it succeeds. For example:
40
+ − 234
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 235
@{ML_response [display,gray] "Scan.repeat ($$ \"h\") (explode \"hhhhello\")"
40
+ − 236
"([\"h\", \"h\", \"h\", \"h\"], [\"e\", \"l\", \"l\", \"o\"])"}
+ − 237
+ − 238
Note that @{ML "Scan.repeat"} stores the parsed items in a list. The function
58
+ − 239
@{ML "Scan.repeat1"} 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>
diff
changeset
+ − 240
succeeds at least once.
48
+ − 241
58
+ − 242
Also note that the parser would have aborted with the exception @{text MORE}, if
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 243
you had run it only on just @{text [quotes] "hhhh"}. This can be avoided by using
49
+ − 244
the wrapper @{ML Scan.finite} and the ``stopper-token'' @{ML Symbol.stopper}. With
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 245
them you can write:
49
+ − 246
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 247
@{ML_response [display,gray] "Scan.finite Symbol.stopper (Scan.repeat ($$ \"h\")) (explode \"hhhh\")"
49
+ − 248
"([\"h\", \"h\", \"h\", \"h\"], [])"}
+ − 249
65
+ − 250
@{ML Symbol.stopper} is the ``end-of-input'' indicator for parsing strings;
128
+ − 251
other stoppers need to be used when parsing, for example, tokens. However, this kind of
65
+ − 252
manually wrapping is often already done by the surrounding infrastructure.
49
+ − 253
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 254
The function @{ML Scan.repeat} can be used with @{ML Scan.one} to read any
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 255
string as in
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 256
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 257
@{ML_response [display,gray]
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 258
"let
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 259
val p = Scan.repeat (Scan.one Symbol.not_eof)
236
+ − 260
val input = explode \"foo bar foo\"
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 261
in
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 262
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>
diff
changeset
+ − 263
end"
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 264
"([\"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>
diff
changeset
+ − 265
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 266
where the function @{ML Symbol.not_eof} ensures that we do not read beyond the
65
+ − 267
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>
diff
changeset
+ − 268
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 269
The function @{ML "Scan.unless p q" for p q} 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>
diff
changeset
+ − 270
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>
diff
changeset
+ − 271
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 272
@{ML_response_fake_both [display,gray] "Scan.unless ($$ \"h\") ($$ \"w\") (explode \"hello\")"
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 273
"Exception FAIL raised"}
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 274
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 275
fails, while
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 276
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 277
@{ML_response [display,gray] "Scan.unless ($$ \"h\") ($$ \"w\") (explode \"world\")"
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 278
"(\"w\",[\"o\", \"r\", \"l\", \"d\"])"}
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 279
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 280
succeeds.
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 281
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 282
The functions @{ML Scan.repeat} and @{ML Scan.unless} can be combined to read any
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 283
input until a certain marker symbol is reached. In the example below the marker
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>
diff
changeset
+ − 284
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>
diff
changeset
+ − 285
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 286
@{ML_response [display,gray]
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 287
"let
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 288
val p = Scan.repeat (Scan.unless ($$ \"*\") (Scan.one Symbol.not_eof))
236
+ − 289
val input1 = explode \"fooooo\"
+ − 290
val input2 = explode \"foo*ooo\"
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 291
in
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 292
(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>
diff
changeset
+ − 293
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>
diff
changeset
+ − 294
end"
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 295
"(([\"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>
diff
changeset
+ − 296
([\"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>
diff
changeset
+ − 297
220
+ − 298
After parsing is done, you almost always want to apply a function to the parsed
104
+ − 299
items. One way to do this is the function @{ML "(p >> f)" for p f}, which runs
58
+ − 300
first the parser @{text p} and upon successful completion applies the
+ − 301
function @{text f} to the result. For example
38
+ − 302
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 303
@{ML_response [display,gray]
40
+ − 304
"let
193
+ − 305
fun double (x, y) = (x ^ x, y ^ y)
40
+ − 306
in
+ − 307
(($$ \"h\") -- ($$ \"e\") >> double) (explode \"hello\")
+ − 308
end"
+ − 309
"((\"hh\", \"ee\"), [\"l\", \"l\", \"o\"])"}
+ − 310
104
+ − 311
doubles the two parsed input strings; or
59
+ − 312
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 313
@{ML_response [display,gray]
59
+ − 314
"let
104
+ − 315
val p = Scan.repeat (Scan.one Symbol.not_eof)
236
+ − 316
val input = explode \"foo bar foo\"
59
+ − 317
in
104
+ − 318
Scan.finite Symbol.stopper (p >> implode) input
59
+ − 319
end"
+ − 320
"(\"foo bar foo\",[])"}
+ − 321
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>
diff
changeset
+ − 322
where the single-character strings in the parsed output are transformed
59
+ − 323
back into one string.
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 324
193
+ − 325
(FIXME: move to an earlier place)
+ − 326
125
+ − 327
The function @{ML Scan.ahead} parses some input, but leaves the original
+ − 328
input unchanged. For example:
+ − 329
+ − 330
@{ML_response [display,gray]
+ − 331
"Scan.ahead (Scan.this_string \"foo\") (explode \"foo\")"
+ − 332
"(\"foo\", [\"f\", \"o\", \"o\"])"}
56
126646f2aa88
added a para on Scan.unless and an exercise about scanning comments
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 333
40
+ − 334
The function @{ML Scan.lift} takes a parser and a pair as arguments. This function applies
+ − 335
the given parser to the second component of the pair and leaves the first component
+ − 336
untouched. For example
38
+ − 337
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 338
@{ML_response [display,gray]
236
+ − 339
"Scan.lift ($$ \"h\" -- $$ \"e\") (1, explode \"hello\")"
40
+ − 340
"((\"h\", \"e\"), (1, [\"l\", \"l\", \"o\"]))"}
+ − 341
43
02f76f1b6e7b
added positions to anti-quotations; removed old antiquotation_setup; tuned the text a bit
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 342
(FIXME: In which situations is this useful? Give examples.)
149
+ − 343
+ − 344
\begin{exercise}\label{ex:scancmts}
+ − 345
Write a parser that parses an input string so that any comment enclosed
220
+ − 346
within @{text "(*\<dots>*)"} is replaced by the same comment but enclosed within
149
+ − 347
@{text "(**\<dots>**)"} in the output string. To enclose a string, you can use the
+ − 348
function @{ML "enclose s1 s2 s" for s1 s2 s} which produces the string @{ML
236
+ − 349
"s1 ^ s ^ s2" for s1 s2 s}. Hint: To simplify the task ignore the proper
+ − 350
nesting of comments.
149
+ − 351
\end{exercise}
40
+ − 352
*}
+ − 353
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 354
section {* Parsing Theory Syntax *}
38
+ − 355
40
+ − 356
text {*
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 357
Most of the time, however, Isabelle developers have to deal with parsing
156
+ − 358
tokens, not strings. These token parsers have the type:
128
+ − 359
*}
+ − 360
+ − 361
ML{*type 'a parser = OuterLex.token list -> 'a * OuterLex.token list*}
+ − 362
+ − 363
text {*
149
+ − 364
The reason for using token parsers is that theory syntax, as well as the
128
+ − 365
parsers for the arguments of proof methods, use the type @{ML_type
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 366
OuterLex.token}.
42
+ − 367
+ − 368
\begin{readmore}
40
+ − 369
The parser functions for the theory syntax are contained in the structure
42
+ − 370
@{ML_struct OuterParse} defined in the file @{ML_file "Pure/Isar/outer_parse.ML"}.
+ − 371
The definition for tokens is in the file @{ML_file "Pure/Isar/outer_lex.ML"}.
+ − 372
\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>
diff
changeset
+ − 373
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 374
The structure @{ML_struct OuterLex} defines several kinds of tokens (for
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 375
example @{ML "Ident" in OuterLex} for identifiers, @{ML "Keyword" in
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 376
OuterLex} for keywords and @{ML "Command" in OuterLex} for commands). Some
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 377
token parsers take into account the kind of tokens. The first example shows
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 378
how to generate a token list out of a string using the function @{ML
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 379
"OuterSyntax.scan"}. It is given the argument @{ML "Position.none"} since,
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 380
at the moment, we are not interested in generating precise error
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 381
messages. The following code\footnote{Note that because of a possible bug in
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 382
the PolyML runtime system, the result is printed as @{text [quotes] "?"},
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 383
instead of the tokens.}
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 384
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 385
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 386
@{ML_response_fake [display,gray] "OuterSyntax.scan Position.none \"hello world\""
50
+ − 387
"[Token (\<dots>,(Ident, \"hello\"),\<dots>),
+ − 388
Token (\<dots>,(Space, \" \"),\<dots>),
+ − 389
Token (\<dots>,(Ident, \"world\"),\<dots>)]"}
+ − 390
+ − 391
produces three tokens where the first and the last are identifiers, since
58
+ − 392
@{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>
diff
changeset
+ − 393
other syntactic category. The second indicates a space.
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 394
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 395
We can easily change what is recognised as a keyword with
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 396
@{ML OuterKeyword.keyword}. For example calling this function
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 397
*}
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 398
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 399
ML{*val _ = OuterKeyword.keyword "hello"*}
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 400
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 401
text {*
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 402
then lexing @{text [quotes] "hello world"} will produce
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 403
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 404
@{ML_response_fake [display,gray] "OuterSyntax.scan Position.none \"hello world\""
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 405
"[Token (\<dots>,(Keyword, \"hello\"),\<dots>),
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 406
Token (\<dots>,(Space, \" \"),\<dots>),
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 407
Token (\<dots>,(Ident, \"world\"),\<dots>)]"}
50
+ − 408
+ − 409
Many parsing functions later on will require spaces, comments and the like
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 410
to have already been filtered out. So from now on we are going to use the
221
+ − 411
functions @{ML filter} and @{ML OuterLex.is_proper} to do this. 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>
diff
changeset
+ − 412
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 413
@{ML_response_fake [display,gray]
50
+ − 414
"let
+ − 415
val input = OuterSyntax.scan Position.none \"hello world\"
+ − 416
in
+ − 417
filter OuterLex.is_proper input
+ − 418
end"
+ − 419
"[Token (\<dots>,(Ident, \"hello\"), \<dots>), Token (\<dots>,(Ident, \"world\"), \<dots>)]"}
+ − 420
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 421
For convenience we define the function:
50
+ − 422
*}
+ − 423
69
+ − 424
ML{*fun filtered_input str =
160
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 425
filter OuterLex.is_proper (OuterSyntax.scan Position.none str) *}
50
+ − 426
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 427
text {*
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 428
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>
diff
changeset
+ − 429
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 430
@{ML_response_fake [display,gray]
50
+ − 431
"filtered_input \"inductive | for\""
+ − 432
"[Token (\<dots>,(Command, \"inductive\"),\<dots>),
+ − 433
Token (\<dots>,(Keyword, \"|\"),\<dots>),
+ − 434
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>
diff
changeset
+ − 435
221
+ − 436
you obtain a list consisting of only one command and two keyword tokens.
104
+ − 437
If you want to see which keywords and commands are currently known to Isabelle, type in
75
+ − 438
the following code (you might have to adjust the @{ML print_depth} in order to
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 439
see the complete list):
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 440
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 441
@{ML_response_fake [display,gray]
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 442
"let
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 443
val (keywords, commands) = OuterKeyword.get_lexicons ()
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 444
in
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 445
(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>
diff
changeset
+ − 446
end"
132
+ − 447
"([\"}\", \"{\", \<dots>], [\"\<rightleftharpoons>\", \"\<leftharpoondown>\", \<dots>])"}
42
+ − 448
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 449
The parser @{ML "OuterParse.$$$"} parses a single keyword. For example:
50
+ − 450
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 451
@{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>
diff
changeset
+ − 452
"let
50
+ − 453
val input1 = filtered_input \"where for\"
+ − 454
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>
diff
changeset
+ − 455
in
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 456
(OuterParse.$$$ \"where\" input1, OuterParse.$$$ \"|\" input2)
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 457
end"
128
+ − 458
"((\"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>
diff
changeset
+ − 459
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 460
Any non-keyword string can be parsed with the function @{ML OuterParse.reserved}.
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 461
For example:
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 462
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 463
@{ML_response [display,gray]
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 464
"let
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 465
val p = OuterParse.reserved \"bar\"
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 466
val input = filtered_input \"bar\"
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 467
in
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 468
p input
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 469
end"
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 470
"(\"bar\",[])"}
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 471
108
8bea3f74889d
added to the tactical chapter; polished; added the tabularstar environment (which is just tabular*)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 472
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>
diff
changeset
+ − 473
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 474
@{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>
diff
changeset
+ − 475
"let
50
+ − 476
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>
diff
changeset
+ − 477
in
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 478
(OuterParse.$$$ \"|\" -- OuterParse.$$$ \"in\") input
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 479
end"
183
+ − 480
"((\"|\", \"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>
diff
changeset
+ − 481
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 482
The parser @{ML "OuterParse.enum s p" for s p} parses a possibly empty
58
+ − 483
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>
diff
changeset
+ − 484
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>
diff
changeset
+ − 485
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 486
@{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>
diff
changeset
+ − 487
"let
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 488
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>
diff
changeset
+ − 489
in
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 490
(OuterParse.enum \"|\" (OuterParse.$$$ \"in\")) input
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 491
end"
183
+ − 492
"([\"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>
diff
changeset
+ − 493
50
+ − 494
@{ML "OuterParse.enum1"} works similarly, except that the parsed list must
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 495
be non-empty. Note that we had to add a string @{text [quotes] "foo"} at the
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 496
end of the parsed string, otherwise the parser would have consumed all
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 497
tokens and then failed with the exception @{text "MORE"}. Like in the
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 498
previous section, we can avoid this exception using the wrapper @{ML
50
+ − 499
Scan.finite}. This time, however, we have to use the ``stopper-token'' @{ML
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 500
OuterLex.stopper}. We can write:
49
+ − 501
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 502
@{ML_response [display,gray]
49
+ − 503
"let
50
+ − 504
val input = filtered_input \"in | in | in\"
49
+ − 505
in
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 506
Scan.finite OuterLex.stopper
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 507
(OuterParse.enum \"|\" (OuterParse.$$$ \"in\")) input
49
+ − 508
end"
183
+ − 509
"([\"in\", \"in\", \"in\"], [])"}
49
+ − 510
75
+ − 511
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>
diff
changeset
+ − 512
*}
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 513
69
+ − 514
ML{*fun parse p input = Scan.finite OuterLex.stopper (Scan.error p) input *}
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 515
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 516
text {*
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 517
49
+ − 518
The function @{ML "OuterParse.!!!"} can be used to force termination of the
193
+ − 519
parser in case of a dead end, just like @{ML "Scan.!!"} (see previous section).
+ − 520
Except that the error message of @{ML "OuterParse.!!!"} is fixed to be
+ − 521
@{text [quotes] "Outer syntax error"}
221
+ − 522
together with a relatively precise description of the failure. For example:
49
+ − 523
72
7b8c4fe235aa
added an antiquotation option [gray] for gray boxes around displays
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 524
@{ML_response_fake [display,gray]
49
+ − 525
"let
50
+ − 526
val input = filtered_input \"in |\"
49
+ − 527
val parse_bar_then_in = OuterParse.$$$ \"|\" -- OuterParse.$$$ \"in\"
+ − 528
in
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 529
parse (OuterParse.!!! parse_bar_then_in) input
49
+ − 530
end"
+ − 531
"Exception ERROR \"Outer syntax error: keyword \"|\" expected,
+ − 532
but keyword in was found\" raised"
+ − 533
}
42
+ − 534
65
+ − 535
\begin{exercise} (FIXME)
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 536
A type-identifier, for example @{typ "'a"}, is a token of
54
+ − 537
kind @{ML "Keyword" in OuterLex}. It can be parsed using
+ − 538
the function @{ML OuterParse.type_ident}.
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 539
\end{exercise}
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 540
104
+ − 541
(FIXME: or give parser for numbers)
53
0c3580c831a4
removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 542
125
+ − 543
Whenever there is a possibility that the processing of user input can fail,
221
+ − 544
it is a good idea to give all available information about where the error
220
+ − 545
occurred. For this Isabelle can attach positional information to tokens
125
+ − 546
and then thread this information up the processing chain. To see this,
128
+ − 547
modify the function @{ML filtered_input} described earlier to
41
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 548
*}
b11653b11bd3
further progress on the parsing section and tuning on the antiqu's
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 549
125
+ − 550
ML{*fun filtered_input' str =
+ − 551
filter OuterLex.is_proper (OuterSyntax.scan (Position.line 7) str) *}
49
+ − 552
+ − 553
text {*
125
+ − 554
where we pretend the parsed string starts on line 7. An example is
49
+ − 555
125
+ − 556
@{ML_response_fake [display,gray]
+ − 557
"filtered_input' \"foo \\n bar\""
+ − 558
"[Token ((\"foo\", ({line=7, end_line=7}, {line=7})), (Ident, \"foo\"), \<dots>),
+ − 559
Token ((\"bar\", ({line=8, end_line=8}, {line=8})), (Ident, \"bar\"), \<dots>)]"}
+ − 560
+ − 561
in which the @{text [quotes] "\\n"} causes the second token to be in
+ − 562
line 8.
+ − 563
126
+ − 564
By using the parser @{ML OuterParse.position} you can decode the positional
125
+ − 565
information and return it as part of the parsed input. For example
+ − 566
+ − 567
@{ML_response_fake [display,gray]
+ − 568
"let
+ − 569
val input = (filtered_input' \"where\")
+ − 570
in
+ − 571
parse (OuterParse.position (OuterParse.$$$ \"where\")) input
+ − 572
end"
+ − 573
"((\"where\", {line=7, end_line=7}), [])"}
+ − 574
+ − 575
\begin{readmore}
+ − 576
The functions related to positions are implemented in the file
+ − 577
@{ML_file "Pure/General/position.ML"}.
+ − 578
\end{readmore}
49
+ − 579
+ − 580
*}
+ − 581
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 582
text {*
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 583
(FIXME: there are also handy parsers for ML-expressions and ML-files)
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 584
*}
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 585
193
+ − 586
section {* Context Parser (TBD) *}
+ − 587
+ − 588
text {*
+ − 589
Used for example in \isacommand{attribute\_setup} and \isacommand{method\_setup}.
+ − 590
*}
+ − 591
207
+ − 592
section {* Argument and Attribute Parsers (TBD) *}
+ − 593
44
dee4b3e66dfe
added a readme chapter for prospective authors; added commands for referring to the Isar Reference Manual
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 594
section {* Parsing Inner Syntax *}
42
+ − 595
125
+ − 596
text {*
+ − 597
There is usually no need to write your own parser for parsing inner syntax, that is
+ − 598
for terms and types: you can just call the pre-defined parsers. Terms can
+ − 599
be parsed using the function @{ML OuterParse.term}. For example:
+ − 600
+ − 601
@{ML_response [display,gray]
+ − 602
"let
+ − 603
val input = OuterSyntax.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>
diff
changeset
+ − 604
in
125
+ − 605
OuterParse.term input
+ − 606
end"
+ − 607
"(\"\\^E\\^Ftoken\\^Efoo\\^E\\^F\\^E\", [])"}
+ − 608
+ − 609
The function @{ML OuterParse.prop} is similar, except that it gives a different
127
+ − 610
error message, when parsing fails. As you can see, the parser not just returns
+ − 611
the parsed string, but also some encoded information. You can decode the
+ − 612
information with the function @{ML YXML.parse}. For example
+ − 613
+ − 614
@{ML_response [display,gray]
+ − 615
"YXML.parse \"\\^E\\^Ftoken\\^Efoo\\^E\\^F\\^E\""
+ − 616
"XML.Elem (\"token\", [], [XML.Text \"foo\"])"}
+ − 617
149
+ − 618
The result of the decoding is an XML-tree. You can see better what is going on if
131
+ − 619
you replace @{ML Position.none} by @{ML "Position.line 42"}, say:
101
+ − 620
125
+ − 621
@{ML_response [display,gray]
+ − 622
"let
+ − 623
val input = OuterSyntax.scan (Position.line 42) \"foo\"
+ − 624
in
127
+ − 625
YXML.parse (fst (OuterParse.term input))
125
+ − 626
end"
127
+ − 627
"XML.Elem (\"token\", [(\"line\", \"42\"), (\"end_line\", \"42\")], [XML.Text \"foo\"])"}
125
+ − 628
149
+ − 629
The positional information is stored as part of an XML-tree so that code
+ − 630
called later on will be able to give more precise error messages.
125
+ − 631
127
+ − 632
\begin{readmore}
128
+ − 633
The functions to do with input and output of XML and YXML are defined
127
+ − 634
in @{ML_file "Pure/General/xml.ML"} and @{ML_file "Pure/General/yxml.ML"}.
+ − 635
\end{readmore}
160
cc9359bfacf4
redefined the functions warning and tracing in order to properly match more antiquotations
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 636
125
+ − 637
*}
101
+ − 638
116
+ − 639
section {* Parsing Specifications\label{sec:parsingspecs} *}
101
+ − 640
+ − 641
text {*
121
+ − 642
There are a number of special purpose parsers that help with parsing
156
+ − 643
specifications of function definitions, inductive predicates and so on. In
220
+ − 644
Chapter~\ref{chp:package}, for example, we will need to parse specifications
121
+ − 645
for inductive predicates of the form:
+ − 646
*}
101
+ − 647
121
+ − 648
simple_inductive
+ − 649
even and odd
+ − 650
where
+ − 651
even0: "even 0"
+ − 652
| evenS: "odd n \<Longrightarrow> even (Suc n)"
+ − 653
| oddS: "even n \<Longrightarrow> odd (Suc n)"
101
+ − 654
+ − 655
text {*
121
+ − 656
For this we are going to use the parser:
101
+ − 657
*}
+ − 658
121
+ − 659
ML %linenosgray{*val spec_parser =
126
+ − 660
OuterParse.fixes --
+ − 661
Scan.optional
+ − 662
(OuterParse.$$$ "where" |--
+ − 663
OuterParse.!!!
+ − 664
(OuterParse.enum1 "|"
+ − 665
(SpecParse.opt_thm_name ":" -- OuterParse.prop))) []*}
120
+ − 666
101
+ − 667
text {*
126
+ − 668
Note that the parser does not parse the keyword \simpleinductive, even if it is
+ − 669
meant to process definitions as shown above. The parser of the keyword
128
+ − 670
will be given by the infrastructure that will eventually call @{ML spec_parser}.
126
+ − 671
+ − 672
124
+ − 673
To see what the parser returns, let us parse the string corresponding to the
121
+ − 674
definition of @{term even} and @{term odd}:
+ − 675
101
+ − 676
@{ML_response [display,gray]
+ − 677
"let
+ − 678
val input = filtered_input
+ − 679
(\"even and odd \" ^
+ − 680
\"where \" ^
+ − 681
\" even0[intro]: \\\"even 0\\\" \" ^
+ − 682
\"| evenS[intro]: \\\"odd n \<Longrightarrow> even (Suc n)\\\" \" ^
+ − 683
\"| oddS[intro]: \\\"even n \<Longrightarrow> odd (Suc n)\\\"\")
+ − 684
in
120
+ − 685
parse spec_parser input
101
+ − 686
end"
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 687
"(([(even, NONE, NoSyn), (odd, NONE, NoSyn)],
101
+ − 688
[((even0,\<dots>), \"\\^E\\^Ftoken\\^Eeven 0\\^E\\^F\\^E\"),
+ − 689
((evenS,\<dots>), \"\\^E\\^Ftoken\\^Eodd n \<Longrightarrow> even (Suc n)\\^E\\^F\\^E\"),
+ − 690
((oddS,\<dots>), \"\\^E\\^Ftoken\\^Eeven n \<Longrightarrow> odd (Suc n)\\^E\\^F\\^E\")]), [])"}
121
+ − 691
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 692
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>
diff
changeset
+ − 693
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>
diff
changeset
+ − 694
rules where every rule has optionally a name and an attribute.
121
+ − 695
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 696
The function @{ML OuterParse.fixes} in Line 2 of the parser reads an
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 697
\isacommand{and}-separated
124
+ − 698
list of variables that can include optional type annotations and syntax translations.
121
+ − 699
For example:\footnote{Note that in the code we need to write
+ − 700
@{text "\\\"int \<Rightarrow> bool\\\""} in order to properly escape the double quotes
+ − 701
in the compound type.}
+ − 702
+ − 703
@{ML_response [display,gray]
+ − 704
"let
+ − 705
val input = filtered_input
+ − 706
\"foo::\\\"int \<Rightarrow> bool\\\" and bar::nat (\\\"BAR\\\" 100) and blonk\"
+ − 707
in
219
+ − 708
parse OuterParse.fixes input
121
+ − 709
end"
+ − 710
"([(foo, SOME \"\\^E\\^Ftoken\\^Eint \<Rightarrow> bool\\^E\\^F\\^E\", NoSyn),
+ − 711
(bar, SOME \"\\^E\\^Ftoken\\^Enat\\^E\\^F\\^E\", Mixfix (\"BAR\", [], 100)),
+ − 712
(blonk, NONE, NoSyn)],[])"}
50
+ − 713
*}
+ − 714
121
+ − 715
text {*
156
+ − 716
Whenever types are given, they are stored in the @{ML SOME}s. The types are
+ − 717
not yet used to type the variables: this must be done by type-inference later
149
+ − 718
on. Since types are part of the inner syntax they are strings with some
+ − 719
encoded information (see previous section). If a syntax translation is
220
+ − 720
present for a variable, then it is stored in the @{ML Mixfix} data structure;
149
+ − 721
no syntax translation is indicated by @{ML NoSyn}.
121
+ − 722
+ − 723
\begin{readmore}
220
+ − 724
The data structure for syntax annotations is defined in @{ML_file "Pure/Syntax/mixfix.ML"}.
121
+ − 725
\end{readmore}
+ − 726
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 727
Lines 3 to 7 in the function @{ML spec_parser} implement the parser for a
219
+ − 728
list of introduction rules, that is propositions with theorem annotations
+ − 729
such as rule names and attributes. The introduction rules are propositions
+ − 730
parsed by @{ML OuterParse.prop}. However, they can include an optional
+ − 731
theorem name plus some attributes. For example
121
+ − 732
+ − 733
@{ML_response [display,gray] "let
+ − 734
val input = filtered_input \"foo_lemma[intro,dest!]:\"
+ − 735
val ((name, attrib), _) = parse (SpecParse.thm_name \":\") input
+ − 736
in
+ − 737
(name, map Args.dest_src attrib)
+ − 738
end" "(foo_lemma, [((\"intro\", []), \<dots>), ((\"dest\", [\<dots>]), \<dots>)])"}
+ − 739
+ − 740
The function @{ML opt_thm_name in SpecParse} is the ``optional'' variant of
131
+ − 741
@{ML thm_name in SpecParse}. Theorem names can contain attributes. The name
+ − 742
has to end with @{text [quotes] ":"}---see the argument of
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 743
the function @{ML SpecParse.opt_thm_name} in Line 7.
121
+ − 744
+ − 745
\begin{readmore}
+ − 746
Attributes and arguments are implemented in the files @{ML_file "Pure/Isar/attrib.ML"}
+ − 747
and @{ML_file "Pure/Isar/args.ML"}.
+ − 748
\end{readmore}
101
+ − 749
*}
65
+ − 750
193
+ − 751
text_raw {*
+ − 752
\begin{exercise}
207
+ − 753
Have a look at how the parser @{ML SpecParse.where_alt_specs} is implemented
+ − 754
in file @{ML_file "Pure/Isar/spec_parse.ML"}. This parser corresponds
+ − 755
to the ``where-part'' of the introduction rules given above. Below
+ − 756
we paraphrase the code of @{ML SpecParse.where_alt_specs} adapted to our
+ − 757
purposes.
193
+ − 758
\begin{isabelle}
+ − 759
*}
+ − 760
ML %linenosgray{*val spec_parser' =
+ − 761
OuterParse.fixes --
+ − 762
Scan.optional
+ − 763
(OuterParse.$$$ "where" |--
+ − 764
OuterParse.!!!
+ − 765
(OuterParse.enum1 "|"
+ − 766
((SpecParse.opt_thm_name ":" -- OuterParse.prop) --|
+ − 767
Scan.option (Scan.ahead (OuterParse.name ||
+ − 768
OuterParse.$$$ "[") --
+ − 769
OuterParse.!!! (OuterParse.$$$ "|"))))) [] *}
+ − 770
text_raw {*
+ − 771
\end{isabelle}
207
+ − 772
Both parsers accept the same input, but if you look closely, you can notice
+ − 773
an additional ``tail'' (Lines 8 to 10) in @{ML spec_parser'}. What is the purpose of
+ − 774
this additional ``tail''?
193
+ − 775
\end{exercise}
+ − 776
*}
+ − 777
229
+ − 778
text {*
+ − 779
(FIXME: @{ML OuterParse.type_args}, @{ML OuterParse.typ}, @{ML OuterParse.opt_mixfix})
+ − 780
*}
+ − 781
+ − 782
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 783
section {* New Commands and Keyword Files\label{sec:newcommand} *}
65
+ − 784
+ − 785
text {*
68
+ − 786
Often new commands, for example for providing new definitional principles,
+ − 787
need to be implemented. While this is not difficult on the ML-level,
66
+ − 788
new commands, in order to be useful, need to be recognised by
65
+ − 789
ProofGeneral. This results in some subtle configuration issues, which we
+ − 790
will explain in this section.
+ − 791
74
+ − 792
To keep things simple, let us start with a ``silly'' command that does nothing
+ − 793
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>
diff
changeset
+ − 794
defined as:
68
+ − 795
*}
65
+ − 796
69
+ − 797
ML{*let
219
+ − 798
val do_nothing = Scan.succeed (LocalTheory.theory I)
68
+ − 799
val kind = OuterKeyword.thy_decl
65
+ − 800
in
219
+ − 801
OuterSyntax.local_theory "foobar" "description of foobar" kind do_nothing
69
+ − 802
end *}
65
+ − 803
68
+ − 804
text {*
219
+ − 805
The crucial function @{ML OuterSyntax.local_theory} expects a name for the command, a
+ − 806
short description, a kind indicator (which we will explain later more thoroughly) and a
+ − 807
parser producing a local theory transition (its purpose will also explained
66
+ − 808
later).
65
+ − 809
101
+ − 810
While this is everything you have to do on the ML-level, you need a keyword
68
+ − 811
file that can be loaded by ProofGeneral. This is to enable ProofGeneral to
+ − 812
recognise \isacommand{foobar} as a command. Such a keyword file can be
74
+ − 813
generated with the command-line:
68
+ − 814
74
+ − 815
@{text [display] "$ isabelle keywords -k foobar some_log_files"}
65
+ − 816
74
+ − 817
The option @{text "-k foobar"} indicates which postfix the name of the keyword file
80
+ − 818
will be assigned. In the case above the file will be named @{text
86
+ − 819
"isar-keywords-foobar.el"}. This command requires log files to be
68
+ − 820
present (in order to extract the keywords from them). To generate these log
101
+ − 821
files, you first need to package the code above into a separate theory file named
68
+ − 822
@{text "Command.thy"}, say---see Figure~\ref{fig:commandtheory} for the
+ − 823
complete code.
65
+ − 824
66
+ − 825
+ − 826
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ − 827
\begin{figure}[t]
69
+ − 828
\begin{graybox}\small
66
+ − 829
\isacommand{theory}~@{text Command}\\
+ − 830
\isacommand{imports}~@{text Main}\\
+ − 831
\isacommand{begin}\\
85
+ − 832
\isacommand{ML}~@{text "\<verbopen>"}\\
66
+ − 833
@{ML
+ − 834
"let
219
+ − 835
val do_nothing = Scan.succeed (LocalTheory.theory I)
68
+ − 836
val kind = OuterKeyword.thy_decl
66
+ − 837
in
219
+ − 838
OuterSyntax.local_theory \"foobar\" \"description of foobar\" kind do_nothing
66
+ − 839
end"}\\
85
+ − 840
@{text "\<verbclose>"}\\
66
+ − 841
\isacommand{end}
80
+ − 842
\end{graybox}
219
+ − 843
\caption{The file @{text "Command.thy"} is necessary for generating a log
66
+ − 844
file. This log file enables Isabelle to generate a keyword file containing
68
+ − 845
the command \isacommand{foobar}.\label{fig:commandtheory}}
66
+ − 846
\end{figure}
+ − 847
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+ − 848
75
+ − 849
For our purposes it is sufficient to use the log files of the theories
68
+ − 850
@{text "Pure"}, @{text "HOL"} and @{text "Pure-ProofGeneral"}, as well as
75
+ − 851
the log file for the theory @{text "Command.thy"}, which contains the new
+ − 852
\isacommand{foobar}-command. If you target other logics besides HOL, such
74
+ − 853
as Nominal or ZF, then you need to adapt the log files appropriately.
104
+ − 854
74
+ − 855
@{text Pure} and @{text HOL} are usually compiled during the installation of
+ − 856
Isabelle. So log files for them should be already available. If not, then
75
+ − 857
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>
diff
changeset
+ − 858
distribution.
65
+ − 859
+ − 860
@{text [display]
+ − 861
"$ ./build -m \"Pure\"
+ − 862
$ ./build -m \"HOL\""}
+ − 863
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 864
The @{text "Pure-ProofGeneral"} theory needs to be compiled with:
65
+ − 865
+ − 866
@{text [display] "$ ./build -m \"Pure-ProofGeneral\" \"Pure\""}
+ − 867
101
+ − 868
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>
diff
changeset
+ − 869
with:
66
+ − 870
68
+ − 871
@{text [display] "$ isabelle mkdir FoobarCommand"}
66
+ − 872
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 873
This generates a directory containing the files:
66
+ − 874
+ − 875
@{text [display]
+ − 876
"./IsaMakefile
68
+ − 877
./FoobarCommand/ROOT.ML
+ − 878
./FoobarCommand/document
+ − 879
./FoobarCommand/document/root.tex"}
65
+ − 880
+ − 881
101
+ − 882
You need to copy the file @{text "Command.thy"} into the directory @{text "FoobarCommand"}
66
+ − 883
and add the line
+ − 884
207
+ − 885
@{text [display] "no_document use_thy \"Command\";"}
66
+ − 886
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 887
to the file @{text "./FoobarCommand/ROOT.ML"}. You can now compile the theory by just typing:
65
+ − 888
+ − 889
@{text [display] "$ isabelle make"}
+ − 890
101
+ − 891
If the compilation succeeds, you have finally created all the necessary log files.
+ − 892
They are stored in the directory
65
+ − 893
66
+ − 894
@{text [display] "~/.isabelle/heaps/Isabelle2008/polyml-5.2.1_x86-linux/log"}
65
+ − 895
74
+ − 896
or something similar depending on your Isabelle distribution and architecture.
+ − 897
One quick way to assign a shell variable to this directory is by typing
66
+ − 898
+ − 899
@{text [display] "$ ISABELLE_LOGS=\"$(isabelle getenv -b ISABELLE_OUTPUT)\"/log"}
+ − 900
156
+ − 901
on the Unix prompt. If you now type @{text "ls $ISABELLE_LOGS"}, then the
128
+ − 902
directory should include the files:
65
+ − 903
+ − 904
@{text [display]
+ − 905
"Pure.gz
+ − 906
HOL.gz
+ − 907
Pure-ProofGeneral.gz
68
+ − 908
HOL-FoobarCommand.gz"}
65
+ − 909
101
+ − 910
From them you can create the keyword files. Assuming the name
75
+ − 911
of the directory is in @{text "$ISABELLE_LOGS"},
74
+ − 912
then the Unix command for creating the keyword file is:
65
+ − 913
+ − 914
@{text [display]
68
+ − 915
"$ isabelle keywords -k foobar
80
+ − 916
$ISABELLE_LOGS/{Pure.gz,HOL.gz,Pure-ProofGeneral.gz,HOL-FoobarCommand.gz}"}
65
+ − 917
80
+ − 918
The result is the file @{text "isar-keywords-foobar.el"}. It should contain
86
+ − 919
the string @{text "foobar"} twice.\footnote{To see whether things are fine, check
80
+ − 920
that @{text "grep foobar"} on this file returns something
86
+ − 921
non-empty.} This keyword file needs to
80
+ − 922
be copied into the directory @{text "~/.isabelle/etc"}. To make Isabelle
101
+ − 923
aware of this keyword file, you have to start Isabelle with the option @{text
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 924
"-k foobar"}, that is:
65
+ − 925
80
+ − 926
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 927
@{text [display] "$ isabelle emacs -k foobar a_theory_file"}
65
+ − 928
101
+ − 929
If you now build a theory on top of @{text "Command.thy"},
+ − 930
then the command \isacommand{foobar} can be used.
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 931
Similarly with any other new command, and also any new keyword that is
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 932
introduced with
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 933
*}
65
+ − 934
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 935
ML{*val _ = OuterKeyword.keyword "blink" *}
65
+ − 936
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 937
text {*
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 938
At the moment the command \isacommand{foobar} is not very useful. Let us refine
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 939
it a bit
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 940
next by letting it take a proposition as argument and printing this proposition
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 941
inside the tracing buffer.
68
+ − 942
75
+ − 943
The crucial part of a command is the function that determines the behaviour
+ − 944
of the command. In the code above we used a ``do-nothing''-function, which
+ − 945
because of @{ML Scan.succeed} does not parse any argument, but immediately
219
+ − 946
returns the simple function @{ML "LocalTheory.theory I"}. We can
75
+ − 947
replace this code by a function that first parses a proposition (using the
+ − 948
parser @{ML OuterParse.prop}), then prints out the tracing
219
+ − 949
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>
diff
changeset
+ − 950
finally does nothing. For this you can write:
68
+ − 951
*}
+ − 952
69
+ − 953
ML{*let
219
+ − 954
fun trace_prop str =
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>
diff
changeset
+ − 955
LocalTheory.theory (fn lthy => (tracing str; lthy))
75
+ − 956
219
+ − 957
val trace_prop_parser = OuterParse.prop >> trace_prop
68
+ − 958
val kind = OuterKeyword.thy_decl
+ − 959
in
219
+ − 960
OuterSyntax.local_theory "foobar" "traces a proposition"
+ − 961
kind trace_prop_parser
69
+ − 962
end *}
68
+ − 963
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>
diff
changeset
+ − 964
68
+ − 965
text {*
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 966
Now you can type
68
+ − 967
74
+ − 968
\begin{isabelle}
+ − 969
\isacommand{foobar}~@{text [quotes] "True \<and> False"}\\
75
+ − 970
@{text "> \"True \<and> False\""}
74
+ − 971
\end{isabelle}
68
+ − 972
+ − 973
and see the proposition in the tracing buffer.
+ − 974
219
+ − 975
Note that so far we used @{ML thy_decl in OuterKeyword} as the kind
+ − 976
indicator for the command. This means that the command finishes as soon as
+ − 977
the arguments are processed. Examples of this kind of commands are
+ − 978
\isacommand{definition} and \isacommand{declare}. In other cases, commands
+ − 979
are expected to parse some arguments, for example a proposition, and then
+ − 980
``open up'' a proof in order to prove the proposition (for example
86
+ − 981
\isacommand{lemma}) or prove some other properties (for example
219
+ − 982
\isacommand{function}). To achieve this kind of behaviour, you have to use
+ − 983
the kind indicator @{ML thy_goal in OuterKeyword} and the function @{ML
+ − 984
"local_theory_to_proof" in OuterSyntax} to set up the command. Note,
+ − 985
however, once you change the ``kind'' of a command from @{ML thy_decl in
+ − 986
OuterKeyword} to @{ML thy_goal in OuterKeyword} then the keyword file needs
+ − 987
to be re-created!
68
+ − 988
75
+ − 989
Below we change \isacommand{foobar} so that it takes a proposition as
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 990
argument and then starts a proof in order to prove it. Therefore in Line 13,
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 991
we set the kind indicator to @{ML thy_goal in OuterKeyword}.
68
+ − 992
*}
+ − 993
114
+ − 994
ML%linenosgray{*let
219
+ − 995
fun prove_prop str ctxt =
68
+ − 996
let
+ − 997
val prop = Syntax.read_prop ctxt str
+ − 998
in
75
+ − 999
Proof.theorem_i NONE (K I) [[(prop,[])]] ctxt
68
+ − 1000
end;
+ − 1001
219
+ − 1002
val prove_prop_parser = OuterParse.prop >> prove_prop
68
+ − 1003
val kind = OuterKeyword.thy_goal
+ − 1004
in
219
+ − 1005
OuterSyntax.local_theory_to_proof "foobar" "proving a proposition"
+ − 1006
kind prove_prop_parser
69
+ − 1007
end *}
68
+ − 1008
+ − 1009
text {*
219
+ − 1010
The function @{text prove_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>
diff
changeset
+ − 1011
proved) and a context as argument. The context is necessary in order to be able to use
74
+ − 1012
@{ML Syntax.read_prop}, which converts a string into a proper proposition.
75
+ − 1013
In Line 6 the function @{ML Proof.theorem_i} starts the proof for the
+ − 1014
proposition. Its argument @{ML NONE} stands for a locale (which we chose to
+ − 1015
omit); the argument @{ML "(K I)"} stands for a function that determines what
+ − 1016
should be done with the theorem once it is proved (we chose to just forget
219
+ − 1017
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>
diff
changeset
+ − 1018
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1019
If you now type \isacommand{foobar}~@{text [quotes] "True \<and> True"}, you obtain the following
219
+ − 1020
proof state
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1021
74
+ − 1022
\begin{isabelle}
80
+ − 1023
\isacommand{foobar}~@{text [quotes] "True \<and> True"}\\
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1024
@{text "goal (1 subgoal):"}\\
74
+ − 1025
@{text "1. True \<and> True"}
+ − 1026
\end{isabelle}
68
+ − 1027
219
+ − 1028
and you can build the following proof
68
+ − 1029
74
+ − 1030
\begin{isabelle}
80
+ − 1031
\isacommand{foobar}~@{text [quotes] "True \<and> True"}\\
74
+ − 1032
\isacommand{apply}@{text "(rule conjI)"}\\
+ − 1033
\isacommand{apply}@{text "(rule TrueI)+"}\\
+ − 1034
\isacommand{done}
+ − 1035
\end{isabelle}
+ − 1036
230
8def50824320
added material about OuterKeyword.keyword and OuterParse.reserved
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1037
(FIXME: read a name and show how to store theorems)
65
+ − 1038
*}
+ − 1039
211
d5accbc67e1b
more work on simple inductive and marked all sections that are still seriously incomplete with TBD
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1040
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>
diff
changeset
+ − 1041
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1042
text {*
207
+ − 1043
(FIXME: maybe move to after the tactic section)
+ − 1044
221
+ − 1045
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>
diff
changeset
+ − 1046
in \isacommand{apply}. To print out all currently known methods you can use the
192
+ − 1047
Isabelle command:
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1048
207
+ − 1049
\begin{isabelle}
+ − 1050
\isacommand{print\_methods}\\
+ − 1051
@{text "> methods:"}\\
+ − 1052
@{text "> -: do nothing (insert current facts only)"}\\
+ − 1053
@{text "> HOL.default: apply some intro/elim rule (potentially classical)"}\\
+ − 1054
@{text "> ..."}
+ − 1055
\end{isabelle}
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1056
193
+ − 1057
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>
diff
changeset
+ − 1058
*}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1059
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1060
method_setup %gray foobar_meth =
181
+ − 1061
{* Scan.succeed
+ − 1062
(K (SIMPLE_METHOD ((etac @{thm conjE} THEN' rtac @{thm conjI}) 1))) *}
207
+ − 1063
"foobar 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>
diff
changeset
+ − 1064
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1065
text {*
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1066
It defines the method @{text foobar_meth}, which takes no arguments (therefore the
207
+ − 1067
parser @{ML Scan.succeed}) and only applies a single tactic, namely the tactic which
+ − 1068
applies @{thm [source] conjE} and then @{thm [source] conjI}. The function @{ML SIMPLE_METHOD}
+ − 1069
turns such a tactic into a method. @{text "Foobar_meth"} 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>
diff
changeset
+ − 1070
*}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1071
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1072
lemma shows "A \<and> B \<Longrightarrow> C \<and> D"
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1073
apply(foobar_meth)
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1074
txt {*
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1075
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>
diff
changeset
+ − 1076
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1077
\begin{minipage}{\textwidth}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1078
@{subgoals}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1079
\end{minipage} *}
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1080
(*<*)oops(*>*)
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1081
193
+ − 1082
+ − 1083
(*
+ − 1084
ML {* SIMPLE_METHOD *}
+ − 1085
ML {* METHOD *}
+ − 1086
ML {* K (SIMPLE_METHOD ((etac @{thm conjE} THEN' rtac @{thm conjI}) 1)) *}
+ − 1087
ML {* Scan.succeed *}
+ − 1088
*)
+ − 1089
186
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1090
text {*
371e4375c994
made the Ackermann function example safer and included suggestions from MW
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1091
(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>
diff
changeset
+ − 1092
*}
178
fb8f22dd8ad0
adapted to latest Attrib.setup changes and more work on the simple induct chapter
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1093
75
+ − 1094
(*<*)
194
+ − 1095
(* THIS IS AN OLD VERSION OF THE PARSING CHAPTER BY JEREMY DAWSON *)
38
+ − 1096
chapter {* Parsing *}
+ − 1097
+ − 1098
text {*
+ − 1099
4
+ − 1100
Lots of Standard ML code is given in this document, for various reasons,
+ − 1101
including:
+ − 1102
\begin{itemize}
+ − 1103
\item direct quotation of code found in the Isabelle source files,
+ − 1104
or simplified versions of such code
+ − 1105
\item identifiers found in the Isabelle source code, with their types
+ − 1106
(or specialisations of their types)
+ − 1107
\item code examples, which can be run by the reader, to help illustrate the
+ − 1108
behaviour of functions found in the Isabelle source code
+ − 1109
\item ancillary functions, not from the Isabelle source code,
+ − 1110
which enable the reader to run relevant code examples
+ − 1111
\item type abbreviations, which help explain the uses of certain functions
+ − 1112
\end{itemize}
+ − 1113
+ − 1114
*}
+ − 1115
+ − 1116
section {* Parsing Isar input *}
+ − 1117
+ − 1118
text {*
+ − 1119
+ − 1120
The typical parsing function has the type
+ − 1121
\texttt{'src -> 'res * 'src}, with input
+ − 1122
of type \texttt{'src}, returning a result
+ − 1123
of type \texttt{'res}, which is (or is derived from) the first part of the
+ − 1124
input, and also returning the remainder of the input.
+ − 1125
(In the common case, when it is clear what the ``remainder of the input''
+ − 1126
means, we will just say that the functions ``returns'' the
+ − 1127
value of type \texttt{'res}).
+ − 1128
An exception is raised if an appropriate value
+ − 1129
cannot be produced from the input.
+ − 1130
A range of exceptions can be used to identify different reasons
+ − 1131
for the failure of a parse.
+ − 1132
+ − 1133
This contrasts the standard parsing function in Standard ML,
+ − 1134
which is of type
+ − 1135
\texttt{type ('res, 'src) reader = 'src -> ('res * 'src) option};
+ − 1136
(for example, \texttt{List.getItem} and \texttt{Substring.getc}).
+ − 1137
However, much of the discussion at
+ − 1138
FIX file:/home/jeremy/html/ml/SMLBasis/string-cvt.html
+ − 1139
is relevant.
+ − 1140
+ − 1141
Naturally one may convert between the two different sorts of parsing functions
+ − 1142
as follows:
+ − 1143
\begin{verbatim}
+ − 1144
open StringCvt ;
+ − 1145
type ('res, 'src) ex_reader = 'src -> 'res * 'src
75
+ − 1146
ex_reader : ('res, 'src) reader -> ('res, 'src) ex_reader
4
+ − 1147
fun ex_reader rdr src = Option.valOf (rdr src) ;
75
+ − 1148
reader : ('res, 'src) ex_reader -> ('res, 'src) reader
4
+ − 1149
fun reader exrdr src = SOME (exrdr src) handle _ => NONE ;
+ − 1150
\end{verbatim}
+ − 1151
+ − 1152
*}
+ − 1153
+ − 1154
section{* The \texttt{Scan} structure *}
+ − 1155
+ − 1156
text {*
+ − 1157
The source file is \texttt{src/General/scan.ML}.
+ − 1158
This structure provides functions for using and combining parsing functions
+ − 1159
of the type \texttt{'src -> 'res * 'src}.
+ − 1160
Three exceptions are used:
+ − 1161
\begin{verbatim}
+ − 1162
exception MORE of string option; (*need more input (prompt)*)
+ − 1163
exception FAIL of string option; (*try alternatives (reason of failure)*)
+ − 1164
exception ABORT of string; (*dead end*)
+ − 1165
\end{verbatim}
+ − 1166
Many functions in this structure (generally those with names composed of
+ − 1167
symbols) are declared as infix.
+ − 1168
+ − 1169
Some functions from that structure are
+ − 1170
\begin{verbatim}
+ − 1171
|-- : ('src -> 'res1 * 'src') * ('src' -> 'res2 * 'src'') ->
+ − 1172
'src -> 'res2 * 'src''
+ − 1173
--| : ('src -> 'res1 * 'src') * ('src' -> 'res2 * 'src'') ->
+ − 1174
'src -> 'res1 * 'src''
+ − 1175
-- : ('src -> 'res1 * 'src') * ('src' -> 'res2 * 'src'') ->
+ − 1176
'src -> ('res1 * 'res2) * 'src''
+ − 1177
^^ : ('src -> string * 'src') * ('src' -> string * 'src'') ->
+ − 1178
'src -> string * 'src''
+ − 1179
\end{verbatim}
+ − 1180
These functions parse a result off the input source twice.
+ − 1181
+ − 1182
\texttt{|--} and \texttt{--|}
+ − 1183
return the first result and the second result, respectively.
+ − 1184
+ − 1185
\texttt{--} returns both.
+ − 1186
+ − 1187
\verb|^^| returns the result of concatenating the two results
+ − 1188
(which must be strings).
+ − 1189
+ − 1190
Note how, although the types
+ − 1191
\texttt{'src}, \texttt{'src'} and \texttt{'src''} will normally be the same,
+ − 1192
the types as shown help suggest the behaviour of the functions.
+ − 1193
\begin{verbatim}
+ − 1194
:-- : ('src -> 'res1 * 'src') * ('res1 -> 'src' -> 'res2 * 'src'') ->
+ − 1195
'src -> ('res1 * 'res2) * 'src''
+ − 1196
:|-- : ('src -> 'res1 * 'src') * ('res1 -> 'src' -> 'res2 * 'src'') ->
+ − 1197
'src -> 'res2 * 'src''
+ − 1198
\end{verbatim}
+ − 1199
These are similar to \texttt{|--} and \texttt{--|},
+ − 1200
except that the second parsing function can depend on the result of the first.
+ − 1201
\begin{verbatim}
+ − 1202
>> : ('src -> 'res1 * 'src') * ('res1 -> 'res2) -> 'src -> 'res2 * 'src'
+ − 1203
|| : ('src -> 'res_src) * ('src -> 'res_src) -> 'src -> 'res_src
+ − 1204
\end{verbatim}
+ − 1205
\texttt{p >> f} applies a function \texttt{f} to the result of a parse.
+ − 1206
+ − 1207
\texttt{||} tries a second parsing function if the first one
+ − 1208
fails by raising an exception of the form \texttt{FAIL \_}.
+ − 1209
+ − 1210
\begin{verbatim}
+ − 1211
succeed : 'res -> ('src -> 'res * 'src) ;
+ − 1212
fail : ('src -> 'res_src) ;
+ − 1213
!! : ('src * string option -> string) ->
+ − 1214
('src -> 'res_src) -> ('src -> 'res_src) ;
+ − 1215
\end{verbatim}
+ − 1216
\texttt{succeed r} returns \texttt{r}, with the input unchanged.
+ − 1217
\texttt{fail} always fails, raising exception \texttt{FAIL NONE}.
+ − 1218
\texttt{!! f} only affects the failure mode, turning a failure that
+ − 1219
raises \texttt{FAIL \_} into a failure that raises \texttt{ABORT ...}.
+ − 1220
This is used to prevent recovery from the failure ---
+ − 1221
thus, in \texttt{!! parse1 || parse2}, if \texttt{parse1} fails,
+ − 1222
it won't recover by trying \texttt{parse2}.
+ − 1223
+ − 1224
\begin{verbatim}
+ − 1225
one : ('si -> bool) -> ('si list -> 'si * 'si list) ;
+ − 1226
some : ('si -> 'res option) -> ('si list -> 'res * 'si list) ;
+ − 1227
\end{verbatim}
+ − 1228
These require the input to be a list of items:
+ − 1229
they fail, raising \texttt{MORE NONE} if the list is empty.
+ − 1230
On other failures they raise \texttt{FAIL NONE}
+ − 1231
+ − 1232
\texttt{one p} takes the first
+ − 1233
item from the list if it satisfies \texttt{p}, otherwise fails.
+ − 1234
+ − 1235
\texttt{some f} takes the first
+ − 1236
item from the list and applies \texttt{f} to it, failing if this returns
+ − 1237
\texttt{NONE}.
+ − 1238
+ − 1239
\begin{verbatim}
+ − 1240
many : ('si -> bool) -> 'si list -> 'si list * 'si list ;
+ − 1241
\end{verbatim}
+ − 1242
\texttt{many p} takes items from the input until it encounters one
+ − 1243
which does not satisfy \texttt{p}. If it reaches the end of the input
+ − 1244
it fails, raising \texttt{MORE NONE}.
+ − 1245
+ − 1246
\texttt{many1} (with the same type) fails if the first item
+ − 1247
does not satisfy \texttt{p}.
+ − 1248
+ − 1249
\begin{verbatim}
+ − 1250
option : ('src -> 'res * 'src) -> ('src -> 'res option * 'src)
+ − 1251
optional : ('src -> 'res * 'src) -> 'res -> ('src -> 'res * 'src)
+ − 1252
\end{verbatim}
+ − 1253
\texttt{option}:
+ − 1254
where the parser \texttt{f} succeeds with result \texttt{r}
+ − 1255
or raises \texttt{FAIL \_},
+ − 1256
\texttt{option f} gives the result \texttt{SOME r} or \texttt{NONE}.
+ − 1257
+ − 1258
\texttt{optional}: if parser \texttt{f} fails by raising \texttt{FAIL \_},
+ − 1259
\texttt{optional f default} provides the result \texttt{default}.
+ − 1260
+ − 1261
\begin{verbatim}
+ − 1262
repeat : ('src -> 'res * 'src) -> 'src -> 'res list * 'src
+ − 1263
repeat1 : ('src -> 'res * 'src) -> 'src -> 'res list * 'src
+ − 1264
bulk : ('src -> 'res * 'src) -> 'src -> 'res list * 'src
+ − 1265
\end{verbatim}
+ − 1266
\texttt{repeat f} repeatedly parses an item off the remaining input until
+ − 1267
\texttt{f} fails with \texttt{FAIL \_}
+ − 1268
+ − 1269
\texttt{repeat1} is as for \texttt{repeat}, but requires at least one
+ − 1270
successful parse.
+ − 1271
+ − 1272
\begin{verbatim}
+ − 1273
lift : ('src -> 'res * 'src) -> ('ex * 'src -> 'res * ('ex * 'src))
+ − 1274
\end{verbatim}
+ − 1275
\texttt{lift} changes the source type of a parser by putting in an extra
+ − 1276
component \texttt{'ex}, which is ignored in the parsing.
+ − 1277
+ − 1278
The \texttt{Scan} structure also provides the type \texttt{lexicon},
+ − 1279
HOW DO THEY WORK ?? TO BE COMPLETED
+ − 1280
\begin{verbatim}
+ − 1281
dest_lexicon: lexicon -> string list ;
+ − 1282
make_lexicon: string list list -> lexicon ;
+ − 1283
empty_lexicon: lexicon ;
+ − 1284
extend_lexicon: string list list -> lexicon -> lexicon ;
+ − 1285
merge_lexicons: lexicon -> lexicon -> lexicon ;
+ − 1286
is_literal: lexicon -> string list -> bool ;
+ − 1287
literal: lexicon -> string list -> string list * string list ;
+ − 1288
\end{verbatim}
+ − 1289
Two lexicons, for the commands and keywords, are stored and can be retrieved
+ − 1290
by:
+ − 1291
\begin{verbatim}
+ − 1292
val (command_lexicon, keyword_lexicon) = OuterSyntax.get_lexicons () ;
+ − 1293
val commands = Scan.dest_lexicon command_lexicon ;
+ − 1294
val keywords = Scan.dest_lexicon keyword_lexicon ;
+ − 1295
\end{verbatim}
+ − 1296
*}
+ − 1297
+ − 1298
section{* The \texttt{OuterLex} structure *}
+ − 1299
+ − 1300
text {*
+ − 1301
The source file is @{text "src/Pure/Isar/outer_lex.ML"}.
+ − 1302
In some other source files its name is abbreviated:
+ − 1303
\begin{verbatim}
+ − 1304
structure T = OuterLex;
+ − 1305
\end{verbatim}
+ − 1306
This structure defines the type \texttt{token}.
+ − 1307
(The types
+ − 1308
\texttt{OuterLex.token},
+ − 1309
\texttt{OuterParse.token} and
+ − 1310
\texttt{SpecParse.token} are all the same).
+ − 1311
+ − 1312
Input text is split up into tokens, and the input source type for many parsing
+ − 1313
functions is \texttt{token list}.
+ − 1314
220
+ − 1315
The data type definition (which is not published in the signature) is
4
+ − 1316
\begin{verbatim}
+ − 1317
datatype token = Token of Position.T * (token_kind * string);
+ − 1318
\end{verbatim}
+ − 1319
but here are some runnable examples for viewing tokens:
+ − 1320
+ − 1321
*}
+ − 1322
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1323
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1324
4
+ − 1325
69
+ − 1326
ML{*
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1327
val toks = OuterSyntax.scan Position.none
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1328
"theory,imports;begin x.y.z apply ?v1 ?'a 'a -- || 44 simp (* xx *) { * fff * }" ;
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1329
*}
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1330
69
+ − 1331
ML{*
4
+ − 1332
print_depth 20 ;
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1333
*}
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1334
69
+ − 1335
ML{*
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1336
map OuterLex.text_of toks ;
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1337
*}
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1338
69
+ − 1339
ML{*
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1340
val proper_toks = filter OuterLex.is_proper toks ;
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1341
*}
4
+ − 1342
69
+ − 1343
ML{*
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1344
map OuterLex.kind_of proper_toks
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1345
*}
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1346
69
+ − 1347
ML{*
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1348
map OuterLex.unparse proper_toks ;
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1349
*}
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1350
69
+ − 1351
ML{*
47
4daf913fdbe1
hakked latex so that it does not display ML {* *}; general tuning
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 1352
OuterLex.stopper
4
+ − 1353
*}
+ − 1354
+ − 1355
text {*
+ − 1356
+ − 1357
The function \texttt{is\_proper : token -> bool} identifies tokens which are
+ − 1358
not white space or comments: many parsing functions assume require spaces or
+ − 1359
comments to have been filtered out.
+ − 1360
+ − 1361
There is a special end-of-file token:
+ − 1362
\begin{verbatim}
+ − 1363
val (tok_eof : token, is_eof : token -> bool) = T.stopper ;
+ − 1364
(* end of file token *)
+ − 1365
\end{verbatim}
+ − 1366
+ − 1367
*}
+ − 1368
+ − 1369
section {* The \texttt{OuterParse} structure *}
+ − 1370
+ − 1371
text {*
+ − 1372
The source file is \texttt{src/Pure/Isar/outer\_parse.ML}.
+ − 1373
In some other source files its name is abbreviated:
+ − 1374
\begin{verbatim}
+ − 1375
structure P = OuterParse;
+ − 1376
\end{verbatim}
+ − 1377
Here the parsers use \texttt{token list} as the input source type.
+ − 1378
+ − 1379
Some of the parsers simply select the first token, provided that it is of the
+ − 1380
right kind (as returned by \texttt{T.kind\_of}): these are
+ − 1381
\texttt{ command, keyword, short\_ident, long\_ident, sym\_ident, term\_var,
+ − 1382
type\_ident, type\_var, number, string, alt\_string, verbatim, sync, eof}
+ − 1383
Others select the first token, provided that it is one of several kinds,
+ − 1384
(eg, \texttt{name, xname, text, typ}).
+ − 1385
+ − 1386
\begin{verbatim}
+ − 1387
type 'a tlp = token list -> 'a * token list ; (* token list parser *)
+ − 1388
$$$ : string -> string tlp
+ − 1389
nat : int tlp ;
+ − 1390
maybe : 'a tlp -> 'a option tlp ;
+ − 1391
\end{verbatim}
+ − 1392
+ − 1393
\texttt{\$\$\$ s} returns the first token,
+ − 1394
if it equals \texttt{s} \emph{and} \texttt{s} is a keyword.
+ − 1395
+ − 1396
\texttt{nat} returns the first token, if it is a number, and evaluates it.
+ − 1397
+ − 1398
\texttt{maybe}: if \texttt{p} returns \texttt{r},
+ − 1399
then \texttt{maybe p} returns \texttt{SOME r} ;
+ − 1400
if the first token is an underscore, it returns \texttt{NONE}.
+ − 1401
+ − 1402
A few examples:
+ − 1403
\begin{verbatim}
+ − 1404
P.list : 'a tlp -> 'a list tlp ; (* likewise P.list1 *)
+ − 1405
P.and_list : 'a tlp -> 'a list tlp ; (* likewise P.and_list1 *)
+ − 1406
val toks : token list = OuterSyntax.scan "44 ,_, 66,77" ;
+ − 1407
val proper_toks = List.filter T.is_proper toks ;
+ − 1408
P.list P.nat toks ; (* OK, doesn't recognize white space *)
+ − 1409
P.list P.nat proper_toks ; (* fails, doesn't recognize what follows ',' *)
+ − 1410
P.list (P.maybe P.nat) proper_toks ; (* fails, end of input *)
+ − 1411
P.list (P.maybe P.nat) (proper_toks @ [tok_eof]) ; (* OK *)
+ − 1412
val toks : token list = OuterSyntax.scan "44 and 55 and 66 and 77" ;
+ − 1413
P.and_list P.nat (List.filter T.is_proper toks @ [tok_eof]) ; (* ??? *)
+ − 1414
\end{verbatim}
+ − 1415
+ − 1416
The following code helps run examples:
+ − 1417
\begin{verbatim}
+ − 1418
fun parse_str tlp str =
+ − 1419
let val toks : token list = OuterSyntax.scan str ;
+ − 1420
val proper_toks = List.filter T.is_proper toks @ [tok_eof] ;
+ − 1421
val (res, rem_toks) = tlp proper_toks ;
+ − 1422
val rem_str = String.concat
+ − 1423
(Library.separate " " (List.map T.unparse rem_toks)) ;
+ − 1424
in (res, rem_str) end ;
+ − 1425
\end{verbatim}
+ − 1426
+ − 1427
Some examples from \texttt{src/Pure/Isar/outer\_parse.ML}
+ − 1428
\begin{verbatim}
+ − 1429
val type_args =
+ − 1430
type_ident >> Library.single ||
+ − 1431
$$$ "(" |-- !!! (list1 type_ident --| $$$ ")") ||
+ − 1432
Scan.succeed [];
+ − 1433
\end{verbatim}
+ − 1434
There are three ways parsing a list of type arguments can succeed.
+ − 1435
The first line reads a single type argument, and turns it into a singleton
+ − 1436
list.
+ − 1437
The second line reads "(", and then the remainder, ignoring the "(" ;
+ − 1438
the remainder consists of a list of type identifiers (at least one),
+ − 1439
and then a ")" which is also ignored.
+ − 1440
The \texttt{!!!} ensures that if the parsing proceeds this far and then fails,
+ − 1441
it won't try the third line (see the description of \texttt{Scan.!!}).
+ − 1442
The third line consumes no input and returns the empty list.
+ − 1443
+ − 1444
\begin{verbatim}
+ − 1445
fun triple2 (x, (y, z)) = (x, y, z);
+ − 1446
val arity = xname -- ($$$ "::" |-- !!! (
+ − 1447
Scan.optional ($$$ "(" |-- !!! (list1 sort --| $$$ ")")) []
+ − 1448
-- sort)) >> triple2;
+ − 1449
\end{verbatim}
+ − 1450
The parser \texttt{arity} reads a typename $t$, then ``\texttt{::}'' (which is
+ − 1451
ignored), then optionally a list $ss$ of sorts and then another sort $s$.
+ − 1452
The result $(t, (ss, s))$ is transformed by \texttt{triple2} to $(t, ss, s)$.
+ − 1453
The second line reads the optional list of sorts:
+ − 1454
it reads first ``\texttt{(}'' and last ``\texttt{)}'', which are both ignored,
+ − 1455
and between them a comma-separated list of sorts.
+ − 1456
If this list is absent, the default \texttt{[]} provides the list of sorts.
+ − 1457
+ − 1458
\begin{verbatim}
+ − 1459
parse_str P.type_args "('a, 'b) ntyp" ;
+ − 1460
parse_str P.type_args "'a ntyp" ;
+ − 1461
parse_str P.type_args "ntyp" ;
+ − 1462
parse_str P.arity "ty :: tycl" ;
+ − 1463
parse_str P.arity "ty :: (tycl1, tycl2) tycl" ;
+ − 1464
\end{verbatim}
+ − 1465
+ − 1466
*}
+ − 1467
+ − 1468
section {* The \texttt{SpecParse} structure *}
+ − 1469
+ − 1470
text {*
+ − 1471
The source file is \texttt{src/Pure/Isar/spec\_parse.ML}.
+ − 1472
This structure contains token list parsers for more complicated values.
+ − 1473
For example,
+ − 1474
\begin{verbatim}
+ − 1475
open SpecParse ;
+ − 1476
attrib : Attrib.src tok_rdr ;
+ − 1477
attribs : Attrib.src list tok_rdr ;
+ − 1478
opt_attribs : Attrib.src list tok_rdr ;
+ − 1479
xthm : (thmref * Attrib.src list) tok_rdr ;
+ − 1480
xthms1 : (thmref * Attrib.src list) list tok_rdr ;
+ − 1481
+ − 1482
parse_str attrib "simp" ;
+ − 1483
parse_str opt_attribs "hello" ;
+ − 1484
val (ass, "") = parse_str attribs "[standard, xxxx, simp, intro, OF sym]" ;
+ − 1485
map Args.dest_src ass ;
+ − 1486
val (asrc, "") = parse_str attrib "THEN trans [THEN sym]" ;
+ − 1487
+ − 1488
parse_str xthm "mythm [attr]" ;
+ − 1489
parse_str xthms1 "thm1 [attr] thms2" ;
+ − 1490
\end{verbatim}
+ − 1491
+ − 1492
As you can see, attributes are described using types of the \texttt{Args}
+ − 1493
structure, described below.
+ − 1494
*}
+ − 1495
+ − 1496
section{* The \texttt{Args} structure *}
+ − 1497
+ − 1498
text {*
+ − 1499
The source file is \texttt{src/Pure/Isar/args.ML}.
220
+ − 1500
The primary type of this structure is the \texttt{src} data type;
4
+ − 1501
the single constructors not published in the signature, but
+ − 1502
\texttt{Args.src} and \texttt{Args.dest\_src}
+ − 1503
are in fact the constructor and destructor functions.
+ − 1504
Note that the types \texttt{Attrib.src} and \texttt{Method.src}
+ − 1505
are in fact \texttt{Args.src}.
+ − 1506
+ − 1507
\begin{verbatim}
+ − 1508
src : (string * Args.T list) * Position.T -> Args.src ;
+ − 1509
dest_src : Args.src -> (string * Args.T list) * Position.T ;
+ − 1510
Args.pretty_src : Proof.context -> Args.src -> Pretty.T ;
+ − 1511
fun pr_src ctxt src = Pretty.string_of (Args.pretty_src ctxt src) ;
+ − 1512
+ − 1513
val thy = ML_Context.the_context () ;
+ − 1514
val ctxt = ProofContext.init thy ;
+ − 1515
map (pr_src ctxt) ass ;
+ − 1516
\end{verbatim}
+ − 1517
+ − 1518
So an \texttt{Args.src} consists of the first word, then a list of further
+ − 1519
``arguments'', of type \texttt{Args.T}, with information about position in the
+ − 1520
input.
+ − 1521
\begin{verbatim}
+ − 1522
(* how an Args.src is parsed *)
+ − 1523
P.position : 'a tlp -> ('a * Position.T) tlp ;
+ − 1524
P.arguments : Args.T list tlp ;
+ − 1525
+ − 1526
val parse_src : Args.src tlp =
+ − 1527
P.position (P.xname -- P.arguments) >> Args.src ;
+ − 1528
\end{verbatim}
+ − 1529
+ − 1530
\begin{verbatim}
+ − 1531
val ((first_word, args), pos) = Args.dest_src asrc ;
+ − 1532
map Args.string_of args ;
+ − 1533
\end{verbatim}
+ − 1534
+ − 1535
The \texttt{Args} structure contains more parsers and parser transformers
+ − 1536
for which the input source type is \texttt{Args.T list}. For example,
+ − 1537
\begin{verbatim}
+ − 1538
type 'a atlp = Args.T list -> 'a * Args.T list ;
+ − 1539
open Args ;
+ − 1540
nat : int atlp ; (* also Args.int *)
+ − 1541
thm_sel : PureThy.interval list atlp ;
+ − 1542
list : 'a atlp -> 'a list atlp ;
+ − 1543
attribs : (string -> string) -> Args.src list atlp ;
+ − 1544
opt_attribs : (string -> string) -> Args.src list atlp ;
+ − 1545
+ − 1546
(* parse_atl_str : 'a atlp -> (string -> 'a * string) ;
+ − 1547
given an Args.T list parser, to get a string parser *)
+ − 1548
fun parse_atl_str atlp str =
+ − 1549
let val (ats, rem_str) = parse_str P.arguments str ;
+ − 1550
val (res, rem_ats) = atlp ats ;
+ − 1551
in (res, String.concat (Library.separate " "
+ − 1552
(List.map Args.string_of rem_ats @ [rem_str]))) end ;
+ − 1553
+ − 1554
parse_atl_str Args.int "-1-," ;
+ − 1555
parse_atl_str (Scan.option Args.int) "x1-," ;
+ − 1556
parse_atl_str Args.thm_sel "(1-,4,13-22)" ;
+ − 1557
+ − 1558
val (ats as atsrc :: _, "") = parse_atl_str (Args.attribs I)
+ − 1559
"[THEN trans [THEN sym], simp, OF sym]" ;
+ − 1560
\end{verbatim}
+ − 1561
+ − 1562
From here, an attribute is interpreted using \texttt{Attrib.attribute}.
+ − 1563
+ − 1564
\texttt{Args} has a large number of functions which parse an \texttt{Args.src}
+ − 1565
and also refer to a generic context.
+ − 1566
Note the use of \texttt{Scan.lift} for this.
+ − 1567
(as does \texttt{Attrib} - RETHINK THIS)
+ − 1568
+ − 1569
(\texttt{Args.syntax} shown below has type specialised)
+ − 1570
+ − 1571
\begin{verbatim}
+ − 1572
type ('res, 'src) parse_fn = 'src -> 'res * 'src ;
+ − 1573
type 'a cgatlp = ('a, Context.generic * Args.T list) parse_fn ;
+ − 1574
Scan.lift : 'a atlp -> 'a cgatlp ;
+ − 1575
term : term cgatlp ;
+ − 1576
typ : typ cgatlp ;
+ − 1577
+ − 1578
Args.syntax : string -> 'res cgatlp -> src -> ('res, Context.generic) parse_fn ;
+ − 1579
Attrib.thm : thm cgatlp ;
+ − 1580
Attrib.thms : thm list cgatlp ;
+ − 1581
Attrib.multi_thm : thm list cgatlp ;
+ − 1582
+ − 1583
(* parse_cgatl_str : 'a cgatlp -> (string -> 'a * string) ;
+ − 1584
given a (Context.generic * Args.T list) parser, to get a string parser *)
+ − 1585
fun parse_cgatl_str cgatlp str =
+ − 1586
let
+ − 1587
(* use the current generic context *)
+ − 1588
val generic = Context.Theory thy ;
+ − 1589
val (ats, rem_str) = parse_str P.arguments str ;
+ − 1590
(* ignore any change to the generic context *)
+ − 1591
val (res, (_, rem_ats)) = cgatlp (generic, ats) ;
+ − 1592
in (res, String.concat (Library.separate " "
+ − 1593
(List.map Args.string_of rem_ats @ [rem_str]))) end ;
+ − 1594
\end{verbatim}
+ − 1595
*}
+ − 1596
+ − 1597
section{* Attributes, and the \texttt{Attrib} structure *}
+ − 1598
+ − 1599
text {*
+ − 1600
The type \texttt{attribute} is declared in \texttt{src/Pure/thm.ML}.
+ − 1601
The source file for the \texttt{Attrib} structure is
+ − 1602
\texttt{src/Pure/Isar/attrib.ML}.
+ − 1603
Most attributes use a theorem to change a generic context (for example,
+ − 1604
by declaring that the theorem should be used, by default, in simplification),
+ − 1605
or change a theorem (which most often involves referring to the current
+ − 1606
theory).
+ − 1607
The functions \texttt{Thm.rule\_attribute} and
+ − 1608
\texttt{Thm.declaration\_attribute} create attributes of these kinds.
+ − 1609
+ − 1610
\begin{verbatim}
+ − 1611
type attribute = Context.generic * thm -> Context.generic * thm;
+ − 1612
type 'a trf = 'a -> 'a ; (* transformer of a given type *)
+ − 1613
Thm.rule_attribute : (Context.generic -> thm -> thm) -> attribute ;
+ − 1614
Thm.declaration_attribute : (thm -> Context.generic trf) -> attribute ;
+ − 1615
+ − 1616
Attrib.print_attributes : theory -> unit ;
+ − 1617
Attrib.pretty_attribs : Proof.context -> src list -> Pretty.T list ;
+ − 1618
+ − 1619
List.app Pretty.writeln (Attrib.pretty_attribs ctxt ass) ;
+ − 1620
\end{verbatim}
+ − 1621
+ − 1622
An attribute is stored in a theory as indicated by:
+ − 1623
\begin{verbatim}
+ − 1624
Attrib.add_attributes :
+ − 1625
(bstring * (src -> attribute) * string) list -> theory trf ;
+ − 1626
(*
+ − 1627
Attrib.add_attributes [("THEN", THEN_att, "resolution with rule")] ;
+ − 1628
*)
+ − 1629
\end{verbatim}
+ − 1630
where the first and third arguments are name and description of the attribute,
+ − 1631
and the second is a function which parses the attribute input text
+ − 1632
(including the attribute name, which has necessarily already been parsed).
+ − 1633
Here, \texttt{THEN\_att} is a function declared in the code for the
+ − 1634
structure \texttt{Attrib}, but not published in its signature.
+ − 1635
The source file \texttt{src/Pure/Isar/attrib.ML} shows the use of
+ − 1636
\texttt{Attrib.add\_attributes} to add a number of attributes.
+ − 1637
+ − 1638
\begin{verbatim}
+ − 1639
FullAttrib.THEN_att : src -> attribute ;
+ − 1640
FullAttrib.THEN_att atsrc (generic, ML_Context.thm "sym") ;
+ − 1641
FullAttrib.THEN_att atsrc (generic, ML_Context.thm "all_comm") ;
+ − 1642
\end{verbatim}
+ − 1643
+ − 1644
\begin{verbatim}
+ − 1645
Attrib.syntax : attribute cgatlp -> src -> attribute ;
+ − 1646
Attrib.no_args : attribute -> src -> attribute ;
+ − 1647
\end{verbatim}
+ − 1648
When this is called as \texttt{syntax scan src (gc, th)}
+ − 1649
the generic context \texttt{gc} is used
+ − 1650
(and potentially changed to \texttt{gc'})
+ − 1651
by \texttt{scan} in parsing to obtain an attribute \texttt{attr} which would
+ − 1652
then be applied to \texttt{(gc', th)}.
+ − 1653
The source for parsing the attribute is the arguments part of \texttt{src},
+ − 1654
which must all be consumed by the parse.
+ − 1655
+ − 1656
For example, for \texttt{Attrib.no\_args attr src}, the attribute parser
+ − 1657
simply returns \texttt{attr}, requiring that the arguments part of
+ − 1658
\texttt{src} must be empty.
+ − 1659
+ − 1660
Some examples from \texttt{src/Pure/Isar/attrib.ML}, modified:
+ − 1661
\begin{verbatim}
+ − 1662
fun rot_att_n n (gc, th) = (gc, rotate_prems n th) ;
+ − 1663
rot_att_n : int -> attribute ;
+ − 1664
val rot_arg = Scan.lift (Scan.optional Args.int 1 : int atlp) : int cgatlp ;
+ − 1665
val rotated_att : src -> attribute =
+ − 1666
Attrib.syntax (rot_arg >> rot_att_n : attribute cgatlp) ;
+ − 1667
+ − 1668
val THEN_arg : int cgatlp = Scan.lift
+ − 1669
(Scan.optional (Args.bracks Args.nat : int atlp) 1 : int atlp) ;
+ − 1670
+ − 1671
Attrib.thm : thm cgatlp ;
+ − 1672
+ − 1673
THEN_arg -- Attrib.thm : (int * thm) cgatlp ;
+ − 1674
+ − 1675
fun THEN_att_n (n, tht) (gc, th) = (gc, th RSN (n, tht)) ;
+ − 1676
THEN_att_n : int * thm -> attribute ;
+ − 1677
+ − 1678
val THEN_att : src -> attribute = Attrib.syntax
+ − 1679
(THEN_arg -- Attrib.thm >> THEN_att_n : attribute cgatlp);
+ − 1680
\end{verbatim}
+ − 1681
The functions I've called \texttt{rot\_arg} and \texttt{THEN\_arg}
+ − 1682
read an optional argument, which for \texttt{rotated} is an integer,
+ − 1683
and for \texttt{THEN} is a natural enclosed in square brackets;
+ − 1684
the default, if the argument is absent, is 1 in each case.
+ − 1685
Functions \texttt{rot\_att\_n} and \texttt{THEN\_att\_n} turn these into
+ − 1686
attributes, where \texttt{THEN\_att\_n} also requires a theorem, which is
+ − 1687
parsed by \texttt{Attrib.thm}.
+ − 1688
Infix operators \texttt{--} and \texttt{>>} are in the structure \texttt{Scan}.
+ − 1689
+ − 1690
*}
+ − 1691
+ − 1692
section{* Methods, and the \texttt{Method} structure *}
+ − 1693
+ − 1694
text {*
+ − 1695
The source file is \texttt{src/Pure/Isar/method.ML}.
+ − 1696
The type \texttt{method} is defined by the datatype declaration
+ − 1697
\begin{verbatim}
+ − 1698
(* datatype method = Meth of thm list -> cases_tactic; *)
+ − 1699
RuleCases.NO_CASES : tactic -> cases_tactic ;
+ − 1700
\end{verbatim}
+ − 1701
In fact \texttt{RAW\_METHOD\_CASES} (below) is exactly the constructor
+ − 1702
\texttt{Meth}.
+ − 1703
A \texttt{cases\_tactic} is an elaborated version of a tactic.
+ − 1704
\texttt{NO\_CASES tac} is a \texttt{cases\_tactic} which consists of a
+ − 1705
\texttt{cases\_tactic} without any further case information.
+ − 1706
For further details see the description of structure \texttt{RuleCases} below.
+ − 1707
The list of theorems to be passed to a method consists of the current
+ − 1708
\emph{facts} in the proof.
+ − 1709
+ − 1710
\begin{verbatim}
+ − 1711
RAW_METHOD : (thm list -> tactic) -> method ;
+ − 1712
METHOD : (thm list -> tactic) -> method ;
+ − 1713
+ − 1714
SIMPLE_METHOD : tactic -> method ;
+ − 1715
SIMPLE_METHOD' : (int -> tactic) -> method ;
+ − 1716
SIMPLE_METHOD'' : ((int -> tactic) -> tactic) -> (int -> tactic) -> method ;
+ − 1717
+ − 1718
RAW_METHOD_CASES : (thm list -> cases_tactic) -> method ;
+ − 1719
METHOD_CASES : (thm list -> cases_tactic) -> method ;
+ − 1720
\end{verbatim}
+ − 1721
A method is, in its simplest form, a tactic; applying the method is to apply
+ − 1722
the tactic to the current goal state.
+ − 1723
+ − 1724
Applying \texttt{RAW\_METHOD tacf} creates a tactic by applying
+ − 1725
\texttt{tacf} to the current {facts}, and applying that tactic to the
+ − 1726
goal state.
+ − 1727
+ − 1728
\texttt{METHOD} is similar but also first applies
+ − 1729
\texttt{Goal.conjunction\_tac} to all subgoals.
+ − 1730
+ − 1731
\texttt{SIMPLE\_METHOD tac} inserts the facts into all subgoals and then
+ − 1732
applies \texttt{tacf}.
+ − 1733
+ − 1734
\texttt{SIMPLE\_METHOD' tacf} inserts the facts and then
+ − 1735
applies \texttt{tacf} to subgoal 1.
+ − 1736
+ − 1737
\texttt{SIMPLE\_METHOD'' quant tacf} does this for subgoal(s) selected by
+ − 1738
\texttt{quant}, which may be, for example,
+ − 1739
\texttt{ALLGOALS} (all subgoals),
+ − 1740
\texttt{TRYALL} (try all subgoals, failure is OK),
+ − 1741
\texttt{FIRSTGOAL} (try subgoals until it succeeds once),
+ − 1742
\texttt{(fn tacf => tacf 4)} (subgoal 4), etc
16
+ − 1743
(see the \texttt{Tactical} structure, FIXME) %%\cite[Chapter 4]{ref}).
4
+ − 1744
+ − 1745
A method is stored in a theory as indicated by:
+ − 1746
\begin{verbatim}
+ − 1747
Method.add_method :
+ − 1748
(bstring * (src -> Proof.context -> method) * string) -> theory trf ;
+ − 1749
( *
+ − 1750
* )
+ − 1751
\end{verbatim}
+ − 1752
where the first and third arguments are name and description of the method,
+ − 1753
and the second is a function which parses the method input text
+ − 1754
(including the method name, which has necessarily already been parsed).
+ − 1755
+ − 1756
Here, \texttt{xxx} is a function declared in the code for the
+ − 1757
structure \texttt{Method}, but not published in its signature.
+ − 1758
The source file \texttt{src/Pure/Isar/method.ML} shows the use of
+ − 1759
\texttt{Method.add\_method} to add a number of methods.
+ − 1760
+ − 1761
+ − 1762
*}
75
+ − 1763
(*>*)
220
+ − 1764
end