src/HOL/Tools/datatype_codegen.ML
author berghofe
Mon Dec 16 11:18:35 2002 +0100 (2002-12-16 ago)
changeset 13754 021cf00435a9
parent 12890 75b254b1c8ba
child 14104 29f726e36e5c
permissions -rw-r--r--
Code generator for datatypes now also generates suitable term_of functions (when
term_of mode is switched on).
berghofe@12446
     1
(*  Title:      HOL/datatype_codegen.ML
berghofe@12445
     2
    ID:         $Id$
berghofe@12445
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@12445
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
berghofe@12445
     5
berghofe@12445
     6
Code generator for inductive datatypes.
berghofe@12445
     7
*)
berghofe@12445
     8
berghofe@12445
     9
signature DATATYPE_CODEGEN =
berghofe@12445
    10
sig
berghofe@12445
    11
  val setup: (theory -> theory) list
berghofe@12445
    12
end;
berghofe@12445
    13
berghofe@12445
    14
structure DatatypeCodegen : DATATYPE_CODEGEN =
berghofe@12445
    15
struct
berghofe@12445
    16
berghofe@12445
    17
open Codegen;
berghofe@12445
    18
berghofe@12445
    19
fun mk_tuple [p] = p
berghofe@12445
    20
  | mk_tuple ps = Pretty.block (Pretty.str "(" ::
berghofe@12445
    21
      flat (separate [Pretty.str ",", Pretty.brk 1] (map single ps)) @
berghofe@12445
    22
        [Pretty.str ")"]);
berghofe@12445
    23
berghofe@12445
    24
(**** datatype definition ****)
berghofe@12445
    25
wenzelm@12890
    26
fun add_dt_defs thy dep gr (descr: DatatypeAux.descr) =
berghofe@12445
    27
  let
berghofe@12445
    28
    val sg = sign_of thy;
berghofe@12445
    29
    val tab = DatatypePackage.get_datatypes thy;
berghofe@12445
    30
berghofe@12445
    31
    val descr' = filter (can (map DatatypeAux.dest_DtTFree o #2 o snd)) descr;
berghofe@12445
    32
berghofe@12445
    33
    val (_, (_, _, (cname, _) :: _)) :: _ = descr';
berghofe@12445
    34
    val dname = mk_const_id sg cname;
berghofe@12445
    35
berghofe@12445
    36
    fun mk_dtdef gr prfx [] = (gr, [])
berghofe@12445
    37
      | mk_dtdef gr prfx ((_, (tname, dts, cs))::xs) =
berghofe@12445
    38
          let
berghofe@12445
    39
            val tvs = map DatatypeAux.dest_DtTFree dts;
berghofe@12445
    40
            val sorts = map (rpair []) tvs;
berghofe@12445
    41
            val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs;
