src/HOL/Tools/datatype_codegen.ML
author haftmann
Thu Apr 06 16:10:46 2006 +0200 (2006-04-06)
changeset 19346 c4c003abd830
parent 19008 14c1b2f5dda4
child 19458 a70f1b0f09cd
permissions -rw-r--r--
cleanup in datatype package
berghofe@12446
     1
(*  Title:      HOL/datatype_codegen.ML
berghofe@12445
     2
    ID:         $Id$
berghofe@12445
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@12445
     4
berghofe@12445
     5
Code generator for inductive datatypes.
berghofe@12445
     6
*)
berghofe@12445
     7
berghofe@12445
     8
signature DATATYPE_CODEGEN =
berghofe@12445
     9
sig
haftmann@19346
    10
  val get_datatype_spec_thms: theory -> string
haftmann@19346
    11
    -> (((string * sort) list * (string * typ list) list) * tactic) option
haftmann@19346
    12
  val get_case_const_data: theory -> string -> (string * int) list option
haftmann@19346
    13
  val get_all_datatype_cons: theory -> (string * string) list
haftmann@19346
    14
  val get_datatype_case_consts: theory -> string list
wenzelm@18708
    15
  val setup: theory -> theory
berghofe@12445
    16
end;
berghofe@12445
    17
berghofe@12445
    18
structure DatatypeCodegen : DATATYPE_CODEGEN =
berghofe@12445
    19
struct
berghofe@12445
    20
berghofe@12445
    21
open Codegen;
berghofe@12445
    22
berghofe@12445
    23
fun mk_tuple [p] = p
berghofe@12445
    24
  | mk_tuple ps = Pretty.block (Pretty.str "(" ::
skalberg@15570
    25
      List.concat (separate [Pretty.str ",", Pretty.brk 1] (map single ps)) @
berghofe@12445
    26
        [Pretty.str ")"]);
berghofe@12445
    27
berghofe@12445
    28
(**** datatype definition ****)
berghofe@12445
    29
berghofe@14104
    30
(* find shortest path to constructor with no recursive arguments *)
berghofe@14104
    31
berghofe@14104
    32
fun find_nonempty (descr: DatatypeAux.descr) is i =
berghofe@14104
    33
  let
haftmann@17521
    34
    val (_, _, constrs) = valOf (AList.lookup (op =) descr i);
skalberg@15531
    35
    fun arg_nonempty (_, DatatypeAux.DtRec i) = if i mem is then NONE
skalberg@15570
    36
          else Option.map (curry op + 1 o snd) (find_nonempty descr (i::is) i)
skalberg@15531
    37
      | arg_nonempty _ = SOME 0;
skalberg@15570
    38
    fun max xs = Library.foldl
skalberg@15531
    39
      (fn (NONE, _) => NONE
skalberg@15531
    40
        | (SOME i, SOME j) => SOME (Int.max (i, j))
skalberg@15531
    41
        | (_, NONE) => NONE) (SOME 0, xs);
berghofe@14104
    42
    val xs = sort (int_ord o pairself snd)
skalberg@15570
    43
      (List.mapPartial (fn (s, dts) => Option.map (pair s)
berghofe@14104
    44
        (max (map (arg_nonempty o DatatypeAux.strip_dtyp) dts))) constrs)
skalberg@15531
    45
  in case xs of [] => NONE | x :: _ => SOME x end;
berghofe@14104
    46
berghofe@17144
    47
fun add_dt_defs thy defs dep module gr (descr: DatatypeAux.descr) =
berghofe@12445
    48
  let
berghofe@12445
    49
    val sg = sign_of thy;
berghofe@12445
    50
    val tab = DatatypePackage.get_datatypes thy;
berghofe@12445
    51
skalberg@15570
    52
    val descr' = List.filter (can (map DatatypeAux.dest_DtTFree o #2 o snd)) descr;
skalberg@15570
    53
    val rtnames = map (#1 o snd) (List.filter (fn (_, (_, _, cs)) =>
berghofe@14104
    54
      exists (exists DatatypeAux.is_rec_type o snd) cs) descr');
berghofe@12445
    55
berghofe@17144
    56
    val (_, (tname, _, _)) :: _ = descr';
berghofe@17144
    57
    val node_id = tname ^ " (type)";
berghofe@17144
    58
    val module' = if_library (thyname_of_type tname thy) module;
berghofe@12445
    59
berghofe@12445
    60
    fun mk_dtdef gr prfx [] = (gr, [])
