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