src/HOL/Tools/function_package/size.ML
author berghofe
Tue, 18 Dec 2007 12:26:00 +0100
changeset 25689 4853eeb03158
parent 25679 b77f797b528a
child 25769 850abe253ffc
permissions -rw-r--r--
Alternative names are now also used when storing theorems for size functions.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Tools/function_package/size.ML
141df8b68f63 size hook
haftmann
parents:
diff changeset
     2
    ID:         $Id$
141df8b68f63 size hook
haftmann
parents:
diff changeset
     3
    Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
141df8b68f63 size hook
haftmann
parents:
diff changeset
     4
141df8b68f63 size hook
haftmann
parents:
diff changeset
     5
Size functions for datatypes.
141df8b68f63 size hook
haftmann
parents:
diff changeset
     6
*)
141df8b68f63 size hook
haftmann
parents:
diff changeset
     7
141df8b68f63 size hook
haftmann
parents:
diff changeset
     8
signature SIZE =
141df8b68f63 size hook
haftmann
parents:
diff changeset
     9
sig
141df8b68f63 size hook
haftmann
parents:
diff changeset
    10
  val size_thms: theory -> string -> thm list
141df8b68f63 size hook
haftmann
parents:
diff changeset
    11
  val setup: theory -> theory
141df8b68f63 size hook
haftmann
parents:
diff changeset
    12
end;
141df8b68f63 size hook
haftmann
parents:
diff changeset
    13
141df8b68f63 size hook
haftmann
parents:
diff changeset
    14
structure Size: SIZE =
141df8b68f63 size hook
haftmann
parents:
diff changeset
    15
struct
141df8b68f63 size hook
haftmann
parents:
diff changeset
    16
141df8b68f63 size hook
haftmann
parents:
diff changeset
    17
open DatatypeAux;
141df8b68f63 size hook
haftmann
parents:
diff changeset
    18
24714
1618c2ac1b74 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24711
diff changeset
    19
structure SizeData = TheoryDataFun
1618c2ac1b74 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24711
diff changeset
    20
(
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    21
  type T = (string * thm list) Symtab.table;
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    22
  val empty = Symtab.empty;
141df8b68f63 size hook
haftmann
parents:
diff changeset
    23
  val copy = I
141df8b68f63 size hook
haftmann
parents:
diff changeset
    24
  val extend = I
141df8b68f63 size hook
haftmann
parents:
diff changeset
    25
  fun merge _ = Symtab.merge (K true);
24714
1618c2ac1b74 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24711
diff changeset
    26
);
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    27
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    28
val lookup_size = SizeData.get #> Symtab.lookup;
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    29
25569
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25539
diff changeset
    30
fun instance_size_class tyco thy =
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25539
diff changeset
    31
  thy
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25539
diff changeset
    32
  |> TheoryTarget.instantiation
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25539
diff changeset
    33
       ([tyco], replicate (Sign.arity_number thy tyco) HOLogic.typeS, [HOLogic.class_size])
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25539
diff changeset
    34
  |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25539
diff changeset
    35
  |> LocalTheory.exit
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25539
diff changeset
    36
  |> ProofContext.theory_of;
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    37
141df8b68f63 size hook
haftmann
parents:
diff changeset
    38
fun plus (t1, t2) = Const ("HOL.plus_class.plus",
141df8b68f63 size hook
haftmann
parents:
diff changeset
    39
  HOLogic.natT --> HOLogic.natT --> HOLogic.natT) $ t1 $ t2;
