src/HOL/Tools/Function/size.ML
author haftmann
Thu, 01 May 2014 09:30:36 +0200
changeset 56812 baef1c110f12
parent 56651 fc105315822a
child 56846 9df717fef2bb
permissions -rw-r--r--
centralized upper/lowercase name mangling
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31775
2b04504fcb69 uniformly capitialized names for subdirectories
haftmann
parents: 31737
diff changeset
     1
(*  Title:      HOL/Tools/Function/size.ML
43084
946c8e171ffd more precise authorship, reflecting my own ignorance and hg annotate
krauss
parents: 42361
diff changeset
     2
    Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
     3
141df8b68f63 size hook
haftmann
parents:
diff changeset
     4
Size functions for datatypes.
141df8b68f63 size hook
haftmann
parents:
diff changeset
     5
*)
141df8b68f63 size hook
haftmann
parents:
diff changeset
     6
141df8b68f63 size hook
haftmann
parents:
diff changeset
     7
signature SIZE =
141df8b68f63 size hook
haftmann
parents:
diff changeset
     8
sig
141df8b68f63 size hook
haftmann
parents:
diff changeset
     9
  val setup: theory -> theory
141df8b68f63 size hook
haftmann
parents:
diff changeset
    10
end;
141df8b68f63 size hook
haftmann
parents:
diff changeset
    11
141df8b68f63 size hook
haftmann
parents:
diff changeset
    12
structure Size: SIZE =
141df8b68f63 size hook
haftmann
parents:
diff changeset
    13
struct
141df8b68f63 size hook
haftmann
parents:
diff changeset
    14
35267
8dfd816713c6 moved remaning class operations from Algebras.thy to Groups.thy
haftmann
parents: 35064
diff changeset
    15
fun plus (t1, t2) = Const (@{const_name Groups.plus},
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    16
  HOLogic.natT --> HOLogic.natT --> HOLogic.natT) $ t1 $ t2;
