src/HOL/Tools/datatype_codegen.ML
author skalberg
Fri, 04 Mar 2005 15:07:34 +0100
changeset 15574 b1d1b5bfc464
parent 15570 8d8c70b41bab
child 16645 a152d6b21c31
permissions -rw-r--r--
Removed practically all references to Library.foldr.
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
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    10
  val setup: (theory -> theory) list
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    11
end;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    12
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    13
structure DatatypeCodegen : DATATYPE_CODEGEN =
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    14
struct
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    15
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    16
open Codegen;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    17
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    18
fun mk_tuple [p] = p
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    19
  | mk_tuple ps = Pretty.block (Pretty.str "(" ::
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    20
      List.concat (separate [Pretty.str ",", Pretty.brk 1] (map single ps)) @
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    21
        [Pretty.str ")"]);
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    22
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    23
(**** datatype definition ****)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    24
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
    25
(* find shortest path to constructor with no recursive arguments *)
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
    26
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
    27
fun find_nonempty (descr: DatatypeAux.descr) is i =
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
    28
  let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    29
    val (_, _, constrs) = valOf (assoc (descr, i));
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
    30
    fun arg_nonempty (_, DatatypeAux.DtRec i) = if i mem is then NONE
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    31
          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
    32
      | arg_nonempty _ = SOME 0;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    33
    fun max xs = Library.foldl
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
    34
      (fn (NONE, _) => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
    35
        | (SOME i, SOME j) => SOME (Int.max (i, j))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
    36
        | (_, NONE) => NONE) (SOME 0, xs);
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
    37
    val xs = sort (int_ord o pairself snd)
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    38
      (List.mapPartial (fn (s, dts) => Option.map (pair s)
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
    39
        (max (map (arg_nonempty o DatatypeAux.strip_dtyp) dts))) constrs)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
    40
  in case xs of [] => NONE | x :: _ => SOME x end;
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
    41
12890
75b254b1c8ba made MLWorks happy;
wenzelm
parents: 12822
diff changeset
    42
