src/HOL/Tools/function_package/size.ML
author wenzelm
Sat Oct 13 17:16:40 2007 +0200 (2007-10-13)
changeset 25017 e82ab4962f80
parent 24962 60d33fb8ea5d
child 25155 65612c9f7381
permissions -rw-r--r--
Theory.specify_const: added deps argument;
     1 (*  Title:      HOL/Tools/function_package/size.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
     4 
     5 Size functions for datatypes.
     6 *)
     7 
     8 signature SIZE =
     9 sig
    10   val size_thms: theory -> string -> thm list
    11   val setup: theory -> theory
    12 end;
    13 
    14 structure Size: SIZE =
    15 struct
    16 
    17 open DatatypeAux;
    18 
    19 structure SizeData = TheoryDataFun
    20 (
    21   type T = thm list Symtab.table;
    22   val empty = Symtab.empty;
    23   val copy = I
    24   val extend = I
    25   fun merge _ = Symtab.merge (K true);
    26 );
    27 
    28 fun add_axioms label ts atts thy =
    29   thy
    30   |> PureThy.add_axiomss_i [((label, ts), atts)];
    31 
    32 val Const (size_name, _) = HOLogic.size_const dummyT;
    33 val size_name_base = NameSpace.base size_name;
    34 val size_suffix = "_" ^ size_name_base;
    35 
    36 fun instance_size_class tyco thy =
    37   let
    38     val n = Sign.arity_number thy tyco;
    39   in
    40     thy
    41     |> Class.prove_instance (Class.intro_classes_tac [])
    42         [(tyco, replicate n HOLogic.typeS, [HOLogic.class_size])] []
    43     |> snd
    44   end
    45 
    46 fun plus (t1, t2) = Const ("HOL.plus_class.plus",
    47   HOLogic.natT --> HOLogic.natT --> HOLogic.natT) $ t1 $ t2;
    48 
    49 fun indexify_names names =
    50   let
    51     fun index (x :: xs) tab =
    52       (case AList.lookup (op =) tab x of
    53         NONE => if member (op =) xs x then (x ^ "1") :: index xs ((x, 2) :: tab) else x :: index xs tab
    54       | SOME i => (x ^ string_of_int i) :: index xs ((x, i + 1) :: tab))
    55     | index [] _ = [];
    56   in index names [] end;
    57 
    58 fun make_size head_len descr' sorts recTs thy =
    59   let
    60     val size_names = replicate head_len size_name @
    61       map (Sign.intern_const thy) (indexify_names
    62         (map (fn T => name_of_typ T ^ size_suffix) (Library.drop (head_len, recTs))));
    63     val size_consts = map2 (fn s => fn T => Const (s, T --> HOLogic.natT))
    64       size_names recTs;
    65 
    66     fun make_tnames Ts =
    67       let
    68         fun type_name (TFree (name, _)) = implode (tl (explode name))
    69           | type_name (Type (name, _)) = 
    70               let val name' = Sign.base_name name
    71               in if Syntax.is_identifier name' then name' else "x" end;
    72       in indexify_names (map type_name Ts) end;
    73 
    74     fun make_size_eqn size_const T (cname, cargs) =
    75       let
    76         val recs = filter is_rec_type cargs;
    77         val Ts = map (typ_of_dtyp descr' sorts) cargs;
    78         val recTs = map (typ_of_dtyp descr' sorts) recs;
    79         val tnames = make_tnames Ts;
    80         val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
    81         val ts = map2 (fn (r, s) => fn T => nth size_consts (dest_DtRec r) $
    82           Free (s, T)) (recs ~~ rec_tnames) recTs;
    83         val t = if null ts then HOLogic.zero else
    84           Library.foldl plus (hd ts, tl ts @ [HOLogic.Suc_zero]);
    85       in
    86         HOLogic.mk_Trueprop (HOLogic.mk_eq (size_const $
    87           list_comb (Const (cname, Ts ---> T), map2 (curry Free) tnames Ts), t))
    88       end
    89 
    90   in
    91     maps (fn (((_, (_, _, constrs)), size_const), T) =>
    92       map (make_size_eqn size_const T) constrs) (descr' ~~ size_consts ~~ recTs)
    93   end;
    94 
    95 fun prove_size_thms (info : datatype_info) head_len thy =
    96   let
    97     val descr' = #descr info;
    98     val sorts = #sorts info;
    99     val reccomb_names = #rec_names info;
   100     val primrec_thms = #rec_rewrites info;
   101     val recTs = get_rec_types descr' sorts;
   102 
   103     val size_names = replicate head_len size_name @
   104       map (Sign.full_name thy) (DatatypeProp.indexify_names
   105         (map (fn T => name_of_typ T ^ size_suffix) (Library.drop (head_len, recTs))));
   106     val def_names = map (fn s => s ^ "_def") (DatatypeProp.indexify_names
   107       (map (fn T => name_of_typ T ^ size_suffix) recTs));
   108 
   109     fun make_sizefun (_, cargs) =
   110       let
   111         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   112         val k = length (filter is_rec_type cargs);
   113         val ts = map Bound (k - 1 downto 0);
   114         val t = if null ts then HOLogic.zero else
   115           Library.foldl plus (hd ts, tl ts @ [HOLogic.Suc_zero]);
   116 
   117       in
   118         foldr (fn (T, t') => Abs ("x", T, t')) t (Ts @ replicate k HOLogic.natT)
   119       end;
   120 
   121     val fs = maps (fn (_, (_, _, constrs)) => map make_sizefun constrs) descr';
   122     val fTs = map fastype_of fs;
   123 
   124     val (size_def_thms, thy') =
   125       thy
   126       |> Sign.add_consts_i (map (fn (s, T) =>
   127            (Sign.base_name s, T --> HOLogic.natT, NoSyn))
   128            (Library.drop (head_len, size_names ~~ recTs)))
   129       |> fold (fn (_, (name, _, _)) => instance_size_class name) descr'
   130       |> PureThy.add_defs_i true (map (Thm.no_attributes o (fn (((s, T), def_name), rec_name) =>
   131            (def_name, Logic.mk_equals (Const (s, T --> HOLogic.natT),
   132             list_comb (Const (rec_name, fTs @ [T] ---> HOLogic.natT), fs)))))
   133             (size_names ~~ recTs ~~ def_names ~~ reccomb_names));
   134 
   135     val rewrites = size_def_thms @ map mk_meta_eq primrec_thms;
   136 
   137     val size_thms = map (fn t => Goal.prove_global thy' [] [] t
   138       (fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1]))
   139         (make_size head_len descr' sorts recTs thy')
   140 
   141   in
   142     thy'
   143     |> PureThy.add_thmss [((size_name_base, size_thms), [])]
   144     |>> flat
   145   end;
   146 
   147 fun axiomatize_size_thms (info : datatype_info) head_len thy =
   148   let
   149     val descr' = #descr info;
   150     val sorts = #sorts info;
   151     val recTs = get_rec_types descr' sorts;
   152 
   153     val used = map fst (fold Term.add_tfreesT recTs []);
   154 
   155     val size_names = DatatypeProp.indexify_names
   156       (map (fn T => name_of_typ T ^ size_suffix) (Library.drop (head_len, recTs)));
   157 
   158     val thy' = thy |> fold (fn (s, T) =>
   159         snd o Theory.specify_const [] (Sign.base_name s, T --> HOLogic.natT, NoSyn) [])
   160       (size_names ~~ Library.drop (head_len, recTs))
   161     val size_axs = make_size head_len descr' sorts recTs thy';
   162   in
   163     thy'
   164     |> add_axioms size_name_base size_axs []
   165     ||> fold (fn (_, (name, _, _)) => instance_size_class name) descr'
   166     |>> flat
   167   end;
   168 
   169 fun add_size_thms (name :: _) thy =
   170   let
   171     val info = DatatypePackage.the_datatype thy name;
   172     val descr' = #descr info;
   173     val head_len = #head_len info;
   174     val typnames = map (#1 o snd) (curry Library.take head_len descr');
   175     val prefix = space_implode "_" (map NameSpace.base typnames);
   176     val no_size = exists (fn (_, (_, _, constrs)) => exists (fn (_, cargs) => exists (fn dt =>
   177       is_rec_type dt andalso not (null (fst (strip_dtyp dt)))) cargs) constrs)
   178         (#descr info)
   179   in if no_size then thy
   180     else
   181       thy
   182       |> Sign.add_path prefix
   183       |> (if ! quick_and_dirty
   184         then axiomatize_size_thms info head_len
   185         else prove_size_thms info head_len)
   186       ||> Sign.parent_path
   187       |-> (fn thms => PureThy.add_thmss [(("", thms),
   188             [Simplifier.simp_add, Thm.declaration_attribute
   189               (fn thm => Context.mapping (Code.add_default_func thm) I)])])
   190       |-> (fn thms => SizeData.map (fold (fn typname => Symtab.update_new
   191             (typname, flat thms)) typnames))
   192   end;
   193 
   194 fun size_thms thy = the o Symtab.lookup (SizeData.get thy);
   195 
   196 val setup = DatatypePackage.interpretation add_size_thms;
   197 
   198 end;