src/Tools/Code/code_runtime.ML
author haftmann
Thu Jan 26 16:06:19 2017 +0100 (2017-01-26)
changeset 64957 3faa9b31ff78
parent 64956 de7ce0fad5bc
child 64958 85b87da722ab
permissions -rw-r--r--
tuned structure and terminology
     1 (*  Title:      Tools/Code/code_runtime.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Runtime services building on code generation into implementation language SML.
     5 *)
     6 
     7 signature CODE_RUNTIME =
     8 sig
     9   val target: string
    10   val value: Proof.context ->
    11     (Proof.context -> unit -> 'a) * ((unit -> 'a) -> Proof.context -> Proof.context) * string ->
    12     string * string -> 'a
    13   type 'a cookie = (Proof.context -> unit -> 'a) * ((unit -> 'a) -> Proof.context -> Proof.context) * string
    14   val dynamic_value: 'a cookie -> Proof.context -> string option
    15     -> ((term -> term) -> 'a -> 'a) -> term -> string list -> 'a option
    16   val dynamic_value_strict: 'a cookie -> Proof.context -> string option
    17     -> ((term -> term) -> 'a -> 'a) -> term -> string list -> 'a
    18   val dynamic_value_exn: 'a cookie -> Proof.context -> string option
    19     -> ((term -> term) -> 'a -> 'a) -> term -> string list -> 'a Exn.result
    20   val static_value: 'a cookie -> { ctxt: Proof.context, target: string option,
    21     lift_postproc: (term -> term) -> 'a -> 'a, consts: string list }
    22     -> Proof.context -> term -> 'a option
    23   val static_value_strict: 'a cookie -> { ctxt: Proof.context, target: string option,
    24     lift_postproc: (term -> term) -> 'a -> 'a, consts: string list }
    25     -> Proof.context -> term -> 'a
    26   val static_value_exn: 'a cookie -> { ctxt: Proof.context, target: string option,
    27     lift_postproc: (term -> term) -> 'a -> 'a, consts: string list }
    28     -> Proof.context -> term -> 'a Exn.result
    29   val dynamic_holds_conv: Proof.context -> conv
    30   val static_holds_conv: { ctxt: Proof.context, consts: string list } -> Proof.context -> conv
    31   val experimental_computation: (term -> 'a) cookie
    32     -> { ctxt: Proof.context, lift_postproc: (term -> term) -> 'a -> 'a,
    33            terms: term list, T: typ }
    34     -> Proof.context -> term -> 'a (*EXPERIMENTAL!*)
    35   val code_reflect: (string * string list option) list -> string list -> string
    36     -> string option -> theory -> theory
    37   datatype truth = Holds
    38   val put_truth: (unit -> truth) -> Proof.context -> Proof.context
    39   val trace: bool Config.T
    40   val polyml_as_definition: (binding * typ) list -> Path.T list -> theory -> theory
    41 end;
    42 
    43 structure Code_Runtime : CODE_RUNTIME =
    44 struct
    45 
    46 open Basic_Code_Symbol;
    47 
    48 (** ML compiler as evaluation environment **)
    49 
    50 (* technical prerequisites *)
    51 
    52 val this = "Code_Runtime";
    53 val s_truth = Long_Name.append this "truth";
    54 val s_Holds = Long_Name.append this "Holds";
    55 
    56 val target = "Eval";
    57 
    58 datatype truth = Holds;
    59 
    60 val _ = Theory.setup
    61   (Code_Target.add_derived_target (target, [(Code_ML.target_SML, I)])
    62   #> Code_Target.set_printings (Type_Constructor (@{type_name prop},
    63     [(target, SOME (0, (K o K o K) (Code_Printer.str s_truth)))]))
    64   #> Code_Target.set_printings (Constant (@{const_name Code_Generator.holds},
    65     [(target, SOME (Code_Printer.plain_const_syntax s_Holds))]))
    66   #> Code_Target.add_reserved target this
    67   #> fold (Code_Target.add_reserved target) ["oo", "ooo", "oooo", "upto", "downto", "orf", "andf"]);
    68        (*avoid further pervasive infix names*)
    69 
    70 val trace = Attrib.setup_config_bool @{binding "code_runtime_trace"} (K false);
    71 
    72 fun compile_ML verbose code context =
    73  (if Config.get_generic context trace then tracing code else ();
    74   Code_Preproc.timed "compiling ML" Context.proof_of
    75     (ML_Context.exec (fn () => ML_Compiler0.ML ML_Env.context
    76     {line = 0, file = "generated code", verbose = verbose,
    77        debug = false} code)) context);
    78 
    79 fun value ctxt (get, put, put_ml) (prelude, value) =
    80   let
    81     val code =
    82       prelude ^ "\nval _ = Context.put_generic_context (SOME (Context.map_proof (" ^
    83       put_ml ^ " (fn () => " ^ value ^ ")) (Context.the_generic_context ())))";
    84     val ctxt' = ctxt
    85       |> put (fn () => error ("Bad compilation for " ^ quote put_ml))
    86       |> Context.proof_map (compile_ML false code);
    87     val computator = get ctxt';
    88   in Code_Preproc.timed_exec "running ML" computator ctxt' end;
    89 
    90 
    91 (* evaluation into ML language values *)
    92 
    93 type 'a cookie = (Proof.context -> unit -> 'a) * ((unit -> 'a) -> Proof.context -> Proof.context) * string;
    94 
    95 fun reject_vars ctxt t =
    96   ((Sign.no_frees ctxt o Sign.no_vars ctxt o map_types (K dummyT)) t; t);
    97 
    98 fun build_compilation_text ctxt some_target program consts =
    99   Code_Target.compilation_text ctxt (the_default target some_target) program consts false
   100   #>> (fn ml_modules => space_implode "\n\n" (map snd ml_modules));
   101 
   102 fun run_compilation_text cookie ctxt comp vs_t args =
   103   let
   104     val (program_code, value_name) = comp vs_t;
   105     val value_code = space_implode " "
   106       (value_name :: "()" :: map (enclose "(" ")") args);
   107   in Exn.interruptible_capture (value ctxt cookie) (program_code, value_code) end;
   108 
   109 fun partiality_as_none e = SOME (Exn.release e)
   110   handle General.Match => NONE
   111     | General.Bind => NONE
   112     | General.Fail _ => NONE;
   113 
   114 fun dynamic_value_exn cookie ctxt some_target postproc t args =
   115   let
   116     val _ = reject_vars ctxt t;
   117     val _ = if Config.get ctxt trace
   118       then tracing ("Evaluation of term " ^ quote (Syntax.string_of_term ctxt t))
   119       else ()
   120     fun comp program _ vs_ty_t deps =
   121       run_compilation_text cookie ctxt (build_compilation_text ctxt some_target program deps) vs_ty_t args;
   122   in Code_Thingol.dynamic_value ctxt (Exn.map_res o postproc) comp t end;
   123 
   124 fun dynamic_value_strict cookie ctxt some_target postproc t args =
   125   Exn.release (dynamic_value_exn cookie ctxt some_target postproc t args);
   126 
   127 fun dynamic_value cookie ctxt some_target postproc t args =
   128   partiality_as_none (dynamic_value_exn cookie ctxt some_target postproc t args);
   129 
   130 fun static_value_exn cookie { ctxt, target, lift_postproc, consts } =
   131   let
   132     fun compilation' { program, deps } =
   133       let
   134         val compilation'' = run_compilation_text cookie ctxt
   135           (build_compilation_text ctxt target program (map Constant deps));
   136       in fn _ => fn _ => fn vs_ty_t => fn _ => compilation'' vs_ty_t [] end;
   137     val compilation = Code_Thingol.static_value { ctxt = ctxt,
   138       lift_postproc = Exn.map_res o lift_postproc, consts = consts }
   139       compilation';
   140   in fn ctxt' => compilation ctxt' o reject_vars ctxt' end;
   141 
   142 fun static_value_strict cookie x = Exn.release oo static_value_exn cookie x;
   143 
   144 fun static_value cookie x = partiality_as_none oo static_value_exn cookie x;
   145 
   146 
   147 (* evaluation for truth or nothing *)
   148 
   149 structure Truth_Result = Proof_Data
   150 (
   151   type T = unit -> truth;
   152   val empty: T = fn () => raise Fail "Truth_Result";
   153   fun init _ = empty;
   154 );
   155 val put_truth = Truth_Result.put;
   156 val truth_cookie = (Truth_Result.get, put_truth, Long_Name.append this "put_truth");
   157 
   158 local
   159 
   160 val reject_vars = fn ctxt => tap (reject_vars ctxt o Thm.term_of);
   161 
   162 fun check_holds ctxt evaluator vs_t ct =
   163   let
   164     val t = Thm.term_of ct;
   165     val _ = if fastype_of t <> propT
   166       then error ("Not a proposition: " ^ Syntax.string_of_term ctxt t)
   167       else ();
   168     val iff = Thm.cterm_of ctxt (Term.Const (@{const_name Pure.eq}, propT --> propT --> propT));
   169     val result = case partiality_as_none (run_compilation_text truth_cookie ctxt evaluator vs_t [])
   170      of SOME Holds => true
   171       | _ => false;
   172   in
   173     Thm.mk_binop iff ct (if result then @{cprop "PROP Code_Generator.holds"} else ct)
   174   end;
   175 
   176 val (_, raw_check_holds_oracle) = Context.>>> (Context.map_theory_result
   177   (Thm.add_oracle (@{binding holds_by_evaluation},
   178   fn (ctxt, evaluator, vs_t, ct) => check_holds ctxt evaluator vs_t ct)));
   179 
   180 fun check_holds_oracle ctxt evaluator vs_ty_t ct =
   181   raw_check_holds_oracle (ctxt, evaluator, vs_ty_t, ct);
   182 
   183 in
   184 
   185 fun dynamic_holds_conv ctxt = Code_Thingol.dynamic_conv ctxt
   186   (fn program => fn vs_t => fn deps =>
   187     check_holds_oracle ctxt (build_compilation_text ctxt NONE program deps) vs_t)
   188       o reject_vars ctxt;
   189 
   190 fun static_holds_conv (ctxt_consts as { ctxt, ... }) =
   191   Code_Thingol.static_conv_thingol ctxt_consts (fn { program, deps } => fn ctxt' => fn vs_t =>
   192     K (check_holds_oracle ctxt' (build_compilation_text ctxt NONE program (map Constant deps)) vs_t o reject_vars ctxt'));
   193 
   194 end; (*local*)
   195 
   196 
   197 (** generator for computations -- partial implementations of the universal morphism from Isabelle to ML terms **)
   198 
   199 (* auxiliary *)
   200 
   201 val generated_computationN = "Generated_Computation";
   202 
   203 
   204 (* possible type signatures of constants *)
   205 
   206 fun typ_signatures_for T =
   207   let
   208     val (Ts, T') = strip_type T;
   209   in map_range (fn n => (drop n Ts ---> T', take n Ts)) (length Ts + 1) end;
   210 
   211 fun typ_signatures cTs =
   212   let
   213     fun add (c, T) =
   214       fold (fn (T, Ts) => Typtab.map_default (T, []) (cons (c, Ts)))
   215         (typ_signatures_for T);
   216   in
   217     Typtab.empty
   218     |> fold add cTs
   219     |> Typtab.lookup_list
   220   end;
   221 
   222 
   223 (* name mangling *)
   224 
   225 local
   226 
   227 fun tycos_of (Type (tyco, Ts)) = maps tycos_of Ts @ [tyco]
   228   | tycos_of _ = [];
   229 
   230 val ml_name_of = Name.desymbolize NONE o Long_Name.base_name;
   231 
   232 in
   233 
   234 fun of_term_for_typ Ts =
   235   let
   236     val names = Ts
   237       |> map (suffix "_of_term" o space_implode "_" o map ml_name_of o tycos_of)
   238       |> Name.variant_list [];
   239   in the o AList.lookup (op =) (Ts ~~ names) end;
   240 
   241 fun eval_for_const ctxt cTs =
   242   let
   243     fun symbol_list (c, T) = c :: maps tycos_of (Sign.const_typargs (Proof_Context.theory_of ctxt) (c, T))
   244     val names = cTs
   245       |> map (prefix "eval_" o space_implode "_" o map ml_name_of o symbol_list)
   246       |> Name.variant_list [];
   247   in the o AList.lookup (op =) (cTs ~~ names) end;
   248 
   249 end;
   250 
   251 
   252 (* checks for input terms *)
   253 
   254 fun monomorphic T = fold_atyps ((K o K) false) T true;
   255 
   256 fun check_typ ctxt T t =
   257   Syntax.check_term ctxt (Type.constraint T t);
   258 
   259 fun check_computation_input ctxt cTs t =
   260   let
   261     fun check t = check_comb (strip_comb t)
   262     and check_comb (t as Abs _, _) =
   263           error ("Bad term, contains abstraction: " ^ Syntax.string_of_term ctxt t)
   264       | check_comb (t as Const (cT as (c, T)), ts) =
   265           let
   266             val _ = if not (member (op =) cTs cT)
   267               then error ("Bad term, computation cannot proceed on constant " ^ Syntax.string_of_term ctxt t)
   268               else ();
   269             val _ = if not (monomorphic T)
   270               then error ("Bad term, contains polymorphic constant " ^ Syntax.string_of_term ctxt t)
   271               else ();
   272             val _ = map check ts;
   273           in () end;
   274     val _ = check t;
   275   in t end;
   276 
   277 
   278 (* code generation for of the universal morphism *)
   279 
   280 fun print_of_term_funs { typ_sign_for, eval_for_const, of_term_for_typ } Ts =
   281   let
   282     val var_names = map_range (fn n => "t" ^ string_of_int (n + 1));
   283     fun print_lhs c xs = "Const (" ^ quote c ^ ", _)"
   284       |> fold (fn x => fn s => s ^ " $ " ^ x) xs
   285       |> enclose "(" ")";
   286     fun print_rhs c Ts T xs = eval_for_const (c, Ts ---> T)
   287       |> fold2 (fn T' => fn x => fn s =>
   288          s ^ (" (" ^ of_term_for_typ T' ^ " " ^ x ^ ")")) Ts xs
   289     fun print_eq T (c, Ts) =
   290       let
   291         val xs = var_names (length Ts);
   292       in print_lhs c xs ^ " = " ^ print_rhs c Ts T xs end;
   293     fun print_eqs T =
   294       let
   295         val typ_signs = typ_sign_for T;
   296         val name = of_term_for_typ T;
   297       in
   298         map (print_eq T) typ_signs
   299         |> map (prefix (name ^ " "))
   300         |> space_implode "\n  | "
   301       end;
   302   in
   303     map print_eqs Ts
   304     |> space_implode "\nand "
   305     |> prefix "fun "
   306   end;
   307 
   308 fun print_computation_code ctxt compiled_value requested_Ts cTs =
   309   let
   310     val proper_cTs = map_filter I cTs;
   311     val typ_sign_for = typ_signatures proper_cTs;
   312     fun add_typ T Ts =
   313       if member (op =) Ts T
   314       then Ts
   315       else Ts
   316         |> cons T
   317         |> fold (fold add_typ o snd) (typ_sign_for T);
   318     val required_Ts = fold add_typ requested_Ts [];
   319     val of_term_for_typ' = of_term_for_typ required_Ts;
   320     val eval_for_const' = eval_for_const ctxt proper_cTs;
   321     val eval_for_const'' = the_default "_" o Option.map eval_for_const';
   322     val eval_tuple = enclose "(" ")" (commas (map eval_for_const' proper_cTs));
   323     fun mk_abs s = "fn " ^ s ^ " => ";
   324     val eval_abs = space_implode ""
   325       (map (mk_abs o eval_for_const'') cTs);
   326     val of_term_code = print_of_term_funs {
   327       typ_sign_for = typ_sign_for,
   328       eval_for_const = eval_for_const',
   329       of_term_for_typ = of_term_for_typ' } required_Ts;
   330   in
   331     (cat_lines [
   332       "structure " ^ generated_computationN ^ " =",
   333       "struct",
   334       "",
   335       "val " ^ eval_tuple ^ " = " ^ compiled_value ^ " ()",
   336       "  (" ^ eval_abs,
   337       "    " ^ eval_tuple ^ ");",
   338       "",
   339       of_term_code,
   340       "",
   341       "end"
   342     ], map (prefix (generated_computationN ^ ".") o of_term_for_typ') requested_Ts)
   343   end;
   344 
   345 fun mount_computation ctxt cTs T raw_computation lift_postproc =
   346   Code_Preproc.static_value { ctxt = ctxt, lift_postproc = lift_postproc, consts = [] }
   347     (fn _ => fn ctxt' =>
   348       check_typ ctxt' T
   349       #> reject_vars ctxt'
   350       #> check_computation_input ctxt' cTs
   351       #> raw_computation);
   352 
   353 fun compile_computation cookie ctxt T program evals vs_ty_evals deps =
   354   let
   355     fun the_const (Const cT) = cT
   356       | the_const t = error ("No constant after preprocessing: " ^ Syntax.string_of_term ctxt t)
   357     val raw_cTs = case evals of
   358         Abs (_, _, t) => (map the_const o snd o strip_comb) t
   359       | _ => error ("Bad term after preprocessing: " ^ Syntax.string_of_term ctxt evals);
   360     val cTs = fold_rev (fn cT => fn cTs =>
   361       (if member (op =) cTs (SOME cT) then NONE else SOME cT) :: cTs) raw_cTs [];
   362     fun comp' vs_ty_evals =
   363       let
   364         val (generated_code, compiled_value) =
   365           build_compilation_text ctxt NONE program deps vs_ty_evals;
   366         val (of_term_code, [of_term]) = print_computation_code ctxt compiled_value [T] cTs;
   367       in
   368         (generated_code ^ "\n" ^ of_term_code,
   369           enclose "(" ")" ("fn () => " ^ of_term))
   370       end;
   371     val compiled_computation =
   372       Exn.release (run_compilation_text cookie ctxt comp' vs_ty_evals []);
   373   in (map_filter I cTs, compiled_computation) end;
   374 
   375 fun experimental_computation cookie { ctxt, lift_postproc, terms = ts, T } =
   376   let
   377     val _ = if not (monomorphic T)
   378       then error ("Polymorphic type: " ^ Syntax.string_of_typ ctxt T)
   379       else ();
   380     val cTs = (fold o fold_aterms)
   381       (fn (t as Const (cT as (_, T))) =>
   382         if not (monomorphic T) then error ("Polymorphic constant:" ^ Syntax.string_of_term ctxt t)
   383         else insert (op =) cT | _ => I) ts [];
   384     val evals = Abs ("eval", map snd cTs ---> TFree (Name.aT, []), list_comb (Bound 0, map Const cTs));
   385     val (cTs, raw_computation) = Code_Thingol.dynamic_value ctxt
   386       (K I) (compile_computation cookie ctxt T) evals;
   387   in
   388     mount_computation ctxt cTs T raw_computation lift_postproc
   389   end;
   390 
   391 
   392 (** code antiquotation **)
   393 
   394 fun runtime_code ctxt module_name program tycos consts =
   395   let
   396     val thy = Proof_Context.theory_of ctxt;
   397     val (ml_modules, target_names) =
   398       Code_Target.produce_code_for ctxt
   399         target NONE module_name [] program false (map Constant consts @ map Type_Constructor tycos);
   400     val ml_code = space_implode "\n\n" (map snd ml_modules);
   401     val (consts', tycos') = chop (length consts) target_names;
   402     val consts_map = map2 (fn const =>
   403       fn NONE =>
   404           error ("Constant " ^ (quote o Code.string_of_const thy) const ^
   405             "\nhas a user-defined serialization")
   406        | SOME const' => (const, const')) consts consts'
   407     val tycos_map = map2 (fn tyco =>
   408       fn NONE =>
   409           error ("Type " ^ quote (Proof_Context.markup_type ctxt tyco) ^
   410             "\nhas a user-defined serialization")
   411         | SOME tyco' => (tyco, tyco')) tycos tycos';
   412   in (ml_code, (tycos_map, consts_map)) end;
   413 
   414 local
   415 
   416 structure Code_Antiq_Data = Proof_Data
   417 (
   418   type T = { named_consts: string list,
   419     first_occurrence: bool,
   420     generated_code: {
   421       code: string,
   422       consts_map: (string * string) list
   423     } lazy
   424   };
   425   val empty: T = { named_consts = [],
   426     first_occurrence = true,
   427     generated_code = Lazy.value {
   428       code = "",
   429       consts_map = []
   430     }
   431   };
   432   fun init _ = empty;
   433 );
   434 
   435 val is_first_occurrence = #first_occurrence o Code_Antiq_Data.get;
   436 
   437 fun lazy_code ctxt consts = Lazy.lazy (fn () =>
   438   let
   439     val program = Code_Thingol.consts_program ctxt consts;
   440     val (code, (_, consts_map)) =
   441       runtime_code ctxt Code_Target.generatedN program [] consts
   442   in { code = code, consts_map = consts_map } end);
   443 
   444 fun register_const const ctxt =
   445   let
   446     val consts = insert (op =) const ((#named_consts o Code_Antiq_Data.get) ctxt);
   447   in
   448     ctxt
   449     |> Code_Antiq_Data.put { 
   450         named_consts = consts,
   451         first_occurrence = false,
   452         generated_code = lazy_code ctxt consts
   453       }
   454   end;
   455 
   456 fun print_code is_first_occ const ctxt =
   457   let
   458     val { code, consts_map } = (Lazy.force o #generated_code o Code_Antiq_Data.get) ctxt;
   459     val effective_code = if is_first_occ then code else "";
   460     val body = ML_Context.struct_name ctxt ^ "." ^ the (AList.lookup (op =) consts_map const);
   461   in (effective_code, body) end;
   462 
   463 in
   464 
   465 fun ml_code_antiq raw_const ctxt =
   466   let
   467     val thy = Proof_Context.theory_of ctxt;
   468     val const = Code.check_const thy raw_const;
   469     val is_first = is_first_occurrence ctxt;
   470   in (print_code is_first const, register_const const ctxt) end;
   471 
   472 end; (*local*)
   473 
   474 
   475 (** reflection support **)
   476 
   477 fun check_datatype thy tyco some_consts =
   478   let
   479     val declared_constrs = (map fst o snd o fst o Code.get_type thy) tyco;
   480     val constrs = case some_consts
   481      of SOME [] => []
   482       | SOME consts =>
   483           let
   484             val missing_constrs = subtract (op =) consts declared_constrs;
   485             val _ = if null missing_constrs then []
   486               else error ("Missing constructor(s) " ^ commas_quote missing_constrs
   487                 ^ " for datatype " ^ quote tyco);
   488             val false_constrs = subtract (op =) declared_constrs consts;
   489             val _ = if null false_constrs then []
   490               else error ("Non-constructor(s) " ^ commas_quote false_constrs
   491                 ^ " for datatype " ^ quote tyco)
   492           in consts end
   493       | NONE => declared_constrs;
   494   in (tyco, constrs) end;
   495 
   496 fun add_eval_tyco (tyco, tyco') thy =
   497   let
   498     val k = Sign.arity_number thy tyco;
   499     fun pr pr' _ [] = tyco'
   500       | pr pr' _ [ty] =
   501           Code_Printer.concat [pr' Code_Printer.BR ty, tyco']
   502       | pr pr' _ tys =
   503           Code_Printer.concat [Code_Printer.enum "," "(" ")" (map (pr' Code_Printer.BR) tys), tyco']
   504   in
   505     thy
   506     |> Code_Target.set_printings (Type_Constructor (tyco, [(target, SOME (k, pr))]))
   507   end;
   508 
   509 fun add_eval_constr (const, const') thy =
   510   let
   511     val k = Code.args_number thy const;
   512     fun pr pr' fxy ts = Code_Printer.brackify fxy
   513       (const' :: the_list (Code_Printer.tuplify pr' Code_Printer.BR (map fst ts)));
   514   in
   515     thy
   516     |> Code_Target.set_printings (Constant (const,
   517       [(target, SOME (Code_Printer.simple_const_syntax (k, pr)))]))
   518   end;
   519 
   520 fun add_eval_const (const, const') = Code_Target.set_printings (Constant
   521   (const, [(target, SOME (Code_Printer.simple_const_syntax (0, (K o K o K) const')))]));
   522 
   523 fun process_reflection (code, (tyco_map, (constr_map, const_map))) module_name NONE thy =
   524       thy
   525       |> Code_Target.add_reserved target module_name
   526       |> Context.theory_map (compile_ML true code)
   527       |> fold (add_eval_tyco o apsnd Code_Printer.str) tyco_map
   528       |> fold (add_eval_constr o apsnd Code_Printer.str) constr_map
   529       |> fold (add_eval_const o apsnd Code_Printer.str) const_map
   530   | process_reflection (code, _) _ (SOME file_name) thy =
   531       let
   532         val preamble =
   533           "(* Generated from " ^
   534             Path.implode (Resources.thy_path (Path.basic (Context.theory_name thy))) ^
   535           "; DO NOT EDIT! *)";
   536         val _ = File.write (Path.explode file_name) (preamble ^ "\n\n" ^ code);
   537       in
   538         thy
   539       end;
   540 
   541 fun gen_code_reflect prep_type prep_const raw_datatypes raw_functions module_name some_file thy  =
   542   let
   543     val ctxt = Proof_Context.init_global thy;
   544     val datatypes = map (fn (raw_tyco, raw_cos) =>
   545       (prep_type ctxt raw_tyco, (Option.map o map) (prep_const thy) raw_cos)) raw_datatypes;
   546     val (tycos, constrs) = map_split (uncurry (check_datatype thy)) datatypes
   547       |> apsnd flat;
   548     val functions = map (prep_const thy) raw_functions;
   549     val consts = constrs @ functions;
   550     val program = Code_Thingol.consts_program ctxt consts;
   551     val result = runtime_code ctxt module_name program tycos consts
   552       |> (apsnd o apsnd) (chop (length constrs));
   553   in
   554     thy
   555     |> process_reflection result module_name some_file
   556   end;
   557 
   558 val code_reflect = gen_code_reflect Code_Target.cert_tyco (K I);
   559 val code_reflect_cmd = gen_code_reflect Code_Target.read_tyco Code.read_const;
   560 
   561 
   562 (** Isar setup **)
   563 
   564 val _ =
   565   Theory.setup (ML_Antiquotation.declaration @{binding code} Args.term (fn _ => ml_code_antiq));
   566 
   567 local
   568 
   569 val parse_datatype =
   570   Parse.name -- Scan.optional (@{keyword "="} |--
   571     (((Parse.sym_ident || Parse.string) >> (fn "_" => NONE | _ => Scan.fail ()))
   572     || ((Parse.term ::: (Scan.repeat (@{keyword "|"} |-- Parse.term))) >> SOME))) (SOME []);
   573 
   574 in
   575 
   576 val _ =
   577   Outer_Syntax.command @{command_keyword code_reflect}
   578     "enrich runtime environment with generated code"
   579     (Parse.name -- Scan.optional (@{keyword "datatypes"} |-- Parse.!!! (parse_datatype
   580       ::: Scan.repeat (@{keyword "and"} |-- parse_datatype))) []
   581     -- Scan.optional (@{keyword "functions"} |-- Parse.!!!  (Scan.repeat1 Parse.name)) []
   582     -- Scan.option (@{keyword "file"} |-- Parse.!!! Parse.name)
   583     >> (fn (((module_name, raw_datatypes), raw_functions), some_file) => Toplevel.theory
   584       (code_reflect_cmd raw_datatypes raw_functions module_name some_file)));
   585 
   586 end; (*local*)
   587 
   588 
   589 (** using external SML files as substitute for proper definitions -- only for polyml!  **)
   590 
   591 local
   592 
   593 structure Loaded_Values = Theory_Data
   594 (
   595   type T = string list
   596   val empty = []
   597   val extend = I
   598   fun merge data : T = Library.merge (op =) data
   599 );
   600 
   601 fun notify_val (string, value) = 
   602   let
   603     val _ = #enterVal ML_Env.name_space (string, value);
   604     val _ = Theory.setup (Loaded_Values.map (insert (op =) string));
   605   in () end;
   606 
   607 fun abort _ = error "Only value bindings allowed.";
   608 
   609 val notifying_context : ML_Compiler0.context =
   610  {name_space =
   611    {lookupVal    = #lookupVal ML_Env.name_space,
   612     lookupType   = #lookupType ML_Env.name_space,
   613     lookupFix    = #lookupFix ML_Env.name_space,
   614     lookupStruct = #lookupStruct ML_Env.name_space,
   615     lookupSig    = #lookupSig ML_Env.name_space,
   616     lookupFunct  = #lookupFunct ML_Env.name_space,
   617     enterVal     = notify_val,
   618     enterType    = abort,
   619     enterFix     = abort,
   620     enterStruct  = abort,
   621     enterSig     = abort,
   622     enterFunct   = abort,
   623     allVal       = #allVal ML_Env.name_space,
   624     allType      = #allType ML_Env.name_space,
   625     allFix       = #allFix ML_Env.name_space,
   626     allStruct    = #allStruct ML_Env.name_space,
   627     allSig       = #allSig ML_Env.name_space,
   628     allFunct     = #allFunct ML_Env.name_space},
   629   print_depth = NONE,
   630   here = #here ML_Env.context,
   631   print = #print ML_Env.context,
   632   error = #error ML_Env.context};
   633 
   634 in
   635 
   636 fun use_file filepath thy =
   637   let
   638     val thy' = Loaded_Values.put [] thy;
   639     val _ = Context.put_generic_context ((SOME o Context.Theory) thy');
   640     val _ =
   641       ML_Compiler0.ML notifying_context
   642         {line = 0, file = Path.implode filepath, verbose = false, debug = false}
   643         (File.read filepath);
   644     val thy'' = Context.the_global_context ();
   645     val names = Loaded_Values.get thy'';
   646   in (names, thy'') end;
   647 
   648 end;
   649 
   650 fun add_definiendum (ml_name, (b, T)) thy =
   651   thy
   652   |> Code_Target.add_reserved target ml_name
   653   |> Specification.axiomatization [(b, SOME T, NoSyn)] [] [] []
   654   |-> (fn ([Const (const, _)], _) =>
   655     Code_Target.set_printings (Constant (const,
   656       [(target, SOME (Code_Printer.simple_const_syntax (0, (K o K o K o Code_Printer.str) ml_name)))]))
   657   #> tap (fn thy => Code_Target.produce_code (Proof_Context.init_global thy) false [const] target NONE Code_Target.generatedN []));
   658 
   659 fun process_file filepath (definienda, thy) =
   660   let
   661     val (ml_names, thy') = use_file filepath thy;
   662     val superfluous = subtract (fn ((name1, _), name2) => name1 = name2) definienda ml_names;
   663     val _ = if null superfluous then ()
   664       else error ("Value binding(s) " ^ commas_quote superfluous
   665         ^ " found in external file " ^ Path.print filepath
   666         ^ " not present among the given contants binding(s).");
   667     val these_definienda = AList.make (the o AList.lookup (op =) definienda) ml_names;
   668     val thy'' = fold add_definiendum these_definienda thy';
   669     val definienda' = fold (AList.delete (op =)) ml_names definienda;
   670   in (definienda', thy'') end;
   671 
   672 fun polyml_as_definition bTs filepaths thy =
   673   let
   674     val definienda = map (fn bT => ((Binding.name_of o fst) bT, bT)) bTs;
   675     val (remaining, thy') = fold process_file filepaths (definienda, thy);
   676     val _ = if null remaining then ()
   677       else error ("Constant binding(s) " ^ commas_quote (map fst remaining)
   678         ^ " not present in external file(s).");
   679   in thy' end;
   680 
   681 end; (*struct*)