berghofe@12445
    61
      | mk_dtdef gr prfx ((_, (tname, dts, cs))::xs) =
berghofe@12445
    62
          let
berghofe@12445
    63
            val tvs = map DatatypeAux.dest_DtTFree dts;
berghofe@12445
    64
            val sorts = map (rpair []) tvs;
berghofe@12445
    65
            val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs;
berghofe@17144
    66
            val (gr', (_, type_id)) = mk_type_id module' tname gr;
berghofe@17144
    67
            val (gr'', ps) =
berghofe@17144
    68
              foldl_map (fn (gr, (cname, cargs)) =>
berghofe@17144
    69
                foldl_map (invoke_tycodegen thy defs node_id module' false)
berghofe@17144
    70
                  (gr, cargs) |>>>
berghofe@17144
    71
                mk_const_id module' cname) (gr', cs');
berghofe@17144
    72
            val (gr''', rest) = mk_dtdef gr'' "and " xs
berghofe@12445
    73
          in
berghofe@17144
    74
            (gr''',
berghofe@12445
    75
             Pretty.block (Pretty.str prfx ::
berghofe@12445
    76
               (if null tvs then [] else
berghofe@12445
    77
                  [mk_tuple (map Pretty.str tvs), Pretty.str " "]) @
berghofe@17144
    78
               [Pretty.str (type_id ^ " ="), Pretty.brk 1] @
skalberg@15570
    79
               List.concat (separate [Pretty.brk 1, Pretty.str "| "]
berghofe@17144
    80
                 (map (fn (ps', (_, cname)) => [Pretty.block
berghofe@17144
    81
                   (Pretty.str cname ::
berghofe@12445
    82
                    (if null ps' then [] else
skalberg@15570
    83
                     List.concat ([Pretty.str " of", Pretty.brk 1] ::
berghofe@12445
    84
                       separate [Pretty.str " *", Pretty.brk 1]
berghofe@12445
    85
                         (map single ps'))))]) ps))) :: rest)
berghofe@13754
    86
          end;
berghofe@13754
    87
berghofe@17144
    88
    fun mk_term_of_def gr prfx [] = []
berghofe@17144
    89
      | mk_term_of_def gr prfx ((_, (tname, dts, cs)) :: xs) =
berghofe@13754
    90
          let
berghofe@13754
    91
            val tvs = map DatatypeAux.dest_DtTFree dts;
berghofe@13754
    92
            val sorts = map (rpair []) tvs;
berghofe@13754
    93
            val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs;
berghofe@13754
    94
            val dts' = map (DatatypeAux.typ_of_dtyp descr sorts) dts;
berghofe@13754
    95
            val T = Type (tname, dts');
berghofe@17144
    96
            val rest = mk_term_of_def gr "and " xs;
berghofe@13754
    97
            val (_, eqs) = foldl_map (fn (prfx, (cname, Ts)) =>
berghofe@13754
    98
              let val args = map (fn i =>
berghofe@13754
    99
                Pretty.str ("x" ^ string_of_int i)) (1 upto length Ts)
berghofe@13754
   100
              in ("  | ", Pretty.blk (4,
berghofe@17144
   101
                [Pretty.str prfx, mk_term_of gr module' false T, Pretty.brk 1,
berghofe@17144
   102
                 if null Ts then Pretty.str (snd (get_const_id cname gr))
berghofe@16645
   103
                 else parens (Pretty.block
berghofe@17144
   104
                   [Pretty.str (snd (get_const_id cname gr)),
berghofe@13754
   105
                    Pretty.brk 1, mk_tuple args]),
berghofe@13754
   106
                 Pretty.str " =", Pretty.brk 1] @
skalberg@15570
   107
                 List.concat (separate [Pretty.str " $", Pretty.brk 1]
berghofe@13754
   108
                   ([Pretty.str ("Const (\"" ^ cname ^ "\","), Pretty.brk 1,
berghofe@13754
   109
                     mk_type false (Ts ---> T), Pretty.str ")"] ::
berghofe@17144
   110
                    map (fn (x, U) => [Pretty.block [mk_term_of gr module' false U,
berghofe@13754
   111
                      Pretty.brk 1, x]]) (args ~~ Ts)))))
berghofe@13754
   112
              end) (prfx, cs')
berghofe@14104
   113
          in eqs @ rest end;
berghofe@14104
   114
berghofe@17144
   115
    fun mk_gen_of_def gr prfx [] = []
berghofe@17144
   116
      | mk_gen_of_def gr prfx ((i, (tname, dts, cs)) :: xs) =
berghofe@14104
   117
          let
berghofe@14104
   118
            val tvs = map DatatypeAux.dest_DtTFree dts;
berghofe@14104
   119
            val sorts = map (rpair []) tvs;
berghofe@14104
   120
            val (cs1, cs2) =
skalberg@15570
   121
              List.partition (exists DatatypeAux.is_rec_type o snd) cs;
skalberg@15531
   122
            val SOME (cname, _) = find_nonempty descr [i] i;
berghofe@14104
   123
berghofe@14104
   124
            fun mk_delay p = Pretty.block
berghofe@14104
   125
              [Pretty.str "fn () =>", Pretty.brk 1, p];
berghofe@14104
   126
berghofe@15397
   127
            fun mk_constr s b (cname, dts) =
berghofe@14104
   128
              let
berghofe@17144
   129
                val gs = map (fn dt => mk_app false (mk_gen gr module' false rtnames s
berghofe@15397
   130
                    (DatatypeAux.typ_of_dtyp descr sorts dt))
berghofe@15397
   131
                  [Pretty.str (if b andalso DatatypeAux.is_rec_type dt then "0"
berghofe@15397
   132
                     else "j")]) dts;
berghofe@17144
   133
                val (_, id) = get_const_id cname gr
berghofe@14104
   134
              in case gs of
berghofe@14104
   135
                  _ :: _ :: _ => Pretty.block
berghofe@14104
   136
                    [Pretty.str id, Pretty.brk 1, mk_tuple gs]
berghofe@14104
   137
                | _ => mk_app false (Pretty.str id) (map parens gs)
berghofe@14104
   138
              end;
berghofe@14104
   139
berghofe@15397
   140
            fun mk_choice [c] = mk_constr "(i-1)" false c
berghofe@14104
   141
              | mk_choice cs = Pretty.block [Pretty.str "one_of",
berghofe@14104
   142
                  Pretty.brk 1, Pretty.blk (1, Pretty.str "[" ::
skalberg@15570
   143
                  List.concat (separate [Pretty.str ",", Pretty.fbrk]
berghofe@15397
   144
                    (map (single o mk_delay o mk_constr "(i-1)" false) cs)) @
berghofe@14104
   145
                  [Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"];
berghofe@14104
   146
berghofe@14104
   147
            val gs = map (Pretty.str o suffix "G" o strip_tname) tvs;
berghofe@17144
   148
            val gen_name = "gen_" ^ snd (get_type_id tname gr)
berghofe@14104
   149
berghofe@14104
   150
          in
berghofe@14104
   151
            Pretty.blk (4, separate (Pretty.brk 1) 
berghofe@14104
   152
                (Pretty.str (prfx ^ gen_name ^
berghofe@14104
   153
                   (if null cs1 then "" else "'")) :: gs @
berghofe@14104
   154
                 (if null cs1 then [] else [Pretty.str "i"]) @
berghofe@14104
   155
                 [Pretty.str "j"]) @
berghofe@14104
   156
              [Pretty.str " =", Pretty.brk 1] @
berghofe@14104
   157
              (if not (null cs1) andalso not (null cs2)
berghofe@14104
   158
               then [Pretty.str "frequency", Pretty.brk 1,
berghofe@14104
   159
                 Pretty.blk (1, [Pretty.str "[",
berghofe@14104
   160
                   mk_tuple [Pretty.str "i", mk_delay (mk_choice cs1)],
berghofe@14104
   161
                   Pretty.str ",", Pretty.fbrk,
berghofe@14104
   162
                   mk_tuple [Pretty.str "1", mk_delay (mk_choice cs2)],
berghofe@14104
   163
                   Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"]
berghofe@14104
   164
               else if null cs2 then
berghofe@14104
   165
                 [Pretty.block [Pretty.str "(case", Pretty.brk 1,
berghofe@14104
   166
                   Pretty.str "i", Pretty.brk 1, Pretty.str "of",
berghofe@14104
   167
                   Pretty.brk 1, Pretty.str "0 =>", Pretty.brk 1,
haftmann@17521
   168
                   mk_constr "0" true (cname, valOf (AList.lookup (op =) cs cname)),
berghofe@14104
   169
                   Pretty.brk 1, Pretty.str "| _ =>", Pretty.brk 1,
berghofe@14104
   170
                   mk_choice cs1, Pretty.str ")"]]
berghofe@14104
   171
               else [mk_choice cs2])) ::
berghofe@14104
   172
            (if null cs1 then []
berghofe@14104
   173
             else [Pretty.blk (4, separate (Pretty.brk 1) 
berghofe@14104
   174
                 (Pretty.str ("and " ^ gen_name) :: gs @ [Pretty.str "i"]) @
berghofe@14104
   175
               [Pretty.str " =", Pretty.brk 1] @
berghofe@14104
   176
               separate (Pretty.brk 1) (Pretty.str (gen_name ^ "'") :: gs @
berghofe@14104
   177
                 [Pretty.str "i", Pretty.str "i"]))]) @
berghofe@17144
   178
            mk_gen_of_def gr "and " xs
berghofe@14104
   179
          end
berghofe@13754
   180
berghofe@12445
   181
  in
berghofe@17144
   182
    (add_edge_acyclic (node_id, dep) gr
berghofe@12445
   183
        handle Graph.CYCLES _ => gr) handle Graph.UNDEF _ =>
berghofe@12445
   184
         let
berghofe@17144
   185
           val gr1 = add_edge (node_id, dep)
berghofe@17144
   186
             (new_node (node_id, (NONE, "", "")) gr);
berghofe@12445
   187
           val (gr2, dtdef) = mk_dtdef gr1 "datatype " descr';
berghofe@12445
   188
         in
berghofe@17144
   189
           map_node node_id (K (NONE, module',
berghofe@12445
   190
             Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk dtdef @
berghofe@13754
   191
               [Pretty.str ";"])) ^ "\n\n" ^
berghofe@13754
   192
             (if "term_of" mem !mode then
berghofe@13754
   193
                Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk
berghofe@17144
   194
                  (mk_term_of_def gr2 "fun " descr') @ [Pretty.str ";"])) ^ "\n\n"
berghofe@14104
   195
              else "") ^
berghofe@14104
   196
             (if "test" mem !mode then
berghofe@14104
   197
                Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk
berghofe@17144
   198
                  (mk_gen_of_def gr2 "fun " descr') @ [Pretty.str ";"])) ^ "\n\n"
berghofe@13754
   199
              else ""))) gr2
berghofe@17144
   200
         end
berghofe@12445
   201
  end;
berghofe@12445
   202
berghofe@12445
   203
berghofe@12445
   204
(**** case expressions ****)
berghofe@12445
   205
berghofe@17144
   206
fun pretty_case thy defs gr dep module brack constrs (c as Const (_, T)) ts =
berghofe@12445
   207
  let val i = length constrs
berghofe@12445
   208
  in if length ts <= i then
berghofe@17144
   209
       invoke_codegen thy defs dep module brack (gr, eta_expand c ts (i+1))
berghofe@12445
   210
    else
berghofe@12445
   211
      let
skalberg@15570
   212
        val ts1 = Library.take (i, ts);
skalberg@15570
   213
        val t :: ts2 = Library.drop (i, ts);
skalberg@15574
   214
        val names = foldr add_term_names
skalberg@15574
   215
          (map (fst o fst o dest_Var) (foldr add_term_vars [] ts1)) ts1;
skalberg@15570
   216
        val (Ts, dT) = split_last (Library.take (i+1, fst (strip_type T)));
berghofe@12445
   217
berghofe@12445
   218
        fun pcase gr [] [] [] = ([], gr)
berghofe@12445
   219
          | pcase gr ((cname, cargs)::cs) (t::ts) (U::Us) =
berghofe@12445
   220
              let
berghofe@12445
   221
                val j = length cargs;
berghofe@12445
   222
                val xs = variantlist (replicate j "x", names);
skalberg@15570
   223
                val Us' = Library.take (j, fst (strip_type U));
berghofe@12445
   224
                val frees = map Free (xs ~~ Us');
berghofe@17144
   225
                val (gr0, cp) = invoke_codegen thy defs dep module false
berghofe@12445
   226
                  (gr, list_comb (Const (cname, Us' ---> dT), frees));
berghofe@12445
   227
                val t' = Envir.beta_norm (list_comb (t, frees));
berghofe@17144
   228
                val (gr1, p) = invoke_codegen thy defs dep module false (gr0, t');
berghofe@12445
   229
                val (ps, gr2) = pcase gr1 cs ts Us;
berghofe@12445
   230
              in
berghofe@12445
   231
                ([Pretty.block [cp, Pretty.str " =>", Pretty.brk 1, p]] :: ps, gr2)
berghofe@12445
   232
              end;
berghofe@12445
   233
berghofe@12445
   234
        val (ps1, gr1) = pcase gr constrs ts1 Ts;
skalberg@15570
   235
        val ps = List.concat (separate [Pretty.brk 1, Pretty.str "| "] ps1);
berghofe@17144
   236
        val (gr2, p) = invoke_codegen thy defs dep module false (gr1, t);
berghofe@17144
   237
        val (gr3, ps2) = foldl_map (invoke_codegen thy defs dep module true) (gr2, ts2)
berghofe@12445
   238
      in (gr3, (if not (null ts2) andalso brack then parens else I)
berghofe@12445
   239
        (Pretty.block (separate (Pretty.brk 1)
berghofe@12445
   240
          (Pretty.block ([Pretty.str "(case ", p, Pretty.str " of",
berghofe@12445
   241
             Pretty.brk 1] @ ps @ [Pretty.str ")"]) :: ps2))))
berghofe@12445
   242
      end
berghofe@12445
   243
  end;
berghofe@12445
   244
berghofe@12445
   245
berghofe@12445
   246
(**** constructors ****)
berghofe@12445
   247
berghofe@17144
   248
fun pretty_constr thy defs gr dep module brack args (c as Const (s, T)) ts =
berghofe@12445
   249
  let val i = length args
berghofe@12445
   250
  in if i > 1 andalso length ts < i then
berghofe@17144
   251
      invoke_codegen thy defs dep module brack (gr, eta_expand c ts i)
berghofe@12445
   252
     else
berghofe@12445
   253
       let
berghofe@17144
   254
         val id = mk_qual_id module (get_const_id s gr);
berghofe@16645
   255
         val (gr', ps) = foldl_map
berghofe@17144
   256
           (invoke_codegen thy defs dep module (i = 1)) (gr, ts);
berghofe@12445
   257
       in (case args of
berghofe@12445
   258
          _ :: _ :: _ => (gr', (if brack then parens else I)
berghofe@12445
   259
            (Pretty.block [Pretty.str id, Pretty.brk 1, mk_tuple ps]))
berghofe@12445
   260
        | _ => (gr', mk_app brack (Pretty.str id) ps))
berghofe@12445
   261
       end
berghofe@12445
   262
  end;
berghofe@12445
   263
berghofe@12445
   264
berghofe@12445
   265
(**** code generators for terms and types ****)
berghofe@12445
   266
berghofe@17144
   267
fun datatype_codegen thy defs gr dep module brack t = (case strip_comb t of
berghofe@12445
   268
   (c as Const (s, T), ts) =>
haftmann@18247
   269
       (case Library.find_first (fn (_, {index, descr, case_name, ...}) =>
berghofe@12445
   270
         s = case_name orelse
haftmann@17521
   271
           AList.defined (op =) ((#3 o the o AList.lookup (op =) descr) index) s)
berghofe@12445
   272
             (Symtab.dest (DatatypePackage.get_datatypes thy)) of
skalberg@15531
   273
          NONE => NONE
skalberg@15531
   274
        | SOME (tname, {index, descr, ...}) =>
haftmann@18247
   275
           if is_some (get_assoc_code thy s T) then NONE else
haftmann@17521
   276
           let val SOME (_, _, constrs) = AList.lookup (op =) descr index
haftmann@17521
   277
           in (case (AList.lookup (op =) constrs s, strip_type T) of
berghofe@17144
   278
               (NONE, _) => SOME (pretty_case thy defs gr dep module brack
haftmann@18247
   279
                 ((#3 o the o AList.lookup (op =) descr) index) c ts)
berghofe@16645
   280
             | (SOME args, (_, Type _)) => SOME (pretty_constr thy defs
berghofe@17144
   281
                 (fst (invoke_tycodegen thy defs dep module false
berghofe@16645
   282
                    (gr, snd (strip_type T))))
berghofe@17144
   283
                 dep module brack args c ts)
skalberg@15531
   284
             | _ => NONE)
berghofe@12445
   285
           end)
skalberg@15531
   286
 |  _ => NONE);
berghofe@12445
   287
berghofe@17144
   288
fun datatype_tycodegen thy defs gr dep module brack (Type (s, Ts)) =
wenzelm@17412
   289
      (case Symtab.lookup (DatatypePackage.get_datatypes thy) s of
skalberg@15531
   290
         NONE => NONE
skalberg@15531
   291
       | SOME {descr, ...} =>
skalberg@15570
   292
           if isSome (get_assoc_type thy s) then NONE else
berghofe@16645
   293
           let
berghofe@16645
   294
             val (gr', ps) = foldl_map
berghofe@17144
   295
               (invoke_tycodegen thy defs dep module false) (gr, Ts);
berghofe@17144
   296
             val gr'' = add_dt_defs thy defs dep module gr' descr
berghofe@16645
   297
           in SOME (gr'',
berghofe@12445
   298
             Pretty.block ((if null Ts then [] else
berghofe@12445
   299
               [mk_tuple ps, Pretty.str " "]) @
berghofe@17144
   300
               [Pretty.str (mk_qual_id module (get_type_id s gr''))]))
berghofe@12445
   301
           end)
berghofe@16645
   302
  | datatype_tycodegen _ _ _ _ _ _ _ = NONE;
berghofe@12445
   303
haftmann@18247
   304
haftmann@19346
   305
(** code 2nd generation **)
haftmann@19346
   306
haftmann@19346
   307
fun datatype_tac thy dtco =
haftmann@19346
   308
  let
haftmann@19346
   309
    val ctxt = Context.init_proof thy;
haftmann@19346
   310
    val inject = (#inject o DatatypePackage.the_datatype thy) dtco;
haftmann@19346
   311
    val simpset = Simplifier.context ctxt
haftmann@19346
   312
      (MetaSimplifier.empty_ss addsimprocs [distinct_simproc]);
haftmann@19346
   313
  in
haftmann@19346
   314
    (TRY o ALLGOALS o resolve_tac) [HOL.eq_reflection]
haftmann@19346
   315
    THEN (
haftmann@19346
   316
      (ALLGOALS o resolve_tac) (eqTrueI :: inject)
haftmann@19346
   317
      ORELSE (ALLGOALS o simp_tac) simpset
haftmann@19346
   318
    )
haftmann@19346
   319
    THEN (ALLGOALS o resolve_tac) [HOL.refl, Drule.reflexive_thm]
haftmann@19346
   320
  end;
haftmann@19346
   321
haftmann@19346
   322
fun get_datatype_spec_thms thy dtco =
haftmann@19346
   323
  case DatatypePackage.get_datatype_spec thy dtco
haftmann@19346
   324
   of SOME vs_cos =>
haftmann@19346
   325
        SOME (vs_cos, datatype_tac thy dtco)
haftmann@19346
   326
    | NONE => NONE;
haftmann@19346
   327
haftmann@19346
   328
fun get_all_datatype_cons thy =
haftmann@19346
   329
  Symtab.fold (fn (dtco, _) => fold
haftmann@19346
   330
    (fn (co, _) => cons (co, dtco))
haftmann@19346
   331
      ((snd o the oo DatatypePackage.get_datatype_spec) thy dtco))
haftmann@19346
   332
        (DatatypePackage.get_datatypes thy) [];
haftmann@19346
   333
haftmann@19346
   334
fun get_case_const_data thy c =
haftmann@19346
   335
  case find_first (fn (_, {index, descr, case_name, ...}) =>
haftmann@19346
   336
      case_name = c
haftmann@19346
   337
    ) ((Symtab.dest o DatatypePackage.get_datatypes) thy)
haftmann@19346
   338
   of NONE => NONE
haftmann@19346
   339
    | SOME (_, {index, descr, ...}) =>
haftmann@19346
   340
        (SOME o map (apsnd length) o #3 o the o AList.lookup (op =) descr) index;
haftmann@19346
   341
haftmann@19346
   342
fun get_datatype_case_consts thy =
haftmann@19346
   343
  Symtab.fold (fn (_, {case_name, ...}) => cons case_name)
haftmann@19346
   344
    (DatatypePackage.get_datatypes thy) [];
haftmann@19346
   345
haftmann@19008
   346
val setup = 
wenzelm@18708
   347
  add_codegen "datatype" datatype_codegen #>
wenzelm@18708
   348
  add_tycodegen "datatype" datatype_tycodegen #>
haftmann@19346
   349
  CodegenTheorems.add_datatype_extr
haftmann@19346
   350
    get_datatype_spec_thms #>
haftmann@18963
   351
  CodegenPackage.set_get_datatype
haftmann@19346
   352
    DatatypePackage.get_datatype_spec #>
haftmann@18451
   353
  CodegenPackage.set_get_all_datatype_cons
haftmann@19346
   354
    get_all_datatype_cons #>
haftmann@18518
   355
  CodegenPackage.ensure_datatype_case_consts
haftmann@19346
   356
    get_datatype_case_consts
haftmann@19346
   357
    get_case_const_data;
berghofe@12445
   358
berghofe@12445
   359
end;