| 18537 |      1 | %
 | 
|  |      2 | \begin{isabellebody}%
 | 
|  |      3 | \def\isabellecontext{integration}%
 | 
|  |      4 | %
 | 
|  |      5 | \isadelimtheory
 | 
|  |      6 | \isanewline
 | 
|  |      7 | \isanewline
 | 
|  |      8 | \isanewline
 | 
|  |      9 | %
 | 
|  |     10 | \endisadelimtheory
 | 
|  |     11 | %
 | 
|  |     12 | \isatagtheory
 | 
|  |     13 | \isacommand{theory}\isamarkupfalse%
 | 
|  |     14 | \ integration\ \isakeyword{imports}\ base\ \isakeyword{begin}%
 | 
|  |     15 | \endisatagtheory
 | 
|  |     16 | {\isafoldtheory}%
 | 
|  |     17 | %
 | 
|  |     18 | \isadelimtheory
 | 
|  |     19 | %
 | 
|  |     20 | \endisadelimtheory
 | 
|  |     21 | %
 | 
|  |     22 | \isamarkupchapter{System integration%
 | 
|  |     23 | }
 | 
|  |     24 | \isamarkuptrue%
 | 
|  |     25 | %
 | 
|  |     26 | \isamarkupsection{Isar toplevel%
 | 
|  |     27 | }
 | 
|  |     28 | \isamarkuptrue%
 | 
|  |     29 | %
 | 
|  |     30 | \begin{isamarkuptext}%
 | 
|  |     31 | The Isar toplevel may be considered the centeral hub of the
 | 
|  |     32 |   Isabelle/Isar system, where all key components and sub-systems are
 | 
|  |     33 |   integrated into a single read-eval-print loop of Isar commands.
 | 
|  |     34 |   Here we even incorporate the existing {\ML} toplevel of the compiler
 | 
|  |     35 |   and run-time system (cf.\ \secref{sec:ML-toplevel}).
 | 
|  |     36 | 
 | 
|  |     37 |   Isabelle/Isar departs from original ``LCF system architecture''
 | 
|  |     38 |   where {\ML} was really The Meta Language for defining theories and
 | 
|  |     39 |   conducting proofs.  Instead, {\ML} merely serves as the
 | 
|  |     40 |   implementation language for the system (and user extensions), while
 | 
|  |     41 |   our specific Isar toplevel supports particular notions of
 | 
|  |     42 |   incremental theory and proof development more directly.  This
 | 
|  |     43 |   includes the graph structure of theories and the block structure of
 | 
|  |     44 |   proofs, support for unlimited undo, facilities for tracing,
 | 
|  |     45 |   debugging, timing, profiling.
 | 
|  |     46 | 
 | 
|  |     47 |   \medskip The toplevel maintains an implicit state, which is
 | 
|  |     48 |   transformed by a sequence of transitions -- either interactively or
 | 
|  |     49 |   in batch-mode.  In interactive mode, Isar state transitions are
 | 
|  |     50 |   encapsulated as safe transactions, such that both failure and undo
 | 
|  |     51 |   are handled conveniently without destroying the underlying draft
 | 
|  |     52 |   theory (cf.~\secref{sec:context-theory}).  In batch mode,
 | 
|  |     53 |   transitions operate in a strictly linear (destructive) fashion, such
 | 
|  |     54 |   that error conditions abort the present attempt to construct a
 | 
|  |     55 |   theory altogether.
 | 
|  |     56 | 
 | 
|  |     57 |   The toplevel state is a disjoint sum of empty \isa{toplevel}, or
 | 
|  |     58 |   \isa{theory}, or \isa{proof}.  On entering the main Isar loop we
 | 
|  |     59 |   start with an empty toplevel.  A theory is commenced by giving a
 | 
|  |     60 |   \isa{{\isasymTHEORY}} header; within a theory we may issue theory
 | 
| 20027 |     61 |   commands such as \isa{{\isasymDEFINITION}}, or state a \isa{{\isasymTHEOREM}} to be proven.  Now we are within a proof state, with a
 | 
|  |     62 |   rich collection of Isar proof commands for structured proof
 | 
| 18537 |     63 |   composition, or unstructured proof scripts.  When the proof is
 | 
|  |     64 |   concluded we get back to the theory, which is then updated by
 | 
|  |     65 |   storing the resulting fact.  Further theory declarations or theorem
 | 
|  |     66 |   statements with proofs may follow, until we eventually conclude the
 | 
|  |     67 |   theory development by issuing \isa{{\isasymEND}}.  The resulting theory
 | 
|  |     68 |   is then stored within the theory database and we are back to the
 | 
|  |     69 |   empty toplevel.
 | 
|  |     70 | 
 | 
