src/Pure/Tools/codegen_package.ML
changeset 19042 630b8dd0b31a
parent 19038 62c5f7591a43
child 19111 1f6112de1d0f
equal deleted inserted replaced
19041:1a8f08f9f8af 19042:630b8dd0b31a
    36   val set_get_all_datatype_cons : (theory -> (string * string) list)
    36   val set_get_all_datatype_cons : (theory -> (string * string) list)
    37     -> theory -> theory;
    37     -> theory -> theory;
    38   val set_get_datatype: (theory -> string -> ((string * sort) list * (string * typ list) list) option)
    38   val set_get_datatype: (theory -> string -> ((string * sort) list * (string * typ list) list) option)
    39     -> theory -> theory;
    39     -> theory -> theory;
    40   val set_int_tyco: string -> theory -> theory;
    40   val set_int_tyco: string -> theory -> theory;
       
    41 
       
    42   val codegen_incr: term -> theory -> (string * CodegenThingol.def) list * theory;
       
    43   val get_ml_fun_datatype: theory -> (string -> string)
       
    44     -> ((string * CodegenThingol.funn) list -> Pretty.T)
       
    45         * ((string * CodegenThingol.datatyp) list -> Pretty.T);
    41 
    46 
    42   val appgen_default: appgen;
    47   val appgen_default: appgen;
    43   val appgen_let: (int -> term -> term list * term)
    48   val appgen_let: (int -> term -> term list * term)
    44     -> appgen;
    49     -> appgen;
    45   val appgen_split: (int -> term -> term list * term)
    50   val appgen_split: (int -> term -> term list * term)
   498   (serializers := (
   503   (serializers := (
   499     ! serializers
   504     ! serializers
   500     |> Symtab.update (
   505     |> Symtab.update (
   501          #ml CodegenSerializer.serializers
   506          #ml CodegenSerializer.serializers
   502          |> apsnd (fn seri => seri
   507          |> apsnd (fn seri => seri
   503             (nsp_dtcon, nsp_class, fn tyco' => tyco' = idf_of_name thy nsp_tyco tyco )
   508             (nsp_dtcon, nsp_class, fn tyco' => tyco' = idf_of_name thy nsp_tyco tyco)
   504               [[nsp_module], [nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_mem, nsp_inst]]
   509               [[nsp_module], [nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_mem, nsp_inst]]
   505             )
   510             )
   506        )
   511        )
   507     ); thy);
   512     ); thy);
   508 
   513 
   589               val idfs = map (idf_of_name thy nsp_mem o fst) cs;
   594               val idfs = map (idf_of_name thy nsp_mem o fst) cs;
   590             in
   595             in
   591               trns
   596               trns
   592               |> debug 5 (fn _ => "trying defgen class declaration for " ^ quote cls)
   597               |> debug 5 (fn _ => "trying defgen class declaration for " ^ quote cls)
   593               |> fold_map (ensure_def_class thy tabs) (ClassPackage.the_superclasses thy cls)
   598               |> fold_map (ensure_def_class thy tabs) (ClassPackage.the_superclasses thy cls)
   594               ||>> (codegen_type thy tabs o map snd) cs
   599               ||>> (exprsgen_type thy tabs o map snd) cs
   595               ||>> (fold_map o fold_map) (exprgen_tyvar_sort thy tabs) sortctxts
   600               ||>> (fold_map o fold_map) (exprgen_tyvar_sort thy tabs) sortctxts
   596               |-> (fn ((supcls, memtypes), sortctxts) => succeed
   601               |-> (fn ((supcls, memtypes), sortctxts) => succeed
   597                 (Class (supcls, ("a", idfs ~~ (sortctxts ~~ memtypes)))))
   602                 (Class (supcls, ("a", idfs ~~ (sortctxts ~~ memtypes)))))
   598             end
   603             end
   599         | _ =>
   604         | _ =>
   618                       idf_of_name thy nsp_dtcon, tys)) cos;
   623                       idf_of_name thy nsp_dtcon, tys)) cos;
   619                   in
   624                   in
   620                     trns
   625                     trns
   621                     |> debug 5 (fn _ => "trying defgen datatype for " ^ quote dtco)
   626                     |> debug 5 (fn _ => "trying defgen datatype for " ^ quote dtco)
   622                     |> fold_map (exprgen_tyvar_sort thy tabs) vars
   627                     |> fold_map (exprgen_tyvar_sort thy tabs) vars
   623                     ||>> fold_map (fn (c, ty) => codegen_type thy tabs ty #-> (fn ty' => pair (c, ty'))) cos'
   628                     ||>> fold_map (fn (c, ty) => exprsgen_type thy tabs ty #-> (fn ty' => pair (c, ty'))) cos'
   624                     |-> (fn (sorts, cos'') => succeed (Datatype
   629                     |-> (fn (sorts, cos'') => succeed (Datatype
   625                          (sorts, cos'')))
   630                          (sorts, cos'')))
   626                   end
   631                   end
   627               | NONE =>
   632               | NONE =>
   628                   trns
   633                   trns
   655   | exprgen_type thy tabs (Type (tyco, tys)) trns =
   660   | exprgen_type thy tabs (Type (tyco, tys)) trns =
   656       trns
   661       trns
   657       |> ensure_def_tyco thy tabs tyco
   662       |> ensure_def_tyco thy tabs tyco
   658       ||>> fold_map (exprgen_type thy tabs) tys
   663       ||>> fold_map (exprgen_type thy tabs) tys
   659       |-> (fn (tyco, tys) => pair (tyco `%% tys))
   664       |-> (fn (tyco, tys) => pair (tyco `%% tys))
   660 and codegen_type thy tabs =
   665 and exprsgen_type thy tabs =
   661   fold_map (exprgen_type thy tabs) o snd o devarify_typs;
   666   fold_map (exprgen_type thy tabs) o snd o devarify_typs;
   662 
   667 
   663 fun exprgen_classlookup thy tabs (ClassPackage.Instance (inst, ls)) trns =
   668 fun exprgen_classlookup thy tabs (ClassPackage.Instance (inst, ls)) trns =
   664       trns
   669       trns
   665       |> ensure_def_inst thy tabs inst
   670       |> ensure_def_inst thy tabs inst
   693               then error ("inconsistent type for default rule")
   698               then error ("inconsistent type for default rule")
   694               else (map2 (curry Free) vs tys, t)
   699               else (map2 (curry Free) vs tys, t)
   695             end;
   700             end;
   696         in
   701         in
   697           trns
   702           trns
   698           |> (codegen_eqs thy tabs o map dest_eqthm) eq_thms
   703           |> (exprsgen_eqs thy tabs o map dest_eqthm) eq_thms
   699           ||>> (codegen_eqs thy tabs o the_list o Option.map mk_default) default
   704           ||>> (exprsgen_eqs thy tabs o the_list o Option.map mk_default) default
   700           ||>> codegen_type thy tabs [ty]
   705           ||>> exprsgen_type thy tabs [ty]
   701           ||>> fold_map (exprgen_tyvar_sort thy tabs) sortctxt
   706           ||>> fold_map (exprgen_tyvar_sort thy tabs) sortctxt
   702           |-> (fn (((eqs, eq_default), [ty]), sortctxt) => (pair o SOME) (eqs @ eq_default, (sortctxt, ty)))
   707           |-> (fn (((eqs, eq_default), [ty]), sortctxt) => (pair o SOME) (eqs @ eq_default, (sortctxt, ty)))
   703         end
   708         end
   704     | NONE => (NONE, trns)
   709     | NONE => (NONE, trns)
   705 and ensure_def_inst thy (tabs as (_, (insttab, _, _))) (cls, tyco) trns =
   710 and ensure_def_inst thy (tabs as (_, (insttab, _, _))) (cls, tyco) trns =
   823             trns
   828             trns
   824             |> exprgen_term thy tabs t'
   829             |> exprgen_term thy tabs t'
   825             ||>> fold_map (exprgen_term thy tabs) ts
   830             ||>> fold_map (exprgen_term thy tabs) ts
   826             |-> (fn (e, es) => pair (e `$$ es))
   831             |-> (fn (e, es) => pair (e `$$ es))
   827       end
   832       end
   828 and codegen_term thy tabs =
   833 and exprsgen_term thy tabs =
   829   fold_map (exprgen_term thy tabs) o snd o devarify_term_typs
   834   fold_map (exprgen_term thy tabs) o snd o devarify_term_typs
   830 and codegen_eqs thy tabs =
   835 and exprsgen_eqs thy tabs =
   831   apfst (map (fn (rhs::args) => (args, rhs)))
   836   apfst (map (fn (rhs::args) => (args, rhs)))
   832     oo fold_burrow (codegen_term thy tabs)
   837     oo fold_burrow (exprsgen_term thy tabs)
   833     o map (fn (args, rhs) => (rhs :: args))
   838     o map (fn (args, rhs) => (rhs :: args))
   834 and appgen_default thy tabs ((c, ty), ts) trns =
   839 and appgen_default thy tabs ((c, ty), ts) trns =
   835   trns
   840   trns
   836   |> ensure_def_const thy tabs (c, ty)
   841   |> ensure_def_const thy tabs (c, ty)
   837   ||>> (fold_map o fold_map) (exprgen_classlookup thy tabs)
   842   ||>> (fold_map o fold_map) (exprgen_classlookup thy tabs)
   838          (ClassPackage.extract_classlookup thy (c, ty))
   843          (ClassPackage.extract_classlookup thy (c, ty))
   839   ||>> codegen_type thy tabs [ty]
   844   ||>> exprsgen_type thy tabs [ty]
   840   ||>> fold_map (exprgen_term thy tabs) ts
   845   ||>> fold_map (exprgen_term thy tabs) ts
   841   |-> (fn (((c, ls), [ty]), es) =>
   846   |-> (fn (((c, ls), [ty]), es) =>
   842          pair (IConst ((c, ty), ls) `$$ es))
   847          pair (IConst ((c, ty), ls) `$$ es))
   843 and appgen thy tabs ((f, ty), ts) trns =
   848 and appgen thy tabs ((f, ty), ts) trns =
   844   case Symtab.lookup ((#appconst o #gens o CodegenData.get) thy) f
   849   case Symtab.lookup ((#appconst o #gens o CodegenData.get) thy) f
   923     trns
   928     trns
   924     |> gen_ensure_def [("wfrec", (K o succeed) Undef)] ("generating wfrec") idf
   929     |> gen_ensure_def [("wfrec", (K o succeed) Undef)] ("generating wfrec") idf
   925     |> exprgen_type thy tabs ty'
   930     |> exprgen_type thy tabs ty'
   926     ||>> (fold_map o fold_map) (exprgen_classlookup thy tabs)
   931     ||>> (fold_map o fold_map) (exprgen_classlookup thy tabs)
   927            (ClassPackage.extract_classlookup thy (c, ty))
   932            (ClassPackage.extract_classlookup thy (c, ty))
   928     ||>> codegen_type thy tabs [ty_def]
   933     ||>> exprsgen_type thy tabs [ty_def]
   929     ||>> exprgen_term thy tabs tf
   934     ||>> exprgen_term thy tabs tf
   930     ||>> exprgen_term thy tabs tx
   935     ||>> exprgen_term thy tabs tx
   931     |-> (fn ((((_, ls), [ty]), tf), tx) => pair (IConst ((idf, ty), ls) `$ tf `$ tx))
   936     |-> (fn ((((_, ls), [ty]), tf), tx) => pair (IConst ((idf, ty), ls) `$ tf `$ tx))
   932   end;
   937   end;
   933 
   938 
   986 
   991 
   987 (** theory interface **)
   992 (** theory interface **)
   988 
   993 
   989 fun mk_tabs thy =
   994 fun mk_tabs thy =
   990   let
   995   let
       
   996     fun get_specifications thy c =
       
   997       Defs.specifications_of (Theory.defs_of thy) c;
   991     fun extract_defs thy =
   998     fun extract_defs thy =
   992       let
   999       let
   993         fun dest thm =
  1000         fun dest thm =
   994           let
  1001           let
   995             val (lhs, rhs) = Logic.dest_equals (prop_of thm);
  1002             val (lhs, rhs) = Logic.dest_equals (prop_of thm);
  1035          |> Symtab.update_new (c, (ty, tys)),
  1042          |> Symtab.update_new (c, (ty, tys)),
  1036          overltab2
  1043          overltab2
  1037          |> fold (fn ty' => ConstNameMangler.declare (thy, deftab)
  1044          |> fold (fn ty' => ConstNameMangler.declare (thy, deftab)
  1038               (c, (ty, ty')) #> snd) tys)
  1045               (c, (ty, ty')) #> snd) tys)
  1039       end;
  1046       end;
       
  1047     (* über *alle*: (map fst o NameSpace.dest_table o Consts.space_of o Sign.consts_of) thy
       
  1048        * (c, ty) reicht dann zur zünftigen Deklaration
       
  1049        * somit fliegt ein Haufen Grusch raus, deftab bleibt allerdings wegen thyname
       
  1050       fun mk_overltabs thy =
       
  1051       (Symtab.empty, ConstNameMangler.empty)
       
  1052       |> Symtab.fold
       
  1053           (fn c => if (is_none o ClassPackage.lookup_const_class thy) c
       
  1054             then case get_specifications thy c
       
  1055              of [_] => NONE
       
  1056               | tys => fold
       
  1057                 (fn (overltab1, overltab2) => (
       
  1058                     overltab1
       
  1059                     |> Symtab.update_new (c, (Sign.the_const_type thy c, tys)),
       
  1060                     overltab2
       
  1061                     |> fold (fn (ty, (_, thyname)) => ConstNameMangler.declare (thy, deftab)
       
  1062                          (c, (Sign.the_const_type thy c, ty)) #> snd) tys))
       
  1063                 else I
       
  1064           ) deftab
       
  1065       |> add_monoeq thy deftab;*)
  1040     fun mk_overltabs thy deftab =
  1066     fun mk_overltabs thy deftab =
  1041       (Symtab.empty, ConstNameMangler.empty)
  1067       (Symtab.empty, ConstNameMangler.empty)
  1042       |> Symtab.fold
  1068       |> Symtab.fold
  1043           (fn (c, [_]) => I
  1069           (fn (c, [_]) => I
  1044             | (c, tytab) =>
  1070             | (c, tytab) =>
  1124         add_case_const_i get_case_const_data case_c thy;
  1150         add_case_const_i get_case_const_data case_c thy;
  1125   in
  1151   in
  1126     fold ensure (get_datatype_case_consts thy) thy
  1152     fold ensure (get_datatype_case_consts thy) thy
  1127   end;
  1153   end;
  1128 
  1154 
       
  1155 fun codegen_incr t thy =
       
  1156   thy
       
  1157   |> `(#modl o CodegenData.get)
       
  1158   ||>> expand_module NONE (fn thy => fn tabs => exprsgen_term thy tabs [t])
       
  1159   ||>> `(#modl o CodegenData.get)
       
  1160   |-> (fn ((modl_old, _), modl_new) => pair (CodegenThingol.diff_module (modl_new, modl_old)));
       
  1161 
       
  1162 fun get_ml_fun_datatype thy resolv =
       
  1163   let
       
  1164     val target_data = 
       
  1165       ((fn data => (the o Symtab.lookup data) "ml") o #target_data o CodegenData.get) thy;
       
  1166   in
       
  1167     CodegenSerializer.ml_fun_datatype (nsp_dtcon, nsp_class, K false)
       
  1168       ((Option.map fst oo Symtab.lookup o #syntax_tyco) target_data,
       
  1169       (Option.map fst oo Symtab.lookup o #syntax_const) target_data)
       
  1170       resolv
       
  1171   end;
  1129 
  1172 
  1130 
  1173 
  1131 (** target languages **)
  1174 (** target languages **)
  1132 
  1175 
  1133 (* primitive definitions *)
  1176 (* primitive definitions *)
  1198   let
  1241   let
  1199     fun check_tyco thy tyco =
  1242     fun check_tyco thy tyco =
  1200       if Sign.declared_tyname thy tyco
  1243       if Sign.declared_tyname thy tyco
  1201       then tyco
  1244       then tyco
  1202       else error ("no such type constructor: " ^ quote tyco);
  1245       else error ("no such type constructor: " ^ quote tyco);
  1203     fun prep_tyco thy tyco =
  1246     fun prep_tyco thy raw_tyco =
  1204       tyco
  1247       raw_tyco
  1205       |> Sign.intern_type thy
  1248       |> Sign.intern_type thy
  1206       |> check_tyco thy
  1249       |> check_tyco thy
  1207       |> idf_of_name thy nsp_tyco;
  1250       |> idf_of_name thy nsp_tyco;
       
  1251     fun no_args_tyco thy raw_tyco =
       
  1252       AList.lookup (op =) ((NameSpace.dest_table o #types o Type.rep_tsig o Sign.tsig_of) thy)
       
  1253         (Sign.intern_type thy raw_tyco)
       
  1254       |> (fn SOME ((Type.LogicalType i), _) => i);
  1208     fun mk reader target thy =
  1255     fun mk reader target thy =
  1209       let
  1256       let
  1210         val _ = get_serializer target;
  1257         val _ = get_serializer target;
  1211         val tyco = prep_tyco thy raw_tyco;
  1258         val tyco = prep_tyco thy raw_tyco;
  1212       in
  1259       in
  1223                       (tyco, (pretty, stamp ())),
  1270                       (tyco, (pretty, stamp ())),
  1224                     syntax_const))),
  1271                     syntax_const))),
  1225               logic_data)))
  1272               logic_data)))
  1226       end;
  1273       end;
  1227   in
  1274   in
  1228     CodegenSerializer.parse_syntax (read_quote (fn thy => prep_tyco thy raw_tyco) read_typ codegen_type)
  1275     CodegenSerializer.parse_syntax (fn thy => no_args_tyco thy raw_tyco)
       
  1276     (read_quote (fn thy => prep_tyco thy raw_tyco) read_typ exprsgen_type)
  1229     #-> (fn reader => pair (mk reader))
  1277     #-> (fn reader => pair (mk reader))
  1230   end;
  1278   end;
  1231 
  1279 
  1232 fun add_pretty_syntax_const c target pretty =
  1280 fun add_pretty_syntax_const c target pretty =
  1233   map_codegen_data
  1281   map_codegen_data
  1244 
  1292 
  1245 fun parse_syntax_const raw_const =
  1293 fun parse_syntax_const raw_const =
  1246   let
  1294   let
  1247     fun prep_const thy raw_const =
  1295     fun prep_const thy raw_const =
  1248       idf_of_const thy (mk_tabs thy) (read_const thy raw_const);
  1296       idf_of_const thy (mk_tabs thy) (read_const thy raw_const);
       
  1297     fun no_args_const thy raw_const =
       
  1298       (length o fst o strip_type o snd o read_const thy) raw_const;
  1249     fun mk reader target thy =
  1299     fun mk reader target thy =
  1250       let
  1300       let
  1251         val _ = get_serializer target;
  1301         val _ = get_serializer target;
  1252         val c = prep_const thy raw_const;
  1302         val c = prep_const thy raw_const;
  1253       in
  1303       in
  1255         |> ensure_prim c target
  1305         |> ensure_prim c target
  1256         |> reader
  1306         |> reader
  1257         |-> (fn pretty => add_pretty_syntax_const c target pretty)
  1307         |-> (fn pretty => add_pretty_syntax_const c target pretty)
  1258       end;
  1308       end;
  1259   in
  1309   in
  1260     CodegenSerializer.parse_syntax (read_quote (fn thy => prep_const thy raw_const) Sign.read_term codegen_term)
  1310     CodegenSerializer.parse_syntax (fn thy => no_args_const thy raw_const)
       
  1311       (read_quote (fn thy => prep_const thy raw_const) Sign.read_term exprsgen_term)
  1261     #-> (fn reader => pair (mk reader))
  1312     #-> (fn reader => pair (mk reader))
  1262   end;
  1313   end;
  1263 
  1314 
  1264 fun add_pretty_list raw_nil raw_cons (target, seri) thy =
  1315 fun add_pretty_list raw_nil raw_cons (target, seri) thy =
  1265   let
  1316   let