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