|  |     71 |   In addition to these proper state transformations, there are also
 | 
|  |     72 |   some diagnostic commands for peeking at the toplevel state without
 | 
|  |     73 |   modifying it (e.g.\ \isakeyword{thm}, \isakeyword{term},
 | 
|  |     74 |   \isakeyword{print-cases}).%
 | 
|  |     75 | \end{isamarkuptext}%
 | 
|  |     76 | \isamarkuptrue%
 | 
|  |     77 | %
 | 
|  |     78 | \isadelimmlref
 | 
|  |     79 | %
 | 
|  |     80 | \endisadelimmlref
 | 
|  |     81 | %
 | 
|  |     82 | \isatagmlref
 | 
|  |     83 | %
 | 
|  |     84 | \begin{isamarkuptext}%
 | 
|  |     85 | \begin{mldecls}
 | 
|  |     86 |   \indexmltype{Toplevel.state}\verb|type Toplevel.state| \\
 | 
|  |     87 |   \indexml{Toplevel.UNDEF}\verb|Toplevel.UNDEF: exn| \\
 | 
|  |     88 |   \indexml{Toplevel.is-toplevel}\verb|Toplevel.is_toplevel: Toplevel.state -> bool| \\
 | 
|  |     89 |   \indexml{Toplevel.theory-of}\verb|Toplevel.theory_of: Toplevel.state -> theory| \\
 | 
|  |     90 |   \indexml{Toplevel.proof-of}\verb|Toplevel.proof_of: Toplevel.state -> Proof.state| \\
 | 
|  |     91 |   \indexml{Toplevel.debug}\verb|Toplevel.debug: bool ref| \\
 | 
|  |     92 |   \indexml{Toplevel.timing}\verb|Toplevel.timing: bool ref| \\
 | 
|  |     93 |   \indexml{Toplevel.profiling}\verb|Toplevel.profiling: int ref| \\
 | 
|  |     94 |   \end{mldecls}
 | 
|  |     95 | 
 | 
|  |     96 |   \begin{description}
 | 
|  |     97 | 
 | 
|  |     98 |   \item \verb|Toplevel.state| represents Isar toplevel states,
 | 
|  |     99 |   which are normally only manipulated through the toplevel transition
 | 
|  |    100 |   concept (\secref{sec:toplevel-transition}).  Also note that a
 | 
|  |    101 |   toplevel state is subject to the same linerarity restrictions as a
 | 
|  |    102 |   theory context (cf.~\secref{sec:context-theory}).
 | 
|  |    103 | 
 | 
|  |    104 |   \item \verb|Toplevel.UNDEF| is raised for undefined toplevel
 | 
|  |    105 |   operations: \verb|Toplevel.state| is a sum type, many operations
 | 
|  |    106 |   work only partially for certain cases.
 | 
|  |    107 | 
 | 
|  |    108 |   \item \verb|Toplevel.is_toplevel| checks for an empty toplevel state.
 | 
|  |    109 | 
 | 
|  |    110 |   \item \verb|Toplevel.theory_of| gets the theory of a theory or proof
 | 
|  |    111 |   (!), otherwise raises \verb|Toplevel.UNDEF|.
 | 
|  |    112 | 
 | 
|  |    113 |   \item \verb|Toplevel.proof_of| gets the Isar proof state if
 | 
|  |    114 |   available, otherwise raises \verb|Toplevel.UNDEF|.
 | 
|  |    115 | 
 | 
|  |    116 |   \item \verb|set Toplevel.debug| makes the toplevel print further
 | 
|  |    117 |   details about internal error conditions, exceptions being raised
 | 
|  |    118 |   etc.
 | 
|  |    119 | 
 | 
|  |    120 |   \item \verb|set Toplevel.timing| makes the toplevel print timing
 | 
|  |    121 |   information for each Isar command being executed.
 | 
|  |    122 | 
 | 
|  |    123 |   \item \verb|Toplevel.profiling| controls low-level profiling of the
 | 
|  |    124 |   underlying {\ML} runtime system.\footnote{For Poly/ML, 1 means time
 | 
|  |    125 |   and 2 space profiling.}
 | 
|  |    126 | 
 | 
|  |    127 |   \end{description}%
 | 
|  |    128 | \end{isamarkuptext}%
 | 
|  |    129 | \isamarkuptrue%
 | 
|  |    130 | %
 | 
|  |    131 | \endisatagmlref
 | 
|  |    132 | {\isafoldmlref}%
 | 
|  |    133 | %
 | 
|  |    134 | \isadelimmlref
 | 
|  |    135 | %
 | 
|  |    136 | \endisadelimmlref
 | 
|  |    137 | %
 | 
