# Theory Set

Up to index of Isabelle/HOL

theory Set
imports Lattices
(*  Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel *)header {* Set theory for higher-order logic *}theory Setimports Latticesbeginsubsection {* Sets as predicates *}typedecl 'a setaxiomatization Collect :: "('a => bool) => 'a set" -- "comprehension"  and member :: "'a => 'a set => bool" -- "membership"where  mem_Collect_eq [iff, code_unfold]: "member a (Collect P) = P a"  and Collect_mem_eq [simp]: "Collect (λx. member x A) = A"notation  member  ("op :") and  member  ("(_/ : _)" [51, 51] 50)abbreviation not_member where  "not_member x A ≡ ~ (x : A)" -- "non-membership"notation  not_member  ("op ~:") and  not_member  ("(_/ ~: _)" [51, 51] 50)notation (xsymbols)  member      ("op ∈") and  member      ("(_/ ∈ _)" [51, 51] 50) and  not_member  ("op ∉") and  not_member  ("(_/ ∉ _)" [51, 51] 50)notation (HTML output)  member      ("op ∈") and  member      ("(_/ ∈ _)" [51, 51] 50) and  not_member  ("op ∉") and  not_member  ("(_/ ∉ _)" [51, 51] 50)text {* Set comprehensions *}syntax  "_Coll" :: "pttrn => bool => 'a set"    ("(1{_./ _})")translations  "{x. P}" == "CONST Collect (%x. P)"syntax  "_Collect" :: "idt => 'a set => bool => 'a set"    ("(1{_ :/ _./ _})")syntax (xsymbols)  "_Collect" :: "idt => 'a set => bool => 'a set"    ("(1{_ ∈/ _./ _})")translations  "{x:A. P}" => "{x. x:A & P}"lemma CollectI: "P a ==> a ∈ {x. P x}"  by simplemma CollectD: "a ∈ {x. P x} ==> P a"  by simplemma Collect_cong: "(!!x. P x = Q x) ==> {x. P x} = {x. Q x}"  by simptext {*Simproc for pulling @{text "x=t"} in @{text "{x. … & x=t & …}"}to the front (and similarly for @{text "t=x"}):*}simproc_setup defined_Collect ("{x. P x & Q x}") = {*  fn _ =>    Quantifier1.rearrange_Collect     (rtac @{thm Collect_cong} 1 THEN      rtac @{thm iffI} 1 THEN      ALLGOALS        (EVERY' [REPEAT_DETERM o etac @{thm conjE}, DEPTH_SOLVE_1 o ares_tac @{thms conjI}]))*}lemmas CollectE = CollectD [elim_format]lemma set_eqI:  assumes "!!x. x ∈ A <-> x ∈ B"  shows "A = B"proof -  from assms have "{x. x ∈ A} = {x. x ∈ B}" by simp  then show ?thesis by simpqedlemma set_eq_iff [no_atp]:  "A = B <-> (∀x. x ∈ A <-> x ∈ B)"  by (auto intro:set_eqI)text {* Lifting of predicate class instances *}instantiation set :: (type) boolean_algebrabegindefinition less_eq_set where  "A ≤ B <-> (λx. member x A) ≤ (λx. member x B)"definition less_set where  "A < B <-> (λx. member x A) < (λx. member x B)"definition inf_set where  "A \<sqinter> B = Collect ((λx. member x A) \<sqinter> (λx. member x B))"definition sup_set where  "A \<squnion> B = Collect ((λx. member x A) \<squnion> (λx. member x B))"definition bot_set where  "⊥ = Collect ⊥"definition top_set where  "\<top> = Collect \<top>"definition uminus_set where  "- A = Collect (- (λx. member x A))"definition minus_set where  "A - B = Collect ((λx. member x A) - (λx. member x B))"instance proofqed (simp_all add: less_eq_set_def less_set_def inf_set_def sup_set_def  bot_set_def top_set_def uminus_set_def minus_set_def  less_le_not_le inf_compl_bot sup_compl_top sup_inf_distrib1 diff_eq  set_eqI fun_eq_iff  del: inf_apply sup_apply bot_apply top_apply minus_apply uminus_apply)endtext {* Set enumerations *}abbreviation empty :: "'a set" ("{}") where  "{} ≡ bot"definition insert :: "'a => 'a set => 'a set" where  insert_compr: "insert a B = {x. x = a ∨ x ∈ B}"syntax  "_Finset" :: "args => 'a set"    ("{(_)}")translations  "{x, xs}" == "CONST insert x {xs}"  "{x}" == "CONST insert x {}"subsection {* Subsets and bounded quantifiers *}abbreviation  subset :: "'a set => 'a set => bool" where  "subset ≡ less"abbreviation  subset_eq :: "'a set => 'a set => bool" where  "subset_eq ≡ less_eq"notation (output)  subset  ("op <") and  subset  ("(_/ < _)" [51, 51] 50) and  subset_eq  ("op <=") and  subset_eq  ("(_/ <= _)" [51, 51] 50)notation (xsymbols)  subset  ("op ⊂") and  subset  ("(_/ ⊂ _)" [51, 51] 50) and  subset_eq  ("op ⊆") and  subset_eq  ("(_/ ⊆ _)" [51, 51] 50)notation (HTML output)  subset  ("op ⊂") and  subset  ("(_/ ⊂ _)" [51, 51] 50) and  subset_eq  ("op ⊆") and  subset_eq  ("(_/ ⊆ _)" [51, 51] 50)abbreviation (input)  supset :: "'a set => 'a set => bool" where  "supset ≡ greater"abbreviation (input)  supset_eq :: "'a set => 'a set => bool" where  "supset_eq ≡ greater_eq"notation (xsymbols)  supset  ("op ⊃") and  supset  ("(_/ ⊃ _)" [51, 51] 50) and  supset_eq  ("op ⊇") and  supset_eq  ("(_/ ⊇ _)" [51, 51] 50)definition Ball :: "'a set => ('a => bool) => bool" where  "Ball A P <-> (∀x. x ∈ A --> P x)"   -- "bounded universal quantifiers"definition Bex :: "'a set => ('a => bool) => bool" where  "Bex A P <-> (∃x. x ∈ A ∧ P x)"   -- "bounded existential quantifiers"syntax  "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3ALL _:_./ _)" [0, 0, 10] 10)  "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3EX _:_./ _)" [0, 0, 10] 10)  "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3EX! _:_./ _)" [0, 0, 10] 10)  "_Bleast"     :: "id => 'a set => bool => 'a"           ("(3LEAST _:_./ _)" [0, 0, 10] 10)syntax (HOL)  "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3! _:_./ _)" [0, 0, 10] 10)  "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3? _:_./ _)" [0, 0, 10] 10)  "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3?! _:_./ _)" [0, 0, 10] 10)syntax (xsymbols)  "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3∀_∈_./ _)" [0, 0, 10] 10)  "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3∃_∈_./ _)" [0, 0, 10] 10)  "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3∃!_∈_./ _)" [0, 0, 10] 10)  "_Bleast"     :: "id => 'a set => bool => 'a"           ("(3LEAST_∈_./ _)" [0, 0, 10] 10)syntax (HTML output)  "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3∀_∈_./ _)" [0, 0, 10] 10)  "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3∃_∈_./ _)" [0, 0, 10] 10)  "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3∃!_∈_./ _)" [0, 0, 10] 10)translations  "ALL x:A. P" == "CONST Ball A (%x. P)"  "EX x:A. P" == "CONST Bex A (%x. P)"  "EX! x:A. P" => "EX! x. x:A & P"  "LEAST x:A. P" => "LEAST x. x:A & P"syntax (output)  "_setlessAll" :: "[idt, 'a, bool] => bool"  ("(3ALL _<_./ _)"  [0, 0, 10] 10)  "_setlessEx"  :: "[idt, 'a, bool] => bool"  ("(3EX _<_./ _)"  [0, 0, 10] 10)  "_setleAll"   :: "[idt, 'a, bool] => bool"  ("(3ALL _<=_./ _)" [0, 0, 10] 10)  "_setleEx"    :: "[idt, 'a, bool] => bool"  ("(3EX _<=_./ _)" [0, 0, 10] 10)  "_setleEx1"   :: "[idt, 'a, bool] => bool"  ("(3EX! _<=_./ _)" [0, 0, 10] 10)syntax (xsymbols)  "_setlessAll" :: "[idt, 'a, bool] => bool"   ("(3∀_⊂_./ _)"  [0, 0, 10] 10)  "_setlessEx"  :: "[idt, 'a, bool] => bool"   ("(3∃_⊂_./ _)"  [0, 0, 10] 10)  "_setleAll"   :: "[idt, 'a, bool] => bool"   ("(3∀_⊆_./ _)" [0, 0, 10] 10)  "_setleEx"    :: "[idt, 'a, bool] => bool"   ("(3∃_⊆_./ _)" [0, 0, 10] 10)  "_setleEx1"   :: "[idt, 'a, bool] => bool"   ("(3∃!_⊆_./ _)" [0, 0, 10] 10)syntax (HOL output)  "_setlessAll" :: "[idt, 'a, bool] => bool"   ("(3! _<_./ _)"  [0, 0, 10] 10)  "_setlessEx"  :: "[idt, 'a, bool] => bool"   ("(3? _<_./ _)"  [0, 0, 10] 10)  "_setleAll"   :: "[idt, 'a, bool] => bool"   ("(3! _<=_./ _)" [0, 0, 10] 10)  "_setleEx"    :: "[idt, 'a, bool] => bool"   ("(3? _<=_./ _)" [0, 0, 10] 10)  "_setleEx1"   :: "[idt, 'a, bool] => bool"   ("(3?! _<=_./ _)" [0, 0, 10] 10)syntax (HTML output)  "_setlessAll" :: "[idt, 'a, bool] => bool"   ("(3∀_⊂_./ _)"  [0, 0, 10] 10)  "_setlessEx"  :: "[idt, 'a, bool] => bool"   ("(3∃_⊂_./ _)"  [0, 0, 10] 10)  "_setleAll"   :: "[idt, 'a, bool] => bool"   ("(3∀_⊆_./ _)" [0, 0, 10] 10)  "_setleEx"    :: "[idt, 'a, bool] => bool"   ("(3∃_⊆_./ _)" [0, 0, 10] 10)  "_setleEx1"   :: "[idt, 'a, bool] => bool"   ("(3∃!_⊆_./ _)" [0, 0, 10] 10)translations "∀A⊂B. P"   =>  "ALL A. A ⊂ B --> P" "∃A⊂B. P"   =>  "EX A. A ⊂ B & P" "∀A⊆B. P"   =>  "ALL A. A ⊆ B --> P" "∃A⊆B. P"   =>  "EX A. A ⊆ B & P" "∃!A⊆B. P"  =>  "EX! A. A ⊆ B & P"print_translation {*let  val All_binder = Mixfix.binder_name @{const_syntax All};  val Ex_binder = Mixfix.binder_name @{const_syntax Ex};  val impl = @{const_syntax HOL.implies};  val conj = @{const_syntax HOL.conj};  val sbset = @{const_syntax subset};  val sbset_eq = @{const_syntax subset_eq};  val trans =   [((All_binder, impl, sbset), @{syntax_const "_setlessAll"}),    ((All_binder, impl, sbset_eq), @{syntax_const "_setleAll"}),    ((Ex_binder, conj, sbset), @{syntax_const "_setlessEx"}),    ((Ex_binder, conj, sbset_eq), @{syntax_const "_setleEx"})];  fun mk v (v', T) c n P =    if v = v' andalso not (Term.exists_subterm (fn Free (x, _) => x = v | _ => false) n)    then Syntax.const c $Syntax_Trans.mark_bound_body (v', T)$ n $P else raise Match; fun tr' q = (q, fn [Const (@{syntax_const "_bound"}, _)$ Free (v, Type (@{type_name set}, _)),            Const (c, _) $(Const (d, _)$ (Const (@{syntax_const "_bound"}, _) $Free (v', T))$ n) $P] => (case AList.lookup (op =) trans (q, c, d) of NONE => raise Match | SOME l => mk v (v', T) l n P) | _ => raise Match);in [tr' All_binder, tr' Ex_binder]end*}text {* \medskip Translate between @{text "{e | x1...xn. P}"} and @{text "{u. EX x1..xn. u = e & P}"}; @{text "{y. EX x1..xn. y = e & P}"} is only translated if @{text "[0..n] subset bvs(e)"}.*}syntax "_Setcompr" :: "'a => idts => bool => 'a set" ("(1{_ |/_./ _})")parse_translation {* let val ex_tr = snd (Syntax_Trans.mk_binder_tr ("EX ", @{const_syntax Ex})); fun nvars (Const (@{syntax_const "_idts"}, _)$ _ $idts) = nvars idts + 1 | nvars _ = 1; fun setcompr_tr [e, idts, b] = let val eq = Syntax.const @{const_syntax HOL.eq}$ Bound (nvars idts) $e; val P = Syntax.const @{const_syntax HOL.conj}$ eq $b; val exP = ex_tr [idts, P]; in Syntax.const @{const_syntax Collect}$ absdummy dummyT exP end;  in [(@{syntax_const "_Setcompr"}, setcompr_tr)] end;*}print_translation {* [Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Ball} @{syntax_const "_Ball"},  Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Bex} @{syntax_const "_Bex"}]*} -- {* to avoid eta-contraction of body *}print_translation {*let  val ex_tr' = snd (Syntax_Trans.mk_binder_tr' (@{const_syntax Ex}, "DUMMY"));  fun setcompr_tr' [Abs (abs as (_, _, P))] =    let      fun check (Const (@{const_syntax Ex}, _) $Abs (_, _, P), n) = check (P, n + 1) | check (Const (@{const_syntax HOL.conj}, _)$              (Const (@{const_syntax HOL.eq}, _) $Bound m$ e) $P, n) = n > 0 andalso m = n andalso not (loose_bvar1 (P, n)) andalso subset (op =) (0 upto (n - 1), add_loose_bnos (e, 0, [])) | check _ = false; fun tr' (_$ abs) =          let val _ $idts$ (_ $(_$ _ $e)$ Q) = ex_tr' [abs]          in Syntax.const @{syntax_const "_Setcompr"} $e$ idts $Q end; in if check (P, 0) then tr' P else let val (x as _$ Free(xN, _), t) = Syntax_Trans.atomic_abs_tr' abs;          val M = Syntax.const @{syntax_const "_Coll"} $x$ t;        in          case t of            Const (@{const_syntax HOL.conj}, _) $(Const (@{const_syntax Set.member}, _)$                (Const (@{syntax_const "_bound"}, _) $Free (yN, _))$ A) $P => if xN = yN then Syntax.const @{syntax_const "_Collect"}$ x $A$ P else M          | _ => M        end    end;  in [(@{const_syntax Collect}, setcompr_tr')] end;*}simproc_setup defined_Bex ("EX x:A. P x & Q x") = {*  let    val unfold_bex_tac = unfold_tac @{thms Bex_def};    fun prove_bex_tac ss = unfold_bex_tac ss THEN Quantifier1.prove_one_point_ex_tac;  in fn _ => fn ss => Quantifier1.rearrange_bex (prove_bex_tac ss) ss end*}simproc_setup defined_All ("ALL x:A. P x --> Q x") = {*  let    val unfold_ball_tac = unfold_tac @{thms Ball_def};    fun prove_ball_tac ss = unfold_ball_tac ss THEN Quantifier1.prove_one_point_all_tac;  in fn _ => fn ss => Quantifier1.rearrange_ball (prove_ball_tac ss) ss end*}lemma ballI [intro!]: "(!!x. x:A ==> P x) ==> ALL x:A. P x"  by (simp add: Ball_def)lemmas strip = impI allI ballIlemma bspec [dest?]: "ALL x:A. P x ==> x:A ==> P x"  by (simp add: Ball_def)text {*  Gives better instantiation for bound:*}declaration {* fn _ =>  Classical.map_cs (fn cs => cs addbefore ("bspec", dtac @{thm bspec} THEN' assume_tac))*}ML {*structure Simpdata =structopen Simpdata;val mksimps_pairs = [(@{const_name Ball}, @{thms bspec})] @ mksimps_pairs;end;open Simpdata;*}declaration {* fn _ =>  Simplifier.map_ss (Simplifier.set_mksimps (mksimps mksimps_pairs))*}lemma ballE [elim]: "ALL x:A. P x ==> (P x ==> Q) ==> (x ~: A ==> Q) ==> Q"  by (unfold Ball_def) blastlemma bexI [intro]: "P x ==> x:A ==> EX x:A. P x"  -- {* Normally the best argument order: @{prop "P x"} constrains the    choice of @{prop "x:A"}. *}  by (unfold Bex_def) blastlemma rev_bexI [intro?]: "x:A ==> P x ==> EX x:A. P x"  -- {* The best argument order when there is only one @{prop "x:A"}. *}  by (unfold Bex_def) blastlemma bexCI: "(ALL x:A. ~P x ==> P a) ==> a:A ==> EX x:A. P x"  by (unfold Bex_def) blastlemma bexE [elim!]: "EX x:A. P x ==> (!!x. x:A ==> P x ==> Q) ==> Q"  by (unfold Bex_def) blastlemma ball_triv [simp]: "(ALL x:A. P) = ((EX x. x:A) --> P)"  -- {* Trival rewrite rule. *}  by (simp add: Ball_def)lemma bex_triv [simp]: "(EX x:A. P) = ((EX x. x:A) & P)"  -- {* Dual form for existentials. *}  by (simp add: Bex_def)lemma bex_triv_one_point1 [simp]: "(EX x:A. x = a) = (a:A)"  by blastlemma bex_triv_one_point2 [simp]: "(EX x:A. a = x) = (a:A)"  by blastlemma bex_one_point1 [simp]: "(EX x:A. x = a & P x) = (a:A & P a)"  by blastlemma bex_one_point2 [simp]: "(EX x:A. a = x & P x) = (a:A & P a)"  by blastlemma ball_one_point1 [simp]: "(ALL x:A. x = a --> P x) = (a:A --> P a)"  by blastlemma ball_one_point2 [simp]: "(ALL x:A. a = x --> P x) = (a:A --> P a)"  by blastlemma ball_conj_distrib:  "(∀x∈A. P x ∧ Q x) <-> ((∀x∈A. P x) ∧ (∀x∈A. Q x))"  by blastlemma bex_disj_distrib:  "(∃x∈A. P x ∨ Q x) <-> ((∃x∈A. P x) ∨ (∃x∈A. Q x))"  by blasttext {* Congruence rules *}lemma ball_cong:  "A = B ==> (!!x. x:B ==> P x = Q x) ==>    (ALL x:A. P x) = (ALL x:B. Q x)"  by (simp add: Ball_def)lemma strong_ball_cong [cong]:  "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>    (ALL x:A. P x) = (ALL x:B. Q x)"  by (simp add: simp_implies_def Ball_def)lemma bex_cong:  "A = B ==> (!!x. x:B ==> P x = Q x) ==>    (EX x:A. P x) = (EX x:B. Q x)"  by (simp add: Bex_def cong: conj_cong)lemma strong_bex_cong [cong]:  "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>    (EX x:A. P x) = (EX x:B. Q x)"  by (simp add: simp_implies_def Bex_def cong: conj_cong)subsection {* Basic operations *}subsubsection {* Subsets *}lemma subsetI [intro!]: "(!!x. x ∈ A ==> x ∈ B) ==> A ⊆ B"  by (simp add: less_eq_set_def le_fun_def)text {*  \medskip Map the type @{text "'a set => anything"} to just @{typ  'a}; for overloading constants whose first argument has type @{typ  "'a set"}.*}lemma subsetD [elim, intro?]: "A ⊆ B ==> c ∈ A ==> c ∈ B"  by (simp add: less_eq_set_def le_fun_def)  -- {* Rule in Modus Ponens style. *}lemma rev_subsetD [no_atp,intro?]: "c ∈ A ==> A ⊆ B ==> c ∈ B"  -- {* The same, with reversed premises for use with @{text erule} --      cf @{text rev_mp}. *}  by (rule subsetD)text {*  \medskip Converts @{prop "A ⊆ B"} to @{prop "x ∈ A ==> x ∈ B"}.*}lemma subsetCE [no_atp,elim]: "A ⊆ B ==> (c ∉ A ==> P) ==> (c ∈ B ==> P) ==> P"  -- {* Classical elimination rule. *}  by (auto simp add: less_eq_set_def le_fun_def)lemma subset_eq [no_atp]: "A ≤ B = (∀x∈A. x ∈ B)" by blastlemma contra_subsetD [no_atp]: "A ⊆ B ==> c ∉ B ==> c ∉ A"  by blastlemma subset_refl: "A ⊆ A"  by (fact order_refl) (* already [iff] *)lemma subset_trans: "A ⊆ B ==> B ⊆ C ==> A ⊆ C"  by (fact order_trans)lemma set_rev_mp: "x:A ==> A ⊆ B ==> x:B"  by (rule subsetD)lemma set_mp: "A ⊆ B ==> x:A ==> x:B"  by (rule subsetD)lemma subset_not_subset_eq [code]:  "A ⊂ B <-> A ⊆ B ∧ ¬ B ⊆ A"  by (fact less_le_not_le)lemma eq_mem_trans: "a=b ==> b ∈ A ==> a ∈ A"  by simplemmas basic_trans_rules [trans] =  order_trans_rules set_rev_mp set_mp eq_mem_transsubsubsection {* Equality *}lemma subset_antisym [intro!]: "A ⊆ B ==> B ⊆ A ==> A = B"  -- {* Anti-symmetry of the subset relation. *}  by (iprover intro: set_eqI subsetD)text {*  \medskip Equality rules from ZF set theory -- are they appropriate  here?*}lemma equalityD1: "A = B ==> A ⊆ B"  by simplemma equalityD2: "A = B ==> B ⊆ A"  by simptext {*  \medskip Be careful when adding this to the claset as @{text  subset_empty} is in the simpset: @{prop "A = {}"} goes to @{prop "{}  ⊆ A"} and @{prop "A ⊆ {}"} and then back to @{prop "A = {}"}!*}lemma equalityE: "A = B ==> (A ⊆ B ==> B ⊆ A ==> P) ==> P"  by simplemma equalityCE [elim]:    "A = B ==> (c ∈ A ==> c ∈ B ==> P) ==> (c ∉ A ==> c ∉ B ==> P) ==> P"  by blastlemma eqset_imp_iff: "A = B ==> (x : A) = (x : B)"  by simplemma eqelem_imp_iff: "x = y ==> (x : A) = (y : A)"  by simpsubsubsection {* The empty set *}lemma empty_def:  "{} = {x. False}"  by (simp add: bot_set_def bot_fun_def)lemma empty_iff [simp]: "(c : {}) = False"  by (simp add: empty_def)lemma emptyE [elim!]: "a : {} ==> P"  by simplemma empty_subsetI [iff]: "{} ⊆ A"    -- {* One effect is to delete the ASSUMPTION @{prop "{} <= A"} *}  by blastlemma equals0I: "(!!y. y ∈ A ==> False) ==> A = {}"  by blastlemma equals0D: "A = {} ==> a ∉ A"    -- {* Use for reasoning about disjointness: @{text "A Int B = {}"} *}  by blastlemma ball_empty [simp]: "Ball {} P = True"  by (simp add: Ball_def)lemma bex_empty [simp]: "Bex {} P = False"  by (simp add: Bex_def)subsubsection {* The universal set -- UNIV *}abbreviation UNIV :: "'a set" where  "UNIV ≡ top"lemma UNIV_def:  "UNIV = {x. True}"  by (simp add: top_set_def top_fun_def)lemma UNIV_I [simp]: "x : UNIV"  by (simp add: UNIV_def)declare UNIV_I [intro]  -- {* unsafe makes it less likely to cause problems *}lemma UNIV_witness [intro?]: "EX x. x : UNIV"  by simplemma subset_UNIV: "A ⊆ UNIV"  by (fact top_greatest) (* already simp *)text {*  \medskip Eta-contracting these two rules (to remove @{text P})  causes them to be ignored because of their interaction with  congruence rules.*}lemma ball_UNIV [simp]: "Ball UNIV P = All P"  by (simp add: Ball_def)lemma bex_UNIV [simp]: "Bex UNIV P = Ex P"  by (simp add: Bex_def)lemma UNIV_eq_I: "(!!x. x ∈ A) ==> UNIV = A"  by autolemma UNIV_not_empty [iff]: "UNIV ~= {}"  by (blast elim: equalityE)subsubsection {* The Powerset operator -- Pow *}definition Pow :: "'a set => 'a set set" where  Pow_def: "Pow A = {B. B ≤ A}"lemma Pow_iff [iff]: "(A ∈ Pow B) = (A ⊆ B)"  by (simp add: Pow_def)lemma PowI: "A ⊆ B ==> A ∈ Pow B"  by (simp add: Pow_def)lemma PowD: "A ∈ Pow B ==> A ⊆ B"  by (simp add: Pow_def)lemma Pow_bottom: "{} ∈ Pow B"  by simplemma Pow_top: "A ∈ Pow A"  by simplemma Pow_not_empty: "Pow A ≠ {}"  using Pow_top by blastsubsubsection {* Set complement *}lemma Compl_iff [simp]: "(c ∈ -A) = (c ∉ A)"  by (simp add: fun_Compl_def uminus_set_def)lemma ComplI [intro!]: "(c ∈ A ==> False) ==> c ∈ -A"  by (simp add: fun_Compl_def uminus_set_def) blasttext {*  \medskip This form, with negated conclusion, works well with the  Classical prover.  Negated assumptions behave like formulae on the  right side of the notional turnstile ... *}lemma ComplD [dest!]: "c : -A ==> c~:A"  by simplemmas ComplE = ComplD [elim_format]lemma Compl_eq: "- A = {x. ~ x : A}"  by blastsubsubsection {* Binary intersection *}abbreviation inter :: "'a set => 'a set => 'a set" (infixl "Int" 70) where  "op Int ≡ inf"notation (xsymbols)  inter  (infixl "∩" 70)notation (HTML output)  inter  (infixl "∩" 70)lemma Int_def:  "A ∩ B = {x. x ∈ A ∧ x ∈ B}"  by (simp add: inf_set_def inf_fun_def)lemma Int_iff [simp]: "(c : A Int B) = (c:A & c:B)"  by (unfold Int_def) blastlemma IntI [intro!]: "c:A ==> c:B ==> c : A Int B"  by simplemma IntD1: "c : A Int B ==> c:A"  by simplemma IntD2: "c : A Int B ==> c:B"  by simplemma IntE [elim!]: "c : A Int B ==> (c:A ==> c:B ==> P) ==> P"  by simplemma mono_Int: "mono f ==> f (A ∩ B) ⊆ f A ∩ f B"  by (fact mono_inf)subsubsection {* Binary union *}abbreviation union :: "'a set => 'a set => 'a set" (infixl "Un" 65) where  "union ≡ sup"notation (xsymbols)  union  (infixl "∪" 65)notation (HTML output)  union  (infixl "∪" 65)lemma Un_def:  "A ∪ B = {x. x ∈ A ∨ x ∈ B}"  by (simp add: sup_set_def sup_fun_def)lemma Un_iff [simp]: "(c : A Un B) = (c:A | c:B)"  by (unfold Un_def) blastlemma UnI1 [elim?]: "c:A ==> c : A Un B"  by simplemma UnI2 [elim?]: "c:B ==> c : A Un B"  by simptext {*  \medskip Classical introduction rule: no commitment to @{prop A} vs  @{prop B}.*}lemma UnCI [intro!]: "(c~:B ==> c:A) ==> c : A Un B"  by autolemma UnE [elim!]: "c : A Un B ==> (c:A ==> P) ==> (c:B ==> P) ==> P"  by (unfold Un_def) blastlemma insert_def: "insert a B = {x. x = a} ∪ B"  by (simp add: insert_compr Un_def)lemma mono_Un: "mono f ==> f A ∪ f B ⊆ f (A ∪ B)"  by (fact mono_sup)subsubsection {* Set difference *}lemma Diff_iff [simp]: "(c : A - B) = (c:A & c~:B)"  by (simp add: minus_set_def fun_diff_def)lemma DiffI [intro!]: "c : A ==> c ~: B ==> c : A - B"  by simplemma DiffD1: "c : A - B ==> c : A"  by simplemma DiffD2: "c : A - B ==> c : B ==> P"  by simplemma DiffE [elim!]: "c : A - B ==> (c:A ==> c~:B ==> P) ==> P"  by simplemma set_diff_eq: "A - B = {x. x : A & ~ x : B}" by blastlemma Compl_eq_Diff_UNIV: "-A = (UNIV - A)"by blastsubsubsection {* Augmenting a set -- @{const insert} *}lemma insert_iff [simp]: "(a : insert b A) = (a = b | a:A)"  by (unfold insert_def) blastlemma insertI1: "a : insert a B"  by simplemma insertI2: "a : B ==> a : insert b B"  by simplemma insertE [elim!]: "a : insert b A ==> (a = b ==> P) ==> (a:A ==> P) ==> P"  by (unfold insert_def) blastlemma insertCI [intro!]: "(a~:B ==> a = b) ==> a: insert b B"  -- {* Classical introduction rule. *}  by autolemma subset_insert_iff: "(A ⊆ insert x B) = (if x:A then A - {x} ⊆ B else A ⊆ B)"  by autolemma set_insert:  assumes "x ∈ A"  obtains B where "A = insert x B" and "x ∉ B"proof  from assms show "A = insert x (A - {x})" by blastnext  show "x ∉ A - {x}" by blastqedlemma insert_ident: "x ~: A ==> x ~: B ==> (insert x A = insert x B) = (A = B)"by autolemma insert_eq_iff: assumes "a ∉ A" "b ∉ B"shows "insert a A = insert b B <->  (if a=b then A=B else ∃C. A = insert b C ∧ b ∉ C ∧ B = insert a C ∧ a ∉ C)"  (is "?L <-> ?R")proof  assume ?L  show ?R  proof cases    assume "a=b" with assms ?L show ?R by (simp add: insert_ident)  next    assume "a≠b"    let ?C = "A - {b}"    have "A = insert b ?C ∧ b ∉ ?C ∧ B = insert a ?C ∧ a ∉ ?C"      using assms ?L a≠b by auto    thus ?R using a≠b by auto  qednext  assume ?R thus ?L by (auto split: if_splits)qedsubsubsection {* Singletons, using insert *}lemma singletonI [intro!,no_atp]: "a : {a}"    -- {* Redundant? But unlike @{text insertCI}, it proves the subgoal immediately! *}  by (rule insertI1)lemma singletonD [dest!,no_atp]: "b : {a} ==> b = a"  by blastlemmas singletonE = singletonD [elim_format]lemma singleton_iff: "(b : {a}) = (b = a)"  by blastlemma singleton_inject [dest!]: "{a} = {b} ==> a = b"  by blastlemma singleton_insert_inj_eq [iff,no_atp]:     "({b} = insert a A) = (a = b & A ⊆ {b})"  by blastlemma singleton_insert_inj_eq' [iff,no_atp]:     "(insert a A = {b}) = (a = b & A ⊆ {b})"  by blastlemma subset_singletonD: "A ⊆ {x} ==> A = {} | A = {x}"  by fastlemma singleton_conv [simp]: "{x. x = a} = {a}"  by blastlemma singleton_conv2 [simp]: "{x. a = x} = {a}"  by blastlemma diff_single_insert: "A - {x} ⊆ B ==> A ⊆ insert x B"  by blastlemma doubleton_eq_iff: "({a,b} = {c,d}) = (a=c & b=d | a=d & b=c)"  by (blast elim: equalityE)subsubsection {* Image of a set under a function *}text {*  Frequently @{term b} does not have the syntactic form of @{term "f x"}.*}definition image :: "('a => 'b) => 'a set => 'b set" (infixr "" 90) where  image_def [no_atp]: "f  A = {y. EX x:A. y = f(x)}"abbreviation  range :: "('a => 'b) => 'b set" where -- "of function"  "range f == f  UNIV"lemma image_eqI [simp, intro]: "b = f x ==> x:A ==> b : fA"  by (unfold image_def) blastlemma imageI: "x : A ==> f x : f  A"  by (rule image_eqI) (rule refl)lemma rev_image_eqI: "x:A ==> b = f x ==> b : fA"  -- {* This version's more effective when we already have the    required @{term x}. *}  by (unfold image_def) blastlemma imageE [elim!]:  "b : (%x. f x)A ==> (!!x. b = f x ==> x:A ==> P) ==> P"  -- {* The eta-expansion gives variable-name preservation. *}  by (unfold image_def) blastlemma image_Un: "f(A Un B) = fA Un fB"  by blastlemma image_iff: "(z : fA) = (EX x:A. z = f x)"  by blastlemma image_subset_iff [no_atp]: "(fA ⊆ B) = (∀x∈A. f x ∈ B)"  -- {* This rewrite rule would confuse users if made default. *}  by blastlemma subset_image_iff: "(B ⊆ fA) = (EX AA. AA ⊆ A & B = fAA)"  apply safe   prefer 2 apply fast  apply (rule_tac x = "{a. a : A & f a : B}" in exI, fast)  donelemma image_subsetI: "(!!x. x ∈ A ==> f x ∈ B) ==> fA ⊆ B"  -- {* Replaces the three steps @{text subsetI}, @{text imageE},    @{text hypsubst}, but breaks too many existing proofs. *}  by blasttext {*  \medskip Range of a function -- just a translation for image!*}lemma image_ident [simp]: "(%x. x)  Y = Y"  by blastlemma range_eqI: "b = f x ==> b ∈ range f"  by simplemma rangeI: "f x ∈ range f"  by simplemma rangeE [elim?]: "b ∈ range (λx. f x) ==> (!!x. b = f x ==> P) ==> P"  by blastsubsubsection {* Some rules with @{text "if"} *}text{* Elimination of @{text"{x. … & x=t & …}"}. *}lemma Collect_conv_if: "{x. x=a & P x} = (if P a then {a} else {})"  by autolemma Collect_conv_if2: "{x. a=x & P x} = (if P a then {a} else {})"  by autotext {*  Rewrite rules for boolean case-splitting: faster than @{text  "split_if [split]"}.*}lemma split_if_eq1: "((if Q then x else y) = b) = ((Q --> x = b) & (~ Q --> y = b))"  by (rule split_if)lemma split_if_eq2: "(a = (if Q then x else y)) = ((Q --> a = x) & (~ Q --> a = y))"  by (rule split_if)text {*  Split ifs on either side of the membership relation.  Not for @{text  "[simp]"} -- can cause goals to blow up!*}lemma split_if_mem1: "((if Q then x else y) : b) = ((Q --> x : b) & (~ Q --> y : b))"  by (rule split_if)lemma split_if_mem2: "(a : (if Q then x else y)) = ((Q --> a : x) & (~ Q --> a : y))"  by (rule split_if [where P="%S. a : S"])lemmas split_ifs = if_bool_eq_conj split_if_eq1 split_if_eq2 split_if_mem1 split_if_mem2(*Would like to add these, but the existing code only searches for the  outer-level constant, which in this case is just Set.member; we instead need  to use term-nets to associate patterns with rules.  Also, if a rule fails to  apply, then the formula should be kept.  [("uminus", Compl_iff RS iffD1), ("minus", [Diff_iff RS iffD1]),   ("Int", [IntD1,IntD2]),   ("Collect", [CollectD]), ("Inter", [InterD]), ("INTER", [INT_D])] *)subsection {* Further operations and lemmas *}subsubsection {* The proper subset'' relation *}lemma psubsetI [intro!,no_atp]: "A ⊆ B ==> A ≠ B ==> A ⊂ B"  by (unfold less_le) blastlemma psubsetE [elim!,no_atp]:     "[|A ⊂ B;  [|A ⊆ B; ~ (B⊆A)|] ==> R|] ==> R"  by (unfold less_le) blastlemma psubset_insert_iff:  "(A ⊂ insert x B) = (if x ∈ B then A ⊂ B else if x ∈ A then A - {x} ⊂ B else A ⊆ B)"  by (auto simp add: less_le subset_insert_iff)lemma psubset_eq: "(A ⊂ B) = (A ⊆ B & A ≠ B)"  by (simp only: less_le)lemma psubset_imp_subset: "A ⊂ B ==> A ⊆ B"  by (simp add: psubset_eq)lemma psubset_trans: "[| A ⊂ B; B ⊂ C |] ==> A ⊂ C"apply (unfold less_le)apply (auto dest: subset_antisym)donelemma psubsetD: "[| A ⊂ B; c ∈ A |] ==> c ∈ B"apply (unfold less_le)apply (auto dest: subsetD)donelemma psubset_subset_trans: "A ⊂ B ==> B ⊆ C ==> A ⊂ C"  by (auto simp add: psubset_eq)lemma subset_psubset_trans: "A ⊆ B ==> B ⊂ C ==> A ⊂ C"  by (auto simp add: psubset_eq)lemma psubset_imp_ex_mem: "A ⊂ B ==> ∃b. b ∈ (B - A)"  by (unfold less_le) blastlemma atomize_ball:    "(!!x. x ∈ A ==> P x) == Trueprop (∀x∈A. P x)"  by (simp only: Ball_def atomize_all atomize_imp)lemmas [symmetric, rulify] = atomize_ball  and [symmetric, defn] = atomize_balllemma image_Pow_mono:  assumes "f  A ≤ B"  shows "(image f)  (Pow A) ≤ Pow B"using assms by blastlemma image_Pow_surj:  assumes "f  A = B"  shows "(image f)  (Pow A) = Pow B"using assms unfolding Pow_def proof(auto)  fix Y assume *: "Y ≤ f  A"  obtain X where X_def: "X = {x ∈ A. f x ∈ Y}" by blast  have "f  X = Y ∧ X ≤ A" unfolding X_def using * by auto  thus "Y ∈ (image f)  {X. X ≤ A}" by blastqedsubsubsection {* Derived rules involving subsets. *}text {* @{text insert}. *}lemma subset_insertI: "B ⊆ insert a B"  by (rule subsetI) (erule insertI2)lemma subset_insertI2: "A ⊆ B ==> A ⊆ insert b B"  by blastlemma subset_insert: "x ∉ A ==> (A ⊆ insert x B) = (A ⊆ B)"  by blasttext {* \medskip Finite Union -- the least upper bound of two sets. *}lemma Un_upper1: "A ⊆ A ∪ B"  by (fact sup_ge1)lemma Un_upper2: "B ⊆ A ∪ B"  by (fact sup_ge2)lemma Un_least: "A ⊆ C ==> B ⊆ C ==> A ∪ B ⊆ C"  by (fact sup_least)text {* \medskip Finite Intersection -- the greatest lower bound of two sets. *}lemma Int_lower1: "A ∩ B ⊆ A"  by (fact inf_le1)lemma Int_lower2: "A ∩ B ⊆ B"  by (fact inf_le2)lemma Int_greatest: "C ⊆ A ==> C ⊆ B ==> C ⊆ A ∩ B"  by (fact inf_greatest)text {* \medskip Set difference. *}lemma Diff_subset: "A - B ⊆ A"  by blastlemma Diff_subset_conv: "(A - B ⊆ C) = (A ⊆ B ∪ C)"by blastsubsubsection {* Equalities involving union, intersection, inclusion, etc. *}text {* @{text "{}"}. *}lemma Collect_const [simp]: "{s. P} = (if P then UNIV else {})"  -- {* supersedes @{text "Collect_False_empty"} *}  by autolemma subset_empty [simp]: "(A ⊆ {}) = (A = {})"  by (fact bot_unique)lemma not_psubset_empty [iff]: "¬ (A < {})"  by (fact not_less_bot) (* FIXME: already simp *)lemma Collect_empty_eq [simp]: "(Collect P = {}) = (∀x. ¬ P x)"by blastlemma empty_Collect_eq [simp]: "({} = Collect P) = (∀x. ¬ P x)"by blastlemma Collect_neg_eq: "{x. ¬ P x} = - {x. P x}"  by blastlemma Collect_disj_eq: "{x. P x | Q x} = {x. P x} ∪ {x. Q x}"  by blastlemma Collect_imp_eq: "{x. P x --> Q x} = -{x. P x} ∪ {x. Q x}"  by blastlemma Collect_conj_eq: "{x. P x & Q x} = {x. P x} ∩ {x. Q x}"  by blasttext {* \medskip @{text insert}. *}lemma insert_is_Un: "insert a A = {a} Un A"  -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a {}"} *}  by blastlemma insert_not_empty [simp]: "insert a A ≠ {}"  by blastlemmas empty_not_insert = insert_not_empty [symmetric]declare empty_not_insert [simp]lemma insert_absorb: "a ∈ A ==> insert a A = A"  -- {* @{text "[simp]"} causes recursive calls when there are nested inserts *}  -- {* with \emph{quadratic} running time *}  by blastlemma insert_absorb2 [simp]: "insert x (insert x A) = insert x A"  by blastlemma insert_commute: "insert x (insert y A) = insert y (insert x A)"  by blastlemma insert_subset [simp]: "(insert x A ⊆ B) = (x ∈ B & A ⊆ B)"  by blastlemma mk_disjoint_insert: "a ∈ A ==> ∃B. A = insert a B & a ∉ B"  -- {* use new @{text B} rather than @{text "A - {a}"} to avoid infinite unfolding *}  apply (rule_tac x = "A - {a}" in exI, blast)  donelemma insert_Collect: "insert a (Collect P) = {u. u ≠ a --> P u}"  by autolemma insert_inter_insert[simp]: "insert a A ∩ insert a B = insert a (A ∩ B)"  by blastlemma insert_disjoint [simp,no_atp]: "(insert a A ∩ B = {}) = (a ∉ B ∧ A ∩ B = {})" "({} = insert a A ∩ B) = (a ∉ B ∧ {} = A ∩ B)"  by autolemma disjoint_insert [simp,no_atp]: "(B ∩ insert a A = {}) = (a ∉ B ∧ B ∩ A = {})" "({} = A ∩ insert b B) = (b ∉ A ∧ {} = A ∩ B)"  by autotext {* \medskip @{text image}. *}lemma image_empty [simp]: "f{} = {}"  by blastlemma image_insert [simp]: "f  insert a B = insert (f a) (fB)"  by blastlemma image_constant: "x ∈ A ==> (λx. c)  A = {c}"  by autolemma image_constant_conv: "(%x. c)  A = (if A = {} then {} else {c})"by autolemma image_image: "f  (g  A) = (λx. f (g x))  A"by blastlemma insert_image [simp]: "x ∈ A ==> insert (f x) (fA) = fA"by blastlemma image_is_empty [iff]: "(fA = {}) = (A = {})"by blastlemma empty_is_image[iff]: "({} = f  A) = (A = {})"by blastlemma image_Collect [no_atp]: "f  {x. P x} = {f x | x. P x}"  -- {* NOT suitable as a default simprule: the RHS isn't simpler than the LHS,      with its implicit quantifier and conjunction.  Also image enjoys better      equational properties than does the RHS. *}  by blastlemma if_image_distrib [simp]:  "(λx. if P x then f x else g x)  S    = (f  (S ∩ {x. P x})) ∪ (g  (S ∩ {x. ¬ P x}))"  by (auto simp add: image_def)lemma image_cong: "M = N ==> (!!x. x ∈ N ==> f x = g x) ==> fM = gN"  by (simp add: image_def)lemma image_Int_subset: "f(A Int B) <= fA Int fB"by blastlemma image_diff_subset: "fA - fB <= f(A - B)"by blasttext {* \medskip @{text range}. *}lemma full_SetCompr_eq [no_atp]: "{u. ∃x. u = f x} = range f"  by autolemma range_composition: "range (λx. f (g x)) = frange g"by (subst image_image, simp)text {* \medskip @{text Int} *}lemma Int_absorb: "A ∩ A = A"  by (fact inf_idem) (* already simp *)lemma Int_left_absorb: "A ∩ (A ∩ B) = A ∩ B"  by (fact inf_left_idem)lemma Int_commute: "A ∩ B = B ∩ A"  by (fact inf_commute)lemma Int_left_commute: "A ∩ (B ∩ C) = B ∩ (A ∩ C)"  by (fact inf_left_commute)lemma Int_assoc: "(A ∩ B) ∩ C = A ∩ (B ∩ C)"  by (fact inf_assoc)lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute  -- {* Intersection is an AC-operator *}lemma Int_absorb1: "B ⊆ A ==> A ∩ B = B"  by (fact inf_absorb2)lemma Int_absorb2: "A ⊆ B ==> A ∩ B = A"  by (fact inf_absorb1)lemma Int_empty_left: "{} ∩ B = {}"  by (fact inf_bot_left) (* already simp *)lemma Int_empty_right: "A ∩ {} = {}"  by (fact inf_bot_right) (* already simp *)lemma disjoint_eq_subset_Compl: "(A ∩ B = {}) = (A ⊆ -B)"  by blastlemma disjoint_iff_not_equal: "(A ∩ B = {}) = (∀x∈A. ∀y∈B. x ≠ y)"  by blastlemma Int_UNIV_left: "UNIV ∩ B = B"  by (fact inf_top_left) (* already simp *)lemma Int_UNIV_right: "A ∩ UNIV = A"  by (fact inf_top_right) (* already simp *)lemma Int_Un_distrib: "A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)"  by (fact inf_sup_distrib1)lemma Int_Un_distrib2: "(B ∪ C) ∩ A = (B ∩ A) ∪ (C ∩ A)"  by (fact inf_sup_distrib2)lemma Int_UNIV [simp,no_atp]: "(A ∩ B = UNIV) = (A = UNIV & B = UNIV)"  by (fact inf_eq_top_iff) (* already simp *)lemma Int_subset_iff [no_atp, simp]: "(C ⊆ A ∩ B) = (C ⊆ A & C ⊆ B)"  by (fact le_inf_iff)lemma Int_Collect: "(x ∈ A ∩ {x. P x}) = (x ∈ A & P x)"  by blasttext {* \medskip @{text Un}. *}lemma Un_absorb: "A ∪ A = A"  by (fact sup_idem) (* already simp *)lemma Un_left_absorb: "A ∪ (A ∪ B) = A ∪ B"  by (fact sup_left_idem)lemma Un_commute: "A ∪ B = B ∪ A"  by (fact sup_commute)lemma Un_left_commute: "A ∪ (B ∪ C) = B ∪ (A ∪ C)"  by (fact sup_left_commute)lemma Un_assoc: "(A ∪ B) ∪ C = A ∪ (B ∪ C)"  by (fact sup_assoc)lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute  -- {* Union is an AC-operator *}lemma Un_absorb1: "A ⊆ B ==> A ∪ B = B"  by (fact sup_absorb2)lemma Un_absorb2: "B ⊆ A ==> A ∪ B = A"  by (fact sup_absorb1)lemma Un_empty_left: "{} ∪ B = B"  by (fact sup_bot_left) (* already simp *)lemma Un_empty_right: "A ∪ {} = A"  by (fact sup_bot_right) (* already simp *)lemma Un_UNIV_left: "UNIV ∪ B = UNIV"  by (fact sup_top_left) (* already simp *)lemma Un_UNIV_right: "A ∪ UNIV = UNIV"  by (fact sup_top_right) (* already simp *)lemma Un_insert_left [simp]: "(insert a B) ∪ C = insert a (B ∪ C)"  by blastlemma Un_insert_right [simp]: "A ∪ (insert a B) = insert a (A ∪ B)"  by blastlemma Int_insert_left:    "(insert a B) Int C = (if a ∈ C then insert a (B ∩ C) else B ∩ C)"  by autolemma Int_insert_left_if0[simp]:    "a ∉ C ==> (insert a B) Int C = B ∩ C"  by autolemma Int_insert_left_if1[simp]:    "a ∈ C ==> (insert a B) Int C = insert a (B Int C)"  by autolemma Int_insert_right:    "A ∩ (insert a B) = (if a ∈ A then insert a (A ∩ B) else A ∩ B)"  by autolemma Int_insert_right_if0[simp]:    "a ∉ A ==> A Int (insert a B) = A Int B"  by autolemma Int_insert_right_if1[simp]:    "a ∈ A ==> A Int (insert a B) = insert a (A Int B)"  by autolemma Un_Int_distrib: "A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)"  by (fact sup_inf_distrib1)lemma Un_Int_distrib2: "(B ∩ C) ∪ A = (B ∪ A) ∩ (C ∪ A)"  by (fact sup_inf_distrib2)lemma Un_Int_crazy:    "(A ∩ B) ∪ (B ∩ C) ∪ (C ∩ A) = (A ∪ B) ∩ (B ∪ C) ∩ (C ∪ A)"  by blastlemma subset_Un_eq: "(A ⊆ B) = (A ∪ B = B)"  by (fact le_iff_sup)lemma Un_empty [iff]: "(A ∪ B = {}) = (A = {} & B = {})"  by (fact sup_eq_bot_iff) (* FIXME: already simp *)lemma Un_subset_iff [no_atp, simp]: "(A ∪ B ⊆ C) = (A ⊆ C & B ⊆ C)"  by (fact le_sup_iff)lemma Un_Diff_Int: "(A - B) ∪ (A ∩ B) = A"  by blastlemma Diff_Int2: "A ∩ C - B ∩ C = A ∩ C - B"  by blasttext {* \medskip Set complement *}lemma Compl_disjoint [simp]: "A ∩ -A = {}"  by (fact inf_compl_bot)lemma Compl_disjoint2 [simp]: "-A ∩ A = {}"  by (fact compl_inf_bot)lemma Compl_partition: "A ∪ -A = UNIV"  by (fact sup_compl_top)lemma Compl_partition2: "-A ∪ A = UNIV"  by (fact compl_sup_top)lemma double_complement: "- (-A) = (A::'a set)"  by (fact double_compl) (* already simp *)lemma Compl_Un: "-(A ∪ B) = (-A) ∩ (-B)"  by (fact compl_sup) (* already simp *)lemma Compl_Int: "-(A ∩ B) = (-A) ∪ (-B)"  by (fact compl_inf) (* already simp *)lemma subset_Compl_self_eq: "(A ⊆ -A) = (A = {})"  by blastlemma Un_Int_assoc_eq: "((A ∩ B) ∪ C = A ∩ (B ∪ C)) = (C ⊆ A)"  -- {* Halmos, Naive Set Theory, page 16. *}  by blastlemma Compl_UNIV_eq: "-UNIV = {}"  by (fact compl_top_eq) (* already simp *)lemma Compl_empty_eq: "-{} = UNIV"  by (fact compl_bot_eq) (* already simp *)lemma Compl_subset_Compl_iff [iff]: "(-A ⊆ -B) = (B ⊆ A)"  by (fact compl_le_compl_iff) (* FIXME: already simp *)lemma Compl_eq_Compl_iff [iff]: "(-A = -B) = (A = (B::'a set))"  by (fact compl_eq_compl_iff) (* FIXME: already simp *)lemma Compl_insert: "- insert x A = (-A) - {x}"  by blasttext {* \medskip Bounded quantifiers.  The following are not added to the default simpset because  (a) they duplicate the body and (b) there are no similar rules for @{text Int}. *}lemma ball_Un: "(∀x ∈ A ∪ B. P x) = ((∀x∈A. P x) & (∀x∈B. P x))"  by blastlemma bex_Un: "(∃x ∈ A ∪ B. P x) = ((∃x∈A. P x) | (∃x∈B. P x))"  by blasttext {* \medskip Set difference. *}lemma Diff_eq: "A - B = A ∩ (-B)"  by blastlemma Diff_eq_empty_iff [simp,no_atp]: "(A - B = {}) = (A ⊆ B)"  by blastlemma Diff_cancel [simp]: "A - A = {}"  by blastlemma Diff_idemp [simp]: "(A - B) - B = A - (B::'a set)"by blastlemma Diff_triv: "A ∩ B = {} ==> A - B = A"  by (blast elim: equalityE)lemma empty_Diff [simp]: "{} - A = {}"  by blastlemma Diff_empty [simp]: "A - {} = A"  by blastlemma Diff_UNIV [simp]: "A - UNIV = {}"  by blastlemma Diff_insert0 [simp,no_atp]: "x ∉ A ==> A - insert x B = A - B"  by blastlemma Diff_insert: "A - insert a B = A - B - {a}"  -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}  by blastlemma Diff_insert2: "A - insert a B = A - {a} - B"  -- {* NOT SUITABLE FOR REWRITING since @{text "{a} == insert a 0"} *}  by blastlemma insert_Diff_if: "insert x A - B = (if x ∈ B then A - B else insert x (A - B))"  by autolemma insert_Diff1 [simp]: "x ∈ B ==> insert x A - B = A - B"  by blastlemma insert_Diff_single[simp]: "insert a (A - {a}) = insert a A"by blastlemma insert_Diff: "a ∈ A ==> insert a (A - {a}) = A"  by blastlemma Diff_insert_absorb: "x ∉ A ==> (insert x A) - {x} = A"  by autolemma Diff_disjoint [simp]: "A ∩ (B - A) = {}"  by blastlemma Diff_partition: "A ⊆ B ==> A ∪ (B - A) = B"  by blastlemma double_diff: "A ⊆ B ==> B ⊆ C ==> B - (C - A) = A"  by blastlemma Un_Diff_cancel [simp]: "A ∪ (B - A) = A ∪ B"  by blastlemma Un_Diff_cancel2 [simp]: "(B - A) ∪ A = B ∪ A"  by blastlemma Diff_Un: "A - (B ∪ C) = (A - B) ∩ (A - C)"  by blastlemma Diff_Int: "A - (B ∩ C) = (A - B) ∪ (A - C)"  by blastlemma Un_Diff: "(A ∪ B) - C = (A - C) ∪ (B - C)"  by blastlemma Int_Diff: "(A ∩ B) - C = A ∩ (B - C)"  by blastlemma Diff_Int_distrib: "C ∩ (A - B) = (C ∩ A) - (C ∩ B)"  by blastlemma Diff_Int_distrib2: "(A - B) ∩ C = (A ∩ C) - (B ∩ C)"  by blastlemma Diff_Compl [simp]: "A - (- B) = A ∩ B"  by autolemma Compl_Diff_eq [simp]: "- (A - B) = -A ∪ B"  by blasttext {* \medskip Quantification over type @{typ bool}. *}lemma bool_induct: "P True ==> P False ==> P x"  by (cases x) autolemma all_bool_eq: "(∀b. P b) <-> P True ∧ P False"  by (auto intro: bool_induct)lemma bool_contrapos: "P x ==> ¬ P False ==> P True"  by (cases x) autolemma ex_bool_eq: "(∃b. P b) <-> P True ∨ P False"  by (auto intro: bool_contrapos)lemma UNIV_bool [no_atp]: "UNIV = {False, True}"  by (auto intro: bool_induct)text {* \medskip @{text Pow} *}lemma Pow_empty [simp]: "Pow {} = {{}}"  by (auto simp add: Pow_def)lemma Pow_insert: "Pow (insert a A) = Pow A ∪ (insert a  Pow A)"  by (blast intro: image_eqI [where ?x = "u - {a}", standard])lemma Pow_Compl: "Pow (- A) = {-B | B. A ∈ Pow B}"  by (blast intro: exI [where ?x = "- u", standard])lemma Pow_UNIV [simp]: "Pow UNIV = UNIV"  by blastlemma Un_Pow_subset: "Pow A ∪ Pow B ⊆ Pow (A ∪ B)"  by blastlemma Pow_Int_eq [simp]: "Pow (A ∩ B) = Pow A ∩ Pow B"  by blasttext {* \medskip Miscellany. *}lemma set_eq_subset: "(A = B) = (A ⊆ B & B ⊆ A)"  by blastlemma subset_iff [no_atp]: "(A ⊆ B) = (∀t. t ∈ A --> t ∈ B)"  by blastlemma subset_iff_psubset_eq: "(A ⊆ B) = ((A ⊂ B) | (A = B))"  by (unfold less_le) blastlemma all_not_in_conv [simp]: "(∀x. x ∉ A) = (A = {})"  by blastlemma ex_in_conv: "(∃x. x ∈ A) = (A ≠ {})"  by blastlemma ball_simps [simp, no_atp]:  "!!A P Q. (∀x∈A. P x ∨ Q) <-> ((∀x∈A. P x) ∨ Q)"  "!!A P Q. (∀x∈A. P ∨ Q x) <-> (P ∨ (∀x∈A. Q x))"  "!!A P Q. (∀x∈A. P --> Q x) <-> (P --> (∀x∈A. Q x))"  "!!A P Q. (∀x∈A. P x --> Q) <-> ((∃x∈A. P x) --> Q)"  "!!P. (∀x∈{}. P x) <-> True"  "!!P. (∀x∈UNIV. P x) <-> (∀x. P x)"  "!!a B P. (∀x∈insert a B. P x) <-> (P a ∧ (∀x∈B. P x))"  "!!P Q. (∀x∈Collect Q. P x) <-> (∀x. Q x --> P x)"  "!!A P f. (∀x∈fA. P x) <-> (∀x∈A. P (f x))"  "!!A P. (¬ (∀x∈A. P x)) <-> (∃x∈A. ¬ P x)"  by autolemma bex_simps [simp, no_atp]:  "!!A P Q. (∃x∈A. P x ∧ Q) <-> ((∃x∈A. P x) ∧ Q)"  "!!A P Q. (∃x∈A. P ∧ Q x) <-> (P ∧ (∃x∈A. Q x))"  "!!P. (∃x∈{}. P x) <-> False"  "!!P. (∃x∈UNIV. P x) <-> (∃x. P x)"  "!!a B P. (∃x∈insert a B. P x) <-> (P a | (∃x∈B. P x))"  "!!P Q. (∃x∈Collect Q. P x) <-> (∃x. Q x ∧ P x)"  "!!A P f. (∃x∈fA. P x) <-> (∃x∈A. P (f x))"  "!!A P. (¬(∃x∈A. P x)) <-> (∀x∈A. ¬ P x)"  by autosubsubsection {* Monotonicity of various operations *}lemma image_mono: "A ⊆ B ==> fA ⊆ fB"  by blastlemma Pow_mono: "A ⊆ B ==> Pow A ⊆ Pow B"  by blastlemma insert_mono: "C ⊆ D ==> insert a C ⊆ insert a D"  by blastlemma Un_mono: "A ⊆ C ==> B ⊆ D ==> A ∪ B ⊆ C ∪ D"  by (fact sup_mono)lemma Int_mono: "A ⊆ C ==> B ⊆ D ==> A ∩ B ⊆ C ∩ D"  by (fact inf_mono)lemma Diff_mono: "A ⊆ C ==> D ⊆ B ==> A - B ⊆ C - D"  by blastlemma Compl_anti_mono: "A ⊆ B ==> -B ⊆ -A"  by (fact compl_mono)text {* \medskip Monotonicity of implications. *}lemma in_mono: "A ⊆ B ==> x ∈ A --> x ∈ B"  apply (rule impI)  apply (erule subsetD, assumption)  donelemma conj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 & P2) --> (Q1 & Q2)"  by iproverlemma disj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 | P2) --> (Q1 | Q2)"  by iproverlemma imp_mono: "Q1 --> P1 ==> P2 --> Q2 ==> (P1 --> P2) --> (Q1 --> Q2)"  by iproverlemma imp_refl: "P --> P" ..lemma not_mono: "Q --> P ==> ~ P --> ~ Q"  by iproverlemma ex_mono: "(!!x. P x --> Q x) ==> (EX x. P x) --> (EX x. Q x)"  by iproverlemma all_mono: "(!!x. P x --> Q x) ==> (ALL x. P x) --> (ALL x. Q x)"  by iproverlemma Collect_mono: "(!!x. P x --> Q x) ==> Collect P ⊆ Collect Q"  by blastlemma Int_Collect_mono:    "A ⊆ B ==> (!!x. x ∈ A ==> P x --> Q x) ==> A ∩ Collect P ⊆ B ∩ Collect Q"  by blastlemmas basic_monos =  subset_refl imp_refl disj_mono conj_mono  ex_mono Collect_mono in_monolemma eq_to_mono: "a = b ==> c = d ==> b --> d ==> a --> c"  by iproversubsubsection {* Inverse image of a function *}definition vimage :: "('a => 'b) => 'b set => 'a set" (infixr "-" 90) where  "f - B == {x. f x : B}"lemma vimage_eq [simp]: "(a : f - B) = (f a : B)"  by (unfold vimage_def) blastlemma vimage_singleton_eq: "(a : f - {b}) = (f a = b)"  by simplemma vimageI [intro]: "f a = b ==> b:B ==> a : f - B"  by (unfold vimage_def) blastlemma vimageI2: "f a : A ==> a : f - A"  by (unfold vimage_def) fastlemma vimageE [elim!]: "a: f - B ==> (!!x. f a = x ==> x:B ==> P) ==> P"  by (unfold vimage_def) blastlemma vimageD: "a : f - A ==> f a : A"  by (unfold vimage_def) fastlemma vimage_empty [simp]: "f - {} = {}"  by blastlemma vimage_Compl: "f - (-A) = -(f - A)"  by blastlemma vimage_Un [simp]: "f - (A Un B) = (f - A) Un (f - B)"  by blastlemma vimage_Int [simp]: "f - (A Int B) = (f - A) Int (f - B)"  by fastlemma vimage_Collect_eq [simp]: "f - Collect P = {y. P (f y)}"  by blastlemma vimage_Collect: "(!!x. P (f x) = Q x) ==> f - (Collect P) = Collect Q"  by blastlemma vimage_insert: "f-(insert a B) = (f-{a}) Un (f-B)"  -- {* NOT suitable for rewriting because of the recurrence of @{term "{a}"}. *}  by blastlemma vimage_Diff: "f - (A - B) = (f - A) - (f - B)"  by blastlemma vimage_UNIV [simp]: "f - UNIV = UNIV"  by blastlemma vimage_mono: "A ⊆ B ==> f - A ⊆ f - B"  -- {* monotonicity *}  by blastlemma vimage_image_eq [no_atp]: "f - (f  A) = {y. EX x:A. f x = f y}"by (blast intro: sym)lemma image_vimage_subset: "f  (f - A) <= A"by blastlemma image_vimage_eq [simp]: "f  (f - A) = A Int range f"by blastlemma vimage_const [simp]: "((λx. c) - A) = (if c ∈ A then UNIV else {})"  by autolemma vimage_if [simp]: "((λx. if x ∈ B then c else d) - A) =    (if c ∈ A then (if d ∈ A then UNIV else B)    else if d ∈ A then -B else {})"    by (auto simp add: vimage_def) lemma vimage_inter_cong:  "(!! w. w ∈ S ==> f w = g w) ==> f - y ∩ S = g - y ∩ S"  by autolemma vimage_ident [simp]: "(%x. x) - Y = Y"  by blastsubsubsection {* Getting the Contents of a Singleton Set *}definition the_elem :: "'a set => 'a" where  "the_elem X = (THE x. X = {x})"lemma the_elem_eq [simp]: "the_elem {x} = x"  by (simp add: the_elem_def)subsubsection {* Least value operator *}lemma Least_mono:  "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y    ==> (LEAST y. y : f  S) = f (LEAST x. x : S)"    -- {* Courtesy of Stephan Merz *}  apply clarify  apply (erule_tac P = "%x. x : S" in LeastI2_order, fast)  apply (rule LeastI2_order)  apply (auto elim: monoD intro!: order_antisym)  donesubsubsection {* Monad operation *}definition bind :: "'a set => ('a => 'b set) => 'b set" where  "bind A f = {x. ∃B ∈ fA. x ∈ B}"hide_const (open) bindlemma bind_bind:  fixes A :: "'a set"  shows "Set.bind (Set.bind A B) C = Set.bind A (λx. Set.bind (B x) C)"  by (auto simp add: bind_def)lemma empty_bind [simp]:  "Set.bind {} f = {}"  by (simp add: bind_def)lemma nonempty_bind_const:  "A ≠ {} ==> Set.bind A (λ_. B) = B"  by (auto simp add: bind_def)lemma bind_const: "Set.bind A (λ_. B) = (if A = {} then {} else B)"  by (auto simp add: bind_def)subsubsection {* Operations for execution *}definition is_empty :: "'a set => bool" where  [code_abbrev]: "is_empty A <-> A = {}"hide_const (open) is_emptydefinition remove :: "'a => 'a set => 'a set" where  [code_abbrev]: "remove x A = A - {x}"hide_const (open) removelemma member_remove [simp]:  "x ∈ Set.remove y A <-> x ∈ A ∧ x ≠ y"  by (simp add: remove_def)definition filter :: "('a => bool) => 'a set => 'a set" where  [code_abbrev]: "filter P A = {a ∈ A. P a}"hide_const (open) filterlemma member_filter [simp]:  "x ∈ Set.filter P A <-> x ∈ A ∧ P x"  by (simp add: filter_def)instantiation set :: (equal) equalbegindefinition  "HOL.equal A B <-> A ⊆ B ∧ B ⊆ A"instance proofqed (auto simp add: equal_set_def)endtext {* Misc *}hide_const (open) member not_memberlemmas equalityI = subset_antisymML {*val Ball_def = @{thm Ball_def}val Bex_def = @{thm Bex_def}val CollectD = @{thm CollectD}val CollectE = @{thm CollectE}val CollectI = @{thm CollectI}val Collect_conj_eq = @{thm Collect_conj_eq}val Collect_mem_eq = @{thm Collect_mem_eq}val IntD1 = @{thm IntD1}val IntD2 = @{thm IntD2}val IntE = @{thm IntE}val IntI = @{thm IntI}val Int_Collect = @{thm Int_Collect}val UNIV_I = @{thm UNIV_I}val UNIV_witness = @{thm UNIV_witness}val UnE = @{thm UnE}val UnI1 = @{thm UnI1}val UnI2 = @{thm UnI2}val ballE = @{thm ballE}val ballI = @{thm ballI}val bexCI = @{thm bexCI}val bexE = @{thm bexE}val bexI = @{thm bexI}val bex_triv = @{thm bex_triv}val bspec = @{thm bspec}val contra_subsetD = @{thm contra_subsetD}val equalityCE = @{thm equalityCE}val equalityD1 = @{thm equalityD1}val equalityD2 = @{thm equalityD2}val equalityE = @{thm equalityE}val equalityI = @{thm equalityI}val imageE = @{thm imageE}val imageI = @{thm imageI}val image_Un = @{thm image_Un}val image_insert = @{thm image_insert}val insert_commute = @{thm insert_commute}val insert_iff = @{thm insert_iff}val mem_Collect_eq = @{thm mem_Collect_eq}val rangeE = @{thm rangeE}val rangeI = @{thm rangeI}val range_eqI = @{thm range_eqI}val subsetCE = @{thm subsetCE}val subsetD = @{thm subsetD}val subsetI = @{thm subsetI}val subset_refl = @{thm subset_refl}val subset_trans = @{thm subset_trans}val vimageD = @{thm vimageD}val vimageE = @{thm vimageE}val vimageI = @{thm vimageI}val vimageI2 = @{thm vimageI2}val vimage_Collect = @{thm vimage_Collect}val vimage_Int = @{thm vimage_Int}val vimage_Un = @{thm vimage_Un}*}end