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