src/HOL/Tools/datatype_codegen.ML
author haftmann
Thu Aug 17 09:24:47 2006 +0200 (2006-08-17)
changeset 20389 8b6ecb22ef35
parent 20382 39964c8dcd54
child 20426 9ffea7a8b31c
permissions -rw-r--r--
cleanup
     1 (*  Title:      HOL/datatype_codegen.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer & Florian Haftmann, TU Muenchen
     4 
     5 Code generator for inductive datatypes.
     6 *)
     7 
     8 signature DATATYPE_CODEGEN =
     9 sig
    10   val get_datatype_spec_thms: theory -> string
    11     -> (((string * sort) list * (string * typ list) list) * tactic) option
    12   val datatype_tac: theory -> string -> tactic
    13   val dest_case_expr: theory -> term
    14     -> ((string * typ) list * ((term * typ) * (term * term) list)) option
    15   val add_datatype_case_const: string -> theory -> theory
    16   val add_datatype_case_defs: string -> theory -> theory
    17   val datatypes_dependency: theory -> string list list
    18   val add_hook_bootstrap: DatatypeHooks.hook -> theory -> theory
    19   val get_datatype_mut_specs: theory -> string list
    20     -> ((string * sort) list * (string * (string * typ list) list) list)
    21   val get_datatype_arities: theory -> string list -> sort
    22     -> (string * (((string * sort list) * sort) * term list)) list option
    23   val prove_arities: (thm list -> tactic) -> string list -> sort
    24     -> (theory -> ((string * sort list) * sort) list -> (string * term list) list
    25     -> ((bstring * attribute list) * term) list) -> theory -> theory
    26   val setup: theory -> theory
    27 end;
    28 
    29 structure DatatypeCodegen : DATATYPE_CODEGEN =
    30 struct
    31 
    32 open Codegen;
    33 
    34 fun mk_tuple [p] = p
    35   | mk_tuple ps = Pretty.block (Pretty.str "(" ::
    36       List.concat (separate [Pretty.str ",", Pretty.brk 1] (map single ps)) @
    37         [Pretty.str ")"]);
    38 
    39 (**** datatype definition ****)
    40 
    41 (* find shortest path to constructor with no recursive arguments *)
    42 
    43 fun find_nonempty (descr: DatatypeAux.descr) is i =
    44   let
    45     val (_, _, constrs) = valOf (AList.lookup (op =) descr i);
    46     fun arg_nonempty (_, DatatypeAux.DtRec i) = if i mem is then NONE
    47           else Option.map (curry op + 1 o snd) (find_nonempty descr (i::is) i)
    48       | arg_nonempty _ = SOME 0;
    49     fun max xs = Library.foldl
    50       (fn (NONE, _) => NONE
    51         | (SOME i, SOME j) => SOME (Int.max (i, j))
    52         | (_, NONE) => NONE) (SOME 0, xs);
    53     val xs = sort (int_ord o pairself snd)
    54       (List.mapPartial (fn (s, dts) => Option.map (pair s)
    55         (max (map (arg_nonempty o DatatypeAux.strip_dtyp) dts))) constrs)
    56   in case xs of [] => NONE | x :: _ => SOME x end;
    57 
    58 fun add_dt_defs thy defs dep module gr (descr: DatatypeAux.descr) =
    59   let
    60     val sg = sign_of thy;
    61     val tab = DatatypePackage.get_datatypes thy;
    62 
    63     val descr' = List.filter (can (map DatatypeAux.dest_DtTFree o #2 o snd)) descr;
    64     val rtnames = map (#1 o snd) (List.filter (fn (_, (_, _, cs)) =>
    65       exists (exists DatatypeAux.is_rec_type o snd) cs) descr');
    66 
    67     val (_, (tname, _, _)) :: _ = descr';
    68     val node_id = tname ^ " (type)";
    69     val module' = if_library (thyname_of_type tname thy) module;
    70 
    71     fun mk_dtdef gr prfx [] = (gr, [])
    72       | mk_dtdef gr prfx ((_, (tname, dts, cs))::xs) =
    73           let
    74             val tvs = map DatatypeAux.dest_DtTFree dts;
    75             val sorts = map (rpair []) tvs;
    76             val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs;
    77             val (gr', (_, type_id)) = mk_type_id module' tname gr;
    78             val (gr'', ps) =
    79               foldl_map (fn (gr, (cname, cargs)) =>
    80                 foldl_map (invoke_tycodegen thy defs node_id module' false)
    81                   (gr, cargs) |>>>
    82                 mk_const_id module' cname) (gr', cs');
    83             val (gr''', rest) = mk_dtdef gr'' "and " xs
    84           in
    85             (gr''',
    86              Pretty.block (Pretty.str prfx ::
    87                (if null tvs then [] else
    88                   [mk_tuple (map Pretty.str tvs), Pretty.str " "]) @
    89                [Pretty.str (type_id ^ " ="), Pretty.brk 1] @
    90                List.concat (separate [Pretty.brk 1, Pretty.str "| "]
    91                  (map (fn (ps', (_, cname)) => [Pretty.block
    92                    (Pretty.str cname ::
    93                     (if null ps' then [] else
    94                      List.concat ([Pretty.str " of", Pretty.brk 1] ::
    95                        separate [Pretty.str " *", Pretty.brk 1]
    96                          (map single ps'))))]) ps))) :: rest)
    97           end;
    98 
    99     fun mk_term_of_def gr prfx [] = []
   100       | mk_term_of_def gr prfx ((_, (tname, dts, cs)) :: xs) =
   101           let
   102             val tvs = map DatatypeAux.dest_DtTFree dts;
   103             val sorts = map (rpair []) tvs;
   104             val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs;
   105             val dts' = map (DatatypeAux.typ_of_dtyp descr sorts) dts;
   106             val T = Type (tname, dts');
   107             val rest = mk_term_of_def gr "and " xs;
   108             val (_, eqs) = foldl_map (fn (prfx, (cname, Ts)) =>
   109               let val args = map (fn i =>
   110                 Pretty.str ("x" ^ string_of_int i)) (1 upto length Ts)
   111               in ("  | ", Pretty.blk (4,
   112                 [Pretty.str prfx, mk_term_of gr module' false T, Pretty.brk 1,
   113                  if null Ts then Pretty.str (snd (get_const_id cname gr))
   114                  else parens (Pretty.block
   115                    [Pretty.str (snd (get_const_id cname gr)),
   116                     Pretty.brk 1, mk_tuple args]),
   117                  Pretty.str " =", Pretty.brk 1] @
   118                  List.concat (separate [Pretty.str " $", Pretty.brk 1]
   119                    ([Pretty.str ("Const (\"" ^ cname ^ "\","), Pretty.brk 1,
   120                      mk_type false (Ts ---> T), Pretty.str ")"] ::
   121                     map (fn (x, U) => [Pretty.block [mk_term_of gr module' false U,
   122                       Pretty.brk 1, x]]) (args ~~ Ts)))))
   123               end) (prfx, cs')
   124           in eqs @ rest end;
   125 
   126     fun mk_gen_of_def gr prfx [] = []
   127       | mk_gen_of_def gr prfx ((i, (tname, dts, cs)) :: xs) =
   128           let
   129             val tvs = map DatatypeAux.dest_DtTFree dts;
   130             val sorts = map (rpair []) tvs;
   131             val (cs1, cs2) =
   132               List.partition (exists DatatypeAux.is_rec_type o snd) cs;
   133             val SOME (cname, _) = find_nonempty descr [i] i;
   134 
   135             fun mk_delay p = Pretty.block
   136               [Pretty.str "fn () =>", Pretty.brk 1, p];
   137 
   138             fun mk_constr s b (cname, dts) =
   139               let
   140                 val gs = map (fn dt => mk_app false (mk_gen gr module' false rtnames s
   141                     (DatatypeAux.typ_of_dtyp descr sorts dt))
   142                   [Pretty.str (if b andalso DatatypeAux.is_rec_type dt then "0"
   143                      else "j")]) dts;
   144                 val (_, id) = get_const_id cname gr
   145               in case gs of
   146                   _ :: _ :: _ => Pretty.block
   147                     [Pretty.str id, Pretty.brk 1, mk_tuple gs]
   148                 | _ => mk_app false (Pretty.str id) (map parens gs)
   149               end;
   150 
   151             fun mk_choice [c] = mk_constr "(i-1)" false c
   152               | mk_choice cs = Pretty.block [Pretty.str "one_of",
   153                   Pretty.brk 1, Pretty.blk (1, Pretty.str "[" ::
   154                   List.concat (separate [Pretty.str ",", Pretty.fbrk]
   155                     (map (single o mk_delay o mk_constr "(i-1)" false) cs)) @
   156                   [Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"];
   157 
   158             val gs = map (Pretty.str o suffix "G" o strip_tname) tvs;
   159             val gen_name = "gen_" ^ snd (get_type_id tname gr)
   160 
   161           in
   162             Pretty.blk (4, separate (Pretty.brk 1) 
   163                 (Pretty.str (prfx ^ gen_name ^
   164                    (if null cs1 then "" else "'")) :: gs @
   165                  (if null cs1 then [] else [Pretty.str "i"]) @
   166                  [Pretty.str "j"]) @
   167               [Pretty.str " =", Pretty.brk 1] @
   168               (if not (null cs1) andalso not (null cs2)
   169                then [Pretty.str "frequency", Pretty.brk 1,
   170                  Pretty.blk (1, [Pretty.str "[",
   171                    mk_tuple [Pretty.str "i", mk_delay (mk_choice cs1)],
   172                    Pretty.str ",", Pretty.fbrk,
   173                    mk_tuple [Pretty.str "1", mk_delay (mk_choice cs2)],
   174                    Pretty.str "]"]), Pretty.brk 1, Pretty.str "()"]
   175                else if null cs2 then
   176                  [Pretty.block [Pretty.str "(case", Pretty.brk 1,
   177                    Pretty.str "i", Pretty.brk 1, Pretty.str "of",
   178                    Pretty.brk 1, Pretty.str "0 =>", Pretty.brk 1,
   179                    mk_constr "0" true (cname, valOf (AList.lookup (op =) cs cname)),
   180                    Pretty.brk 1, Pretty.str "| _ =>", Pretty.brk 1,
   181                    mk_choice cs1, Pretty.str ")"]]
   182                else [mk_choice cs2])) ::
   183             (if null cs1 then []
   184              else [Pretty.blk (4, separate (Pretty.brk 1) 
   185                  (Pretty.str ("and " ^ gen_name) :: gs @ [Pretty.str "i"]) @
   186                [Pretty.str " =", Pretty.brk 1] @
   187                separate (Pretty.brk 1) (Pretty.str (gen_name ^ "'") :: gs @
   188                  [Pretty.str "i", Pretty.str "i"]))]) @
   189             mk_gen_of_def gr "and " xs
   190           end
   191 
   192   in
   193     (add_edge_acyclic (node_id, dep) gr
   194         handle Graph.CYCLES _ => gr) handle Graph.UNDEF _ =>
   195          let
   196            val gr1 = add_edge (node_id, dep)
   197              (new_node (node_id, (NONE, "", "")) gr);
   198            val (gr2, dtdef) = mk_dtdef gr1 "datatype " descr';
   199          in
   200            map_node node_id (K (NONE, module',
   201              Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk dtdef @
   202                [Pretty.str ";"])) ^ "\n\n" ^
   203              (if "term_of" mem !mode then
   204                 Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk
   205                   (mk_term_of_def gr2 "fun " descr') @ [Pretty.str ";"])) ^ "\n\n"
   206               else "") ^
   207              (if "test" mem !mode then
   208                 Pretty.string_of (Pretty.blk (0, separate Pretty.fbrk
   209                   (mk_gen_of_def gr2 "fun " descr') @ [Pretty.str ";"])) ^ "\n\n"
   210               else ""))) gr2
   211          end
   212   end;
   213 
   214 
   215 (**** case expressions ****)
   216 
   217 fun pretty_case thy defs gr dep module brack constrs (c as Const (_, T)) ts =
   218   let val i = length constrs
   219   in if length ts <= i then
   220        invoke_codegen thy defs dep module brack (gr, eta_expand c ts (i+1))
   221     else
   222       let
   223         val ts1 = Library.take (i, ts);
   224         val t :: ts2 = Library.drop (i, ts);
   225         val names = foldr add_term_names
   226           (map (fst o fst o dest_Var) (foldr add_term_vars [] ts1)) ts1;
   227         val (Ts, dT) = split_last (Library.take (i+1, fst (strip_type T)));
   228 
   229         fun pcase gr [] [] [] = ([], gr)
   230           | pcase gr ((cname, cargs)::cs) (t::ts) (U::Us) =
   231               let
   232                 val j = length cargs;
   233                 val xs = Name.variant_list names (replicate j "x");
   234                 val Us' = Library.take (j, fst (strip_type U));
   235                 val frees = map Free (xs ~~ Us');
   236                 val (gr0, cp) = invoke_codegen thy defs dep module false
   237                   (gr, list_comb (Const (cname, Us' ---> dT), frees));
   238                 val t' = Envir.beta_norm (list_comb (t, frees));
   239                 val (gr1, p) = invoke_codegen thy defs dep module false (gr0, t');
   240                 val (ps, gr2) = pcase gr1 cs ts Us;
   241               in
   242                 ([Pretty.block [cp, Pretty.str " =>", Pretty.brk 1, p]] :: ps, gr2)
   243               end;
   244 
   245         val (ps1, gr1) = pcase gr constrs ts1 Ts;
   246         val ps = List.concat (separate [Pretty.brk 1, Pretty.str "| "] ps1);
   247         val (gr2, p) = invoke_codegen thy defs dep module false (gr1, t);
   248         val (gr3, ps2) = foldl_map (invoke_codegen thy defs dep module true) (gr2, ts2)
   249       in (gr3, (if not (null ts2) andalso brack then parens else I)
   250         (Pretty.block (separate (Pretty.brk 1)
   251           (Pretty.block ([Pretty.str "(case ", p, Pretty.str " of",
   252              Pretty.brk 1] @ ps @ [Pretty.str ")"]) :: ps2))))
   253       end
   254   end;
   255 
   256 
   257 (**** constructors ****)
   258 
   259 fun pretty_constr thy defs gr dep module brack args (c as Const (s, T)) ts =
   260   let val i = length args
   261   in if i > 1 andalso length ts < i then
   262       invoke_codegen thy defs dep module brack (gr, eta_expand c ts i)
   263      else
   264        let
   265          val id = mk_qual_id module (get_const_id s gr);
   266          val (gr', ps) = foldl_map
   267            (invoke_codegen thy defs dep module (i = 1)) (gr, ts);
   268        in (case args of
   269           _ :: _ :: _ => (gr', (if brack then parens else I)
   270             (Pretty.block [Pretty.str id, Pretty.brk 1, mk_tuple ps]))
   271         | _ => (gr', mk_app brack (Pretty.str id) ps))
   272        end
   273   end;
   274 
   275 
   276 (**** code generators for terms and types ****)
   277 
   278 fun datatype_codegen thy defs gr dep module brack t = (case strip_comb t of
   279    (c as Const (s, T), ts) =>
   280        (case Library.find_first (fn (_, {index, descr, case_name, ...}) =>
   281          s = case_name orelse
   282            AList.defined (op =) ((#3 o the o AList.lookup (op =) descr) index) s)
   283              (Symtab.dest (DatatypePackage.get_datatypes thy)) of
   284           NONE => NONE
   285         | SOME (tname, {index, descr, ...}) =>
   286            if is_some (get_assoc_code thy s T) then NONE else
   287            let val SOME (_, _, constrs) = AList.lookup (op =) descr index
   288            in (case (AList.lookup (op =) constrs s, strip_type T) of
   289                (NONE, _) => SOME (pretty_case thy defs gr dep module brack
   290                  ((#3 o the o AList.lookup (op =) descr) index) c ts)
   291              | (SOME args, (_, Type _)) => SOME (pretty_constr thy defs
   292                  (fst (invoke_tycodegen thy defs dep module false
   293                     (gr, snd (strip_type T))))
   294                  dep module brack args c ts)
   295              | _ => NONE)
   296            end)
   297  |  _ => NONE);
   298 
   299 fun datatype_tycodegen thy defs gr dep module brack (Type (s, Ts)) =
   300       (case Symtab.lookup (DatatypePackage.get_datatypes thy) s of
   301          NONE => NONE
   302        | SOME {descr, ...} =>
   303            if isSome (get_assoc_type thy s) then NONE else
   304            let
   305              val (gr', ps) = foldl_map
   306                (invoke_tycodegen thy defs dep module false) (gr, Ts);
   307              val gr'' = add_dt_defs thy defs dep module gr' descr
   308            in SOME (gr'',
   309              Pretty.block ((if null Ts then [] else
   310                [mk_tuple ps, Pretty.str " "]) @
   311                [Pretty.str (mk_qual_id module (get_type_id s gr''))]))
   312            end)
   313   | datatype_tycodegen _ _ _ _ _ _ _ = NONE;
   314 
   315 
   316 (** code 2nd generation **)
   317 
   318 fun datatypes_dependency thy =
   319   let
   320     val dtnames = DatatypePackage.get_datatypes thy;
   321     fun add_node (dtname, _) =
   322       let
   323         fun add_tycos (Type (tyco, tys)) = insert (op =) tyco #> fold add_tycos tys
   324           | add_tycos _ = I;
   325         val deps = (filter (Symtab.defined dtnames) o maps (fn ty =>
   326           add_tycos ty [])
   327             o maps snd o snd o the o DatatypePackage.get_datatype_spec thy) dtname
   328       in
   329         Graph.default_node (dtname, ())
   330         #> fold (fn dtname' =>
   331              Graph.default_node (dtname', ())
   332              #> Graph.add_edge (dtname', dtname)
   333            ) deps
   334       end
   335   in
   336     Graph.empty
   337     |> Symtab.fold add_node dtnames
   338     |> Graph.strong_conn
   339   end;
   340 
   341 fun add_hook_bootstrap hook thy =
   342   thy
   343   |> fold hook (datatypes_dependency thy)
   344   |> DatatypeHooks.add hook;
   345 
   346 fun get_datatype_mut_specs thy (tycos as tyco :: _) =
   347   let
   348     val tycos' = (map (#1 o snd) o #descr o DatatypePackage.the_datatype thy) tyco;
   349     val _ = if gen_subset (op =) (tycos, tycos') then () else
   350       error ("datatype constructors are not mutually recursive: " ^ (commas o map quote) tycos);
   351     val (vs::_, css) = split_list (map (the o DatatypePackage.get_datatype_spec thy) tycos);
   352   in (vs, tycos ~~ css) end;
   353 
   354 fun get_datatype_arities thy tycos sort =
   355   let
   356     val algebra = Sign.classes_of thy;
   357     val (vs_proto, css_proto) = get_datatype_mut_specs thy tycos;
   358     val vs = map (fn (v, vsort) => (v, Sorts.inter_sort algebra (vsort, sort))) vs_proto;
   359     fun inst_type tyco (c, tys) =
   360       let
   361         val tys' = (map o map_atyps)
   362           (fn TFree (v, _) => TFree (v, the (AList.lookup (op =) vs v))) tys
   363       in (c, tys') end;
   364     val css = map (fn (tyco, cs) => (tyco, (map (inst_type tyco) cs))) css_proto;
   365     fun mk_arity tyco =
   366       ((tyco, map snd vs), sort);
   367     fun typ_of_sort ty =
   368       let
   369         val arities = map (fn (tyco, _) => ((tyco, map snd vs), sort)) css;
   370       in ClassPackage.assume_arities_of_sort thy arities (ty, sort) end;
   371     fun mk_cons tyco (c, tys) =
   372       let
   373         val ts = Name.names Name.context "a" tys;
   374         val ty = tys ---> Type (tyco, map TFree vs);
   375       in list_comb (Const (c, ty), map Free ts) end;
   376   in if forall (fn (_, cs) => forall (fn (_, tys) => forall typ_of_sort tys) cs) css
   377     then SOME (
   378       map (fn (tyco, cs) => (tyco, (mk_arity tyco, map (mk_cons tyco) cs))) css
   379     ) else NONE
   380   end;
   381 
   382 fun prove_arities tac tycos sort f thy =
   383   case get_datatype_arities thy tycos sort
   384    of NONE => thy
   385     | SOME insts => let
   386         fun proven ((tyco, asorts), sort) =
   387           Sorts.of_sort (Sign.classes_of thy)
   388             (Type (tyco, map TFree (Name.names Name.context "'a" asorts)), sort);
   389         val (arities, css) = (split_list o map_filter
   390           (fn (tyco, (arity, cs)) => if proven arity
   391             then NONE else SOME (arity, (tyco, cs)))) insts;
   392       in
   393         thy
   394         |> K ((not o null) arities) ? ClassPackage.prove_instance_arity tac
   395              arities ("", []) (f thy arities css)
   396       end;
   397 
   398 fun dtyp_of_case_const thy c =
   399   get_first (fn (dtco, { case_name, ... }) => if case_name = c then SOME dtco else NONE)
   400     ((Symtab.dest o DatatypePackage.get_datatypes) thy);
   401 
   402 fun dest_case_app cs ts tys =
   403   let
   404     val abs = Name.names Name.context "a" (Library.drop (length ts, tys));
   405     val (ts', t) = split_last (ts @ map Free abs);
   406     val (tys', sty) = split_last tys;
   407     fun freenames_of t = fold_aterms
   408       (fn Free (v, _) => insert (op =) v | _ => I) t [];
   409     fun dest_case ((c, tys_decl), ty) t =
   410       let
   411         val (vs, t') = Term.strip_abs_eta (length tys_decl) t;
   412         val c' = list_comb (Const (c, map snd vs ---> sty), map Free vs);
   413       in (c', t') end;
   414   in (abs, ((t, sty), map2 dest_case (cs ~~ tys') ts')) end;
   415 
   416 fun dest_case_expr thy t =
   417   case strip_comb t
   418    of (Const (c, ty), ts) =>
   419         (case dtyp_of_case_const thy c
   420          of SOME dtco =>
   421               let val (vs, cs) = (the o DatatypePackage.get_datatype_spec thy) dtco;
   422               in SOME (dest_case_app cs ts (Library.take (length cs + 1, (fst o strip_type) ty))) end
   423           | _ => NONE)
   424     | _ => NONE;
   425 
   426 fun datatype_tac thy dtco =
   427   let
   428     val ctxt = Context.init_proof thy;
   429     val inject = (#inject o DatatypePackage.the_datatype thy) dtco;
   430     val simpset = Simplifier.context ctxt
   431       (MetaSimplifier.empty_ss addsimprocs [distinct_simproc]);
   432   in
   433     (TRY o ALLGOALS o match_tac) [HOL.eq_reflection]
   434     THEN (
   435       (ALLGOALS o match_tac) (eqTrueI :: inject)
   436       ORELSE (ALLGOALS o simp_tac) simpset
   437     )
   438     THEN (ALLGOALS o match_tac) [HOL.refl, Drule.reflexive_thm]
   439   end;
   440 
   441 fun get_datatype_spec_thms thy dtco =
   442   case DatatypePackage.get_datatype_spec thy dtco
   443    of SOME vs_cos =>
   444         SOME (vs_cos, datatype_tac thy dtco)
   445     | NONE => NONE;
   446 
   447 fun add_datatype_case_const dtco thy =
   448   let
   449     val {case_name, index, descr, ...} = DatatypePackage.the_datatype thy dtco;
   450   in
   451     CodegenPackage.add_appconst_i (case_name, CodegenPackage.appgen_case dest_case_expr) thy
   452   end;
   453 
   454 fun add_datatype_case_defs dtco thy =
   455   let
   456     val {case_rewrites, ...} = DatatypePackage.the_datatype thy dtco
   457   in
   458     fold CodegenTheorems.add_fun case_rewrites thy
   459   end;
   460 
   461 val setup = 
   462   add_codegen "datatype" datatype_codegen #>
   463   add_tycodegen "datatype" datatype_tycodegen #>
   464   CodegenTheorems.add_datatype_extr
   465     get_datatype_spec_thms #>
   466   DatatypeHooks.add (fold add_datatype_case_const) #>
   467   DatatypeHooks.add (fold add_datatype_case_defs)
   468 
   469 end;