src/HOL/HOLCF/Tools/Domain/domain_induction.ML
changeset 40774 0437dbc127b3
parent 40488 c67253b83dc8
child 40832 4352ca878c41
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/HOLCF/Tools/Domain/domain_induction.ML	Sat Nov 27 16:08:10 2010 -0800
     1.3 @@ -0,0 +1,439 @@
     1.4 +(*  Title:      HOLCF/Tools/Domain/domain_induction.ML
     1.5 +    Author:     David von Oheimb
     1.6 +    Author:     Brian Huffman
     1.7 +
     1.8 +Proofs of high-level (co)induction rules for domain command.
     1.9 +*)
    1.10 +
    1.11 +signature DOMAIN_INDUCTION =
    1.12 +sig
    1.13 +  val comp_theorems :
    1.14 +      binding list ->
    1.15 +      Domain_Take_Proofs.take_induct_info ->
    1.16 +      Domain_Constructors.constr_info list ->
    1.17 +      theory -> thm list * theory
    1.18 +
    1.19 +  val quiet_mode: bool Unsynchronized.ref;
    1.20 +  val trace_domain: bool Unsynchronized.ref;
    1.21 +end;
    1.22 +
    1.23 +structure Domain_Induction :> DOMAIN_INDUCTION =
    1.24 +struct
    1.25 +
    1.26 +val quiet_mode = Unsynchronized.ref false;
    1.27 +val trace_domain = Unsynchronized.ref false;
    1.28 +
    1.29 +fun message s = if !quiet_mode then () else writeln s;
    1.30 +fun trace s = if !trace_domain then tracing s else ();
    1.31 +
    1.32 +open HOLCF_Library;
    1.33 +
    1.34 +(******************************************************************************)
    1.35 +(***************************** proofs about take ******************************)
    1.36 +(******************************************************************************)
    1.37 +
    1.38 +fun take_theorems
    1.39 +    (dbinds : binding list)
    1.40 +    (take_info : Domain_Take_Proofs.take_induct_info)
    1.41 +    (constr_infos : Domain_Constructors.constr_info list)
    1.42 +    (thy : theory) : thm list list * theory =
    1.43 +let
    1.44 +  val {take_consts, take_Suc_thms, deflation_take_thms, ...} = take_info;
    1.45 +  val deflation_thms = Domain_Take_Proofs.get_deflation_thms thy;
    1.46 +
    1.47 +  val n = Free ("n", @{typ nat});
    1.48 +  val n' = @{const Suc} $ n;
    1.49 +
    1.50 +  local
    1.51 +    val newTs = map (#absT o #iso_info) constr_infos;
    1.52 +    val subs = newTs ~~ map (fn t => t $ n) take_consts;
    1.53 +    fun is_ID (Const (c, _)) = (c = @{const_name ID})
    1.54 +      | is_ID _              = false;
    1.55 +  in
    1.56 +    fun map_of_arg thy v T =
    1.57 +      let val m = Domain_Take_Proofs.map_of_typ thy subs T;
    1.58 +      in if is_ID m then v else mk_capply (m, v) end;
    1.59 +  end
    1.60 +
    1.61 +  fun prove_take_apps
    1.62 +      ((dbind, take_const), constr_info) thy =
    1.63 +    let
    1.64 +      val {iso_info, con_specs, con_betas, ...} = constr_info;
    1.65 +      val {abs_inverse, ...} = iso_info;
    1.66 +      fun prove_take_app (con_const, args) =
    1.67 +        let
    1.68 +          val Ts = map snd args;
    1.69 +          val ns = Name.variant_list ["n"] (Datatype_Prop.make_tnames Ts);
    1.70 +          val vs = map Free (ns ~~ Ts);
    1.71 +          val lhs = mk_capply (take_const $ n', list_ccomb (con_const, vs));
    1.72 +          val rhs = list_ccomb (con_const, map2 (map_of_arg thy) vs Ts);
    1.73 +          val goal = mk_trp (mk_eq (lhs, rhs));
    1.74 +          val rules =
    1.75 +              [abs_inverse] @ con_betas @ @{thms take_con_rules}
    1.76 +              @ take_Suc_thms @ deflation_thms @ deflation_take_thms;
    1.77 +          val tac = simp_tac (HOL_basic_ss addsimps rules) 1;
    1.78 +        in
    1.79 +          Goal.prove_global thy [] [] goal (K tac)
    1.80 +        end;
    1.81 +      val take_apps = map prove_take_app con_specs;
    1.82 +    in
    1.83 +      yield_singleton Global_Theory.add_thmss
    1.84 +        ((Binding.qualified true "take_rews" dbind, take_apps),
    1.85 +        [Simplifier.simp_add]) thy
    1.86 +    end;
    1.87 +in
    1.88 +  fold_map prove_take_apps
    1.89 +    (dbinds ~~ take_consts ~~ constr_infos) thy
    1.90 +end;
    1.91 +
    1.92 +(******************************************************************************)
    1.93 +(****************************** induction rules *******************************)
    1.94 +(******************************************************************************)
    1.95 +
    1.96 +val case_UU_allI =
    1.97 +    @{lemma "(!!x. x ~= UU ==> P x) ==> P UU ==> ALL x. P x" by metis};
    1.98 +
    1.99 +fun prove_induction
   1.100 +    (comp_dbind : binding)
   1.101 +    (constr_infos : Domain_Constructors.constr_info list)
   1.102 +    (take_info : Domain_Take_Proofs.take_induct_info)
   1.103 +    (take_rews : thm list)
   1.104 +    (thy : theory) =
   1.105 +let
   1.106 +  val comp_dname = Binding.name_of comp_dbind;
   1.107 +
   1.108 +  val iso_infos = map #iso_info constr_infos;
   1.109 +  val exhausts = map #exhaust constr_infos;
   1.110 +  val con_rews = maps #con_rews constr_infos;
   1.111 +  val {take_consts, take_induct_thms, ...} = take_info;
   1.112 +
   1.113 +  val newTs = map #absT iso_infos;
   1.114 +  val P_names = Datatype_Prop.indexify_names (map (K "P") newTs);
   1.115 +  val x_names = Datatype_Prop.indexify_names (map (K "x") newTs);
   1.116 +  val P_types = map (fn T => T --> HOLogic.boolT) newTs;
   1.117 +  val Ps = map Free (P_names ~~ P_types);
   1.118 +  val xs = map Free (x_names ~~ newTs);
   1.119 +  val n = Free ("n", HOLogic.natT);
   1.120 +
   1.121 +  fun con_assm defined p (con, args) =
   1.122 +    let
   1.123 +      val Ts = map snd args;
   1.124 +      val ns = Name.variant_list P_names (Datatype_Prop.make_tnames Ts);
   1.125 +      val vs = map Free (ns ~~ Ts);
   1.126 +      val nonlazy = map snd (filter_out (fst o fst) (args ~~ vs));
   1.127 +      fun ind_hyp (v, T) t =
   1.128 +          case AList.lookup (op =) (newTs ~~ Ps) T of NONE => t
   1.129 +          | SOME p' => Logic.mk_implies (mk_trp (p' $ v), t);
   1.130 +      val t1 = mk_trp (p $ list_ccomb (con, vs));
   1.131 +      val t2 = fold_rev ind_hyp (vs ~~ Ts) t1;
   1.132 +      val t3 = Logic.list_implies (map (mk_trp o mk_defined) nonlazy, t2);
   1.133 +    in fold_rev Logic.all vs (if defined then t3 else t2) end;
   1.134 +  fun eq_assms ((p, T), cons) =
   1.135 +      mk_trp (p $ HOLCF_Library.mk_bottom T) :: map (con_assm true p) cons;
   1.136 +  val assms = maps eq_assms (Ps ~~ newTs ~~ map #con_specs constr_infos);
   1.137 +
   1.138 +  val take_ss = HOL_ss addsimps (@{thm Rep_cfun_strict1} :: take_rews);
   1.139 +  fun quant_tac ctxt i = EVERY
   1.140 +    (map (fn name => res_inst_tac ctxt [(("x", 0), name)] spec i) x_names);
   1.141 +
   1.142 +  (* FIXME: move this message to domain_take_proofs.ML *)
   1.143 +  val is_finite = #is_finite take_info;
   1.144 +  val _ = if is_finite
   1.145 +          then message ("Proving finiteness rule for domain "^comp_dname^" ...")
   1.146 +          else ();
   1.147 +
   1.148 +  val _ = trace " Proving finite_ind...";
   1.149 +  val finite_ind =
   1.150 +    let
   1.151 +      val concls =
   1.152 +          map (fn ((P, t), x) => P $ mk_capply (t $ n, x))
   1.153 +              (Ps ~~ take_consts ~~ xs);
   1.154 +      val goal = mk_trp (foldr1 mk_conj concls);
   1.155 +
   1.156 +      fun tacf {prems, context} =
   1.157 +        let
   1.158 +          (* Prove stronger prems, without definedness side conditions *)
   1.159 +          fun con_thm p (con, args) =
   1.160 +            let
   1.161 +              val subgoal = con_assm false p (con, args);
   1.162 +              val rules = prems @ con_rews @ simp_thms;
   1.163 +              val simplify = asm_simp_tac (HOL_basic_ss addsimps rules);
   1.164 +              fun arg_tac (lazy, _) =
   1.165 +                  rtac (if lazy then allI else case_UU_allI) 1;
   1.166 +              val tacs =
   1.167 +                  rewrite_goals_tac @{thms atomize_all atomize_imp} ::
   1.168 +                  map arg_tac args @
   1.169 +                  [REPEAT (rtac impI 1), ALLGOALS simplify];
   1.170 +            in
   1.171 +              Goal.prove context [] [] subgoal (K (EVERY tacs))
   1.172 +            end;
   1.173 +          fun eq_thms (p, cons) = map (con_thm p) cons;
   1.174 +          val conss = map #con_specs constr_infos;
   1.175 +          val prems' = maps eq_thms (Ps ~~ conss);
   1.176 +
   1.177 +          val tacs1 = [
   1.178 +            quant_tac context 1,
   1.179 +            simp_tac HOL_ss 1,
   1.180 +            InductTacs.induct_tac context [[SOME "n"]] 1,
   1.181 +            simp_tac (take_ss addsimps prems) 1,
   1.182 +            TRY (safe_tac HOL_cs)];
   1.183 +          fun con_tac _ = 
   1.184 +            asm_simp_tac take_ss 1 THEN
   1.185 +            (resolve_tac prems' THEN_ALL_NEW etac spec) 1;
   1.186 +          fun cases_tacs (cons, exhaust) =
   1.187 +            res_inst_tac context [(("y", 0), "x")] exhaust 1 ::
   1.188 +            asm_simp_tac (take_ss addsimps prems) 1 ::
   1.189 +            map con_tac cons;
   1.190 +          val tacs = tacs1 @ maps cases_tacs (conss ~~ exhausts)
   1.191 +        in
   1.192 +          EVERY (map DETERM tacs)
   1.193 +        end;
   1.194 +    in Goal.prove_global thy [] assms goal tacf end;
   1.195 +
   1.196 +  val _ = trace " Proving ind...";
   1.197 +  val ind =
   1.198 +    let
   1.199 +      val concls = map (op $) (Ps ~~ xs);
   1.200 +      val goal = mk_trp (foldr1 mk_conj concls);
   1.201 +      val adms = if is_finite then [] else map (mk_trp o mk_adm) Ps;
   1.202 +      fun tacf {prems, context} =
   1.203 +        let
   1.204 +          fun finite_tac (take_induct, fin_ind) =
   1.205 +              rtac take_induct 1 THEN
   1.206 +              (if is_finite then all_tac else resolve_tac prems 1) THEN
   1.207 +              (rtac fin_ind THEN_ALL_NEW solve_tac prems) 1;
   1.208 +          val fin_inds = Project_Rule.projections context finite_ind;
   1.209 +        in
   1.210 +          TRY (safe_tac HOL_cs) THEN
   1.211 +          EVERY (map finite_tac (take_induct_thms ~~ fin_inds))
   1.212 +        end;
   1.213 +    in Goal.prove_global thy [] (adms @ assms) goal tacf end
   1.214 +
   1.215 +  (* case names for induction rules *)
   1.216 +  val dnames = map (fst o dest_Type) newTs;
   1.217 +  val case_ns =
   1.218 +    let
   1.219 +      val adms =
   1.220 +          if is_finite then [] else
   1.221 +          if length dnames = 1 then ["adm"] else
   1.222 +          map (fn s => "adm_" ^ Long_Name.base_name s) dnames;
   1.223 +      val bottoms =
   1.224 +          if length dnames = 1 then ["bottom"] else
   1.225 +          map (fn s => "bottom_" ^ Long_Name.base_name s) dnames;
   1.226 +      fun one_eq bot constr_info =
   1.227 +        let fun name_of (c, args) = Long_Name.base_name (fst (dest_Const c));
   1.228 +        in bot :: map name_of (#con_specs constr_info) end;
   1.229 +    in adms @ flat (map2 one_eq bottoms constr_infos) end;
   1.230 +
   1.231 +  val inducts = Project_Rule.projections (ProofContext.init_global thy) ind;
   1.232 +  fun ind_rule (dname, rule) =
   1.233 +      ((Binding.empty, rule),
   1.234 +       [Rule_Cases.case_names case_ns, Induct.induct_type dname]);
   1.235 +
   1.236 +in
   1.237 +  thy
   1.238 +  |> snd o Global_Theory.add_thms [
   1.239 +     ((Binding.qualified true "finite_induct" comp_dbind, finite_ind), []),
   1.240 +     ((Binding.qualified true "induct"        comp_dbind, ind       ), [])]
   1.241 +  |> (snd o Global_Theory.add_thms (map ind_rule (dnames ~~ inducts)))
   1.242 +end; (* prove_induction *)
   1.243 +
   1.244 +(******************************************************************************)
   1.245 +(************************ bisimulation and coinduction ************************)
   1.246 +(******************************************************************************)
   1.247 +
   1.248 +fun prove_coinduction
   1.249 +    (comp_dbind : binding, dbinds : binding list)
   1.250 +    (constr_infos : Domain_Constructors.constr_info list)
   1.251 +    (take_info : Domain_Take_Proofs.take_induct_info)
   1.252 +    (take_rews : thm list list)
   1.253 +    (thy : theory) : theory =
   1.254 +let
   1.255 +  val iso_infos = map #iso_info constr_infos;
   1.256 +  val newTs = map #absT iso_infos;
   1.257 +
   1.258 +  val {take_consts, take_0_thms, take_lemma_thms, ...} = take_info;
   1.259 +
   1.260 +  val R_names = Datatype_Prop.indexify_names (map (K "R") newTs);
   1.261 +  val R_types = map (fn T => T --> T --> boolT) newTs;
   1.262 +  val Rs = map Free (R_names ~~ R_types);
   1.263 +  val n = Free ("n", natT);
   1.264 +  val reserved = "x" :: "y" :: R_names;
   1.265 +
   1.266 +  (* declare bisimulation predicate *)
   1.267 +  val bisim_bind = Binding.suffix_name "_bisim" comp_dbind;
   1.268 +  val bisim_type = R_types ---> boolT;
   1.269 +  val (bisim_const, thy) =
   1.270 +      Sign.declare_const ((bisim_bind, bisim_type), NoSyn) thy;
   1.271 +
   1.272 +  (* define bisimulation predicate *)
   1.273 +  local
   1.274 +    fun one_con T (con, args) =
   1.275 +      let
   1.276 +        val Ts = map snd args;
   1.277 +        val ns1 = Name.variant_list reserved (Datatype_Prop.make_tnames Ts);
   1.278 +        val ns2 = map (fn n => n^"'") ns1;
   1.279 +        val vs1 = map Free (ns1 ~~ Ts);
   1.280 +        val vs2 = map Free (ns2 ~~ Ts);
   1.281 +        val eq1 = mk_eq (Free ("x", T), list_ccomb (con, vs1));
   1.282 +        val eq2 = mk_eq (Free ("y", T), list_ccomb (con, vs2));
   1.283 +        fun rel ((v1, v2), T) =
   1.284 +            case AList.lookup (op =) (newTs ~~ Rs) T of
   1.285 +              NONE => mk_eq (v1, v2) | SOME r => r $ v1 $ v2;
   1.286 +        val eqs = foldr1 mk_conj (map rel (vs1 ~~ vs2 ~~ Ts) @ [eq1, eq2]);
   1.287 +      in
   1.288 +        Library.foldr mk_ex (vs1 @ vs2, eqs)
   1.289 +      end;
   1.290 +    fun one_eq ((T, R), cons) =
   1.291 +      let
   1.292 +        val x = Free ("x", T);
   1.293 +        val y = Free ("y", T);
   1.294 +        val disj1 = mk_conj (mk_eq (x, mk_bottom T), mk_eq (y, mk_bottom T));
   1.295 +        val disjs = disj1 :: map (one_con T) cons;
   1.296 +      in
   1.297 +        mk_all (x, mk_all (y, mk_imp (R $ x $ y, foldr1 mk_disj disjs)))
   1.298 +      end;
   1.299 +    val conjs = map one_eq (newTs ~~ Rs ~~ map #con_specs constr_infos);
   1.300 +    val bisim_rhs = lambdas Rs (Library.foldr1 mk_conj conjs);
   1.301 +    val bisim_eqn = Logic.mk_equals (bisim_const, bisim_rhs);
   1.302 +  in
   1.303 +    val (bisim_def_thm, thy) = thy |>
   1.304 +        yield_singleton (Global_Theory.add_defs false)
   1.305 +         ((Binding.qualified true "bisim_def" comp_dbind, bisim_eqn), []);
   1.306 +  end (* local *)
   1.307 +
   1.308 +  (* prove coinduction lemma *)
   1.309 +  val coind_lemma =
   1.310 +    let
   1.311 +      val assm = mk_trp (list_comb (bisim_const, Rs));
   1.312 +      fun one ((T, R), take_const) =
   1.313 +        let
   1.314 +          val x = Free ("x", T);
   1.315 +          val y = Free ("y", T);
   1.316 +          val lhs = mk_capply (take_const $ n, x);
   1.317 +          val rhs = mk_capply (take_const $ n, y);
   1.318 +        in
   1.319 +          mk_all (x, mk_all (y, mk_imp (R $ x $ y, mk_eq (lhs, rhs))))
   1.320 +        end;
   1.321 +      val goal =
   1.322 +          mk_trp (foldr1 mk_conj (map one (newTs ~~ Rs ~~ take_consts)));
   1.323 +      val rules = @{thm Rep_cfun_strict1} :: take_0_thms;
   1.324 +      fun tacf {prems, context} =
   1.325 +        let
   1.326 +          val prem' = rewrite_rule [bisim_def_thm] (hd prems);
   1.327 +          val prems' = Project_Rule.projections context prem';
   1.328 +          val dests = map (fn th => th RS spec RS spec RS mp) prems';
   1.329 +          fun one_tac (dest, rews) =
   1.330 +              dtac dest 1 THEN safe_tac HOL_cs THEN
   1.331 +              ALLGOALS (asm_simp_tac (HOL_basic_ss addsimps rews));
   1.332 +        in
   1.333 +          rtac @{thm nat.induct} 1 THEN
   1.334 +          simp_tac (HOL_ss addsimps rules) 1 THEN
   1.335 +          safe_tac HOL_cs THEN
   1.336 +          EVERY (map one_tac (dests ~~ take_rews))
   1.337 +        end
   1.338 +    in
   1.339 +      Goal.prove_global thy [] [assm] goal tacf
   1.340 +    end;
   1.341 +
   1.342 +  (* prove individual coinduction rules *)
   1.343 +  fun prove_coind ((T, R), take_lemma) =
   1.344 +    let
   1.345 +      val x = Free ("x", T);
   1.346 +      val y = Free ("y", T);
   1.347 +      val assm1 = mk_trp (list_comb (bisim_const, Rs));
   1.348 +      val assm2 = mk_trp (R $ x $ y);
   1.349 +      val goal = mk_trp (mk_eq (x, y));
   1.350 +      fun tacf {prems, context} =
   1.351 +        let
   1.352 +          val rule = hd prems RS coind_lemma;
   1.353 +        in
   1.354 +          rtac take_lemma 1 THEN
   1.355 +          asm_simp_tac (HOL_basic_ss addsimps (rule :: prems)) 1
   1.356 +        end;
   1.357 +    in
   1.358 +      Goal.prove_global thy [] [assm1, assm2] goal tacf
   1.359 +    end;
   1.360 +  val coinds = map prove_coind (newTs ~~ Rs ~~ take_lemma_thms);
   1.361 +  val coind_binds = map (Binding.qualified true "coinduct") dbinds;
   1.362 +
   1.363 +in
   1.364 +  thy |> snd o Global_Theory.add_thms
   1.365 +    (map Thm.no_attributes (coind_binds ~~ coinds))
   1.366 +end; (* let *)
   1.367 +
   1.368 +(******************************************************************************)
   1.369 +(******************************* main function ********************************)
   1.370 +(******************************************************************************)
   1.371 +
   1.372 +fun comp_theorems
   1.373 +    (dbinds : binding list)
   1.374 +    (take_info : Domain_Take_Proofs.take_induct_info)
   1.375 +    (constr_infos : Domain_Constructors.constr_info list)
   1.376 +    (thy : theory) =
   1.377 +let
   1.378 +
   1.379 +val comp_dname = space_implode "_" (map Binding.name_of dbinds);
   1.380 +val comp_dbind = Binding.name comp_dname;
   1.381 +
   1.382 +(* Test for emptiness *)
   1.383 +(* FIXME: reimplement emptiness test
   1.384 +local
   1.385 +  open Domain_Library;
   1.386 +  val dnames = map (fst o fst) eqs;
   1.387 +  val conss = map snd eqs;
   1.388 +  fun rec_to ns lazy_rec (n,cons) = forall (exists (fn arg => 
   1.389 +        is_rec arg andalso not (member (op =) ns (rec_of arg)) andalso
   1.390 +        ((rec_of arg =  n andalso not (lazy_rec orelse is_lazy arg)) orelse 
   1.391 +          rec_of arg <> n andalso rec_to (rec_of arg::ns) 
   1.392 +            (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
   1.393 +        ) o snd) cons;
   1.394 +  fun warn (n,cons) =
   1.395 +    if rec_to [] false (n,cons)
   1.396 +    then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
   1.397 +    else false;
   1.398 +in
   1.399 +  val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
   1.400 +  val is_emptys = map warn n__eqs;
   1.401 +end;
   1.402 +*)
   1.403 +
   1.404 +(* Test for indirect recursion *)
   1.405 +local
   1.406 +  val newTs = map (#absT o #iso_info) constr_infos;
   1.407 +  fun indirect_typ (Type (_, Ts)) =
   1.408 +      exists (fn T => member (op =) newTs T orelse indirect_typ T) Ts
   1.409 +    | indirect_typ _ = false;
   1.410 +  fun indirect_arg (_, T) = indirect_typ T;
   1.411 +  fun indirect_con (_, args) = exists indirect_arg args;
   1.412 +  fun indirect_eq cons = exists indirect_con cons;
   1.413 +in
   1.414 +  val is_indirect = exists indirect_eq (map #con_specs constr_infos);
   1.415 +  val _ =
   1.416 +      if is_indirect
   1.417 +      then message "Indirect recursion detected, skipping proofs of (co)induction rules"
   1.418 +      else message ("Proving induction properties of domain "^comp_dname^" ...");
   1.419 +end;
   1.420 +
   1.421 +(* theorems about take *)
   1.422 +
   1.423 +val (take_rewss, thy) =
   1.424 +    take_theorems dbinds take_info constr_infos thy;
   1.425 +
   1.426 +val {take_lemma_thms, take_0_thms, take_strict_thms, ...} = take_info;
   1.427 +
   1.428 +val take_rews = take_0_thms @ take_strict_thms @ flat take_rewss;
   1.429 +
   1.430 +(* prove induction rules, unless definition is indirect recursive *)
   1.431 +val thy =
   1.432 +    if is_indirect then thy else
   1.433 +    prove_induction comp_dbind constr_infos take_info take_rews thy;
   1.434 +
   1.435 +val thy =
   1.436 +    if is_indirect then thy else
   1.437 +    prove_coinduction (comp_dbind, dbinds) constr_infos take_info take_rewss thy;
   1.438 +
   1.439 +in
   1.440 +  (take_rews, thy)
   1.441 +end; (* let *)
   1.442 +end; (* struct *)