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