| author | wenzelm |
| Thu, 14 Apr 2016 16:59:47 +0200 | |
| changeset 62983 | ba9072b303a2 |
| parent 62969 | 9f394a16c557 |
| child 63138 | 70f4d67235a0 |
| permissions | -rw-r--r-- |
| 61656 | 1 |
(*:maxLineLen=78:*) |
2 |
||
| 28762 | 3 |
theory Inner_Syntax |
| 42651 | 4 |
imports Base Main |
| 28762 | 5 |
begin |
6 |
||
| 58618 | 7 |
chapter \<open>Inner syntax --- the term language \label{ch:inner-syntax}\<close>
|
| 28762 | 8 |
|
| 62106 | 9 |
text \<open> |
10 |
The inner syntax of Isabelle provides concrete notation for the main |
|
11 |
entities of the logical framework, notably \<open>\<lambda>\<close>-terms with types and type |
|
12 |
classes. Applications may either extend existing syntactic categories by |
|
13 |
additional notation, or define new sub-languages that are linked to the |
|
14 |
standard term language via some explicit markers. For example \<^verbatim>\<open>FOO\<close>~\<open>foo\<close> |
|
15 |
could embed the syntax corresponding for some user-defined nonterminal \<open>foo\<close> |
|
16 |
--- within the bounds of the given lexical syntax of Isabelle/Pure. |
|
| 46282 | 17 |
|
| 62106 | 18 |
The most basic way to specify concrete syntax for logical entities works via |
19 |
mixfix annotations (\secref{sec:mixfix}), which may be usually given as part
|
|
20 |
of the original declaration or via explicit notation commands later on |
|
21 |
(\secref{sec:notation}). This already covers many needs of concrete syntax
|
|
22 |
without having to understand the full complexity of inner syntax layers. |
|
| 46282 | 23 |
|
| 62106 | 24 |
Further details of the syntax engine involves the classical distinction of |
25 |
lexical language versus context-free grammar (see \secref{sec:pure-syntax}),
|
|
26 |
and various mechanisms for \<^emph>\<open>syntax transformations\<close> (see |
|
27 |
\secref{sec:syntax-transformations}).
|
|
| 58618 | 28 |
\<close> |
| 46282 | 29 |
|
30 |
||
| 58618 | 31 |
section \<open>Printing logical entities\<close> |
| 28762 | 32 |
|
| 58618 | 33 |
subsection \<open>Diagnostic commands \label{sec:print-diag}\<close>
|
| 28762 | 34 |
|
| 58618 | 35 |
text \<open> |
| 28762 | 36 |
\begin{matharray}{rcl}
|
| 61493 | 37 |
@{command_def "typ"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
|
38 |
@{command_def "term"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
|
|
39 |
@{command_def "prop"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
|
|
40 |
@{command_def "thm"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
|
|
41 |
@{command_def "prf"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
|
|
42 |
@{command_def "full_prf"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
|
|
43 |
@{command_def "print_state"}\<open>\<^sup>*\<close> & : & \<open>any \<rightarrow>\<close> \\
|
|
| 28762 | 44 |
\end{matharray}
|
45 |
||
46 |
These diagnostic commands assist interactive development by printing |
|
47 |
internal logical entities in a human-readable fashion. |
|
48 |
||
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
55108
diff
changeset
|
49 |
@{rail \<open>
|
| 48792 | 50 |
@@{command typ} @{syntax modes}? @{syntax type} ('::' @{syntax sort})?
|
| 28762 | 51 |
; |
|
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
52 |
@@{command term} @{syntax modes}? @{syntax term}
|
| 28762 | 53 |
; |
|
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
54 |
@@{command prop} @{syntax modes}? @{syntax prop}
|
| 28762 | 55 |
; |
| 62969 | 56 |
@@{command thm} @{syntax modes}? @{syntax thms}
|
| 28762 | 57 |
; |
| 62969 | 58 |
( @@{command prf} | @@{command full_prf} ) @{syntax modes}? @{syntax thms}?
|
| 28762 | 59 |
; |
| 52430 | 60 |
@@{command print_state} @{syntax modes}?
|
| 28762 | 61 |
; |
|
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
62 |
@{syntax_def modes}: '(' (@{syntax name} + ) ')'
|
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
55108
diff
changeset
|
63 |
\<close>} |
| 28762 | 64 |
|
| 62106 | 65 |
\<^descr> @{command "typ"}~\<open>\<tau>\<close> reads and prints a type expression according to the
|
66 |
current context. |
|
| 48792 | 67 |
|
| 62106 | 68 |
\<^descr> @{command "typ"}~\<open>\<tau> :: s\<close> uses type-inference to determine the most
|
69 |
general way to make \<open>\<tau>\<close> conform to sort \<open>s\<close>. For concrete \<open>\<tau>\<close> this checks if |
|
70 |
the type belongs to that sort. Dummy type parameters ``\<open>_\<close>'' (underscore) |
|
71 |
are assigned to fresh type variables with most general sorts, according the |
|
72 |
the principles of type-inference. |
|
|
28766
accab7594b8e
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28765
diff
changeset
|
73 |
|
| 62106 | 74 |
\<^descr> @{command "term"}~\<open>t\<close> and @{command "prop"}~\<open>\<phi>\<close> read, type-check and
|
75 |
print terms or propositions according to the current theory or proof |
|
76 |
context; the inferred type of \<open>t\<close> is output as well. Note that these |
|
77 |
commands are also useful in inspecting the current environment of term |
|
78 |
abbreviations. |
|
| 28762 | 79 |
|
| 62106 | 80 |
\<^descr> @{command "thm"}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close> retrieves theorems from the current theory
|
81 |
or proof context. Note that any attributes included in the theorem |
|
82 |
specifications are applied to a temporary context derived from the current |
|
83 |
theory or proof; the result is discarded, i.e.\ attributes involved in |
|
84 |
\<open>a\<^sub>1, \<dots>, a\<^sub>n\<close> do not have any permanent effect. |
|
| 28762 | 85 |
|
| 62106 | 86 |
\<^descr> @{command "prf"} displays the (compact) proof term of the current proof
|
87 |
state (if present), or of the given theorems. Note that this requires |
|
88 |
proof terms to be switched on for the current object logic (see the |
|
89 |
``Proof terms'' section of the Isabelle reference manual for information |
|
90 |
on how to do this). |
|
| 28762 | 91 |
|
| 62106 | 92 |
\<^descr> @{command "full_prf"} is like @{command "prf"}, but displays the full
|
93 |
proof term, i.e.\ also displays information omitted in the compact proof |
|
94 |
term, which is denoted by ``\<open>_\<close>'' placeholders there. |
|
| 28762 | 95 |
|
| 62106 | 96 |
\<^descr> @{command "print_state"} prints the current proof state (if present),
|
97 |
including current facts and goals. |
|
| 28762 | 98 |
|
| 61997 | 99 |
All of the diagnostic commands above admit a list of \<open>modes\<close> to be |
100 |
specified, which is appended to the current print mode; see also |
|
101 |
\secref{sec:print-modes}. Thus the output behavior may be modified according
|
|
102 |
particular print mode features. For example, @{command
|
|
103 |
"print_state"}~\<open>(latex)\<close> prints the current proof state with mathematical |
|
104 |
symbols and special characters represented in {\LaTeX} source, according to
|
|
105 |
the Isabelle style @{cite "isabelle-system"}.
|
|
| 28762 | 106 |
|
| 62106 | 107 |
Note that antiquotations (cf.\ \secref{sec:antiq}) provide a more systematic
|
108 |
way to include formal items into the printed text document. |
|
| 58618 | 109 |
\<close> |
| 28762 | 110 |
|
111 |
||
| 58618 | 112 |
subsection \<open>Details of printed content\<close> |
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
113 |
|
| 58618 | 114 |
text \<open> |
| 42655 | 115 |
\begin{tabular}{rcll}
|
| 61493 | 116 |
@{attribute_def show_markup} & : & \<open>attribute\<close> \\
|
117 |
@{attribute_def show_types} & : & \<open>attribute\<close> & default \<open>false\<close> \\
|
|
118 |
@{attribute_def show_sorts} & : & \<open>attribute\<close> & default \<open>false\<close> \\
|
|
119 |
@{attribute_def show_consts} & : & \<open>attribute\<close> & default \<open>false\<close> \\
|
|
120 |
@{attribute_def show_abbrevs} & : & \<open>attribute\<close> & default \<open>true\<close> \\
|
|
121 |
@{attribute_def show_brackets} & : & \<open>attribute\<close> & default \<open>false\<close> \\
|
|
122 |
@{attribute_def names_long} & : & \<open>attribute\<close> & default \<open>false\<close> \\
|
|
123 |
@{attribute_def names_short} & : & \<open>attribute\<close> & default \<open>false\<close> \\
|
|
124 |
@{attribute_def names_unique} & : & \<open>attribute\<close> & default \<open>true\<close> \\
|
|
125 |
@{attribute_def eta_contract} & : & \<open>attribute\<close> & default \<open>true\<close> \\
|
|
126 |
@{attribute_def goals_limit} & : & \<open>attribute\<close> & default \<open>10\<close> \\
|
|
127 |
@{attribute_def show_main_goal} & : & \<open>attribute\<close> & default \<open>false\<close> \\
|
|
128 |
@{attribute_def show_hyps} & : & \<open>attribute\<close> & default \<open>false\<close> \\
|
|
129 |
@{attribute_def show_tags} & : & \<open>attribute\<close> & default \<open>false\<close> \\
|
|
130 |
@{attribute_def show_question_marks} & : & \<open>attribute\<close> & default \<open>true\<close> \\
|
|
| 42655 | 131 |
\end{tabular}
|
| 61421 | 132 |
\<^medskip> |
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
133 |
|
| 62106 | 134 |
These configuration options control the detail of information that is |
135 |
displayed for types, terms, theorems, goals etc. See also |
|
| 42655 | 136 |
\secref{sec:config}.
|
|
28765
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
137 |
|
| 62106 | 138 |
\<^descr> @{attribute show_markup} controls direct inlining of markup into the
|
139 |
printed representation of formal entities --- notably type and sort |
|
140 |
constraints. This enables Prover IDE users to retrieve that information via |
|
141 |
tooltips or popups while hovering with the mouse over the output window, for |
|
142 |
example. Consequently, this option is enabled by default for Isabelle/jEdit. |
|
| 49699 | 143 |
|
| 62106 | 144 |
\<^descr> @{attribute show_types} and @{attribute show_sorts} control printing of
|
145 |
type constraints for term variables, and sort constraints for type |
|
146 |
variables. By default, neither of these are shown in output. If @{attribute
|
|
147 |
show_sorts} is enabled, types are always shown as well. In Isabelle/jEdit, |
|
148 |
manual setting of these options is normally not required thanks to |
|
149 |
@{attribute show_markup} above.
|
|
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
150 |
|
| 62106 | 151 |
Note that displaying types and sorts may explain why a polymorphic inference |
152 |
rule fails to resolve with some goal, or why a rewrite rule does not apply |
|
153 |
as expected. |
|
|
28765
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
154 |
|
| 62106 | 155 |
\<^descr> @{attribute show_consts} controls printing of types of constants when
|
156 |
displaying a goal state. |
|
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
157 |
|
| 62106 | 158 |
Note that the output can be enormous, because polymorphic constants often |
159 |
occur at several different type instances. |
|
160 |
||
161 |
\<^descr> @{attribute show_abbrevs} controls folding of constant abbreviations.
|
|
|
40879
ca132ef44944
configuration option "show_abbrevs" supersedes print mode "no_abbrevs", with inverted meaning;
wenzelm
parents:
40255
diff
changeset
|
162 |
|
| 62106 | 163 |
\<^descr> @{attribute show_brackets} controls bracketing in pretty printed output.
|
164 |
If enabled, all sub-expressions of the pretty printing tree will be |
|
165 |
parenthesized, even if this produces malformed term syntax! This crude way |
|
166 |
of showing the internal structure of pretty printed entities may |
|
167 |
occasionally help to diagnose problems with operator priorities, for |
|
168 |
example. |
|
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
169 |
|
| 62106 | 170 |
\<^descr> @{attribute names_long}, @{attribute names_short}, and @{attribute
|
171 |
names_unique} control the way of printing fully qualified internal names in |
|
172 |
external form. See also \secref{sec:antiq} for the document antiquotation
|
|
173 |
options of the same names. |
|
|
42358
b47d41d9f4b5
Name_Space: proper configuration options long_names, short_names, unique_names instead of former unsynchronized references;
wenzelm
parents:
42279
diff
changeset
|
174 |
|
| 62106 | 175 |
\<^descr> @{attribute eta_contract} controls \<open>\<eta>\<close>-contracted printing of terms.
|
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
176 |
|
| 62106 | 177 |
The \<open>\<eta>\<close>-contraction law asserts @{prop "(\<lambda>x. f x) \<equiv> f"}, provided \<open>x\<close> is not
|
178 |
free in \<open>f\<close>. It asserts \<^emph>\<open>extensionality\<close> of functions: @{prop "f \<equiv> g"} if
|
|
179 |
@{prop "f x \<equiv> g x"} for all \<open>x\<close>. Higher-order unification frequently puts
|
|
180 |
terms into a fully \<open>\<eta>\<close>-expanded form. For example, if \<open>F\<close> has type \<open>(\<tau> \<Rightarrow> \<tau>) |
|
181 |
\<Rightarrow> \<tau>\<close> then its expanded form is @{term "\<lambda>h. F (\<lambda>x. h x)"}.
|
|
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
182 |
|
| 62106 | 183 |
Enabling @{attribute eta_contract} makes Isabelle perform \<open>\<eta>\<close>-contractions
|
184 |
before printing, so that @{term "\<lambda>h. F (\<lambda>x. h x)"} appears simply as \<open>F\<close>.
|
|
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
185 |
|
| 62106 | 186 |
Note that the distinction between a term and its \<open>\<eta>\<close>-expanded form |
187 |
occasionally matters. While higher-order resolution and rewriting operate |
|
188 |
modulo \<open>\<alpha>\<beta>\<eta>\<close>-conversion, some other tools might look at terms more |
|
189 |
discretely. |
|
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
190 |
|
| 62106 | 191 |
\<^descr> @{attribute goals_limit} controls the maximum number of subgoals to be
|
192 |
printed. |
|
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
193 |
|
| 62106 | 194 |
\<^descr> @{attribute show_main_goal} controls whether the main result to be proven
|
195 |
should be displayed. This information might be relevant for schematic goals, |
|
196 |
to inspect the current claim that has been synthesized so far. |
|
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
197 |
|
| 62106 | 198 |
\<^descr> @{attribute show_hyps} controls printing of implicit hypotheses of local
|
199 |
facts. Normally, only those hypotheses are displayed that are \<^emph>\<open>not\<close> covered |
|
200 |
by the assumptions of the current context: this situation indicates a fault |
|
201 |
in some tool being used. |
|
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
202 |
|
| 62106 | 203 |
By enabling @{attribute show_hyps}, output of \<^emph>\<open>all\<close> hypotheses can be
|
204 |
enforced, which is occasionally useful for diagnostic purposes. |
|
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
205 |
|
| 62106 | 206 |
\<^descr> @{attribute show_tags} controls printing of extra annotations within
|
207 |
theorems, such as internal position information, or the case names being |
|
208 |
attached by the attribute @{attribute case_names}.
|
|
|
28765
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
209 |
|
| 62106 | 210 |
Note that the @{attribute tagged} and @{attribute untagged} attributes
|
211 |
provide low-level access to the collection of tags associated with a |
|
212 |
theorem. |
|
|
28765
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
213 |
|
| 62106 | 214 |
\<^descr> @{attribute show_question_marks} controls printing of question marks for
|
215 |
schematic variables, such as \<open>?x\<close>. Only the leading question mark is |
|
216 |
affected, the remaining text is unchanged (including proper markup for |
|
217 |
schematic variables that might be relevant for user interfaces). |
|
| 58618 | 218 |
\<close> |
|
28765
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
219 |
|
|
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
220 |
|
| 58618 | 221 |
subsection \<open>Alternative print modes \label{sec:print-modes}\<close>
|
| 46284 | 222 |
|
| 58618 | 223 |
text \<open> |
| 46284 | 224 |
\begin{mldecls}
|
225 |
@{index_ML print_mode_value: "unit -> string list"} \\
|
|
226 |
@{index_ML Print_Mode.with_modes: "string list -> ('a -> 'b) -> 'a -> 'b"} \\
|
|
227 |
\end{mldecls}
|
|
228 |
||
| 62106 | 229 |
The \<^emph>\<open>print mode\<close> facility allows to modify various operations for printing. |
230 |
Commands like @{command typ}, @{command term}, @{command thm} (see
|
|
231 |
\secref{sec:print-diag}) take additional print modes as optional argument.
|
|
232 |
The underlying ML operations are as follows. |
|
| 46284 | 233 |
|
| 62106 | 234 |
\<^descr> @{ML "print_mode_value ()"} yields the list of currently active print
|
235 |
mode names. This should be understood as symbolic representation of |
|
236 |
certain individual features for printing (with precedence from left to |
|
237 |
right). |
|
| 46284 | 238 |
|
| 62106 | 239 |
\<^descr> @{ML Print_Mode.with_modes}~\<open>modes f x\<close> evaluates \<open>f x\<close> in an execution
|
240 |
context where the print mode is prepended by the given \<open>modes\<close>. This |
|
241 |
provides a thread-safe way to augment print modes. It is also monotonic in |
|
242 |
the set of mode names: it retains the default print mode that certain |
|
243 |
user-interfaces might have installed for their proper functioning! |
|
| 46284 | 244 |
|
| 61421 | 245 |
\<^medskip> |
| 62106 | 246 |
The pretty printer for inner syntax maintains alternative mixfix productions |
247 |
for any print mode name invented by the user, say in commands like @{command
|
|
248 |
notation} or @{command abbreviation}. Mode names can be arbitrary, but the
|
|
249 |
following ones have a specific meaning by convention: |
|
| 46284 | 250 |
|
| 62106 | 251 |
\<^item> \<^verbatim>\<open>""\<close> (the empty string): default mode; implicitly active as last |
252 |
element in the list of modes. |
|
| 46284 | 253 |
|
| 62106 | 254 |
\<^item> \<^verbatim>\<open>input\<close>: dummy print mode that is never active; may be used to specify |
255 |
notation that is only available for input. |
|
| 46284 | 256 |
|
| 62106 | 257 |
\<^item> \<^verbatim>\<open>internal\<close> dummy print mode that is never active; used internally in |
258 |
Isabelle/Pure. |
|
| 46284 | 259 |
|
| 62106 | 260 |
\<^item> \<^verbatim>\<open>ASCII\<close>: prefer ASCII art over mathematical symbols. |
| 46284 | 261 |
|
| 62106 | 262 |
\<^item> \<^verbatim>\<open>latex\<close>: additional mode that is active in {\LaTeX} document
|
263 |
preparation of Isabelle theory sources; allows to provide alternative |
|
264 |
output notation. |
|
| 58618 | 265 |
\<close> |
| 46284 | 266 |
|
267 |
||
| 58618 | 268 |
section \<open>Mixfix annotations \label{sec:mixfix}\<close>
|
| 28762 | 269 |
|
| 62106 | 270 |
text \<open> |
271 |
Mixfix annotations specify concrete \<^emph>\<open>inner syntax\<close> of Isabelle types and |
|
272 |
terms. Locally fixed parameters in toplevel theorem statements, locale and |
|
273 |
class specifications also admit mixfix annotations in a fairly uniform |
|
274 |
manner. A mixfix annotation describes the concrete syntax, the translation |
|
275 |
to abstract syntax, and the pretty printing. Special case annotations |
|
276 |
provide a simple means of specifying infix operators and binders. |
|
| 46290 | 277 |
|
| 62106 | 278 |
Isabelle mixfix syntax is inspired by {\OBJ} @{cite OBJ}. It allows to
|
279 |
specify any context-free priority grammar, which is more general than the |
|
280 |
fixity declarations of ML and Prolog. |
|
| 28762 | 281 |
|
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
55108
diff
changeset
|
282 |
@{rail \<open>
|
|
51654
8450b944e58a
just one syntax category "mixfix" -- check structure annotation semantically;
wenzelm
parents:
50636
diff
changeset
|
283 |
@{syntax_def mixfix}: '('
|
| 58761 | 284 |
(@{syntax template} prios? @{syntax nat}? |
|
285 |
(@'infix' | @'infixl' | @'infixr') @{syntax template} @{syntax nat} |
|
|
286 |
@'binder' @{syntax template} prios? @{syntax nat} |
|
|
287 |
@'structure') ')' |
|
| 46290 | 288 |
; |
| 62807 | 289 |
@{syntax template}: string
|
| 46289 | 290 |
; |
|
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
291 |
prios: '[' (@{syntax nat} + ',') ']'
|
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
55108
diff
changeset
|
292 |
\<close>} |
| 28762 | 293 |
|
| 62106 | 294 |
The string given as \<open>template\<close> may include literal text, spacing, blocks, |
295 |
and arguments (denoted by ``\<open>_\<close>''); the special symbol ``\<^verbatim>\<open>\<index>\<close>'' (printed as |
|
296 |
``\<open>\<index>\<close>'') represents an index argument that specifies an implicit @{keyword
|
|
297 |
"structure"} reference (see also \secref{sec:locale}). Only locally fixed
|
|
298 |
variables may be declared as @{keyword "structure"}.
|
|
|
51657
3db1bbc82d8d
more accurate documentation of "(structure)" mixfix;
wenzelm
parents:
51654
diff
changeset
|
299 |
|
| 62106 | 300 |
Infix and binder declarations provide common abbreviations for particular |
301 |
mixfix declarations. So in practice, mixfix templates mostly degenerate to |
|
302 |
literal text for concrete syntax, such as ``\<^verbatim>\<open>++\<close>'' for an infix symbol. |
|
| 61503 | 303 |
\<close> |
| 28762 | 304 |
|
| 46290 | 305 |
|
| 58618 | 306 |
subsection \<open>The general mixfix form\<close> |
| 46290 | 307 |
|
| 62106 | 308 |
text \<open> |
309 |
In full generality, mixfix declarations work as follows. Suppose a constant |
|
310 |
\<open>c :: \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>\<close> is annotated by \<open>(mixfix [p\<^sub>1, \<dots>, p\<^sub>n] p)\<close>, where |
|
311 |
\<open>mixfix\<close> is a string \<open>d\<^sub>0 _ d\<^sub>1 _ \<dots> _ d\<^sub>n\<close> consisting of delimiters that |
|
312 |
surround argument positions as indicated by underscores. |
|
| 28762 | 313 |
|
| 62106 | 314 |
Altogether this determines a production for a context-free priority grammar, |
315 |
where for each argument \<open>i\<close> the syntactic category is determined by \<open>\<tau>\<^sub>i\<close> |
|
316 |
(with priority \<open>p\<^sub>i\<close>), and the result category is determined from \<open>\<tau>\<close> (with |
|
317 |
priority \<open>p\<close>). Priority specifications are optional, with default 0 for |
|
318 |
arguments and 1000 for the result.\<^footnote>\<open>Omitting priorities is prone to |
|
319 |
syntactic ambiguities unless the delimiter tokens determine fully bracketed |
|
320 |
notation, as in \<open>if _ then _ else _ fi\<close>.\<close> |
|
| 28762 | 321 |
|
| 62106 | 322 |
Since \<open>\<tau>\<close> may be again a function type, the constant type scheme may have |
323 |
more argument positions than the mixfix pattern. Printing a nested |
|
324 |
application \<open>c t\<^sub>1 \<dots> t\<^sub>m\<close> for \<open>m > n\<close> works by attaching concrete notation |
|
325 |
only to the innermost part, essentially by printing \<open>(c t\<^sub>1 \<dots> t\<^sub>n) \<dots> t\<^sub>m\<close> |
|
326 |
instead. If a term has fewer arguments than specified in the mixfix |
|
| 28762 | 327 |
template, the concrete syntax is ignored. |
328 |
||
| 61421 | 329 |
\<^medskip> |
| 62106 | 330 |
A mixfix template may also contain additional directives for pretty |
331 |
printing, notably spaces, blocks, and breaks. The general template format is |
|
332 |
a sequence over any of the following entities. |
|
| 28762 | 333 |
|
| 62106 | 334 |
\<^descr> \<open>d\<close> is a delimiter, namely a non-empty sequence of characters other than |
335 |
the following special characters: |
|
| 28762 | 336 |
|
| 61421 | 337 |
\<^medskip> |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
338 |
\begin{tabular}{ll}
|
| 61503 | 339 |
\<^verbatim>\<open>'\<close> & single quote \\ |
340 |
\<^verbatim>\<open>_\<close> & underscore \\ |
|
| 61493 | 341 |
\<open>\<index>\<close> & index symbol \\ |
| 61503 | 342 |
\<^verbatim>\<open>(\<close> & open parenthesis \\ |
343 |
\<^verbatim>\<open>)\<close> & close parenthesis \\ |
|
344 |
\<^verbatim>\<open>/\<close> & slash \\ |
|
| 62807 | 345 |
\<open>\<open> \<close>\<close> & cartouche delimiters \\ |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
346 |
\end{tabular}
|
| 61421 | 347 |
\<^medskip> |
| 28762 | 348 |
|
| 62106 | 349 |
\<^descr> \<^verbatim>\<open>'\<close> escapes the special meaning of these meta-characters, producing a |
350 |
literal version of the following character, unless that is a blank. |
|
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
351 |
|
| 62106 | 352 |
A single quote followed by a blank separates delimiters, without affecting |
353 |
printing, but input tokens may have additional white space here. |
|
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
354 |
|
| 62106 | 355 |
\<^descr> \<^verbatim>\<open>_\<close> is an argument position, which stands for a certain syntactic |
356 |
category in the underlying grammar. |
|
| 28762 | 357 |
|
| 62106 | 358 |
\<^descr> \<open>\<index>\<close> is an indexed argument position; this is the place where implicit |
359 |
structure arguments can be attached. |
|
| 28762 | 360 |
|
| 62106 | 361 |
\<^descr> \<open>s\<close> is a non-empty sequence of spaces for printing. This and the following |
362 |
specifications do not affect parsing at all. |
|
363 |
||
| 62807 | 364 |
\<^descr> \<^verbatim>\<open>(\<close>\<open>n\<close> opens a pretty printing block. The optional natural number |
365 |
specifies the block indentation, i.e. how much spaces to add when a line |
|
366 |
break occurs within the block. The default indentation is 0. |
|
367 |
||
368 |
\<^descr> \<^verbatim>\<open>(\<close>\<open>\<open>properties\<close>\<close> opens a pretty printing block, with properties |
|
369 |
specified within the given text cartouche. The syntax and semantics of |
|
370 |
the category @{syntax_ref mixfix_properties} is described below.
|
|
| 28762 | 371 |
|
| 61503 | 372 |
\<^descr> \<^verbatim>\<open>)\<close> closes a pretty printing block. |
| 28762 | 373 |
|
| 61503 | 374 |
\<^descr> \<^verbatim>\<open>//\<close> forces a line break. |
| 28762 | 375 |
|
| 62106 | 376 |
\<^descr> \<^verbatim>\<open>/\<close>\<open>s\<close> allows a line break. Here \<open>s\<close> stands for the string of spaces |
377 |
(zero or more) right after the slash. These spaces are printed if the break |
|
378 |
is \<^emph>\<open>not\<close> taken. |
|
| 28762 | 379 |
|
380 |
||
| 62807 | 381 |
\<^medskip> |
382 |
Block properties allow more control over the details of pretty-printed |
|
383 |
output. The concrete syntax is defined as follows. |
|
384 |
||
385 |
@{rail \<open>
|
|
386 |
@{syntax_def "mixfix_properties"}: (entry *)
|
|
387 |
; |
|
388 |
entry: atom ('=' atom)?
|
|
389 |
; |
|
390 |
atom: @{syntax ident} | @{syntax int} | @{syntax float} | @{syntax cartouche}
|
|
391 |
\<close>} |
|
392 |
||
393 |
Each @{syntax entry} is a name-value pair: if the value is omitted, if
|
|
394 |
defaults to \<^verbatim>\<open>true\<close> (intended for Boolean properties). The following |
|
395 |
standard block properties are supported: |
|
396 |
||
397 |
\<^item> \<open>indent\<close> (natural number): the block indentation --- the same as for the |
|
398 |
simple syntax without block properties. |
|
399 |
||
400 |
\<^item> \<open>consistent\<close> (Boolean): this block has consistent breaks (if one break |
|
401 |
is taken, all breaks are taken). |
|
402 |
||
403 |
\<^item> \<open>unbreakable\<close> (Boolean): all possible breaks of the block are disabled |
|
404 |
(turned into spaces). |
|
405 |
||
406 |
\<^item> \<open>markup\<close> (string): the optional name of the markup node. If this is |
|
407 |
provided, all remaining properties are turned into its XML attributes. |
|
408 |
This allows to specify free-form PIDE markup, e.g.\ for specialized |
|
409 |
output. |
|
410 |
||
411 |
\<^medskip> |
|
412 |
Note that the general idea of pretty printing with blocks and breaks is |
|
413 |
described in @{cite "paulson-ml2"}; it goes back to @{cite "Oppen:1980"}.
|
|
| 58618 | 414 |
\<close> |
| 28762 | 415 |
|
416 |
||
| 58618 | 417 |
subsection \<open>Infixes\<close> |
| 46290 | 418 |
|
| 62106 | 419 |
text \<open> |
420 |
Infix operators are specified by convenient short forms that abbreviate |
|
421 |
general mixfix annotations as follows: |
|
| 46290 | 422 |
|
423 |
\begin{center}
|
|
424 |
\begin{tabular}{lll}
|
|
425 |
||
| 61503 | 426 |
\<^verbatim>\<open>(\<close>@{keyword_def "infix"}~\<^verbatim>\<open>"\<close>\<open>sy\<close>\<^verbatim>\<open>"\<close> \<open>p\<close>\<^verbatim>\<open>)\<close>
|
| 61493 | 427 |
& \<open>\<mapsto>\<close> & |
| 61503 | 428 |
\<^verbatim>\<open>("(_\<close>~\<open>sy\<close>\<^verbatim>\<open>/ _)" [\<close>\<open>p + 1\<close>\<^verbatim>\<open>,\<close>~\<open>p + 1\<close>\<^verbatim>\<open>]\<close>~\<open>p\<close>\<^verbatim>\<open>)\<close> \\
|
429 |
\<^verbatim>\<open>(\<close>@{keyword_def "infixl"}~\<^verbatim>\<open>"\<close>\<open>sy\<close>\<^verbatim>\<open>"\<close> \<open>p\<close>\<^verbatim>\<open>)\<close>
|
|
| 61493 | 430 |
& \<open>\<mapsto>\<close> & |
| 61503 | 431 |
\<^verbatim>\<open>("(_\<close>~\<open>sy\<close>\<^verbatim>\<open>/ _)" [\<close>\<open>p\<close>\<^verbatim>\<open>,\<close>~\<open>p + 1\<close>\<^verbatim>\<open>]\<close>~\<open>p\<close>\<^verbatim>\<open>)\<close> \\
|
432 |
\<^verbatim>\<open>(\<close>@{keyword_def "infixr"}~\<^verbatim>\<open>"\<close>\<open>sy\<close>\<^verbatim>\<open>"\<close>~\<open>p\<close>\<^verbatim>\<open>)\<close>
|
|
| 61493 | 433 |
& \<open>\<mapsto>\<close> & |
| 61503 | 434 |
\<^verbatim>\<open>("(_\<close>~\<open>sy\<close>\<^verbatim>\<open>/ _)" [\<close>\<open>p + 1\<close>\<^verbatim>\<open>,\<close>~\<open>p\<close>\<^verbatim>\<open>]\<close>~\<open>p\<close>\<^verbatim>\<open>)\<close> \\
|
| 46290 | 435 |
|
436 |
\end{tabular}
|
|
437 |
\end{center}
|
|
438 |
||
| 62106 | 439 |
The mixfix template \<^verbatim>\<open>"(_\<close>~\<open>sy\<close>\<^verbatim>\<open>/ _)"\<close> specifies two argument positions; |
440 |
the delimiter is preceded by a space and followed by a space or line break; |
|
441 |
the entire phrase is a pretty printing block. |
|
| 46290 | 442 |
|
| 62106 | 443 |
The alternative notation \<^verbatim>\<open>op\<close>~\<open>sy\<close> is introduced in addition. Thus any |
444 |
infix operator may be written in prefix form (as in ML), independently of |
|
445 |
the number of arguments in the term. |
|
| 58618 | 446 |
\<close> |
| 46290 | 447 |
|
448 |
||
| 58618 | 449 |
subsection \<open>Binders\<close> |
| 46290 | 450 |
|
| 62106 | 451 |
text \<open> |
452 |
A \<^emph>\<open>binder\<close> is a variable-binding construct such as a quantifier. The idea |
|
453 |
to formalize \<open>\<forall>x. b\<close> as \<open>All (\<lambda>x. b)\<close> for \<open>All :: ('a \<Rightarrow> bool) \<Rightarrow> bool\<close>
|
|
454 |
already goes back to @{cite church40}. Isabelle declarations of certain
|
|
455 |
higher-order operators may be annotated with @{keyword_def "binder"}
|
|
456 |
annotations as follows: |
|
| 46290 | 457 |
|
458 |
\begin{center}
|
|
| 61503 | 459 |
\<open>c ::\<close>~\<^verbatim>\<open>"\<close>\<open>(\<tau>\<^sub>1 \<Rightarrow> \<tau>\<^sub>2) \<Rightarrow> \<tau>\<^sub>3\<close>\<^verbatim>\<open>" (\<close>@{keyword "binder"}~\<^verbatim>\<open>"\<close>\<open>sy\<close>\<^verbatim>\<open>" [\<close>\<open>p\<close>\<^verbatim>\<open>]\<close>~\<open>q\<close>\<^verbatim>\<open>)\<close>
|
| 46290 | 460 |
\end{center}
|
461 |
||
| 62106 | 462 |
This introduces concrete binder syntax \<open>sy x. b\<close>, where \<open>x\<close> is a bound |
463 |
variable of type \<open>\<tau>\<^sub>1\<close>, the body \<open>b\<close> has type \<open>\<tau>\<^sub>2\<close> and the whole term has |
|
464 |
type \<open>\<tau>\<^sub>3\<close>. The optional integer \<open>p\<close> specifies the syntactic priority of the |
|
465 |
body; the default is \<open>q\<close>, which is also the priority of the whole construct. |
|
| 46290 | 466 |
|
467 |
Internally, the binder syntax is expanded to something like this: |
|
468 |
\begin{center}
|
|
| 61503 | 469 |
\<open>c_binder ::\<close>~\<^verbatim>\<open>"\<close>\<open>idts \<Rightarrow> \<tau>\<^sub>2 \<Rightarrow> \<tau>\<^sub>3\<close>\<^verbatim>\<open>" ("(3\<close>\<open>sy\<close>\<^verbatim>\<open>_./ _)" [0,\<close>~\<open>p\<close>\<^verbatim>\<open>]\<close>~\<open>q\<close>\<^verbatim>\<open>)\<close>
|
| 46290 | 470 |
\end{center}
|
471 |
||
472 |
Here @{syntax (inner) idts} is the nonterminal symbol for a list of
|
|
473 |
identifiers with optional type constraints (see also |
|
| 62106 | 474 |
\secref{sec:pure-grammar}). The mixfix template \<^verbatim>\<open>"(3\<close>\<open>sy\<close>\<^verbatim>\<open>_./ _)"\<close> defines
|
475 |
argument positions for the bound identifiers and the body, separated by a |
|
476 |
dot with optional line break; the entire phrase is a pretty printing block |
|
477 |
of indentation level 3. Note that there is no extra space after \<open>sy\<close>, so it |
|
478 |
needs to be included user specification if the binder syntax ends with a |
|
479 |
token that may be continued by an identifier token at the start of @{syntax
|
|
480 |
(inner) idts}. |
|
| 46290 | 481 |
|
| 62106 | 482 |
Furthermore, a syntax translation to transforms \<open>c_binder x\<^sub>1 \<dots> x\<^sub>n b\<close> into |
483 |
iterated application \<open>c (\<lambda>x\<^sub>1. \<dots> c (\<lambda>x\<^sub>n. b)\<dots>)\<close>. This works in both |
|
484 |
directions, for parsing and printing. |
|
485 |
\<close> |
|
| 46290 | 486 |
|
487 |
||
| 58618 | 488 |
section \<open>Explicit notation \label{sec:notation}\<close>
|
| 28762 | 489 |
|
| 58618 | 490 |
text \<open> |
| 28762 | 491 |
\begin{matharray}{rcll}
|
| 61493 | 492 |
@{command_def "type_notation"} & : & \<open>local_theory \<rightarrow> local_theory\<close> \\
|
493 |
@{command_def "no_type_notation"} & : & \<open>local_theory \<rightarrow> local_theory\<close> \\
|
|
494 |
@{command_def "notation"} & : & \<open>local_theory \<rightarrow> local_theory\<close> \\
|
|
495 |
@{command_def "no_notation"} & : & \<open>local_theory \<rightarrow> local_theory\<close> \\
|
|
496 |
@{command_def "write"} & : & \<open>proof(state) \<rightarrow> proof(state)\<close> \\
|
|
| 28762 | 497 |
\end{matharray}
|
498 |
||
| 62106 | 499 |
Commands that introduce new logical entities (terms or types) usually allow |
500 |
to provide mixfix annotations on the spot, which is convenient for default |
|
501 |
notation. Nonetheless, the syntax may be modified later on by declarations |
|
502 |
for explicit notation. This allows to add or delete mixfix annotations for |
|
503 |
of existing logical entities within the current context. |
|
| 46288 | 504 |
|
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
55108
diff
changeset
|
505 |
@{rail \<open>
|
|
59783
00b62aa9f430
tuned syntax diagrams -- no duplication of "target";
wenzelm
parents:
58842
diff
changeset
|
506 |
(@@{command type_notation} | @@{command no_type_notation}) @{syntax mode}? \<newline>
|
| 62969 | 507 |
(@{syntax name} @{syntax mixfix} + @'and')
|
| 35413 | 508 |
; |
|
59783
00b62aa9f430
tuned syntax diagrams -- no duplication of "target";
wenzelm
parents:
58842
diff
changeset
|
509 |
(@@{command notation} | @@{command no_notation}) @{syntax mode}? \<newline>
|
| 62969 | 510 |
(@{syntax name} @{syntax mixfix} + @'and')
|
| 28762 | 511 |
; |
| 62969 | 512 |
@@{command write} @{syntax mode}? (@{syntax name} @{syntax mixfix} + @'and')
|
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
55108
diff
changeset
|
513 |
\<close>} |
| 28762 | 514 |
|
| 62106 | 515 |
\<^descr> @{command "type_notation"}~\<open>c (mx)\<close> associates mixfix syntax with an
|
516 |
existing type constructor. The arity of the constructor is retrieved from |
|
517 |
the context. |
|
| 46282 | 518 |
|
| 62106 | 519 |
\<^descr> @{command "no_type_notation"} is similar to @{command "type_notation"},
|
520 |
but removes the specified syntax annotation from the present context. |
|
| 35413 | 521 |
|
| 62106 | 522 |
\<^descr> @{command "notation"}~\<open>c (mx)\<close> associates mixfix syntax with an existing
|
523 |
constant or fixed variable. The type declaration of the given entity is |
|
524 |
retrieved from the context. |
|
| 46282 | 525 |
|
| 62106 | 526 |
\<^descr> @{command "no_notation"} is similar to @{command "notation"}, but removes
|
527 |
the specified syntax annotation from the present context. |
|
| 28762 | 528 |
|
| 62106 | 529 |
\<^descr> @{command "write"} is similar to @{command "notation"}, but works within
|
530 |
an Isar proof body. |
|
| 58618 | 531 |
\<close> |
| 28762 | 532 |
|
| 28778 | 533 |
|
| 58618 | 534 |
section \<open>The Pure syntax \label{sec:pure-syntax}\<close>
|
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
535 |
|
| 58618 | 536 |
subsection \<open>Lexical matters \label{sec:inner-lex}\<close>
|
| 46282 | 537 |
|
| 62106 | 538 |
text \<open> |
539 |
The inner lexical syntax vaguely resembles the outer one |
|
540 |
(\secref{sec:outer-lex}), but some details are different. There are two main
|
|
541 |
categories of inner syntax tokens: |
|
| 46282 | 542 |
|
| 62106 | 543 |
\<^enum> \<^emph>\<open>delimiters\<close> --- the literal tokens occurring in productions of the given |
544 |
priority grammar (cf.\ \secref{sec:priority-grammar});
|
|
| 46282 | 545 |
|
| 61477 | 546 |
\<^enum> \<^emph>\<open>named tokens\<close> --- various categories of identifiers etc. |
| 46282 | 547 |
|
548 |
||
| 62106 | 549 |
Delimiters override named tokens and may thus render certain identifiers |
550 |
inaccessible. Sometimes the logical context admits alternative ways to refer |
|
551 |
to the same entity, potentially via qualified names. |
|
| 46282 | 552 |
|
| 61421 | 553 |
\<^medskip> |
| 62106 | 554 |
The categories for named tokens are defined once and for all as follows, |
555 |
reusing some categories of the outer token syntax (\secref{sec:outer-lex}).
|
|
| 46282 | 556 |
|
557 |
\begin{center}
|
|
558 |
\begin{supertabular}{rcl}
|
|
559 |
@{syntax_def (inner) id} & = & @{syntax_ref ident} \\
|
|
560 |
@{syntax_def (inner) longid} & = & @{syntax_ref longident} \\
|
|
561 |
@{syntax_def (inner) var} & = & @{syntax_ref var} \\
|
|
562 |
@{syntax_def (inner) tid} & = & @{syntax_ref typefree} \\
|
|
563 |
@{syntax_def (inner) tvar} & = & @{syntax_ref typevar} \\
|
|
|
58410
6d46ad54a2ab
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents:
58409
diff
changeset
|
564 |
@{syntax_def (inner) num_token} & = & @{syntax_ref nat} \\
|
| 61503 | 565 |
@{syntax_def (inner) float_token} & = & @{syntax_ref nat}\<^verbatim>\<open>.\<close>@{syntax_ref nat} \\
|
566 |
@{syntax_def (inner) str_token} & = & \<^verbatim>\<open>''\<close> \<open>\<dots>\<close> \<^verbatim>\<open>''\<close> \\
|
|
567 |
@{syntax_def (inner) string_token} & = & \<^verbatim>\<open>"\<close> \<open>\<dots>\<close> \<^verbatim>\<open>"\<close> \\
|
|
| 61493 | 568 |
@{syntax_def (inner) cartouche} & = & @{verbatim "\<open>"} \<open>\<dots>\<close> @{verbatim "\<close>"} \\
|
| 46282 | 569 |
\end{supertabular}
|
570 |
\end{center}
|
|
571 |
||
572 |
The token categories @{syntax (inner) num_token}, @{syntax (inner)
|
|
| 58421 | 573 |
float_token}, @{syntax (inner) str_token}, @{syntax (inner) string_token},
|
574 |
and @{syntax (inner) cartouche} are not used in Pure. Object-logics may
|
|
575 |
implement numerals and string literals by adding appropriate syntax |
|
576 |
declarations, together with some translation functions (e.g.\ see @{file
|
|
577 |
"~~/src/HOL/Tools/string_syntax.ML"}). |
|
| 46282 | 578 |
|
| 58421 | 579 |
The derived categories @{syntax_def (inner) num_const}, and @{syntax_def
|
580 |
(inner) float_const}, provide robust access to the respective tokens: the |
|
581 |
syntax tree holds a syntactic constant instead of a free variable. |
|
| 58618 | 582 |
\<close> |
| 46282 | 583 |
|
584 |
||
| 58618 | 585 |
subsection \<open>Priority grammars \label{sec:priority-grammar}\<close>
|
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
586 |
|
| 62106 | 587 |
text \<open> |
588 |
A context-free grammar consists of a set of \<^emph>\<open>terminal symbols\<close>, a set of |
|
589 |
\<^emph>\<open>nonterminal symbols\<close> and a set of \<^emph>\<open>productions\<close>. Productions have the |
|
590 |
form \<open>A = \<gamma>\<close>, where \<open>A\<close> is a nonterminal and \<open>\<gamma>\<close> is a string of terminals |
|
591 |
and nonterminals. One designated nonterminal is called the \<^emph>\<open>root symbol\<close>. |
|
592 |
The language defined by the grammar consists of all strings of terminals |
|
593 |
that can be derived from the root symbol by applying productions as rewrite |
|
594 |
rules. |
|
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
595 |
|
| 62106 | 596 |
The standard Isabelle parser for inner syntax uses a \<^emph>\<open>priority grammar\<close>. |
597 |
Each nonterminal is decorated by an integer priority: \<open>A\<^sup>(\<^sup>p\<^sup>)\<close>. In a |
|
598 |
derivation, \<open>A\<^sup>(\<^sup>p\<^sup>)\<close> may be rewritten using a production \<open>A\<^sup>(\<^sup>q\<^sup>) = \<gamma>\<close> only |
|
599 |
if \<open>p \<le> q\<close>. Any priority grammar can be translated into a normal |
|
600 |
context-free grammar by introducing new nonterminals and productions. |
|
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
601 |
|
| 61421 | 602 |
\<^medskip> |
| 62106 | 603 |
Formally, a set of context free productions \<open>G\<close> induces a derivation |
604 |
relation \<open>\<longrightarrow>\<^sub>G\<close> as follows. Let \<open>\<alpha>\<close> and \<open>\<beta>\<close> denote strings of terminal or |
|
605 |
nonterminal symbols. Then \<open>\<alpha> A\<^sup>(\<^sup>p\<^sup>) \<beta> \<longrightarrow>\<^sub>G \<alpha> \<gamma> \<beta>\<close> holds if and only if \<open>G\<close> |
|
| 61493 | 606 |
contains some production \<open>A\<^sup>(\<^sup>q\<^sup>) = \<gamma>\<close> for \<open>p \<le> q\<close>. |
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
607 |
|
| 61421 | 608 |
\<^medskip> |
| 62106 | 609 |
The following grammar for arithmetic expressions demonstrates how binding |
610 |
power and associativity of operators can be enforced by priorities. |
|
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
611 |
|
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
612 |
\begin{center}
|
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
613 |
\begin{tabular}{rclr}
|
| 61503 | 614 |
\<open>A\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>)\<close> & \<open>=\<close> & \<^verbatim>\<open>(\<close> \<open>A\<^sup>(\<^sup>0\<^sup>)\<close> \<^verbatim>\<open>)\<close> \\ |
615 |
\<open>A\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>)\<close> & \<open>=\<close> & \<^verbatim>\<open>0\<close> \\ |
|
616 |
\<open>A\<^sup>(\<^sup>0\<^sup>)\<close> & \<open>=\<close> & \<open>A\<^sup>(\<^sup>0\<^sup>)\<close> \<^verbatim>\<open>+\<close> \<open>A\<^sup>(\<^sup>1\<^sup>)\<close> \\ |
|
617 |
\<open>A\<^sup>(\<^sup>2\<^sup>)\<close> & \<open>=\<close> & \<open>A\<^sup>(\<^sup>3\<^sup>)\<close> \<^verbatim>\<open>*\<close> \<open>A\<^sup>(\<^sup>2\<^sup>)\<close> \\ |
|
618 |
\<open>A\<^sup>(\<^sup>3\<^sup>)\<close> & \<open>=\<close> & \<^verbatim>\<open>-\<close> \<open>A\<^sup>(\<^sup>3\<^sup>)\<close> \\ |
|
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
619 |
\end{tabular}
|
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
620 |
\end{center}
|
| 62106 | 621 |
The choice of priorities determines that \<^verbatim>\<open>-\<close> binds tighter than \<^verbatim>\<open>*\<close>, which |
622 |
binds tighter than \<^verbatim>\<open>+\<close>. Furthermore \<^verbatim>\<open>+\<close> associates to the left and \<^verbatim>\<open>*\<close> to |
|
623 |
the right. |
|
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
624 |
|
| 61421 | 625 |
\<^medskip> |
626 |
For clarity, grammars obey these conventions: |
|
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
627 |
|
| 62106 | 628 |
\<^item> All priorities must lie between 0 and 1000. |
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
629 |
|
| 62106 | 630 |
\<^item> Priority 0 on the right-hand side and priority 1000 on the left-hand |
631 |
side may be omitted. |
|
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
632 |
|
| 62106 | 633 |
\<^item> The production \<open>A\<^sup>(\<^sup>p\<^sup>) = \<alpha>\<close> is written as \<open>A = \<alpha> (p)\<close>, i.e.\ the |
634 |
priority of the left-hand side actually appears in a column on the far |
|
635 |
right. |
|
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
636 |
|
| 62106 | 637 |
\<^item> Alternatives are separated by \<open>|\<close>. |
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
638 |
|
| 62106 | 639 |
\<^item> Repetition is indicated by dots \<open>(\<dots>)\<close> in an informal but obvious way. |
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
640 |
|
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
641 |
Using these conventions, the example grammar specification above |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
642 |
takes the form: |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
643 |
\begin{center}
|
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
644 |
\begin{tabular}{rclc}
|
| 61503 | 645 |
\<open>A\<close> & \<open>=\<close> & \<^verbatim>\<open>(\<close> \<open>A\<close> \<^verbatim>\<open>)\<close> \\ |
646 |
& \<open>|\<close> & \<^verbatim>\<open>0\<close> & \qquad\qquad \\ |
|
647 |
& \<open>|\<close> & \<open>A\<close> \<^verbatim>\<open>+\<close> \<open>A\<^sup>(\<^sup>1\<^sup>)\<close> & \<open>(0)\<close> \\ |
|
648 |
& \<open>|\<close> & \<open>A\<^sup>(\<^sup>3\<^sup>)\<close> \<^verbatim>\<open>*\<close> \<open>A\<^sup>(\<^sup>2\<^sup>)\<close> & \<open>(2)\<close> \\ |
|
649 |
& \<open>|\<close> & \<^verbatim>\<open>-\<close> \<open>A\<^sup>(\<^sup>3\<^sup>)\<close> & \<open>(3)\<close> \\ |
|
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
650 |
\end{tabular}
|
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
651 |
\end{center}
|
| 58618 | 652 |
\<close> |
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
653 |
|
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
654 |
|
| 58618 | 655 |
subsection \<open>The Pure grammar \label{sec:pure-grammar}\<close>
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
656 |
|
| 62106 | 657 |
text \<open> |
658 |
The priority grammar of the \<open>Pure\<close> theory is defined approximately like |
|
659 |
this: |
|
| 28774 | 660 |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
661 |
\begin{center}
|
| 28773 | 662 |
\begin{supertabular}{rclr}
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
663 |
|
| 61493 | 664 |
@{syntax_def (inner) any} & = & \<open>prop | logic\<close> \\\\
|
| 28772 | 665 |
|
| 61503 | 666 |
@{syntax_def (inner) prop} & = & \<^verbatim>\<open>(\<close> \<open>prop\<close> \<^verbatim>\<open>)\<close> \\
|
667 |
& \<open>|\<close> & \<open>prop\<^sup>(\<^sup>4\<^sup>)\<close> \<^verbatim>\<open>::\<close> \<open>type\<close> & \<open>(3)\<close> \\ |
|
668 |
& \<open>|\<close> & \<open>any\<^sup>(\<^sup>3\<^sup>)\<close> \<^verbatim>\<open>==\<close> \<open>any\<^sup>(\<^sup>3\<^sup>)\<close> & \<open>(2)\<close> \\ |
|
| 61493 | 669 |
& \<open>|\<close> & \<open>any\<^sup>(\<^sup>3\<^sup>)\<close> \<open>\<equiv>\<close> \<open>any\<^sup>(\<^sup>3\<^sup>)\<close> & \<open>(2)\<close> \\ |
| 61503 | 670 |
& \<open>|\<close> & \<open>prop\<^sup>(\<^sup>3\<^sup>)\<close> \<^verbatim>\<open>&&&\<close> \<open>prop\<^sup>(\<^sup>2\<^sup>)\<close> & \<open>(2)\<close> \\ |
671 |
& \<open>|\<close> & \<open>prop\<^sup>(\<^sup>2\<^sup>)\<close> \<^verbatim>\<open>==>\<close> \<open>prop\<^sup>(\<^sup>1\<^sup>)\<close> & \<open>(1)\<close> \\ |
|
| 61493 | 672 |
& \<open>|\<close> & \<open>prop\<^sup>(\<^sup>2\<^sup>)\<close> \<open>\<Longrightarrow>\<close> \<open>prop\<^sup>(\<^sup>1\<^sup>)\<close> & \<open>(1)\<close> \\ |
| 61503 | 673 |
& \<open>|\<close> & \<^verbatim>\<open>[|\<close> \<open>prop\<close> \<^verbatim>\<open>;\<close> \<open>\<dots>\<close> \<^verbatim>\<open>;\<close> \<open>prop\<close> \<^verbatim>\<open>|]\<close> \<^verbatim>\<open>==>\<close> \<open>prop\<^sup>(\<^sup>1\<^sup>)\<close> & \<open>(1)\<close> \\ |
674 |
& \<open>|\<close> & \<open>\<lbrakk>\<close> \<open>prop\<close> \<^verbatim>\<open>;\<close> \<open>\<dots>\<close> \<^verbatim>\<open>;\<close> \<open>prop\<close> \<open>\<rbrakk>\<close> \<open>\<Longrightarrow>\<close> \<open>prop\<^sup>(\<^sup>1\<^sup>)\<close> & \<open>(1)\<close> \\ |
|
675 |
& \<open>|\<close> & \<^verbatim>\<open>!!\<close> \<open>idts\<close> \<^verbatim>\<open>.\<close> \<open>prop\<close> & \<open>(0)\<close> \\ |
|
676 |
& \<open>|\<close> & \<open>\<And>\<close> \<open>idts\<close> \<^verbatim>\<open>.\<close> \<open>prop\<close> & \<open>(0)\<close> \\ |
|
677 |
& \<open>|\<close> & \<^verbatim>\<open>OFCLASS\<close> \<^verbatim>\<open>(\<close> \<open>type\<close> \<^verbatim>\<open>,\<close> \<open>logic\<close> \<^verbatim>\<open>)\<close> \\ |
|
678 |
& \<open>|\<close> & \<^verbatim>\<open>SORT_CONSTRAINT\<close> \<^verbatim>\<open>(\<close> \<open>type\<close> \<^verbatim>\<open>)\<close> \\ |
|
679 |
& \<open>|\<close> & \<^verbatim>\<open>TERM\<close> \<open>logic\<close> \\ |
|
680 |
& \<open>|\<close> & \<^verbatim>\<open>PROP\<close> \<open>aprop\<close> \\\\ |
|
| 28772 | 681 |
|
| 61503 | 682 |
@{syntax_def (inner) aprop} & = & \<^verbatim>\<open>(\<close> \<open>aprop\<close> \<^verbatim>\<open>)\<close> \\
|
683 |
& \<open>|\<close> & \<open>id | longid | var |\<close>~~\<^verbatim>\<open>_\<close>~~\<open>|\<close>~~\<^verbatim>\<open>...\<close> \\ |
|
684 |
& \<open>|\<close> & \<^verbatim>\<open>CONST\<close> \<open>id |\<close>~~\<^verbatim>\<open>CONST\<close> \<open>longid\<close> \\ |
|
685 |
& \<open>|\<close> & \<^verbatim>\<open>XCONST\<close> \<open>id |\<close>~~\<^verbatim>\<open>XCONST\<close> \<open>longid\<close> \\ |
|
| 61493 | 686 |
& \<open>|\<close> & \<open>logic\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>) any\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>) \<dots> any\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>)\<close> & \<open>(999)\<close> \\\\ |
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
687 |
|
| 61503 | 688 |
@{syntax_def (inner) logic} & = & \<^verbatim>\<open>(\<close> \<open>logic\<close> \<^verbatim>\<open>)\<close> \\
|
689 |
& \<open>|\<close> & \<open>logic\<^sup>(\<^sup>4\<^sup>)\<close> \<^verbatim>\<open>::\<close> \<open>type\<close> & \<open>(3)\<close> \\ |
|
690 |
& \<open>|\<close> & \<open>id | longid | var |\<close>~~\<^verbatim>\<open>_\<close>~~\<open>|\<close>~~\<^verbatim>\<open>...\<close> \\ |
|
691 |
& \<open>|\<close> & \<^verbatim>\<open>CONST\<close> \<open>id |\<close>~~\<^verbatim>\<open>CONST\<close> \<open>longid\<close> \\ |
|
692 |
& \<open>|\<close> & \<^verbatim>\<open>XCONST\<close> \<open>id |\<close>~~\<^verbatim>\<open>XCONST\<close> \<open>longid\<close> \\ |
|
| 61493 | 693 |
& \<open>|\<close> & \<open>logic\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>) any\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>) \<dots> any\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>)\<close> & \<open>(999)\<close> \\ |
| 61503 | 694 |
& \<open>|\<close> & \<^verbatim>\<open>%\<close> \<open>pttrns\<close> \<^verbatim>\<open>.\<close> \<open>any\<^sup>(\<^sup>3\<^sup>)\<close> & \<open>(3)\<close> \\ |
695 |
& \<open>|\<close> & \<open>\<lambda>\<close> \<open>pttrns\<close> \<^verbatim>\<open>.\<close> \<open>any\<^sup>(\<^sup>3\<^sup>)\<close> & \<open>(3)\<close> \\ |
|
696 |
& \<open>|\<close> & \<^verbatim>\<open>op\<close> \<^verbatim>\<open>==\<close>~~\<open>|\<close>~~\<^verbatim>\<open>op\<close> \<open>\<equiv>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>op\<close> \<^verbatim>\<open>&&&\<close> \\ |
|
697 |
& \<open>|\<close> & \<^verbatim>\<open>op\<close> \<^verbatim>\<open>==>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>op\<close> \<open>\<Longrightarrow>\<close> \\ |
|
698 |
& \<open>|\<close> & \<^verbatim>\<open>TYPE\<close> \<^verbatim>\<open>(\<close> \<open>type\<close> \<^verbatim>\<open>)\<close> \\\\ |
|
| 28772 | 699 |
|
| 61503 | 700 |
@{syntax_def (inner) idt} & = & \<^verbatim>\<open>(\<close> \<open>idt\<close> \<^verbatim>\<open>)\<close>~~\<open>| id |\<close>~~\<^verbatim>\<open>_\<close> \\
|
701 |
& \<open>|\<close> & \<open>id\<close> \<^verbatim>\<open>::\<close> \<open>type\<close> & \<open>(0)\<close> \\ |
|
702 |
& \<open>|\<close> & \<^verbatim>\<open>_\<close> \<^verbatim>\<open>::\<close> \<open>type\<close> & \<open>(0)\<close> \\\\ |
|
| 28772 | 703 |
|
| 61503 | 704 |
@{syntax_def (inner) index} & = & \<^verbatim>\<open>\<^bsub>\<close> \<open>logic\<^sup>(\<^sup>0\<^sup>)\<close> \<^verbatim>\<open>\<^esub>\<close>~~\<open>| | \<index>\<close> \\\\
|
| 46287 | 705 |
|
| 61493 | 706 |
@{syntax_def (inner) idts} & = & \<open>idt | idt\<^sup>(\<^sup>1\<^sup>) idts\<close> & \<open>(0)\<close> \\\\
|
| 28772 | 707 |
|
| 61493 | 708 |
@{syntax_def (inner) pttrn} & = & \<open>idt\<close> \\\\
|
| 28772 | 709 |
|
| 61493 | 710 |
@{syntax_def (inner) pttrns} & = & \<open>pttrn | pttrn\<^sup>(\<^sup>1\<^sup>) pttrns\<close> & \<open>(0)\<close> \\\\
|
| 28774 | 711 |
|
| 61503 | 712 |
@{syntax_def (inner) type} & = & \<^verbatim>\<open>(\<close> \<open>type\<close> \<^verbatim>\<open>)\<close> \\
|
713 |
& \<open>|\<close> & \<open>tid | tvar |\<close>~~\<^verbatim>\<open>_\<close> \\ |
|
714 |
& \<open>|\<close> & \<open>tid\<close> \<^verbatim>\<open>::\<close> \<open>sort | tvar\<close>~~\<^verbatim>\<open>::\<close> \<open>sort |\<close>~~\<^verbatim>\<open>_\<close> \<^verbatim>\<open>::\<close> \<open>sort\<close> \\ |
|
| 61493 | 715 |
& \<open>|\<close> & \<open>type_name | type\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>) type_name\<close> \\ |
| 61503 | 716 |
& \<open>|\<close> & \<^verbatim>\<open>(\<close> \<open>type\<close> \<^verbatim>\<open>,\<close> \<open>\<dots>\<close> \<^verbatim>\<open>,\<close> \<open>type\<close> \<^verbatim>\<open>)\<close> \<open>type_name\<close> \\ |
717 |
& \<open>|\<close> & \<open>type\<^sup>(\<^sup>1\<^sup>)\<close> \<^verbatim>\<open>=>\<close> \<open>type\<close> & \<open>(0)\<close> \\ |
|
| 61493 | 718 |
& \<open>|\<close> & \<open>type\<^sup>(\<^sup>1\<^sup>)\<close> \<open>\<Rightarrow>\<close> \<open>type\<close> & \<open>(0)\<close> \\ |
| 61503 | 719 |
& \<open>|\<close> & \<^verbatim>\<open>[\<close> \<open>type\<close> \<^verbatim>\<open>,\<close> \<open>\<dots>\<close> \<^verbatim>\<open>,\<close> \<open>type\<close> \<^verbatim>\<open>]\<close> \<^verbatim>\<open>=>\<close> \<open>type\<close> & \<open>(0)\<close> \\ |
720 |
& \<open>|\<close> & \<^verbatim>\<open>[\<close> \<open>type\<close> \<^verbatim>\<open>,\<close> \<open>\<dots>\<close> \<^verbatim>\<open>,\<close> \<open>type\<close> \<^verbatim>\<open>]\<close> \<open>\<Rightarrow>\<close> \<open>type\<close> & \<open>(0)\<close> \\ |
|
| 61493 | 721 |
@{syntax_def (inner) type_name} & = & \<open>id | longid\<close> \\\\
|
| 28772 | 722 |
|
| 61503 | 723 |
@{syntax_def (inner) sort} & = & @{syntax class_name}~~\<open>|\<close>~~\<^verbatim>\<open>{}\<close> \\
|
724 |
& \<open>|\<close> & \<^verbatim>\<open>{\<close> @{syntax class_name} \<^verbatim>\<open>,\<close> \<open>\<dots>\<close> \<^verbatim>\<open>,\<close> @{syntax class_name} \<^verbatim>\<open>}\<close> \\
|
|
| 61493 | 725 |
@{syntax_def (inner) class_name} & = & \<open>id | longid\<close> \\
|
| 28773 | 726 |
\end{supertabular}
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
727 |
\end{center}
|
|
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
728 |
|
| 61421 | 729 |
\<^medskip> |
| 62106 | 730 |
Here literal terminals are printed \<^verbatim>\<open>verbatim\<close>; see also |
731 |
\secref{sec:inner-lex} for further token categories of the inner syntax. The
|
|
732 |
meaning of the nonterminals defined by the above grammar is as follows: |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
733 |
|
| 61439 | 734 |
\<^descr> @{syntax_ref (inner) any} denotes any term.
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
735 |
|
| 62106 | 736 |
\<^descr> @{syntax_ref (inner) prop} denotes meta-level propositions, which are
|
737 |
terms of type @{typ prop}. The syntax of such formulae of the meta-logic is
|
|
738 |
carefully distinguished from usual conventions for object-logics. In |
|
739 |
particular, plain \<open>\<lambda>\<close>-term notation is \<^emph>\<open>not\<close> recognized as @{syntax (inner)
|
|
740 |
prop}. |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
741 |
|
| 62106 | 742 |
\<^descr> @{syntax_ref (inner) aprop} denotes atomic propositions, which are
|
743 |
embedded into regular @{syntax (inner) prop} by means of an explicit \<^verbatim>\<open>PROP\<close>
|
|
744 |
token. |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
745 |
|
| 62106 | 746 |
Terms of type @{typ prop} with non-constant head, e.g.\ a plain variable,
|
747 |
are printed in this form. Constants that yield type @{typ prop} are expected
|
|
748 |
to provide their own concrete syntax; otherwise the printed version will |
|
749 |
appear like @{syntax (inner) logic} and cannot be parsed again as @{syntax
|
|
750 |
(inner) prop}. |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
751 |
|
| 62106 | 752 |
\<^descr> @{syntax_ref (inner) logic} denotes arbitrary terms of a logical type,
|
753 |
excluding type @{typ prop}. This is the main syntactic category of
|
|
754 |
object-logic entities, covering plain \<open>\<lambda>\<close>-term notation (variables, |
|
755 |
abstraction, application), plus anything defined by the user. |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
756 |
|
| 62106 | 757 |
When specifying notation for logical entities, all logical types (excluding |
758 |
@{typ prop}) are \<^emph>\<open>collapsed\<close> to this single category of @{syntax (inner)
|
|
759 |
logic}. |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
760 |
|
| 62106 | 761 |
\<^descr> @{syntax_ref (inner) index} denotes an optional index term for indexed
|
762 |
syntax. If omitted, it refers to the first @{keyword_ref "structure"}
|
|
763 |
variable in the context. The special dummy ``\<open>\<index>\<close>'' serves as pattern |
|
764 |
variable in mixfix annotations that introduce indexed notation. |
|
| 46287 | 765 |
|
| 62106 | 766 |
\<^descr> @{syntax_ref (inner) idt} denotes identifiers, possibly constrained by
|
767 |
types. |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
768 |
|
| 62106 | 769 |
\<^descr> @{syntax_ref (inner) idts} denotes a sequence of @{syntax_ref (inner)
|
770 |
idt}. This is the most basic category for variables in iterated binders, |
|
771 |
such as \<open>\<lambda>\<close> or \<open>\<And>\<close>. |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
772 |
|
| 62106 | 773 |
\<^descr> @{syntax_ref (inner) pttrn} and @{syntax_ref (inner) pttrns} denote
|
774 |
patterns for abstraction, cases bindings etc. In Pure, these categories |
|
775 |
start as a merely copy of @{syntax (inner) idt} and @{syntax (inner) idts},
|
|
776 |
respectively. Object-logics may add additional productions for binding |
|
777 |
forms. |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
778 |
|
| 61439 | 779 |
\<^descr> @{syntax_ref (inner) type} denotes types of the meta-logic.
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
780 |
|
| 61439 | 781 |
\<^descr> @{syntax_ref (inner) sort} denotes meta-level sorts.
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
782 |
|
|
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
783 |
|
| 28774 | 784 |
Here are some further explanations of certain syntax features. |
| 28773 | 785 |
|
| 62106 | 786 |
\<^item> In @{syntax (inner) idts}, note that \<open>x :: nat y\<close> is parsed as \<open>x :: (nat
|
787 |
y)\<close>, treating \<open>y\<close> like a type constructor applied to \<open>nat\<close>. To avoid this |
|
788 |
interpretation, write \<open>(x :: nat) y\<close> with explicit parentheses. |
|
| 28773 | 789 |
|
| 62106 | 790 |
\<^item> Similarly, \<open>x :: nat y :: nat\<close> is parsed as \<open>x :: (nat y :: nat)\<close>. The |
791 |
correct form is \<open>(x :: nat) (y :: nat)\<close>, or \<open>(x :: nat) y :: nat\<close> if \<open>y\<close> is |
|
792 |
last in the sequence of identifiers. |
|
| 28773 | 793 |
|
| 62106 | 794 |
\<^item> Type constraints for terms bind very weakly. For example, \<open>x < y :: nat\<close> |
795 |
is normally parsed as \<open>(x < y) :: nat\<close>, unless \<open><\<close> has a very low priority, |
|
796 |
in which case the input is likely to be ambiguous. The correct form is \<open>x < |
|
797 |
(y :: nat)\<close>. |
|
| 28773 | 798 |
|
| 61421 | 799 |
\<^item> Dummy variables (written as underscore) may occur in different |
| 28774 | 800 |
roles. |
| 28773 | 801 |
|
| 62106 | 802 |
\<^descr> A type ``\<open>_\<close>'' or ``\<open>_ :: sort\<close>'' acts like an anonymous inference |
803 |
parameter, which is filled-in according to the most general type produced |
|
804 |
by the type-checking phase. |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
805 |
|
| 62106 | 806 |
\<^descr> A bound ``\<open>_\<close>'' refers to a vacuous abstraction, where the body does not |
807 |
refer to the binding introduced here. As in the term @{term "\<lambda>x _. x"},
|
|
808 |
which is \<open>\<alpha>\<close>-equivalent to \<open>\<lambda>x y. x\<close>. |
|
| 28773 | 809 |
|
| 62106 | 810 |
\<^descr> A free ``\<open>_\<close>'' refers to an implicit outer binding. Higher definitional |
811 |
packages usually allow forms like \<open>f x _ = x\<close>. |
|
| 28773 | 812 |
|
| 62106 | 813 |
\<^descr> A schematic ``\<open>_\<close>'' (within a term pattern, see \secref{sec:term-decls})
|
814 |
refers to an anonymous variable that is implicitly abstracted over its |
|
815 |
context of locally bound variables. For example, this allows pattern |
|
816 |
matching of \<open>{x. f x = g x}\<close> against \<open>{x. _ = _}\<close>, or even \<open>{_. _ = _}\<close> by
|
|
| 61458 | 817 |
using both bound and schematic dummies. |
| 28773 | 818 |
|
| 62106 | 819 |
\<^descr> The three literal dots ``\<^verbatim>\<open>...\<close>'' may be also written as ellipsis symbol |
820 |
\<^verbatim>\<open>\<dots>\<close>. In both cases this refers to a special schematic variable, which is |
|
821 |
bound in the context. This special term abbreviation works nicely with |
|
| 28774 | 822 |
calculational reasoning (\secref{sec:calculation}).
|
823 |
||
| 62106 | 824 |
\<^descr> \<^verbatim>\<open>CONST\<close> ensures that the given identifier is treated as constant term, |
825 |
and passed through the parse tree in fully internalized form. This is |
|
826 |
particularly relevant for translation rules (\secref{sec:syn-trans}),
|
|
827 |
notably on the RHS. |
|
| 46287 | 828 |
|
| 62106 | 829 |
\<^descr> \<^verbatim>\<open>XCONST\<close> is similar to \<^verbatim>\<open>CONST\<close>, but retains the constant name as given. |
830 |
This is only relevant to translation rules (\secref{sec:syn-trans}), notably
|
|
831 |
on the LHS. |
|
| 58618 | 832 |
\<close> |
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
833 |
|
| 28777 | 834 |
|
| 58618 | 835 |
subsection \<open>Inspecting the syntax\<close> |
| 28777 | 836 |
|
| 58618 | 837 |
text \<open> |
| 46282 | 838 |
\begin{matharray}{rcl}
|
| 61493 | 839 |
@{command_def "print_syntax"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
|
| 46282 | 840 |
\end{matharray}
|
| 28777 | 841 |
|
| 62106 | 842 |
\<^descr> @{command "print_syntax"} prints the inner syntax of the current context.
|
843 |
The output can be quite large; the most important sections are explained |
|
844 |
below. |
|
| 28777 | 845 |
|
| 62106 | 846 |
\<^descr> \<open>lexicon\<close> lists the delimiters of the inner token language; see |
847 |
\secref{sec:inner-lex}.
|
|
| 28777 | 848 |
|
| 62106 | 849 |
\<^descr> \<open>prods\<close> lists the productions of the underlying priority grammar; see |
850 |
\secref{sec:priority-grammar}.
|
|
| 28777 | 851 |
|
| 62106 | 852 |
The nonterminal \<open>A\<^sup>(\<^sup>p\<^sup>)\<close> is rendered in plain text as \<open>A[p]\<close>; delimiters |
853 |
are quoted. Many productions have an extra \<open>\<dots> => name\<close>. These names later |
|
854 |
become the heads of parse trees; they also guide the pretty printer. |
|
855 |
||
856 |
Productions without such parse tree names are called \<^emph>\<open>copy productions\<close>. |
|
857 |
Their right-hand side must have exactly one nonterminal symbol (or named |
|
858 |
token). The parser does not create a new parse tree node for copy |
|
859 |
productions, but simply returns the parse tree of the right-hand symbol. |
|
| 46282 | 860 |
|
| 61458 | 861 |
If the right-hand side of a copy production consists of a single |
| 61477 | 862 |
nonterminal without any delimiters, then it is called a \<^emph>\<open>chain |
| 62106 | 863 |
production\<close>. Chain productions act as abbreviations: conceptually, they |
864 |
are removed from the grammar by adding new productions. Priority |
|
865 |
information attached to chain productions is ignored; only the dummy value |
|
866 |
\<open>-1\<close> is displayed. |
|
| 46282 | 867 |
|
| 62106 | 868 |
\<^descr> \<open>print modes\<close> lists the alternative print modes provided by this |
869 |
grammar; see \secref{sec:print-modes}.
|
|
| 28777 | 870 |
|
| 62106 | 871 |
\<^descr> \<open>parse_rules\<close> and \<open>print_rules\<close> relate to syntax translations (macros); |
872 |
see \secref{sec:syn-trans}.
|
|
| 46282 | 873 |
|
| 62106 | 874 |
\<^descr> \<open>parse_ast_translation\<close> and \<open>print_ast_translation\<close> list sets of |
875 |
constants that invoke translation functions for abstract syntax trees, |
|
876 |
which are only required in very special situations; see |
|
877 |
\secref{sec:tr-funs}.
|
|
| 28777 | 878 |
|
| 62106 | 879 |
\<^descr> \<open>parse_translation\<close> and \<open>print_translation\<close> list the sets of constants |
880 |
that invoke regular translation functions; see \secref{sec:tr-funs}.
|
|
| 58618 | 881 |
\<close> |
| 28774 | 882 |
|
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
883 |
|
| 58618 | 884 |
subsection \<open>Ambiguity of parsed expressions\<close> |
| 46291 | 885 |
|
| 58618 | 886 |
text \<open> |
| 46291 | 887 |
\begin{tabular}{rcll}
|
| 61493 | 888 |
@{attribute_def syntax_ambiguity_warning} & : & \<open>attribute\<close> & default \<open>true\<close> \\
|
889 |
@{attribute_def syntax_ambiguity_limit} & : & \<open>attribute\<close> & default \<open>10\<close> \\
|
|
| 46291 | 890 |
\end{tabular}
|
891 |
||
| 62106 | 892 |
Depending on the grammar and the given input, parsing may be ambiguous. |
893 |
Isabelle lets the Earley parser enumerate all possible parse trees, and then |
|
894 |
tries to make the best out of the situation. Terms that cannot be |
|
895 |
type-checked are filtered out, which often leads to a unique result in the |
|
896 |
end. Unlike regular type reconstruction, which is applied to the whole |
|
897 |
collection of input terms simultaneously, the filtering stage only treats |
|
898 |
each given term in isolation. Filtering is also not attempted for individual |
|
| 46291 | 899 |
types or raw ASTs (as required for @{command translations}).
|
900 |
||
| 62106 | 901 |
Certain warning or error messages are printed, depending on the situation |
902 |
and the given configuration options. Parsing ultimately fails, if multiple |
|
903 |
results remain after the filtering phase. |
|
| 46291 | 904 |
|
| 62106 | 905 |
\<^descr> @{attribute syntax_ambiguity_warning} controls output of explicit warning
|
906 |
messages about syntax ambiguity. |
|
| 46291 | 907 |
|
| 62106 | 908 |
\<^descr> @{attribute syntax_ambiguity_limit} determines the number of resulting
|
909 |
parse trees that are shown as part of the printed message in case of an |
|
910 |
ambiguity. |
|
| 58618 | 911 |
\<close> |
| 46291 | 912 |
|
913 |
||
| 58618 | 914 |
section \<open>Syntax transformations \label{sec:syntax-transformations}\<close>
|
| 48113 | 915 |
|
| 62106 | 916 |
text \<open> |
917 |
The inner syntax engine of Isabelle provides separate mechanisms to |
|
918 |
transform parse trees either via rewrite systems on first-order ASTs |
|
919 |
(\secref{sec:syn-trans}), or ML functions on ASTs or syntactic \<open>\<lambda>\<close>-terms
|
|
920 |
(\secref{sec:tr-funs}). This works both for parsing and printing, as
|
|
921 |
outlined in \figref{fig:parse-print}.
|
|
| 48113 | 922 |
|
923 |
\begin{figure}[htbp]
|
|
924 |
\begin{center}
|
|
925 |
\begin{tabular}{cl}
|
|
926 |
string & \\ |
|
| 61493 | 927 |
\<open>\<down>\<close> & lexer + parser \\ |
| 48113 | 928 |
parse tree & \\ |
| 61493 | 929 |
\<open>\<down>\<close> & parse AST translation \\ |
| 48113 | 930 |
AST & \\ |
| 61493 | 931 |
\<open>\<down>\<close> & AST rewriting (macros) \\ |
| 48113 | 932 |
AST & \\ |
| 61493 | 933 |
\<open>\<down>\<close> & parse translation \\ |
| 48113 | 934 |
--- pre-term --- & \\ |
| 61493 | 935 |
\<open>\<down>\<close> & print translation \\ |
| 48113 | 936 |
AST & \\ |
| 61493 | 937 |
\<open>\<down>\<close> & AST rewriting (macros) \\ |
| 48113 | 938 |
AST & \\ |
| 61493 | 939 |
\<open>\<down>\<close> & print AST translation \\ |
| 48113 | 940 |
string & |
941 |
\end{tabular}
|
|
942 |
\end{center}
|
|
943 |
\caption{Parsing and printing with translations}\label{fig:parse-print}
|
|
944 |
\end{figure}
|
|
945 |
||
| 62106 | 946 |
These intermediate syntax tree formats eventually lead to a pre-term with |
947 |
all names and binding scopes resolved, but most type information still |
|
948 |
missing. Explicit type constraints might be given by the user, or implicit |
|
949 |
position information by the system --- both need to be passed-through |
|
950 |
carefully by syntax transformations. |
|
| 48113 | 951 |
|
| 62106 | 952 |
Pre-terms are further processed by the so-called \<^emph>\<open>check\<close> and \<^emph>\<open>uncheck\<close> |
953 |
phases that are intertwined with type-inference (see also @{cite
|
|
954 |
"isabelle-implementation"}). The latter allows to operate on higher-order |
|
955 |
abstract syntax with proper binding and type information already available. |
|
| 48113 | 956 |
|
| 62106 | 957 |
As a rule of thumb, anything that manipulates bindings of variables or |
958 |
constants needs to be implemented as syntax transformation (see below). |
|
959 |
Anything else is better done via check/uncheck: a prominent example |
|
960 |
application is the @{command abbreviation} concept of Isabelle/Pure.
|
|
961 |
\<close> |
|
| 48113 | 962 |
|
963 |
||
| 58618 | 964 |
subsection \<open>Abstract syntax trees \label{sec:ast}\<close>
|
| 48113 | 965 |
|
| 62106 | 966 |
text \<open> |
967 |
The ML datatype @{ML_type Ast.ast} explicitly represents the intermediate
|
|
968 |
AST format that is used for syntax rewriting (\secref{sec:syn-trans}). It is
|
|
969 |
defined in ML as follows: |
|
|
61408
9020a3ba6c9a
@{verbatim [display]} supersedes old alltt/ttbox;
wenzelm
parents:
61143
diff
changeset
|
970 |
@{verbatim [display]
|
|
9020a3ba6c9a
@{verbatim [display]} supersedes old alltt/ttbox;
wenzelm
parents:
61143
diff
changeset
|
971 |
\<open>datatype ast = |
|
9020a3ba6c9a
@{verbatim [display]} supersedes old alltt/ttbox;
wenzelm
parents:
61143
diff
changeset
|
972 |
Constant of string | |
|
9020a3ba6c9a
@{verbatim [display]} supersedes old alltt/ttbox;
wenzelm
parents:
61143
diff
changeset
|
973 |
Variable of string | |
|
9020a3ba6c9a
@{verbatim [display]} supersedes old alltt/ttbox;
wenzelm
parents:
61143
diff
changeset
|
974 |
Appl of ast list\<close>} |
| 48114 | 975 |
|
| 62106 | 976 |
An AST is either an atom (constant or variable) or a list of (at least two) |
977 |
subtrees. Occasional diagnostic output of ASTs uses notation that resembles |
|
978 |
S-expression of LISP. Constant atoms are shown as quoted strings, variable |
|
979 |
atoms as non-quoted strings and applications as a parenthesized list of |
|
980 |
subtrees. For example, the AST |
|
| 58724 | 981 |
@{ML [display] \<open>Ast.Appl [Ast.Constant "_abs", Ast.Variable "x", Ast.Variable "t"]\<close>}
|
| 62106 | 982 |
is pretty-printed as \<^verbatim>\<open>("_abs" x t)\<close>. Note that \<^verbatim>\<open>()\<close> and \<^verbatim>\<open>(x)\<close> are
|
983 |
excluded as ASTs, because they have too few subtrees. |
|
| 48114 | 984 |
|
| 61421 | 985 |
\<^medskip> |
| 62106 | 986 |
AST application is merely a pro-forma mechanism to indicate certain |
987 |
syntactic structures. Thus \<^verbatim>\<open>(c a b)\<close> could mean either term application or |
|
988 |
type application, depending on the syntactic context. |
|
| 48114 | 989 |
|
| 62106 | 990 |
Nested application like \<^verbatim>\<open>(("_abs" x t) u)\<close> is also possible, but ASTs are
|
991 |
definitely first-order: the syntax constant \<^verbatim>\<open>"_abs"\<close> does not bind the \<^verbatim>\<open>x\<close> |
|
992 |
in any way. Proper bindings are introduced in later stages of the term |
|
993 |
syntax, where \<^verbatim>\<open>("_abs" x t)\<close> becomes an @{ML Abs} node and occurrences of
|
|
994 |
\<^verbatim>\<open>x\<close> in \<^verbatim>\<open>t\<close> are replaced by bound variables (represented as de-Bruijn |
|
995 |
indices). |
|
| 58618 | 996 |
\<close> |
| 48113 | 997 |
|
998 |
||
| 58618 | 999 |
subsubsection \<open>AST constants versus variables\<close> |
| 48114 | 1000 |
|
| 62106 | 1001 |
text \<open> |
1002 |
Depending on the situation --- input syntax, output syntax, translation |
|
1003 |
patterns --- the distinction of atomic ASTs as @{ML Ast.Constant} versus
|
|
1004 |
@{ML Ast.Variable} serves slightly different purposes.
|
|
| 48114 | 1005 |
|
| 62106 | 1006 |
Input syntax of a term such as \<open>f a b = c\<close> does not yet indicate the scopes |
1007 |
of atomic entities \<open>f, a, b, c\<close>: they could be global constants or local |
|
1008 |
variables, even bound ones depending on the context of the term. @{ML
|
|
1009 |
Ast.Variable} leaves this choice still open: later syntax layers (or |
|
1010 |
translation functions) may capture such a variable to determine its role |
|
1011 |
specifically, to make it a constant, bound variable, free variable etc. In |
|
1012 |
contrast, syntax translations that introduce already known constants would |
|
1013 |
rather do it via @{ML Ast.Constant} to prevent accidental re-interpretation
|
|
1014 |
later on. |
|
| 48114 | 1015 |
|
| 62106 | 1016 |
Output syntax turns term constants into @{ML Ast.Constant} and variables
|
1017 |
(free or schematic) into @{ML Ast.Variable}. This information is precise
|
|
1018 |
when printing fully formal \<open>\<lambda>\<close>-terms. |
|
| 48114 | 1019 |
|
| 61421 | 1020 |
\<^medskip> |
| 62106 | 1021 |
AST translation patterns (\secref{sec:syn-trans}) that represent terms
|
1022 |
cannot distinguish constants and variables syntactically. Explicit |
|
1023 |
indication of \<open>CONST c\<close> inside the term language is required, unless \<open>c\<close> is |
|
1024 |
known as special \<^emph>\<open>syntax constant\<close> (see also @{command syntax}). It is also
|
|
1025 |
possible to use @{command syntax} declarations (without mixfix annotation)
|
|
1026 |
to enforce that certain unqualified names are always treated as constant |
|
1027 |
within the syntax machinery. |
|
| 48114 | 1028 |
|
| 62106 | 1029 |
The situation is simpler for ASTs that represent types or sorts, since the |
1030 |
concrete syntax already distinguishes type variables from type constants |
|
1031 |
(constructors). So \<open>('a, 'b) foo\<close> corresponds to an AST application of some
|
|
1032 |
constant for \<open>foo\<close> and variable arguments for \<open>'a\<close> and \<open>'b\<close>. Note that the |
|
1033 |
postfix application is merely a feature of the concrete syntax, while in the |
|
1034 |
AST the constructor occurs in head position. |
|
1035 |
\<close> |
|
| 48114 | 1036 |
|
1037 |
||
| 58618 | 1038 |
subsubsection \<open>Authentic syntax names\<close> |
| 48114 | 1039 |
|
| 62106 | 1040 |
text \<open> |
1041 |
Naming constant entities within ASTs is another delicate issue. Unqualified |
|
1042 |
names are resolved in the name space tables in the last stage of parsing, |
|
1043 |
after all translations have been applied. Since syntax transformations do |
|
1044 |
not know about this later name resolution, there can be surprises in |
|
1045 |
boundary cases. |
|
| 48114 | 1046 |
|
| 62106 | 1047 |
\<^emph>\<open>Authentic syntax names\<close> for @{ML Ast.Constant} avoid this problem: the
|
1048 |
fully-qualified constant name with a special prefix for its formal category |
|
1049 |
(\<open>class\<close>, \<open>type\<close>, \<open>const\<close>, \<open>fixed\<close>) represents the information faithfully |
|
1050 |
within the untyped AST format. Accidental overlap with free or bound |
|
1051 |
variables is excluded as well. Authentic syntax names work implicitly in the |
|
1052 |
following situations: |
|
| 48114 | 1053 |
|
| 62106 | 1054 |
\<^item> Input of term constants (or fixed variables) that are introduced by |
1055 |
concrete syntax via @{command notation}: the correspondence of a
|
|
1056 |
particular grammar production to some known term entity is preserved. |
|
| 48114 | 1057 |
|
| 62106 | 1058 |
\<^item> Input of type constants (constructors) and type classes --- thanks to |
1059 |
explicit syntactic distinction independently on the context. |
|
| 48114 | 1060 |
|
| 62106 | 1061 |
\<^item> Output of term constants, type constants, type classes --- this |
1062 |
information is already available from the internal term to be printed. |
|
| 48114 | 1063 |
|
| 62106 | 1064 |
In other words, syntax transformations that operate on input terms written |
1065 |
as prefix applications are difficult to make robust. Luckily, this case |
|
1066 |
rarely occurs in practice, because syntax forms to be translated usually |
|
1067 |
correspond to some concrete notation. |
|
1068 |
\<close> |
|
| 48114 | 1069 |
|
1070 |
||
| 58618 | 1071 |
subsection \<open>Raw syntax and translations \label{sec:syn-trans}\<close>
|
| 28762 | 1072 |
|
| 58618 | 1073 |
text \<open> |
| 48117 | 1074 |
\begin{tabular}{rcll}
|
| 61493 | 1075 |
@{command_def "nonterminal"} & : & \<open>theory \<rightarrow> theory\<close> \\
|
1076 |
@{command_def "syntax"} & : & \<open>theory \<rightarrow> theory\<close> \\
|
|
1077 |
@{command_def "no_syntax"} & : & \<open>theory \<rightarrow> theory\<close> \\
|
|
1078 |
@{command_def "translations"} & : & \<open>theory \<rightarrow> theory\<close> \\
|
|
1079 |
@{command_def "no_translations"} & : & \<open>theory \<rightarrow> theory\<close> \\
|
|
1080 |
@{attribute_def syntax_ast_trace} & : & \<open>attribute\<close> & default \<open>false\<close> \\
|
|
1081 |
@{attribute_def syntax_ast_stats} & : & \<open>attribute\<close> & default \<open>false\<close> \\
|
|
| 48117 | 1082 |
\end{tabular}
|
| 61421 | 1083 |
\<^medskip> |
|
59783
00b62aa9f430
tuned syntax diagrams -- no duplication of "target";
wenzelm
parents:
58842
diff
changeset
|
1084 |
|
| 62106 | 1085 |
Unlike mixfix notation for existing formal entities (\secref{sec:notation}),
|
1086 |
raw syntax declarations provide full access to the priority grammar of the |
|
1087 |
inner syntax, without any sanity checks. This includes additional syntactic |
|
1088 |
categories (via @{command nonterminal}) and free-form grammar productions
|
|
1089 |
(via @{command syntax}). Additional syntax translations (or macros, via
|
|
1090 |
@{command translations}) are required to turn resulting parse trees into
|
|
1091 |
proper representations of formal entities again. |
|
| 46292 | 1092 |
|
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
55108
diff
changeset
|
1093 |
@{rail \<open>
|
|
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
1094 |
@@{command nonterminal} (@{syntax name} + @'and')
|
| 28762 | 1095 |
; |
|
46494
ea2ae63336f3
clarified outer syntax "constdecl", which is only local to some rail diagrams;
wenzelm
parents:
46483
diff
changeset
|
1096 |
(@@{command syntax} | @@{command no_syntax}) @{syntax mode}? (constdecl +)
|
| 28762 | 1097 |
; |
|
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
1098 |
(@@{command translations} | @@{command no_translations})
|
|
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
1099 |
(transpat ('==' | '=>' | '<=' | '\<rightleftharpoons>' | '\<rightharpoonup>' | '\<leftharpoondown>') transpat +)
|
| 28762 | 1100 |
; |
1101 |
||
|
46494
ea2ae63336f3
clarified outer syntax "constdecl", which is only local to some rail diagrams;
wenzelm
parents:
46483
diff
changeset
|
1102 |
constdecl: @{syntax name} '::' @{syntax type} @{syntax mixfix}?
|
|
ea2ae63336f3
clarified outer syntax "constdecl", which is only local to some rail diagrams;
wenzelm
parents:
46483
diff
changeset
|
1103 |
; |
|
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
1104 |
mode: ('(' ( @{syntax name} | @'output' | @{syntax name} @'output' ) ')')
|
| 28762 | 1105 |
; |
| 62969 | 1106 |
transpat: ('(' @{syntax name} ')')? @{syntax string}
|
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
55108
diff
changeset
|
1107 |
\<close>} |
| 28762 | 1108 |
|
| 62106 | 1109 |
\<^descr> @{command "nonterminal"}~\<open>c\<close> declares a type constructor \<open>c\<close> (without
|
1110 |
arguments) to act as purely syntactic type: a nonterminal symbol of the |
|
1111 |
inner syntax. |
|
| 28762 | 1112 |
|
| 62106 | 1113 |
\<^descr> @{command "syntax"}~\<open>(mode) c :: \<sigma> (mx)\<close> augments the priority grammar and
|
1114 |
the pretty printer table for the given print mode (default \<^verbatim>\<open>""\<close>). An |
|
1115 |
optional keyword @{keyword_ref "output"} means that only the pretty printer
|
|
1116 |
table is affected. |
|
| 46292 | 1117 |
|
| 62106 | 1118 |
Following \secref{sec:mixfix}, the mixfix annotation \<open>mx = template ps q\<close>
|
1119 |
together with type \<open>\<sigma> = \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>\<close> and specify a grammar production. |
|
1120 |
The \<open>template\<close> contains delimiter tokens that surround \<open>n\<close> argument |
|
1121 |
positions (\<^verbatim>\<open>_\<close>). The latter correspond to nonterminal symbols \<open>A\<^sub>i\<close> derived |
|
1122 |
from the argument types \<open>\<tau>\<^sub>i\<close> as follows: |
|
| 46292 | 1123 |
|
| 61493 | 1124 |
\<^item> \<open>prop\<close> if \<open>\<tau>\<^sub>i = prop\<close> |
| 46292 | 1125 |
|
| 62106 | 1126 |
\<^item> \<open>logic\<close> if \<open>\<tau>\<^sub>i = (\<dots>)\<kappa>\<close> for logical type constructor \<open>\<kappa> \<noteq> prop\<close> |
| 46292 | 1127 |
|
| 61493 | 1128 |
\<^item> \<open>any\<close> if \<open>\<tau>\<^sub>i = \<alpha>\<close> for type variables |
| 46292 | 1129 |
|
| 62106 | 1130 |
\<^item> \<open>\<kappa>\<close> if \<open>\<tau>\<^sub>i = \<kappa>\<close> for nonterminal \<open>\<kappa>\<close> (syntactic type constructor) |
| 46292 | 1131 |
|
| 62106 | 1132 |
Each \<open>A\<^sub>i\<close> is decorated by priority \<open>p\<^sub>i\<close> from the given list \<open>ps\<close>; missing |
1133 |
priorities default to 0. |
|
| 46292 | 1134 |
|
| 62106 | 1135 |
The resulting nonterminal of the production is determined similarly from |
1136 |
type \<open>\<tau>\<close>, with priority \<open>q\<close> and default 1000. |
|
| 46292 | 1137 |
|
| 61421 | 1138 |
\<^medskip> |
| 62106 | 1139 |
Parsing via this production produces parse trees \<open>t\<^sub>1, \<dots>, t\<^sub>n\<close> for the |
1140 |
argument slots. The resulting parse tree is composed as \<open>c t\<^sub>1 \<dots> t\<^sub>n\<close>, by |
|
1141 |
using the syntax constant \<open>c\<close> of the syntax declaration. |
|
| 46292 | 1142 |
|
| 62106 | 1143 |
Such syntactic constants are invented on the spot, without formal check |
1144 |
wrt.\ existing declarations. It is conventional to use plain identifiers |
|
1145 |
prefixed by a single underscore (e.g.\ \<open>_foobar\<close>). Names should be chosen |
|
1146 |
with care, to avoid clashes with other syntax declarations. |
|
| 46292 | 1147 |
|
| 61421 | 1148 |
\<^medskip> |
| 62106 | 1149 |
The special case of copy production is specified by \<open>c =\<close>~\<^verbatim>\<open>""\<close> (empty |
1150 |
string). It means that the resulting parse tree \<open>t\<close> is copied directly, |
|
1151 |
without any further decoration. |
|
| 46282 | 1152 |
|
| 62106 | 1153 |
\<^descr> @{command "no_syntax"}~\<open>(mode) decls\<close> removes grammar declarations (and
|
1154 |
translations) resulting from \<open>decls\<close>, which are interpreted in the same |
|
1155 |
manner as for @{command "syntax"} above.
|
|
1156 |
||
1157 |
\<^descr> @{command "translations"}~\<open>rules\<close> specifies syntactic translation rules
|
|
1158 |
(i.e.\ macros) as first-order rewrite rules on ASTs (\secref{sec:ast}). The
|
|
1159 |
theory context maintains two independent lists translation rules: parse |
|
1160 |
rules (\<^verbatim>\<open>=>\<close> or \<open>\<rightharpoonup>\<close>) and print rules (\<^verbatim>\<open><=\<close> or \<open>\<leftharpoondown>\<close>). For convenience, both |
|
1161 |
can be specified simultaneously as parse~/ print rules (\<^verbatim>\<open>==\<close> or \<open>\<rightleftharpoons>\<close>). |
|
| 46282 | 1162 |
|
| 62106 | 1163 |
Translation patterns may be prefixed by the syntactic category to be used |
1164 |
for parsing; the default is \<open>logic\<close> which means that regular term syntax is |
|
1165 |
used. Both sides of the syntax translation rule undergo parsing and parse |
|
1166 |
AST translations \secref{sec:tr-funs}, in order to perform some fundamental
|
|
1167 |
normalization like \<open>\<lambda>x y. b \<leadsto> \<lambda>x. \<lambda>y. b\<close>, but other AST translation rules |
|
1168 |
are \<^emph>\<open>not\<close> applied recursively here. |
|
| 48115 | 1169 |
|
| 62106 | 1170 |
When processing AST patterns, the inner syntax lexer runs in a different |
1171 |
mode that allows identifiers to start with underscore. This accommodates the |
|
1172 |
usual naming convention for auxiliary syntax constants --- those that do not |
|
1173 |
have a logical counter part --- by allowing to specify arbitrary AST |
|
1174 |
applications within the term syntax, independently of the corresponding |
|
1175 |
concrete syntax. |
|
| 48115 | 1176 |
|
1177 |
Atomic ASTs are distinguished as @{ML Ast.Constant} versus @{ML
|
|
| 62106 | 1178 |
Ast.Variable} as follows: a qualified name or syntax constant declared via |
1179 |
@{command syntax}, or parse tree head of concrete notation becomes @{ML
|
|
1180 |
Ast.Constant}, anything else @{ML Ast.Variable}. Note that \<open>CONST\<close> and
|
|
1181 |
\<open>XCONST\<close> within the term language (\secref{sec:pure-grammar}) allow to
|
|
1182 |
enforce treatment as constants. |
|
| 48115 | 1183 |
|
| 62106 | 1184 |
AST rewrite rules \<open>(lhs, rhs)\<close> need to obey the following side-conditions: |
| 48115 | 1185 |
|
| 62106 | 1186 |
\<^item> Rules must be left linear: \<open>lhs\<close> must not contain repeated |
1187 |
variables.\<^footnote>\<open>The deeper reason for this is that AST equality is not |
|
1188 |
well-defined: different occurrences of the ``same'' AST could be decorated |
|
1189 |
differently by accidental type-constraints or source position information, |
|
1190 |
for example.\<close> |
|
| 48115 | 1191 |
|
| 61493 | 1192 |
\<^item> Every variable in \<open>rhs\<close> must also occur in \<open>lhs\<close>. |
| 48115 | 1193 |
|
| 62106 | 1194 |
\<^descr> @{command "no_translations"}~\<open>rules\<close> removes syntactic translation rules,
|
1195 |
which are interpreted in the same manner as for @{command "translations"}
|
|
1196 |
above. |
|
| 28762 | 1197 |
|
| 62106 | 1198 |
\<^descr> @{attribute syntax_ast_trace} and @{attribute syntax_ast_stats} control
|
1199 |
diagnostic output in the AST normalization process, when translation rules |
|
1200 |
are applied to concrete input or output. |
|
| 48117 | 1201 |
|
| 46293 | 1202 |
|
| 62106 | 1203 |
Raw syntax and translations provides a slightly more low-level access to the |
1204 |
grammar and the form of resulting parse trees. It is often possible to avoid |
|
1205 |
this untyped macro mechanism, and use type-safe @{command abbreviation} or
|
|
1206 |
@{command notation} instead. Some important situations where @{command
|
|
1207 |
syntax} and @{command translations} are really need are as follows:
|
|
| 46293 | 1208 |
|
| 62106 | 1209 |
\<^item> Iterated replacement via recursive @{command translations}. For example,
|
1210 |
consider list enumeration @{term "[a, b, c, d]"} as defined in theory
|
|
1211 |
@{theory List} in Isabelle/HOL.
|
|
| 46293 | 1212 |
|
| 62106 | 1213 |
\<^item> Change of binding status of variables: anything beyond the built-in |
1214 |
@{keyword "binder"} mixfix annotation requires explicit syntax translations.
|
|
1215 |
For example, consider list filter comprehension @{term "[x \<leftarrow> xs . P]"} as
|
|
1216 |
defined in theory @{theory List} in Isabelle/HOL.
|
|
| 61458 | 1217 |
\<close> |
| 46293 | 1218 |
|
| 28762 | 1219 |
|
| 58618 | 1220 |
subsubsection \<open>Applying translation rules\<close> |
| 48117 | 1221 |
|
| 62106 | 1222 |
text \<open> |
1223 |
As a term is being parsed or printed, an AST is generated as an intermediate |
|
1224 |
form according to \figref{fig:parse-print}. The AST is normalized by
|
|
1225 |
applying translation rules in the manner of a first-order term rewriting |
|
1226 |
system. We first examine how a single rule is applied. |
|
| 48117 | 1227 |
|
| 62106 | 1228 |
Let \<open>t\<close> be the abstract syntax tree to be normalized and \<open>(lhs, rhs)\<close> some |
1229 |
translation rule. A subtree \<open>u\<close> of \<open>t\<close> is called \<^emph>\<open>redex\<close> if it is an |
|
1230 |
instance of \<open>lhs\<close>; in this case the pattern \<open>lhs\<close> is said to match the |
|
1231 |
object \<open>u\<close>. A redex matched by \<open>lhs\<close> may be replaced by the corresponding |
|
1232 |
instance of \<open>rhs\<close>, thus \<^emph>\<open>rewriting\<close> the AST \<open>t\<close>. Matching requires some |
|
1233 |
notion of \<^emph>\<open>place-holders\<close> in rule patterns: @{ML Ast.Variable} serves this
|
|
1234 |
purpose. |
|
| 48117 | 1235 |
|
| 62106 | 1236 |
More precisely, the matching of the object \<open>u\<close> against the pattern \<open>lhs\<close> is |
1237 |
performed as follows: |
|
| 48117 | 1238 |
|
| 62106 | 1239 |
\<^item> Objects of the form @{ML Ast.Variable}~\<open>x\<close> or @{ML Ast.Constant}~\<open>x\<close> are
|
1240 |
matched by pattern @{ML Ast.Constant}~\<open>x\<close>. Thus all atomic ASTs in the
|
|
1241 |
object are treated as (potential) constants, and a successful match makes |
|
1242 |
them actual constants even before name space resolution (see also |
|
1243 |
\secref{sec:ast}).
|
|
| 48117 | 1244 |
|
| 62106 | 1245 |
\<^item> Object \<open>u\<close> is matched by pattern @{ML Ast.Variable}~\<open>x\<close>, binding \<open>x\<close> to
|
1246 |
\<open>u\<close>. |
|
| 48117 | 1247 |
|
| 62106 | 1248 |
\<^item> Object @{ML Ast.Appl}~\<open>us\<close> is matched by @{ML Ast.Appl}~\<open>ts\<close> if \<open>us\<close> and
|
1249 |
\<open>ts\<close> have the same length and each corresponding subtree matches. |
|
| 48117 | 1250 |
|
| 62106 | 1251 |
\<^item> In every other case, matching fails. |
| 48117 | 1252 |
|
| 62106 | 1253 |
A successful match yields a substitution that is applied to \<open>rhs\<close>, |
1254 |
generating the instance that replaces \<open>u\<close>. |
|
| 48117 | 1255 |
|
| 62106 | 1256 |
Normalizing an AST involves repeatedly applying translation rules until none |
1257 |
are applicable. This works yoyo-like: top-down, bottom-up, top-down, etc. At |
|
1258 |
each subtree position, rules are chosen in order of appearance in the theory |
|
1259 |
definitions. |
|
| 48117 | 1260 |
|
| 62106 | 1261 |
The configuration options @{attribute syntax_ast_trace} and @{attribute
|
1262 |
syntax_ast_stats} might help to understand this process and diagnose |
|
1263 |
problems. |
|
| 48117 | 1264 |
|
1265 |
\begin{warn}
|
|
| 62106 | 1266 |
If syntax translation rules work incorrectly, the output of @{command_ref
|
1267 |
print_syntax} with its \<^emph>\<open>rules\<close> sections reveals the actual internal forms |
|
1268 |
of AST pattern, without potentially confusing concrete syntax. Recall that |
|
1269 |
AST constants appear as quoted strings and variables without quotes. |
|
| 48117 | 1270 |
\end{warn}
|
1271 |
||
1272 |
\begin{warn}
|
|
| 62106 | 1273 |
If @{attribute_ref eta_contract} is set to \<open>true\<close>, terms will be
|
1274 |
\<open>\<eta>\<close>-contracted \<^emph>\<open>before\<close> the AST rewriter sees them. Thus some abstraction |
|
1275 |
nodes needed for print rules to match may vanish. For example, \<open>Ball A (\<lambda>x. |
|
1276 |
P x)\<close> would contract to \<open>Ball A P\<close> and the standard print rule would fail to |
|
1277 |
apply. This problem can be avoided by hand-written ML translation functions |
|
1278 |
(see also \secref{sec:tr-funs}), which is in fact the same mechanism used in
|
|
1279 |
built-in @{keyword "binder"} declarations.
|
|
| 48117 | 1280 |
\end{warn}
|
| 58618 | 1281 |
\<close> |
| 48117 | 1282 |
|
| 28762 | 1283 |
|
| 58618 | 1284 |
subsection \<open>Syntax translation functions \label{sec:tr-funs}\<close>
|
| 28762 | 1285 |
|
| 58618 | 1286 |
text \<open> |
| 28762 | 1287 |
\begin{matharray}{rcl}
|
| 61493 | 1288 |
@{command_def "parse_ast_translation"} & : & \<open>theory \<rightarrow> theory\<close> \\
|
1289 |
@{command_def "parse_translation"} & : & \<open>theory \<rightarrow> theory\<close> \\
|
|
1290 |
@{command_def "print_translation"} & : & \<open>theory \<rightarrow> theory\<close> \\
|
|
1291 |
@{command_def "typed_print_translation"} & : & \<open>theory \<rightarrow> theory\<close> \\
|
|
1292 |
@{command_def "print_ast_translation"} & : & \<open>theory \<rightarrow> theory\<close> \\
|
|
1293 |
@{ML_antiquotation_def "class_syntax"} & : & \<open>ML antiquotation\<close> \\
|
|
1294 |
@{ML_antiquotation_def "type_syntax"} & : & \<open>ML antiquotation\<close> \\
|
|
1295 |
@{ML_antiquotation_def "const_syntax"} & : & \<open>ML antiquotation\<close> \\
|
|
1296 |
@{ML_antiquotation_def "syntax_const"} & : & \<open>ML antiquotation\<close> \\
|
|
| 28762 | 1297 |
\end{matharray}
|
1298 |
||
| 48118 | 1299 |
Syntax translation functions written in ML admit almost arbitrary |
1300 |
manipulations of inner syntax, at the expense of some complexity and |
|
1301 |
obscurity in the implementation. |
|
1302 |
||
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
55108
diff
changeset
|
1303 |
@{rail \<open>
|
|
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
1304 |
( @@{command parse_ast_translation} | @@{command parse_translation} |
|
|
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
1305 |
@@{command print_translation} | @@{command typed_print_translation} |
|
| 52143 | 1306 |
@@{command print_ast_translation}) @{syntax text}
|
|
48119
55c305e29f4b
cover @{class_syntax}, @{type_syntax}, @{const_syntax}, @{syntax_const} in isar-ref, in contrast to other ML antiquotations in implementation manual;
wenzelm
parents:
48118
diff
changeset
|
1307 |
; |
|
55c305e29f4b
cover @{class_syntax}, @{type_syntax}, @{const_syntax}, @{syntax_const} in isar-ref, in contrast to other ML antiquotations in implementation manual;
wenzelm
parents:
48118
diff
changeset
|
1308 |
(@@{ML_antiquotation class_syntax} |
|
|
55c305e29f4b
cover @{class_syntax}, @{type_syntax}, @{const_syntax}, @{syntax_const} in isar-ref, in contrast to other ML antiquotations in implementation manual;
wenzelm
parents:
48118
diff
changeset
|
1309 |
@@{ML_antiquotation type_syntax} |
|
|
55c305e29f4b
cover @{class_syntax}, @{type_syntax}, @{const_syntax}, @{syntax_const} in isar-ref, in contrast to other ML antiquotations in implementation manual;
wenzelm
parents:
48118
diff
changeset
|
1310 |
@@{ML_antiquotation const_syntax} |
|
|
55c305e29f4b
cover @{class_syntax}, @{type_syntax}, @{const_syntax}, @{syntax_const} in isar-ref, in contrast to other ML antiquotations in implementation manual;
wenzelm
parents:
48118
diff
changeset
|
1311 |
@@{ML_antiquotation syntax_const}) name
|
|
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
55108
diff
changeset
|
1312 |
\<close>} |
| 28762 | 1313 |
|
| 62106 | 1314 |
\<^descr> @{command parse_translation} etc. declare syntax translation functions to
|
1315 |
the theory. Any of these commands have a single @{syntax text} argument that
|
|
1316 |
refers to an ML expression of appropriate type as follows: |
|
| 48118 | 1317 |
|
| 61421 | 1318 |
\<^medskip> |
|
48119
55c305e29f4b
cover @{class_syntax}, @{type_syntax}, @{const_syntax}, @{syntax_const} in isar-ref, in contrast to other ML antiquotations in implementation manual;
wenzelm
parents:
48118
diff
changeset
|
1319 |
{\footnotesize
|
| 52143 | 1320 |
\begin{tabular}{l}
|
1321 |
@{command parse_ast_translation} : \\
|
|
1322 |
\quad @{ML_type "(string * (Proof.context -> Ast.ast list -> Ast.ast)) list"} \\
|
|
1323 |
@{command parse_translation} : \\
|
|
1324 |
\quad @{ML_type "(string * (Proof.context -> term list -> term)) list"} \\
|
|
1325 |
@{command print_translation} : \\
|
|
1326 |
\quad @{ML_type "(string * (Proof.context -> term list -> term)) list"} \\
|
|
1327 |
@{command typed_print_translation} : \\
|
|
1328 |
\quad @{ML_type "(string * (Proof.context -> typ -> term list -> term)) list"} \\
|
|
1329 |
@{command print_ast_translation} : \\
|
|
1330 |
\quad @{ML_type "(string * (Proof.context -> Ast.ast list -> Ast.ast)) list"} \\
|
|
| 48118 | 1331 |
\end{tabular}}
|
| 61421 | 1332 |
\<^medskip> |
| 28762 | 1333 |
|
| 62106 | 1334 |
The argument list consists of \<open>(c, tr)\<close> pairs, where \<open>c\<close> is the syntax name |
1335 |
of the formal entity involved, and \<open>tr\<close> a function that translates a syntax |
|
1336 |
form \<open>c args\<close> into \<open>tr ctxt args\<close> (depending on the context). The |
|
1337 |
Isabelle/ML naming convention for parse translations is \<open>c_tr\<close> and for print |
|
1338 |
translations \<open>c_tr'\<close>. |
|
| 48118 | 1339 |
|
1340 |
The @{command_ref print_syntax} command displays the sets of names
|
|
| 62106 | 1341 |
associated with the translation functions of a theory under |
1342 |
\<open>parse_ast_translation\<close> etc. |
|
| 48118 | 1343 |
|
| 62106 | 1344 |
\<^descr> \<open>@{class_syntax c}\<close>, \<open>@{type_syntax c}\<close>, \<open>@{const_syntax c}\<close> inline the
|
1345 |
authentic syntax name of the given formal entities into the ML source. This |
|
1346 |
is the fully-qualified logical name prefixed by a special marker to indicate |
|
1347 |
its kind: thus different logical name spaces are properly distinguished |
|
1348 |
within parse trees. |
|
|
48119
55c305e29f4b
cover @{class_syntax}, @{type_syntax}, @{const_syntax}, @{syntax_const} in isar-ref, in contrast to other ML antiquotations in implementation manual;
wenzelm
parents:
48118
diff
changeset
|
1349 |
|
| 62106 | 1350 |
\<^descr> \<open>@{const_syntax c}\<close> inlines the name \<open>c\<close> of the given syntax constant,
|
1351 |
having checked that it has been declared via some @{command syntax} commands
|
|
1352 |
within the theory context. Note that the usual naming convention makes |
|
1353 |
syntax constants start with underscore, to reduce the chance of accidental |
|
1354 |
clashes with other names occurring in parse trees (unqualified constants |
|
1355 |
etc.). |
|
| 58618 | 1356 |
\<close> |
| 48118 | 1357 |
|
|
48119
55c305e29f4b
cover @{class_syntax}, @{type_syntax}, @{const_syntax}, @{syntax_const} in isar-ref, in contrast to other ML antiquotations in implementation manual;
wenzelm
parents:
48118
diff
changeset
|
1358 |
|
| 58618 | 1359 |
subsubsection \<open>The translation strategy\<close> |
| 28762 | 1360 |
|
| 62106 | 1361 |
text \<open> |
1362 |
The different kinds of translation functions are invoked during the |
|
1363 |
transformations between parse trees, ASTs and syntactic terms (cf.\ |
|
1364 |
\figref{fig:parse-print}). Whenever a combination of the form \<open>c x\<^sub>1 \<dots> x\<^sub>n\<close>
|
|
1365 |
is encountered, and a translation function \<open>f\<close> of appropriate kind is |
|
1366 |
declared for \<open>c\<close>, the result is produced by evaluation of \<open>f [x\<^sub>1, \<dots>, x\<^sub>n]\<close> |
|
1367 |
in ML. |
|
| 48118 | 1368 |
|
| 62106 | 1369 |
For AST translations, the arguments \<open>x\<^sub>1, \<dots>, x\<^sub>n\<close> are ASTs. A combination |
1370 |
has the form @{ML "Ast.Constant"}~\<open>c\<close> or @{ML "Ast.Appl"}~\<open>[\<close>@{ML
|
|
1371 |
Ast.Constant}~\<open>c, x\<^sub>1, \<dots>, x\<^sub>n]\<close>. For term translations, the arguments are |
|
1372 |
terms and a combination has the form @{ML Const}~\<open>(c, \<tau>)\<close> or @{ML
|
|
1373 |
Const}~\<open>(c, \<tau>) $ x\<^sub>1 $ \<dots> $ x\<^sub>n\<close>. Terms allow more sophisticated |
|
1374 |
transformations than ASTs do, typically involving abstractions and bound |
|
1375 |
variables. \<^emph>\<open>Typed\<close> print translations may even peek at the type \<open>\<tau>\<close> of the |
|
1376 |
constant they are invoked on, although some information might have been |
|
1377 |
suppressed for term output already. |
|
| 48118 | 1378 |
|
| 62106 | 1379 |
Regardless of whether they act on ASTs or terms, translation functions |
1380 |
called during the parsing process differ from those for printing in their |
|
1381 |
overall behaviour: |
|
| 48118 | 1382 |
|
| 62106 | 1383 |
\<^descr>[Parse translations] are applied bottom-up. The arguments are already in |
1384 |
translated form. The translations must not fail; exceptions trigger an |
|
1385 |
error message. There may be at most one function associated with any |
|
1386 |
syntactic name. |
|
| 46294 | 1387 |
|
| 62106 | 1388 |
\<^descr>[Print translations] are applied top-down. They are supplied with |
1389 |
arguments that are partly still in internal form. The result again |
|
1390 |
undergoes translation; therefore a print translation should not introduce |
|
1391 |
as head the very constant that invoked it. The function may raise |
|
1392 |
exception @{ML Match} to indicate failure; in this event it has no effect.
|
|
1393 |
Multiple functions associated with some syntactic name are tried in the |
|
1394 |
order of declaration in the theory. |
|
| 48118 | 1395 |
|
| 62106 | 1396 |
Only constant atoms --- constructor @{ML Ast.Constant} for ASTs and @{ML
|
1397 |
Const} for terms --- can invoke translation functions. This means that parse |
|
1398 |
translations can only be associated with parse tree heads of concrete |
|
1399 |
syntax, or syntactic constants introduced via other translations. For plain |
|
1400 |
identifiers within the term language, the status of constant versus variable |
|
1401 |
is not yet know during parsing. This is in contrast to print translations, |
|
1402 |
where constants are explicitly known from the given term in its fully |
|
1403 |
internal form. |
|
| 58618 | 1404 |
\<close> |
| 28762 | 1405 |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1406 |
|
| 58618 | 1407 |
subsection \<open>Built-in syntax transformations\<close> |
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1408 |
|
| 58618 | 1409 |
text \<open> |
| 62106 | 1410 |
Here are some further details of the main syntax transformation phases of |
1411 |
\figref{fig:parse-print}.
|
|
| 58618 | 1412 |
\<close> |
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1413 |
|
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1414 |
|
| 58618 | 1415 |
subsubsection \<open>Transforming parse trees to ASTs\<close> |
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1416 |
|
| 62106 | 1417 |
text \<open> |
1418 |
The parse tree is the raw output of the parser. It is transformed into an |
|
1419 |
AST according to some basic scheme that may be augmented by AST translation |
|
1420 |
functions as explained in \secref{sec:tr-funs}.
|
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1421 |
|
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1422 |
The parse tree is constructed by nesting the right-hand sides of the |
| 62106 | 1423 |
productions used to recognize the input. Such parse trees are simply lists |
1424 |
of tokens and constituent parse trees, the latter representing the |
|
1425 |
nonterminals of the productions. Ignoring AST translation functions, parse |
|
1426 |
trees are transformed to ASTs by stripping out delimiters and copy |
|
1427 |
productions, while retaining some source position information from input |
|
1428 |
tokens. |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1429 |
|
| 62106 | 1430 |
The Pure syntax provides predefined AST translations to make the basic |
1431 |
\<open>\<lambda>\<close>-term structure more apparent within the (first-order) AST |
|
1432 |
representation, and thus facilitate the use of @{command translations} (see
|
|
1433 |
also \secref{sec:syn-trans}). This covers ordinary term application, type
|
|
1434 |
application, nested abstraction, iterated meta implications and function |
|
1435 |
types. The effect is illustrated on some representative input strings is as |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1436 |
follows: |
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1437 |
|
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1438 |
\begin{center}
|
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1439 |
\begin{tabular}{ll}
|
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1440 |
input source & AST \\ |
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1441 |
\hline |
| 61503 | 1442 |
\<open>f x y z\<close> & \<^verbatim>\<open>(f x y z)\<close> \\ |
1443 |
\<open>'a ty\<close> & \<^verbatim>\<open>(ty 'a)\<close> \\ |
|
1444 |
\<open>('a, 'b)ty\<close> & \<^verbatim>\<open>(ty 'a 'b)\<close> \\
|
|
1445 |
\<open>\<lambda>x y z. t\<close> & \<^verbatim>\<open>("_abs" x ("_abs" y ("_abs" z t)))\<close> \\
|
|
1446 |
\<open>\<lambda>x :: 'a. t\<close> & \<^verbatim>\<open>("_abs" ("_constrain" x 'a) t)\<close> \\
|
|
1447 |
\<open>\<lbrakk>P; Q; R\<rbrakk> \<Longrightarrow> S\<close> & \<^verbatim>\<open>("Pure.imp" P ("Pure.imp" Q ("Pure.imp" R S)))\<close> \\
|
|
1448 |
\<open>['a, 'b, 'c] \<Rightarrow> 'd\<close> & \<^verbatim>\<open>("fun" 'a ("fun" 'b ("fun" 'c 'd)))\<close> \\
|
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1449 |
\end{tabular}
|
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1450 |
\end{center}
|
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1451 |
|
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1452 |
Note that type and sort constraints may occur in further places --- |
| 62106 | 1453 |
translations need to be ready to cope with them. The built-in syntax |
1454 |
transformation from parse trees to ASTs insert additional constraints that |
|
1455 |
represent source positions. |
|
| 58618 | 1456 |
\<close> |
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1457 |
|
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1458 |
|
| 58618 | 1459 |
subsubsection \<open>Transforming ASTs to terms\<close> |
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1460 |
|
| 62106 | 1461 |
text \<open> |
1462 |
After application of macros (\secref{sec:syn-trans}), the AST is transformed
|
|
1463 |
into a term. This term still lacks proper type information, but it might |
|
1464 |
contain some constraints consisting of applications with head \<^verbatim>\<open>_constrain\<close>, |
|
1465 |
where the second argument is a type encoded as a pre-term within the syntax. |
|
1466 |
Type inference later introduces correct types, or indicates type errors in |
|
1467 |
the input. |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1468 |
|
| 62106 | 1469 |
Ignoring parse translations, ASTs are transformed to terms by mapping AST |
1470 |
constants to term constants, AST variables to term variables or constants |
|
1471 |
(according to the name space), and AST applications to iterated term |
|
1472 |
applications. |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1473 |
|
| 62106 | 1474 |
The outcome is still a first-order term. Proper abstractions and bound |
1475 |
variables are introduced by parse translations associated with certain |
|
1476 |
syntax constants. Thus \<^verbatim>\<open>("_abs" x x)\<close> eventually becomes a de-Bruijn term
|
|
1477 |
\<^verbatim>\<open>Abs ("x", _, Bound 0)\<close>.
|
|
| 58618 | 1478 |
\<close> |
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1479 |
|
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1480 |
|
| 58618 | 1481 |
subsubsection \<open>Printing of terms\<close> |
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1482 |
|
| 62106 | 1483 |
text \<open> |
1484 |
The output phase is essentially the inverse of the input phase. Terms are |
|
1485 |
translated via abstract syntax trees into pretty-printed text. |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1486 |
|
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1487 |
Ignoring print translations, the transformation maps term constants, |
|
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1488 |
variables and applications to the corresponding constructs on ASTs. |
| 62106 | 1489 |
Abstractions are mapped to applications of the special constant \<^verbatim>\<open>_abs\<close> as |
1490 |
seen before. Type constraints are represented via special \<^verbatim>\<open>_constrain\<close> |
|
1491 |
forms, according to various policies of type annotation determined |
|
1492 |
elsewhere. Sort constraints of type variables are handled in a similar |
|
1493 |
fashion. |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1494 |
|
| 62106 | 1495 |
After application of macros (\secref{sec:syn-trans}), the AST is finally
|
1496 |
pretty-printed. The built-in print AST translations reverse the |
|
1497 |
corresponding parse AST translations. |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1498 |
|
| 61421 | 1499 |
\<^medskip> |
1500 |
For the actual printing process, the priority grammar |
|
| 62106 | 1501 |
(\secref{sec:priority-grammar}) plays a vital role: productions are used as
|
1502 |
templates for pretty printing, with argument slots stemming from |
|
1503 |
nonterminals, and syntactic sugar stemming from literal tokens. |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1504 |
|
| 62106 | 1505 |
Each AST application with constant head \<open>c\<close> and arguments \<open>t\<^sub>1\<close>, \dots, |
1506 |
\<open>t\<^sub>n\<close> (for \<open>n = 0\<close> the AST is just the constant \<open>c\<close> itself) is printed |
|
1507 |
according to the first grammar production of result name \<open>c\<close>. The required |
|
1508 |
syntax priority of the argument slot is given by its nonterminal \<open>A\<^sup>(\<^sup>p\<^sup>)\<close>. |
|
1509 |
The argument \<open>t\<^sub>i\<close> that corresponds to the position of \<open>A\<^sup>(\<^sup>p\<^sup>)\<close> is printed |
|
1510 |
recursively, and then put in parentheses \<^emph>\<open>if\<close> its priority \<open>p\<close> requires |
|
1511 |
this. The resulting output is concatenated with the syntactic sugar |
|
1512 |
according to the grammar production. |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1513 |
|
| 62106 | 1514 |
If an AST application \<open>(c x\<^sub>1 \<dots> x\<^sub>m)\<close> has more arguments than the |
1515 |
corresponding production, it is first split into \<open>((c x\<^sub>1 \<dots> x\<^sub>n) x\<^sub>n\<^sub>+\<^sub>1 \<dots> |
|
1516 |
x\<^sub>m)\<close> and then printed recursively as above. |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1517 |
|
| 62106 | 1518 |
Applications with too few arguments or with non-constant head or without a |
1519 |
corresponding production are printed in prefix-form like \<open>f t\<^sub>1 \<dots> t\<^sub>n\<close> for |
|
1520 |
terms. |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1521 |
|
| 62106 | 1522 |
Multiple productions associated with some name \<open>c\<close> are tried in order of |
1523 |
appearance within the grammar. An occurrence of some AST variable \<open>x\<close> is |
|
1524 |
printed as \<open>x\<close> outright. |
|
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1525 |
|
| 61421 | 1526 |
\<^medskip> |
| 62106 | 1527 |
White space is \<^emph>\<open>not\<close> inserted automatically. If blanks (or breaks) are |
1528 |
required to separate tokens, they need to be specified in the mixfix |
|
1529 |
declaration (\secref{sec:mixfix}).
|
|
| 58618 | 1530 |
\<close> |
|
52414
8429123bc58a
more on built-in syntax transformations, based on reduced version of old material;
wenzelm
parents:
52413
diff
changeset
|
1531 |
|
| 28762 | 1532 |
end |