doc-src/IsarImplementation/Thy/Integration.thy
changeset 40099 0fb7891f0c7c
parent 39882 ab0afd03a042
child 40149 4c35be108990
equal deleted inserted replaced
40098:9dbb01456031 40099:0fb7891f0c7c
     6 
     6 
     7 section {* Isar toplevel \label{sec:isar-toplevel} *}
     7 section {* Isar toplevel \label{sec:isar-toplevel} *}
     8 
     8 
     9 text {* The Isar toplevel may be considered the centeral hub of the
     9 text {* The Isar toplevel may be considered the centeral hub of the
    10   Isabelle/Isar system, where all key components and sub-systems are
    10   Isabelle/Isar system, where all key components and sub-systems are
    11   integrated into a single read-eval-print loop of Isar commands.  We
    11   integrated into a single read-eval-print loop of Isar commands,
    12   shall even incorporate the existing {\ML} toplevel of the compiler
    12   which also incorporates the underlying ML compiler.
    13   and run-time system (cf.\ \secref{sec:ML-toplevel}).
       
    14 
    13 
    15   Isabelle/Isar departs from the original ``LCF system architecture''
    14   Isabelle/Isar departs from the original ``LCF system architecture''
    16   where {\ML} was really The Meta Language for defining theories and
    15   where ML was really The Meta Language for defining theories and
    17   conducting proofs.  Instead, {\ML} now only serves as the
    16   conducting proofs.  Instead, ML now only serves as the
    18   implementation language for the system (and user extensions), while
    17   implementation language for the system (and user extensions), while
    19   the specific Isar toplevel supports the concepts of theory and proof
    18   the specific Isar toplevel supports the concepts of theory and proof
    20   development natively.  This includes the graph structure of theories
    19   development natively.  This includes the graph structure of theories
    21   and the block structure of proofs, support for unlimited undo,
    20   and the block structure of proofs, support for unlimited undo,
    22   facilities for tracing, debugging, timing, profiling etc.
    21   facilities for tracing, debugging, timing, profiling etc.
    64   @{index_ML Toplevel.profiling: "int Unsynchronized.ref"} \\
    63   @{index_ML Toplevel.profiling: "int Unsynchronized.ref"} \\
    65   \end{mldecls}
    64   \end{mldecls}
    66 
    65 
    67   \begin{description}
    66   \begin{description}
    68 
    67 
    69   \item @{ML_type Toplevel.state} represents Isar toplevel states,
    68   \item Type @{ML_type Toplevel.state} represents Isar toplevel
    70   which are normally manipulated through the concept of toplevel
    69   states, which are normally manipulated through the concept of
    71   transitions only (\secref{sec:toplevel-transition}).  Also note that
    70   toplevel transitions only (\secref{sec:toplevel-transition}).  Also
    72   a raw toplevel state is subject to the same linearity restrictions
    71   note that a raw toplevel state is subject to the same linearity
    73   as a theory context (cf.~\secref{sec:context-theory}).
    72   restrictions as a theory context (cf.~\secref{sec:context-theory}).
    74 
    73 
    75   \item @{ML Toplevel.UNDEF} is raised for undefined toplevel
    74   \item @{ML Toplevel.UNDEF} is raised for undefined toplevel
    76   operations.  Many operations work only partially for certain cases,
    75   operations.  Many operations work only partially for certain cases,
    77   since @{ML_type Toplevel.state} is a sum type.
    76   since @{ML_type Toplevel.state} is a sum type.
    78 
    77 
    92 
    91 
    93   \item @{ML "Toplevel.timing := true"} makes the toplevel print timing
    92   \item @{ML "Toplevel.timing := true"} makes the toplevel print timing
    94   information for each Isar command being executed.
    93   information for each Isar command being executed.
    95 
    94 
    96   \item @{ML Toplevel.profiling}~@{verbatim ":="}~@{text "n"} controls
    95   \item @{ML Toplevel.profiling}~@{verbatim ":="}~@{text "n"} controls
    97   low-level profiling of the underlying {\ML} runtime system.  For
    96   low-level profiling of the underlying ML runtime system.  For
    98   Poly/ML, @{text "n = 1"} means time and @{text "n = 2"} space
    97   Poly/ML, @{text "n = 1"} means time and @{text "n = 2"} space
    99   profiling.
    98   profiling.
       
    99 
       
   100   \end{description}
       
   101 *}
       
   102 
       
   103 text %mlantiq {*
       
   104   \begin{matharray}{rcl}
       
   105   @{ML_antiquotation_def "Isar.state"} & : & @{text ML_antiquotation} \\
       
   106   \end{matharray}
       
   107 
       
   108   \begin{description}
       
   109 
       
   110   \item @{text "@{Isar.state}"} refers to Isar toplevel state at that
       
   111   point --- as abstract value.
       
   112 
       
   113   This only works for diagnostic ML commands, such as @{command
       
   114   ML_val} or @{command ML_command}.
   100 
   115 
   101   \end{description}
   116   \end{description}
   102 *}
   117 *}
   103 
   118 
   104 
   119 
   180 
   195 
   181   \end{description}
   196   \end{description}
   182 *}
   197 *}
   183 
   198 
   184 
   199 
   185 subsection {* Toplevel control *}
       
   186 
       
   187 text {*
       
   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 Proof~General.
       
   193 
       
   194   \begin{description}
       
   195 
       
   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}).
       
   203 
       
   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.
       
   208 
       
   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.
       
   212 
       
   213   \item \isacommand{quit} terminates the Isabelle/Isar process without
       
   214   saving.
       
   215 
       
   216   \end{description}
       
   217 *}
       
   218 
       
   219 
       
   220 section {* ML toplevel \label{sec:ML-toplevel} *}
       
   221 
       
   222 text {*
       
   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
       
   231   theory).
       
   232 
       
   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}).
       
   239 *}
       
   240 
       
   241 text %mlref {*
       
   242   \begin{mldecls}
       
   243   @{index_ML ML_Context.the_generic_context: "unit -> Context.generic"} \\
       
   244   @{index_ML "Context.>> ": "(Context.generic -> Context.generic) -> unit"} \\
       
   245   \end{mldecls}
       
   246 
       
   247   \begin{description}
       
   248 
       
   249   \item @{ML "ML_Context.the_generic_context ()"} refers to the theory
       
   250   context of the {\ML} toplevel --- at compile time!  {\ML} code needs
       
   251   to take care to refer to @{ML "ML_Context.the_generic_context ()"}
       
   252   correctly.  Recall that evaluation of a function body is delayed
       
   253   until actual runtime.  Moreover, persistent {\ML} toplevel bindings
       
   254   to an unfinished theory should be avoided: code should either
       
   255   project out the desired information immediately, or produce an
       
   256   explicit @{ML_type theory_ref} (cf.\ \secref{sec:context-theory}).
       
   257 
       
   258   \item @{ML "Context.>>"}~@{text f} applies context transformation
       
   259   @{text f} to the implicit context of the {\ML} toplevel.
       
   260 
       
   261   \end{description}
       
   262 
       
   263   It is very important to note that the above functions are really
       
   264   restricted to the compile time, even though the {\ML} compiler is
       
   265   invoked at runtime!  The majority of {\ML} code uses explicit
       
   266   functional arguments of a theory or proof context instead.  Thus it
       
   267   may be invoked for an arbitrary context later on, without having to
       
   268   worry about any operational details.
       
   269 
       
   270   \bigskip
       
   271 
       
   272   \begin{mldecls}
       
   273   @{index_ML Isar.main: "unit -> unit"} \\
       
   274   @{index_ML Isar.loop: "unit -> unit"} \\
       
   275   @{index_ML Isar.state: "unit -> Toplevel.state"} \\
       
   276   @{index_ML Isar.exn: "unit -> (exn * string) option"} \\
       
   277   @{index_ML Isar.goal: "unit ->
       
   278   {context: Proof.context, facts: thm list, goal: thm}"} \\
       
   279   \end{mldecls}
       
   280 
       
   281   \begin{description}
       
   282 
       
   283   \item @{ML "Isar.main ()"} invokes the Isar toplevel from {\ML},
       
   284   initializing an empty toplevel state.
       
   285 
       
   286   \item @{ML "Isar.loop ()"} continues the Isar toplevel with the
       
   287   current state, after having dropped out of the Isar toplevel loop.
       
   288 
       
   289   \item @{ML "Isar.state ()"} and @{ML "Isar.exn ()"} get current
       
   290   toplevel state and error condition, respectively.  This only works
       
   291   after having dropped out of the Isar toplevel loop.
       
   292 
       
   293   \item @{ML "Isar.goal ()"} produces the full Isar goal state,
       
   294   consisting of proof context, facts that have been indicated for
       
   295   immediate use, and the tactical goal according to
       
   296   \secref{sec:tactical-goals}.
       
   297 
       
   298   \end{description}
       
   299 *}
       
   300 
       
   301 
       
   302 section {* Theory database \label{sec:theory-database} *}
   200 section {* Theory database \label{sec:theory-database} *}
   303 
   201 
   304 text {*
   202 text {*
   305   The theory database maintains a collection of theories, together
   203   The theory database maintains a collection of theories, together
   306   with some administrative information about their original sources,
   204   with some administrative information about their original sources,
   313   is only a hint to the underlying theory loader.  The internal theory
   211   is only a hint to the underlying theory loader.  The internal theory
   314   name space is flat!
   212   name space is flat!
   315 
   213 
   316   Theory @{text A} is associated with the main theory file @{text
   214   Theory @{text A} is associated with the main theory file @{text
   317   A}\verb,.thy,, which needs to be accessible through the theory
   215   A}\verb,.thy,, which needs to be accessible through the theory
   318   loader path.  Any number of additional {\ML} source files may be
   216   loader path.  Any number of additional ML source files may be
   319   associated with each theory, by declaring these dependencies in the
   217   associated with each theory, by declaring these dependencies in the
   320   theory header as @{text \<USES>}, and loading them consecutively
   218   theory header as @{text \<USES>}, and loading them consecutively
   321   within the theory context.  The system keeps track of incoming {\ML}
   219   within the theory context.  The system keeps track of incoming ML
   322   sources and associates them with the current theory.
   220   sources and associates them with the current theory.
   323 
   221 
   324   The basic internal actions of the theory database are @{text
   222   The basic internal actions of the theory database are @{text
   325   "update"} and @{text "remove"}:
   223   "update"} and @{text "remove"}:
   326 
   224