src/Pure/ML/ml_context.ML
changeset 62889 99c7f31615c2
parent 62878 1cec457e0a03
child 62900 c641bf9402fd
equal deleted inserted replaced
62888:64f44d7279e5 62889:99c7f31615c2
    34 
    34 
    35 fun thm name = Proof_Context.get_thm (Context.the_local_context ()) name;
    35 fun thm name = Proof_Context.get_thm (Context.the_local_context ()) name;
    36 fun thms name = Proof_Context.get_thms (Context.the_local_context ()) name;
    36 fun thms name = Proof_Context.get_thms (Context.the_local_context ()) name;
    37 
    37 
    38 fun exec (e: unit -> unit) context =
    38 fun exec (e: unit -> unit) context =
    39   (case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of
    39   (case Context.setmp_generic_context (SOME context)
       
    40       (fn () => (e (); Context.get_generic_context ())) () of
    40     SOME context' => context'
    41     SOME context' => context'
    41   | NONE => error "Missing context after execution");
    42   | NONE => error "Missing context after execution");
    42 
    43 
    43 
    44 
    44 
    45 
   168 fun eval flags pos ants =
   169 fun eval flags pos ants =
   169   let
   170   let
   170     val non_verbose = ML_Compiler.verbose false flags;
   171     val non_verbose = ML_Compiler.verbose false flags;
   171 
   172 
   172     (*prepare source text*)
   173     (*prepare source text*)
   173     val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ());
   174     val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.get_generic_context ());
   174     val _ =
   175     val _ =
   175       (case env_ctxt of
   176       (case env_ctxt of
   176         SOME ctxt =>
   177         SOME ctxt =>
   177           if Config.get ctxt ML_Options.source_trace andalso Context_Position.is_visible ctxt
   178           if Config.get ctxt ML_Options.source_trace andalso Context_Position.is_visible ctxt
   178           then tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
   179           then tracing (cat_lines [ML_Lex.flatten env, ML_Lex.flatten body])
   179           else ()
   180           else ()
   180       | NONE => ());
   181       | NONE => ());
   181 
   182 
   182     (*prepare environment*)
   183     (*prepare environment*)
   183     val _ =
   184     val _ =
   184       Context.setmp_thread_data
   185       Context.setmp_generic_context
   185         (Option.map (Context.Proof o Context_Position.set_visible false) env_ctxt)
   186         (Option.map (Context.Proof o Context_Position.set_visible false) env_ctxt)
   186         (fn () => (ML_Compiler.eval non_verbose Position.none env; Context.thread_data ())) ()
   187         (fn () =>
       
   188           (ML_Compiler.eval non_verbose Position.none env; Context.get_generic_context ())) ()
   187       |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
   189       |> (fn NONE => () | SOME context' => Context.>> (ML_Env.inherit context'));
   188 
   190 
   189     (*eval body*)
   191     (*eval body*)
   190     val _ = ML_Compiler.eval flags pos body;
   192     val _ = ML_Compiler.eval flags pos body;
   191 
   193 
   192     (*clear environment*)
   194     (*clear environment*)
   193     val _ =
   195     val _ =
   194       (case (env_ctxt, is_some (Context.thread_data ())) of
   196       (case (env_ctxt, is_some (Context.get_generic_context ())) of
   195         (SOME ctxt, true) =>
   197         (SOME ctxt, true) =>
   196           let
   198           let
   197             val name = struct_name ctxt;
   199             val name = struct_name ctxt;
   198             val _ = ML_Compiler.eval non_verbose Position.none (reset_env name);
   200             val _ = ML_Compiler.eval non_verbose Position.none (reset_env name);
   199             val _ = Context.>> (ML_Env.forget_structure name);
   201             val _ = Context.>> (ML_Env.forget_structure name);
   212 
   214 
   213 fun eval_source flags source =
   215 fun eval_source flags source =
   214   eval flags (Input.pos_of source) (ML_Lex.read_source (#SML_syntax flags) source);
   216   eval flags (Input.pos_of source) (ML_Lex.read_source (#SML_syntax flags) source);
   215 
   217 
   216 fun eval_in ctxt flags pos ants =
   218 fun eval_in ctxt flags pos ants =
   217   Context.setmp_thread_data (Option.map Context.Proof ctxt)
   219   Context.setmp_generic_context (Option.map Context.Proof ctxt)
   218     (fn () => eval flags pos ants) ();
   220     (fn () => eval flags pos ants) ();
   219 
   221 
   220 fun eval_source_in ctxt flags source =
   222 fun eval_source_in ctxt flags source =
   221   Context.setmp_thread_data (Option.map Context.Proof ctxt)
   223   Context.setmp_generic_context (Option.map Context.Proof ctxt)
   222     (fn () => eval_source flags source) ();
   224     (fn () => eval_source flags source) ();
   223 
   225 
   224 fun expression range name constraint body ants =
   226 fun expression range name constraint body ants =
   225   exec (fn () =>
   227   exec (fn () =>
   226     eval ML_Compiler.flags (#1 range)
   228     eval ML_Compiler.flags (#1 range)
   227      (ML_Lex.read "Context.set_thread_data (SOME (let val " @ ML_Lex.read_set_range range name @
   229      (ML_Lex.read "Context.put_generic_context (SOME (let val " @ ML_Lex.read_set_range range name @
   228       ML_Lex.read (": " ^ constraint ^ " =") @ ants @
   230       ML_Lex.read (": " ^ constraint ^ " =") @ ants @
   229       ML_Lex.read ("in " ^ body ^ " end (Context.the_generic_context ())));")));
   231       ML_Lex.read ("in " ^ body ^ " end (Context.the_generic_context ())));")));
   230 
   232 
   231 end;
   233 end;
   232 
   234