author | wenzelm |
Thu, 02 Feb 2012 18:11:42 +0100 | |
changeset 46282 | 83864b045a72 |
parent 45703 | c7a13ce60161 |
child 46284 | 6233d0b74d71 |
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 |
|
27 |
translations} --- either as rewrite systems on first-order ASTs |
|
28 |
(\secref{sec:syn-trans}) or ML functions on ASTs or @{text |
|
29 |
"\<lambda>"}-terms that represent parse trees (\secref{sec:tr-funs}). |
|
30 |
*} |
|
31 |
||
32 |
||
28762 | 33 |
section {* Printing logical entities *} |
34 |
||
35 |
subsection {* Diagnostic commands *} |
|
36 |
||
37 |
text {* |
|
38 |
\begin{matharray}{rcl} |
|
28766
accab7594b8e
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28765
diff
changeset
|
39 |
@{command_def "typ"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\ |
28762 | 40 |
@{command_def "term"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\ |
41 |
@{command_def "prop"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\ |
|
28766
accab7594b8e
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28765
diff
changeset
|
42 |
@{command_def "thm"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\ |
28762 | 43 |
@{command_def "prf"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\ |
44 |
@{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
|
45 |
@{command_def "pr"}@{text "\<^sup>*"} & : & @{text "any \<rightarrow>"} \\ |
28762 | 46 |
\end{matharray} |
47 |
||
48 |
These diagnostic commands assist interactive development by printing |
|
49 |
internal logical entities in a human-readable fashion. |
|
50 |
||
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
51 |
@{rail " |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
52 |
@@{command typ} @{syntax modes}? @{syntax type} |
28762 | 53 |
; |
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
54 |
@@{command term} @{syntax modes}? @{syntax term} |
28762 | 55 |
; |
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
56 |
@@{command prop} @{syntax modes}? @{syntax prop} |
28762 | 57 |
; |
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
58 |
@@{command thm} @{syntax modes}? @{syntax thmrefs} |
28762 | 59 |
; |
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
60 |
( @@{command prf} | @@{command full_prf} ) @{syntax modes}? @{syntax thmrefs}? |
28762 | 61 |
; |
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
62 |
@@{command pr} @{syntax modes}? @{syntax nat}? |
28762 | 63 |
; |
64 |
||
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
65 |
@{syntax_def modes}: '(' (@{syntax name} + ) ')' |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
66 |
"} |
28762 | 67 |
|
68 |
\begin{description} |
|
69 |
||
28766
accab7594b8e
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28765
diff
changeset
|
70 |
\item @{command "typ"}~@{text \<tau>} reads and prints types of the |
accab7594b8e
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28765
diff
changeset
|
71 |
meta-logic according to the current theory or proof context. |
accab7594b8e
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28765
diff
changeset
|
72 |
|
accab7594b8e
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28765
diff
changeset
|
73 |
\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
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
inspecting the current environment of term abbreviations. |
28762 | 78 |
|
79 |
\item @{command "thm"}~@{text "a\<^sub>1 \<dots> a\<^sub>n"} retrieves |
|
80 |
theorems from the current theory or proof context. Note that any |
|
81 |
attributes included in the theorem specifications are applied to a |
|
82 |
temporary context derived from the current theory or proof; the |
|
83 |
result is discarded, i.e.\ attributes involved in @{text "a\<^sub>1, |
|
84 |
\<dots>, a\<^sub>n"} do not have any permanent effect. |
|
85 |
||
86 |
\item @{command "prf"} displays the (compact) proof term of the |
|
87 |
current proof state (if present), or of the given theorems. Note |
|
88 |
that this requires proof terms to be switched on for the current |
|
89 |
object logic (see the ``Proof terms'' section of the Isabelle |
|
90 |
reference manual for information on how to do this). |
|
91 |
||
92 |
\item @{command "full_prf"} is like @{command "prf"}, but displays |
|
93 |
the full proof term, i.e.\ also displays information omitted in the |
|
94 |
compact proof term, which is denoted by ``@{text _}'' placeholders |
|
95 |
there. |
|
96 |
||
39165 | 97 |
\item @{command "pr"}~@{text "goals"} prints the current proof state |
98 |
(if present), including current facts and goals. The optional limit |
|
99 |
arguments affect the number of goals to be displayed, which is |
|
100 |
initially 10. Omitting limit value leaves the current setting |
|
101 |
unchanged. |
|
28766
accab7594b8e
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28765
diff
changeset
|
102 |
|
28762 | 103 |
\end{description} |
104 |
||
105 |
All of the diagnostic commands above admit a list of @{text modes} |
|
42926 | 106 |
to be specified, which is appended to the current print mode; see |
107 |
\secref{sec:print-modes}. Thus the output behavior may be modified |
|
28762 | 108 |
according particular print mode features. For example, @{command |
109 |
"pr"}~@{text "(latex xsymbols)"} would print the current proof state |
|
110 |
with mathematical symbols and special characters represented in |
|
111 |
{\LaTeX} source, according to the Isabelle style |
|
112 |
\cite{isabelle-sys}. |
|
113 |
||
114 |
Note that antiquotations (cf.\ \secref{sec:antiq}) provide a more |
|
115 |
systematic way to include formal items into the printed text |
|
116 |
document. |
|
117 |
*} |
|
118 |
||
119 |
||
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
120 |
subsection {* Details of printed content *} |
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
121 |
|
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
122 |
text {* |
42655 | 123 |
\begin{tabular}{rcll} |
124 |
@{attribute_def show_types} & : & @{text attribute} & default @{text false} \\ |
|
125 |
@{attribute_def show_sorts} & : & @{text attribute} & default @{text false} \\ |
|
126 |
@{attribute_def show_consts} & : & @{text attribute} & default @{text false} \\ |
|
127 |
@{attribute_def show_abbrevs} & : & @{text attribute} & default @{text true} \\ |
|
128 |
@{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
|
129 |
@{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
|
130 |
@{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
|
131 |
@{attribute_def names_unique} & : & @{text attribute} & default @{text true} \\ |
42655 | 132 |
@{attribute_def eta_contract} & : & @{text attribute} & default @{text true} \\ |
133 |
@{attribute_def goals_limit} & : & @{text attribute} & default @{text 10} \\ |
|
134 |
@{attribute_def show_main_goal} & : & @{text attribute} & default @{text false} \\ |
|
135 |
@{attribute_def show_hyps} & : & @{text attribute} & default @{text false} \\ |
|
136 |
@{attribute_def show_tags} & : & @{text attribute} & default @{text false} \\ |
|
137 |
@{attribute_def show_question_marks} & : & @{text attribute} & default @{text true} \\ |
|
138 |
\end{tabular} |
|
139 |
\medskip |
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
140 |
|
42655 | 141 |
These configuration options control the detail of information that |
142 |
is displayed for types, terms, theorems, goals etc. See also |
|
143 |
\secref{sec:config}. |
|
28765
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
144 |
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
145 |
\begin{description} |
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
146 |
|
42655 | 147 |
\item @{attribute show_types} and @{attribute show_sorts} control |
148 |
printing of type constraints for term variables, and sort |
|
149 |
constraints for type variables. By default, neither of these are |
|
150 |
shown in output. If @{attribute show_sorts} is enabled, types are |
|
151 |
always shown as well. |
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
152 |
|
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
153 |
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
|
154 |
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
|
155 |
rule does not apply as expected. |
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
156 |
|
42655 | 157 |
\item @{attribute show_consts} controls printing of types of |
158 |
constants when displaying a goal state. |
|
28765
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
159 |
|
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
160 |
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
|
161 |
often occur at several different type instances. |
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
162 |
|
42655 | 163 |
\item @{attribute show_abbrevs} controls folding of constant |
164 |
abbreviations. |
|
40879
ca132ef44944
configuration option "show_abbrevs" supersedes print mode "no_abbrevs", with inverted meaning;
wenzelm
parents:
40255
diff
changeset
|
165 |
|
42655 | 166 |
\item @{attribute show_brackets} controls bracketing in pretty |
167 |
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
|
168 |
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
|
169 |
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
|
170 |
pretty printed entities may occasionally help to diagnose problems |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
171 |
with operator priorities, for example. |
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
172 |
|
42669
04dfffda5671
more conventional naming scheme: names_long, names_short, names_unique;
wenzelm
parents:
42655
diff
changeset
|
173 |
\item @{attribute names_long}, @{attribute names_short}, and |
04dfffda5671
more conventional naming scheme: names_long, names_short, names_unique;
wenzelm
parents:
42655
diff
changeset
|
174 |
@{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
|
175 |
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
|
176 |
\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
|
177 |
same names. |
b47d41d9f4b5
Name_Space: proper configuration options long_names, short_names, unique_names instead of former unsynchronized references;
wenzelm
parents:
42279
diff
changeset
|
178 |
|
42655 | 179 |
\item @{attribute eta_contract} controls @{text "\<eta>"}-contracted |
180 |
printing of terms. |
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
181 |
|
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
182 |
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
|
183 |
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
|
184 |
\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
|
185 |
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
|
186 |
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
|
187 |
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
|
188 |
"\<lambda>h. F (\<lambda>x. h x)"}. |
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
189 |
|
42655 | 190 |
Enabling @{attribute eta_contract} makes Isabelle perform @{text |
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
191 |
\<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
|
192 |
appears simply as @{text F}. |
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
193 |
|
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
194 |
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
|
195 |
form occasionally matters. While higher-order resolution and |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
196 |
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
|
197 |
might look at terms more discretely. |
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
198 |
|
42655 | 199 |
\item @{attribute goals_limit} controls the maximum number of |
39130 | 200 |
subgoals to be shown in goal output. |
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
201 |
|
42655 | 202 |
\item @{attribute show_main_goal} controls whether the main result |
203 |
to be proven should be displayed. This information might be |
|
39130 | 204 |
relevant for schematic goals, to inspect the current claim that has |
205 |
been synthesized so far. |
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
206 |
|
42655 | 207 |
\item @{attribute show_hyps} controls printing of implicit |
208 |
hypotheses of local facts. Normally, only those hypotheses are |
|
209 |
displayed that are \emph{not} covered by the assumptions of the |
|
210 |
current context: this situation indicates a fault in some tool being |
|
211 |
used. |
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
212 |
|
42655 | 213 |
By enabling @{attribute show_hyps}, output of \emph{all} hypotheses |
214 |
can be enforced, which is occasionally useful for diagnostic |
|
215 |
purposes. |
|
28763
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
216 |
|
42655 | 217 |
\item @{attribute show_tags} controls printing of extra annotations |
218 |
within theorems, such as internal position information, or the case |
|
219 |
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
|
220 |
|
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
221 |
Note that the @{attribute tagged} and @{attribute untagged} |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
222 |
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
|
223 |
associated with a theorem. |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
224 |
|
42655 | 225 |
\item @{attribute show_question_marks} controls printing of question |
226 |
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
|
227 |
question mark is affected, the remaining text is unchanged |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
228 |
(including proper markup for schematic variables that might be |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
229 |
relevant for user interfaces). |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
230 |
|
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
231 |
\end{description} |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
232 |
*} |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
233 |
|
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
234 |
|
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
235 |
subsection {* Printing limits *} |
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 |
text {* |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
238 |
\begin{mldecls} |
36745 | 239 |
@{index_ML Pretty.margin_default: "int Unsynchronized.ref"} \\ |
28765
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
240 |
@{index_ML print_depth: "int -> unit"} \\ |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
241 |
\end{mldecls} |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
242 |
|
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
243 |
These ML functions set limits for pretty printed text. |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
244 |
|
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
245 |
\begin{description} |
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
246 |
|
36745 | 247 |
\item @{ML Pretty.margin_default} indicates the global default for |
248 |
the right margin of the built-in pretty printer, with initial value |
|
249 |
76. Note that user-interfaces typically control margins |
|
250 |
automatically when resizing windows, or even bypass the formatting |
|
251 |
engine of Isabelle/ML altogether and do it within the front end via |
|
252 |
Isabelle/Scala. |
|
28765
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
253 |
|
da8f6f4a74be
misc tuning and rearrangement of section "Printing logical entities";
wenzelm
parents:
28763
diff
changeset
|
254 |
\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
|
255 |
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
|
256 |
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
|
257 |
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
|
258 |
|
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
259 |
\end{description} |
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
260 |
*} |
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
261 |
|
b5e6122ff575
added pretty printing options (from old ref manual);
wenzelm
parents:
28762
diff
changeset
|
262 |
|
46282 | 263 |
section {* Mixfix annotations \label{sec:mixfix} *} |
28762 | 264 |
|
265 |
text {* Mixfix annotations specify concrete \emph{inner syntax} of |
|
35351
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
32833
diff
changeset
|
266 |
Isabelle types and terms. Locally fixed parameters in toplevel |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
32833
diff
changeset
|
267 |
theorem statements, locale specifications etc.\ also admit mixfix |
7425aece4ee3
allow general mixfix syntax for type constructors;
wenzelm
parents:
32833
diff
changeset
|
268 |
annotations. |
28762 | 269 |
|
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
270 |
@{rail " |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
271 |
@{syntax_def \"infix\"}: '(' (@'infix' | @'infixl' | @'infixr') |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
272 |
@{syntax string} @{syntax nat} ')' |
28762 | 273 |
; |
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
274 |
@{syntax_def mixfix}: @{syntax \"infix\"} | '(' @{syntax string} prios? @{syntax nat}? ')' | |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
275 |
'(' @'binder' @{syntax string} prios? @{syntax nat} ')' |
28762 | 276 |
; |
42705 | 277 |
@{syntax_def struct_mixfix}: @{syntax mixfix} | '(' @'structure' ')' |
28762 | 278 |
; |
279 |
||
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
280 |
prios: '[' (@{syntax nat} + ',') ']' |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
281 |
"} |
28762 | 282 |
|
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
283 |
Here the @{syntax string} specifications refer to the actual mixfix |
28762 | 284 |
template, which may include literal text, spacing, blocks, and |
285 |
arguments (denoted by ``@{text _}''); the special symbol |
|
286 |
``@{verbatim "\<index>"}'' (printed as ``@{text "\<index>"}'') represents an index |
|
287 |
argument that specifies an implicit structure reference (see also |
|
288 |
\secref{sec:locale}). Infix and binder declarations provide common |
|
289 |
abbreviations for particular mixfix declarations. So in practice, |
|
290 |
mixfix templates mostly degenerate to literal text for concrete |
|
291 |
syntax, such as ``@{verbatim "++"}'' for an infix symbol. |
|
292 |
||
293 |
\medskip In full generality, mixfix declarations work as follows. |
|
294 |
Suppose a constant @{text "c :: \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>"} is |
|
295 |
annotated by @{text "(mixfix [p\<^sub>1, \<dots>, p\<^sub>n] p)"}, where @{text |
|
296 |
"mixfix"} is a string @{text "d\<^sub>0 _ d\<^sub>1 _ \<dots> _ d\<^sub>n"} consisting of |
|
297 |
delimiters that surround argument positions as indicated by |
|
298 |
underscores. |
|
299 |
||
300 |
Altogether this determines a production for a context-free priority |
|
301 |
grammar, where for each argument @{text "i"} the syntactic category |
|
302 |
is determined by @{text "\<tau>\<^sub>i"} (with priority @{text "p\<^sub>i"}), and |
|
303 |
the result category is determined from @{text "\<tau>"} (with |
|
304 |
priority @{text "p"}). Priority specifications are optional, with |
|
305 |
default 0 for arguments and 1000 for the result. |
|
306 |
||
307 |
Since @{text "\<tau>"} may be again a function type, the constant |
|
308 |
type scheme may have more argument positions than the mixfix |
|
309 |
pattern. Printing a nested application @{text "c t\<^sub>1 \<dots> t\<^sub>m"} for |
|
310 |
@{text "m > n"} works by attaching concrete notation only to the |
|
311 |
innermost part, essentially by printing @{text "(c t\<^sub>1 \<dots> t\<^sub>n) \<dots> t\<^sub>m"} |
|
312 |
instead. If a term has fewer arguments than specified in the mixfix |
|
313 |
template, the concrete syntax is ignored. |
|
314 |
||
315 |
\medskip A mixfix template may also contain additional directives |
|
316 |
for pretty printing, notably spaces, blocks, and breaks. The |
|
317 |
general template format is a sequence over any of the following |
|
318 |
entities. |
|
319 |
||
28778 | 320 |
\begin{description} |
28762 | 321 |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
322 |
\item @{text "d"} is a delimiter, namely a non-empty sequence of |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
323 |
characters other than the following special characters: |
28762 | 324 |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
325 |
\smallskip |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
326 |
\begin{tabular}{ll} |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
327 |
@{verbatim "'"} & single quote \\ |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
328 |
@{verbatim "_"} & underscore \\ |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
329 |
@{text "\<index>"} & index symbol \\ |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
330 |
@{verbatim "("} & open parenthesis \\ |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
331 |
@{verbatim ")"} & close parenthesis \\ |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
332 |
@{verbatim "/"} & slash \\ |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
333 |
\end{tabular} |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
334 |
\medskip |
28762 | 335 |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
336 |
\item @{verbatim "'"} escapes the special meaning of these |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
337 |
meta-characters, producing a literal version of the following |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
338 |
character, unless that is a blank. |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
339 |
|
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
340 |
A single quote followed by a blank separates delimiters, without |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
341 |
affecting printing, but input tokens may have additional white space |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
342 |
here. |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
343 |
|
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
344 |
\item @{verbatim "_"} is an argument position, which stands for a |
28762 | 345 |
certain syntactic category in the underlying grammar. |
346 |
||
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
347 |
\item @{text "\<index>"} is an indexed argument position; this is the place |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
348 |
where implicit structure arguments can be attached. |
28762 | 349 |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
350 |
\item @{text "s"} is a non-empty sequence of spaces for printing. |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
351 |
This and the following specifications do not affect parsing at all. |
28762 | 352 |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
353 |
\item @{verbatim "("}@{text n} opens a pretty printing block. The |
28762 | 354 |
optional number specifies how much indentation to add when a line |
355 |
break occurs within the block. If the parenthesis is not followed |
|
356 |
by digits, the indentation defaults to 0. A block specified via |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
357 |
@{verbatim "(00"} is unbreakable. |
28762 | 358 |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
359 |
\item @{verbatim ")"} closes a pretty printing block. |
28762 | 360 |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
361 |
\item @{verbatim "//"} forces a line break. |
28762 | 362 |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
363 |
\item @{verbatim "/"}@{text s} allows a line break. Here @{text s} |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
364 |
stands for the string of spaces (zero or more) right after the |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
365 |
slash. These spaces are printed if the break is \emph{not} taken. |
28762 | 366 |
|
28778 | 367 |
\end{description} |
28762 | 368 |
|
28771
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
369 |
For example, the template @{verbatim "(_ +/ _)"} specifies an infix |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
370 |
operator. There are two argument positions; the delimiter |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
371 |
@{verbatim "+"} is preceded by a space and followed by a space or |
4510201c6aaf
mixfix annotations: verbatim for special symbols;
wenzelm
parents:
28770
diff
changeset
|
372 |
line break; the entire phrase is a pretty printing block. |
28762 | 373 |
|
374 |
The general idea of pretty printing with blocks and breaks is also |
|
375 |
described in \cite{paulson-ml2}. |
|
376 |
*} |
|
377 |
||
378 |
||
46282 | 379 |
section {* Explicit notation \label{sec:notation} *} |
28762 | 380 |
|
381 |
text {* |
|
382 |
\begin{matharray}{rcll} |
|
35413 | 383 |
@{command_def "type_notation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\ |
384 |
@{command_def "no_type_notation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\ |
|
28762 | 385 |
@{command_def "notation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\ |
386 |
@{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
|
387 |
@{command_def "write"} & : & @{text "proof(state) \<rightarrow> proof(state)"} \\ |
28762 | 388 |
\end{matharray} |
389 |
||
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
390 |
@{rail " |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
391 |
(@@{command type_notation} | @@{command no_type_notation}) @{syntax target}? |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
392 |
@{syntax mode}? \\ (@{syntax nameref} @{syntax mixfix} + @'and') |
35413 | 393 |
; |
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
394 |
(@@{command notation} | @@{command no_notation}) @{syntax target}? @{syntax mode}? \\ |
42705 | 395 |
(@{syntax nameref} @{syntax struct_mixfix} + @'and') |
28762 | 396 |
; |
42705 | 397 |
@@{command write} @{syntax mode}? (@{syntax nameref} @{syntax struct_mixfix} + @'and') |
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
398 |
"} |
28762 | 399 |
|
400 |
\begin{description} |
|
401 |
||
35413 | 402 |
\item @{command "type_notation"}~@{text "c (mx)"} associates mixfix |
403 |
syntax with an existing type constructor. The arity of the |
|
404 |
constructor is retrieved from the context. |
|
46282 | 405 |
|
35413 | 406 |
\item @{command "no_type_notation"} is similar to @{command |
407 |
"type_notation"}, but removes the specified syntax annotation from |
|
408 |
the present context. |
|
409 |
||
28762 | 410 |
\item @{command "notation"}~@{text "c (mx)"} associates mixfix |
35413 | 411 |
syntax with an existing constant or fixed variable. The type |
412 |
declaration of the given entity is retrieved from the context. |
|
46282 | 413 |
|
28762 | 414 |
\item @{command "no_notation"} is similar to @{command "notation"}, |
415 |
but removes the specified syntax annotation from the present |
|
416 |
context. |
|
417 |
||
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
|
418 |
\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
|
419 |
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
|
420 |
|
28762 | 421 |
\end{description} |
35413 | 422 |
|
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
|
423 |
Note that the more primitive commands @{command "syntax"} and |
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
|
424 |
@{command "no_syntax"} (\secref{sec:syn-trans}) provide raw access |
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
|
425 |
to the syntax tables of a global theory. |
28762 | 426 |
*} |
427 |
||
28778 | 428 |
|
429 |
section {* The Pure syntax \label{sec:pure-syntax} *} |
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
430 |
|
46282 | 431 |
subsection {* Lexical matters \label{sec:inner-lex} *} |
432 |
||
433 |
text {* The inner lexical syntax vaguely resembles the outer one |
|
434 |
(\secref{sec:outer-lex}), but some details are different. There are |
|
435 |
two main categories of inner syntax tokens: |
|
436 |
||
437 |
\begin{enumerate} |
|
438 |
||
439 |
\item \emph{delimiters} --- the literal tokens occurring in |
|
440 |
productions of the given priority grammar (cf.\ |
|
441 |
\secref{sec:priority-grammar}); |
|
442 |
||
443 |
\item \emph{named tokens} --- various categories of identifiers etc. |
|
444 |
||
445 |
\end{enumerate} |
|
446 |
||
447 |
Delimiters override named tokens and may thus render certain |
|
448 |
identifiers inaccessible. Sometimes the logical context admits |
|
449 |
alternative ways to refer to the same entity, potentially via |
|
450 |
qualified names. |
|
451 |
||
452 |
\medskip The categories for named tokens are defined once and for |
|
453 |
all as follows, reusing some categories of the outer token syntax |
|
454 |
(\secref{sec:outer-lex}). |
|
455 |
||
456 |
\begin{center} |
|
457 |
\begin{supertabular}{rcl} |
|
458 |
@{syntax_def (inner) id} & = & @{syntax_ref ident} \\ |
|
459 |
@{syntax_def (inner) longid} & = & @{syntax_ref longident} \\ |
|
460 |
@{syntax_def (inner) var} & = & @{syntax_ref var} \\ |
|
461 |
@{syntax_def (inner) tid} & = & @{syntax_ref typefree} \\ |
|
462 |
@{syntax_def (inner) tvar} & = & @{syntax_ref typevar} \\ |
|
463 |
@{syntax_def (inner) num_token} & = & @{syntax_ref nat}@{text " | "}@{verbatim "-"}@{syntax_ref nat} \\ |
|
464 |
@{syntax_def (inner) float_token} & = & @{syntax_ref nat}@{verbatim "."}@{syntax_ref nat}@{text " | "}@{verbatim "-"}@{syntax_ref nat}@{verbatim "."}@{syntax_ref nat} \\ |
|
465 |
@{syntax_def (inner) xnum_token} & = & @{verbatim "#"}@{syntax_ref nat}@{text " | "}@{verbatim "#-"}@{syntax_ref nat} \\ |
|
466 |
||
467 |
@{syntax_def (inner) xstr} & = & @{verbatim "''"} @{text "\<dots>"} @{verbatim "''"} \\ |
|
468 |
\end{supertabular} |
|
469 |
\end{center} |
|
470 |
||
471 |
The token categories @{syntax (inner) num_token}, @{syntax (inner) |
|
472 |
float_token}, @{syntax (inner) xnum_token}, and @{syntax (inner) |
|
473 |
xstr} are not used in Pure. Object-logics may implement numerals |
|
474 |
and string constants by adding appropriate syntax declarations, |
|
475 |
together with some translation functions (e.g.\ see Isabelle/HOL). |
|
476 |
||
477 |
The derived categories @{syntax_def (inner) num_const}, @{syntax_def |
|
478 |
(inner) float_const}, and @{syntax_def (inner) num_const} provide |
|
479 |
robust access to the respective tokens: the syntax tree holds a |
|
480 |
syntactic constant instead of a free variable. |
|
481 |
*} |
|
482 |
||
483 |
||
28777 | 484 |
subsection {* Priority grammars \label{sec:priority-grammar} *} |
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
485 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
486 |
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
|
487 |
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
|
488 |
\emph{productions}. Productions have the form @{text "A = \<gamma>"}, |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
489 |
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
|
490 |
terminals and nonterminals. One designated nonterminal is called |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
491 |
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
|
492 |
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
|
493 |
root symbol by applying productions as rewrite rules. |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
494 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
495 |
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
|
496 |
grammar}. Each nonterminal is decorated by an integer priority: |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
497 |
@{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
|
498 |
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
|
499 |
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
|
500 |
grammar by introducing new nonterminals and productions. |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
501 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
502 |
\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
|
503 |
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
|
504 |
\<alpha>} and @{text \<beta>} denote strings of terminal or nonterminal symbols. |
28774 | 505 |
Then @{text "\<alpha> A\<^sup>(\<^sup>p\<^sup>) \<beta> \<longrightarrow>\<^sub>G \<alpha> \<gamma> \<beta>"} holds if and only if @{text G} |
506 |
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
|
507 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
508 |
\medskip The following grammar for arithmetic expressions |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
509 |
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
|
510 |
enforced by priorities. |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
511 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
512 |
\begin{center} |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
513 |
\begin{tabular}{rclr} |
28774 | 514 |
@{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
|
515 |
@{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
|
516 |
@{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
|
517 |
@{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
|
518 |
@{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
|
519 |
\end{tabular} |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
520 |
\end{center} |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
521 |
The choice of priorities determines that @{verbatim "-"} binds |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
522 |
tighter than @{verbatim "*"}, which binds tighter than @{verbatim |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
523 |
"+"}. Furthermore @{verbatim "+"} associates to the left and |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
524 |
@{verbatim "*"} to the right. |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
525 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
526 |
\medskip For clarity, grammars obey these conventions: |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
527 |
\begin{itemize} |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
528 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
529 |
\item All priorities must lie between 0 and 1000. |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
530 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
531 |
\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
|
532 |
left-hand side may be omitted. |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
533 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
534 |
\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
|
535 |
(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
|
536 |
a column on the far right. |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
537 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
538 |
\item Alternatives are separated by @{text "|"}. |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
539 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
540 |
\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
|
541 |
but obvious way. |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
542 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
543 |
\end{itemize} |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
544 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
545 |
Using these conventions, the example grammar specification above |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
546 |
takes the form: |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
547 |
\begin{center} |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
548 |
\begin{tabular}{rclc} |
28774 | 549 |
@{text A} & @{text "="} & @{verbatim "("} @{text A} @{verbatim ")"} \\ |
550 |
& @{text "|"} & @{verbatim 0} & \qquad\qquad \\ |
|
28769
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
551 |
& @{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
|
552 |
& @{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
|
553 |
& @{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
|
554 |
\end{tabular} |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
555 |
\end{center} |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
556 |
*} |
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
557 |
|
8fc228f21861
added section "Priority grammars" (variant from old ref manual);
wenzelm
parents:
28767
diff
changeset
|
558 |
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
559 |
subsection {* The Pure grammar *} |
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
560 |
|
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
561 |
text {* |
28773 | 562 |
The priority grammar of the @{text "Pure"} theory is defined as follows: |
563 |
||
28774 | 564 |
%FIXME syntax for "index" (?) |
565 |
%FIXME "op" versions of ==> etc. (?) |
|
566 |
||
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
567 |
\begin{center} |
28773 | 568 |
\begin{supertabular}{rclr} |
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
569 |
|
28778 | 570 |
@{syntax_def (inner) any} & = & @{text "prop | logic"} \\\\ |
28772 | 571 |
|
28778 | 572 |
@{syntax_def (inner) prop} & = & @{verbatim "("} @{text prop} @{verbatim ")"} \\ |
28772 | 573 |
& @{text "|"} & @{text "prop\<^sup>(\<^sup>4\<^sup>)"} @{verbatim "::"} @{text type} & @{text "(3)"} \\ |
574 |
& @{text "|"} & @{text "any\<^sup>(\<^sup>3\<^sup>)"} @{verbatim "=="} @{text "any\<^sup>(\<^sup>2\<^sup>)"} & @{text "(2)"} \\ |
|
28773 | 575 |
& @{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
|
576 |
& @{text "|"} & @{text "prop\<^sup>(\<^sup>3\<^sup>)"} @{verbatim "&&&"} @{text "prop\<^sup>(\<^sup>2\<^sup>)"} & @{text "(2)"} \\ |
28772 | 577 |
& @{text "|"} & @{text "prop\<^sup>(\<^sup>2\<^sup>)"} @{verbatim "==>"} @{text "prop\<^sup>(\<^sup>1\<^sup>)"} & @{text "(1)"} \\ |
28773 | 578 |
& @{text "|"} & @{text "prop\<^sup>(\<^sup>2\<^sup>)"} @{text "\<Longrightarrow>"} @{text "prop\<^sup>(\<^sup>1\<^sup>)"} & @{text "(1)"} \\ |
28772 | 579 |
& @{text "|"} & @{verbatim "[|"} @{text prop} @{verbatim ";"} @{text "\<dots>"} @{verbatim ";"} @{text prop} @{verbatim "|]"} @{verbatim "==>"} @{text "prop\<^sup>(\<^sup>1\<^sup>)"} & @{text "(1)"} \\ |
28773 | 580 |
& @{text "|"} & @{text "\<lbrakk>"} @{text prop} @{verbatim ";"} @{text "\<dots>"} @{verbatim ";"} @{text prop} @{text "\<rbrakk>"} @{text "\<Longrightarrow>"} @{text "prop\<^sup>(\<^sup>1\<^sup>)"} & @{text "(1)"} \\ |
28772 | 581 |
& @{text "|"} & @{verbatim "!!"} @{text idts} @{verbatim "."} @{text prop} & @{text "(0)"} \\ |
28773 | 582 |
& @{text "|"} & @{text "\<And>"} @{text idts} @{verbatim "."} @{text prop} & @{text "(0)"} \\ |
583 |
& @{text "|"} & @{verbatim OFCLASS} @{verbatim "("} @{text type} @{verbatim ","} @{text logic} @{verbatim ")"} \\ |
|
584 |
& @{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
|
585 |
& @{text "|"} & @{verbatim TERM} @{text logic} \\ |
28773 | 586 |
& @{text "|"} & @{verbatim PROP} @{text aprop} \\\\ |
28772 | 587 |
|
28856
5e009a80fe6d
Pure syntax: more coherent treatment of aprop, permanent TERM and &&&;
wenzelm
parents:
28779
diff
changeset
|
588 |
@{syntax_def (inner) aprop} & = & @{verbatim "("} @{text aprop} @{verbatim ")"} \\ |
5e009a80fe6d
Pure syntax: more coherent treatment of aprop, permanent TERM and &&&;
wenzelm
parents:
28779
diff
changeset
|
589 |
& @{text "|"} & @{text "id | longid | var | "}@{verbatim "_"}@{text " | "}@{verbatim "..."} \\ |
5e009a80fe6d
Pure syntax: more coherent treatment of aprop, permanent TERM and &&&;
wenzelm
parents:
28779
diff
changeset
|
590 |
& @{text "|"} & @{verbatim CONST} @{text "id | "}@{verbatim CONST} @{text "longid"} \\ |
28773 | 591 |
& @{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
|
592 |
|
28778 | 593 |
@{syntax_def (inner) logic} & = & @{verbatim "("} @{text logic} @{verbatim ")"} \\ |
28772 | 594 |
& @{text "|"} & @{text "logic\<^sup>(\<^sup>4\<^sup>)"} @{verbatim "::"} @{text type} & @{text "(3)"} \\ |
28773 | 595 |
& @{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
|
596 |
& @{text "|"} & @{verbatim CONST} @{text "id | "}@{verbatim CONST} @{text "longid"} \\ |
28773 | 597 |
& @{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)"} \\ |
28772 | 598 |
& @{text "|"} & @{verbatim "%"} @{text pttrns} @{verbatim "."} @{text "any\<^sup>(\<^sup>3\<^sup>)"} & @{text "(3)"} \\ |
28773 | 599 |
& @{text "|"} & @{text \<lambda>} @{text pttrns} @{verbatim "."} @{text "any\<^sup>(\<^sup>3\<^sup>)"} & @{text "(3)"} \\ |
28772 | 600 |
& @{text "|"} & @{verbatim TYPE} @{verbatim "("} @{text type} @{verbatim ")"} \\\\ |
601 |
||
28778 | 602 |
@{syntax_def (inner) idt} & = & @{verbatim "("} @{text idt} @{verbatim ")"}@{text " | id | "}@{verbatim "_"} \\ |
28773 | 603 |
& @{text "|"} & @{text id} @{verbatim "::"} @{text type} & @{text "(0)"} \\ |
604 |
& @{text "|"} & @{verbatim "_"} @{verbatim "::"} @{text type} & @{text "(0)"} \\\\ |
|
28772 | 605 |
|
28778 | 606 |
@{syntax_def (inner) idts} & = & @{text "idt | idt\<^sup>(\<^sup>1\<^sup>) idts"} & @{text "(0)"} \\\\ |
28772 | 607 |
|
28778 | 608 |
@{syntax_def (inner) pttrn} & = & @{text idt} \\\\ |
28772 | 609 |
|
28778 | 610 |
@{syntax_def (inner) pttrns} & = & @{text "pttrn | pttrn\<^sup>(\<^sup>1\<^sup>) pttrns"} & @{text "(0)"} \\\\ |
28774 | 611 |
|
28778 | 612 |
@{syntax_def (inner) type} & = & @{verbatim "("} @{text type} @{verbatim ")"} \\ |
28773 | 613 |
& @{text "|"} & @{text "tid | tvar | "}@{verbatim "_"} \\ |
614 |
& @{text "|"} & @{text "tid"} @{verbatim "::"} @{text "sort | tvar "}@{verbatim "::"} @{text "sort | "}@{verbatim "_"} @{verbatim "::"} @{text "sort"} \\ |
|
28772 | 615 |
& @{text "|"} & @{text "id | type\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>) id | "}@{verbatim "("} @{text type} @{verbatim ","} @{text "\<dots>"} @{verbatim ","} @{text type} @{verbatim ")"} @{text id} \\ |
29741 | 616 |
& @{text "|"} & @{text "longid | type\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>) longid"} \\ |
617 |
& @{text "|"} & @{verbatim "("} @{text type} @{verbatim ","} @{text "\<dots>"} @{verbatim ","} @{text type} @{verbatim ")"} @{text longid} \\ |
|
28772 | 618 |
& @{text "|"} & @{text "type\<^sup>(\<^sup>1\<^sup>)"} @{verbatim "=>"} @{text type} & @{text "(0)"} \\ |
28773 | 619 |
& @{text "|"} & @{text "type\<^sup>(\<^sup>1\<^sup>)"} @{text "\<Rightarrow>"} @{text type} & @{text "(0)"} \\ |
620 |
& @{text "|"} & @{verbatim "["} @{text type} @{verbatim ","} @{text "\<dots>"} @{verbatim ","} @{text type} @{verbatim "]"} @{verbatim "=>"} @{text type} & @{text "(0)"} \\ |
|
621 |
& @{text "|"} & @{verbatim "["} @{text type} @{verbatim ","} @{text "\<dots>"} @{verbatim ","} @{text type} @{verbatim "]"} @{text "\<Rightarrow>"} @{text type} & @{text "(0)"} \\\\ |
|
28772 | 622 |
|
29741 | 623 |
@{syntax_def (inner) sort} & = & @{text "id | longid | "}@{verbatim "{}"} \\ |
624 |
& @{text "|"} & @{verbatim "{"} @{text "(id | longid)"} @{verbatim ","} @{text "\<dots>"} @{verbatim ","} @{text "(id | longid)"} @{verbatim "}"} \\ |
|
28773 | 625 |
\end{supertabular} |
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
626 |
\end{center} |
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
627 |
|
28774 | 628 |
\medskip Here literal terminals are printed @{verbatim "verbatim"}; |
629 |
see also \secref{sec:inner-lex} for further token categories of the |
|
630 |
inner syntax. The meaning of the nonterminals defined by the above |
|
631 |
grammar is as follows: |
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
632 |
|
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
633 |
\begin{description} |
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
634 |
|
28778 | 635 |
\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
|
636 |
|
28778 | 637 |
\item @{syntax_ref (inner) prop} denotes meta-level propositions, |
638 |
which are terms of type @{typ prop}. The syntax of such formulae of |
|
639 |
the meta-logic is carefully distinguished from usual conventions for |
|
640 |
object-logics. In particular, plain @{text "\<lambda>"}-term notation is |
|
641 |
\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
|
642 |
|
28778 | 643 |
\item @{syntax_ref (inner) aprop} denotes atomic propositions, which |
644 |
are embedded into regular @{syntax (inner) prop} by means of an |
|
645 |
explicit @{verbatim PROP} token. |
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
646 |
|
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
647 |
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
|
648 |
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
|
649 |
prop} are expected to provide their own concrete syntax; otherwise |
28778 | 650 |
the printed version will appear like @{syntax (inner) logic} and |
651 |
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
|
652 |
|
28778 | 653 |
\item @{syntax_ref (inner) logic} denotes arbitrary terms of a |
654 |
logical type, excluding type @{typ prop}. This is the main |
|
655 |
syntactic category of object-logic entities, covering plain @{text |
|
656 |
\<lambda>}-term notation (variables, abstraction, application), plus |
|
657 |
anything defined by the user. |
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
658 |
|
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
659 |
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
|
660 |
(excluding @{typ prop}) are \emph{collapsed} to this single category |
28778 | 661 |
of @{syntax (inner) logic}. |
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
662 |
|
28778 | 663 |
\item @{syntax_ref (inner) idt} denotes identifiers, possibly |
664 |
constrained by types. |
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
665 |
|
28778 | 666 |
\item @{syntax_ref (inner) idts} denotes a sequence of @{syntax_ref |
667 |
(inner) idt}. This is the most basic category for variables in |
|
668 |
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
|
669 |
|
28778 | 670 |
\item @{syntax_ref (inner) pttrn} and @{syntax_ref (inner) pttrns} |
671 |
denote patterns for abstraction, cases bindings etc. In Pure, these |
|
672 |
categories start as a merely copy of @{syntax (inner) idt} and |
|
673 |
@{syntax (inner) idts}, respectively. Object-logics may add |
|
674 |
additional productions for binding forms. |
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
675 |
|
28778 | 676 |
\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
|
677 |
|
28778 | 678 |
\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
|
679 |
|
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
680 |
\end{description} |
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
681 |
|
28774 | 682 |
Here are some further explanations of certain syntax features. |
28773 | 683 |
|
684 |
\begin{itemize} |
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
685 |
|
28778 | 686 |
\item In @{syntax (inner) idts}, note that @{text "x :: nat y"} is |
687 |
parsed as @{text "x :: (nat y)"}, treating @{text y} like a type |
|
688 |
constructor applied to @{text nat}. To avoid this interpretation, |
|
689 |
write @{text "(x :: nat) y"} with explicit parentheses. |
|
28773 | 690 |
|
691 |
\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
|
692 |
(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
|
693 |
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
|
694 |
sequence of identifiers. |
28773 | 695 |
|
696 |
\item Type constraints for terms bind very weakly. For example, |
|
697 |
@{text "x < y :: nat"} is normally parsed as @{text "(x < y) :: |
|
698 |
nat"}, unless @{text "<"} has a very low priority, in which case the |
|
699 |
input is likely to be ambiguous. The correct form is @{text "x < (y |
|
700 |
:: nat)"}. |
|
701 |
||
702 |
\item Constraints may be either written with two literal colons |
|
703 |
``@{verbatim "::"}'' or the double-colon symbol @{verbatim "\<Colon>"}, |
|
28774 | 704 |
which actually looks exactly the same in some {\LaTeX} styles. |
28773 | 705 |
|
28774 | 706 |
\item Dummy variables (written as underscore) may occur in different |
707 |
roles. |
|
28773 | 708 |
|
709 |
\begin{description} |
|
710 |
||
28774 | 711 |
\item A type ``@{text "_"}'' or ``@{text "_ :: sort"}'' acts like an |
712 |
anonymous inference parameter, which is filled-in according to the |
|
713 |
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
|
714 |
|
28774 | 715 |
\item A bound ``@{text "_"}'' refers to a vacuous abstraction, where |
716 |
the body does not refer to the binding introduced here. As in the |
|
717 |
term @{term "\<lambda>x _. x"}, which is @{text "\<alpha>"}-equivalent to @{text |
|
718 |
"\<lambda>x y. x"}. |
|
28773 | 719 |
|
28774 | 720 |
\item A free ``@{text "_"}'' refers to an implicit outer binding. |
721 |
Higher definitional packages usually allow forms like @{text "f x _ |
|
722 |
= x"}. |
|
28773 | 723 |
|
28774 | 724 |
\item A schematic ``@{text "_"}'' (within a term pattern, see |
725 |
\secref{sec:term-decls}) refers to an anonymous variable that is |
|
726 |
implicitly abstracted over its context of locally bound variables. |
|
727 |
For example, this allows pattern matching of @{text "{x. f x = g |
|
728 |
x}"} against @{text "{x. _ = _}"}, or even @{text "{_. _ = _}"} by |
|
729 |
using both bound and schematic dummies. |
|
28773 | 730 |
|
731 |
\end{description} |
|
732 |
||
28774 | 733 |
\item The three literal dots ``@{verbatim "..."}'' may be also |
734 |
written as ellipsis symbol @{verbatim "\<dots>"}. In both cases this |
|
735 |
refers to a special schematic variable, which is bound in the |
|
736 |
context. This special term abbreviation works nicely with |
|
737 |
calculational reasoning (\secref{sec:calculation}). |
|
738 |
||
28773 | 739 |
\end{itemize} |
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
740 |
*} |
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
741 |
|
28777 | 742 |
|
46282 | 743 |
subsection {* Inspecting the syntax *} |
28777 | 744 |
|
46282 | 745 |
text {* |
746 |
\begin{matharray}{rcl} |
|
747 |
@{command_def "print_syntax"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\ |
|
748 |
\end{matharray} |
|
28777 | 749 |
|
46282 | 750 |
\begin{description} |
751 |
||
752 |
\item @{command "print_syntax"} prints the inner syntax of the |
|
753 |
current context. The output can be quite large; the most important |
|
754 |
sections are explained below. |
|
28777 | 755 |
|
46282 | 756 |
\begin{description} |
28777 | 757 |
|
46282 | 758 |
\item @{text "lexicon"} lists the delimiters of the inner token |
759 |
language; see \secref{sec:inner-lex}. |
|
28777 | 760 |
|
46282 | 761 |
\item @{text "prods"} lists the productions of the underlying |
762 |
priority grammar; see \secref{sec:priority-grammar}. |
|
28777 | 763 |
|
46282 | 764 |
The nonterminal @{text "A\<^sup>(\<^sup>p\<^sup>)"} is rendered in plain text as @{text |
765 |
"A[p]"}; delimiters are quoted. Many productions have an extra |
|
766 |
@{text "\<dots> => name"}. These names later become the heads of parse |
|
767 |
trees; they also guide the pretty printer. |
|
28777 | 768 |
|
46282 | 769 |
Productions without such parse tree names are called \emph{copy |
770 |
productions}. Their right-hand side must have exactly one |
|
771 |
nonterminal symbol (or named token). The parser does not create a |
|
772 |
new parse tree node for copy productions, but simply returns the |
|
773 |
parse tree of the right-hand symbol. |
|
774 |
||
775 |
If the right-hand side of a copy production consists of a single |
|
776 |
nonterminal without any delimiters, then it is called a \emph{chain |
|
777 |
production}. Chain productions act as abbreviations: conceptually, |
|
778 |
they are removed from the grammar by adding new productions. |
|
779 |
Priority information attached to chain productions is ignored; only |
|
780 |
the dummy value @{text "-1"} is displayed. |
|
781 |
||
782 |
\item @{text "print modes"} lists the alternative print modes |
|
783 |
provided by this grammar; see \secref{sec:print-modes}. |
|
28777 | 784 |
|
46282 | 785 |
\item @{text "parse_rules"} and @{text "print_rules"} relate to |
786 |
syntax translations (macros); see \secref{sec:syn-trans}. |
|
787 |
||
788 |
\item @{text "parse_ast_translation"} and @{text |
|
789 |
"print_ast_translation"} list sets of constants that invoke |
|
790 |
translation functions for abstract syntax trees, which are only |
|
791 |
required in very special situations; see \secref{sec:tr-funs}. |
|
28777 | 792 |
|
46282 | 793 |
\item @{text "parse_translation"} and @{text "print_translation"} |
794 |
list the sets of constants that invoke regular translation |
|
795 |
functions; see \secref{sec:tr-funs}. |
|
29157 | 796 |
|
46282 | 797 |
\end{description} |
798 |
||
799 |
\end{description} |
|
28777 | 800 |
*} |
28774 | 801 |
|
28770
93a372e2dc7a
added section "The Pure grammar" (incomplete version, based on old ref manual);
wenzelm
parents:
28769
diff
changeset
|
802 |
|
46282 | 803 |
section {* Raw syntax and translations \label{sec:syn-trans} *} |
28762 | 804 |
|
805 |
text {* |
|
806 |
\begin{matharray}{rcl} |
|
41229
d797baa3d57c
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
wenzelm
parents:
40879
diff
changeset
|
807 |
@{command_def "nonterminal"} & : & @{text "theory \<rightarrow> theory"} \\ |
28762 | 808 |
@{command_def "syntax"} & : & @{text "theory \<rightarrow> theory"} \\ |
809 |
@{command_def "no_syntax"} & : & @{text "theory \<rightarrow> theory"} \\ |
|
810 |
@{command_def "translations"} & : & @{text "theory \<rightarrow> theory"} \\ |
|
811 |
@{command_def "no_translations"} & : & @{text "theory \<rightarrow> theory"} \\ |
|
812 |
\end{matharray} |
|
813 |
||
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
814 |
@{rail " |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
815 |
@@{command nonterminal} (@{syntax name} + @'and') |
28762 | 816 |
; |
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
817 |
(@@{command syntax} | @@{command no_syntax}) @{syntax mode}? (@{syntax constdecl} +) |
28762 | 818 |
; |
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
819 |
(@@{command translations} | @@{command no_translations}) |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
820 |
(transpat ('==' | '=>' | '<=' | '\<rightleftharpoons>' | '\<rightharpoonup>' | '\<leftharpoondown>') transpat +) |
28762 | 821 |
; |
822 |
||
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
823 |
mode: ('(' ( @{syntax name} | @'output' | @{syntax name} @'output' ) ')') |
28762 | 824 |
; |
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
825 |
transpat: ('(' @{syntax nameref} ')')? @{syntax string} |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
826 |
"} |
28762 | 827 |
|
828 |
\begin{description} |
|
46282 | 829 |
|
41229
d797baa3d57c
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
wenzelm
parents:
40879
diff
changeset
|
830 |
\item @{command "nonterminal"}~@{text c} declares a type |
28762 | 831 |
constructor @{text c} (without arguments) to act as purely syntactic |
832 |
type: a nonterminal symbol of the inner syntax. |
|
833 |
||
834 |
\item @{command "syntax"}~@{text "(mode) decls"} is similar to |
|
835 |
@{command "consts"}~@{text decls}, except that the actual logical |
|
836 |
signature extension is omitted. Thus the context free grammar of |
|
837 |
Isabelle's inner syntax may be augmented in arbitrary ways, |
|
838 |
independently of the logic. The @{text mode} argument refers to the |
|
839 |
print mode that the grammar rules belong; unless the @{keyword_ref |
|
840 |
"output"} indicator is given, all productions are added both to the |
|
841 |
input and output grammar. |
|
46282 | 842 |
|
28762 | 843 |
\item @{command "no_syntax"}~@{text "(mode) decls"} removes grammar |
844 |
declarations (and translations) resulting from @{text decls}, which |
|
845 |
are interpreted in the same manner as for @{command "syntax"} above. |
|
46282 | 846 |
|
28762 | 847 |
\item @{command "translations"}~@{text rules} specifies syntactic |
848 |
translation rules (i.e.\ macros): parse~/ print rules (@{text "\<rightleftharpoons>"}), |
|
849 |
parse rules (@{text "\<rightharpoonup>"}), or print rules (@{text "\<leftharpoondown>"}). |
|
850 |
Translation patterns may be prefixed by the syntactic category to be |
|
851 |
used for parsing; the default is @{text logic}. |
|
46282 | 852 |
|
28762 | 853 |
\item @{command "no_translations"}~@{text rules} removes syntactic |
854 |
translation rules, which are interpreted in the same manner as for |
|
855 |
@{command "translations"} above. |
|
856 |
||
857 |
\end{description} |
|
858 |
*} |
|
859 |
||
860 |
||
28779
698960f08652
separate section "Inspecting the syntax" for print_syntax;
wenzelm
parents:
28778
diff
changeset
|
861 |
section {* Syntax translation functions \label{sec:tr-funs} *} |
28762 | 862 |
|
863 |
text {* |
|
864 |
\begin{matharray}{rcl} |
|
865 |
@{command_def "parse_ast_translation"} & : & @{text "theory \<rightarrow> theory"} \\ |
|
866 |
@{command_def "parse_translation"} & : & @{text "theory \<rightarrow> theory"} \\ |
|
867 |
@{command_def "print_translation"} & : & @{text "theory \<rightarrow> theory"} \\ |
|
868 |
@{command_def "typed_print_translation"} & : & @{text "theory \<rightarrow> theory"} \\ |
|
869 |
@{command_def "print_ast_translation"} & : & @{text "theory \<rightarrow> theory"} \\ |
|
870 |
\end{matharray} |
|
871 |
||
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
872 |
@{rail " |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
873 |
( @@{command parse_ast_translation} | @@{command parse_translation} | |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
874 |
@@{command print_translation} | @@{command typed_print_translation} | |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
875 |
@@{command print_ast_translation}) ('(' @'advanced' ')')? @{syntax text} |
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
876 |
"} |
28762 | 877 |
|
878 |
Syntax translation functions written in ML admit almost arbitrary |
|
879 |
manipulations of Isabelle's inner syntax. Any of the above commands |
|
42596
6c621a9d612a
modernized rail diagrams using @{rail} antiquotation;
wenzelm
parents:
42358
diff
changeset
|
880 |
have a single @{syntax text} argument that refers to an ML |
28762 | 881 |
expression of appropriate type, which are as follows by default: |
882 |
||
883 |
%FIXME proper antiquotations |
|
884 |
\begin{ttbox} |
|
885 |
val parse_ast_translation : (string * (ast list -> ast)) list |
|
886 |
val parse_translation : (string * (term list -> term)) list |
|
887 |
val print_translation : (string * (term list -> term)) list |
|
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
41229
diff
changeset
|
888 |
val typed_print_translation : (string * (typ -> term list -> term)) list |
28762 | 889 |
val print_ast_translation : (string * (ast list -> ast)) list |
890 |
\end{ttbox} |
|
891 |
||
892 |
If the @{text "(advanced)"} option is given, the corresponding |
|
893 |
translation functions may depend on the current theory or proof |
|
894 |
context. This allows to implement advanced syntax mechanisms, as |
|
895 |
translations functions may refer to specific theory declarations or |
|
896 |
auxiliary proof data. |
|
897 |
||
30397 | 898 |
See also \cite{isabelle-ref} for more information on the general |
899 |
concept of syntax transformations in Isabelle. |
|
28762 | 900 |
|
901 |
%FIXME proper antiquotations |
|
902 |
\begin{ttbox} |
|
903 |
val parse_ast_translation: |
|
904 |
(string * (Proof.context -> ast list -> ast)) list |
|
905 |
val parse_translation: |
|
906 |
(string * (Proof.context -> term list -> term)) list |
|
907 |
val print_translation: |
|
908 |
(string * (Proof.context -> term list -> term)) list |
|
909 |
val typed_print_translation: |
|
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
41229
diff
changeset
|
910 |
(string * (Proof.context -> typ -> term list -> term)) list |
28762 | 911 |
val print_ast_translation: |
912 |
(string * (Proof.context -> ast list -> ast)) list |
|
913 |
\end{ttbox} |
|
914 |
*} |
|
915 |
||
916 |
end |