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