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