|  |    138 | \isamarkupsubsection{Toplevel transitions%
 | 
|  |    139 | }
 | 
|  |    140 | \isamarkuptrue%
 | 
|  |    141 | %
 | 
|  |    142 | \begin{isamarkuptext}%
 | 
|  |    143 | An Isar toplevel transition consists of a partial
 | 
|  |    144 |   function on the toplevel state, with additional information for
 | 
|  |    145 |   diagnostics and error reporting: there are fields for command name,
 | 
|  |    146 |   source position, optional source text, as well as flags for
 | 
|  |    147 |   interactive-only commands (which issue a warning in batch-mode),
 | 
|  |    148 |   printing of result state, etc.
 | 
|  |    149 | 
 | 
|  |    150 |   The operational part is represented as a sequential union of a list
 | 
|  |    151 |   of partial functions, which are tried in turn until the first one
 | 
|  |    152 |   succeeds (i.e.\ does not raise \verb|Toplevel.UNDEF|).  For example,
 | 
|  |    153 |   a single Isar command like \isacommand{qed} consists of the union of
 | 
|  |    154 |   some function \verb|Proof.state -> Proof.state| for proofs
 | 
|  |    155 |   within proofs, plus \verb|Proof.state -> theory| for proofs at
 | 
|  |    156 |   the outer theory level.
 | 
|  |    157 | 
 | 
|  |    158 |   Toplevel transitions are composed via transition transformers.
 | 
|  |    159 |   Internally, Isar commands are put together from an empty transition
 | 
|  |    160 |   extended by name and source position (and optional source text).  It
 | 
|  |    161 |   is then left to the individual command parser to turn the given
 | 
|  |    162 |   syntax body into a suitable transition transformer that adjoin
 | 
|  |    163 |   actual operations on a theory or proof state etc.%
 | 
|  |    164 | \end{isamarkuptext}%
 | 
|  |    165 | \isamarkuptrue%
 | 
|  |    166 | %
 | 
|  |    167 | \isadelimmlref
 | 
|  |    168 | %
 | 
|  |    169 | \endisadelimmlref
 | 
|  |    170 | %
 | 
|  |    171 | \isatagmlref
 | 
|  |    172 | %
 | 
|  |    173 | \begin{isamarkuptext}%
 | 
|  |    174 | \begin{mldecls}
 | 
|  |    175 |   \indexml{Toplevel.print}\verb|Toplevel.print: Toplevel.transition -> Toplevel.transition| \\
 | 
|  |    176 |   \indexml{Toplevel.no-timing}\verb|Toplevel.no_timing: Toplevel.transition -> Toplevel.transition| \\
 | 
|  |    177 |   \indexml{Toplevel.keep}\verb|Toplevel.keep: (Toplevel.state -> unit) ->|\isasep\isanewline%
 | 
|  |    178 | \verb|  Toplevel.transition -> Toplevel.transition| \\
 | 
|  |    179 |   \indexml{Toplevel.theory}\verb|Toplevel.theory: (theory -> theory) ->|\isasep\isanewline%
 | 
|  |    180 | \verb|  Toplevel.transition -> Toplevel.transition| \\
 | 
|  |    181 |   \indexml{Toplevel.theory-to-proof}\verb|Toplevel.theory_to_proof: (theory -> Proof.state) ->|\isasep\isanewline%
 | 
|  |    182 | \verb|  Toplevel.transition -> Toplevel.transition| \\
 | 
|  |    183 |   \indexml{Toplevel.proof}\verb|Toplevel.proof: (Proof.state -> Proof.state) ->|\isasep\isanewline%
 | 
|  |    184 | \verb|  Toplevel.transition -> Toplevel.transition| \\
 | 
|  |    185 |   \indexml{Toplevel.proofs}\verb|Toplevel.proofs: (Proof.state -> Proof.state Seq.seq) ->|\isasep\isanewline%
 | 
|  |    186 | \verb|  Toplevel.transition -> Toplevel.transition| \\
 | 
|  |    187 |   \indexml{Toplevel.proof-to-theory}\verb|Toplevel.proof_to_theory: (Proof.state -> theory) ->|\isasep\isanewline%
 | 
|  |    188 | \verb|  Toplevel.transition -> Toplevel.transition| \\
 | 
|  |    189 |   \end{mldecls}
 | 
|  |    190 | 
 | 
|  |    191 |   \begin{description}
 | 
|  |    192 | 
 | 
|  |    193 |   \item \verb|Toplevel.print| sets the print flag, which causes the
 | 
|  |    194 |   resulting state of the transition to be echoed in interactive mode.
 | 
|  |    195 | 
 | 
|  |    196 |   \item \verb|Toplevel.no_timing| indicates that the transition should
 | 
