src/Pure/Tools/codegen_package.ML
changeset 20175 0a8ca32f6e64
parent 20105 454f4be984b7
child 20183 fd546b0c8a7c
equal deleted inserted replaced
20174:c057b3618963 20175:0a8ca32f6e64
    18         * ((string * CodegenThingol.datatyp) list -> Pretty.T);
    18         * ((string * CodegenThingol.datatyp) list -> Pretty.T);
    19 
    19 
    20   val add_pretty_list: string -> string -> string * (int * string)
    20   val add_pretty_list: string -> string -> string * (int * string)
    21     -> theory -> theory;
    21     -> theory -> theory;
    22   val add_alias: string * string -> theory -> theory;
    22   val add_alias: string * string -> theory -> theory;
    23   val set_get_all_datatype_cons : (theory -> (string * string) list)
    23   val set_get_all_datatype_cons: (theory -> (string * string) list)
    24     -> theory -> theory;
    24     -> theory -> theory;
    25   val set_int_tyco: string -> theory -> theory;
    25   val purge_code: theory -> theory;
    26 
    26 
    27   type appgen;
    27   type appgen;
    28   val add_appconst: xstring * appgen -> theory -> theory;
    28   val add_appconst: xstring * appgen -> theory -> theory;
    29   val add_appconst_i: string * appgen -> theory -> theory;
    29   val add_appconst_i: string * appgen -> theory -> theory;
    30   val appgen_default: appgen;
    30   val appgen_default: appgen;
   242 val serializers = ref (
   242 val serializers = ref (
   243   Symtab.empty
   243   Symtab.empty
   244   |> Symtab.update (
   244   |> Symtab.update (
   245        #ml CodegenSerializer.serializers
   245        #ml CodegenSerializer.serializers
   246        |> apsnd (fn seri => seri
   246        |> apsnd (fn seri => seri
   247             (nsp_dtcon, nsp_class, K false)
   247             (nsp_dtcon, nsp_class)
   248             [[nsp_module], [nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_class, nsp_mem, nsp_inst, nsp_instmem]]
   248             [[nsp_module], [nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_class, nsp_mem, nsp_inst, nsp_instmem]]
   249           )
   249           )
   250      )
   250      )
   251   |> Symtab.update (
   251   |> Symtab.update (
   252        #haskell CodegenSerializer.serializers
   252        #haskell CodegenSerializer.serializers
   371       in CodegenData.put { modl = modl, appgens = appgens,
   371       in CodegenData.put { modl = modl, appgens = appgens,
   372            target_data = target_data, logic_data = logic_data } thy end;
   372            target_data = target_data, logic_data = logic_data } thy end;
   373 
   373 
   374 val print_code = CodegenData.print;
   374 val print_code = CodegenData.print;
   375 
   375 
       
   376 val purge_code = map_codegen_data (fn (_, appgens, target_data, logic_data) => 
       
   377   (empty_module, appgens, target_data, logic_data));
   376 
   378 
   377 (* advanced name handling *)
   379 (* advanced name handling *)
   378 
   380 
   379 fun add_alias (src, dst) =
   381 fun add_alias (src, dst) =
   380   map_codegen_data
   382   map_codegen_data
   467 (* further theory data accessors *)
   469 (* further theory data accessors *)
   468 
   470 
   469 fun gen_add_appconst prep_const (raw_c, appgen) thy =
   471 fun gen_add_appconst prep_const (raw_c, appgen) thy =
   470   let
   472   let
   471     val c = prep_const thy raw_c;
   473     val c = prep_const thy raw_c;
   472     val _ = writeln c;
       
   473     val i = (length o fst o strip_type o Sign.the_const_type thy) c
   474     val i = (length o fst o strip_type o Sign.the_const_type thy) c
   474     val _ = (writeln o string_of_int) i;
       
   475   in map_codegen_data
   475   in map_codegen_data
   476     (fn (modl, appgens, target_data, logic_data) =>
   476     (fn (modl, appgens, target_data, logic_data) =>
   477        (modl,
   477        (modl,
   478         appgens |> Symtab.update (c, (i, (appgen, stamp ()))),
   478         appgens |> Symtab.update (c, (i, (appgen, stamp ()))),
   479         target_data, logic_data)) thy
   479         target_data, logic_data)) thy
   505                   val (vars, cos) = (fst o the o CodegenTheorems.get_datatypes thy) dtco
   505                   val (vars, cos) = (fst o the o CodegenTheorems.get_datatypes thy) dtco
   506                 in
   506                 in
   507                   SOME (c, (the o AList.lookup (op =) cos) c ---> Type (dtco, map TFree vars) |> Logic.varifyT)
   507                   SOME (c, (the o AList.lookup (op =) cos) c ---> Type (dtco, map TFree vars) |> Logic.varifyT)
   508                 end
   508                 end
   509             | NONE => NONE;
   509             | NONE => NONE;
   510 
       
   511 fun set_int_tyco tyco thy =
       
   512   (serializers := (
       
   513     ! serializers
       
   514     |> Symtab.update (
       
   515          #ml CodegenSerializer.serializers
       
   516          |> apsnd (fn seri => seri
       
   517             (nsp_dtcon, nsp_class, fn tyco' => tyco' = idf_of_name thy nsp_tyco tyco)
       
   518               [[nsp_module], [nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_mem, nsp_inst, nsp_instmem]]
       
   519             )
       
   520        )
       
   521     ); thy);
       
   522 
   510 
   523 
   511 
   524 (* definition and expression generators *)
   512 (* definition and expression generators *)
   525 
   513 
   526 fun check_strict thy f x ((false, _, _), _) =
   514 fun check_strict thy f x ((false, _, _), _) =
   672                 |> ensure_def_class thy tabs supclass
   660                 |> ensure_def_class thy tabs supclass
   673                 ||>> fold_map (exprgen_classlookup thy tabs)
   661                 ||>> fold_map (exprgen_classlookup thy tabs)
   674                       (ClassPackage.sortlookup thy ([supclass], arity_typ));
   662                       (ClassPackage.sortlookup thy ([supclass], arity_typ));
   675               fun gen_membr (m, ty) trns =
   663               fun gen_membr (m, ty) trns =
   676                 trns
   664                 trns
   677                 |> tap (fn _ => writeln ("(1) " ^ m))
       
   678                 |> mk_fun thy tabs (m, ty)
   665                 |> mk_fun thy tabs (m, ty)
   679                 |> tap (fn _ => writeln "(2)")
       
   680                 |-> (fn NONE => error ("could not derive definition for member "
   666                 |-> (fn NONE => error ("could not derive definition for member "
   681                           ^ quote m ^ " :: " ^ Sign.string_of_typ thy ty)
   667                           ^ quote m ^ " :: " ^ Sign.string_of_typ thy ty)
   682                       | SOME (funn, sorts) => fold_map (fn (sort, sort_ctxt) =>
   668                       | SOME (funn, sorts) => fold_map (fn (sort, sort_ctxt) =>
   683                           fold_map (exprgen_classlookup thy tabs)
   669                           fold_map (exprgen_classlookup thy tabs)
   684                             (ClassPackage.sortlookup thy (sort, TFree sort_ctxt)))
   670                             (ClassPackage.sortlookup thy (sort, TFree sort_ctxt)))
   685                             (print sorts ~~ print operational_arity)
   671                             (print sorts ~~ print operational_arity)
   686                 #> tap (fn _ => writeln "(3)")
       
   687                 #-> (fn lss =>
   672                 #-> (fn lss =>
   688                        pair (idf_of_name thy nsp_mem m, ((idf_of_name thy nsp_instmem m, funn), lss))));
   673                        pair (idf_of_name thy nsp_mem m, ((idf_of_name thy nsp_instmem m, funn), lss))));
   689             in
   674             in
   690               trns
   675               trns
   691               |> debug_msg (fn _ => "trying defgen class instance for (" ^ quote cls
   676               |> debug_msg (fn _ => "trying defgen class instance for (" ^ quote cls
   769       trns
   754       trns
   770       |> exprgen_type thy tabs ty
   755       |> exprgen_type thy tabs ty
   771       |-> (fn ty => pair (IVar v))
   756       |-> (fn ty => pair (IVar v))
   772   | exprgen_term thy tabs (Abs (raw_v, ty, raw_t)) trns =
   757   | exprgen_term thy tabs (Abs (raw_v, ty, raw_t)) trns =
   773       let
   758       let
   774         val (v, t) = Term.variant_abs (CodegenThingol.proper_name raw_v, ty, raw_t);
   759         val (v, t) = Term.variant_abs (Name.alphanum raw_v, ty, raw_t);
   775       in
   760       in
   776         trns
   761         trns
   777         |> exprgen_type thy tabs ty
   762         |> exprgen_type thy tabs ty
   778         ||>> exprgen_term thy tabs t
   763         ||>> exprgen_term thy tabs t
   779         |-> (fn (ty, e) => pair ((v, ty) `|-> e))
   764         |-> (fn (ty, e) => pair ((v, ty) `|-> e))
   805   case Symtab.lookup ((#appgens o CodegenData.get) thy) f
   790   case Symtab.lookup ((#appgens o CodegenData.get) thy) f
   806    of SOME (i, (ag, _)) =>
   791    of SOME (i, (ag, _)) =>
   807         if length ts < i then
   792         if length ts < i then
   808           let
   793           let
   809             val tys = Library.take (i - length ts, ((fst o strip_type) ty));
   794             val tys = Library.take (i - length ts, ((fst o strip_type) ty));
   810             val vs = CodegenThingol.give_names [f] tys;
   795             val vs = Name.give_names (Name.declare f Name.context) "a" tys;
   811           in
   796           in
   812             trns
   797             trns
   813             |> fold_map (exprgen_type thy tabs) tys
   798             |> fold_map (exprgen_type thy tabs) tys
   814             ||>> ag thy tabs ((f, ty), ts @ map Free vs)
   799             ||>> ag thy tabs ((f, ty), ts @ map Free vs)
   815             |-> (fn (tys, e) => pair (map2 (fn (v, _) => pair v) vs tys `|--> e))
   800             |-> (fn (tys, e) => pair (map2 (fn (v, _) => pair v) vs tys `|--> e))
   854         trns
   839         trns
   855         |> appgen_default thy tabs app;
   840         |> appgen_default thy tabs app;
   856 
   841 
   857 fun appgen_case dest_case_expr thy tabs (app as (c_ty, ts)) trns =
   842 fun appgen_case dest_case_expr thy tabs (app as (c_ty, ts)) trns =
   858   let
   843   let
   859     val _ = (writeln o fst) c_ty;
       
   860     val _ = (writeln o Sign.string_of_typ thy o snd) c_ty;
       
   861     val SOME ([], ((st, sty), ds)) = dest_case_expr thy (list_comb (Const c_ty, ts));
   844     val SOME ([], ((st, sty), ds)) = dest_case_expr thy (list_comb (Const c_ty, ts));
   862     fun clausegen (dt, bt) trns =
   845     fun clausegen (dt, bt) trns =
   863       trns
   846       trns
   864       |> exprgen_term thy tabs dt
   847       |> exprgen_term thy tabs dt
   865       ||>> exprgen_term thy tabs bt;
   848       ||>> exprgen_term thy tabs bt;
  1036       else add_alias (src, dst) thy
  1019       else add_alias (src, dst) thy
  1037   in fold add inconsistent thy end;
  1020   in fold add inconsistent thy end;
  1038 
  1021 
  1039 fun codegen_term t thy =
  1022 fun codegen_term t thy =
  1040   thy
  1023   thy
  1041   |> expand_module (SOME (Symtab.keys (#target_data (CodegenData.get thy)))) NONE exprgen_term t;
  1024   |> expand_module (SOME [] (*(Symtab.keys (#target_data (CodegenData.get thy)))*)) NONE exprgen_term t;
  1042 
  1025 
  1043 val is_dtcon = has_nsp nsp_dtcon;
  1026 val is_dtcon = has_nsp nsp_dtcon;
  1044 
  1027 
  1045 fun consts_of_idfs thy =
  1028 fun consts_of_idfs thy =
  1046   map (the o const_of_idf thy (mk_tabs thy NONE));
  1029   map (the o const_of_idf thy (mk_tabs thy NONE));
  1056 fun get_ml_fun_datatype thy resolv =
  1039 fun get_ml_fun_datatype thy resolv =
  1057   let
  1040   let
  1058     val target_data =
  1041     val target_data =
  1059       ((fn data => (the o Symtab.lookup data) "ml") o #target_data o CodegenData.get) thy;
  1042       ((fn data => (the o Symtab.lookup data) "ml") o #target_data o CodegenData.get) thy;
  1060   in
  1043   in
  1061     CodegenSerializer.ml_fun_datatype (nsp_dtcon, nsp_class, K false)
  1044     CodegenSerializer.ml_fun_datatype (nsp_dtcon, nsp_class)
  1062       ((Option.map fst oo Symtab.lookup o #syntax_tyco) target_data,
  1045       ((Option.map fst oo Symtab.lookup o #syntax_tyco) target_data,
  1063       (Option.map fst oo Symtab.lookup o #syntax_const) target_data)
  1046       (Option.map fst oo Symtab.lookup o #syntax_const) target_data)
  1064       resolv
  1047       resolv
  1065   end;
  1048   end;
  1066 
  1049 
  1315     >> (Toplevel.theory oo fold o fold)
  1298     >> (Toplevel.theory oo fold o fold)
  1316           (fn (target, modifier) => modifier target)
  1299           (fn (target, modifier) => modifier target)
  1317   );
  1300   );
  1318 
  1301 
  1319 val purgeP =
  1302 val purgeP =
  1320   OuterSyntax.command purgeK "purge all defintions for constant" K.thy_decl (
  1303   OuterSyntax.command purgeK "purge all incrementally generated code" K.thy_decl
  1321     Scan.repeat1 P.term
  1304     (Scan.succeed (Toplevel.theory purge_code));
  1322     >> (Toplevel.theory o purge_consts)
       
  1323   );
       
  1324 
  1305 
  1325 val aliasP =
  1306 val aliasP =
  1326   OuterSyntax.command aliasK "declare an alias for a theory identifier" K.thy_decl (
  1307   OuterSyntax.command aliasK "declare an alias for a theory identifier" K.thy_decl (
  1327     Scan.repeat1 (P.name -- P.name)
  1308     Scan.repeat1 (P.name -- P.name)
  1328       >> (Toplevel.theory oo fold) add_alias
  1309       >> (Toplevel.theory oo fold) add_alias