author | wenzelm |
Sun, 01 Jun 2025 17:06:33 +0200 | |
changeset 82680 | f7f8bb1c28ce |
parent 76987 | 4c275405faae |
permissions | -rw-r--r-- |
61656 | 1 |
(*:maxLineLen=78:*) |
57344 | 2 |
|
30124 | 3 |
theory Syntax |
4 |
imports Base |
|
5 |
begin |
|
6 |
||
58618 | 7 |
chapter \<open>Concrete syntax and type-checking\<close> |
30124 | 8 |
|
61854 | 9 |
text \<open> |
10 |
Pure \<open>\<lambda>\<close>-calculus as introduced in \chref{ch:logic} is an adequate |
|
11 |
foundation for logical languages --- in the tradition of \<^emph>\<open>higher-order |
|
12 |
abstract syntax\<close> --- but end-users require additional means for reading and |
|
13 |
printing of terms and types. This important add-on outside the logical core |
|
14 |
is called \<^emph>\<open>inner syntax\<close> in Isabelle jargon, as opposed to the \<^emph>\<open>outer |
|
76987 | 15 |
syntax\<close> of the theory and proof language \<^cite>\<open>"isabelle-isar-ref"\<close>. |
45258 | 16 |
|
76987 | 17 |
For example, according to \<^cite>\<open>church40\<close> quantifiers are represented as |
61854 | 18 |
higher-order constants \<open>All :: ('a \<Rightarrow> bool) \<Rightarrow> bool\<close> such that \<open>All (\<lambda>x::'a. B |
19 |
x)\<close> faithfully represents the idea that is displayed in Isabelle as \<open>\<forall>x::'a. |
|
20 |
B x\<close> via @{keyword "binder"} notation. Moreover, type-inference in the style |
|
76987 | 21 |
of Hindley-Milner \<^cite>\<open>hindleymilner\<close> (and extensions) enables users to |
61854 | 22 |
write \<open>\<forall>x. B x\<close> concisely, when the type \<open>'a\<close> is already clear from the |
23 |
context.\<^footnote>\<open>Type-inference taken to the extreme can easily confuse users. |
|
24 |
Beginners often stumble over unexpectedly general types inferred by the |
|
25 |
system.\<close> |
|
45258 | 26 |
|
61416 | 27 |
\<^medskip> |
61854 | 28 |
The main inner syntax operations are \<^emph>\<open>read\<close> for parsing together with |
29 |
type-checking, and \<^emph>\<open>pretty\<close> for formatted output. See also |
|
30 |
\secref{sec:read-print}. |
|
45258 | 31 |
|
32 |
Furthermore, the input and output syntax layers are sub-divided into |
|
61854 | 33 |
separate phases for \<^emph>\<open>concrete syntax\<close> versus \<^emph>\<open>abstract syntax\<close>, see also |
34 |
\secref{sec:parse-unparse} and \secref{sec:term-check}, respectively. This |
|
35 |
results in the following decomposition of the main operations: |
|
45258 | 36 |
|
61854 | 37 |
\<^item> \<open>read = parse; check\<close> |
45258 | 38 |
|
61854 | 39 |
\<^item> \<open>pretty = uncheck; unparse\<close> |
45258 | 40 |
|
57496 | 41 |
For example, some specification package might thus intercept syntax |
61854 | 42 |
processing at a well-defined stage after \<open>parse\<close>, to a augment the resulting |
43 |
pre-term before full type-reconstruction is performed by \<open>check\<close>. Note that |
|
44 |
the formal status of bound variables, versus free variables, versus |
|
45 |
constants must not be changed between these phases. |
|
57345 | 46 |
|
61416 | 47 |
\<^medskip> |
61854 | 48 |
In general, \<open>check\<close> and \<open>uncheck\<close> operate simultaneously on a list of terms. |
49 |
This is particular important for type-checking, to reconstruct types for |
|
50 |
several terms of the same context and scope. In contrast, \<open>parse\<close> and |
|
51 |
\<open>unparse\<close> operate separately on single terms. |
|
57345 | 52 |
|
53 |
There are analogous operations to read and print types, with the same |
|
54 |
sub-division into phases. |
|
58618 | 55 |
\<close> |
45258 | 56 |
|
30272 | 57 |
|
58618 | 58 |
section \<open>Reading and pretty printing \label{sec:read-print}\<close> |
34924 | 59 |
|
58618 | 60 |
text \<open> |
57496 | 61 |
Read and print operations are roughly dual to each other, such that for the |
61854 | 62 |
user \<open>s' = pretty (read s)\<close> looks similar to the original source text \<open>s\<close>, |
63 |
but the details depend on many side-conditions. There are also explicit |
|
64 |
options to control the removal of type information in the output. The |
|
65 |
default configuration routinely looses information, so \<open>t' = read (pretty |
|
66 |
t)\<close> might fail, or produce a differently typed term, or a completely |
|
67 |
different term in the face of syntactic overloading. |
|
58618 | 68 |
\<close> |
34924 | 69 |
|
58618 | 70 |
text %mlref \<open> |
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
71 |
\begin{mldecls} |
73764 | 72 |
@{define_ML Syntax.read_typs: "Proof.context -> string list -> typ list"} \\ |
73 |
@{define_ML Syntax.read_terms: "Proof.context -> string list -> term list"} \\ |
|
74 |
@{define_ML Syntax.read_props: "Proof.context -> string list -> term list"} \\[0.5ex] |
|
75 |
@{define_ML Syntax.read_typ: "Proof.context -> string -> typ"} \\ |
|
76 |
@{define_ML Syntax.read_term: "Proof.context -> string -> term"} \\ |
|
77 |
@{define_ML Syntax.read_prop: "Proof.context -> string -> term"} \\[0.5ex] |
|
78 |
@{define_ML Syntax.pretty_typ: "Proof.context -> typ -> Pretty.T"} \\ |
|
79 |
@{define_ML Syntax.pretty_term: "Proof.context -> term -> Pretty.T"} \\ |
|
80 |
@{define_ML Syntax.string_of_typ: "Proof.context -> typ -> string"} \\ |
|
81 |
@{define_ML Syntax.string_of_term: "Proof.context -> term -> string"} \\ |
|
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
82 |
\end{mldecls} |
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
83 |
|
69597 | 84 |
\<^descr> \<^ML>\<open>Syntax.read_typs\<close>~\<open>ctxt strs\<close> parses and checks a simultaneous list |
61854 | 85 |
of source strings as types of the logic. |
57345 | 86 |
|
69597 | 87 |
\<^descr> \<^ML>\<open>Syntax.read_terms\<close>~\<open>ctxt strs\<close> parses and checks a simultaneous list |
61854 | 88 |
of source strings as terms of the logic. Type-reconstruction puts all parsed |
89 |
terms into the same scope: types of free variables ultimately need to |
|
90 |
coincide. |
|
57345 | 91 |
|
92 |
If particular type-constraints are required for some of the arguments, the |
|
57346 | 93 |
read operations needs to be split into its parse and check phases. Then it |
69597 | 94 |
is possible to use \<^ML>\<open>Type.constraint\<close> on the intermediate pre-terms |
57846 | 95 |
(\secref{sec:term-check}). |
57345 | 96 |
|
69597 | 97 |
\<^descr> \<^ML>\<open>Syntax.read_props\<close>~\<open>ctxt strs\<close> parses and checks a simultaneous list |
61854 | 98 |
of source strings as terms of the logic, with an implicit type-constraint |
69597 | 99 |
for each argument to enforce type \<^typ>\<open>prop\<close>; this also affects the inner |
100 |
syntax for parsing. The remaining type-reconstruction works as for \<^ML>\<open>Syntax.read_terms\<close>. |
|
57345 | 101 |
|
69597 | 102 |
\<^descr> \<^ML>\<open>Syntax.read_typ\<close>, \<^ML>\<open>Syntax.read_term\<close>, \<^ML>\<open>Syntax.read_prop\<close> are |
61854 | 103 |
like the simultaneous versions, but operate on a single argument only. This |
104 |
convenient shorthand is adequate in situations where a single item in its |
|
69597 | 105 |
own scope is processed. Do not use \<^ML>\<open>map o Syntax.read_term\<close> where \<^ML>\<open>Syntax.read_terms\<close> is actually intended! |
57345 | 106 |
|
69597 | 107 |
\<^descr> \<^ML>\<open>Syntax.pretty_typ\<close>~\<open>ctxt T\<close> and \<^ML>\<open>Syntax.pretty_term\<close>~\<open>ctxt t\<close> |
61854 | 108 |
uncheck and pretty-print the given type or term, respectively. Although the |
109 |
uncheck phase acts on a simultaneous list as well, this is rarely used in |
|
110 |
practice, so only the singleton case is provided as combined pretty |
|
111 |
operation. There is no distinction of term vs.\ proposition. |
|
57345 | 112 |
|
69597 | 113 |
\<^descr> \<^ML>\<open>Syntax.string_of_typ\<close> and \<^ML>\<open>Syntax.string_of_term\<close> are convenient |
114 |
compositions of \<^ML>\<open>Syntax.pretty_typ\<close> and \<^ML>\<open>Syntax.pretty_term\<close> with |
|
115 |
\<^ML>\<open>Pretty.string_of\<close> for output. The result may be concatenated with other |
|
61854 | 116 |
strings, as long as there is no further formatting and line-breaking |
117 |
involved. |
|
57345 | 118 |
|
119 |
||
69597 | 120 |
\<^ML>\<open>Syntax.read_term\<close>, \<^ML>\<open>Syntax.read_prop\<close>, and \<^ML>\<open>Syntax.string_of_term\<close> are the most important operations in practice. |
57345 | 121 |
|
61416 | 122 |
\<^medskip> |
61854 | 123 |
Note that the string values that are passed in and out are annotated by the |
76987 | 124 |
system, to carry further markup that is relevant for the Prover IDE \<^cite>\<open>"isabelle-jedit"\<close>. User code should neither compose its own input strings, |
61854 | 125 |
nor try to analyze the output strings. Conceptually this is an abstract |
126 |
datatype, encoded as concrete string for historical reasons. |
|
57345 | 127 |
|
128 |
The standard way to provide the required position markup for input works via |
|
69597 | 129 |
the outer syntax parser wrapper \<^ML>\<open>Parse.inner_syntax\<close>, which is already |
130 |
part of \<^ML>\<open>Parse.typ\<close>, \<^ML>\<open>Parse.term\<close>, \<^ML>\<open>Parse.prop\<close>. So a string |
|
57345 | 131 |
obtained from one of the latter may be directly passed to the corresponding |
57346 | 132 |
read operation: this yields PIDE markup of the input and precise positions |
133 |
for warning and error messages. |
|
58618 | 134 |
\<close> |
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
135 |
|
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
136 |
|
58618 | 137 |
section \<open>Parsing and unparsing \label{sec:parse-unparse}\<close> |
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
138 |
|
58618 | 139 |
text \<open> |
57496 | 140 |
Parsing and unparsing converts between actual source text and a certain |
61477 | 141 |
\<^emph>\<open>pre-term\<close> format, where all bindings and scopes are already resolved |
57496 | 142 |
faithfully. Thus the names of free variables or constants are determined in |
143 |
the sense of the logical context, but type information might be still |
|
61854 | 144 |
missing. Pre-terms support an explicit language of \<^emph>\<open>type constraints\<close> that |
145 |
may be augmented by user code to guide the later \<^emph>\<open>check\<close> phase. |
|
45258 | 146 |
|
57496 | 147 |
Actual parsing is based on traditional lexical analysis and Earley parsing |
148 |
for arbitrary context-free grammars. The user can specify the grammar |
|
61477 | 149 |
declaratively via mixfix annotations. Moreover, there are \<^emph>\<open>syntax |
150 |
translations\<close> that can be augmented by the user, either declaratively via |
|
57496 | 151 |
@{command translations} or programmatically via @{command |
76987 | 152 |
parse_translation}, @{command print_translation} \<^cite>\<open>"isabelle-isar-ref"\<close>. The final scope-resolution is performed by the system, |
58555 | 153 |
according to name spaces for types, term variables and constants determined |
154 |
by the context. |
|
58618 | 155 |
\<close> |
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
156 |
|
58618 | 157 |
text %mlref \<open> |
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
158 |
\begin{mldecls} |
73764 | 159 |
@{define_ML Syntax.parse_typ: "Proof.context -> string -> typ"} \\ |
160 |
@{define_ML Syntax.parse_term: "Proof.context -> string -> term"} \\ |
|
161 |
@{define_ML Syntax.parse_prop: "Proof.context -> string -> term"} \\[0.5ex] |
|
162 |
@{define_ML Syntax.unparse_typ: "Proof.context -> typ -> Pretty.T"} \\ |
|
163 |
@{define_ML Syntax.unparse_term: "Proof.context -> term -> Pretty.T"} \\ |
|
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
164 |
\end{mldecls} |
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
165 |
|
69597 | 166 |
\<^descr> \<^ML>\<open>Syntax.parse_typ\<close>~\<open>ctxt str\<close> parses a source string as pre-type that |
61854 | 167 |
is ready to be used with subsequent check operations. |
57346 | 168 |
|
69597 | 169 |
\<^descr> \<^ML>\<open>Syntax.parse_term\<close>~\<open>ctxt str\<close> parses a source string as pre-term that |
61854 | 170 |
is ready to be used with subsequent check operations. |
57346 | 171 |
|
69597 | 172 |
\<^descr> \<^ML>\<open>Syntax.parse_prop\<close>~\<open>ctxt str\<close> parses a source string as pre-term that |
61854 | 173 |
is ready to be used with subsequent check operations. The inner syntax |
69597 | 174 |
category is \<^typ>\<open>prop\<close> and a suitable type-constraint is included to ensure |
61854 | 175 |
that this information is observed in subsequent type reconstruction. |
57346 | 176 |
|
69597 | 177 |
\<^descr> \<^ML>\<open>Syntax.unparse_typ\<close>~\<open>ctxt T\<close> unparses a type after uncheck |
61854 | 178 |
operations, to turn it into a pretty tree. |
57346 | 179 |
|
69597 | 180 |
\<^descr> \<^ML>\<open>Syntax.unparse_term\<close>~\<open>ctxt T\<close> unparses a term after uncheck |
61854 | 181 |
operations, to turn it into a pretty tree. There is no distinction for |
182 |
propositions here. |
|
57346 | 183 |
|
184 |
||
69597 | 185 |
These operations always operate on a single item; use the combinator \<^ML>\<open>map\<close> to apply them to a list. |
58618 | 186 |
\<close> |
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
187 |
|
39852 | 188 |
|
58618 | 189 |
section \<open>Checking and unchecking \label{sec:term-check}\<close> |
34924 | 190 |
|
61854 | 191 |
text \<open> |
192 |
These operations define the transition from pre-terms and fully-annotated |
|
193 |
terms in the sense of the logical core (\chref{ch:logic}). |
|
45258 | 194 |
|
61854 | 195 |
The \<^emph>\<open>check\<close> phase is meant to subsume a variety of mechanisms in the manner |
196 |
of ``type-inference'' or ``type-reconstruction'' or ``type-improvement'', |
|
197 |
not just type-checking in the narrow sense. The \<^emph>\<open>uncheck\<close> phase is roughly |
|
198 |
dual, it prunes type-information before pretty printing. |
|
45258 | 199 |
|
200 |
A typical add-on for the check/uncheck syntax layer is the @{command |
|
76987 | 201 |
abbreviation} mechanism \<^cite>\<open>"isabelle-isar-ref"\<close>. Here the user specifies |
61854 | 202 |
syntactic definitions that are managed by the system as polymorphic \<open>let\<close> |
203 |
bindings. These are expanded during the \<open>check\<close> phase, and contracted during |
|
204 |
the \<open>uncheck\<close> phase, without affecting the type-assignment of the given |
|
205 |
terms. |
|
45258 | 206 |
|
61416 | 207 |
\<^medskip> |
61854 | 208 |
The precise meaning of type checking depends on the context --- additional |
209 |
check/uncheck modules might be defined in user space. |
|
45258 | 210 |
|
61854 | 211 |
For example, the @{command class} command defines a context where \<open>check\<close> |
212 |
treats certain type instances of overloaded constants according to the |
|
213 |
``dictionary construction'' of its logical foundation. This involves ``type |
|
214 |
improvement'' (specialization of slightly too general types) and replacement |
|
76987 | 215 |
by certain locale parameters. See also \<^cite>\<open>"Haftmann-Wenzel:2009"\<close>. |
58618 | 216 |
\<close> |
39852 | 217 |
|
58618 | 218 |
text %mlref \<open> |
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
219 |
\begin{mldecls} |
73764 | 220 |
@{define_ML Syntax.check_typs: "Proof.context -> typ list -> typ list"} \\ |
221 |
@{define_ML Syntax.check_terms: "Proof.context -> term list -> term list"} \\ |
|
222 |
@{define_ML Syntax.check_props: "Proof.context -> term list -> term list"} \\[0.5ex] |
|
223 |
@{define_ML Syntax.uncheck_typs: "Proof.context -> typ list -> typ list"} \\ |
|
224 |
@{define_ML Syntax.uncheck_terms: "Proof.context -> term list -> term list"} \\ |
|
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
225 |
\end{mldecls} |
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
226 |
|
69597 | 227 |
\<^descr> \<^ML>\<open>Syntax.check_typs\<close>~\<open>ctxt Ts\<close> checks a simultaneous list of pre-types |
61854 | 228 |
as types of the logic. Typically, this involves normalization of type |
229 |
synonyms. |
|
57346 | 230 |
|
69597 | 231 |
\<^descr> \<^ML>\<open>Syntax.check_terms\<close>~\<open>ctxt ts\<close> checks a simultaneous list of pre-terms |
61854 | 232 |
as terms of the logic. Typically, this involves type-inference and |
233 |
normalization term abbreviations. The types within the given terms are |
|
69597 | 234 |
treated in the same way as for \<^ML>\<open>Syntax.check_typs\<close>. |
57346 | 235 |
|
236 |
Applications sometimes need to check several types and terms together. The |
|
69597 | 237 |
standard approach uses \<^ML>\<open>Logic.mk_type\<close> to embed the language of types |
57346 | 238 |
into that of terms; all arguments are appended into one list of terms that |
69597 | 239 |
is checked; afterwards the type arguments are recovered with \<^ML>\<open>Logic.dest_type\<close>. |
57346 | 240 |
|
69597 | 241 |
\<^descr> \<^ML>\<open>Syntax.check_props\<close>~\<open>ctxt ts\<close> checks a simultaneous list of pre-terms |
242 |
as terms of the logic, such that all terms are constrained by type \<^typ>\<open>prop\<close>. The remaining check operation works as \<^ML>\<open>Syntax.check_terms\<close> |
|
61854 | 243 |
above. |
57346 | 244 |
|
69597 | 245 |
\<^descr> \<^ML>\<open>Syntax.uncheck_typs\<close>~\<open>ctxt Ts\<close> unchecks a simultaneous list of types |
61854 | 246 |
of the logic, in preparation of pretty printing. |
57346 | 247 |
|
69597 | 248 |
\<^descr> \<^ML>\<open>Syntax.uncheck_terms\<close>~\<open>ctxt ts\<close> unchecks a simultaneous list of terms |
61854 | 249 |
of the logic, in preparation of pretty printing. There is no distinction for |
250 |
propositions here. |
|
57346 | 251 |
|
252 |
||
57496 | 253 |
These operations always operate simultaneously on a list; use the combinator |
69597 | 254 |
\<^ML>\<open>singleton\<close> to apply them to a single item. |
58618 | 255 |
\<close> |
39876
1ff9bce085bd
preliminary material on "Concrete syntax and type-checking";
wenzelm
parents:
39865
diff
changeset
|
256 |
|
30124 | 257 |
end |