fun add_dt_defs thy dep gr (descr: DatatypeAux.descr) =
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    43
  let
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    44
    val sg = sign_of thy;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    45
    val tab = DatatypePackage.get_datatypes thy;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    46
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    47
    val descr' = List.filter (can (map DatatypeAux.dest_DtTFree o #2 o snd)) descr;
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    48
    val rtnames = map (#1 o snd) (List.filter (fn (_, (_, _, cs)) =>
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
    49
      exists (exists DatatypeAux.is_rec_type o snd) cs) descr');
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    50
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    51
    val (_, (_, _, (cname, _) :: _)) :: _ = descr';
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    52
    val dname = mk_const_id sg cname;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    53
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    54
    fun mk_dtdef gr prfx [] = (gr, [])
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    55
      | mk_dtdef gr prfx ((_, (tname, dts, cs))::xs) =
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    56
          let
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    57
            val tvs = map DatatypeAux.dest_DtTFree dts;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    58
            val sorts = map (rpair []) tvs;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    59
            val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    60
            val (gr', ps) = foldl_map (fn (gr, (cname, cargs)) =>
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    61
              apsnd (pair cname) (foldl_map
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    62
                (invoke_tycodegen thy dname false) (gr, cargs))) (gr, cs');
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    63
            val (gr'', rest) = mk_dtdef gr' "and " xs
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    64
          in
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    65
            (gr'',
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    66
             Pretty.block (Pretty.str prfx ::
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    67
               (if null tvs then [] else
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    68
                  [mk_tuple (map Pretty.str tvs), Pretty.str " "]) @
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    69
               [Pretty.str (mk_type_id sg tname ^ " ="), Pretty.brk 1] @
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    70
               List.concat (separate [Pretty.brk 1, Pretty.str "| "]
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    71
                 (map (fn (cname, ps') => [Pretty.block
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    72
                   (Pretty.str (mk_const_id sg cname) ::
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    73
                    (if null ps' then [] else
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    74
                     List.concat ([Pretty.str " of", Pretty.brk 1] ::
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    75
                       separate [Pretty.str " *", Pretty.brk 1]
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
    76
                         (map single ps'))))]) ps))) :: rest)
13754
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    77
          end;
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    78
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    79
    fun mk_term_of_def prfx [] = []
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    80
      | mk_term_of_def prfx ((_, (tname, dts, cs)) :: xs) =
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    81
          let
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    82
            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
    83
            val sorts = map (rpair []) tvs;
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    84
            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
    85
            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
    86
            val T = Type (tname, dts');
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    87
            val rest = mk_term_of_def "and " xs;
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    88
            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
    89
              let val args = map (fn i =>
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    90
                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
    91
              in ("  | ", Pretty.blk (4,
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    92
                [Pretty.str prfx, mk_term_of sg false T, Pretty.brk 1,
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    93
                 if null Ts then Pretty.str (mk_const_id sg cname)
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    94
                 else parens (Pretty.block [Pretty.str (mk_const_id sg cname),
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    95
                    Pretty.brk 1, mk_tuple args]),
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
    96
                 Pretty.str " =", Pretty.brk 1] @
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    97
                 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
    98
                   ([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
    99
                     mk_type false (Ts ---> T), Pretty.str ")"] ::
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
   100
                    map (fn (x, U) => [Pretty.block [mk_term_of sg false U,
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
   101
                      Pretty.brk 1, x]]) (args ~~ Ts)))))
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
   102
              end) (prfx, cs')
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   103
          in eqs @ rest end;
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   105
    fun mk_gen_of_def prfx [] = []
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   106
      | mk_gen_of_def prfx ((i, (tname, dts, cs)) :: xs) =
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   107
          let
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   108
            val tvs = map DatatypeAux.dest_DtTFree dts;
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   109
            val sorts = map (rpair []) tvs;
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   110
            val (cs1, cs2) =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   111
              List.partition (exists DatatypeAux.is_rec_type o snd) cs;
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   112
            val SOME (cname, _) = find_nonempty descr [i] i;
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   113
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   114
            fun mk_delay p = Pretty.block
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   115
              [Pretty.str "fn () =>", Pretty.brk 1, p];
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   116
15397
5260ac75e07c Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents: 14981
diff changeset
   117
            fun mk_constr s b (cname, dts) =
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   118
              let
15397
5260ac75e07c Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents: 14981
diff changeset
   119
                val gs = map (fn dt => mk_app false (mk_gen sg false rtnames s
5260ac75e07c Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents: 14981
diff changeset
   120
                    (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
   121
                  [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
   122
                     else "j")]) dts;
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   123
                val id = mk_const_id sg cname
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   124
              in case gs of
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   125
                  _ :: _ :: _ => Pretty.block
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   126
                    [Pretty.str id, Pretty.brk 1, mk_tuple gs]
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   127
                | _ => mk_app false (Pretty.str id) (map parens gs)
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   128
              end;
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   129
15397
5260ac75e07c Fixed bug in mk_gen_of_def that could cause non-termination of the generator
berghofe
parents: 14981
diff changeset
   130
            fun mk_choice [c] = mk_constr "(i-1)" false c
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   131
              | mk_choice cs = Pretty.block [Pretty.str "one_of",
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   132
                  Pretty.brk 1, Pretty.blk (1, Pretty.str "[" ::
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   133
                  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
   134
                    (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
   135
                  [Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"];
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   136
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   137
            val gs = map (Pretty.str o suffix "G" o strip_tname) tvs;
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   138
            val gen_name = "gen_" ^ mk_type_id sg tname
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   139
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   140
          in
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   141
            Pretty.blk (4, separate (Pretty.brk 1) 
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   142
                (Pretty.str (prfx ^ gen_name ^
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   143
                   (if null cs1 then "" else "'")) :: gs @
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   144
                 (if null cs1 then [] else [Pretty.str "i"]) @
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   145
                 [Pretty.str "j"]) @
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   146
              [Pretty.str " =", Pretty.brk 1] @
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   147
              (if not (null cs1) andalso not (null cs2)
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   148
               then [Pretty.str "frequency", Pretty.brk 1,
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   149
                 Pretty.blk (1, [Pretty.str "[",
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   150
                   mk_tuple [Pretty.str "i", mk_delay (mk_choice cs1)],
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   151
                   Pretty.str ",", Pretty.fbrk,
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   152
                   mk_tuple [Pretty.str "1", mk_delay (mk_choice cs2)],
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   153
                   Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"]
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   154
               else if null cs2 then
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   155
                 [Pretty.block [Pretty.str "(case", Pretty.brk 1,
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   156
                   Pretty.str "i", Pretty.brk 1, Pretty.str "of",
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   157
                   Pretty.brk 1, Pretty.str "0 =>", Pretty.brk 1,
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   158
                   mk_constr "0" true (cname, valOf (assoc (cs, cname))),
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   159
                   Pretty.brk 1, Pretty.str "| _ =>", Pretty.brk 1,
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   160
                   mk_choice cs1, Pretty.str ")"]]
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   161
               else [mk_choice cs2])) ::
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   162
            (if null cs1 then []
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   163
             else [Pretty.blk (4, separate (Pretty.brk 1) 
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   164
                 (Pretty.str ("and " ^ gen_name) :: gs @ [Pretty.str "i"]) @
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   165
               [Pretty.str " =", Pretty.brk 1] @
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   166
               separate (Pretty.brk 1) (Pretty.str (gen_name ^ "'") :: gs @
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   167
                 [Pretty.str "i", Pretty.str "i"]))]) @
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   168
            mk_gen_of_def "and " xs
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   169
          end
13754
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
   170
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   171
  in
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   172
    ((Graph.add_edge_acyclic (dname, dep) gr
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   173
        handle Graph.CYCLES _ => gr) handle Graph.UNDEF _ =>
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   174
         let
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   175
           val gr1 = Graph.add_edge (dname, dep)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   176
             (Graph.new_node (dname, (NONE, "")) gr);
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   177
           val (gr2, dtdef) = mk_dtdef gr1 "datatype " descr';
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   178
         in
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   179
           Graph.map_node dname (K (NONE,
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   180
             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
   181
               [Pretty.str ";"])) ^ "\n\n" ^
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
   182
             (if "term_of" mem !mode then
021cf00435a9 Code generator for datatypes now also generates suitable term_of functions (when
berghofe
parents: 12890
diff changeset
   183
                Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk
14104
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   184
                  (mk_term_of_def "fun " descr') @ [Pretty.str ";"])) ^ "\n\n"
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   185
              else "") ^
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   186
             (if "test" mem !mode then
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   187
                Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk
29f726e36e5c Added generator for test case generators.
berghofe
parents: 13754
diff changeset
   188
                  (mk_gen_of_def "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
   189
              else ""))) gr2
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   190
         end)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   191
  end;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   192
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   193
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   194
(**** case expressions ****)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   195
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   196
fun pretty_case thy gr dep brack constrs (c as Const (_, T)) ts =
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   197
  let val i = length constrs
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   198
  in if length ts <= i then
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   199
       invoke_codegen thy dep brack (gr, eta_expand c ts (i+1))
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   200
    else
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   201
      let
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   202
        val ts1 = Library.take (i, ts);
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   203
        val t :: ts2 = Library.drop (i, ts);
15574
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   204
        val names = foldr add_term_names
b1d1b5bfc464 Removed practically all references to Library.foldr.
skalberg
parents: 15570
diff changeset
   205
          (map (fst o fst o dest_Var) (foldr add_term_vars [] ts1)) ts1;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   206
        val (Ts, dT) = split_last (Library.take (i+1, fst (strip_type T)));
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   207
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   208
        fun pcase gr [] [] [] = ([], gr)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   209
          | pcase gr ((cname, cargs)::cs) (t::ts) (U::Us) =
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   210
              let
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   211
                val j = length cargs;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   212
                val xs = variantlist (replicate j "x", names);
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   213
                val Us' = Library.take (j, fst (strip_type U));
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   214
                val frees = map Free (xs ~~ Us');
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   215
                val (gr0, cp) = invoke_codegen thy dep false
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   216
                  (gr, list_comb (Const (cname, Us' ---> dT), frees));
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   217
                val t' = Envir.beta_norm (list_comb (t, frees));
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   218
                val (gr1, p) = invoke_codegen thy dep false (gr0, t');
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   219
                val (ps, gr2) = pcase gr1 cs ts Us;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   220
              in
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   221
                ([Pretty.block [cp, Pretty.str " =>", Pretty.brk 1, p]] :: ps, gr2)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   222
              end;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   223
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   224
        val (ps1, gr1) = pcase gr constrs ts1 Ts;
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   225
        val ps = List.concat (separate [Pretty.brk 1, Pretty.str "| "] ps1);
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   226
        val (gr2, p) = invoke_codegen thy dep false (gr1, t);
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   227
        val (gr3, ps2) = foldl_map (invoke_codegen thy dep true) (gr2, ts2)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   228
      in (gr3, (if not (null ts2) andalso brack then parens else I)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   229
        (Pretty.block (separate (Pretty.brk 1)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   230
          (Pretty.block ([Pretty.str "(case ", p, Pretty.str " of",
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   231
             Pretty.brk 1] @ ps @ [Pretty.str ")"]) :: ps2))))
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   232
      end
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   233
  end;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   234
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   235
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   236
(**** constructors ****)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   237
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   238
fun pretty_constr thy gr dep brack args (c as Const (s, _)) ts =
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   239
  let val i = length args
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   240
  in if i > 1 andalso length ts < i then
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   241
      invoke_codegen thy dep brack (gr, eta_expand c ts i)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   242
     else
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   243
       let
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   244
         val id = mk_const_id (sign_of thy) s;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   245
         val (gr', ps) = foldl_map (invoke_codegen thy dep (i = 1)) (gr, ts);
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   246
       in (case args of
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   247
          _ :: _ :: _ => (gr', (if brack then parens else I)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   248
            (Pretty.block [Pretty.str id, Pretty.brk 1, mk_tuple ps]))
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   249
        | _ => (gr', mk_app brack (Pretty.str id) ps))
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   250
       end
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   251
  end;
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   252
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   253
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   254
(**** code generators for terms and types ****)
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   255
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   256
fun datatype_codegen thy gr dep brack t = (case strip_comb t of
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   257
   (c as Const (s, T), ts) =>
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   258
       (case find_first (fn (_, {index, descr, case_name, ...}) =>
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   259
         s = case_name orelse
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   260
           isSome (assoc (#3 (valOf (assoc (descr, index))), s)))
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   261
             (Symtab.dest (DatatypePackage.get_datatypes thy)) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   262
          NONE => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   263
        | SOME (tname, {index, descr, ...}) =>
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   264
           if isSome (get_assoc_code thy s T) then NONE else
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   265
           let val SOME (_, _, constrs) = assoc (descr, index)
12822
073116d65bb9 datatype_codegen now checks type of constructor.
berghofe
parents: 12446
diff changeset
   266
           in (case (assoc (constrs, s), strip_type T) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   267
               (NONE, _) => SOME (pretty_case thy gr dep brack
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   268
                 (#3 (valOf (assoc (descr, index)))) c ts)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   269
             | (SOME args, (_, Type _)) => SOME (pretty_constr thy
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   270
                 (fst (invoke_tycodegen thy dep false (gr, snd (strip_type T))))
12822
073116d65bb9 datatype_codegen now checks type of constructor.
berghofe
parents: 12446
diff changeset
   271
                 dep brack args c ts)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   272
             | _ => NONE)
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   273
           end)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   274
 |  _ => NONE);
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   275
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   276
fun datatype_tycodegen thy gr dep brack (Type (s, Ts)) =
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   277
      (case Symtab.lookup (DatatypePackage.get_datatypes thy, s) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   278
         NONE => NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   279
       | SOME {descr, ...} =>
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   280
           if isSome (get_assoc_type thy s) then NONE else
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   281
           let val (gr', ps) = foldl_map
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   282
             (invoke_tycodegen thy dep false) (gr, Ts)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   283
           in SOME (add_dt_defs thy dep gr' descr,
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   284
             Pretty.block ((if null Ts then [] else
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   285
               [mk_tuple ps, Pretty.str " "]) @
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   286
               [Pretty.str (mk_type_id (sign_of thy) s)]))
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   287
           end)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15397
diff changeset
   288
  | datatype_tycodegen _ _ _ _ _ = NONE;
12445
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   289
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   290
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   291
val setup =
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   292
  [add_codegen "datatype" datatype_codegen,
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   293
   add_tycodegen "datatype" datatype_tycodegen];
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   294
f17eb90bfd16 Code generator for datatypes.
berghofe
parents:
diff changeset
   295
end;