src/Pure/Tools/codegen_package.ML
author wenzelm
Thu Jan 19 21:22:08 2006 +0100 (2006-01-19 ago)
changeset 18708 4b3dadb4fe33
parent 18704 2c86ced392a8
child 18756 5eb3df798405
permissions -rw-r--r--
setup: theory -> theory;
     1 (*  Title:      Pure/Tools/codegen_package.ML
     2     ID:         $Id$
     3     Author:     Florian Haftmann, TU Muenchen
     4 
     5 Code generator from Isabelle theories to
     6 intermediate language ("Thin-gol").
     7 *)
     8 
     9 (*NOTE: for simplifying developement, this package contains
    10 some stuff which will finally be moved upwards to HOL*)
    11 
    12 signature CODEGEN_PACKAGE =
    13 sig
    14   type auxtab;
    15   type eqextr = theory -> auxtab
    16     -> (string * typ) -> (thm list * typ) option;
    17   type defgen;
    18   type appgen = theory -> auxtab
    19     -> (string * typ) * term list -> CodegenThingol.transact -> CodegenThingol.iexpr * CodegenThingol.transact;
    20 
    21   val add_appconst: string * ((int * int) * appgen) -> theory -> theory;
    22   val add_appconst_i: xstring * ((int * int) * appgen) -> theory -> theory;
    23   val add_eqextr: string * eqextr -> theory -> theory;
    24   val add_defgen: string * defgen -> theory -> theory;
    25   val add_prim_class: xstring -> string list -> (string * string)
    26     -> theory -> theory;
    27   val add_prim_tyco: xstring -> string list -> (string * string)
    28     -> theory -> theory;
    29   val add_prim_const: xstring * string option -> string list -> (string * string)
    30     -> theory -> theory;
    31   val add_prim_i: string -> string list -> (string * Pretty.T)
    32     -> theory -> theory;
    33   val add_pretty_list: string -> string -> string * (int * string)
    34     -> theory -> theory;
    35   val add_alias: string * string -> theory -> theory;
    36   val set_is_datatype: (theory -> string -> bool) -> theory -> theory;
    37   val set_get_all_datatype_cons : (theory -> (string * string) list)
    38     -> theory -> theory;
    39   val set_int_tyco: string -> theory -> theory;
    40 
    41   val exprgen_type: theory -> auxtab
    42     -> typ -> CodegenThingol.transact -> CodegenThingol.itype * CodegenThingol.transact;
    43   val exprgen_term: theory -> auxtab
    44     -> term -> CodegenThingol.transact -> CodegenThingol.iexpr * CodegenThingol.transact;
    45   val appgen_default: appgen;
    46 
    47   val appgen_let: (int -> term -> term list * term)
    48     -> appgen;
    49   val appgen_split: (int -> term -> term list * term)
    50     -> appgen;
    51   val appgen_number_of: (term -> term) -> (term -> IntInf.int) -> string -> string
    52     -> appgen;
    53   val add_case_const: (theory -> string -> (string * int) list option) -> xstring
    54     -> theory -> theory;
    55   val add_case_const_i: (theory -> string -> (string * int) list option) -> string
    56     -> theory -> theory;
    57   val defgen_datatype: (theory -> string -> ((string * sort) list * string list) option)
    58     -> (theory -> string * string -> typ list option)
    59     -> defgen;
    60 
    61   val print_codegen_generated: theory -> unit;
    62   val rename_inconsistent: theory -> theory;
    63   val ensure_datatype_case_consts: (theory -> string list)
    64     -> (theory -> string -> (string * int) list option)
    65     -> theory -> theory;
    66 
    67   (*debugging purpose only*)
    68   structure InstNameMangler: NAME_MANGLER;
    69   structure ConstNameMangler: NAME_MANGLER;
    70   structure DatatypeconsNameMangler: NAME_MANGLER;
    71   structure CodegenData: THEORY_DATA;
    72   val mk_tabs: theory -> auxtab;
    73   val alias_get: theory -> string -> string;
    74   val idf_of_name: theory -> string -> string -> string;
    75   val idf_of_const: theory -> auxtab -> string * typ -> string;
    76 end;
    77 
    78 structure CodegenPackage : CODEGEN_PACKAGE =
    79 struct
    80 
    81 open CodegenThingolOp;
    82 infix 8 `%%;
    83 infixr 6 `->;
    84 infixr 6 `-->;
    85 infix 4 `$;
    86 infix 4 `$$;
    87 infixr 5 `|->;
    88 infixr 5 `|-->;
    89 
    90 (* auxiliary *)
    91 
    92 fun devarify_type ty = (fst o Type.freeze_thaw_type) ty;
    93 fun devarify_term t = (fst o Type.freeze_thaw) t;
    94 
    95 val is_number = is_some o Int.fromString;
    96 
    97 fun merge_opt _ (x1, NONE) = x1
    98   | merge_opt _ (NONE, x2) = x2
    99   | merge_opt eq (SOME x1, SOME x2) =
   100       if eq (x1, x2) then SOME x1 else error ("incompatible options during merge");
   101 
   102 
   103 (* shallow name spaces *)
   104 
   105 val nsp_class = "class";
   106 val nsp_tyco = "tyco";
   107 val nsp_const = "const";
   108 val nsp_overl = "overl";
   109 val nsp_dtcon = "dtcon";
   110 val nsp_mem = "mem";
   111 val nsp_inst = "inst";
   112 
   113 
   114 (* code generator basics *)
   115 
   116 structure InstNameMangler = NameManglerFun (
   117   type ctxt = theory;
   118   type src = string * (class * string);
   119   val ord = prod_ord string_ord (prod_ord string_ord string_ord);
   120   fun mk thy ((thyname, (cls, tyco)), i) =
   121     NameSpace.base cls ^ "_" ^ NameSpace.base tyco ^ implode (replicate i "'")
   122     |> NameSpace.append thyname;
   123   fun is_valid _ _ = true;
   124   fun maybe_unique _ _ = NONE;
   125   fun re_mangle _ dst = error ("no such instance: " ^ quote dst);
   126 );
   127 
   128 structure ConstNameMangler = NameManglerFun (
   129   type ctxt = theory;
   130   type src = string * (typ * typ);
   131   val ord = prod_ord string_ord (prod_ord Term.typ_ord Term.typ_ord);
   132   fun mk thy ((c, (ty_decl, ty)), i) =
   133     let
   134       fun mangle (Type (tyco, tys)) =
   135             NameSpace.base tyco :: Library.flat (List.mapPartial mangle tys) |> SOME
   136         | mangle _ =
   137             NONE
   138     in
   139       Vartab.empty
   140       |> Sign.typ_match thy (ty_decl, ty)
   141       |> map (snd o snd) o Vartab.dest
   142       |> List.mapPartial mangle
   143       |> Library.flat
   144       |> null ? K ["x"]
   145       |> cons c
   146       |> space_implode "_"
   147       |> curry (op ^ o swap) ((implode oo replicate) i "'")
   148     end;
   149   fun is_valid _ _ = true;
   150   fun maybe_unique _ _ = NONE;
   151   fun re_mangle _ dst = error ("no such constant: " ^ quote dst);
   152 );
   153 
   154 structure DatatypeconsNameMangler = NameManglerFun (
   155   type ctxt = theory;
   156   type src = string * string;
   157   val ord = prod_ord string_ord string_ord;
   158   fun mk thy ((co, dtco), i) =
   159         let
   160           fun basename 0 = NameSpace.base co
   161             | basename 1 = NameSpace.base dtco ^ "_" ^ NameSpace.base co
   162             | basename i = NameSpace.base dtco ^ "_" ^ NameSpace.base co ^ "_" ^ (implode oo replicate) (i-1) "'";
   163           fun strip_dtco name =
   164             case (rev o NameSpace.unpack) name
   165              of x1::x2::xs =>
   166                   if x2 = NameSpace.base dtco
   167                   then NameSpace.pack (x1::xs)
   168                   else name
   169               | _ => name;
   170         in
   171           NameSpace.append (NameSpace.drop_base dtco) (basename i)
   172           |> strip_dtco
   173         end;
   174   fun is_valid _ _ = true;
   175   fun maybe_unique _ _ = NONE;
   176   fun re_mangle _ dst = error ("no such datatype constructor: " ^ quote dst);
   177 );
   178 
   179 type auxtab = ((typ * thm) list Symtab.table * string Symtab.table)
   180   * (InstNameMangler.T * ((typ * typ list) Symtab.table * ConstNameMangler.T) * DatatypeconsNameMangler.T);
   181 type eqextr = theory -> auxtab
   182   -> (string * typ) -> (thm list * typ) option;
   183 type defgen = theory -> auxtab -> gen_defgen;
   184 type appgen = theory -> auxtab -> (string * typ) * term list -> transact -> iexpr * transact;
   185 
   186 val serializers = ref (
   187   Symtab.empty
   188   |> Symtab.update (
   189        #ml CodegenSerializer.serializers
   190        |> apsnd (fn seri => seri
   191             (nsp_dtcon, nsp_class, "")
   192             [[nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_mem, nsp_inst]]
   193           )
   194      )
   195   |> Symtab.update (
   196        #haskell CodegenSerializer.serializers
   197        |> apsnd (fn seri => seri
   198             nsp_dtcon
   199             [[nsp_class], [nsp_tyco], [nsp_const, nsp_overl, nsp_mem], [nsp_dtcon], [nsp_inst]]
   200           )
   201      )
   202 );
   203 
   204 
   205 (* theory data for code generator *)
   206 
   207 type gens = {
   208   appconst: ((int * int) * (appgen * stamp)) Symtab.table,
   209   eqextrs: (string * (eqextr * stamp)) list,
   210   defgens: (string * (defgen * stamp)) list
   211 };
   212 
   213 fun map_gens f { appconst, eqextrs, defgens } =
   214   let
   215     val (appconst, eqextrs, defgens) =
   216       f (appconst, eqextrs, defgens)
   217   in { appconst = appconst, eqextrs = eqextrs, defgens = defgens } : gens end;
   218 
   219 fun merge_gens
   220   ({ appconst = appconst1 , eqextrs = eqextrs1, defgens = defgens1 },
   221    { appconst = appconst2 , eqextrs = eqextrs2, defgens = defgens2 }) =
   222   { appconst = Symtab.merge
   223       (fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) => bounds1 = bounds2 andalso stamp1 = stamp2)
   224       (appconst1, appconst2),
   225     eqextrs = AList.merge (op =) (eq_snd (op =)) (eqextrs1, eqextrs2),
   226     defgens = AList.merge (op =) (eq_snd (op =)) (defgens1, defgens2)
   227   } : gens;
   228 
   229 type logic_data = {
   230   is_datatype: ((theory -> string -> bool) * stamp) option,
   231   get_all_datatype_cons: ((theory -> (string * string) list) * stamp) option,
   232   alias: string Symtab.table * string Symtab.table
   233 };
   234 
   235 fun map_logic_data f { is_datatype, get_all_datatype_cons, alias } =
   236   let
   237     val ((is_datatype, get_all_datatype_cons), alias) =
   238       f ((is_datatype, get_all_datatype_cons), alias)
   239   in { is_datatype = is_datatype, get_all_datatype_cons = get_all_datatype_cons,
   240     alias = alias } : logic_data end;
   241 
   242 fun merge_logic_data
   243   ({ is_datatype = is_datatype1, get_all_datatype_cons = get_all_datatype_cons1,
   244        alias = alias1 },
   245    { is_datatype = is_datatype2, get_all_datatype_cons = get_all_datatype_cons2,
   246        alias = alias2 }) =
   247   let
   248   in
   249     { is_datatype = merge_opt (eq_snd (op =)) (is_datatype1, is_datatype2),
   250       get_all_datatype_cons = merge_opt (eq_snd (op =)) (get_all_datatype_cons1, get_all_datatype_cons2),
   251       alias = (Symtab.merge (op =) (fst alias1, fst alias2),
   252                Symtab.merge (op =) (snd alias1, snd alias2)) } : logic_data
   253   end;
   254 
   255 type target_data = {
   256   syntax_tyco: (itype CodegenSerializer.pretty_syntax * stamp) Symtab.table,
   257   syntax_const: (iexpr CodegenSerializer.pretty_syntax * stamp) Symtab.table
   258 };
   259 
   260 fun map_target_data f { syntax_tyco, syntax_const } =
   261   let
   262     val (syntax_tyco, syntax_const) =
   263       f (syntax_tyco, syntax_const)
   264   in { syntax_tyco = syntax_tyco, syntax_const = syntax_const } : target_data
   265   end;
   266 
   267 fun merge_target_data
   268   ({ syntax_tyco = syntax_tyco1, syntax_const = syntax_const1 },
   269    { syntax_tyco = syntax_tyco2, syntax_const = syntax_const2 }) =
   270   { syntax_tyco = Symtab.merge (eq_snd (op =)) (syntax_tyco1, syntax_tyco2),
   271     syntax_const = Symtab.merge (eq_snd (op =)) (syntax_const1, syntax_const2) } : target_data;
   272 
   273 structure CodegenData = TheoryDataFun
   274 (struct
   275   val name = "Pure/codegen_package";
   276   type T = {
   277     modl: module,
   278     gens: gens,
   279     logic_data: logic_data,
   280     target_data: target_data Symtab.table
   281   };
   282   val empty = {
   283     modl = empty_module,
   284     gens = { appconst = Symtab.empty, eqextrs = [], defgens = [] } : gens,
   285     logic_data = { is_datatype = NONE, get_all_datatype_cons = NONE,
   286       alias = (Symtab.empty, Symtab.empty) } : logic_data,
   287     target_data =
   288       Symtab.empty
   289       |> Symtab.fold (fn (target, _) =>
   290            Symtab.update (target, { syntax_tyco = Symtab.empty, syntax_const = Symtab.empty })
   291          ) (! serializers)
   292   } : T;
   293   val copy = I;
   294   val extend = I;
   295   fun merge _ (
   296     { modl = modl1, gens = gens1,
   297       target_data = target_data1, logic_data = logic_data1 },
   298     { modl = modl2, gens = gens2,
   299       target_data = target_data2, logic_data = logic_data2 }
   300   ) = {
   301     modl = merge_module (modl1, modl2),
   302     gens = merge_gens (gens1, gens2),
   303     logic_data = merge_logic_data (logic_data1, logic_data2),
   304     target_data = Symtab.join (K (merge_target_data #> SOME))
   305       (target_data1, target_data2)
   306   };
   307   fun print _ _ = ();
   308 end);
   309 
   310 val _ = Context.add_setup CodegenData.init;
   311 
   312 fun map_codegen_data f thy =
   313   case CodegenData.get thy
   314    of { modl, gens, target_data, logic_data } =>
   315       let val (modl, gens, target_data, logic_data) =
   316         f (modl, gens, target_data, logic_data)
   317       in CodegenData.put { modl = modl, gens = gens,
   318            target_data = target_data, logic_data = logic_data } thy end;
   319 
   320 fun print_codegen_generated thy =
   321   let
   322     val module = (#modl o CodegenData.get) thy;
   323   in
   324     (writeln o Pretty.output o Pretty.chunks) [pretty_module module, pretty_deps module]
   325   end;
   326 
   327 fun gen_add_appconst prep_const (raw_c, (bounds, ag)) thy =
   328   let
   329     val c = prep_const thy raw_c;
   330   in map_codegen_data
   331     (fn (modl, gens, target_data, logic_data) =>
   332        (modl,
   333         gens |> map_gens
   334           (fn (appconst, eqextrs, defgens) =>
   335             (appconst
   336              |> Symtab.update (c, (bounds, (ag, stamp ()))),
   337              eqextrs, defgens)), target_data, logic_data)) thy
   338   end;
   339 
   340 val add_appconst = gen_add_appconst Sign.intern_const;
   341 val add_appconst_i = gen_add_appconst (K I);
   342 
   343 fun add_defgen (name, dg) =
   344   map_codegen_data
   345     (fn (modl, gens, target_data, logic_data) =>
   346        (modl,
   347         gens |> map_gens
   348           (fn (appconst, eqextrs, defgens) =>
   349             (appconst, eqextrs, defgens
   350              |> Output.update_warn (op =) ("overwriting existing definition definition generator " ^ name) (name, (dg, stamp ())))),
   351              target_data, logic_data));
   352 
   353 fun get_defgens thy tabs =
   354   (map (apsnd (fn (dg, _) => dg thy tabs)) o #defgens o #gens o CodegenData.get) thy;
   355 
   356 fun add_eqextr (name, eqx) =
   357   map_codegen_data
   358     (fn (modl, gens, target_data, logic_data) =>
   359        (modl,
   360         gens |> map_gens
   361           (fn (appconst, eqextrs, defgens) =>
   362             (appconst, eqextrs
   363              |> Output.update_warn (op =) ("overwriting existing equation extractor " ^ name) (name, (eqx, stamp ())),
   364              defgens)),
   365              target_data, logic_data));
   366 
   367 fun get_eqextrs thy tabs =
   368   (map (fn (_, (eqx, _)) => eqx thy tabs) o #eqextrs o #gens o CodegenData.get) thy;
   369 
   370 fun is_datatype thy =
   371   case (#is_datatype o #logic_data o CodegenData.get) thy
   372    of NONE => K false
   373     | SOME (f, _) => f thy;
   374 
   375 fun get_all_datatype_cons thy =
   376   case (#get_all_datatype_cons o #logic_data o CodegenData.get) thy
   377    of NONE => []
   378     | SOME (f, _) => f thy;
   379 
   380 fun add_alias (src, dst) =
   381   map_codegen_data
   382     (fn (modl, gens, target_data, logic_data) =>
   383        (modl, gens, target_data,
   384         logic_data |> map_logic_data
   385           (apsnd (fn (tab, tab_rev) =>
   386             (tab |> Symtab.update (src, dst),
   387              tab_rev |> Symtab.update (dst, src))))));
   388 
   389 
   390 (* name handling *)
   391 
   392 val alias_get = perhaps o Symtab.lookup o fst o #alias o #logic_data o CodegenData.get;
   393 val alias_rev = perhaps o Symtab.lookup o snd o #alias o #logic_data o CodegenData.get;
   394 
   395 fun add_nsp shallow name =
   396   name
   397   |> NameSpace.unpack
   398   |> split_last
   399   |> apsnd (single #> cons shallow)
   400   |> (op @)
   401   |> NameSpace.pack;
   402 
   403 fun dest_nsp nsp idf =
   404   let
   405     val idf' = NameSpace.unpack idf;
   406     val (idf'', idf_base) = split_last idf';
   407     val (modl, shallow) = split_last idf'';
   408   in
   409     if nsp = shallow
   410     then (SOME o NameSpace.pack) (modl @ [idf_base])
   411     else NONE
   412   end;
   413 
   414 fun idf_of_name thy shallow name =
   415   name
   416   |> alias_get thy
   417   |> add_nsp shallow;
   418 
   419 fun name_of_idf thy shallow idf =
   420   idf
   421   |> dest_nsp shallow
   422   |> Option.map (alias_rev thy);
   423 
   424 fun set_is_datatype f =
   425   map_codegen_data
   426     (fn (modl, gens, target_data, logic_data) =>
   427        (modl, gens, target_data,
   428         logic_data
   429         |> map_logic_data (apfst (fn (is_datatype, get_all_datatype_cons)
   430              => (SOME (f, stamp ()), get_all_datatype_cons)))));
   431 
   432 fun set_get_all_datatype_cons f =
   433   map_codegen_data
   434     (fn (modl, gens, target_data, logic_data) =>
   435        (modl, gens, target_data,
   436         logic_data
   437         |> map_logic_data ((apfst (fn (is_datatype, get_all_datatype_cons)
   438              => (is_datatype, SOME (f, stamp ())))))));
   439 
   440 fun set_int_tyco tyco thy =
   441   (serializers := (
   442     ! serializers
   443     |> Symtab.update (
   444          #ml CodegenSerializer.serializers
   445          |> apsnd (fn seri => seri
   446               (nsp_dtcon, nsp_class, idf_of_name thy nsp_tyco tyco)
   447               [[nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_mem, nsp_inst]]
   448             )
   449        )
   450     ); thy);
   451 
   452 
   453 (* code generator instantiation *)
   454 
   455 fun ensure_def_class thy tabs cls trns =
   456   let
   457     val cls' = idf_of_name thy nsp_class cls;
   458   in
   459     trns
   460     |> debug 4 (fn _ => "generating class " ^ quote cls)
   461     |> gen_ensure_def (get_defgens thy tabs) ("generating class " ^ quote cls) cls'
   462     |> pair cls'
   463   end;
   464 
   465 fun ensure_def_inst thy (tabs as (_, (insttab, _, _))) (cls, tyco) trns =
   466   let
   467     val thyname = (the o AList.lookup (op =) (ClassPackage.the_instances thy cls)) tyco;
   468     val inst = idf_of_name thy nsp_inst (InstNameMangler.get thy insttab (thyname, (cls, tyco)));
   469   in
   470     trns
   471     |> debug 4 (fn _ => "generating instance " ^ quote cls ^ " / " ^ quote tyco)
   472     |> gen_ensure_def (get_defgens thy tabs) ("generating instance " ^ quote cls ^ " / " ^ quote tyco) inst
   473     |> pair inst
   474   end;
   475 
   476 fun ensure_def_tyco thy tabs tyco trns =
   477   let
   478     val tyco' = idf_of_name thy nsp_tyco tyco;
   479   in
   480     trns
   481     |> debug 4 (fn _ => "generating type constructor " ^ quote tyco)
   482     |> gen_ensure_def (get_defgens thy tabs) ("generating type constructor " ^ quote tyco) tyco'
   483     |> pair tyco'
   484   end;
   485 
   486 fun idf_of_const thy (tabs as ((_, clsmemtab), (_, (overltab1, overltab2), dtcontab))) (c, ty) =
   487   let
   488     fun get_overloaded (c, ty) =
   489       case Symtab.lookup overltab1 c
   490        of SOME (ty_decl, tys) =>
   491             (case find_first (curry (Sign.typ_instance thy) ty) tys
   492              of SOME ty' => ConstNameMangler.get thy overltab2
   493                   (idf_of_name thy nsp_overl c, (ty_decl, ty')) |> SOME
   494               | _ => NONE)
   495         | _ => NONE
   496     fun get_datatypecons (c, ty) =
   497       case (snd o strip_type) ty
   498        of Type (tyco, _) =>
   499             try (DatatypeconsNameMangler.get thy dtcontab) (c, tyco)
   500         | _ => NONE;
   501   in case get_datatypecons (c, ty)
   502    of SOME c' => idf_of_name thy nsp_dtcon c'
   503     | NONE => case get_overloaded (c, ty)
   504    of SOME idf => idf
   505     | NONE => case Symtab.lookup clsmemtab c
   506    of SOME _ => idf_of_name thy nsp_mem c
   507     | NONE => idf_of_name thy nsp_const c
   508   end;
   509 
   510 fun recconst_of_idf thy (_, (_, (_, overltab2), _)) idf =
   511   case name_of_idf thy nsp_const idf
   512    of SOME c => SOME (c, Sign.the_const_type thy c)
   513     | NONE => (
   514         case dest_nsp nsp_overl idf
   515          of SOME _ =>
   516               idf
   517               |> ConstNameMangler.rev thy overltab2
   518               |> apfst (the o name_of_idf thy nsp_overl)
   519               |> apsnd snd
   520               |> SOME
   521           | NONE => NONE
   522       );
   523 
   524 fun ensure_def_const thy (tabs as ((_, clsmemtab), (_, overltab, dtcontab))) (c, ty) trns =
   525   let
   526     val c' = idf_of_const thy tabs (c, ty);
   527   in
   528     trns
   529     |> debug 4 (fn _ => "generating constant " ^ quote c)
   530     |> gen_ensure_def (get_defgens thy tabs) ("generating constant " ^ quote c) c'
   531     |> pair c'
   532   end;
   533 
   534 (* fun ensure_def_eq thy tabs (dtco, (eqpred, arity)) trns =
   535   let
   536     val name_dtco = (the ooo name_of_idf) thy nsp_tyco dtco;
   537     val idf_eqinst = idf_of_name thy nsp_eq_inst name_dtco;
   538     val idf_eqpred = idf_of_name thy nsp_eq_pred name_dtco;
   539     val inst_sortlookup = map (fn (v, _) => [ClassPackage.Lookup ([], (v, 0))]) arity;
   540     fun mk_eq_pred _ trns =
   541       trns
   542       |> succeed (eqpred)
   543     fun mk_eq_inst _ trns =
   544       trns
   545       |> gen_ensure_def [("eqpred", mk_eq_pred)] ("generating equality predicate for " ^ quote dtco) idf_eqpred
   546       |> succeed (Classinst ((class_eq, (dtco, arity)), ([], [(fun_eq, (idf_eqpred, inst_sortlookup))])));
   547   in
   548     trns
   549     |> gen_ensure_def [("eqinst", mk_eq_inst)] ("generating equality instance for " ^ quote dtco) idf_eqinst
   550   end; *)
   551 
   552 
   553 (* expression generators *)
   554 
   555 fun exprgen_tyvar_sort thy tabs (v, sort) trns =
   556   trns
   557   |> fold_map (ensure_def_class thy tabs) (ClassPackage.syntactic_sort_of thy sort)
   558   |-> (fn sort => pair (unprefix "'" v, sort));
   559 
   560 fun exprgen_type thy tabs (TVar _) trns =
   561       error "TVar encountered during code generation"
   562   | exprgen_type thy tabs (TFree v_s) trns =
   563       trns
   564       |> exprgen_tyvar_sort thy tabs v_s
   565       |-> (fn v_s => pair (IVarT v_s))
   566   | exprgen_type thy tabs (Type ("fun", [t1, t2])) trns =
   567       trns
   568       |> exprgen_type thy tabs t1
   569       ||>> exprgen_type thy tabs t2
   570       |-> (fn (t1', t2') => pair (t1' `-> t2'))
   571   | exprgen_type thy tabs (Type (tyco, tys)) trns =
   572       trns
   573       |> ensure_def_tyco thy tabs tyco
   574       ||>> fold_map (exprgen_type thy tabs) tys
   575       |-> (fn (tyco, tys) => pair (tyco `%% tys));
   576 
   577 fun mk_lookup thy tabs (ClassPackage.Instance (inst as (cls, tyco), ls)) trns =
   578       trns
   579       |> ensure_def_class thy tabs cls
   580       ||>> ensure_def_inst thy tabs inst
   581       ||>> (fold_map o fold_map) (mk_lookup thy tabs) ls
   582       |-> (fn ((cls, i), ls) => pair (ClassPackage.Instance ((cls, i), ls)))
   583   | mk_lookup thy tabs (ClassPackage.Lookup (clss, (v, i))) trns =
   584       trns
   585       |> fold_map (ensure_def_class thy tabs) clss
   586       |-> (fn clss => pair (ClassPackage.Lookup (clss, (v |> unprefix "'", i))));
   587 
   588 fun appgen_default thy tabs ((c, ty), ts) trns =
   589   trns
   590   |> ensure_def_const thy tabs (c, ty)
   591   ||>> (fold_map o fold_map) (mk_lookup thy tabs)
   592     (ClassPackage.extract_sortlookup thy (c, ty))
   593   ||>> exprgen_type thy tabs ty
   594   ||>> fold_map (exprgen_term thy tabs) ts
   595   |-> (fn (((c, ls), ty), es) =>
   596          pair (Library.foldl IInst ((IConst (c, ty)), ls) `$$ es))
   597 and appgen thy tabs ((f, ty), ts) trns =
   598   case Symtab.lookup ((#appconst o #gens o CodegenData.get) thy) f
   599    of SOME ((imin, imax), (ag, _)) =>
   600         if length ts < imin then
   601           let
   602             val d = imin - length ts;
   603             val vs = Term.invent_names (add_term_names (Const (f, ty), [])) "x" d;
   604             val tys = Library.take (d, ((fst o strip_type) ty));
   605           in
   606             trns
   607             |> debug 10 (fn _ => "eta-expanding")
   608             |> fold_map (exprgen_type thy tabs) tys
   609             ||>> ag thy tabs ((f, ty), ts @ map2 (curry Free) vs tys)
   610             |-> (fn (tys, e) => pair ((vs ~~ tys) `|--> e))
   611           end
   612         else if length ts > imax then
   613           trns
   614           |> debug 10 (fn _ => "splitting arguments (" ^ string_of_int imax ^ ", " ^ string_of_int (length ts) ^ ")")
   615           |> ag thy tabs ((f, ty), Library.take (imax, ts))
   616           ||>> fold_map (exprgen_term thy tabs) (Library.drop (imax, ts))
   617           |-> (fn es => pair (mk_apps es))
   618         else
   619           trns
   620           |> debug 10 (fn _ => "keeping arguments")
   621           |> ag thy tabs ((f, ty), ts)
   622     | NONE =>
   623         trns
   624         |> appgen_default thy tabs ((f, ty), ts)
   625 and exprgen_term thy tabs (Const (f, ty)) trns =
   626       trns
   627       |> appgen thy tabs ((f, ty), [])
   628       |-> (fn e => pair e)
   629   | exprgen_term thy tabs (Var ((v, i), ty)) trns =
   630       trns
   631       |> exprgen_type thy tabs ty
   632       |-> (fn ty => pair (IVarE (if i = 0 then v else v ^ "_" ^ string_of_int i, ty)))
   633   | exprgen_term thy tabs (Free (v, ty)) trns =
   634       trns
   635       |> exprgen_type thy tabs ty
   636       |-> (fn ty => pair (IVarE (v, ty)))
   637   | exprgen_term thy tabs (Abs (v, ty, t)) trns =
   638       trns
   639       |> exprgen_type thy tabs ty
   640       ||>> exprgen_term thy tabs (subst_bound (Free (v, ty), t))
   641       |-> (fn (ty, e) => pair ((v, ty) `|-> e))
   642   | exprgen_term thy tabs (t as t1 $ t2) trns =
   643       let
   644         val (t', ts) = strip_comb t
   645       in case t'
   646        of Const (f, ty) =>
   647             trns
   648             |> appgen thy tabs ((f, ty), ts)
   649             |-> (fn e => pair e)
   650         | _ =>
   651             trns
   652             |> exprgen_term thy tabs t'
   653             ||>> fold_map (exprgen_term thy tabs) ts
   654             |-> (fn (e, es) => pair (e `$$ es))
   655       end;
   656 
   657 
   658 (* application generators *)
   659 
   660 (* fun appgen_eq thy tabs (("op =", Type ("fun", [ty, _])), [t1, t2]) trns =
   661   trns
   662   |> invoke_eq (exprgen_type thy tabs) (ensure_def_eq thy tabs) ty
   663   |-> (fn false => error ("could not derive equality for " ^ Sign.string_of_typ thy ty)
   664         | true => fn trns => trns
   665   |> exprgen_term thy tabs t1
   666   ||>> exprgen_term thy tabs t2
   667   |-> (fn (e1, e2) => pair (Fun_eq `$ e1 `$ e2))); *)
   668 
   669 
   670 (* function extractors *)
   671 
   672 fun mk_fun thy tabs (c, ty) trns =
   673   case get_first (fn eqx => eqx (c, ty)) (get_eqextrs thy tabs)
   674    of SOME (eq_thms, ty) =>
   675         let
   676           val sortctxt = ClassPackage.extract_sortctxt thy ty;
   677           fun dest_eqthm eq_thm =
   678             eq_thm
   679             |> prop_of
   680             |> Logic.dest_equals
   681             |> apfst (strip_comb #> snd);
   682           fun mk_eq (args, rhs) trns =
   683             trns
   684             |> fold_map (exprgen_term thy tabs o devarify_term) args
   685             ||>> (exprgen_term thy tabs o devarify_term) rhs
   686             |-> (fn (args, rhs) => pair (map ipat_of_iexpr args, rhs))
   687         in
   688           trns
   689           |> fold_map (mk_eq o dest_eqthm) eq_thms
   690           ||>> exprgen_type thy tabs (devarify_type ty)
   691           ||>> fold_map (exprgen_tyvar_sort thy tabs) sortctxt
   692           |-> (fn ((eqs, ty), sortctxt) => (pair o SOME) (eqs, (sortctxt, ty)))
   693         end
   694     | NONE => (NONE, trns);
   695 
   696 fun eqextr_defs thy ((deftab, _), _) (c, ty) =
   697   let
   698     fun eq_typ (ty1, ty2) = 
   699       Sign.typ_instance thy (ty1, ty2)
   700       andalso Sign.typ_instance thy (ty2, ty1)
   701   in
   702     Option.mapPartial (get_first (fn (ty', thm) => if eq_typ (ty, ty')
   703       then SOME ([thm], ty')
   704       else NONE
   705     )) (Symtab.lookup deftab c)
   706   end;
   707 
   708 
   709 (* definition generators *)
   710 
   711 fun defgen_clsdecl thy (tabs as (_, (insttab, _, _))) cls trns =
   712   case name_of_idf thy nsp_class cls
   713    of SOME cls =>
   714         let
   715           val cs = (snd o ClassPackage.the_consts_sign thy) cls;
   716           val sortctxts = map (ClassPackage.extract_sortctxt thy o snd) cs;
   717           val idfs = map (idf_of_name thy nsp_mem o fst) cs;
   718         in
   719           trns
   720           |> debug 5 (fn _ => "trying defgen class declaration for " ^ quote cls)
   721           |> fold_map (ensure_def_class thy tabs) (ClassPackage.the_superclasses thy cls)
   722           ||>> fold_map (exprgen_type thy tabs o snd) cs
   723           ||>> (fold_map o fold_map) (exprgen_tyvar_sort thy tabs) sortctxts
   724           |-> (fn ((supcls, memtypes), sortctxts) => succeed
   725             (Class ((supcls, ("a", idfs ~~ (sortctxts ~~ memtypes))), [])))
   726         end
   727     | _ =>
   728         trns
   729         |> fail ("no class definition found for " ^ quote cls);
   730 
   731 fun defgen_funs thy tabs c trns =
   732   case recconst_of_idf thy tabs c
   733    of SOME (c, ty) =>
   734         trns
   735         |> mk_fun thy tabs (c, ty)
   736         |-> (fn (SOME funn) => succeed (Fun funn)
   737               | NONE => fail ("no defining equations found for " ^ quote c))
   738     | NONE =>
   739         trns
   740         |> fail ("not a constant: " ^ quote c);
   741 
   742 fun defgen_datatypecons thy (tabs as (_, (_, _, dtcontab))) co trns =
   743   case Option.map (DatatypeconsNameMangler.rev thy dtcontab) (name_of_idf thy nsp_dtcon co)
   744    of SOME (co, dtco) =>
   745         trns
   746         |> debug 5 (fn _ => "trying defgen datatype constructor for " ^ quote co)
   747         |> ensure_def_tyco thy tabs dtco
   748         |-> (fn dtco => succeed Undef)
   749     | _ =>
   750         trns
   751         |> fail ("not a datatype constructor: " ^ quote co);
   752 
   753 fun defgen_clsmem thy tabs m trns =
   754   case name_of_idf thy nsp_mem m
   755    of SOME m =>
   756         trns
   757         |> debug 5 (fn _ => "trying defgen class member for " ^ quote m)
   758         |> ensure_def_class thy tabs ((the o ClassPackage.lookup_const_class thy) m)
   759         |-> (fn cls => succeed Undef)
   760     | _ =>
   761         trns |> fail ("no class member found for " ^ quote m)
   762 
   763 fun defgen_clsinst thy (tabs as (_, (insttab, _, _))) inst trns =
   764   case Option.map (InstNameMangler.rev thy insttab) (name_of_idf thy nsp_inst inst)
   765    of SOME (_, (cls, tyco)) =>
   766         let
   767           val (arity, memdefs) = ClassPackage.the_inst_sign thy (cls, tyco);
   768           fun gen_membr (m, ty) trns =
   769             trns
   770             |> mk_fun thy tabs (m, ty)
   771             |-> (fn SOME funn => pair (m, funn)
   772                   | NONE => error ("could not derive definition for member " ^ quote m));
   773         in
   774           trns
   775           |> debug 5 (fn _ => "trying defgen class instance for (" ^ quote cls ^ ", " ^ quote tyco ^ ")")
   776           |> ensure_def_class thy tabs cls
   777           ||>> ensure_def_tyco thy tabs tyco
   778           ||>> fold_map (exprgen_tyvar_sort thy tabs) arity
   779           ||>> fold_map gen_membr memdefs
   780           |-> (fn (((cls, tyco), arity), memdefs) =>
   781                  succeed (Classinst ((cls, (tyco, arity)), memdefs)))
   782         end
   783     | _ =>
   784         trns |> fail ("no class instance found for " ^ quote inst);
   785 
   786 
   787 (* parametrized generators, for instantiation in HOL *)
   788 
   789 fun appgen_let strip_abs thy tabs ((c, ty), [t2, t3]) trns =
   790   let
   791     fun dest_let (l as Const (c', _) $ t $ u) =
   792           if c = c' then
   793             case strip_abs 1 u
   794              of ([p], u') => apfst (cons (p, t)) (dest_let u')
   795               | _ => ([], l)
   796           else ([], t)
   797       | dest_let t = ([], t);
   798     fun mk_let (l, r) trns =
   799       trns
   800       |> exprgen_term thy tabs l
   801       ||>> exprgen_term thy tabs r
   802       |-> (fn (l, r) => pair (r, ipat_of_iexpr l));
   803     val (lets, body) = dest_let (Const (c, ty) $ t2 $ t3)
   804   in
   805     trns
   806     |> fold_map mk_let lets
   807     ||>> exprgen_term thy tabs body
   808     |-> (fn (lets, body) =>
   809          pair (Library.foldr (fn ((e, p), body) => ICase (e, [(p, body)])) (lets, body)))
   810   end
   811 
   812 fun appgen_split strip_abs thy tabs (c, [t2]) trns =
   813   let
   814     val ([p], body) = strip_abs 1 (Const c $ t2)
   815   in
   816     trns
   817     |> exprgen_term thy tabs p
   818     ||>> exprgen_term thy tabs body
   819     |-> (fn (IVarE v, body) => pair (IAbs (v, body)))
   820   end;
   821 
   822 fun appgen_number_of mk_int_to_nat bin_to_int tyco_int tyco_nat thy tabs ((_,
   823   Type (_, [_, ty as Type (tyco, [])])), [bin]) trns =
   824     if tyco = tyco_int then
   825       trns
   826       |> exprgen_type thy tabs ty
   827       |-> (fn ty => pair (CodegenThingol.IConst ((IntInf.toString o bin_to_int) bin, ty)))
   828     else if tyco = tyco_nat then
   829       trns
   830       |> exprgen_term thy tabs (mk_int_to_nat bin)
   831     else error ("invalid type constructor for numeral: " ^ quote tyco);
   832 
   833 fun appgen_datatype_case cos thy tabs ((_, ty), ts) trns =
   834   let
   835     val (ts', t) = split_last ts;
   836     val (tys, dty) = (split_last o fst o strip_type) ty;
   837     fun gen_names i =
   838       variantlist (replicate i "x", foldr add_term_names
   839        (map (fst o fst o dest_Var) (foldr add_term_vars [] ts)) ts);
   840     fun cg_case_d (((cname, i), ty), t) trns =
   841       let
   842         val vs = gen_names i;
   843         val tys = Library.take (i, (fst o strip_type) ty);
   844         val frees = map2 (curry Free) vs tys;
   845         val t' = Envir.beta_norm (list_comb (t, frees));
   846       in
   847         trns
   848         |> exprgen_term thy tabs (list_comb (Const (cname, tys ---> dty), frees))
   849         ||>> exprgen_term thy tabs t'
   850         |-> (fn (ep, e) => pair (ipat_of_iexpr ep, e))
   851       end;
   852   in
   853     trns
   854     |> exprgen_term thy tabs t
   855     ||>> fold_map cg_case_d ((cos ~~ tys) ~~ ts')
   856     |-> (fn (t, ds) => pair (ICase (t, ds)))
   857   end;
   858 
   859 fun gen_add_case_const prep_c get_case_const_data raw_c thy =
   860   let
   861     val c = prep_c thy raw_c;
   862     val (tys, dty) = (split_last o fst o strip_type o Sign.the_const_type thy) c;
   863     val cos = (the o get_case_const_data thy) c;
   864     val n_eta = length cos + 1;
   865   in
   866     thy
   867     |> add_appconst_i (c, ((n_eta, n_eta), appgen_datatype_case cos))
   868   end;
   869 
   870 val add_case_const = gen_add_case_const Sign.intern_const;
   871 val add_case_const_i = gen_add_case_const (K I);
   872 
   873 fun defgen_datatype get_datatype get_datacons thy (tabs as (_, (_, _, dtcontab))) dtco trns =
   874   case name_of_idf thy nsp_tyco dtco
   875    of SOME dtco =>
   876         (case get_datatype thy dtco
   877          of SOME (vars, cos) =>
   878               let
   879                 val cotys = map (the o get_datacons thy o rpair dtco) cos;
   880                 val coidfs = map (fn co => (DatatypeconsNameMangler.get thy dtcontab (co, dtco)) |>
   881                   idf_of_name thy nsp_dtcon) cos;
   882               in
   883                 trns
   884                 |> debug 5 (fn _ => "trying defgen datatype for " ^ quote dtco)
   885                 |> fold_map (exprgen_tyvar_sort thy tabs) vars
   886                 ||>> (fold_map o fold_map) (exprgen_type thy tabs o devarify_type) cotys
   887                 |-> (fn (sorts, tys) => succeed (Datatype
   888                      ((sorts, coidfs ~~ tys), [])))
   889               end
   890           | NONE =>
   891               trns
   892               |> fail ("no datatype found for " ^ quote dtco))
   893     | NONE =>
   894         trns
   895         |> fail ("not a type constructor: " ^ quote dtco)
   896 
   897 
   898 
   899 (** theory interface **)
   900 
   901 fun mk_tabs thy =
   902   let
   903     fun extract_defs thy =
   904       let
   905         fun dest tm =
   906           let
   907             val (lhs, rhs) = Logic.dest_equals (prop_of tm);
   908             val (t, args) = strip_comb lhs;
   909             val (c, ty) = dest_Const t
   910           in if forall is_Var args then SOME ((c, ty), tm) else NONE
   911           end handle TERM _ => NONE;
   912         fun prep_def def = (case Codegen.preprocess thy [def] of
   913           [def'] => def' | _ => error "mk_auxtab: bad preprocessor");
   914         fun add_def (name, _) =
   915           case (dest o prep_def o Thm.get_axiom thy) name
   916            of SOME ((c, ty), tm) =>
   917                 Symtab.default (c, []) #> Symtab.map_entry c (cons (ty, tm))
   918             | NONE => I
   919       in
   920         Symtab.empty
   921         |> fold (Symtab.fold add_def o snd o #axioms o Theory.rep_theory)
   922              (thy :: Theory.ancestors_of thy)
   923       end;
   924     fun mk_insttab thy =
   925       InstNameMangler.empty
   926       |> Symtab.fold_map
   927           (fn (cls, (clsmems, clsinsts)) => fold_map
   928             (fn (tyco, thyname) => InstNameMangler.declare thy (thyname, (cls, tyco))) clsinsts)
   929                  (ClassPackage.get_classtab thy)
   930       |-> (fn _ => I);
   931     fun mk_overltabs thy deftab =
   932       (Symtab.empty, ConstNameMangler.empty)
   933       |> Symtab.fold
   934           (fn (c, [_]) => I
   935             | (c, tytab) =>
   936                 (fn (overltab1, overltab2) => (
   937                   overltab1
   938                   |> Symtab.update_new (c, (Sign.the_const_constraint thy c, map fst tytab)),
   939                   overltab2
   940                   |> fold (fn (ty, _) => ConstNameMangler.declare thy (idf_of_name thy nsp_overl c, (Sign.the_const_constraint thy c, ty)) #> snd) tytab
   941                 ))) deftab;
   942     fun mk_dtcontab thy =
   943       DatatypeconsNameMangler.empty
   944       |> fold_map
   945           (fn (_, co_dtco) => DatatypeconsNameMangler.declare_multi thy co_dtco)
   946             (fold (fn (co, dtco) =>
   947               let
   948                 val key = ((NameSpace.drop_base o NameSpace.drop_base) co, NameSpace.base co)
   949               in AList.default (op =) (key, []) #> AList.map_entry (op =) key (cons (co, dtco)) end
   950             ) (get_all_datatype_cons thy) [])
   951       |-> (fn _ => I);
   952     fun mk_clsmemtab thy =
   953       Symtab.empty
   954       |> Symtab.fold
   955           (fn (class, (clsmems, _)) => fold
   956             (fn clsmem => Symtab.update (clsmem, class)) clsmems)
   957               (ClassPackage.get_classtab thy);
   958     val deftab = extract_defs thy;
   959     val insttab = mk_insttab thy;
   960     val overltabs = mk_overltabs thy deftab;
   961     val dtcontab = mk_dtcontab thy;
   962     val clsmemtab = mk_clsmemtab thy;
   963   in ((deftab, clsmemtab), (insttab, overltabs, dtcontab)) end;
   964 
   965 fun check_for_target thy target =
   966   if (Symtab.defined o #target_data o CodegenData.get) thy target
   967   then ()
   968   else error ("unknown code target language: " ^ quote target);
   969 
   970 fun map_module f =
   971   map_codegen_data (fn (modl, gens, target_data, logic_data) =>
   972     (f modl, gens, target_data, logic_data));
   973 
   974 fun expand_module gen thy =
   975   (#modl o CodegenData.get) thy
   976   |> start_transact (gen thy (mk_tabs thy))
   977   |-> (fn x:'a => fn modl => (x, map_module (K modl) thy));
   978 
   979 fun rename_inconsistent thy =
   980   let
   981     fun get_inconsistent thyname =
   982       let
   983         val thy = theory thyname;
   984         fun own_tables get =
   985           (get thy)
   986           |> fold (Symtab.fold (Symtab.remove (K true)) o get) (Theory.parents_of thy)
   987           |> Symtab.keys;
   988         val names = own_tables (#2 o #types o Type.rep_tsig o Sign.tsig_of)
   989           @ own_tables (#2 o #declarations o Consts.dest o #consts o Sign.rep_sg);
   990         fun diff names =
   991           fold (fn name =>
   992             if is_prefix (op =) (NameSpace.unpack thyname) (NameSpace.unpack name)
   993             then I
   994             else cons (name, NameSpace.append thyname (NameSpace.base name))) names [];
   995       in diff names end;
   996     val inconsistent = map get_inconsistent (ThyInfo.names ()) |> Library.flat;
   997     fun add (src, dst) thy =
   998       if (is_some oo Symtab.lookup o fst o #alias o #logic_data o CodegenData.get) thy src
   999       then (warning ("code generator alias already defined for " ^ quote src ^ ", will not overwrite"); thy)
  1000       else add_alias (src, dst) thy
  1001   in fold add inconsistent thy end;
  1002 
  1003 fun ensure_datatype_case_consts get_datatype_case_consts get_case_const_data thy =
  1004   let
  1005     fun ensure case_c thy =
  1006       if
  1007         Symtab.defined ((#appconst o #gens o CodegenData.get) thy) case_c
  1008       then
  1009         (warning ("case constant " ^ quote case_c ^ " already present in application table, will not overwrite"); thy)
  1010       else
  1011         add_case_const_i get_case_const_data case_c thy;
  1012   in
  1013     fold ensure (get_datatype_case_consts thy) thy
  1014   end;
  1015 
  1016 
  1017 
  1018 (** target languages **)
  1019 
  1020 (* primitive definitions *)
  1021 
  1022 fun read_typ thy =
  1023   Sign.read_typ (thy, K NONE);
  1024 
  1025 fun read_const thy (raw_c, raw_ty) =
  1026   let
  1027     val c = Sign.intern_const thy raw_c;
  1028     val _ = if Sign.declared_const thy c
  1029       then ()
  1030       else error ("no such constant: " ^ quote c);
  1031     val ty = case raw_ty
  1032      of NONE => Sign.the_const_constraint thy c
  1033       | SOME raw_ty => read_typ thy raw_ty;
  1034   in (c, ty) end;
  1035 
  1036 fun read_quote reader gen raw thy =
  1037   expand_module
  1038     (fn thy => fn tabs => gen thy tabs (reader thy raw))
  1039     thy;
  1040 
  1041 fun gen_add_prim prep_name prep_primdef raw_name deps (target, raw_primdef) thy =
  1042   let
  1043     val _ = if Symtab.defined ((#target_data o CodegenData.get) thy) target
  1044       then () else error ("unknown target language: " ^ quote target);
  1045     val tabs = mk_tabs thy;
  1046     val name = prep_name thy tabs raw_name;
  1047     val primdef = prep_primdef raw_primdef;
  1048   in
  1049     thy
  1050     |> map_module (CodegenThingol.add_prim name deps (target, primdef))
  1051   end;
  1052 
  1053 val add_prim_i = gen_add_prim ((K o K) I) I;
  1054 val add_prim_class = gen_add_prim
  1055   (fn thy => K (idf_of_name thy nsp_class o Sign.intern_class thy))
  1056   (Pretty.str o CodegenSerializer.parse_targetdef I);
  1057 val add_prim_tyco = gen_add_prim
  1058   (fn thy => K (idf_of_name thy nsp_tyco o Sign.intern_type thy))
  1059   (Pretty.str o CodegenSerializer.parse_targetdef I);
  1060 val add_prim_const = gen_add_prim
  1061   (fn thy => fn tabs => idf_of_const thy tabs o read_const thy)
  1062   (Pretty.str o CodegenSerializer.parse_targetdef I);
  1063 
  1064 val ensure_prim = (map_module oo CodegenThingol.ensure_prim);
  1065 
  1066 
  1067 (* syntax *)
  1068 
  1069 val parse_syntax_tyco =
  1070   let
  1071     fun mk reader raw_tyco target thy =
  1072       let
  1073         val _ = check_for_target thy target;
  1074         fun check_tyco tyco =
  1075           if Sign.declared_tyname thy tyco
  1076           then tyco
  1077           else error ("no such type constructor: " ^ quote tyco);
  1078         fun prep_tyco thy tyco =
  1079           tyco
  1080           |> Sign.intern_type thy
  1081           |> check_tyco
  1082           |> idf_of_name thy nsp_tyco;
  1083         val tyco = prep_tyco thy raw_tyco;
  1084       in
  1085         thy
  1086         |> ensure_prim tyco target
  1087         |> reader
  1088         |-> (fn pretty => map_codegen_data
  1089           (fn (modl, gens, target_data, logic_data) =>
  1090              (modl, gens,
  1091               target_data |> Symtab.map_entry target
  1092                 (map_target_data
  1093                   (fn (syntax_tyco, syntax_const) =>
  1094                    (syntax_tyco |> Symtab.update
  1095                       (tyco, (pretty, stamp ())),
  1096                     syntax_const))),
  1097               logic_data)))
  1098       end;
  1099   in
  1100     CodegenSerializer.parse_syntax (read_quote read_typ exprgen_type)
  1101     #-> (fn reader => pair (mk reader))
  1102   end;
  1103 
  1104 fun add_pretty_syntax_const c target pretty =
  1105   map_codegen_data
  1106     (fn (modl, gens, target_data, logic_data) =>
  1107        (modl, gens,
  1108         target_data |> Symtab.map_entry target
  1109           (map_target_data
  1110             (fn (syntax_tyco, syntax_const) =>
  1111              (syntax_tyco,
  1112               syntax_const
  1113               |> Symtab.update
  1114                  (c, (pretty, stamp ()))))),
  1115         logic_data));
  1116 
  1117 val parse_syntax_const =
  1118   let
  1119     fun mk reader raw_const target thy =
  1120       let
  1121         val _ = check_for_target thy target;
  1122         val tabs = mk_tabs thy;
  1123         val c = idf_of_const thy tabs (read_const thy raw_const);
  1124       in
  1125         thy
  1126         |> ensure_prim c target
  1127         |> reader
  1128         |-> (fn pretty => add_pretty_syntax_const c target pretty)
  1129       end;
  1130   in
  1131     CodegenSerializer.parse_syntax (read_quote Sign.read_term exprgen_term)
  1132     #-> (fn reader => pair (mk reader))
  1133   end;
  1134 
  1135 fun add_pretty_list raw_nil raw_cons (target, seri) thy =
  1136   let
  1137     val _ = check_for_target thy target;
  1138     val tabs = mk_tabs thy;
  1139     fun mk_const raw_name =
  1140       let
  1141         val name = Sign.intern_const thy raw_name;
  1142       in idf_of_const thy tabs (name, Sign.the_const_type thy name) end;
  1143     val nil' = mk_const raw_nil;
  1144     val cons' = mk_const raw_cons;
  1145     val pr' = CodegenSerializer.pretty_list nil' cons' seri;
  1146   in
  1147     thy
  1148     |> ensure_prim cons' target
  1149     |> add_pretty_syntax_const cons' target pr'
  1150   end;
  1151 
  1152 
  1153 
  1154 (** code generation **)
  1155 
  1156 fun generate_code raw_consts thy =
  1157   let
  1158     val consts = map (read_const thy) raw_consts;
  1159     fun generate thy tabs = fold_map (ensure_def_const thy tabs) consts
  1160   in
  1161     thy
  1162     |> expand_module generate
  1163   end;
  1164 
  1165 fun serialize_code target filename raw_consts thy =
  1166   let
  1167     fun get_serializer thy target =
  1168       let
  1169         val _ = check_for_target thy target;
  1170         val target_data =
  1171           thy
  1172           |> CodegenData.get
  1173           |> #target_data
  1174           |> (fn data => (the oo Symtab.lookup) data target);
  1175       in
  1176         (the o Symtab.lookup (! serializers)) target (
  1177           (Option.map fst oo Symtab.lookup o #syntax_tyco) target_data,
  1178           (Option.map fst oo Symtab.lookup o #syntax_const) target_data
  1179         )
  1180       end;
  1181     fun use_code code =
  1182       if target = "ml" andalso filename = "-"
  1183       then use_text Context.ml_output false code
  1184       else File.write (Path.unpack filename) (code ^ "\n");
  1185     fun serialize thy cs =
  1186       let
  1187         val module = (#modl o CodegenData.get) thy;
  1188         val seri = get_serializer thy target "Generated";
  1189       in case cs
  1190        of [] => seri NONE module () |> fst |> Pretty.output |> use_code
  1191         | cs => seri (SOME cs) module () |> fst |> Pretty.output |> use_code
  1192       end;
  1193   in
  1194     thy
  1195     |> (if is_some raw_consts then generate_code (the raw_consts) else pair [])
  1196     |-> (fn cs => `(fn thy => serialize thy cs))
  1197     |-> (fn _ => I)
  1198   end;
  1199 
  1200 
  1201 
  1202 (** toplevel interface **)
  1203 
  1204 local
  1205 
  1206 structure P = OuterParse
  1207 and K = OuterKeyword
  1208 
  1209 in
  1210 
  1211 val (classK, generateK, serializeK,
  1212      primclassK, primtycoK, primconstK,
  1213      syntax_tycoK, syntax_constK, aliasK) =
  1214   ("code_class", "code_generate", "code_serialize",
  1215    "code_primclass", "code_primtyco", "code_primconst",
  1216    "code_syntax_tyco", "code_syntax_const", "code_alias");
  1217 val (constantsK, dependingK) =
  1218   ("constants", "depending_on");
  1219 
  1220 val classP =
  1221   OuterSyntax.command classK "codegen data for classes" K.thy_decl (
  1222     P.xname
  1223     -- ((P.$$$ "\\<Rightarrow>" || P.$$$ "=>") |-- (P.list1 P.name))
  1224     -- (Scan.optional ((P.$$$ "\\<Rightarrow>" || P.$$$ "=>") |-- (P.list1 P.name)) [])
  1225     >> (fn ((name, tycos), consts) => (Toplevel.theory (ClassPackage.add_classentry name consts tycos)))
  1226   )
  1227 
  1228 val generateP =
  1229   OuterSyntax.command generateK "generate executable code for constants" K.thy_decl (
  1230     Scan.repeat1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ))
  1231     >> (fn consts =>
  1232           Toplevel.theory (generate_code consts #> snd))
  1233   );
  1234 
  1235 val serializeP =
  1236   OuterSyntax.command serializeK "serialize executable code for constants" K.thy_decl (
  1237     P.name
  1238     -- P.name
  1239     -- Scan.option (
  1240          P.$$$ constantsK
  1241          |-- Scan.repeat1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ))
  1242        )
  1243     >> (fn ((target, filename), raw_consts) =>
  1244           Toplevel.theory (serialize_code target filename raw_consts))
  1245   );
  1246 
  1247 val aliasP =
  1248   OuterSyntax.command aliasK "declare an alias for a theory identifier" K.thy_decl (
  1249     Scan.repeat1 (P.name -- P.name)
  1250       >> (Toplevel.theory oo fold) add_alias
  1251   );
  1252 
  1253 val primclassP =
  1254   OuterSyntax.command primclassK "define target-lanugage specific class" K.thy_decl (
  1255     P.xname
  1256     -- Scan.optional (P.$$$ dependingK |--
  1257          P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") []
  1258     -- Scan.repeat1 (P.name -- P.text)
  1259       >> (fn ((raw_class, depends), primdefs) =>
  1260             (Toplevel.theory oo fold) (add_prim_class raw_class depends) primdefs)
  1261   );
  1262 
  1263 val primtycoP =
  1264   OuterSyntax.command primtycoK "define target-lanugage specific type constructor" K.thy_decl (
  1265     P.xname
  1266     -- Scan.optional (P.$$$ dependingK |--
  1267          P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") []
  1268     -- Scan.repeat1 (P.name -- P.text)
  1269       >> (fn ((raw_tyco, depends), primdefs) =>
  1270             (Toplevel.theory oo fold) (add_prim_tyco raw_tyco depends) primdefs)
  1271   );
  1272 
  1273 val primconstP =
  1274   OuterSyntax.command primconstK "define target-lanugage specific constant" K.thy_decl (
  1275     (P.xname -- Scan.option (P.$$$ "::" |-- P.typ))
  1276     -- Scan.optional (P.$$$ dependingK |--
  1277          P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") []
  1278     -- Scan.repeat1 (P.name -- P.text)
  1279       >> (fn ((raw_const, depends), primdefs) =>
  1280             (Toplevel.theory oo fold) (add_prim_const raw_const depends) primdefs)
  1281   );
  1282 
  1283 val _ : OuterParse.token list -> (string -> string -> theory -> theory) * OuterParse.token list
  1284  = parse_syntax_tyco;
  1285 
  1286 val syntax_tycoP =
  1287   OuterSyntax.command syntax_tycoK "define code syntax for type constructor" K.thy_decl (
  1288     Scan.repeat1 (
  1289       P.xname
  1290       -- Scan.repeat1 (
  1291            P.name -- parse_syntax_tyco
  1292          )
  1293     )
  1294     >> (Toplevel.theory oo fold) (fn (raw_tyco, syns) =>
  1295           fold (fn (target, modifier) => modifier raw_tyco target) syns)
  1296   );
  1297 
  1298 val syntax_constP =
  1299   OuterSyntax.command syntax_constK "define code syntax for constant" K.thy_decl (
  1300     Scan.repeat1 (
  1301       (P.xname -- Scan.option (P.$$$ "::" |-- P.typ))
  1302       -- Scan.repeat1 (
  1303            P.name -- parse_syntax_const
  1304          )
  1305     )
  1306     >> (Toplevel.theory oo fold) (fn (raw_c, syns) =>
  1307           fold (fn (target, modifier) => modifier raw_c target) syns)
  1308   );
  1309 
  1310 val _ = OuterSyntax.add_parsers [classP, generateP, serializeP, aliasP,
  1311   primclassP, primtycoP, primconstP, syntax_tycoP, syntax_constP];
  1312 val _ = OuterSyntax.add_keywords ["\\<Rightarrow>", "=>", constantsK, dependingK];
  1313 
  1314 
  1315 
  1316 (** theory setup **)
  1317 
  1318 val _ = Context.add_setup
  1319  (add_eqextr ("defs", eqextr_defs) #>
  1320   add_defgen ("clsdecl", defgen_clsdecl) #>
  1321   add_defgen ("funs", defgen_funs) #>
  1322   add_defgen ("clsmem", defgen_clsmem) #>
  1323   add_defgen ("datatypecons", defgen_datatypecons));
  1324 
  1325 (*   add_appconst_i ("op =", ((2, 2), appgen_eq))  *)
  1326 (*   add_defgen ("clsinst", defgen_clsinst)  *)
  1327 
  1328 end; (* local *)
  1329 
  1330 end; (* struct *)