|  |    197 |   never show timing information, e.g.\ because it is merely a
 | 
|  |    198 |   diagnostic command.
 | 
|  |    199 | 
 | 
|  |    200 |   \item \verb|Toplevel.keep| adjoins a diagnostic function.
 | 
|  |    201 | 
 | 
|  |    202 |   \item \verb|Toplevel.theory| adjoins a theory transformer.
 | 
|  |    203 | 
 | 
|  |    204 |   \item \verb|Toplevel.theory_to_proof| adjoins a global goal function,
 | 
| 18645 |    205 |   which turns a theory into a proof state.  The theory may be changed
 | 
|  |    206 |   before entering the proof; the generic Isar goal setup includes an
 | 
|  |    207 |   argument that specifies how to apply the proven result to the
 | 
|  |    208 |   theory, when the proof is finished.
 | 
| 18558 |    209 | 
 | 
| 18537 |    210 |   \item \verb|Toplevel.proof| adjoins a deterministic proof command,
 | 
|  |    211 |   with a singleton result state.
 | 
|  |    212 | 
 | 
|  |    213 |   \item \verb|Toplevel.proofs| adjoins a general proof command, with
 | 
|  |    214 |   zero or more result states (represented as a lazy list).
 | 
|  |    215 | 
 | 
|  |    216 |   \item \verb|Toplevel.proof_to_theory| adjoins a concluding proof
 | 
|  |    217 |   command, that returns the resulting theory, after storing the
 | 
|  |    218 |   resulting facts etc.
 | 
|  |    219 | 
 | 
|  |    220 |   \end{description}%
 | 
|  |    221 | \end{isamarkuptext}%
 | 
|  |    222 | \isamarkuptrue%
 | 
|  |    223 | %
 | 
|  |    224 | \endisatagmlref
 | 
|  |    225 | {\isafoldmlref}%
 | 
|  |    226 | %
 | 
|  |    227 | \isadelimmlref
 | 
|  |    228 | %
 | 
|  |    229 | \endisadelimmlref
 | 
|  |    230 | %
 | 
|  |    231 | \isamarkupsubsection{Toplevel control%
 | 
|  |    232 | }
 | 
|  |    233 | \isamarkuptrue%
 | 
|  |    234 | %
 | 
|  |    235 | \begin{isamarkuptext}%
 | 
|  |    236 | Apart from regular toplevel transactions there are a few
 | 
|  |    237 |   special control commands that modify the behavior the toplevel
 | 
|  |    238 |   itself, and only make sense in interactive mode.  Under normal
 | 
|  |    239 |   circumstances, the user encounters these only implicitly as part of
 | 
|  |    240 |   the protocol between the Isabelle/Isar system and a user-interface
 | 
|  |    241 |   such as ProofGeneral.
 | 
|  |    242 | 
 | 
|  |    243 |   \begin{description}
 | 
|  |    244 | 
 | 
|  |    245 |   \item \isacommand{undo} follows the three-level hierarchy of empty
 | 
|  |    246 |   toplevel vs.\ theory vs.\ proof: undo within a proof reverts to the
 | 
|  |    247 |   previous proof context, undo after a proof reverts to the theory
 | 
|  |    248 |   before the initial goal statement, undo of a theory command reverts
 | 
|  |    249 |   to the previous theory value, undo of a theory header discontinues
 | 
|  |    250 |   the current theory development and removes it from the theory
 | 
|  |    251 |   database (\secref{sec:theory-database}).
 | 
|  |    252 | 
 | 
|  |    253 |   \item \isacommand{kill} aborts the current level of development:
 | 
|  |    254 |   kill in a proof context reverts to the theory before the initial
 | 
|  |    255 |   goal statement, kill in a theory context aborts the current theory
 | 
|  |    256 |   development, removing it from the database.
 | 
|  |    257 | 
 | 
|  |    258 |   \item \isacommand{exit} drops out of the Isar toplevel into the
 | 
|  |    259 |   underlying {\ML} toplevel (\secref{sec:ML-toplevel}).  The Isar
 | 
|  |    260 |   toplevel state is preserved and may be continued later.
 | 
|  |    261 | 
 | 
|  |    262 |   \item \isacommand{quit} terminates the Isabelle/Isar process without
 | 
|  |    263 |   saving.
 | 
|  |    264 | 
 | 
|  |    265 |   \end{description}%
 | 
|  |    266 | \end{isamarkuptext}%
 | 
|  |    267 | \isamarkuptrue%
 | 
|  |    268 | %
 | 
|  |    269 | \isamarkupsection{ML toplevel \label{sec:ML-toplevel}%
 | 
|  |    270 | }
 | 