berghofe@12445
    42
            val (gr', ps) = foldl_map (fn (gr, (cname, cargs)) =>
berghofe@12445
    43
              apsnd (pair cname) (foldl_map
berghofe@12445
    44
                (invoke_tycodegen thy dname false) (gr, cargs))) (gr, cs');
berghofe@12445
    45
            val (gr'', rest) = mk_dtdef gr' "and " xs
berghofe@12445
    46
          in
berghofe@12445
    47
            (gr'',
berghofe@12445
    48
             Pretty.block (Pretty.str prfx ::
berghofe@12445
    49
               (if null tvs then [] else
berghofe@12445
    50
                  [mk_tuple (map Pretty.str tvs), Pretty.str " "]) @
berghofe@12445
    51
               [Pretty.str (mk_type_id sg tname ^ " ="), Pretty.brk 1] @
berghofe@12445
    52
               flat (separate [Pretty.brk 1, Pretty.str "| "]
berghofe@12445
    53
                 (map (fn (cname, ps') => [Pretty.block
berghofe@12445
    54
                   (Pretty.str (mk_const_id sg cname) ::
berghofe@12445
    55
                    (if null ps' then [] else
berghofe@12445
    56
                     flat ([Pretty.str " of", Pretty.brk 1] ::
berghofe@12445
    57
                       separate [Pretty.str " *", Pretty.brk 1]
berghofe@12445
    58
                         (map single ps'))))]) ps))) :: rest)
berghofe@13754
    59
          end;
berghofe@13754
    60
berghofe@13754
    61
    fun mk_term_of_def prfx [] = []
berghofe@13754
    62
      | mk_term_of_def prfx ((_, (tname, dts, cs)) :: xs) =
berghofe@13754
    63
          let
berghofe@13754
    64
            val tvs = map DatatypeAux.dest_DtTFree dts;
berghofe@13754
    65
            val sorts = map (rpair []) tvs;
berghofe@13754
    66
            val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs;
berghofe@13754
    67
            val dts' = map (DatatypeAux.typ_of_dtyp descr sorts) dts;
berghofe@13754
    68
            val T = Type (tname, dts');
berghofe@13754
    69
            val rest = mk_term_of_def "and " xs;
berghofe@13754
    70
            val (_, eqs) = foldl_map (fn (prfx, (cname, Ts)) =>
berghofe@13754
    71
              let val args = map (fn i =>
berghofe@13754
    72
                Pretty.str ("x" ^ string_of_int i)) (1 upto length Ts)
berghofe@13754
    73
              in ("  | ", Pretty.blk (4,
berghofe@13754
    74
                [Pretty.str prfx, mk_term_of sg false T, Pretty.brk 1,
berghofe@13754
    75
                 if null Ts then Pretty.str (mk_const_id sg cname)
berghofe@13754
    76
                 else parens (Pretty.block [Pretty.str (mk_const_id sg cname),
berghofe@13754
    77
                    Pretty.brk 1, mk_tuple args]),
berghofe@13754
    78
                 Pretty.str " =", Pretty.brk 1] @
berghofe@13754
    79
                 flat (separate [Pretty.str " $", Pretty.brk 1]
berghofe@13754
    80
                   ([Pretty.str ("Const (\"" ^ cname ^ "\","), Pretty.brk 1,
berghofe@13754
    81
                     mk_type false (Ts ---> T), Pretty.str ")"] ::
berghofe@13754
    82
                    map (fn (x, U) => [Pretty.block [mk_term_of sg false U,
berghofe@13754
    83
                      Pretty.brk 1, x]]) (args ~~ Ts)))))
berghofe@13754
    84
              end) (prfx, cs')
berghofe@13754
    85
          in eqs @ rest end
berghofe@13754
    86
berghofe@12445
    87
  in
berghofe@12445
    88
    ((Graph.add_edge_acyclic (dname, dep) gr
berghofe@12445
    89
        handle Graph.CYCLES _ => gr) handle Graph.UNDEF _ =>
berghofe@12445
    90
         let
berghofe@12445
    91
           val gr1 = Graph.add_edge (dname, dep)
berghofe@12445
    92
             (Graph.new_node (dname, (None, "")) gr);
berghofe@12445
    93
           val (gr2, dtdef) = mk_dtdef gr1 "datatype " descr';
berghofe@12445
    94
         in
berghofe@12445
    95
           Graph.map_node dname (K (None,
berghofe@12445
    96
             Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk dtdef @
berghofe@13754
    97
               [Pretty.str ";"])) ^ "\n\n" ^
berghofe@13754
    98
             (if "term_of" mem !mode then
berghofe@13754
    99
                Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk
berghofe@13754
   100
                  (mk_term_of_def "fun " descr')  @ [Pretty.str ";"])) ^ "\n\n"
berghofe@13754
   101
              else ""))) gr2
berghofe@12445
   102
         end)
berghofe@12445
   103
  end;
berghofe@12445
   104
berghofe@12445
   105
berghofe@12445
   106
(**** case expressions ****)
berghofe@12445
   107
berghofe@12445
   108
fun pretty_case thy gr dep brack constrs (c as Const (_, T)) ts =
berghofe@12445
   109
  let val i = length constrs
berghofe@12445
   110
  in if length ts <= i then
berghofe@12445
   111
       invoke_codegen thy dep brack (gr, eta_expand c ts (i+1))
berghofe@12445
   112
    else
berghofe@12445
   113
      let
berghofe@12445
   114
        val ts1 = take (i, ts);
berghofe@12445
   115
        val t :: ts2 = drop (i, ts);
berghofe@12445
   116
        val names = foldr add_term_names (ts1,
berghofe@12445
   117
          map (fst o fst o dest_Var) (foldr add_term_vars (ts1, [])));
berghofe@12445
   118
        val (Ts, dT) = split_last (take (i+1, fst (strip_type T)));
berghofe@12445
   119
berghofe@12445
   120
        fun pcase gr [] [] [] = ([], gr)
berghofe@12445
   121
          | pcase gr ((cname, cargs)::cs) (t::ts) (U::Us) =
berghofe@12445
   122
              let
berghofe@12445
   123
                val j = length cargs;
berghofe@12445
   124
                val xs = variantlist (replicate j "x", names);
berghofe@12445
   125
                val Us' = take (j, fst (strip_type U));
berghofe@12445
   126
                val frees = map Free (xs ~~ Us');
berghofe@12445
   127
                val (gr0, cp) = invoke_codegen thy dep false
berghofe@12445
   128
                  (gr, list_comb (Const (cname, Us' ---> dT), frees));
berghofe@12445
   129
                val t' = Envir.beta_norm (list_comb (t, frees));
berghofe@12445
   130
                val (gr1, p) = invoke_codegen thy dep false (gr0, t');
berghofe@12445
   131
                val (ps, gr2) = pcase gr1 cs ts Us;
berghofe@12445
   132
              in
berghofe@12445
   133
                ([Pretty.block [cp, Pretty.str " =>", Pretty.brk 1, p]] :: ps, gr2)
berghofe@12445
   134
              end;
berghofe@12445
   135
berghofe@12445
   136
        val (ps1, gr1) = pcase gr constrs ts1 Ts;
berghofe@12445
   137
        val ps = flat (separate [Pretty.brk 1, Pretty.str "| "] ps1);
berghofe@12445
   138
        val (gr2, p) = invoke_codegen thy dep false (gr1, t);
berghofe@12445
   139
        val (gr3, ps2) = foldl_map (invoke_codegen thy dep true) (gr2, ts2)
berghofe@12445
   140
      in (gr3, (if not (null ts2) andalso brack then parens else I)
berghofe@12445
   141
        (Pretty.block (separate (Pretty.brk 1)
berghofe@12445
   142
          (Pretty.block ([Pretty.str "(case ", p, Pretty.str " of",
berghofe@12445
   143
             Pretty.brk 1] @ ps @ [Pretty.str ")"]) :: ps2))))
berghofe@12445
   144
      end
berghofe@12445
   145
  end;
berghofe@12445
   146
berghofe@12445
   147
berghofe@12445
   148
(**** constructors ****)
berghofe@12445
   149
berghofe@12445
   150
fun pretty_constr thy gr dep brack args (c as Const (s, _)) ts =
berghofe@12445
   151
  let val i = length args
berghofe@12445
   152
  in if i > 1 andalso length ts < i then
berghofe@12445
   153
      invoke_codegen thy dep brack (gr, eta_expand c ts i)
berghofe@12445
   154
     else
berghofe@12445
   155
       let
berghofe@12445
   156
         val id = mk_const_id (sign_of thy) s;
berghofe@12445
   157
         val (gr', ps) = foldl_map (invoke_codegen thy dep (i = 1)) (gr, ts);
berghofe@12445
   158
       in (case args of
berghofe@12445
   159
          _ :: _ :: _ => (gr', (if brack then parens else I)
berghofe@12445
   160
            (Pretty.block [Pretty.str id, Pretty.brk 1, mk_tuple ps]))
berghofe@12445
   161
        | _ => (gr', mk_app brack (Pretty.str id) ps))
berghofe@12445
   162
       end
berghofe@12445
   163
  end;
berghofe@12445
   164
berghofe@12445
   165
berghofe@12445
   166
(**** code generators for terms and types ****)
berghofe@12445
   167
berghofe@12445
   168
fun datatype_codegen thy gr dep brack t = (case strip_comb t of
berghofe@12445
   169
   (c as Const (s, T), ts) =>
berghofe@12445
   170
       (case find_first (fn (_, {index, descr, case_name, ...}) =>
berghofe@12445
   171
         s = case_name orelse
berghofe@12445
   172
           is_some (assoc (#3 (the (assoc (descr, index))), s)))
berghofe@12445
   173
             (Symtab.dest (DatatypePackage.get_datatypes thy)) of
berghofe@12445
   174
          None => None
berghofe@12445
   175
        | Some (tname, {index, descr, ...}) =>
berghofe@12445
   176
           if is_some (get_assoc_code thy s T) then None else
berghofe@12445
   177
           let val Some (_, _, constrs) = assoc (descr, index)
berghofe@12822
   178
           in (case (assoc (constrs, s), strip_type T) of
berghofe@12822
   179
               (None, _) => Some (pretty_case thy gr dep brack
berghofe@12445
   180
                 (#3 (the (assoc (descr, index)))) c ts)
berghofe@12822
   181
             | (Some args, (_, Type _)) => Some (pretty_constr thy
berghofe@12445
   182
                 (fst (invoke_tycodegen thy dep false (gr, snd (strip_type T))))
berghofe@12822
   183
                 dep brack args c ts)
berghofe@12822
   184
             | _ => None)
berghofe@12445
   185
           end)
berghofe@12445
   186
 |  _ => None);
berghofe@12445
   187
berghofe@12445
   188
fun datatype_tycodegen thy gr dep brack (Type (s, Ts)) =
berghofe@12445
   189
      (case Symtab.lookup (DatatypePackage.get_datatypes thy, s) of
berghofe@12445
   190
         None => None
berghofe@12445
   191
       | Some {descr, ...} =>
berghofe@12445
   192
           if is_some (get_assoc_type thy s) then None else
berghofe@12445
   193
           let val (gr', ps) = foldl_map
berghofe@12445
   194
             (invoke_tycodegen thy dep false) (gr, Ts)
berghofe@12445
   195
           in Some (add_dt_defs thy dep gr' descr,
berghofe@12445
   196
             Pretty.block ((if null Ts then [] else
berghofe@12445
   197
               [mk_tuple ps, Pretty.str " "]) @
berghofe@12445
   198
               [Pretty.str (mk_type_id (sign_of thy) s)]))
berghofe@12445
   199
           end)
berghofe@12445
   200
  | datatype_tycodegen _ _ _ _ _ = None;
berghofe@12445
   201
berghofe@12445
   202
berghofe@12445
   203
val setup =
berghofe@12445
   204
  [add_codegen "datatype" datatype_codegen,
berghofe@12445
   205
   add_tycodegen "datatype" datatype_tycodegen];
berghofe@12445
   206
berghofe@12445
   207
end;