src/Pure/Tools/codegen_package.ML
changeset 19150 1457d810b408
parent 19136 00ade10f611d
child 19167 f237c0cb3882
equal deleted inserted replaced
19149:1c31769f9796 19150:1457d810b408
    36     -> theory -> theory;
    36     -> theory -> theory;
    37   val set_get_datatype: (theory -> string -> ((string * sort) list * (string * typ list) list) option)
    37   val set_get_datatype: (theory -> string -> ((string * sort) list * (string * typ list) list) option)
    38     -> theory -> theory;
    38     -> theory -> theory;
    39   val set_int_tyco: string -> theory -> theory;
    39   val set_int_tyco: string -> theory -> theory;
    40 
    40 
    41   val codegen_incr: term -> theory -> (CodegenThingol.iexpr * (string * CodegenThingol.def) list) * theory;
    41   val codegen_term: term -> theory -> CodegenThingol.iexpr * theory;
    42   val is_dtcon: string -> bool;
    42   val is_dtcon: string -> bool;
    43   val consts_of_idfs: theory -> string list -> (string * (string * typ)) list
    43   val consts_of_idfs: theory -> string list -> (string * typ) list;
    44 
    44   val idfs_of_consts: theory -> (string * typ) list -> string list;
       
    45   val get_root_module: theory -> CodegenThingol.module;
    45   val get_ml_fun_datatype: theory -> (string -> string)
    46   val get_ml_fun_datatype: theory -> (string -> string)
    46     -> ((string * CodegenThingol.funn) list -> Pretty.T)
    47     -> ((string * CodegenThingol.funn) list -> Pretty.T)
    47         * ((string * CodegenThingol.datatyp) list -> Pretty.T);
    48         * ((string * CodegenThingol.datatyp) list -> Pretty.T);
    48 
    49 
    49   val appgen_default: appgen;
    50   val appgen_default: appgen;
   186       |> cons c''
   187       |> cons c''
   187       |> space_implode "_"
   188       |> space_implode "_"
   188       |> curry (op ^ o swap) ((implode oo replicate) i "'")
   189       |> curry (op ^ o swap) ((implode oo replicate) i "'")
   189     end;
   190     end;
   190   fun is_valid _ _ = true;
   191   fun is_valid _ _ = true;
   191   fun maybe_unique thy (c, ty) = 
   192   fun maybe_unique thy (c, ty) =
   192     if is_overloaded thy c
   193     if is_overloaded thy c
   193       then NONE
   194       then NONE
   194       else (SOME o idf_of_name thy nsp_const) c;
   195       else (SOME o idf_of_name thy nsp_const) c;
   195   fun re_mangle thy idf =
   196   fun re_mangle thy idf =
   196    case name_of_idf thy nsp_const idf
   197    case name_of_idf thy nsp_const idf
   473     (fn (modl, gens, target_data, logic_data) =>
   474     (fn (modl, gens, target_data, logic_data) =>
   474        (modl,
   475        (modl,
   475         gens |> map_gens
   476         gens |> map_gens
   476           (fn (appconst, eqextrs) =>
   477           (fn (appconst, eqextrs) =>
   477             (appconst, eqextrs
   478             (appconst, eqextrs
   478              |> Output.update_warn (op =) ("overwriting existing equation extractor " ^ name)
   479     |> Output.update_warn (op =) ("overwriting existing equation extractor " ^ name)
   479                  (name, (eqx, stamp ())))),
   480                  (name, (eqx, stamp ())))),
   480              target_data, logic_data));
   481              target_data, logic_data));
   481 
   482 
   482 fun get_eqextrs thy tabs =
   483 fun get_eqextrs thy tabs =
   483   (map (fn (name, (eqx, _)) => (name, eqx thy tabs)) o #eqextrs o #gens o CodegenData.get) thy;
   484   (map (fn (name, (eqx, _)) => (name, eqx thy tabs)) o #eqextrs o #gens o CodegenData.get) thy;
   658 and exprgen_type thy tabs (TVar _) trns =
   659 and exprgen_type thy tabs (TVar _) trns =
   659       error "TVar encountered during code generation"
   660       error "TVar encountered during code generation"
   660   | exprgen_type thy tabs (TFree v_s) trns =
   661   | exprgen_type thy tabs (TFree v_s) trns =
   661       trns
   662       trns
   662       |> exprgen_tyvar_sort thy tabs v_s
   663       |> exprgen_tyvar_sort thy tabs v_s
   663       |-> (fn v_s => pair (IVarT v_s))
   664       |-> (fn (v, sort) => pair (IVarT v))
   664   | exprgen_type thy tabs (Type ("fun", [t1, t2])) trns =
   665   | exprgen_type thy tabs (Type ("fun", [t1, t2])) trns =
   665       trns
   666       trns
   666       |> exprgen_type thy tabs t1
   667       |> exprgen_type thy tabs t1
   667       ||>> exprgen_type thy tabs t2
   668       ||>> exprgen_type thy tabs t2
   668       |-> (fn (t1', t2') => pair (t1' `-> t2'))
   669       |-> (fn (t1', t2') => pair (t1' `-> t2'))
   905     ||>> exprgen_term thy tabs t_body
   906     ||>> exprgen_term thy tabs t_body
   906     |-> (fn ((e, abs), body) => pair (ICase (e, [(abs, body)])))
   907     |-> (fn ((e, abs), body) => pair (ICase (e, [(abs, body)])))
   907   end;
   908   end;
   908 
   909 
   909 fun appgen_split strip_abs thy tabs (c, [t2]) trns =
   910 fun appgen_split strip_abs thy tabs (c, [t2]) trns =
   910   let
   911   case strip_abs 1 (Const c $ t2)
   911     val ([p], body) = strip_abs 1 (Const c $ t2)
   912    of ([p], body) =>
   912   in
   913         trns
   913     trns
   914         |> exprgen_term thy tabs p
   914     |> exprgen_term thy tabs p
   915         ||>> exprgen_term thy tabs body
   915     ||>> exprgen_term thy tabs body
   916         |-> (fn (e, body) => pair (e `|-> body))
   916     |-> (fn (e, body) => pair (e `|-> body))
   917     | _ =>
   917   end;
   918         trns
       
   919         |> appgen_default thy tabs (c, [t2]);
   918 
   920 
   919 fun appgen_number_of mk_int_to_nat bin_to_int tyco_int tyco_nat thy tabs ((_,
   921 fun appgen_number_of mk_int_to_nat bin_to_int tyco_int tyco_nat thy tabs ((_,
   920   Type (_, [_, ty as Type (tyco, [])])), [bin]) trns =
   922   Type (_, [_, ty as Type (tyco, [])])), [bin]) trns =
   921     if tyco = tyco_int then
   923     if tyco = tyco_int then
   922       trns
   924       trns
  1072             end) ((#2 o #constants o Consts.dest o #consts o Sign.rep_sg) thy)
  1074             end) ((#2 o #constants o Consts.dest o #consts o Sign.rep_sg) thy)
  1073       |> (fn (overltab1, overltab2) =>
  1075       |> (fn (overltab1, overltab2) =>
  1074             let
  1076             let
  1075               val c = "op =";
  1077               val c = "op =";
  1076               val ty = Sign.the_const_type thy c;
  1078               val ty = Sign.the_const_type thy c;
  1077               fun inst dtco = 
  1079               fun inst dtco =
  1078                 map_atyps (fn _ => Type (dtco,
  1080                 map_atyps (fn _ => Type (dtco,
  1079                   (map (fn (v, sort) => TVar ((v, 0), sort)) o fst o the o get_datatype thy) dtco)) ty
  1081                   (map (fn (v, sort) => TVar ((v, 0), sort)) o fst o the o get_datatype thy) dtco)) ty
  1080               val dtcos = fold (insert (op =) o snd) (get_all_datatype_cons thy) [];
  1082               val dtcos = fold (insert (op =) o snd) (get_all_datatype_cons thy) [];
  1081               val tys = map inst dtcos;
  1083               val tys = map inst dtcos;
  1082             in
  1084             in
  1094                 val key = ((NameSpace.drop_base o NameSpace.drop_base) co, NameSpace.base co);
  1096                 val key = ((NameSpace.drop_base o NameSpace.drop_base) co, NameSpace.base co);
  1095               in AList.default (op =) (key, []) #> AList.map_entry (op =) key (cons (co, dtco)) end
  1097               in AList.default (op =) (key, []) #> AList.map_entry (op =) key (cons (co, dtco)) end
  1096             ) (get_all_datatype_cons thy) [])
  1098             ) (get_all_datatype_cons thy) [])
  1097       |-> (fn _ => I);
  1099       |-> (fn _ => I);
  1098     fun mk_clsmemtab thy =
  1100     fun mk_clsmemtab thy =
  1099       Symtab.empty
  1101    Symtab.empty
  1100       |> Symtab.fold
  1102       |> Symtab.fold
  1101           (fn (class, (clsmems, _)) => fold
  1103           (fn (class, (clsmems, _)) => fold
  1102             (fn clsmem => Symtab.update (clsmem, class)) clsmems)
  1104             (fn clsmem => Symtab.update (clsmem, class)) clsmems)
  1103               (ClassPackage.get_classtab thy);
  1105               (ClassPackage.get_classtab thy);
  1104     val deftab = extract_defs thy;
  1106     val deftab = extract_defs thy;
  1157         add_case_const_i get_case_const_data case_c thy;
  1159         add_case_const_i get_case_const_data case_c thy;
  1158   in
  1160   in
  1159     fold ensure (get_datatype_case_consts thy) thy
  1161     fold ensure (get_datatype_case_consts thy) thy
  1160   end;
  1162   end;
  1161 
  1163 
  1162 fun codegen_incr t thy =
  1164 fun codegen_term t thy =
  1163   thy
  1165   thy
  1164   |> `(#modl o CodegenData.get)
  1166   |> expand_module NONE exprsgen_term [t]
  1165   ||>> expand_module NONE exprsgen_term [t]
  1167   |-> (fn [t] => pair t);
  1166   ||>> `(#modl o CodegenData.get)
       
  1167   |-> (fn ((modl_old, [t]), modl_new) => pair (t, CodegenThingol.diff_module (modl_new, modl_old)));
       
  1168 
  1168 
  1169 val is_dtcon = has_nsp nsp_dtcon;
  1169 val is_dtcon = has_nsp nsp_dtcon;
  1170 
  1170 
  1171 fun consts_of_idfs thy =
  1171 fun consts_of_idfs thy =
  1172   let
  1172   map (the o recconst_of_idf thy (mk_tabs thy));
  1173     val tabs = mk_tabs thy;
  1173 
  1174   in
  1174 fun idfs_of_consts thy =
  1175     map (fn idf => (idf, (the o recconst_of_idf thy tabs) idf))
  1175   map (idf_of_const thy (mk_tabs thy));
  1176   end;
  1176 
       
  1177 val get_root_module = (#modl o CodegenData.get);
  1177 
  1178 
  1178 fun get_ml_fun_datatype thy resolv =
  1179 fun get_ml_fun_datatype thy resolv =
  1179   let
  1180   let
  1180     val target_data = 
  1181     val target_data =
  1181       ((fn data => (the o Symtab.lookup data) "ml") o #target_data o CodegenData.get) thy;
  1182       ((fn data => (the o Symtab.lookup data) "ml") o #target_data o CodegenData.get) thy;
  1182   in
  1183   in
  1183     CodegenSerializer.ml_fun_datatype (nsp_dtcon, nsp_class, K false)
  1184     CodegenSerializer.ml_fun_datatype (nsp_dtcon, nsp_class, K false)
  1184       ((Option.map fst oo Symtab.lookup o #syntax_tyco) target_data,
  1185       ((Option.map fst oo Symtab.lookup o #syntax_tyco) target_data,
  1185       (Option.map fst oo Symtab.lookup o #syntax_const) target_data)
  1186       (Option.map fst oo Symtab.lookup o #syntax_const) target_data)
  1340 
  1341 
  1341 
  1342 
  1342 (** toplevel interface **)
  1343 (** toplevel interface **)
  1343 
  1344 
  1344 local
  1345 local
  1345  
  1346 
  1346 fun generate_code (SOME raw_consts) thy =
  1347 fun generate_code (SOME raw_consts) thy =
  1347       let
  1348    let
  1348         val consts = map (read_const thy) raw_consts;
  1349         val consts = map (read_const thy) raw_consts;
  1349       in
  1350       in
  1350         thy
  1351         thy
  1351         |> expand_module NONE (fold_map oo ensure_def_const) consts
  1352         |> expand_module NONE (fold_map oo ensure_def_const) consts
  1352         |-> (fn cs => pair (SOME cs))
  1353         |-> (fn cs => pair (SOME cs))