|  |    271 | \isamarkuptrue%
 | 
|  |    272 | %
 | 
|  |    273 | \begin{isamarkuptext}%
 | 
|  |    274 | The {\ML} toplevel provides a read-compile-eval-print loop for
 | 
|  |    275 |   {\ML} values, types, structures, and functors.  {\ML} declarations
 | 
|  |    276 |   operate on the global system state, which consists of the compiler
 | 
|  |    277 |   environment plus the values of {\ML} reference variables.  There is
 | 
|  |    278 |   no clean way to undo {\ML} declarations, except for reverting to a
 | 
|  |    279 |   previously saved state of the whole Isabelle process.  {\ML} input
 | 
|  |    280 |   is either read interactively from a TTY, or from a string (usually
 | 
|  |    281 |   within a theory text), or from a source file (usually associated
 | 
|  |    282 |   with a theory).
 | 
|  |    283 | 
 | 
|  |    284 |   Whenever the {\ML} toplevel is active, the current Isabelle theory
 | 
|  |    285 |   context is passed as an internal reference variable.  Thus {\ML}
 | 
|  |    286 |   code may access the theory context during compilation, it may even
 | 
|  |    287 |   change the value of a theory being under construction --- following
 | 
|  |    288 |   the usual linearity restrictions (cf.~\secref{sec:context-theory}).%
 | 
|  |    289 | \end{isamarkuptext}%
 | 
|  |    290 | \isamarkuptrue%
 | 
|  |    291 | %
 | 
|  |    292 | \isadelimmlref
 | 
|  |    293 | %
 | 
|  |    294 | \endisadelimmlref
 | 
|  |    295 | %
 | 
|  |    296 | \isatagmlref
 | 
|  |    297 | %
 | 
|  |    298 | \begin{isamarkuptext}%
 | 
|  |    299 | \begin{mldecls}
 | 
|  |    300 |   \indexml{context}\verb|context: theory -> unit| \\
 | 
|  |    301 |   \indexml{the-context}\verb|the_context: unit -> theory| \\
 | 
|  |    302 |   \indexml{Context.$>$$>$ }\verb|Context.>> : (theory -> theory) -> unit| \\
 | 
|  |    303 |   \end{mldecls}
 | 
|  |    304 | 
 | 
|  |    305 |   \begin{description}
 | 
|  |    306 | 
 | 
|  |    307 |   \item \verb|context|~\isa{thy} sets the {\ML} theory context to
 | 
|  |    308 |   \isa{thy}.  This is usually performed automatically by the system,
 | 
|  |    309 |   when dropping out of the interactive Isar toplevel into {\ML}, or
 | 
|  |    310 |   when Isar invokes {\ML} to process code from a string or a file.
 | 
|  |    311 | 
 | 
|  |    312 |   \item \verb|the_context ()| refers to the theory context of the
 | 
|  |    313 |   {\ML} toplevel --- at compile time!  {\ML} code needs to take care
 | 
|  |    314 |   to refer to \verb|the_context ()| correctly, recall that evaluation
 | 
|  |    315 |   of a function body is delayed until actual runtime.  Moreover,
 | 
|  |    316 |   persistent {\ML} toplevel bindings to an unfinished theory should be
 | 
|  |    317 |   avoided: code should either project out the desired information
 | 
|  |    318 |   immediately, or produce an explicit \verb|theory_ref| (cf.\
 | 
|  |    319 |   \secref{sec:context-theory}).
 | 
|  |    320 | 
 | 
|  |    321 |   \item \verb|Context.>>|~\isa{f} applies theory transformation
 | 
|  |    322 |   \isa{f} to the current theory of the {\ML} toplevel.  In order to
 | 
|  |    323 |   work as expected, the theory should be still under construction, and
 | 
|  |    324 |   the Isar language element that invoked the {\ML} compiler in the
 | 
|  |    325 |   first place shoule be ready to accept the changed theory value
 | 
|  |    326 |   (e.g.\ \isakeyword{ML-setup}, but not plain \isakeyword{ML}).
 | 
|  |    327 |   Otherwise the theory may get destroyed!
 | 
|  |    328 | 
 | 
|  |    329 |   \end{description}
 | 
|  |    330 | 
 | 
|  |    331 |   It is very important to note that the above functions are really
 | 
|  |    332 |   restricted to the compile time, even though the {\ML} compiler is
 | 
|  |    333 |   invoked at runtime!  The majority of {\ML} code uses explicit
 | 
|  |    334 |   functional arguments of a theory or proof context, as required.
 | 
|  |    335 |   Thus it may get run in an arbitrary context later on.
 | 
|  |    336 | 
 | 
|  |    337 |   \bigskip
 | 
|  |    338 | 
 | 
