4 theory integration imports base begin
6 chapter {* System integration *}
8 section {* Isar toplevel \label{sec:isar-toplevel} *}
10 text {* The Isar toplevel may be considered the centeral hub of the
11 Isabelle/Isar system, where all key components and sub-systems are
12 integrated into a single read-eval-print loop of Isar commands. We
13 shall even incorporate the existing {\ML} toplevel of the compiler
14 and run-time system (cf.\ \secref{sec:ML-toplevel}).
16 Isabelle/Isar departs from the original ``LCF system architecture''
17 where {\ML} was really The Meta Language for defining theories and
18 conducting proofs. Instead, {\ML} now only serves as the
19 implementation language for the system (and user extensions), while
20 the specific Isar toplevel supports the concepts of theory and proof
21 development natively. This includes the graph structure of theories
22 and the block structure of proofs, support for unlimited undo,
23 facilities for tracing, debugging, timing, profiling etc.
25 \medskip The toplevel maintains an implicit state, which is
26 transformed by a sequence of transitions -- either interactively or
27 in batch-mode. In interactive mode, Isar state transitions are
28 encapsulated as safe transactions, such that both failure and undo
29 are handled conveniently without destroying the underlying draft
30 theory (cf.~\secref{sec:context-theory}). In batch mode,
31 transitions operate in a linear (destructive) fashion, such that
32 error conditions abort the present attempt to construct a theory or
35 The toplevel state is a disjoint sum of empty @{text toplevel}, or
36 @{text theory}, or @{text proof}. On entering the main Isar loop we
37 start with an empty toplevel. A theory is commenced by giving a
38 @{text \<THEORY>} header; within a theory we may issue theory
39 commands such as @{text \<DEFINITION>}, or state a @{text
40 \<THEOREM>} to be proven. Now we are within a proof state, with a
41 rich collection of Isar proof commands for structured proof
42 composition, or unstructured proof scripts. When the proof is
43 concluded we get back to the theory, which is then updated by
44 storing the resulting fact. Further theory declarations or theorem
45 statements with proofs may follow, until we eventually conclude the
46 theory development by issuing @{text \<END>}. The resulting theory
47 is then stored within the theory database and we are back to the
50 In addition to these proper state transformations, there are also
51 some diagnostic commands for peeking at the toplevel state without
52 modifying it (e.g.\ \isakeyword{thm}, \isakeyword{term},
53 \isakeyword{print-cases}).
58 @{index_ML_type Toplevel.state} \\
59 @{index_ML Toplevel.UNDEF: "exn"} \\
60 @{index_ML Toplevel.is_toplevel: "Toplevel.state -> bool"} \\
61 @{index_ML Toplevel.theory_of: "Toplevel.state -> theory"} \\
62 @{index_ML Toplevel.proof_of: "Toplevel.state -> Proof.state"} \\
63 @{index_ML Toplevel.debug: "bool ref"} \\
64 @{index_ML Toplevel.timing: "bool ref"} \\
65 @{index_ML Toplevel.profiling: "int ref"} \\
70 \item @{ML_type Toplevel.state} represents Isar toplevel states,
71 which are normally manipulated through the concept of toplevel
72 transitions only (\secref{sec:toplevel-transition}). Also note that
73 a raw toplevel state is subject to the same linearity restrictions
74 as a theory context (cf.~\secref{sec:context-theory}).
76 \item @{ML Toplevel.UNDEF} is raised for undefined toplevel
77 operations. Many operations work only partially for certain cases,
78 since @{ML_type Toplevel.state} is a sum type.
80 \item @{ML Toplevel.is_toplevel}~@{text "state"} checks for an empty
83 \item @{ML Toplevel.theory_of}~@{text "state"} selects the theory of
84 a theory or proof (!), otherwise raises @{ML Toplevel.UNDEF}.
86 \item @{ML Toplevel.proof_of}~@{text "state"} selects the Isar proof
87 state if available, otherwise raises @{ML Toplevel.UNDEF}.
89 \item @{ML "set Toplevel.debug"} makes the toplevel print further
90 details about internal error conditions, exceptions being raised
93 \item @{ML "set Toplevel.timing"} makes the toplevel print timing
94 information for each Isar command being executed.
96 \item @{ML Toplevel.profiling}~@{verbatim ":="}~@{text "n"} controls
97 low-level profiling of the underlying {\ML} runtime system. For
98 Poly/ML, @{text "n = 1"} means time and @{text "n = 2"} space
105 subsection {* Toplevel transitions \label{sec:toplevel-transition} *}
108 An Isar toplevel transition consists of a partial function on the
109 toplevel state, with additional information for diagnostics and
110 error reporting: there are fields for command name, source position,
111 optional source text, as well as flags for interactive-only commands
112 (which issue a warning in batch-mode), printing of result state,
115 The operational part is represented as the sequential union of a
116 list of partial functions, which are tried in turn until the first
117 one succeeds. This acts like an outer case-expression for various
118 alternative state transitions. For example, \isakeyword{qed} acts
119 differently for a local proofs vs.\ the global ending of the main
122 Toplevel transitions are composed via transition transformers.
123 Internally, Isar commands are put together from an empty transition
124 extended by name and source position (and optional source text). It
125 is then left to the individual command parser to turn the given
126 concrete syntax into a suitable transition transformer that adjoin
127 actual operations on a theory or proof state etc.
132 @{index_ML Toplevel.print: "Toplevel.transition -> Toplevel.transition"} \\
133 @{index_ML Toplevel.no_timing: "Toplevel.transition -> Toplevel.transition"} \\
134 @{index_ML Toplevel.keep: "(Toplevel.state -> unit) ->
135 Toplevel.transition -> Toplevel.transition"} \\
136 @{index_ML Toplevel.theory: "(theory -> theory) ->
137 Toplevel.transition -> Toplevel.transition"} \\
138 @{index_ML Toplevel.theory_to_proof: "(theory -> Proof.state) ->
139 Toplevel.transition -> Toplevel.transition"} \\
140 @{index_ML Toplevel.proof: "(Proof.state -> Proof.state) ->
141 Toplevel.transition -> Toplevel.transition"} \\
142 @{index_ML Toplevel.proofs: "(Proof.state -> Proof.state Seq.seq) ->
143 Toplevel.transition -> Toplevel.transition"} \\
144 @{index_ML Toplevel.end_proof: "(bool -> Proof.state -> Proof.context) ->
145 Toplevel.transition -> Toplevel.transition"} \\
150 \item @{ML Toplevel.print}~@{text "tr"} sets the print flag, which
151 causes the toplevel loop to echo the result state (in interactive
154 \item @{ML Toplevel.no_timing}~@{text "tr"} indicates that the
155 transition should never show timing information, e.g.\ because it is
156 a diagnostic command.
158 \item @{ML Toplevel.keep}~@{text "tr"} adjoins a diagnostic
161 \item @{ML Toplevel.theory}~@{text "tr"} adjoins a theory
164 \item @{ML Toplevel.theory_to_proof}~@{text "tr"} adjoins a global
165 goal function, which turns a theory into a proof state. The theory
166 may be changed before entering the proof; the generic Isar goal
167 setup includes an argument that specifies how to apply the proven
168 result to the theory, when the proof is finished.
170 \item @{ML Toplevel.proof}~@{text "tr"} adjoins a deterministic
171 proof command, with a singleton result.
173 \item @{ML Toplevel.proofs}~@{text "tr"} adjoins a general proof
174 command, with zero or more result states (represented as a lazy
177 \item @{ML Toplevel.end_proof}~@{text "tr"} adjoins a concluding
178 proof command, that returns the resulting theory, after storing the
179 resulting facts in the context etc.
185 subsection {* Toplevel control *}
188 There are a few special control commands that modify the behavior
189 the toplevel itself, and only make sense in interactive mode. Under
190 normal circumstances, the user encounters these only implicitly as
191 part of the protocol between the Isabelle/Isar system and a
192 user-interface such as ProofGeneral.
196 \item \isacommand{undo} follows the three-level hierarchy of empty
197 toplevel vs.\ theory vs.\ proof: undo within a proof reverts to the
198 previous proof context, undo after a proof reverts to the theory
199 before the initial goal statement, undo of a theory command reverts
200 to the previous theory value, undo of a theory header discontinues
201 the current theory development and removes it from the theory
202 database (\secref{sec:theory-database}).
204 \item \isacommand{kill} aborts the current level of development:
205 kill in a proof context reverts to the theory before the initial
206 goal statement, kill in a theory context aborts the current theory
207 development, removing it from the database.
209 \item \isacommand{exit} drops out of the Isar toplevel into the
210 underlying {\ML} toplevel (\secref{sec:ML-toplevel}). The Isar
211 toplevel state is preserved and may be continued later.
213 \item \isacommand{quit} terminates the Isabelle/Isar process without
220 section {* ML toplevel \label{sec:ML-toplevel} *}
223 The {\ML} toplevel provides a read-compile-eval-print loop for {\ML}
224 values, types, structures, and functors. {\ML} declarations operate
225 on the global system state, which consists of the compiler
226 environment plus the values of {\ML} reference variables. There is
227 no clean way to undo {\ML} declarations, except for reverting to a
228 previously saved state of the whole Isabelle process. {\ML} input
229 is either read interactively from a TTY, or from a string (usually
230 within a theory text), or from a source file (usually loaded from a
233 Whenever the {\ML} toplevel is active, the current Isabelle theory
234 context is passed as an internal reference variable. Thus {\ML}
235 code may access the theory context during compilation, it may even
236 change the value of a theory being under construction --- while
237 observing the usual linearity restrictions
238 (cf.~\secref{sec:context-theory}).
243 @{index_ML the_context: "unit -> theory"} \\
244 @{index_ML "ML_Context.>> ": "(theory -> theory) -> unit"} \\
249 \item @{ML "the_context ()"} refers to the theory context of the
250 {\ML} toplevel --- at compile time! {\ML} code needs to take care
251 to refer to @{ML "the_context ()"} correctly. Recall that
252 evaluation of a function body is delayed until actual runtime.
253 Moreover, persistent {\ML} toplevel bindings to an unfinished theory
254 should be avoided: code should either project out the desired
255 information immediately, or produce an explicit @{ML_type
256 theory_ref} (cf.\ \secref{sec:context-theory}).
258 \item @{ML "ML_Context.>>"}~@{text f} applies theory transformation
259 @{text f} to the current theory of the {\ML} toplevel. In order to
260 work as expected, the theory should be still under construction, and
261 the Isar language element that invoked the {\ML} compiler in the
262 first place should be ready to accept the changed theory value
263 (e.g.\ \isakeyword{ML-setup}, but not plain \isakeyword{ML}).
264 Otherwise the theory becomes stale!
268 It is very important to note that the above functions are really
269 restricted to the compile time, even though the {\ML} compiler is
270 invoked at runtime! The majority of {\ML} code uses explicit
271 functional arguments of a theory or proof context instead. Thus it
272 may be invoked for an arbitrary context later on, without having to
273 worry about any operational details.
278 @{index_ML Isar.main: "unit -> unit"} \\
279 @{index_ML Isar.loop: "unit -> unit"} \\
280 @{index_ML Isar.state: "unit -> Toplevel.state"} \\
281 @{index_ML Isar.exn: "unit -> (exn * string) option"} \\
282 @{index_ML Isar.context: "unit -> Proof.context"} \\
283 @{index_ML Isar.goal: "unit -> thm list * thm"} \\
288 \item @{ML "Isar.main ()"} invokes the Isar toplevel from {\ML},
289 initializing an empty toplevel state.
291 \item @{ML "Isar.loop ()"} continues the Isar toplevel with the
292 current state, after having dropped out of the Isar toplevel loop.
294 \item @{ML "Isar.state ()"} and @{ML "Isar.exn ()"} get current
295 toplevel state and error condition, respectively. This only works
296 after having dropped out of the Isar toplevel loop.
298 \item @{ML "Isar.context ()"} produces the proof context from @{ML
299 "Isar.state ()"}, analogous to @{ML Context.proof_of}
300 (\secref{sec:generic-context}).
302 \item @{ML "Isar.goal ()"} picks the goal configuration from @{ML
303 "Isar.state ()"}, consisting on the current facts and the goal
304 represented as a theorem according to \secref{sec:tactical-goals}.
310 section {* Theory database \label{sec:theory-database} *}
313 The theory database maintains a collection of theories, together
314 with some administrative information about their original sources,
315 which are held in an external store (i.e.\ some directory within the
316 regular file system).
318 The theory database is organized as a directed acyclic graph;
319 entries are referenced by theory name. Although some additional
320 interfaces allow to include a directory specification as well, this
321 is only a hint to the underlying theory loader. The internal theory
324 Theory @{text A} is associated with the main theory file @{text
325 A}\verb,.thy,, which needs to be accessible through the theory
326 loader path. Any number of additional {\ML} source files may be
327 associated with each theory, by declaring these dependencies in the
328 theory header as @{text \<USES>}, and loading them consecutively
329 within the theory context. The system keeps track of incoming {\ML}
330 sources and associates them with the current theory. The file
331 @{text A}\verb,.ML, is loaded after a theory has been concluded, in
332 order to support legacy proof {\ML} proof scripts.
334 The basic internal actions of the theory database are @{text
335 "update"}, @{text "outdate"}, and @{text "remove"}:
339 \item @{text "update A"} introduces a link of @{text "A"} with a
340 @{text "theory"} value of the same name; it asserts that the theory
341 sources are now consistent with that value;
343 \item @{text "outdate A"} invalidates the link of a theory database
344 entry to its sources, but retains the present theory value;
346 \item @{text "remove A"} deletes entry @{text "A"} from the theory
351 These actions are propagated to sub- or super-graphs of a theory
352 entry as expected, in order to preserve global consistency of the
353 state of all loaded theories with the sources of the external store.
354 This implies certain causalities between actions: @{text "update"}
355 or @{text "outdate"} of an entry will @{text "outdate"} all
356 descendants; @{text "remove"} will @{text "remove"} all descendants.
358 \medskip There are separate user-level interfaces to operate on the
359 theory database directly or indirectly. The primitive actions then
360 just happen automatically while working with the system. In
361 particular, processing a theory header @{text "\<THEORY> A
362 \<IMPORTS> B\<^sub>1 \<dots> B\<^sub>n \<BEGIN>"} ensures that the
363 sub-graph of the collective imports @{text "B\<^sub>1 \<dots> B\<^sub>n"}
364 is up-to-date, too. Earlier theories are reloaded as required, with
365 @{text update} actions proceeding in topological order according to
366 theory dependencies. There may be also a wave of implied @{text
367 outdate} actions for derived theory nodes until a stable situation
368 is achieved eventually.
373 @{index_ML theory: "string -> theory"} \\
374 @{index_ML use_thy: "string -> unit"} \\
375 @{index_ML update_thy: "string -> unit"} \\
376 @{index_ML touch_thy: "string -> unit"} \\
377 @{index_ML remove_thy: "string -> unit"} \\[1ex]
378 @{index_ML ThyInfo.begin_theory}@{verbatim ": ... -> bool -> theory"} \\
379 @{index_ML ThyInfo.end_theory: "theory -> theory"} \\
380 @{index_ML ThyInfo.register_theory: "theory -> unit"} \\[1ex]
381 @{verbatim "datatype action = Update | Outdate | Remove"} \\
382 @{index_ML ThyInfo.add_hook: "(ThyInfo.action -> string -> unit) -> unit"} \\
387 \item @{ML theory}~@{text A} retrieves the theory value presently
388 associated with name @{text A}. Note that the result might be
391 \item @{ML use_thy}~@{text A} loads theory @{text A} if it is absent
392 or out-of-date. It ensures that all parent theories are available
393 as well, but does not reload them if older versions are already
396 \item @{ML update_thy} is similar to @{ML use_thy}, but ensures that
397 theory @{text A} and all ancestors are fully up-to-date.
399 \item @{ML touch_thy}~@{text A} performs and @{text outdate} action
400 on theory @{text A} and all descendants.
402 \item @{ML remove_thy}~@{text A} deletes theory @{text A} and all
403 descendants from the theory database.
405 \item @{ML ThyInfo.begin_theory} is the basic operation behind a
406 @{text \<THEORY>} header declaration. The boolean argument
407 indicates the strictness of treating ancestors: for @{ML true} (as
408 in interactive mode) like @{ML update_thy}, and for @{ML false} (as
409 in batch mode) like @{ML use_thy}. This is {\ML} functions is
410 normally not invoked directly.
412 \item @{ML ThyInfo.end_theory} concludes the loading of a theory
413 proper. An attached theory {\ML} file may be still loaded later on.
414 This is function is normally not invoked directly.
416 \item @{ML ThyInfo.register_theory}~@{text "text thy"} registers an
417 existing theory value with the theory loader database. There is no
418 management of associated sources.
420 \item @{ML "ThyInfo.add_hook"}~@{text f} registers function @{text
421 f} as a hook for theory database actions. The function will be
422 invoked with the action and theory name being involved; thus derived
423 actions may be performed in associated system components, e.g.\
424 maintaining the state of an editor for the theory sources.
426 The kind and order of actions occurring in practice depends both on
427 user interactions and the internal process of resolving theory
428 imports. Hooks should not rely on a particular policy here! Any
429 exceptions raised by the hook are ignored.