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