|  |    339 |   \begin{mldecls}
 | 
|  |    340 |   \indexml{Isar.main}\verb|Isar.main: unit -> unit| \\
 | 
|  |    341 |   \indexml{Isar.loop}\verb|Isar.loop: unit -> unit| \\
 | 
|  |    342 |   \indexml{Isar.state}\verb|Isar.state: unit -> Toplevel.state| \\
 | 
| 20027 |    343 |   \indexml{Isar.context}\verb|Isar.context: unit -> Proof.context| \\
 | 
| 18537 |    344 |   \indexml{Isar.exn}\verb|Isar.exn: unit -> (exn * string) option| \\
 | 
|  |    345 |   \end{mldecls}
 | 
|  |    346 | 
 | 
|  |    347 |   \begin{description}
 | 
|  |    348 | 
 | 
|  |    349 |   \item \verb|Isar.main ()| invokes the Isar toplevel from {\ML},
 | 
|  |    350 |   initializing the state to empty toplevel state.
 | 
|  |    351 | 
 | 
|  |    352 |   \item \verb|Isar.loop ()| continues the Isar toplevel with the
 | 
|  |    353 |   current state, after dropping out of the Isar toplevel loop.
 | 
|  |    354 | 
 | 
|  |    355 |   \item \verb|Isar.state ()| and \verb|Isar.exn ()| get current
 | 
|  |    356 |   toplevel state and optional error condition, respectively.  This
 | 
|  |    357 |   only works after dropping out of the Isar toplevel loop.
 | 
|  |    358 | 
 | 
| 20064 |    359 |   \item \verb|Isar.context ()| produces the proof context from \verb|Isar.state ()|.
 | 
| 20027 |    360 | 
 | 
| 18537 |    361 |   \end{description}%
 | 
|  |    362 | \end{isamarkuptext}%
 | 
|  |    363 | \isamarkuptrue%
 | 
|  |    364 | %
 | 
|  |    365 | \endisatagmlref
 | 
|  |    366 | {\isafoldmlref}%
 | 
|  |    367 | %
 | 
|  |    368 | \isadelimmlref
 | 
|  |    369 | %
 | 
|  |    370 | \endisadelimmlref
 | 
|  |    371 | %
 | 
|  |    372 | \isamarkupsection{Theory database%
 | 
|  |    373 | }
 | 
|  |    374 | \isamarkuptrue%
 | 
|  |    375 | %
 | 
|  |    376 | \begin{isamarkuptext}%
 | 
|  |    377 | The theory database maintains a collection of theories,
 | 
|  |    378 |   together with some administrative information about the original
 | 
|  |    379 |   sources, which are held in an external store (i.e.\ a collection of
 | 
|  |    380 |   directories within the regular file system of the underlying
 | 
|  |    381 |   platform).
 | 
|  |    382 | 
 | 
|  |    383 |   The theory database is organized as a directed acyclic graph, with
 | 
|  |    384 |   entries referenced by theory name.  Although some external
 | 
|  |    385 |   interfaces allow to include a directory specification, this is only
 | 
|  |    386 |   a hint to the underlying theory loader mechanism: the internal
 | 
|  |    387 |   theory name space is flat.
 | 
|  |    388 | 
 | 
|  |    389 |   Theory \isa{A} is associated with the main theory file \isa{A}\verb,.thy,, which needs to be accessible through the theory
 | 
|  |    390 |   loader path.  A number of optional {\ML} source files may be
 | 
|  |    391 |   associated with each theory, by declaring these dependencies in the
 | 
|  |    392 |   theory header as \isa{{\isasymUSES}}, and loading them consecutively
 | 
|  |    393 |   within the theory context.  The system keeps track of incoming {\ML}
 | 
|  |    394 |   sources and associates them with the current theory.  The special
 | 
|  |    395 |   theory {\ML} file \isa{A}\verb,.ML, is loaded after a theory has
 | 
|  |    396 |   been concluded, in order to support legacy proof {\ML} proof
 | 
|  |    397 |   scripts.
 | 
|  |    398 | 
 | 
| 18554 |    399 |   The basic internal actions of the theory database are \isa{update}, \isa{outdate}, and \isa{remove}:
 | 
| 18537 |    400 | 
 | 
|  |    401 |   \begin{itemize}
 | 
|  |    402 | 
 | 
|  |    403 |   \item \isa{update\ A} introduces a link of \isa{A} with a
 | 
|  |    404 |   \isa{theory} value of the same name; it asserts that the theory
 | 
|  |    405 |   sources are consistent with that value.
 | 
|  |    406 | 
 | 
|  |    407 |   \item \isa{outdate\ A} invalidates the link of a theory database
 | 
