src/Pure/Isar/code.ML
changeset 67646 85582dded912
parent 67522 9e712280cc37
child 67649 1e1782c1aedf
equal deleted inserted replaced
67645:5e0c81750441 67646:85582dded912
    88 structure Code : PRIVATE_CODE =
    88 structure Code : PRIVATE_CODE =
    89 struct
    89 struct
    90 
    90 
    91 (** auxiliary **)
    91 (** auxiliary **)
    92 
    92 
       
    93 (* close theorem for storage *)
       
    94 
       
    95 val close = Thm.trim_context o Thm.close_derivation;
       
    96 
       
    97 
    93 (* printing *)
    98 (* printing *)
    94 
    99 
    95 fun string_of_typ thy =
   100 fun string_of_typ thy =
    96   Syntax.string_of_typ (Config.put show_sorts true (Syntax.init_pretty_global thy));
   101   Syntax.string_of_typ (Config.put show_sorts true (Syntax.init_pretty_global thy));
    97 
   102 
   389 (* global theory store *)
   394 (* global theory store *)
   390 
   395 
   391 local
   396 local
   392 
   397 
   393 type data = Any.T Datatab.table;
   398 type data = Any.T Datatab.table;
   394 fun empty_dataref () = Synchronized.var "code data" (NONE : (data * theory) option);
   399 fun empty_dataref () = Synchronized.var "code data" (NONE : (data * Context.theory_id) option);
   395 
   400 
   396 structure Code_Data = Theory_Data
   401 structure Code_Data = Theory_Data
   397 (
   402 (
   398   type T = specs * (data * theory) option Synchronized.var;
   403   type T = specs * (data * Context.theory_id) option Synchronized.var;
   399   val empty = (empty_specs, empty_dataref ());
   404   val empty = (empty_specs, empty_dataref ());
   400   val extend : T -> T = apsnd (K (empty_dataref ()));
   405   val extend : T -> T = apsnd (K (empty_dataref ()));
   401   fun merge ((specs1, _), (specs2, _)) =
   406   fun merge ((specs1, _), (specs2, _)) =
   402     (merge_specs (specs1, specs2), empty_dataref ());
   407     (merge_specs (specs1, specs2), empty_dataref ());
   403 );
   408 );
   415 (* access to data dependent on executable specifications *)
   420 (* access to data dependent on executable specifications *)
   416 
   421 
   417 fun change_yield_data (kind, mk, dest) theory f =
   422 fun change_yield_data (kind, mk, dest) theory f =
   418   let
   423   let
   419     val dataref = (snd o Code_Data.get) theory;
   424     val dataref = (snd o Code_Data.get) theory;
   420     val (datatab, thy) = case Synchronized.value dataref
   425     val (datatab, thy_id) = case Synchronized.value dataref
   421      of SOME (datatab, thy) =>
   426      of SOME (datatab, thy_id) =>
   422         if Context.eq_thy (theory, thy)
   427         if Context.eq_thy_id (Context.theory_id theory, thy_id)
   423           then (datatab, thy)
   428           then (datatab, thy_id)
   424           else (Datatab.empty, theory)
   429           else (Datatab.empty, Context.theory_id theory)
   425       | NONE => (Datatab.empty, theory)
   430       | NONE => (Datatab.empty, Context.theory_id theory)
   426     val data = case Datatab.lookup datatab kind
   431     val data = case Datatab.lookup datatab kind
   427      of SOME data => data
   432      of SOME data => data
   428       | NONE => invoke_init kind;
   433       | NONE => invoke_init kind;
   429     val result as (_, data') = f (dest data);
   434     val result as (_, data') = f (dest data);
   430     val _ = Synchronized.change dataref
   435     val _ = Synchronized.change dataref
   431       ((K o SOME) (Datatab.update (kind, mk data') datatab, thy));
   436       ((K o SOME) (Datatab.update (kind, mk data') datatab, thy_id));
   432   in result end;
   437   in result end;
   433 
   438 
   434 end; (*local*)
   439 end; (*local*)
   435 
   440 
   436 
   441 
  1379 local
  1384 local
  1380 
  1385 
  1381 fun subsumptive_add thy verbose (thm, proper) eqns =
  1386 fun subsumptive_add thy verbose (thm, proper) eqns =
  1382   let
  1387   let
  1383     val args_of = drop_prefix is_Var o rev o snd o strip_comb
  1388     val args_of = drop_prefix is_Var o rev o snd o strip_comb
  1384       o Term.map_types Type.strip_sorts o fst o Logic.dest_equals o Thm.plain_prop_of;
  1389       o Term.map_types Type.strip_sorts o fst o Logic.dest_equals o Thm.plain_prop_of
       
  1390       o Thm.transfer thy;
  1385     val args = args_of thm;
  1391     val args = args_of thm;
  1386     val incr_idx = Logic.incr_indexes ([], [], Thm.maxidx_of thm + 1);
  1392     val incr_idx = Logic.incr_indexes ([], [], Thm.maxidx_of thm + 1);
  1387     fun matches_args args' =
  1393     fun matches_args args' =
  1388       let
  1394       let
  1389         val k = length args' - length args
  1395         val k = length args' - length args
  1394     fun drop (thm', proper') = if (proper orelse not proper')
  1400     fun drop (thm', proper') = if (proper orelse not proper')
  1395       andalso matches_args (args_of thm') then
  1401       andalso matches_args (args_of thm') then
  1396         (if verbose then warning ("Code generator: dropping subsumed code equation\n" ^
  1402         (if verbose then warning ("Code generator: dropping subsumed code equation\n" ^
  1397             Thm.string_of_thm_global thy thm') else (); true)
  1403             Thm.string_of_thm_global thy thm') else (); true)
  1398       else false;
  1404       else false;
  1399   in (thm, proper) :: filter_out drop eqns end;
  1405   in (close thm, proper) :: filter_out drop eqns end;
  1400 
  1406 
  1401 fun add_eqn_for (c, eqn) thy =
  1407 fun add_eqn_for (c, eqn) thy =
  1402   thy |> modify_specs (modify_pending_eqns c
  1408   thy |> modify_specs (modify_pending_eqns c (subsumptive_add thy true eqn));
  1403     (subsumptive_add thy true (apfst Thm.close_derivation eqn)));
       
  1404 
  1409 
  1405 fun add_eqns_for default (c, proto_eqns) thy =
  1410 fun add_eqns_for default (c, proto_eqns) thy =
  1406   thy |> modify_specs (fn specs =>
  1411   thy |> modify_specs (fn specs =>
  1407     if is_default (lookup_fun_spec specs c) orelse not default
  1412     if is_default (lookup_fun_spec specs c) orelse not default
  1408     then
  1413     then
  1409       let
  1414       let
  1410         val eqns = []
  1415         val eqns = []
  1411           |> fold_rev (subsumptive_add thy (not default)) proto_eqns
  1416           |> fold_rev (subsumptive_add thy (not default)) proto_eqns;
  1412           |> (map o apfst) Thm.close_derivation;
       
  1413       in specs |> register_fun_spec c (Eqns (default, eqns)) end
  1417       in specs |> register_fun_spec c (Eqns (default, eqns)) end
  1414     else specs);
  1418     else specs);
  1415 
  1419 
  1416 fun add_abstract_for (c, (thm, tyco_abs as (tyco, _))) =
  1420 fun add_abstract_for (c, (thm, tyco_abs as (tyco, _))) =
  1417   modify_specs (register_fun_spec c (Abstr (Thm.close_derivation thm, tyco_abs))
  1421   modify_specs (register_fun_spec c (Abstr (Thm.close_derivation thm, tyco_abs))