src/HOL/Tools/datatype_codegen.ML
author berghofe
Mon Dec 10 15:30:18 2001 +0100 (2001-12-10 ago)
changeset 12445 f17eb90bfd16
child 12446 86887b40aeb1
permissions -rw-r--r--
Code generator for datatypes.
berghofe@12445
     1
(*  Title:      Pure/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
berghofe@12445
    25
(**** datatype definition ****)
berghofe@12445
    26
berghofe@12445
    27
fun add_dt_defs thy dep gr descr =
berghofe@12445
    28
  let
berghofe@12445
    29
    val sg = sign_of thy;
berghofe@12445
    30
    val tab = DatatypePackage.get_datatypes thy;
berghofe@12445
    31
berghofe@12445
    32
    val descr' = filter (can (map DatatypeAux.dest_DtTFree o #2 o snd)) descr;
berghofe@12445
    33
berghofe@12445
    34
    val (_, (_, _, (cname, _) :: _)) :: _ = descr';
berghofe@12445
    35
    val dname = mk_const_id sg cname;
berghofe@12445
    36
berghofe@12445
    37
    fun mk_dtdef gr prfx [] = (gr, [])
berghofe@12445
    38
      | mk_dtdef gr prfx ((_, (tname, dts, cs))::xs) =
berghofe@12445
    39
          let
berghofe@12445
    40
            val tvs = map DatatypeAux.dest_DtTFree dts;
berghofe@12445
    41
            val sorts = map (rpair []) tvs;
berghofe@12445
    42
            val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs;
berghofe@12445
    43
            val (gr', ps) = foldl_map (fn (gr, (cname, cargs)) =>
berghofe@12445
    44
              apsnd (pair cname) (foldl_map
berghofe@12445
    45
                (invoke_tycodegen thy dname false) (gr, cargs))) (gr, cs');
berghofe@12445
    46
            val (gr'', rest) = mk_dtdef gr' "and " xs
berghofe@12445
    47
          in
berghofe@12445
    48
            (gr'',
berghofe@12445
    49
             Pretty.block (Pretty.str prfx ::
berghofe@12445
    50
               (if null tvs then [] else
berghofe@12445
    51
                  [mk_tuple (map Pretty.str tvs), Pretty.str " "]) @
berghofe@12445
    52
               [Pretty.str (mk_type_id sg tname ^ " ="), Pretty.brk 1] @
berghofe@12445
    53
               flat (separate [Pretty.brk 1, Pretty.str "| "]
berghofe@12445
    54
                 (map (fn (cname, ps') => [Pretty.block
berghofe@12445
    55
                   (Pretty.str (mk_const_id sg cname) ::
berghofe@12445
    56
                    (if null ps' then [] else
berghofe@12445
    57
                     flat ([Pretty.str " of", Pretty.brk 1] ::
berghofe@12445
    58
                       separate [Pretty.str " *", Pretty.brk 1]
berghofe@12445
    59
                         (map single ps'))))]) ps))) :: rest)
berghofe@12445
    60
          end
berghofe@12445
    61
  in
berghofe@12445
    62
    ((Graph.add_edge_acyclic (dname, dep) gr
berghofe@12445
    63
        handle Graph.CYCLES _ => gr) handle Graph.UNDEF _ =>
berghofe@12445
    64
         let
berghofe@12445
    65
           val gr1 = Graph.add_edge (dname, dep)
berghofe@12445
    66
             (Graph.new_node (dname, (None, "")) gr);
berghofe@12445
    67
           val (gr2, dtdef) = mk_dtdef gr1 "datatype " descr';
berghofe@12445
    68
         in
berghofe@12445
    69
           Graph.map_node dname (K (None,
berghofe@12445
    70
             Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk dtdef @
berghofe@12445
    71
               [Pretty.str ";"])) ^ "\n\n")) gr2
berghofe@12445
    72
         end)
berghofe@12445
    73
  end;
berghofe@12445
    74
berghofe@12445
    75
berghofe@12445
    76
(**** case expressions ****)
berghofe@12445
    77
berghofe@12445
    78
fun pretty_case thy gr dep brack constrs (c as Const (_, T)) ts =
berghofe@12445
    79
  let val i = length constrs
berghofe@12445
    80
  in if length ts <= i then
berghofe@12445
    81
       invoke_codegen thy dep brack (gr, eta_expand c ts (i+1))
berghofe@12445
    82
    else
berghofe@12445
    83
      let
berghofe@12445
    84
        val ts1 = take (i, ts);
berghofe@12445
    85
        val t :: ts2 = drop (i, ts);
berghofe@12445
    86
        val names = foldr add_term_names (ts1,
berghofe@12445
    87
          map (fst o fst o dest_Var) (foldr add_term_vars (ts1, [])));
berghofe@12445
    88
        val (Ts, dT) = split_last (take (i+1, fst (strip_type T)));
berghofe@12445
    89
berghofe@12445
    90
        fun pcase gr [] [] [] = ([], gr)
berghofe@12445
    91
          | pcase gr ((cname, cargs)::cs) (t::ts) (U::Us) =
berghofe@12445
    92
              let
berghofe@12445
    93
                val j = length cargs;
berghofe@12445
    94
                val xs = variantlist (replicate j "x", names);
berghofe@12445
    95
                val Us' = take (j, fst (strip_type U));
berghofe@12445
    96
                val frees = map Free (xs ~~ Us');
berghofe@12445
    97
                val (gr0, cp) = invoke_codegen thy dep false
berghofe@12445
    98
                  (gr, list_comb (Const (cname, Us' ---> dT), frees));
berghofe@12445
    99
                val t' = Envir.beta_norm (list_comb (t, frees));
berghofe@12445
   100
                val (gr1, p) = invoke_codegen thy dep false (gr0, t');
berghofe@12445
   101
                val (ps, gr2) = pcase gr1 cs ts Us;
berghofe@12445
   102
              in
berghofe@12445
   103
                ([Pretty.block [cp, Pretty.str " =>", Pretty.brk 1, p]] :: ps, gr2)
berghofe@12445
   104
              end;
berghofe@12445
   105
berghofe@12445
   106
        val (ps1, gr1) = pcase gr constrs ts1 Ts;
berghofe@12445
   107
        val ps = flat (separate [Pretty.brk 1, Pretty.str "| "] ps1);
berghofe@12445
   108
        val (gr2, p) = invoke_codegen thy dep false (gr1, t);
berghofe@12445
   109
        val (gr3, ps2) = foldl_map (invoke_codegen thy dep true) (gr2, ts2)
berghofe@12445
   110
      in (gr3, (if not (null ts2) andalso brack then parens else I)
berghofe@12445
   111
        (Pretty.block (separate (Pretty.brk 1)
berghofe@12445
   112
          (Pretty.block ([Pretty.str "(case ", p, Pretty.str " of",
berghofe@12445
   113
             Pretty.brk 1] @ ps @ [Pretty.str ")"]) :: ps2))))
berghofe@12445
   114
      end
berghofe@12445
   115
  end;
berghofe@12445
   116
berghofe@12445
   117
berghofe@12445
   118
(**** constructors ****)
berghofe@12445
   119
berghofe@12445
   120
fun pretty_constr thy gr dep brack args (c as Const (s, _)) ts =
berghofe@12445
   121
  let val i = length args
berghofe@12445
   122
  in if i > 1 andalso length ts < i then
berghofe@12445
   123
      invoke_codegen thy dep brack (gr, eta_expand c ts i)
berghofe@12445
   124
     else
berghofe@12445
   125
       let
berghofe@12445
   126
         val id = mk_const_id (sign_of thy) s;
berghofe@12445
   127
         val (gr', ps) = foldl_map (invoke_codegen thy dep (i = 1)) (gr, ts);
berghofe@12445
   128
       in (case args of
berghofe@12445
   129
          _ :: _ :: _ => (gr', (if brack then parens else I)
berghofe@12445
   130
            (Pretty.block [Pretty.str id, Pretty.brk 1, mk_tuple ps]))
berghofe@12445
   131
        | _ => (gr', mk_app brack (Pretty.str id) ps))
berghofe@12445
   132
       end
berghofe@12445
   133
  end;
berghofe@12445
   134
berghofe@12445
   135
berghofe@12445
   136
(**** code generators for terms and types ****)
berghofe@12445
   137
berghofe@12445
   138
fun datatype_codegen thy gr dep brack t = (case strip_comb t of
berghofe@12445
   139
   (c as Const (s, T), ts) =>
berghofe@12445
   140
       (case find_first (fn (_, {index, descr, case_name, ...}) =>
berghofe@12445
   141
         s = case_name orelse
berghofe@12445
   142
           is_some (assoc (#3 (the (assoc (descr, index))), s)))
berghofe@12445
   143
             (Symtab.dest (DatatypePackage.get_datatypes thy)) of
berghofe@12445
   144
          None => None
berghofe@12445
   145
        | Some (tname, {index, descr, ...}) =>
berghofe@12445
   146
           if is_some (get_assoc_code thy s T) then None else
berghofe@12445
   147
           let val Some (_, _, constrs) = assoc (descr, index)
berghofe@12445
   148
           in (case assoc (constrs, s) of
berghofe@12445
   149
               None => Some (pretty_case thy gr dep brack
berghofe@12445
   150
                 (#3 (the (assoc (descr, index)))) c ts)
berghofe@12445
   151
             | Some args => Some (pretty_constr thy
berghofe@12445
   152
                 (fst (invoke_tycodegen thy dep false (gr, snd (strip_type T))))
berghofe@12445
   153
                 dep brack args c ts))
berghofe@12445
   154
           end)
berghofe@12445
   155
 |  _ => None);
berghofe@12445
   156
berghofe@12445
   157
fun datatype_tycodegen thy gr dep brack (Type (s, Ts)) =
berghofe@12445
   158
      (case Symtab.lookup (DatatypePackage.get_datatypes thy, s) of
berghofe@12445
   159
         None => None
berghofe@12445
   160
       | Some {descr, ...} =>
berghofe@12445
   161
           if is_some (get_assoc_type thy s) then None else
berghofe@12445
   162
           let val (gr', ps) = foldl_map
berghofe@12445
   163
             (invoke_tycodegen thy dep false) (gr, Ts)
berghofe@12445
   164
           in Some (add_dt_defs thy dep gr' descr,
berghofe@12445
   165
             Pretty.block ((if null Ts then [] else
berghofe@12445
   166
               [mk_tuple ps, Pretty.str " "]) @
berghofe@12445
   167
               [Pretty.str (mk_type_id (sign_of thy) s)]))
berghofe@12445
   168
           end)
berghofe@12445
   169
  | datatype_tycodegen _ _ _ _ _ = None;
berghofe@12445
   170
berghofe@12445
   171
berghofe@12445
   172
val setup =
berghofe@12445
   173
  [add_codegen "datatype" datatype_codegen,
berghofe@12445
   174
   add_tycodegen "datatype" datatype_tycodegen];
berghofe@12445
   175
berghofe@12445
   176
end;