src/Tools/Code/code_runtime.ML
author wenzelm
Fri Mar 21 20:33:56 2014 +0100 (2014-03-21)
changeset 56245 84fc7dfa3cd4
parent 56208 06cc31dff138
child 56618 874bdedb2313
permissions -rw-r--r--
more qualified names;
     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 -> Proof.context -> string option
    21     -> ((term -> term) -> 'a -> 'a) -> string list -> Proof.context -> term -> 'a option
    22   val static_value_strict: 'a cookie -> Proof.context -> string option
    23     -> ((term -> term) -> 'a -> 'a) -> string list -> Proof.context -> term -> 'a
    24   val static_value_exn: 'a cookie -> Proof.context -> string option
    25     -> ((term -> term) -> 'a -> 'a) -> string list -> Proof.context -> term -> 'a Exn.result
    26   val dynamic_holds_conv: Proof.context -> conv
    27   val static_holds_conv: Proof.context -> string list -> Proof.context -> conv
    28   val code_reflect: (string * string list option) list -> string list -> string
    29     -> string option -> theory -> theory
    30   datatype truth = Holds
    31   val put_truth: (unit -> truth) -> Proof.context -> Proof.context
    32   val trace: bool Unsynchronized.ref
    33   val polyml_as_definition: (binding * typ) list -> Path.T list -> theory -> theory
    34 end;
    35 
    36 structure Code_Runtime : CODE_RUNTIME =
    37 struct
    38 
    39 open Basic_Code_Symbol;
    40 open Basic_Code_Thingol;
    41 
    42 (** evaluation **)
    43 
    44 (* technical prerequisites *)
    45 
    46 val this = "Code_Runtime";
    47 val s_truth = Long_Name.append this "truth";
    48 val s_Holds = Long_Name.append this "Holds";
    49 
    50 val target = "Eval";
    51 val structure_generated = "Generated_Code";
    52 
    53 datatype truth = Holds;
    54 
    55 val _ = Theory.setup
    56   (Code_Target.extend_target (target, (Code_ML.target_SML, I))
    57   #> Code_Target.set_printings (Type_Constructor (@{type_name prop},
    58     [(target, SOME (0, (K o K o K) (Code_Printer.str s_truth)))]))
    59   #> Code_Target.set_printings (Constant (@{const_name Code_Generator.holds},
    60     [(target, SOME (Code_Printer.plain_const_syntax s_Holds))]))
    61   #> Code_Target.add_reserved target this
    62   #> fold (Code_Target.add_reserved target) ["oo", "ooo", "oooo", "upto", "downto", "orf", "andf"]);
    63        (*avoid further pervasive infix names*)
    64 
    65 val trace = Unsynchronized.ref false;
    66 
    67 fun exec verbose code =
    68   (if ! trace then tracing code else ();
    69   ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code));
    70 
    71 fun value ctxt (get, put, put_ml) (prelude, value) =
    72   let
    73     val code = (prelude
    74       ^ "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
    75       ^ " (fn () => " ^ value ^ ")) (ML_Context.the_generic_context ())))");
    76     val ctxt' = ctxt
    77       |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
    78       |> Context.proof_map (exec false code);
    79   in get ctxt' () end;
    80 
    81 
    82 (* evaluation into target language values *)
    83 
    84 type 'a cookie = (Proof.context -> unit -> 'a) * ((unit -> 'a) -> Proof.context -> Proof.context) * string;
    85 
    86 fun reject_vars ctxt t =
    87   ((Sign.no_frees ctxt o Sign.no_vars ctxt o map_types (K dummyT)) t; t);
    88 
    89 fun obtain_evaluator ctxt some_target program consts =
    90   let
    91     val evaluator' = Code_Target.evaluator ctxt (the_default target some_target) program consts false;
    92   in
    93     evaluator'
    94     #> apfst (fn ml_modules => space_implode "\n\n" (map snd ml_modules))
    95   end;
    96 
    97 fun evaluation cookie ctxt evaluator vs_t args =
    98   let
    99     val (program_code, value_name) = evaluator vs_t;
   100     val value_code = space_implode " "
   101       (value_name :: "()" :: map (enclose "(" ")") args);
   102   in Exn.interruptible_capture (value ctxt cookie) (program_code, value_code) end;
   103 
   104 fun partiality_as_none e = SOME (Exn.release e)
   105   handle General.Match => NONE
   106     | General.Bind => NONE
   107     | General.Fail _ => NONE;
   108 
   109 fun dynamic_value_exn cookie ctxt some_target postproc t args =
   110   let
   111     val _ = reject_vars ctxt t;
   112     val _ = if ! trace
   113       then tracing ("Evaluation of term " ^ quote (Syntax.string_of_term ctxt t))
   114       else ()
   115     fun evaluator program ((_, vs_ty), t) deps =
   116       evaluation cookie ctxt (obtain_evaluator ctxt some_target program deps) (vs_ty, t) args;
   117   in Code_Thingol.dynamic_value ctxt (Exn.map_result o postproc) evaluator t end;
   118 
   119 fun dynamic_value_strict cookie ctxt some_target postproc t args =
   120   Exn.release (dynamic_value_exn cookie ctxt some_target postproc t args);
   121 
   122 fun dynamic_value cookie ctxt some_target postproc t args =
   123   partiality_as_none (dynamic_value_exn cookie ctxt some_target postproc t args);
   124 
   125 fun static_evaluator cookie ctxt some_target program consts' =
   126   let
   127     val evaluator = obtain_evaluator ctxt some_target program (map Constant consts');
   128     val evaluation' = evaluation cookie ctxt evaluator;
   129   in fn _ => fn ((_, vs_ty), t) => fn _ => evaluation' (vs_ty, t) [] end;
   130 
   131 fun static_value_exn cookie ctxt some_target postproc consts =
   132   let
   133     val evaluator = Code_Thingol.static_value ctxt (Exn.map_result o postproc) consts
   134       (static_evaluator cookie ctxt some_target);
   135   in fn ctxt' => evaluator ctxt' o reject_vars ctxt' end;
   136 
   137 fun static_value_strict cookie ctxt some_target postproc consts =
   138   Exn.release oo static_value_exn cookie ctxt some_target postproc consts;
   139 
   140 fun static_value cookie thy some_target postproc consts =
   141   partiality_as_none oo static_value_exn cookie thy some_target postproc consts;
   142 
   143 
   144 (* evaluation for truth or nothing *)
   145 
   146 structure Truth_Result = Proof_Data
   147 (
   148   type T = unit -> truth
   149   (* FIXME avoid user error with non-user text *)
   150   fun init _ () = error "Truth_Result"
   151 );
   152 val put_truth = Truth_Result.put;
   153 val truth_cookie = (Truth_Result.get, put_truth, Long_Name.append this "put_truth");
   154 
   155 val reject_vars = fn ctxt => tap (reject_vars ctxt o Thm.term_of);
   156 
   157 local
   158 
   159 fun check_holds ctxt evaluator vs_t ct =
   160   let
   161     val thy = Proof_Context.theory_of ctxt;
   162     val t = Thm.term_of ct;
   163     val _ = if fastype_of t <> propT
   164       then error ("Not a proposition: " ^ Syntax.string_of_term_global thy t)
   165       else ();
   166     val iff = Thm.cterm_of thy (Term.Const (@{const_name Pure.eq}, propT --> propT --> propT));
   167     val result = case partiality_as_none (evaluation truth_cookie ctxt evaluator vs_t [])
   168      of SOME Holds => true
   169       | _ => false;
   170   in
   171     Thm.mk_binop iff ct (if result then @{cprop "PROP Code_Generator.holds"} else ct)
   172   end;
   173 
   174 val (_, raw_check_holds_oracle) = Context.>>> (Context.map_theory_result
   175   (Thm.add_oracle (@{binding holds_by_evaluation},
   176   fn (ctxt, evaluator, vs_t, ct) => check_holds ctxt evaluator vs_t ct)));
   177 
   178 fun check_holds_oracle ctxt evaluator ((_, vs_ty), t) deps ct =
   179   raw_check_holds_oracle (ctxt, evaluator, (vs_ty, t), ct);
   180 
   181 in
   182 
   183 fun dynamic_holds_conv ctxt = Code_Thingol.dynamic_conv ctxt
   184   (fn program => fn vs_t => fn deps =>
   185     check_holds_oracle ctxt (obtain_evaluator ctxt NONE program deps) vs_t deps)
   186       o reject_vars ctxt;
   187 
   188 fun static_holds_conv ctxt consts =
   189   Code_Thingol.static_conv ctxt consts (fn program => fn consts' =>
   190     let
   191       val evaluator' = obtain_evaluator ctxt NONE program (map Constant consts')
   192     in
   193       fn ctxt' => fn vs_t => fn deps => check_holds_oracle ctxt' evaluator' vs_t deps o reject_vars ctxt'
   194     end);
   195 
   196 (* o reject_vars ctxt'*)
   197 
   198 end; (*local*)
   199 
   200 
   201 (** instrumentalization **)
   202 
   203 fun evaluation_code ctxt module_name tycos consts =
   204   let
   205     val thy = Proof_Context.theory_of ctxt;
   206     val program = Code_Thingol.consts_program thy consts;
   207     val (ml_modules, target_names) =
   208       Code_Target.produce_code_for ctxt
   209         target NONE module_name [] program false (map Constant consts @ map Type_Constructor tycos);
   210     val ml_code = space_implode "\n\n" (map snd ml_modules);
   211     val (consts', tycos') = chop (length consts) target_names;
   212     val consts_map = map2 (fn const =>
   213       fn NONE =>
   214           error ("Constant " ^ (quote o Code.string_of_const thy) const ^
   215             "\nhas a user-defined serialization")
   216        | SOME const' => (const, const')) consts consts'
   217     val tycos_map = map2 (fn tyco =>
   218       fn NONE =>
   219           error ("Type " ^ quote (Proof_Context.markup_type ctxt tyco) ^
   220             "\nhas a user-defined serialization")
   221         | SOME tyco' => (tyco, tyco')) tycos tycos';
   222   in (ml_code, (tycos_map, consts_map)) end;
   223 
   224 
   225 (* by antiquotation *)
   226 
   227 local
   228 
   229 structure Code_Antiq_Data = Proof_Data
   230 (
   231   type T = (string list * string list) * (bool
   232     * (string * (string * string) list) lazy);
   233   fun init _ = (([], []), (true, (Lazy.value ("", []))));
   234 );
   235 
   236 val is_first_occ = fst o snd o Code_Antiq_Data.get;
   237 
   238 fun register_code new_tycos new_consts ctxt =
   239   let
   240     val ((tycos, consts), _) = Code_Antiq_Data.get ctxt;
   241     val tycos' = fold (insert (op =)) new_tycos tycos;
   242     val consts' = fold (insert (op =)) new_consts consts;
   243     val acc_code = Lazy.lazy (fn () =>
   244       evaluation_code ctxt structure_generated tycos' consts'
   245       |> apsnd snd);
   246   in Code_Antiq_Data.put ((tycos', consts'), (false, acc_code)) ctxt end;
   247 
   248 fun register_const const = register_code [] [const];
   249 
   250 fun print_code is_first const ctxt =
   251   let
   252     val (_, (_, acc_code)) = Code_Antiq_Data.get ctxt;
   253     val (ml_code, consts_map) = Lazy.force acc_code;
   254     val ml_code = if is_first then ml_code else "";
   255     val body = "Isabelle." ^ the (AList.lookup (op =) consts_map const);
   256   in (ml_code, body) end;
   257 
   258 in
   259 
   260 fun ml_code_antiq raw_const ctxt =
   261   let
   262     val thy = Proof_Context.theory_of ctxt;
   263     val const = Code.check_const thy raw_const;
   264     val is_first = is_first_occ ctxt;
   265   in (print_code is_first const, register_const const ctxt) end;
   266 
   267 end; (*local*)
   268 
   269 
   270 (* reflection support *)
   271 
   272 fun check_datatype thy tyco some_consts =
   273   let
   274     val constrs = (map fst o snd o fst o Code.get_type thy) tyco;
   275     val _ = case some_consts
   276      of SOME consts =>
   277           let
   278             val missing_constrs = subtract (op =) consts constrs;
   279             val _ = if null missing_constrs then []
   280               else error ("Missing constructor(s) " ^ commas_quote missing_constrs
   281                 ^ " for datatype " ^ quote tyco);
   282             val false_constrs = subtract (op =) constrs consts;
   283             val _ = if null false_constrs then []
   284               else error ("Non-constructor(s) " ^ commas_quote false_constrs
   285                 ^ " for datatype " ^ quote tyco)
   286           in () end
   287       | NONE => ();
   288   in (tyco, constrs) end;
   289 
   290 fun add_eval_tyco (tyco, tyco') thy =
   291   let
   292     val k = Sign.arity_number thy tyco;
   293     fun pr pr' _ [] = tyco'
   294       | pr pr' _ [ty] =
   295           Code_Printer.concat [pr' Code_Printer.BR ty, tyco']
   296       | pr pr' _ tys =
   297           Code_Printer.concat [Code_Printer.enum "," "(" ")" (map (pr' Code_Printer.BR) tys), tyco']
   298   in
   299     thy
   300     |> Code_Target.set_printings (Type_Constructor (tyco, [(target, SOME (k, pr))]))
   301   end;
   302 
   303 fun add_eval_constr (const, const') thy =
   304   let
   305     val k = Code.args_number thy const;
   306     fun pr pr' fxy ts = Code_Printer.brackify fxy
   307       (const' :: the_list (Code_Printer.tuplify pr' Code_Printer.BR (map fst ts)));
   308   in
   309     thy
   310     |> Code_Target.set_printings (Constant (const,
   311       [(target, SOME (Code_Printer.simple_const_syntax (k, pr)))]))
   312   end;
   313 
   314 fun add_eval_const (const, const') = Code_Target.set_printings (Constant
   315   (const, [(target, SOME (Code_Printer.simple_const_syntax (0, (K o K o K) const')))]));
   316 
   317 fun process_reflection (code, (tyco_map, (constr_map, const_map))) module_name NONE thy =
   318       thy
   319       |> Code_Target.add_reserved target module_name
   320       |> Context.theory_map (exec true code)
   321       |> fold (add_eval_tyco o apsnd Code_Printer.str) tyco_map
   322       |> fold (add_eval_constr o apsnd Code_Printer.str) constr_map
   323       |> fold (add_eval_const o apsnd Code_Printer.str) const_map
   324   | process_reflection (code, _) _ (SOME file_name) thy =
   325       let
   326         val preamble =
   327           "(* Generated from " ^
   328             Path.implode (Resources.thy_path (Path.basic (Context.theory_name thy))) ^
   329           "; DO NOT EDIT! *)";
   330         val _ = File.write (Path.explode file_name) (preamble ^ "\n\n" ^ code);
   331       in
   332         thy
   333       end;
   334 
   335 fun gen_code_reflect prep_type prep_const raw_datatypes raw_functions module_name some_file thy  =
   336   let
   337     val ctxt = Proof_Context.init_global thy;
   338     val datatypes = map (fn (raw_tyco, raw_cos) =>
   339       (prep_type ctxt raw_tyco, (Option.map o map) (prep_const thy) raw_cos)) raw_datatypes;
   340     val (tycos, constrs) = map_split (uncurry (check_datatype thy)) datatypes
   341       |> apsnd flat;
   342     val functions = map (prep_const thy) raw_functions;
   343     val result = evaluation_code ctxt module_name tycos (constrs @ functions)
   344       |> (apsnd o apsnd) (chop (length constrs));
   345   in
   346     thy
   347     |> process_reflection result module_name some_file
   348   end;
   349 
   350 val code_reflect = gen_code_reflect Code_Target.cert_tyco (K I);
   351 val code_reflect_cmd = gen_code_reflect Code_Target.read_tyco Code.read_const;
   352 
   353 
   354 (** Isar setup **)
   355 
   356 val _ =
   357   Theory.setup (ML_Antiquotation.declaration @{binding code} Args.term (fn _ => ml_code_antiq));
   358 
   359 local
   360 
   361 val parse_datatype =
   362   Parse.name --| @{keyword "="} --
   363     (((Parse.sym_ident || Parse.string) >> (fn "_" => NONE | _ => Scan.fail ()))
   364     || ((Parse.term ::: (Scan.repeat (@{keyword "|"} |-- Parse.term))) >> SOME));
   365 
   366 in
   367 
   368 val _ =
   369   Outer_Syntax.command @{command_spec "code_reflect"}
   370     "enrich runtime environment with generated code"
   371     (Parse.name -- Scan.optional (@{keyword "datatypes"} |-- Parse.!!!  (parse_datatype
   372       ::: Scan.repeat (@{keyword "and"} |-- parse_datatype))) []
   373     -- Scan.optional (@{keyword "functions"} |-- Parse.!!!  (Scan.repeat1 Parse.name)) []
   374     -- Scan.option (@{keyword "file"} |-- Parse.!!! Parse.name)
   375     >> (fn (((module_name, raw_datatypes), raw_functions), some_file) => Toplevel.theory
   376       (code_reflect_cmd raw_datatypes raw_functions module_name some_file)));
   377 
   378 end; (*local*)
   379 
   380 
   381 (** using external SML files as substitute for proper definitions -- only for polyml!  **)
   382 
   383 local
   384 
   385 structure Loaded_Values = Theory_Data
   386 (
   387   type T = string list
   388   val empty = []
   389   val extend = I
   390   fun merge data : T = Library.merge (op =) data
   391 );
   392 
   393 fun notify_val (string, value) = 
   394   let
   395     val _ = #enterVal ML_Env.name_space (string, value);
   396     val _ = Theory.setup (Loaded_Values.map (insert (op =) string));
   397   in () end;
   398 
   399 fun abort _ = error "Only value bindings allowed.";
   400 
   401 val notifying_context : use_context =
   402  {tune_source = #tune_source ML_Env.local_context,
   403   name_space =
   404    {lookupVal    = #lookupVal ML_Env.name_space,
   405     lookupType   = #lookupType ML_Env.name_space,
   406     lookupFix    = #lookupFix ML_Env.name_space,
   407     lookupStruct = #lookupStruct ML_Env.name_space,
   408     lookupSig    = #lookupSig ML_Env.name_space,
   409     lookupFunct  = #lookupFunct ML_Env.name_space,
   410     enterVal     = notify_val,
   411     enterType    = abort,
   412     enterFix     = abort,
   413     enterStruct  = abort,
   414     enterSig     = abort,
   415     enterFunct   = abort,
   416     allVal       = #allVal ML_Env.name_space,
   417     allType      = #allType ML_Env.name_space,
   418     allFix       = #allFix ML_Env.name_space,
   419     allStruct    = #allStruct ML_Env.name_space,
   420     allSig       = #allSig ML_Env.name_space,
   421     allFunct     = #allFunct ML_Env.name_space},
   422   str_of_pos = #str_of_pos ML_Env.local_context,
   423   print = #print ML_Env.local_context,
   424   error = #error ML_Env.local_context};
   425 
   426 in
   427 
   428 fun use_file filepath thy =
   429   let
   430     val thy' = Loaded_Values.put [] thy;
   431     val _ = Context.set_thread_data ((SOME o Context.Theory) thy');
   432     val _ = Secure.use_text notifying_context
   433       (0, Path.implode filepath) false (File.read filepath);
   434     val thy'' = Context.the_theory (Context.the_thread_data ());
   435     val names = Loaded_Values.get thy'';
   436   in (names, thy'') end;
   437 
   438 end;
   439 
   440 fun add_definiendum (ml_name, (b, T)) thy =
   441   thy
   442   |> Code_Target.add_reserved target ml_name
   443   |> Specification.axiomatization [(b, SOME T, NoSyn)] []
   444   |-> (fn ([Const (const, _)], _) =>
   445     Code_Target.set_printings (Constant (const,
   446       [(target, SOME (Code_Printer.simple_const_syntax (0, (K o K o K o Code_Printer.str) ml_name)))]))
   447   #> tap (fn thy => Code_Target.produce_code (Proof_Context.init_global thy) false [const] target NONE structure_generated []));
   448 
   449 fun process_file filepath (definienda, thy) =
   450   let
   451     val (ml_names, thy') = use_file filepath thy;
   452     val superfluous = subtract (fn ((name1, _), name2) => name1 = name2) definienda ml_names;
   453     val _ = if null superfluous then ()
   454       else error ("Value binding(s) " ^ commas_quote superfluous
   455         ^ " found in external file " ^ Path.print filepath
   456         ^ " not present among the given contants binding(s).");
   457     val these_definienda = AList.make (the o AList.lookup (op =) definienda) ml_names;
   458     val thy'' = fold add_definiendum these_definienda thy';
   459     val definienda' = fold (AList.delete (op =)) ml_names definienda;
   460   in (definienda', thy'') end;
   461 
   462 fun polyml_as_definition bTs filepaths thy =
   463   let
   464     val definienda = map (fn bT => ((Binding.name_of o fst) bT, bT)) bTs;
   465     val (remaining, thy') = fold process_file filepaths (definienda, thy);
   466     val _ = if null remaining then ()
   467       else error ("Constant binding(s) " ^ commas_quote (map fst remaining)
   468         ^ " not present in external file(s).");
   469   in thy' end;
   470 
   471 end; (*struct*)