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