|  |    408 |   entry to its sources, but retains the present theory value.
 | 
|  |    409 | 
 | 
|  |    410 |   \item \isa{remove\ A} removes entry \isa{A} from the theory
 | 
|  |    411 |   database.
 | 
|  |    412 |   
 | 
|  |    413 |   \end{itemize}
 | 
|  |    414 | 
 | 
|  |    415 |   These actions are propagated to sub- or super-graphs of a theory
 | 
|  |    416 |   entry in the usual way, in order to preserve global consistency of
 | 
|  |    417 |   the state of all loaded theories with the sources of the external
 | 
|  |    418 |   store.  This implies causal dependencies of certain actions: \isa{update} or \isa{outdate} of an entry will \isa{outdate}
 | 
|  |    419 |   all descendants; \isa{remove} will \isa{remove} all
 | 
|  |    420 |   descendants.
 | 
|  |    421 | 
 | 
|  |    422 |   \medskip There are separate user-level interfaces to operate on the
 | 
|  |    423 |   theory database directly or indirectly.  The primitive actions then
 | 
|  |    424 |   just happen automatically while working with the system.  In
 | 
|  |    425 |   particular, processing a theory header \isa{{\isasymTHEORY}\ A\ {\isasymIMPORTS}\ B\isactrlsub {\isadigit{1}}\ {\isasymdots}\ B\isactrlsub n\ {\isasymBEGIN}} ensure that the
 | 
|  |    426 |   sub-graph of the collective imports \isa{B\isactrlsub {\isadigit{1}}\ {\isasymdots}\ B\isactrlsub n}
 | 
|  |    427 |   is up-to-date.  Earlier theories are reloaded as required, with
 | 
|  |    428 |   \isa{update} actions proceeding in topological order according to
 | 
|  |    429 |   theory dependencies.  There may be also a wave of implied \isa{outdate} actions for derived theory nodes until a stable situation
 | 
|  |    430 |   is achieved eventually.%
 | 
|  |    431 | \end{isamarkuptext}%
 | 
|  |    432 | \isamarkuptrue%
 | 
|  |    433 | %
 | 
|  |    434 | \isadelimmlref
 | 
|  |    435 | %
 | 
|  |    436 | \endisadelimmlref
 | 
|  |    437 | %
 | 
|  |    438 | \isatagmlref
 | 
|  |    439 | %
 | 
|  |    440 | \begin{isamarkuptext}%
 | 
|  |    441 | \begin{mldecls}
 | 
|  |    442 |   \indexml{theory}\verb|theory: string -> theory| \\
 | 
|  |    443 |   \indexml{use-thy}\verb|use_thy: string -> unit| \\
 | 
|  |    444 |   \indexml{update-thy}\verb|update_thy: string -> unit| \\
 | 
|  |    445 |   \indexml{use-thy-only}\verb|use_thy_only: string -> unit| \\
 | 
|  |    446 |   \indexml{update-thy-only}\verb|update_thy_only: string -> unit| \\
 | 
|  |    447 |   \indexml{touch-thy}\verb|touch_thy: string -> unit| \\
 | 
|  |    448 |   \indexml{remove-thy}\verb|remove_thy: string -> unit| \\[1ex]
 | 
|  |    449 |   \indexml{ThyInfo.begin-theory}\verb|ThyInfo.begin_theory|\verb|: ... -> bool -> theory| \\
 | 
|  |    450 |   \indexml{ThyInfo.end-theory}\verb|ThyInfo.end_theory: theory -> theory| \\
 | 
|  |    451 |   \indexml{ThyInfo.register-theory}\verb|ThyInfo.register_theory: theory -> unit| \\[1ex]
 | 
|  |    452 |   \verb|datatype action = Update |\verb,|,\verb| Outdate |\verb,|,\verb| Remove| \\
 | 
|  |    453 |   \indexml{ThyInfo.add-hook}\verb|ThyInfo.add_hook: (ThyInfo.action -> string -> unit) -> unit| \\
 | 
|  |    454 |   \end{mldecls}
 | 
|  |    455 | 
 | 
|  |    456 |   \begin{description}
 | 
|  |    457 | 
 | 
|  |    458 |   \item \verb|theory|~\isa{A} retrieves the theory value presently
 | 
|  |    459 |   associated with \isa{A}.  The result is not necessarily
 | 
|  |    460 |   up-to-date!
 | 
|  |    461 | 
 | 
|  |    462 |   \item \verb|use_thy|~\isa{A} loads theory \isa{A} if it is absent
 | 
|  |    463 |   or out-of-date.  It ensures that all parent theories are available
 | 
|  |    464 |   as well, but does not reload them if older versions are already
 | 
|  |    465 |   present.
 | 
