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