src/HOL/Tools/function_package/size.ML
author haftmann
Tue Oct 23 11:48:12 2007 +0200 (2007-10-23)
changeset 25155 65612c9f7381
parent 25017 e82ab4962f80
child 25502 9200b36280c0
permissions -rw-r--r--
dropped code redundancy
     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 make_size head_len descr' sorts recTs thy =
    50   let
    51     val size_names = replicate head_len size_name @
    52       map (Sign.intern_const thy) (DatatypeProp.indexify_names
    53         (map (fn T => name_of_typ T ^ size_suffix) (Library.drop (head_len, recTs))));
    54     val size_consts = map2 (fn s => fn T => Const (s, T --> HOLogic.natT))
    55       size_names recTs;
    56 
    57     fun make_tnames Ts =
    58       let
    59         fun type_name (TFree (name, _)) = implode (tl (explode name))
    60           | type_name (Type (name, _)) = 
    61               let val name' = Sign.base_name name
    62               in if Syntax.is_identifier name' then name' else "x" end;
    63       in DatatypeProp.indexify_names (map type_name Ts) end;
    64 
    65     fun make_size_eqn size_const T (cname, cargs) =
    66       let
    67         val recs = filter is_rec_type cargs;
    68         val Ts = map (typ_of_dtyp descr' sorts) cargs;
    69         val recTs = map (typ_of_dtyp descr' sorts) recs;
    70         val tnames = make_tnames Ts;
    71         val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
    72         val ts = map2 (fn (r, s) => fn T => nth size_consts (dest_DtRec r) $
    73           Free (s, T)) (recs ~~ rec_tnames) recTs;
    74         val t = if null ts then HOLogic.zero else
    75           Library.foldl plus (hd ts, tl ts @ [HOLogic.Suc_zero]);
    76       in
    77         HOLogic.mk_Trueprop (HOLogic.mk_eq (size_const $
    78           list_comb (Const (cname, Ts ---> T), map2 (curry Free) tnames Ts), t))
    79       end
    80 
    81   in
    82     maps (fn (((_, (_, _, constrs)), size_const), T) =>
    83       map (make_size_eqn size_const T) constrs) (descr' ~~ size_consts ~~ recTs)
    84   end;
    85 
    86 fun prove_size_thms (info : datatype_info) head_len thy =
    87   let
    88     val descr' = #descr info;
    89     val sorts = #sorts info;
    90     val reccomb_names = #rec_names info;
    91     val primrec_thms = #rec_rewrites info;
    92     val recTs = get_rec_types descr' sorts;
    93 
    94     val size_names = replicate head_len size_name @
    95       map (Sign.full_name thy) (DatatypeProp.indexify_names
    96         (map (fn T => name_of_typ T ^ size_suffix) (Library.drop (head_len, recTs))));
    97     val def_names = map (fn s => s ^ "_def") (DatatypeProp.indexify_names
    98       (map (fn T => name_of_typ T ^ size_suffix) recTs));
    99 
   100     fun make_sizefun (_, cargs) =
   101       let
   102         val Ts = map (typ_of_dtyp descr' sorts) cargs;
   103         val k = length (filter is_rec_type cargs);
   104         val ts = map Bound (k - 1 downto 0);
   105         val t = if null ts then HOLogic.zero else
   106           Library.foldl plus (hd ts, tl ts @ [HOLogic.Suc_zero]);
   107 
   108       in
   109         foldr (fn (T, t') => Abs ("x", T, t')) t (Ts @ replicate k HOLogic.natT)
   110       end;
   111 
   112     val fs = maps (fn (_, (_, _, constrs)) => map make_sizefun constrs) descr';
   113     val fTs = map fastype_of fs;
   114 
   115     val (size_def_thms, thy') =
   116       thy
   117       |> Sign.add_consts_i (map (fn (s, T) =>
   118            (Sign.base_name s, T --> HOLogic.natT, NoSyn))
   119            (Library.drop (head_len, size_names ~~ recTs)))
   120       |> fold (fn (_, (name, _, _)) => instance_size_class name) descr'
   121       |> PureThy.add_defs_i true (map (Thm.no_attributes o (fn (((s, T), def_name), rec_name) =>
   122            (def_name, Logic.mk_equals (Const (s, T --> HOLogic.natT),
   123             list_comb (Const (rec_name, fTs @ [T] ---> HOLogic.natT), fs)))))
   124             (size_names ~~ recTs ~~ def_names ~~ reccomb_names));
   125 
   126     val rewrites = size_def_thms @ map mk_meta_eq primrec_thms;
   127 
   128     val size_thms = map (fn t => Goal.prove_global thy' [] [] t
   129       (fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1]))
   130         (make_size head_len descr' sorts recTs thy')
   131 
   132   in
   133     thy'
   134     |> PureThy.add_thmss [((size_name_base, size_thms), [])]
   135     |>> flat
   136   end;
   137 
   138 fun axiomatize_size_thms (info : datatype_info) head_len thy =
   139   let
   140     val descr' = #descr info;
   141     val sorts = #sorts info;
   142     val recTs = get_rec_types descr' sorts;
   143 
   144     val used = map fst (fold Term.add_tfreesT recTs []);
   145 
   146     val size_names = DatatypeProp.indexify_names
   147       (map (fn T => name_of_typ T ^ size_suffix) (Library.drop (head_len, recTs)));
   148 
   149     val thy' = thy |> fold (fn (s, T) =>
   150         snd o Theory.specify_const [] (Sign.base_name s, T --> HOLogic.natT, NoSyn) [])
   151       (size_names ~~ Library.drop (head_len, recTs))
   152     val size_axs = make_size head_len descr' sorts recTs thy';
   153   in
   154     thy'
   155     |> add_axioms size_name_base size_axs []
   156     ||> fold (fn (_, (name, _, _)) => instance_size_class name) descr'
   157     |>> flat
   158   end;
   159 
   160 fun add_size_thms (name :: _) thy =
   161   let
   162     val info = DatatypePackage.the_datatype thy name;
   163     val descr' = #descr info;
   164     val head_len = #head_len info;
   165     val typnames = map (#1 o snd) (curry Library.take head_len descr');
   166     val prefix = space_implode "_" (map NameSpace.base typnames);
   167     val no_size = exists (fn (_, (_, _, constrs)) => exists (fn (_, cargs) => exists (fn dt =>
   168       is_rec_type dt andalso not (null (fst (strip_dtyp dt)))) cargs) constrs)
   169         (#descr info)
   170   in if no_size then thy
   171     else
   172       thy
   173       |> Sign.add_path prefix
   174       |> (if ! quick_and_dirty
   175         then axiomatize_size_thms info head_len
   176         else prove_size_thms info head_len)
   177       ||> Sign.parent_path
   178       |-> (fn thms => PureThy.add_thmss [(("", thms),
   179             [Simplifier.simp_add, Thm.declaration_attribute
   180               (fn thm => Context.mapping (Code.add_default_func thm) I)])])
   181       |-> (fn thms => SizeData.map (fold (fn typname => Symtab.update_new
   182             (typname, flat thms)) typnames))
   183   end;
   184 
   185 fun size_thms thy = the o Symtab.lookup (SizeData.get thy);
   186 
   187 val setup = DatatypePackage.interpretation add_size_thms;
   188 
   189 end;