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