141df8b68f63 size hook
haftmann
parents:
diff changeset
    40
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    41
fun size_of_type f g h (T as Type (s, Ts)) =
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    42
      (case f s of
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    43
         SOME t => SOME t
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    44
       | NONE => (case g s of
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    45
           SOME size_name =>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    46
             SOME (list_comb (Const (size_name,
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    47
               map (fn U => U --> HOLogic.natT) Ts @ [T] ---> HOLogic.natT),
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    48
                 map (size_of_type' f g h) Ts))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    49
         | NONE => NONE))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    50
  | size_of_type f g h (TFree (s, _)) = h s
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    51
and size_of_type' f g h T = (case size_of_type f g h T of
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    52
      NONE => Abs ("x", T, HOLogic.zero)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    53
    | SOME t => t);
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    54
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    55
fun is_poly thy (DtType (name, dts)) =
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    56
      (case DatatypePackage.get_datatype thy name of
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    57
         NONE => false
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    58
       | SOME _ => exists (is_poly thy) dts)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    59
  | is_poly _ _ = true;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    60
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    61
fun constrs_of thy name =
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    62
  let
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    63
    val {descr, index, ...} = DatatypePackage.the_datatype thy name
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    64
    val SOME (_, _, constrs) = AList.lookup op = descr index
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    65
  in constrs end;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    66
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    67
val app = curry (list_comb o swap);
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    68
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    69
fun prove_size_thms (info : datatype_info) new_type_names thy =
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    70
  let
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    71
    val {descr, alt_names, sorts, rec_names, rec_rewrites, induction, ...} = info;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    72
    val l = length new_type_names;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    73
    val alt_names' = (case alt_names of
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    74
      NONE => replicate l NONE | SOME names => map SOME names);
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    75
    val descr' = List.take (descr, l);
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    76
    val (rec_names1, rec_names2) = chop l rec_names;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    77
    val recTs = get_rec_types descr sorts;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    78
    val (recTs1, recTs2) = chop l recTs;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    79
    val (_, (_, paramdts, _)) :: _ = descr;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    80
    val paramTs = map (typ_of_dtyp descr sorts) paramdts;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    81
    val ((param_size_fs, param_size_fTs), f_names) = paramTs |>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    82
      map (fn T as TFree (s, _) =>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    83
        let
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    84
          val name = "f" ^ implode (tl (explode s));
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    85
          val U = T --> HOLogic.natT
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    86
        in
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    87
          (((s, Free (name, U)), U), name)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    88
        end) |> split_list |>> split_list;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    89
    val param_size = AList.lookup op = param_size_fs;
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    90
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    91
    val extra_rewrites = descr |> map (#1 o snd) |> distinct op = |>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    92
      List.mapPartial (Option.map snd o lookup_size thy) |> flat;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    93
    val extra_size = Option.map fst o lookup_size thy;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    94
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    95
    val (((size_names, size_fns), def_names), def_names') =
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    96
      recTs1 ~~ alt_names' |>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    97
      map (fn (T as Type (s, _), optname) =>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    98
        let
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    99
          val s' = the_default (Sign.base_name s) optname ^ "_size";
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   100
          val s'' = Sign.full_name thy s'
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   101
        in
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   102
          (s'',
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   103
           (list_comb (Const (s'', param_size_fTs @ [T] ---> HOLogic.natT),
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   104
              map snd param_size_fs),
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   105
            (s' ^ "_def", s' ^ "_overloaded_def")))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   106
        end) |> split_list ||>> split_list ||>> split_list;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   107
    val overloaded_size_fns = map HOLogic.size_const recTs1;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   108
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   109
    (* instantiation for primrec combinator *)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   110
    fun size_of_constr b size_ofp ((_, cargs), (_, cargs')) =
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   111
      let
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   112
        val Ts = map (typ_of_dtyp descr sorts) cargs;
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   113
        val k = length (filter is_rec_type cargs);
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   114
        val (ts, _, _) = fold_rev (fn ((dt, dt'), T) => fn (us, i, j) =>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   115
          if is_rec_type dt then (Bound i :: us, i + 1, j + 1)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   116
          else
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   117
            (if b andalso is_poly thy dt' then
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   118
               case size_of_type (K NONE) extra_size size_ofp T of
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   119
                 NONE => us | SOME sz => sz $ Bound j :: us
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   120
             else us, i, j + 1))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   121
              (cargs ~~ cargs' ~~ Ts) ([], 0, k);
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   122
        val t =
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   123
          if null ts andalso (not b orelse not (exists (is_poly thy) cargs'))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   124
          then HOLogic.zero
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   125
          else foldl1 plus (ts @ [HOLogic.Suc_zero])
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   126
      in
141df8b68f63 size hook
haftmann
parents:
diff changeset
   127
        foldr (fn (T, t') => Abs ("x", T, t')) t (Ts @ replicate k HOLogic.natT)
141df8b68f63 size hook
haftmann
parents:
diff changeset
   128
      end;
141df8b68f63 size hook
haftmann
parents:
diff changeset
   129
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   130
    val fs = maps (fn (_, (name, _, constrs)) =>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   131
      map (size_of_constr true param_size) (constrs ~~ constrs_of thy name)) descr;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   132
    val fs' = maps (fn (n, (name, _, constrs)) =>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   133
      map (size_of_constr (l <= n) (K NONE)) (constrs ~~ constrs_of thy name)) descr;
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   134
    val fTs = map fastype_of fs;
141df8b68f63 size hook
haftmann
parents:
diff changeset
   135
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   136
    val (rec_combs1, rec_combs2) = chop l (map (fn (T, rec_name) =>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   137
      Const (rec_name, fTs @ [T] ---> HOLogic.natT))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   138
        (recTs ~~ rec_names));
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   139
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   140
    val ((size_def_thms, size_def_thms'), thy') =
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   141
      thy
24714
1618c2ac1b74 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24711
diff changeset
   142
      |> Sign.add_consts_i (map (fn (s, T) =>
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   143
           (Sign.base_name s, param_size_fTs @ [T] ---> HOLogic.natT, NoSyn))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   144
           (size_names ~~ recTs1))
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   145
      |> fold (fn (_, (name, _, _)) => instance_size_class name) descr'
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   146
      |> PureThy.add_defs_i false
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   147
        (map (Thm.no_attributes o apsnd (Logic.mk_equals o apsnd (app fs)))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   148
           (def_names ~~ (size_fns ~~ rec_combs1)))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   149
      ||>> PureThy.add_defs_i true
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   150
        (map (Thm.no_attributes o apsnd (Logic.mk_equals o apsnd (app fs')))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   151
           (def_names' ~~ (overloaded_size_fns ~~ rec_combs1)));
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   152
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   153
    val ctxt = ProofContext.init thy';
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   154
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   155
    val simpset1 = HOL_basic_ss addsimps @{thm add_0} :: @{thm add_0_right} ::
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   156
      size_def_thms @ size_def_thms' @ rec_rewrites @ extra_rewrites;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   157
    val xs = map (fn i => "x" ^ string_of_int i) (1 upto length recTs2);
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   158
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   159
    fun mk_unfolded_size_eq tab size_ofp fs (p as (x, T), r) =
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   160
      HOLogic.mk_eq (app fs r $ Free p,
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   161
        the (size_of_type tab extra_size size_ofp T) $ Free p);
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   162
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   163
    fun prove_unfolded_size_eqs size_ofp fs =
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   164
      if null recTs2 then []
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   165
      else map standard (split_conj_thm (SkipProof.prove ctxt [] []
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   166
        (HOLogic.mk_Trueprop (mk_conj (replicate l HOLogic.true_const @
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   167
           map (mk_unfolded_size_eq (AList.lookup op =
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   168
               (new_type_names ~~ map (app fs) rec_combs1)) size_ofp fs)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   169
             (xs ~~ recTs2 ~~ rec_combs2))))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   170
        (fn _ => (indtac induction xs THEN_ALL_NEW asm_simp_tac simpset1) 1)));
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   171
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   172
    val unfolded_size_eqs = prove_unfolded_size_eqs param_size fs @
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   173
      prove_unfolded_size_eqs (K NONE) fs';
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   174
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   175
    (* characteristic equations for size functions *)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   176
    fun gen_mk_size_eq p size_of size_ofp size_const T (cname, cargs) =
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   177
      let
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   178
        val Ts = map (typ_of_dtyp descr sorts) cargs;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   179
        val tnames = Name.variant_list f_names (DatatypeProp.make_tnames Ts);
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   180
        val ts = List.mapPartial (fn (sT as (s, T), dt) =>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   181
          Option.map (fn sz => sz $ Free sT)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   182
            (if p dt then size_of_type size_of extra_size size_ofp T
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   183
             else NONE)) (tnames ~~ Ts ~~ cargs)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   184
      in
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   185
        HOLogic.mk_Trueprop (HOLogic.mk_eq
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   186
          (size_const $ list_comb (Const (cname, Ts ---> T),
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   187
             map2 (curry Free) tnames Ts),
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   188
           if null ts then HOLogic.zero
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   189
           else foldl1 plus (ts @ [HOLogic.Suc_zero])))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   190
      end;
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   191
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   192
    val simpset2 = HOL_basic_ss addsimps
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   193
      size_def_thms @ size_def_thms' @ rec_rewrites @ unfolded_size_eqs;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   194
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   195
    fun prove_size_eqs p size_fns size_ofp =
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   196
      maps (fn (((_, (_, _, constrs)), size_const), T) =>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   197
        map (fn constr => standard (SkipProof.prove ctxt [] []
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   198
          (gen_mk_size_eq p (AList.lookup op = (new_type_names ~~ size_fns))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   199
             size_ofp size_const T constr)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   200
          (fn _ => simp_tac simpset2 1))) constrs)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   201
        (descr' ~~ size_fns ~~ recTs1);
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   202
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   203
    val size_eqns = prove_size_eqs (is_poly thy') size_fns param_size @
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   204
      prove_size_eqs is_rec_type overloaded_size_fns (K NONE);
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   205
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   206
    val ([size_thms], thy'') =  PureThy.add_thmss
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   207
      [(("size", size_eqns),
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   208
        [Simplifier.simp_add, Thm.declaration_attribute
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   209
              (fn thm => Context.mapping (Code.add_default_func thm) I)])] thy'
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   210
141df8b68f63 size hook
haftmann
parents:
diff changeset
   211
  in
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   212
    SizeData.map (fold (Symtab.update_new o apsnd (rpair size_thms))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   213
      (new_type_names ~~ size_names)) thy''
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   214
  end;
141df8b68f63 size hook
haftmann
parents:
diff changeset
   215
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   216
fun add_size_thms (new_type_names as name :: _) thy =
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   217
  let
25689
4853eeb03158 Alternative names are now also used when storing theorems for
berghofe
parents: 25679
diff changeset
   218
    val info as {descr, alt_names, ...} = DatatypePackage.the_datatype thy name;
4853eeb03158 Alternative names are now also used when storing theorems for
berghofe
parents: 25679
diff changeset
   219
    val prefix = NameSpace.map_base (K (space_implode "_"
4853eeb03158 Alternative names are now also used when storing theorems for
berghofe
parents: 25679
diff changeset
   220
      (the_default (map Sign.base_name new_type_names) alt_names))) name;
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   221
    val no_size = exists (fn (_, (_, _, constrs)) => exists (fn (_, cargs) => exists (fn dt =>
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   222
      is_rec_type dt andalso not (null (fst (strip_dtyp dt)))) cargs) constrs) descr
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   223
  in if no_size then thy
141df8b68f63 size hook
haftmann
parents:
diff changeset
   224
    else
141df8b68f63 size hook
haftmann
parents:
diff changeset
   225
      thy
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   226
      |> Sign.root_path
24714
1618c2ac1b74 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24711
diff changeset
   227
      |> Sign.add_path prefix
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   228
      |> prove_size_thms info new_type_names
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   229
      |> Sign.restore_naming thy
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   230
  end;
141df8b68f63 size hook
haftmann
parents:
diff changeset
   231
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   232
val size_thms = snd oo (the oo lookup_size);
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   233
24711
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24710
diff changeset
   234
val setup = DatatypePackage.interpretation add_size_thms;
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   235
141df8b68f63 size hook
haftmann
parents:
diff changeset
   236
end;