src/Tools/Code/code_runtime.ML
changeset 70022 49e178cbf923
parent 70011 9dde788b0128
child 70387 35dd9212bf29
equal deleted inserted replaced
70021:e6e634836556 70022:49e178cbf923
    17     -> ((term -> term) -> 'a -> 'a) -> term -> string list -> 'a
    17     -> ((term -> term) -> 'a -> 'a) -> term -> string list -> 'a
    18   val dynamic_value_exn: 'a cookie -> Proof.context -> string option
    18   val dynamic_value_exn: 'a cookie -> Proof.context -> string option
    19     -> ((term -> term) -> 'a -> 'a) -> term -> string list -> 'a Exn.result
    19     -> ((term -> term) -> 'a -> 'a) -> term -> string list -> 'a Exn.result
    20   val dynamic_holds_conv: Proof.context -> conv
    20   val dynamic_holds_conv: Proof.context -> conv
    21   val code_reflect: (string * string list option) list -> string list -> string
    21   val code_reflect: (string * string list option) list -> string list -> string
    22     -> string option -> theory -> theory
    22     -> Path.binding option -> theory -> theory
       
    23   val code_reflect_cmd: (string * string list option) list -> string list -> string
       
    24     -> Path.binding option -> theory -> theory
    23   datatype truth = Holds
    25   datatype truth = Holds
    24   val put_truth: (unit -> truth) -> Proof.context -> Proof.context
    26   val put_truth: (unit -> truth) -> Proof.context -> Proof.context
    25   val mount_computation: Proof.context -> (string * typ) list -> typ
    27   val mount_computation: Proof.context -> (string * typ) list -> typ
    26     -> (term -> 'ml) -> ((term -> term) -> 'ml option -> 'a) -> Proof.context -> term -> 'a
    28     -> (term -> 'ml) -> ((term -> term) -> 'ml option -> 'a) -> Proof.context -> term -> 'a
    27   val mount_computation_conv: Proof.context -> (string * typ) list -> typ
    29   val mount_computation_conv: Proof.context -> (string * typ) list -> typ
   709       |> Code_Target.add_reserved target module_name
   711       |> Code_Target.add_reserved target module_name
   710       |> Context.theory_map (compile_ML true code)
   712       |> Context.theory_map (compile_ML true code)
   711       |> fold (add_eval_tyco o apsnd Code_Printer.str) tyco_map
   713       |> fold (add_eval_tyco o apsnd Code_Printer.str) tyco_map
   712       |> fold (add_eval_constr o apsnd Code_Printer.str) constr_map
   714       |> fold (add_eval_constr o apsnd Code_Printer.str) constr_map
   713       |> fold (add_eval_const o apsnd Code_Printer.str) const_map
   715       |> fold (add_eval_const o apsnd Code_Printer.str) const_map
   714   | process_reflection (code, _) _ (SOME file_name) thy =
   716   | process_reflection (code, _) _ (SOME binding) thy =
   715       let
   717       let
       
   718         val code_binding = Path.map_binding Code_Target.code_path binding;
   716         val preamble =
   719         val preamble =
   717           "(* Generated from " ^
   720           "(* Generated from " ^
   718             Path.implode (Resources.thy_path (Path.basic (Context.theory_name thy))) ^
   721             Path.implode (Resources.thy_path (Path.basic (Context.theory_name thy))) ^
   719           "; DO NOT EDIT! *)";
   722           "; DO NOT EDIT! *)";
   720         val _ = File.write (Path.explode file_name) (preamble ^ "\n\n" ^ code);
   723         val thy' = Code_Target.export code_binding (preamble ^ "\n\n" ^ code) thy;
   721       in
   724         val _ = Code_Target.code_export_message thy';
   722         thy
   725       in thy' end;
   723       end;
   726 
   724 
   727 fun gen_code_reflect prep_type prep_const raw_datatypes raw_functions module_name file_prefix thy =
   725 fun gen_code_reflect prep_type prep_const raw_datatypes raw_functions module_name some_file thy  =
       
   726   let
   728   let
   727     val ctxt = Proof_Context.init_global thy;
   729     val ctxt = Proof_Context.init_global thy;
   728     val datatypes = map (fn (raw_tyco, raw_cos) =>
   730     val datatypes = map (fn (raw_tyco, raw_cos) =>
   729       (prep_type ctxt raw_tyco, (Option.map o map) (prep_const thy) raw_cos)) raw_datatypes;
   731       (prep_type ctxt raw_tyco, (Option.map o map) (prep_const thy) raw_cos)) raw_datatypes;
   730     val (tycos, constrs) = map_split (uncurry (check_datatype thy)) datatypes
   732     val (tycos, constrs) = map_split (uncurry (check_datatype thy)) datatypes
   734     val program = Code_Thingol.consts_program ctxt consts;
   736     val program = Code_Thingol.consts_program ctxt consts;
   735     val result = runtime_code'' ctxt module_name program tycos consts
   737     val result = runtime_code'' ctxt module_name program tycos consts
   736       |> (apsnd o apsnd) (chop (length constrs));
   738       |> (apsnd o apsnd) (chop (length constrs));
   737   in
   739   in
   738     thy
   740     thy
   739     |> process_reflection result module_name some_file
   741     |> process_reflection result module_name file_prefix
   740   end;
   742   end;
   741 
   743 
   742 val code_reflect = gen_code_reflect Code_Target.cert_tyco (K I);
   744 val code_reflect = gen_code_reflect Code_Target.cert_tyco (K I);
   743 val code_reflect_cmd = gen_code_reflect Code_Target.read_tyco Code.read_const;
   745 val code_reflect_cmd = gen_code_reflect Code_Target.read_tyco Code.read_const;
   744 
   746 
   778   Outer_Syntax.command \<^command_keyword>\<open>code_reflect\<close>
   780   Outer_Syntax.command \<^command_keyword>\<open>code_reflect\<close>
   779     "enrich runtime environment with generated code"
   781     "enrich runtime environment with generated code"
   780     (Parse.name -- Scan.optional (\<^keyword>\<open>datatypes\<close> |-- Parse.!!! (parse_datatype
   782     (Parse.name -- Scan.optional (\<^keyword>\<open>datatypes\<close> |-- Parse.!!! (parse_datatype
   781       ::: Scan.repeat (\<^keyword>\<open>and\<close> |-- parse_datatype))) []
   783       ::: Scan.repeat (\<^keyword>\<open>and\<close> |-- parse_datatype))) []
   782     -- Scan.optional (\<^keyword>\<open>functions\<close> |-- Parse.!!!  (Scan.repeat1 Parse.name)) []
   784     -- Scan.optional (\<^keyword>\<open>functions\<close> |-- Parse.!!!  (Scan.repeat1 Parse.name)) []
   783     -- Scan.option (\<^keyword>\<open>file\<close> |-- Parse.!!! Parse.embedded)
   785     -- Scan.option (\<^keyword>\<open>file_prefix\<close> |-- Parse.!!! (Parse.position Parse.embedded))
   784     >> (fn (((module_name, raw_datatypes), raw_functions), some_file) => Toplevel.theory
   786     >> (fn (((module_name, raw_datatypes), raw_functions), file_prefix) =>
   785       (code_reflect_cmd raw_datatypes raw_functions module_name some_file)));
   787       Toplevel.theory (fn thy =>
       
   788         code_reflect_cmd raw_datatypes raw_functions module_name
       
   789           (Option.map Path.explode_binding file_prefix) thy)));
   786 
   790 
   787 end; (*local*)
   791 end; (*local*)
   788 
   792 
   789 
   793 
   790 (** using external SML files as substitute for proper definitions -- only for polyml!  **)
   794 (** using external SML files as substitute for proper definitions -- only for polyml!  **)