src/Tools/Code/code_runtime.ML
changeset 57435 312660c1a70a
parent 56973 62da80041afd
child 58557 fea97f7be494
equal deleted inserted replaced
57434:6ea8b8592787 57435:312660c1a70a
    30   val static_holds_conv: { ctxt: Proof.context, consts: string list } -> Proof.context -> conv
    30   val static_holds_conv: { ctxt: Proof.context, consts: string list } -> Proof.context -> conv
    31   val code_reflect: (string * string list option) list -> string list -> string
    31   val code_reflect: (string * string list option) list -> string list -> string
    32     -> string option -> theory -> theory
    32     -> string option -> theory -> theory
    33   datatype truth = Holds
    33   datatype truth = Holds
    34   val put_truth: (unit -> truth) -> Proof.context -> Proof.context
    34   val put_truth: (unit -> truth) -> Proof.context -> Proof.context
    35   val trace: bool Unsynchronized.ref
    35   val trace: bool Config.T
    36   val polyml_as_definition: (binding * typ) list -> Path.T list -> theory -> theory
    36   val polyml_as_definition: (binding * typ) list -> Path.T list -> theory -> theory
    37 end;
    37 end;
    38 
    38 
    39 structure Code_Runtime : CODE_RUNTIME =
    39 structure Code_Runtime : CODE_RUNTIME =
    40 struct
    40 struct
    63     [(target, SOME (Code_Printer.plain_const_syntax s_Holds))]))
    63     [(target, SOME (Code_Printer.plain_const_syntax s_Holds))]))
    64   #> Code_Target.add_reserved target this
    64   #> Code_Target.add_reserved target this
    65   #> fold (Code_Target.add_reserved target) ["oo", "ooo", "oooo", "upto", "downto", "orf", "andf"]);
    65   #> fold (Code_Target.add_reserved target) ["oo", "ooo", "oooo", "upto", "downto", "orf", "andf"]);
    66        (*avoid further pervasive infix names*)
    66        (*avoid further pervasive infix names*)
    67 
    67 
    68 val trace = Unsynchronized.ref false;
    68 val trace = Attrib.setup_config_bool @{binding "code_runtime_trace"} (K false);
    69 
    69 
    70 fun exec verbose code =
    70 fun exec ctxt verbose code =
    71   (if ! trace then tracing code else ();
    71   (if Config.get ctxt trace then tracing code else ();
    72   ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code));
    72   ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code));
    73 
    73 
    74 fun value ctxt (get, put, put_ml) (prelude, value) =
    74 fun value ctxt (get, put, put_ml) (prelude, value) =
    75   let
    75   let
    76     val code = (prelude
    76     val code = (prelude
    77       ^ "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
    77       ^ "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
    78       ^ " (fn () => " ^ value ^ ")) (ML_Context.the_generic_context ())))");
    78       ^ " (fn () => " ^ value ^ ")) (ML_Context.the_generic_context ())))");
    79     val ctxt' = ctxt
    79     val ctxt' = ctxt
    80       |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
    80       |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
    81       |> Context.proof_map (exec false code);
    81       |> Context.proof_map (exec ctxt false code);
    82   in get ctxt' () end;
    82   in get ctxt' () end;
    83 
    83 
    84 
    84 
    85 (* evaluation into target language values *)
    85 (* evaluation into target language values *)
    86 
    86 
   110     | General.Fail _ => NONE;
   110     | General.Fail _ => NONE;
   111 
   111 
   112 fun dynamic_value_exn cookie ctxt some_target postproc t args =
   112 fun dynamic_value_exn cookie ctxt some_target postproc t args =
   113   let
   113   let
   114     val _ = reject_vars ctxt t;
   114     val _ = reject_vars ctxt t;
   115     val _ = if ! trace
   115     val _ = if Config.get ctxt trace
   116       then tracing ("Evaluation of term " ^ quote (Syntax.string_of_term ctxt t))
   116       then tracing ("Evaluation of term " ^ quote (Syntax.string_of_term ctxt t))
   117       else ()
   117       else ()
   118     fun evaluator program _ vs_ty_t deps =
   118     fun evaluator program _ vs_ty_t deps =
   119       evaluation cookie ctxt (obtain_evaluator ctxt some_target program deps) vs_ty_t args;
   119       evaluation cookie ctxt (obtain_evaluator ctxt some_target program deps) vs_ty_t args;
   120   in Code_Thingol.dynamic_value ctxt (Exn.map_result o postproc) evaluator t end;
   120   in Code_Thingol.dynamic_value ctxt (Exn.map_result o postproc) evaluator t end;
   311   (const, [(target, SOME (Code_Printer.simple_const_syntax (0, (K o K o K) const')))]));
   311   (const, [(target, SOME (Code_Printer.simple_const_syntax (0, (K o K o K) const')))]));
   312 
   312 
   313 fun process_reflection (code, (tyco_map, (constr_map, const_map))) module_name NONE thy =
   313 fun process_reflection (code, (tyco_map, (constr_map, const_map))) module_name NONE thy =
   314       thy
   314       thy
   315       |> Code_Target.add_reserved target module_name
   315       |> Code_Target.add_reserved target module_name
   316       |> Context.theory_map (exec true code)
   316       |> Context.theory_map (exec (Proof_Context.init_global thy (*FIXME*)) true code)
   317       |> fold (add_eval_tyco o apsnd Code_Printer.str) tyco_map
   317       |> fold (add_eval_tyco o apsnd Code_Printer.str) tyco_map
   318       |> fold (add_eval_constr o apsnd Code_Printer.str) constr_map
   318       |> fold (add_eval_constr o apsnd Code_Printer.str) constr_map
   319       |> fold (add_eval_const o apsnd Code_Printer.str) const_map
   319       |> fold (add_eval_const o apsnd Code_Printer.str) const_map
   320   | process_reflection (code, _) _ (SOME file_name) thy =
   320   | process_reflection (code, _) _ (SOME file_name) thy =
   321       let
   321       let