src/HOL/Tools/Old_Datatype/old_size.ML
changeset 67332 cb96edae56ef
parent 67331 a8770603a269
child 67333 ac0b81ca3ed5
     1.1 --- a/src/HOL/Tools/Old_Datatype/old_size.ML	Tue Jan 02 23:04:15 2018 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,228 +0,0 @@
     1.4 -(*  Title:      HOL/Tools/Old_Datatype/old_size.ML
     1.5 -    Author:     Stefan Berghofer, Florian Haftmann, TU Muenchen
     1.6 -
     1.7 -Size functions for old-style datatypes.
     1.8 -*)
     1.9 -
    1.10 -structure Old_Size: sig end =
    1.11 -struct
    1.12 -
    1.13 -fun plus (t1, t2) = Const (@{const_name Groups.plus},
    1.14 -  HOLogic.natT --> HOLogic.natT --> HOLogic.natT) $ t1 $ t2;
    1.15 -
    1.16 -fun size_of_type f g h (T as Type (s, Ts)) =
    1.17 -      (case f s of
    1.18 -         SOME t => SOME t
    1.19 -       | NONE => (case g s of
    1.20 -           SOME size_name =>
    1.21 -             SOME (list_comb (Const (size_name,
    1.22 -               map (fn U => U --> HOLogic.natT) Ts @ [T] ---> HOLogic.natT),
    1.23 -                 map (size_of_type' f g h) Ts))
    1.24 -         | NONE => NONE))
    1.25 -  | size_of_type _ _ h (TFree (s, _)) = h s
    1.26 -and size_of_type' f g h T = (case size_of_type f g h T of
    1.27 -      NONE => Abs ("x", T, HOLogic.zero)
    1.28 -    | SOME t => t);
    1.29 -
    1.30 -fun is_poly thy (Old_Datatype_Aux.DtType (name, dts)) =
    1.31 -      is_some (BNF_LFP_Size.size_of_global thy name) andalso exists (is_poly thy) dts
    1.32 -  | is_poly _ _ = true;
    1.33 -
    1.34 -fun constrs_of thy name =
    1.35 -  let
    1.36 -    val {descr, index, ...} = Old_Datatype_Data.the_info thy name
    1.37 -    val SOME (_, _, constrs) = AList.lookup op = descr index
    1.38 -  in constrs end;
    1.39 -
    1.40 -val app = curry (list_comb o swap);
    1.41 -
    1.42 -fun prove_size_thms (info : Old_Datatype_Aux.info) new_type_names thy =
    1.43 -  let
    1.44 -    val {descr, rec_names, rec_rewrites, induct, ...} = info;
    1.45 -    val l = length new_type_names;
    1.46 -    val descr' = List.take (descr, l);
    1.47 -    val tycos = map (#1 o snd) descr';
    1.48 -  in
    1.49 -    if forall (fn tyco => can (Sign.arity_sorts thy tyco) [HOLogic.class_size]) tycos then
    1.50 -      (* nothing to do -- the "size" function is already defined *)
    1.51 -      thy
    1.52 -    else
    1.53 -      let
    1.54 -        val recTs = Old_Datatype_Aux.get_rec_types descr;
    1.55 -        val (recTs1, recTs2) = chop l recTs;
    1.56 -        val (_, (_, paramdts, _)) :: _ = descr;
    1.57 -        val paramTs = map (Old_Datatype_Aux.typ_of_dtyp descr) paramdts;
    1.58 -        val ((param_size_fs, param_size_fTs), f_names) = paramTs |>
    1.59 -          map (fn T as TFree (s, _) =>
    1.60 -            let
    1.61 -              val name = "f" ^ unprefix "'" s;
    1.62 -              val U = T --> HOLogic.natT
    1.63 -            in
    1.64 -              (((s, Free (name, U)), U), name)
    1.65 -            end) |> split_list |>> split_list;
    1.66 -        val param_size = AList.lookup op = param_size_fs;
    1.67 -
    1.68 -        val extra_rewrites = descr |> map (#1 o snd) |> distinct op = |>
    1.69 -          map_filter (Option.map (#2 o snd) o BNF_LFP_Size.size_of_global thy) |> flat;
    1.70 -        val extra_size = Option.map fst o BNF_LFP_Size.size_of_global thy;
    1.71 -
    1.72 -        val (((size_names, size_fns), def_names), def_names') =
    1.73 -          recTs1 |> map (fn T as Type (s, _) =>
    1.74 -            let
    1.75 -              val s' = "size_" ^ Long_Name.base_name s;
    1.76 -              val s'' = Sign.full_bname thy s';
    1.77 -            in
    1.78 -              (s'',
    1.79 -               (list_comb (Const (s'', param_size_fTs @ [T] ---> HOLogic.natT),
    1.80 -                  map snd param_size_fs),
    1.81 -                (s' ^ "_def", s' ^ "_overloaded_def")))
    1.82 -            end) |> split_list ||>> split_list ||>> split_list;
    1.83 -        val overloaded_size_fns = map HOLogic.size_const recTs1;
    1.84 -
    1.85 -        (* instantiation for primrec combinator *)
    1.86 -        fun size_of_constr b size_ofp ((_, cargs), (_, cargs')) =
    1.87 -          let
    1.88 -            val Ts = map (Old_Datatype_Aux.typ_of_dtyp descr) cargs;
    1.89 -            val k = length (filter Old_Datatype_Aux.is_rec_type cargs);
    1.90 -            val (ts, _, _) = fold_rev (fn ((dt, dt'), T) => fn (us, i, j) =>
    1.91 -              if Old_Datatype_Aux.is_rec_type dt then (Bound i :: us, i + 1, j + 1)
    1.92 -              else
    1.93 -                (if b andalso is_poly thy dt' then
    1.94 -                   case size_of_type (K NONE) extra_size size_ofp T of
    1.95 -                     NONE => us | SOME sz => sz $ Bound j :: us
    1.96 -                 else us, i, j + 1))
    1.97 -                  (cargs ~~ cargs' ~~ Ts) ([], 0, k);
    1.98 -            val t =
    1.99 -              if null ts andalso (not b orelse not (exists (is_poly thy) cargs'))
   1.100 -              then HOLogic.zero
   1.101 -              else foldl1 plus (ts @ [HOLogic.Suc_zero])
   1.102 -          in
   1.103 -            fold_rev (fn T => fn t' => Abs ("x", T, t')) (Ts @ replicate k HOLogic.natT) t
   1.104 -          end;
   1.105 -
   1.106 -        val fs = maps (fn (_, (name, _, constrs)) =>
   1.107 -          map (size_of_constr true param_size) (constrs ~~ constrs_of thy name)) descr;
   1.108 -        val fs' = maps (fn (n, (name, _, constrs)) =>
   1.109 -          map (size_of_constr (l <= n) (K NONE)) (constrs ~~ constrs_of thy name)) descr;
   1.110 -        val fTs = map fastype_of fs;
   1.111 -
   1.112 -        val (rec_combs1, rec_combs2) = chop l (map (fn (T, rec_name) =>
   1.113 -          Const (rec_name, fTs @ [T] ---> HOLogic.natT))
   1.114 -            (recTs ~~ rec_names));
   1.115 -
   1.116 -        fun define_overloaded (def_name, eq) lthy =
   1.117 -          let
   1.118 -            val (Free (c, _), rhs) = (Logic.dest_equals o Syntax.check_term lthy) eq;
   1.119 -            val (thm, lthy') = lthy
   1.120 -              |> Local_Theory.define ((Binding.name c, NoSyn), ((Binding.name def_name, []), rhs))
   1.121 -              |-> (fn (t, (_, thm)) => Spec_Rules.add Spec_Rules.Equational ([t], [thm]) #> pair thm);
   1.122 -            val ctxt_thy = Proof_Context.init_global (Proof_Context.theory_of lthy');
   1.123 -            val thm' = singleton (Proof_Context.export lthy' ctxt_thy) thm;
   1.124 -          in (thm', lthy') end;
   1.125 -
   1.126 -        val ((size_def_thms, size_def_thms'), thy') =
   1.127 -          thy
   1.128 -          |> Sign.add_consts (map (fn (s, T) => (Binding.name (Long_Name.base_name s),
   1.129 -              param_size_fTs @ [T] ---> HOLogic.natT, NoSyn))
   1.130 -            (size_names ~~ recTs1))
   1.131 -          |> Global_Theory.add_defs false
   1.132 -            (map (Thm.no_attributes o apsnd (Logic.mk_equals o apsnd (app fs)))
   1.133 -               (map Binding.name def_names ~~ (size_fns ~~ rec_combs1)))
   1.134 -          ||> Class.instantiation (tycos, map dest_TFree paramTs, [HOLogic.class_size])
   1.135 -          ||>> fold_map define_overloaded
   1.136 -            (def_names' ~~ map Logic.mk_equals (overloaded_size_fns ~~ map (app fs') rec_combs1))
   1.137 -          ||> Class.prove_instantiation_instance (fn ctxt => Class.intro_classes_tac ctxt [])
   1.138 -          ||> Local_Theory.exit_global;
   1.139 -
   1.140 -        val ctxt = Proof_Context.init_global thy';
   1.141 -
   1.142 -        val simpset1 =
   1.143 -          put_simpset HOL_basic_ss ctxt addsimps @{thm Nat.add_0} :: @{thm Nat.add_0_right} ::
   1.144 -            size_def_thms @ size_def_thms' @ rec_rewrites @ extra_rewrites;
   1.145 -        val xs = map (fn i => "x" ^ string_of_int i) (1 upto length recTs2);
   1.146 -
   1.147 -        fun mk_unfolded_size_eq tab size_ofp fs (p as (_, T), r) =
   1.148 -          HOLogic.mk_eq (app fs r $ Free p,
   1.149 -            the (size_of_type tab extra_size size_ofp T) $ Free p);
   1.150 -
   1.151 -        fun prove_unfolded_size_eqs size_ofp fs =
   1.152 -          if null recTs2 then []
   1.153 -          else Old_Datatype_Aux.split_conj_thm (Goal.prove_sorry ctxt xs []
   1.154 -            (HOLogic.mk_Trueprop (Old_Datatype_Aux.mk_conj (replicate l @{term True} @
   1.155 -               map (mk_unfolded_size_eq (AList.lookup op =
   1.156 -                   (new_type_names ~~ map (app fs) rec_combs1)) size_ofp fs)
   1.157 -                 (xs ~~ recTs2 ~~ rec_combs2))))
   1.158 -            (fn _ => (Old_Datatype_Aux.ind_tac ctxt induct xs THEN_ALL_NEW asm_simp_tac simpset1) 1));
   1.159 -
   1.160 -        val unfolded_size_eqs1 = prove_unfolded_size_eqs param_size fs;
   1.161 -        val unfolded_size_eqs2 = prove_unfolded_size_eqs (K NONE) fs';
   1.162 -
   1.163 -        (* characteristic equations for size functions *)
   1.164 -        fun gen_mk_size_eq p size_of size_ofp size_const T (cname, cargs) =
   1.165 -          let
   1.166 -            val Ts = map (Old_Datatype_Aux.typ_of_dtyp descr) cargs;
   1.167 -            val tnames = Name.variant_list f_names (Old_Datatype_Prop.make_tnames Ts);
   1.168 -            val ts = map_filter (fn (sT as (_, T), dt) =>
   1.169 -              Option.map (fn sz => sz $ Free sT)
   1.170 -                (if p dt then size_of_type size_of extra_size size_ofp T
   1.171 -                 else NONE)) (tnames ~~ Ts ~~ cargs)
   1.172 -          in
   1.173 -            HOLogic.mk_Trueprop (HOLogic.mk_eq
   1.174 -              (size_const $ list_comb (Const (cname, Ts ---> T),
   1.175 -                 map2 (curry Free) tnames Ts),
   1.176 -               if null ts then HOLogic.zero
   1.177 -               else foldl1 plus (ts @ [HOLogic.Suc_zero])))
   1.178 -          end;
   1.179 -
   1.180 -        val simpset2 =
   1.181 -          put_simpset HOL_basic_ss ctxt
   1.182 -            addsimps (rec_rewrites @ size_def_thms @ unfolded_size_eqs1);
   1.183 -        val simpset3 =
   1.184 -          put_simpset HOL_basic_ss ctxt
   1.185 -            addsimps (rec_rewrites @ size_def_thms' @ unfolded_size_eqs2);
   1.186 -
   1.187 -        fun prove_size_eqs p size_fns size_ofp simpset =
   1.188 -          maps (fn (((_, (_, _, constrs)), size_const), T) =>
   1.189 -            map (fn constr => Drule.export_without_context (Goal.prove_sorry ctxt [] []
   1.190 -              (gen_mk_size_eq p (AList.lookup op = (new_type_names ~~ size_fns))
   1.191 -                 size_ofp size_const T constr)
   1.192 -              (fn _ => simp_tac simpset 1))) constrs)
   1.193 -            (descr' ~~ size_fns ~~ recTs1);
   1.194 -
   1.195 -        val size_eqns = prove_size_eqs (is_poly thy') size_fns param_size simpset2 @
   1.196 -          prove_size_eqs Old_Datatype_Aux.is_rec_type overloaded_size_fns (K NONE) simpset3;
   1.197 -
   1.198 -        val ([(_, size_thms)], thy'') = thy'
   1.199 -          |> Global_Theory.note_thmss ""
   1.200 -            [((Binding.name "size",
   1.201 -                [Simplifier.simp_add, Named_Theorems.add @{named_theorems nitpick_simp}]),
   1.202 -              [(size_eqns, [])])];
   1.203 -
   1.204 -      in
   1.205 -        thy''
   1.206 -        |> fold2 (fn new_type_name => fn size_name =>
   1.207 -            BNF_LFP_Size.register_size_global new_type_name size_name refl(*dummy*) size_thms [])
   1.208 -          new_type_names size_names 
   1.209 -        |> Code.declare_default_eqns_global (map (rpair true) size_thms)
   1.210 -      end
   1.211 -  end;
   1.212 -
   1.213 -fun add_size_thms _ (new_type_names as name :: _) thy =
   1.214 -  let
   1.215 -    val info as {descr, ...} = Old_Datatype_Data.the_info thy name;
   1.216 -    val prefix = space_implode "_" (map Long_Name.base_name new_type_names);
   1.217 -    val no_size = exists (fn (_, (_, _, constrs)) => exists (fn (_, cargs) => exists (fn dt =>
   1.218 -      Old_Datatype_Aux.is_rec_type dt andalso
   1.219 -        not (null (fst (Old_Datatype_Aux.strip_dtyp dt)))) cargs) constrs) descr
   1.220 -  in
   1.221 -    if no_size then thy
   1.222 -    else
   1.223 -      thy
   1.224 -      |> Sign.add_path prefix
   1.225 -      |> prove_size_thms info new_type_names
   1.226 -      |> Sign.restore_naming thy
   1.227 -  end;
   1.228 -
   1.229 -val _ = Theory.setup (Old_Datatype_Data.interpretation add_size_thms);
   1.230 -
   1.231 -end;