141df8b68f63 size hook
haftmann
parents:
diff changeset
    17
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    18
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
    19
      (case f s of
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    20
         SOME t => SOME t
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    21
       | NONE => (case g s of
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    22
           SOME size_name =>
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    23
             SOME (list_comb (Const (size_name,
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    24
               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
    25
                 map (size_of_type' f g h) Ts))
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    26
         | NONE => NONE))
56643
41d3596d8a64 move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents: 56639
diff changeset
    27
  | size_of_type _ _ h (TFree (s, _)) = h s
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    28
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
    29
      NONE => Abs ("x", T, HOLogic.zero)
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    30
    | SOME t => t);
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    31
55094
149ccaf4ae5c removed dependency on 'Datatype' structure
blanchet
parents: 52788
diff changeset
    32
fun is_poly thy (Datatype_Aux.DtType (name, dts)) =
56651
fc105315822a localize new size function generation code
blanchet
parents: 56649
diff changeset
    33
      is_some (BNF_LFP_Size.lookup_size_global thy name) andalso exists (is_poly thy) dts
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    34
  | is_poly _ _ = true;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    35
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    36
fun constrs_of thy name =
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    37
  let
55094
149ccaf4ae5c removed dependency on 'Datatype' structure
blanchet
parents: 52788
diff changeset
    38
    val {descr, index, ...} = Datatype_Data.the_info thy name
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    39
    val SOME (_, _, constrs) = AList.lookup op = descr index
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    40
  in constrs end;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    41
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    42
val app = curry (list_comb o swap);
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    43
55094
149ccaf4ae5c removed dependency on 'Datatype' structure
blanchet
parents: 52788
diff changeset
    44
fun prove_size_thms (info : Datatype_Aux.info) new_type_names thy =
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    45
  let
45822
843dc212f69e datatype dtyp with explicit sort information;
wenzelm
parents: 45740
diff changeset
    46
    val {descr, rec_names, rec_rewrites, induct, ...} = info;
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    47
    val l = length new_type_names;
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    48
    val descr' = List.take (descr, l);
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    49
    val tycos = map (#1 o snd) descr';
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    50
  in
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    51
    if forall (fn tyco => can (Sign.arity_sorts thy tyco) [HOLogic.class_size]) tycos then
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    52
      (* nothing to do -- the "size" function is already defined *)
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    53
      thy
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    54
    else
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
    55
      let
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    56
        val recTs = Datatype_Aux.get_rec_types descr;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    57
        val (recTs1, recTs2) = chop l recTs;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    58
        val (_, (_, paramdts, _)) :: _ = descr;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    59
        val paramTs = map (Datatype_Aux.typ_of_dtyp descr) paramdts;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    60
        val ((param_size_fs, param_size_fTs), f_names) = paramTs |>
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    61
          map (fn T as TFree (s, _) =>
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    62
            let
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    63
              val name = "f" ^ unprefix "'" s;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    64
              val U = T --> HOLogic.natT
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    65
            in
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    66
              (((s, Free (name, U)), U), name)
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    67
            end) |> split_list |>> split_list;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    68
        val param_size = AList.lookup op = param_size_fs;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    69
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    70
        val extra_rewrites = descr |> map (#1 o snd) |> distinct op = |>
56651
fc105315822a localize new size function generation code
blanchet
parents: 56649
diff changeset
    71
          map_filter (Option.map (fst o snd) o BNF_LFP_Size.lookup_size_global thy) |> flat;
fc105315822a localize new size function generation code
blanchet
parents: 56649
diff changeset
    72
        val extra_size = Option.map fst o BNF_LFP_Size.lookup_size_global thy;
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    73
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    74
        val (((size_names, size_fns), def_names), def_names') =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    75
          recTs1 |> map (fn T as Type (s, _) =>
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    76
            let
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    77
              val s' = Long_Name.base_name s ^ "_size";
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    78
              val s'' = Sign.full_bname thy s';
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    79
            in
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    80
              (s'',
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    81
               (list_comb (Const (s'', param_size_fTs @ [T] ---> HOLogic.natT),
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    82
                  map snd param_size_fs),
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    83
                (s' ^ "_def", s' ^ "_overloaded_def")))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    84
            end) |> split_list ||>> split_list ||>> split_list;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    85
        val overloaded_size_fns = map HOLogic.size_const recTs1;
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
    86
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    87
        (* instantiation for primrec combinator *)
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    88
        fun size_of_constr b size_ofp ((_, cargs), (_, cargs')) =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    89
          let
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    90
            val Ts = map (Datatype_Aux.typ_of_dtyp descr) cargs;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    91
            val k = length (filter Datatype_Aux.is_rec_type cargs);
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    92
            val (ts, _, _) = fold_rev (fn ((dt, dt'), T) => fn (us, i, j) =>
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    93
              if Datatype_Aux.is_rec_type dt then (Bound i :: us, i + 1, j + 1)
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    94
              else
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    95
                (if b andalso is_poly thy dt' then
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    96
                   case size_of_type (K NONE) extra_size size_ofp T of
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    97
                     NONE => us | SOME sz => sz $ Bound j :: us
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    98
                 else us, i, j + 1))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
    99
                  (cargs ~~ cargs' ~~ Ts) ([], 0, k);
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   100
            val t =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   101
              if null ts andalso (not b orelse not (exists (is_poly thy) cargs'))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   102
              then HOLogic.zero
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   103
              else foldl1 plus (ts @ [HOLogic.Suc_zero])
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   104
          in
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   105
            fold_rev (fn T => fn t' => Abs ("x", T, t')) (Ts @ replicate k HOLogic.natT) t
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   106
          end;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   107
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   108
        val fs = maps (fn (_, (name, _, constrs)) =>
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   109
          map (size_of_constr true param_size) (constrs ~~ constrs_of thy name)) descr;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   110
        val fs' = maps (fn (n, (name, _, constrs)) =>
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   111
          map (size_of_constr (l <= n) (K NONE)) (constrs ~~ constrs_of thy name)) descr;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   112
        val fTs = map fastype_of fs;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   113
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   114
        val (rec_combs1, rec_combs2) = chop l (map (fn (T, rec_name) =>
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   115
          Const (rec_name, fTs @ [T] ---> HOLogic.natT))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   116
            (recTs ~~ rec_names));
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   117
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   118
        fun define_overloaded (def_name, eq) lthy =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   119
          let
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   120
            val (Free (c, _), rhs) = (Logic.dest_equals o Syntax.check_term lthy) eq;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   121
            val (thm, lthy') = lthy
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   122
              |> Local_Theory.define ((Binding.name c, NoSyn), ((Binding.name def_name, []), rhs))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   123
              |-> (fn (t, (_, thm)) => Spec_Rules.add Spec_Rules.Equational ([t], [thm]) #> pair thm);
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   124
            val ctxt_thy = Proof_Context.init_global (Proof_Context.theory_of lthy');
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   125
            val thm' = singleton (Proof_Context.export lthy' ctxt_thy) thm;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   126
          in (thm', lthy') end;
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   127
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   128
        val ((size_def_thms, size_def_thms'), thy') =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   129
          thy
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   130
          |> Sign.add_consts (map (fn (s, T) => (Binding.name (Long_Name.base_name s),
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   131
              param_size_fTs @ [T] ---> HOLogic.natT, NoSyn))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   132
            (size_names ~~ recTs1))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   133
          |> Global_Theory.add_defs false
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   134
            (map (Thm.no_attributes o apsnd (Logic.mk_equals o apsnd (app fs)))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   135
               (map Binding.name def_names ~~ (size_fns ~~ rec_combs1)))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   136
          ||> Class.instantiation (tycos, map dest_TFree paramTs, [HOLogic.class_size])
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   137
          ||>> fold_map define_overloaded
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   138
            (def_names' ~~ map Logic.mk_equals (overloaded_size_fns ~~ map (app fs') rec_combs1))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   139
          ||> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   140
          ||> Local_Theory.exit_global;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   141
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   142
        val ctxt = Proof_Context.init_global thy';
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   143
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   144
        val simpset1 =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   145
          put_simpset HOL_basic_ss ctxt addsimps @{thm Nat.add_0} :: @{thm Nat.add_0_right} ::
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   146
            size_def_thms @ size_def_thms' @ rec_rewrites @ extra_rewrites;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   147
        val xs = map (fn i => "x" ^ string_of_int i) (1 upto length recTs2);
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   148
56643
41d3596d8a64 move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents: 56639
diff changeset
   149
        fun mk_unfolded_size_eq tab size_ofp fs (p as (_, T), r) =
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   150
          HOLogic.mk_eq (app fs r $ Free p,
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   151
            the (size_of_type tab extra_size size_ofp T) $ Free p);
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   152
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   153
        fun prove_unfolded_size_eqs size_ofp fs =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   154
          if null recTs2 then []
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   155
          else Datatype_Aux.split_conj_thm (Goal.prove_sorry ctxt xs []
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   156
            (HOLogic.mk_Trueprop (Datatype_Aux.mk_conj (replicate l @{term True} @
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   157
               map (mk_unfolded_size_eq (AList.lookup op =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   158
                   (new_type_names ~~ map (app fs) rec_combs1)) size_ofp fs)
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   159
                 (xs ~~ recTs2 ~~ rec_combs2))))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   160
            (fn _ => (Datatype_Aux.ind_tac induct xs THEN_ALL_NEW asm_simp_tac simpset1) 1));
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   161
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   162
        val unfolded_size_eqs1 = prove_unfolded_size_eqs param_size fs;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   163
        val unfolded_size_eqs2 = prove_unfolded_size_eqs (K NONE) fs';
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   164
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   165
        (* characteristic equations for size functions *)
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   166
        fun gen_mk_size_eq p size_of size_ofp size_const T (cname, cargs) =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   167
          let
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   168
            val Ts = map (Datatype_Aux.typ_of_dtyp descr) cargs;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   169
            val tnames = Name.variant_list f_names (Datatype_Prop.make_tnames Ts);
56643
41d3596d8a64 move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents: 56639
diff changeset
   170
            val ts = map_filter (fn (sT as (_, T), dt) =>
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   171
              Option.map (fn sz => sz $ Free sT)
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   172
                (if p dt then size_of_type size_of extra_size size_ofp T
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   173
                 else NONE)) (tnames ~~ Ts ~~ cargs)
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   174
          in
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   175
            HOLogic.mk_Trueprop (HOLogic.mk_eq
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   176
              (size_const $ list_comb (Const (cname, Ts ---> T),
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   177
                 map2 (curry Free) tnames Ts),
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   178
               if null ts then HOLogic.zero
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   179
               else foldl1 plus (ts @ [HOLogic.Suc_zero])))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   180
          end;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   181
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   182
        val simpset2 =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   183
          put_simpset HOL_basic_ss ctxt
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   184
            addsimps (rec_rewrites @ size_def_thms @ unfolded_size_eqs1);
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   185
        val simpset3 =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   186
          put_simpset HOL_basic_ss ctxt
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   187
            addsimps (rec_rewrites @ size_def_thms' @ unfolded_size_eqs2);
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   188
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   189
        fun prove_size_eqs p size_fns size_ofp simpset =
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   190
          maps (fn (((_, (_, _, constrs)), size_const), T) =>
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   191
            map (fn constr => Drule.export_without_context (Goal.prove_sorry ctxt [] []
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   192
              (gen_mk_size_eq p (AList.lookup op = (new_type_names ~~ size_fns))
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   193
                 size_ofp size_const T constr)
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   194
              (fn _ => simp_tac simpset 1))) constrs)
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   195
            (descr' ~~ size_fns ~~ recTs1);
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   196
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   197
        val size_eqns = prove_size_eqs (is_poly thy') size_fns param_size simpset2 @
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   198
          prove_size_eqs Datatype_Aux.is_rec_type overloaded_size_fns (K NONE) simpset3;
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   199
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   200
        val ([(_, size_thms)], thy'') = thy'
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   201
          |> Global_Theory.note_thmss ""
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   202
            [((Binding.name "size",
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   203
                [Simplifier.simp_add, Nitpick_Simps.add,
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   204
                 Thm.declaration_attribute (fn thm =>
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   205
                   Context.mapping (Code.add_default_eqn thm) I)]),
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   206
              [(size_eqns, [])])];
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   207
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   208
      in
56643
41d3596d8a64 move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents: 56639
diff changeset
   209
        fold2 (fn new_type_name => fn size_name =>
56651
fc105315822a localize new size function generation code
blanchet
parents: 56649
diff changeset
   210
            BNF_LFP_Size.register_size_global new_type_name size_name size_thms [])
56643
41d3596d8a64 move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents: 56639
diff changeset
   211
          new_type_names size_names thy''
56639
c9d6b581bd3b made old-style 'size' interpretation hook more robust in case 'size' is already specified (either by the user or by the new datatype package)
blanchet
parents: 56375
diff changeset
   212
      end
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   213
  end;
141df8b68f63 size hook
haftmann
parents:
diff changeset
   214
56643
41d3596d8a64 move size hooks together, with new one preceding old one and sharing same theory data
blanchet
parents: 56639
diff changeset
   215
fun add_size_thms _ (new_type_names as name :: _) thy =
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   216
  let
55094
149ccaf4ae5c removed dependency on 'Datatype' structure
blanchet
parents: 52788
diff changeset
   217
    val info as {descr, ...} = Datatype_Data.the_info thy name;
56375
32e0da92c786 use same idiom as used for datatype 'size' function to name constants and theorems emerging from various type interpretations -- reduces the chances of name clashes on theory merges
blanchet
parents: 56239
diff changeset
   218
    val prefix = space_implode "_" (map Long_Name.base_name new_type_names);
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   219
    val no_size = exists (fn (_, (_, _, constrs)) => exists (fn (_, cargs) => exists (fn dt =>
45736
2888e076ac17 do not open ML structures;
wenzelm
parents: 45735
diff changeset
   220
      Datatype_Aux.is_rec_type dt andalso
2888e076ac17 do not open ML structures;
wenzelm
parents: 45735
diff changeset
   221
        not (null (fst (Datatype_Aux.strip_dtyp dt)))) cargs) constrs) descr
45701
615da8b8d758 discontinued obsolete datatype "alt_names";
wenzelm
parents: 43084
diff changeset
   222
  in
615da8b8d758 discontinued obsolete datatype "alt_names";
wenzelm
parents: 43084
diff changeset
   223
    if no_size then thy
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   224
    else
141df8b68f63 size hook
haftmann
parents:
diff changeset
   225
      thy
24714
1618c2ac1b74 proper Sign operations instead of Theory aliases;
wenzelm
parents: 24711
diff changeset
   226
      |> Sign.add_path prefix
25679
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   227
      |> prove_size_thms info new_type_names
b77f797b528a size functions for nested datatypes are now expressed using
berghofe
parents: 25569
diff changeset
   228
      |> Sign.restore_naming thy
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   229
  end;
141df8b68f63 size hook
haftmann
parents:
diff changeset
   230
55094
149ccaf4ae5c removed dependency on 'Datatype' structure
blanchet
parents: 52788
diff changeset
   231
val setup = Datatype_Data.interpretation add_size_thms;
24710
141df8b68f63 size hook
haftmann
parents:
diff changeset
   232
29866
6e93ae65c678 Added Nitpick_Const_Psimp attribute, dropped the 's' in Nitpick_Const_Simps, and killed the Nitpick_Ind_Intros attribute.
blanchet
parents: 29863
diff changeset
   233
end;