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