src/Pure/Tools/codegen_package.ML
changeset 19953 2f54a51f1801
parent 19884 a7be206d8655
child 19956 f992e507020e
equal deleted inserted replaced
19952:eaf2c25654d3 19953:2f54a51f1801
   217           )
   217           )
   218      )
   218      )
   219   |> Symtab.update (
   219   |> Symtab.update (
   220        #haskell CodegenSerializer.serializers
   220        #haskell CodegenSerializer.serializers
   221        |> apsnd (fn seri => seri
   221        |> apsnd (fn seri => seri
   222             [nsp_module, nsp_class, nsp_tyco, nsp_dtcon]
   222             (nsp_dtcon, [nsp_module, nsp_class, nsp_tyco, nsp_dtcon])
   223             [[nsp_module], [nsp_class], [nsp_tyco], [nsp_const, nsp_overl, nsp_mem], [nsp_dtcon], [nsp_inst], [nsp_instmem]]
   223             [[nsp_module], [nsp_class], [nsp_tyco], [nsp_const, nsp_overl, nsp_mem], [nsp_dtcon], [nsp_inst], [nsp_instmem]]
   224           )
   224           )
   225      )
   225      )
   226 );
   226 );
   227 
   227 
   305     syntax_tyco = Symtab.merge (eq_snd (op =)) (syntax_tyco1, syntax_tyco2),
   305     syntax_tyco = Symtab.merge (eq_snd (op =)) (syntax_tyco1, syntax_tyco2),
   306     syntax_const = Symtab.merge (eq_snd (op =)) (syntax_const1, syntax_const2) } : target_data;
   306     syntax_const = Symtab.merge (eq_snd (op =)) (syntax_const1, syntax_const2) } : target_data;
   307 
   307 
   308 structure CodegenData = TheoryDataFun
   308 structure CodegenData = TheoryDataFun
   309 (struct
   309 (struct
   310   val name = "Pure/CodegenPackage";
   310   val name = "Pure/codegen_package";
   311   type T = {
   311   type T = {
   312     modl: module,
   312     modl: module,
   313     gens: gens,
   313     gens: gens,
   314     logic_data: logic_data,
   314     logic_data: logic_data,
   315     target_data: target_data Symtab.table
   315     target_data: target_data Symtab.table
   539     fun defgen_class thy (tabs as (_, (insttab, _, _))) cls trns =
   539     fun defgen_class thy (tabs as (_, (insttab, _, _))) cls trns =
   540       case name_of_idf thy nsp_class cls
   540       case name_of_idf thy nsp_class cls
   541        of SOME cls =>
   541        of SOME cls =>
   542             let
   542             let
   543               val (v, cs) = (ClassPackage.the_consts_sign thy) cls;
   543               val (v, cs) = (ClassPackage.the_consts_sign thy) cls;
   544               val sortctxts = map (ClassPackage.extract_sortctxt thy o snd) cs;
   544               val sortctxts = map (ClassPackage.sortcontext_of_typ thy o snd) cs;
   545               val idfs = map (idf_of_name thy nsp_mem o fst) cs;
   545               val idfs = map (idf_of_name thy nsp_mem o fst) cs;
   546             in
   546             in
   547               trns
   547               trns
   548               |> debug_msg (fn _ => "trying defgen class declaration for " ^ quote cls)
   548               |> debug_msg (fn _ => "trying defgen class declaration for " ^ quote cls)
   549               |> fold_map (ensure_def_class thy tabs) (ClassPackage.the_superclasses thy cls)
   549               |> fold_map (ensure_def_class thy tabs) (ClassPackage.the_superclasses thy cls)
   621       trns
   621       trns
   622       |> fold_map (ensure_def_class thy tabs) clss
   622       |> fold_map (ensure_def_class thy tabs) clss
   623       |-> (fn clss => pair (Lookup (clss, (v |> unprefix "'", if j = 1 then ~1 else i))))
   623       |-> (fn clss => pair (Lookup (clss, (v |> unprefix "'", if j = 1 then ~1 else i))))
   624 and mk_fun thy tabs (c, ty) trns =
   624 and mk_fun thy tabs (c, ty) trns =
   625   case CodegenTheorems.get_funs thy (c, Logic.legacy_varifyT ty)  (* FIXME *)
   625   case CodegenTheorems.get_funs thy (c, Logic.legacy_varifyT ty)  (* FIXME *)
   626    of SOME (ty, eq_thms) =>
   626    of eq_thms as eq_thm :: _ =>
   627         let
   627         let
   628           val msg = cat_lines ("generating code for theorems " :: map string_of_thm eq_thms);
   628           val msg = cat_lines ("generating code for theorems " :: map string_of_thm eq_thms);
   629           val sortctxt = ClassPackage.extract_sortctxt thy ty;
   629           val ty = (Logic.legacy_unvarifyT o CodegenTheorems.extr_typ thy) eq_thm
       
   630           val sortcontext = ClassPackage.sortcontext_of_typ thy ty;
   630           fun dest_eqthm eq_thm =
   631           fun dest_eqthm eq_thm =
   631             let
   632             let
   632               val ((t, args), rhs) =
   633               val ((t, args), rhs) =
   633                 (apfst strip_comb o Logic.dest_equals o prop_of) eq_thm;
   634                 (apfst strip_comb o Logic.dest_equals o Logic.legacy_unvarify o prop_of) eq_thm;
   634             in case t
   635             in case t
   635              of Const (c', _) => if c' = c then (args, rhs)
   636              of Const (c', _) => if c' = c then (args, rhs)
   636                  else error ("illegal function equation for " ^ quote c
   637                  else error ("illegal function equation for " ^ quote c
   637                    ^ ", actually defining " ^ quote c')
   638                    ^ ", actually defining " ^ quote c')
   638               | _ => error ("illegal function equation for " ^ quote c)
   639               | _ => error ("illegal function equation for " ^ quote c)
   643             ||>> exprgen_term thy tabs rhs;
   644             ||>> exprgen_term thy tabs rhs;
   644         in
   645         in
   645           trns
   646           trns
   646           |> message msg (fn trns => trns
   647           |> message msg (fn trns => trns
   647           |> fold_map (exprgen_eq o dest_eqthm) eq_thms
   648           |> fold_map (exprgen_eq o dest_eqthm) eq_thms
       
   649           ||>> fold_map (exprgen_tyvar_sort thy tabs) sortcontext
   648           ||>> exprgen_type thy tabs ty
   650           ||>> exprgen_type thy tabs ty
   649           ||>> fold_map (exprgen_tyvar_sort thy tabs) sortctxt
   651           |-> (fn ((eqs, sortctxt), ty) => (pair o SOME) ((eqs, (sortctxt, ty)), map snd sortcontext)))
   650           |-> (fn ((eqs, ity), sortctxt) => (pair o SOME) ((eqs, (sortctxt, ity)), ty)))
       
   651         end
   652         end
   652     | NONE => (NONE, trns)
   653     | [] => (NONE, trns)
   653 and ensure_def_inst thy (tabs as (_, (insttab, _, _))) (cls, tyco) trns =
   654 and ensure_def_inst thy (tabs as (_, (insttab, _, _))) (cls, tyco) trns =
   654   let
   655   let
   655     fun defgen_inst thy (tabs as (_, (insttab, _, _))) inst trns =
   656     fun defgen_inst thy (tabs as (_, (insttab, _, _))) inst trns =
   656       case Option.map (InstNameMangler.rev thy insttab) (name_of_idf thy nsp_inst inst)
   657       case Option.map (InstNameMangler.rev thy insttab) (name_of_idf thy nsp_inst inst)
   657        of SOME (_, (class, tyco)) =>
   658        of SOME (_, (class, tyco)) =>
   658             let
   659             let
   659               val (arity, memdefs) = ClassPackage.the_inst_sign thy (class, tyco);
   660               val (arity, memdefs) = ClassPackage.the_inst_sign thy (class, tyco);
       
   661               val arity_typ = Type (tyco, (map TFree arity));
       
   662               val operational_arity = map_filter (fn (v, sort) => case ClassPackage.operational_sort_of thy sort
       
   663                of [] => NONE
       
   664                 | sort => SOME (v, sort)) arity;
   660               fun gen_suparity supclass trns =
   665               fun gen_suparity supclass trns =
   661                 trns
   666                 trns
   662                 |> ensure_def_class thy tabs supclass
   667                 |> ensure_def_class thy tabs supclass
   663                 ||>> ensure_def_inst thy tabs (supclass, tyco)
   668                 ||>> fold_map (exprgen_classlookup thy tabs)
   664                 ||>> (fold_map o fold_map) (exprgen_classlookup thy tabs)
   669                       (ClassPackage.sortlookup thy ([supclass], arity_typ));
   665                       (ClassPackage.extract_classlookup_inst thy (class, tyco) supclass)
       
   666                 |-> (fn ((supclass, inst), lss) => pair (supclass, (inst, lss)));
       
   667               fun gen_membr (m, ty) trns =
   670               fun gen_membr (m, ty) trns =
   668                 trns
   671                 trns
   669                 |> mk_fun thy tabs (m, ty)
   672                 |> mk_fun thy tabs (m, ty)
   670                 |-> (fn NONE => error ("could not derive definition for member "
   673                 |-> (fn NONE => error ("could not derive definition for member "
   671                           ^ quote m ^ " :: " ^ Sign.string_of_typ thy ty)
   674                           ^ quote m ^ " :: " ^ Sign.string_of_typ thy ty)
   672                       | SOME (funn, ty_decl) => (fold_map o fold_map) (exprgen_classlookup thy tabs)
   675                       | SOME (funn, sorts) => fold_map (fn (sort, sort_ctxt) =>
   673                        (ClassPackage.extract_classlookup_member thy (ty_decl, ty))
   676                           fold_map (exprgen_classlookup thy tabs)
       
   677                             (ClassPackage.sortlookup thy (sort, TFree sort_ctxt)))
       
   678                             (sorts ~~ operational_arity)
   674                 #-> (fn lss =>
   679                 #-> (fn lss =>
   675                        pair (idf_of_name thy nsp_mem m, ((idf_of_name thy nsp_instmem m, funn), lss))));
   680                        pair (idf_of_name thy nsp_mem m, ((idf_of_name thy nsp_instmem m, funn), lss))));
   676             in
   681             in
   677               trns
   682               trns
   678               |> debug_msg (fn _ => "trying defgen class instance for (" ^ quote cls
   683               |> debug_msg (fn _ => "trying defgen class instance for (" ^ quote cls
   781 and appgen_default thy tabs ((c, ty), ts) trns =
   786 and appgen_default thy tabs ((c, ty), ts) trns =
   782   trns
   787   trns
   783   |> ensure_def_const thy tabs (c, ty)
   788   |> ensure_def_const thy tabs (c, ty)
   784   ||>> exprgen_type thy tabs ty
   789   ||>> exprgen_type thy tabs ty
   785   ||>> (fold_map o fold_map) (exprgen_classlookup thy tabs)
   790   ||>> (fold_map o fold_map) (exprgen_classlookup thy tabs)
   786          (ClassPackage.extract_classlookup thy (c, ty))
   791          (ClassPackage.sortlookups_const thy (c, ty))
   787   ||>> fold_map (exprgen_term thy tabs) ts
   792   ||>> fold_map (exprgen_term thy tabs) ts
   788   |-> (fn (((c, ty), ls), es) =>
   793   |-> (fn (((c, ty), ls), es) =>
   789          pair (IConst (c, (ls, ty)) `$$ es))
   794          pair (IConst (c, (ls, ty)) `$$ es))
   790 and appgen thy tabs ((f, ty), ts) trns =
   795 and appgen thy tabs ((f, ty), ts) trns =
   791   case Symtab.lookup ((#appconst o #gens o CodegenData.get) thy) f
   796   case Symtab.lookup ((#appconst o #gens o CodegenData.get) thy) f
   876   in
   881   in
   877     trns
   882     trns
   878     |> ensure_def ((K o fail) "no extraction of wfrec") false ("generating wfrec") idf
   883     |> ensure_def ((K o fail) "no extraction of wfrec") false ("generating wfrec") idf
   879     |> exprgen_type thy tabs ty'
   884     |> exprgen_type thy tabs ty'
   880     ||>> (fold_map o fold_map) (exprgen_classlookup thy tabs)
   885     ||>> (fold_map o fold_map) (exprgen_classlookup thy tabs)
   881            (ClassPackage.extract_classlookup thy (c, ty))
   886            (ClassPackage.sortlookups_const thy (c, ty))
   882     ||>> exprgen_type thy tabs ty_def
   887     ||>> exprgen_type thy tabs ty_def
   883     ||>> exprgen_term thy tabs tf
   888     ||>> exprgen_term thy tabs tf
   884     ||>> exprgen_term thy tabs tx
   889     ||>> exprgen_term thy tabs tx
   885     |-> (fn ((((_, ls), ty), tf), tx) => pair (IConst (idf, (ls, ty)) `$ tf `$ tx))
   890     |-> (fn ((((_, ls), ty), tf), tx) => pair (IConst (idf, (ls, ty)) `$ tf `$ tx))
   886   end;
   891   end;
  1004       map_module (K CodegenThingol.empty_module) thy
  1009       map_module (K CodegenThingol.empty_module) thy
  1005   | purge_defs (SOME cs) thy =
  1010   | purge_defs (SOME cs) thy =
  1006       let
  1011       let
  1007         val tabs = mk_tabs thy NONE;
  1012         val tabs = mk_tabs thy NONE;
  1008         val idfs = map (idf_of_const' thy tabs) cs;
  1013         val idfs = map (idf_of_const' thy tabs) cs;
  1009         val _ = writeln ("purging " ^ commas idfs);
       
  1010         fun purge idfs modl =
  1014         fun purge idfs modl =
  1011           CodegenThingol.purge_module (filter (can (get_def modl)) idfs) modl
  1015           CodegenThingol.purge_module (filter (can (get_def modl)) idfs) modl
  1012       in
  1016       in
  1013         map_module (purge idfs) thy
  1017         map_module (purge idfs) thy
  1014       end;
  1018       end;