|  |    466 | 
 | 
|  |    467 |   \item \verb|update_thy| is similar to \verb|use_thy|, but ensures that
 | 
|  |    468 |   the \isa{A} and all of its ancestors are fully up-to-date.
 | 
|  |    469 | 
 | 
|  |    470 |   \item \verb|use_thy_only|~\isa{A} is like \verb|use_thy|~\isa{A},
 | 
|  |    471 |   but refrains from loading the attached \isa{A}\verb,.ML, file.
 | 
|  |    472 |   This is occasionally useful in replaying legacy {\ML} proof scripts
 | 
|  |    473 |   by hand.
 | 
|  |    474 |   
 | 
|  |    475 |   \item \verb|update_thy_only| is analogous to \verb|use_thy_only|, but
 | 
|  |    476 |   proceeds like \verb|update_thy| for ancestors.
 | 
|  |    477 | 
 | 
|  |    478 |   \item \verb|touch_thy|~\isa{A} performs \isa{outdate} action on
 | 
|  |    479 |   theory \isa{A} and all of its descendants.
 | 
|  |    480 | 
 | 
|  |    481 |   \item \verb|remove_thy|~\isa{A} removes \isa{A} and all of its
 | 
|  |    482 |   descendants from the theory database.
 | 
|  |    483 | 
 | 
|  |    484 |   \item \verb|ThyInfo.begin_theory| is the basic operation behind a
 | 
|  |    485 |   \isa{{\isasymTHEORY}} header declaration.  The boolean argument
 | 
|  |    486 |   indicates the strictness of treating ancestors: for \verb|true| (as
 | 
|  |    487 |   in interactive mode) like \verb|update_thy|, and for \verb|false| (as
 | 
|  |    488 |   in batch mode) like \verb|use_thy|.  This is {\ML} functions is
 | 
|  |    489 |   normally not invoked directly.
 | 
|  |    490 | 
 | 
|  |    491 |   \item \verb|ThyInfo.end_theory| concludes the loading of a theory
 | 
|  |    492 |   proper; an attached theory {\ML} file may be still loaded later on.
 | 
|  |    493 |   This is {\ML} functions is normally not invoked directly.
 | 
|  |    494 | 
 | 
|  |    495 |   \item \verb|ThyInfo.register_theory|~{text thy} registers an existing
 | 
|  |    496 |   theory value with the theory loader database.  There is no
 | 
|  |    497 |   management of associated sources; this is mainly for bootstrapping.
 | 
|  |    498 | 
 | 
|  |    499 |   \item \verb|ThyInfo.add_hook|~\isa{f} registers function \isa{f} as a hook for theory database actions.  The function will be
 | 
|  |    500 |   invoked with the action and theory name being involved; thus derived
 | 
|  |    501 |   actions may be performed in associated system components, e.g.\
 | 
|  |    502 |   maintaining the state of an editor for theory sources.
 | 
|  |    503 | 
 | 
|  |    504 |   The kind and order of actions occurring in practice depends both on
 | 
|  |    505 |   user interactions and the internal process of resolving theory
 | 
|  |    506 |   imports.  Hooks should not rely on a particular policy here!  Any
 | 
|  |    507 |   exceptions raised by the hook are ignored by the theory database.
 | 
|  |    508 | 
 | 
|  |    509 |   \end{description}%
 | 
|  |    510 | \end{isamarkuptext}%
 | 
|  |    511 | \isamarkuptrue%
 | 
|  |    512 | %
 | 
|  |    513 | \endisatagmlref
 | 
|  |    514 | {\isafoldmlref}%
 | 
|  |    515 | %
 | 
|  |    516 | \isadelimmlref
 | 
|  |    517 | %
 | 
|  |    518 | \endisadelimmlref
 | 
|  |    519 | %
 | 
|  |    520 | \isadelimtheory
 | 
|  |    521 | %
 | 
|  |    522 | \endisadelimtheory
 | 
|  |    523 | %
 | 
|  |    524 | \isatagtheory
 | 
|  |    525 | \isacommand{end}\isamarkupfalse%
 | 
|  |    526 | %
 | 
|  |    527 | \endisatagtheory
 | 
|  |    528 | {\isafoldtheory}%
 | 
|  |    529 | %
 | 
|  |    530 | \isadelimtheory
 | 
|  |    531 | %
 | 
|  |    532 | \endisadelimtheory
 | 
|  |    533 | \isanewline
 | 
|  |    534 | \end{isabellebody}%
 | 
|  |    535 | %%% Local Variables:
 | 
|  |    536 | %%% mode: latex
 | 
|  |    537 | %%% TeX-master: "root"
 | 
|  |    538 | %%% End:
 |