added command 'ML_export';
authorwenzelm
Fri May 25 22:47:57 2018 +0200 (18 months ago)
changeset 68276cbee43ff4ceb
parent 68275 b5d0318757f0
child 68277 c2b227b8e361
added command 'ML_export';
NEWS
src/Doc/Isar_Ref/Spec.thy
src/Pure/ML/ml_env.ML
src/Pure/Pure.thy
     1.1 --- a/NEWS	Fri May 25 22:47:36 2018 +0200
     1.2 +++ b/NEWS	Fri May 25 22:47:57 2018 +0200
     1.3 @@ -358,6 +358,11 @@
     1.4  * Operation Export.export emits theory exports (arbitrary blobs), which
     1.5  are stored persistently in the session build database.
     1.6  
     1.7 +* Command 'ML_export' exports ML toplevel bindings to the global
     1.8 +bootstrap environment of the ML process. This allows ML evaluation
     1.9 +without a formal theory context, e.g. in command-line tools like
    1.10 +"isabelle process".
    1.11 +
    1.12  
    1.13  *** System ***
    1.14  
     2.1 --- a/src/Doc/Isar_Ref/Spec.thy	Fri May 25 22:47:36 2018 +0200
     2.2 +++ b/src/Doc/Isar_Ref/Spec.thy	Fri May 25 22:47:57 2018 +0200
     2.3 @@ -1058,6 +1058,7 @@
     2.4      @{command_def "ML_file_debug"} & : & \<open>local_theory \<rightarrow> local_theory\<close> \\
     2.5      @{command_def "ML_file_no_debug"} & : & \<open>local_theory \<rightarrow> local_theory\<close> \\
     2.6      @{command_def "ML"} & : & \<open>local_theory \<rightarrow> local_theory\<close> \\
     2.7 +    @{command_def "ML_export"} & : & \<open>local_theory \<rightarrow> local_theory\<close> \\
     2.8      @{command_def "ML_prf"} & : & \<open>proof \<rightarrow> proof\<close> \\
     2.9      @{command_def "ML_val"} & : & \<open>any \<rightarrow>\<close> \\
    2.10      @{command_def "ML_command"} & : & \<open>any \<rightarrow>\<close> \\
    2.11 @@ -1081,8 +1082,9 @@
    2.12        @@{command ML_file_debug} |
    2.13        @@{command ML_file_no_debug}) @{syntax name} ';'?
    2.14      ;
    2.15 -    (@@{command ML} | @@{command ML_prf} | @@{command ML_val} |
    2.16 -      @@{command ML_command} | @@{command setup} | @@{command local_setup}) @{syntax text}
    2.17 +    (@@{command ML} | @@{command ML_export} | @@{command ML_prf} |
    2.18 +      @@{command ML_val} | @@{command ML_command} | @@{command setup} |
    2.19 +      @@{command local_setup}) @{syntax text}
    2.20      ;
    2.21      @@{command attribute_setup} @{syntax name} '=' @{syntax text} @{syntax text}?
    2.22    \<close>}
    2.23 @@ -1103,10 +1105,16 @@
    2.24    \<^theory_text>\<open>ML_file_no_debug\<close> change the @{attribute ML_debugger} option locally while
    2.25    the given file is compiled.
    2.26  
    2.27 -  \<^descr> \<^theory_text>\<open>ML text\<close> is similar to \<^theory_text>\<open>ML_file\<close>, but evaluates directly the given
    2.28 -  \<open>text\<close>. Top-level ML bindings are stored within the (global or local) theory
    2.29 +  \<^descr> \<^theory_text>\<open>ML\<close> is similar to \<^theory_text>\<open>ML_file\<close>, but evaluates directly the given \<open>text\<close>.
    2.30 +  Top-level ML bindings are stored within the (global or local) theory
    2.31    context.
    2.32  
    2.33 +  \<^descr> \<^theory_text>\<open>ML_export\<close> is similar to \<^theory_text>\<open>ML\<close>, but the resulting toplevel bindings are
    2.34 +  exported to the global bootstrap environment of the ML process --- it has
    2.35 +  has a lasting effect that cannot be retracted. This allows ML evaluation
    2.36 +  without a formal theory context, e.g. for command-line tools via @{tool
    2.37 +  process} @{cite "isabelle-system"}.
    2.38 +
    2.39    \<^descr> \<^theory_text>\<open>ML_prf\<close> is analogous to \<^theory_text>\<open>ML\<close> but works within a proof context.
    2.40    Top-level ML bindings are stored within the proof context in a purely
    2.41    sequential fashion, disregarding the nested proof structure. ML bindings
     3.1 --- a/src/Pure/ML/ml_env.ML	Fri May 25 22:47:36 2018 +0200
     3.2 +++ b/src/Pure/ML/ml_env.ML	Fri May 25 22:47:57 2018 +0200
     3.3 @@ -13,6 +13,8 @@
     3.4    val add_breakpoints: (serial * (bool Unsynchronized.ref * Thread_Position.T)) list -> unit
     3.5    val init_bootstrap: Context.generic -> Context.generic
     3.6    val SML_environment: bool Config.T
     3.7 +  val set_bootstrap: bool -> Context.generic -> Context.generic
     3.8 +  val restore_bootstrap: Context.generic -> Context.generic -> Context.generic
     3.9    val add_name_space: {SML: bool} -> ML_Name_Space.T -> Context.generic -> Context.generic
    3.10    val make_name_space: {SML: bool, exchange: bool} -> ML_Name_Space.T
    3.11    val context: ML_Compiler0.context
    3.12 @@ -130,6 +132,12 @@
    3.13              in (val2, type1, fixity1, structure2, signature2, functor1) end);
    3.14      in make_data (bootstrap, tables, sml_tables', breakpoints) end);
    3.15  
    3.16 +fun set_bootstrap bootstrap =
    3.17 +  Env.map (fn {tables, sml_tables, breakpoints, ...} =>
    3.18 +    make_data (bootstrap, tables, sml_tables, breakpoints));
    3.19 +
    3.20 +val restore_bootstrap = set_bootstrap o #bootstrap o Env.get;
    3.21 +
    3.22  fun add_name_space {SML} (space: ML_Name_Space.T) =
    3.23    Env.map (fn {bootstrap, tables, sml_tables, breakpoints} =>
    3.24      let
     4.1 --- a/src/Pure/Pure.thy	Fri May 25 22:47:36 2018 +0200
     4.2 +++ b/src/Pure/Pure.thy	Fri May 25 22:47:57 2018 +0200
     4.3 @@ -23,7 +23,7 @@
     4.4    and "external_file" "bibtex_file" :: thy_load
     4.5    and "ML_file" "ML_file_debug" "ML_file_no_debug" :: thy_load % "ML"
     4.6    and "SML_file" "SML_file_debug" "SML_file_no_debug" :: thy_load % "ML"
     4.7 -  and "SML_import" "SML_export" :: thy_decl % "ML"
     4.8 +  and "SML_import" "SML_export" "ML_export" :: thy_decl % "ML"
     4.9    and "ML_prf" :: prf_decl % "proof"  (* FIXME % "ML" ?? *)
    4.10    and "ML_val" "ML_command" :: diag % "ML"
    4.11    and "simproc_setup" :: thy_decl % "ML"
    4.12 @@ -191,6 +191,18 @@
    4.13        end));
    4.14  
    4.15  val _ =
    4.16 +  Outer_Syntax.command ("ML_export", \<^here>)
    4.17 +    "ML text within theory or local theory, and export to bootstrap environment"
    4.18 +    (Parse.ML_source >> (fn source =>
    4.19 +      Toplevel.generic_theory (fn context =>
    4.20 +        context
    4.21 +        |> ML_Env.set_bootstrap true
    4.22 +        |> ML_Context.exec (fn () =>
    4.23 +            ML_Context.eval_source (ML_Compiler.verbose true ML_Compiler.flags) source)
    4.24 +        |> ML_Env.restore_bootstrap context
    4.25 +        |> Local_Theory.propagate_ml_env)));
    4.26 +
    4.27 +val _ =
    4.28    Outer_Syntax.command \<^command_keyword>\<open>ML_prf\<close> "ML text within proof"
    4.29      (Parse.ML_source >> (fn source =>
    4.30        Toplevel.proof (Proof.map_context (Context.proof_map