src/HOL/Set.thy
author wenzelm
Mon Dec 07 10:38:04 2015 +0100 (2015-12-07)
changeset 61799 4cf66f21b764
parent 61518 ff12606337e9
child 61955 e96292f32c3c
permissions -rw-r--r--
isabelle update_cartouches -c -t;
haftmann@32139
     1
(*  Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel *)
clasohm@923
     2
wenzelm@60758
     3
section \<open>Set theory for higher-order logic\<close>
wenzelm@11979
     4
nipkow@15131
     5
theory Set
haftmann@30304
     6
imports Lattices
nipkow@15131
     7
begin
wenzelm@11979
     8
wenzelm@60758
     9
subsection \<open>Sets as predicates\<close>
haftmann@30531
    10
haftmann@45959
    11
typedecl 'a set
wenzelm@3820
    12
wenzelm@61799
    13
axiomatization Collect :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set" \<comment> "comprehension"
wenzelm@61799
    14
  and member :: "'a \<Rightarrow> 'a set \<Rightarrow> bool" \<comment> "membership"
haftmann@45959
    15
where
haftmann@45959
    16
  mem_Collect_eq [iff, code_unfold]: "member a (Collect P) = P a"
haftmann@45959
    17
  and Collect_mem_eq [simp]: "Collect (\<lambda>x. member x A) = A"
wenzelm@19656
    18
wenzelm@21210
    19
notation
haftmann@37677
    20
  member  ("op :") and
nipkow@50580
    21
  member  ("(_/ : _)" [51, 51] 50)
wenzelm@11979
    22
haftmann@37677
    23
abbreviation not_member where
wenzelm@61799
    24
  "not_member x A \<equiv> ~ (x : A)" \<comment> "non-membership"
wenzelm@19656
    25
wenzelm@21210
    26
notation
haftmann@37677
    27
  not_member  ("op ~:") and
nipkow@50580
    28
  not_member  ("(_/ ~: _)" [51, 51] 50)
wenzelm@19656
    29
wenzelm@21210
    30
notation (xsymbols)
haftmann@37677
    31
  member      ("op \<in>") and
nipkow@50580
    32
  member      ("(_/ \<in> _)" [51, 51] 50) and
haftmann@37677
    33
  not_member  ("op \<notin>") and
nipkow@50580
    34
  not_member  ("(_/ \<notin> _)" [51, 51] 50)
wenzelm@19656
    35
haftmann@41107
    36
wenzelm@60758
    37
text \<open>Set comprehensions\<close>
haftmann@32081
    38
haftmann@30531
    39
syntax
wenzelm@35115
    40
  "_Coll" :: "pttrn => bool => 'a set"    ("(1{_./ _})")
haftmann@30531
    41
translations
wenzelm@35115
    42
  "{x. P}" == "CONST Collect (%x. P)"
haftmann@30531
    43
haftmann@32081
    44
syntax
nipkow@51392
    45
  "_Collect" :: "pttrn => 'a set => bool => 'a set"    ("(1{_ :/ _./ _})")
haftmann@32081
    46
syntax (xsymbols)
nipkow@51392
    47
  "_Collect" :: "pttrn => 'a set => bool => 'a set"    ("(1{_ \<in>/ _./ _})")
haftmann@32081
    48
translations
nipkow@51392
    49
  "{p:A. P}" => "CONST Collect (%p. p:A & P)"
haftmann@32081
    50
haftmann@41107
    51
lemma CollectI: "P a \<Longrightarrow> a \<in> {x. P x}"
haftmann@32081
    52
  by simp
haftmann@32081
    53
haftmann@41107
    54
lemma CollectD: "a \<in> {x. P x} \<Longrightarrow> P a"
haftmann@32081
    55
  by simp
haftmann@32081
    56
haftmann@41107
    57
lemma Collect_cong: "(\<And>x. P x = Q x) ==> {x. P x} = {x. Q x}"
haftmann@32081
    58
  by simp
haftmann@32081
    59
wenzelm@60758
    60
text \<open>
wenzelm@61799
    61
Simproc for pulling \<open>x=t\<close> in \<open>{x. \<dots> & x=t & \<dots>}\<close>
wenzelm@61799
    62
to the front (and similarly for \<open>t=x\<close>):
wenzelm@60758
    63
\<close>
wenzelm@60758
    64
wenzelm@60758
    65
simproc_setup defined_Collect ("{x. P x & Q x}") = \<open>
wenzelm@54998
    66
  fn _ => Quantifier1.rearrange_Collect
wenzelm@59498
    67
    (fn ctxt =>
wenzelm@59498
    68
      resolve_tac ctxt @{thms Collect_cong} 1 THEN
wenzelm@59498
    69
      resolve_tac ctxt @{thms iffI} 1 THEN
wenzelm@42459
    70
      ALLGOALS
wenzelm@59498
    71
        (EVERY' [REPEAT_DETERM o eresolve_tac ctxt @{thms conjE},
wenzelm@59499
    72
          DEPTH_SOLVE_1 o (assume_tac ctxt ORELSE' resolve_tac ctxt @{thms conjI})]))
wenzelm@60758
    73
\<close>
haftmann@32117
    74
haftmann@32081
    75
lemmas CollectE = CollectD [elim_format]
haftmann@32081
    76
haftmann@41107
    77
lemma set_eqI:
haftmann@41107
    78
  assumes "\<And>x. x \<in> A \<longleftrightarrow> x \<in> B"
haftmann@41107
    79
  shows "A = B"
haftmann@41107
    80
proof -
haftmann@41107
    81
  from assms have "{x. x \<in> A} = {x. x \<in> B}" by simp
haftmann@41107
    82
  then show ?thesis by simp
haftmann@41107
    83
qed
haftmann@41107
    84
blanchet@54147
    85
lemma set_eq_iff:
haftmann@41107
    86
  "A = B \<longleftrightarrow> (\<forall>x. x \<in> A \<longleftrightarrow> x \<in> B)"
haftmann@41107
    87
  by (auto intro:set_eqI)
haftmann@41107
    88
wenzelm@60758
    89
text \<open>Lifting of predicate class instances\<close>
haftmann@45959
    90
haftmann@45959
    91
instantiation set :: (type) boolean_algebra
haftmann@45959
    92
begin
haftmann@45959
    93
haftmann@45959
    94
definition less_eq_set where
haftmann@46853
    95
  "A \<le> B \<longleftrightarrow> (\<lambda>x. member x A) \<le> (\<lambda>x. member x B)"
haftmann@45959
    96
haftmann@45959
    97
definition less_set where
haftmann@46853
    98
  "A < B \<longleftrightarrow> (\<lambda>x. member x A) < (\<lambda>x. member x B)"
haftmann@45959
    99
haftmann@45959
   100
definition inf_set where
haftmann@46853
   101
  "A \<sqinter> B = Collect ((\<lambda>x. member x A) \<sqinter> (\<lambda>x. member x B))"
haftmann@45959
   102
haftmann@45959
   103
definition sup_set where
haftmann@46853
   104
  "A \<squnion> B = Collect ((\<lambda>x. member x A) \<squnion> (\<lambda>x. member x B))"
haftmann@45959
   105
haftmann@45959
   106
definition bot_set where
haftmann@46853
   107
  "\<bottom> = Collect \<bottom>"
haftmann@45959
   108
haftmann@45959
   109
definition top_set where
haftmann@46853
   110
  "\<top> = Collect \<top>"
haftmann@45959
   111
haftmann@45959
   112
definition uminus_set where
haftmann@46853
   113
  "- A = Collect (- (\<lambda>x. member x A))"
haftmann@45959
   114
haftmann@45959
   115
definition minus_set where
haftmann@46853
   116
  "A - B = Collect ((\<lambda>x. member x A) - (\<lambda>x. member x B))"
haftmann@45959
   117
haftmann@45959
   118
instance proof
haftmann@45959
   119
qed (simp_all add: less_eq_set_def less_set_def inf_set_def sup_set_def
haftmann@45959
   120
  bot_set_def top_set_def uminus_set_def minus_set_def
haftmann@45959
   121
  less_le_not_le inf_compl_bot sup_compl_top sup_inf_distrib1 diff_eq
noschinl@46882
   122
  set_eqI fun_eq_iff
noschinl@46882
   123
  del: inf_apply sup_apply bot_apply top_apply minus_apply uminus_apply)
haftmann@45959
   124
haftmann@45959
   125
end
haftmann@45959
   126
wenzelm@60758
   127
text \<open>Set enumerations\<close>
haftmann@30531
   128
haftmann@32264
   129
abbreviation empty :: "'a set" ("{}") where
haftmann@32264
   130
  "{} \<equiv> bot"
haftmann@31456
   131
haftmann@31456
   132
definition insert :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
haftmann@32081
   133
  insert_compr: "insert a B = {x. x = a \<or> x \<in> B}"
haftmann@31456
   134
haftmann@31456
   135
syntax
wenzelm@35115
   136
  "_Finset" :: "args => 'a set"    ("{(_)}")
haftmann@31456
   137
translations
wenzelm@35115
   138
  "{x, xs}" == "CONST insert x {xs}"
wenzelm@35115
   139
  "{x}" == "CONST insert x {}"
haftmann@31456
   140
haftmann@32081
   141
wenzelm@60758
   142
subsection \<open>Subsets and bounded quantifiers\<close>
haftmann@32081
   143
haftmann@32081
   144
abbreviation
haftmann@32081
   145
  subset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
haftmann@32081
   146
  "subset \<equiv> less"
haftmann@32081
   147
haftmann@32081
   148
abbreviation
haftmann@32081
   149
  subset_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
haftmann@32081
   150
  "subset_eq \<equiv> less_eq"
haftmann@32081
   151
haftmann@32081
   152
notation (output)
haftmann@32081
   153
  subset  ("op <") and
nipkow@50580
   154
  subset  ("(_/ < _)" [51, 51] 50) and
haftmann@32081
   155
  subset_eq  ("op <=") and
nipkow@50580
   156
  subset_eq  ("(_/ <= _)" [51, 51] 50)
haftmann@32081
   157
haftmann@32081
   158
notation (xsymbols)
haftmann@32081
   159
  subset  ("op \<subset>") and
nipkow@50580
   160
  subset  ("(_/ \<subset> _)" [51, 51] 50) and
haftmann@32081
   161
  subset_eq  ("op \<subseteq>") and
nipkow@50580
   162
  subset_eq  ("(_/ \<subseteq> _)" [51, 51] 50)
haftmann@32081
   163
haftmann@32081
   164
abbreviation (input)
haftmann@32081
   165
  supset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
haftmann@32081
   166
  "supset \<equiv> greater"
haftmann@32081
   167
haftmann@32081
   168
abbreviation (input)
haftmann@32081
   169
  supset_eq :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
haftmann@32081
   170
  "supset_eq \<equiv> greater_eq"
haftmann@32081
   171
haftmann@32081
   172
notation (xsymbols)
haftmann@32081
   173
  supset  ("op \<supset>") and
nipkow@50580
   174
  supset  ("(_/ \<supset> _)" [51, 51] 50) and
haftmann@32081
   175
  supset_eq  ("op \<supseteq>") and
nipkow@50580
   176
  supset_eq  ("(_/ \<supseteq> _)" [51, 51] 50)
haftmann@32081
   177
haftmann@37387
   178
definition Ball :: "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
wenzelm@61799
   179
  "Ball A P \<longleftrightarrow> (\<forall>x. x \<in> A \<longrightarrow> P x)"   \<comment> "bounded universal quantifiers"
haftmann@32077
   180
haftmann@37387
   181
definition Bex :: "'a set \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
wenzelm@61799
   182
  "Bex A P \<longleftrightarrow> (\<exists>x. x \<in> A \<and> P x)"   \<comment> "bounded existential quantifiers"
haftmann@32077
   183
haftmann@30531
   184
syntax
haftmann@30531
   185
  "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3ALL _:_./ _)" [0, 0, 10] 10)
haftmann@30531
   186
  "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3EX _:_./ _)" [0, 0, 10] 10)
haftmann@30531
   187
  "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3EX! _:_./ _)" [0, 0, 10] 10)
haftmann@30531
   188
  "_Bleast"     :: "id => 'a set => bool => 'a"           ("(3LEAST _:_./ _)" [0, 0, 10] 10)
haftmann@30531
   189
haftmann@30531
   190
syntax (HOL)
haftmann@30531
   191
  "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3! _:_./ _)" [0, 0, 10] 10)
haftmann@30531
   192
  "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3? _:_./ _)" [0, 0, 10] 10)
haftmann@30531
   193
  "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3?! _:_./ _)" [0, 0, 10] 10)
haftmann@30531
   194
haftmann@30531
   195
syntax (xsymbols)
haftmann@30531
   196
  "_Ball"       :: "pttrn => 'a set => bool => bool"      ("(3\<forall>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@30531
   197
  "_Bex"        :: "pttrn => 'a set => bool => bool"      ("(3\<exists>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@30531
   198
  "_Bex1"       :: "pttrn => 'a set => bool => bool"      ("(3\<exists>!_\<in>_./ _)" [0, 0, 10] 10)
haftmann@30531
   199
  "_Bleast"     :: "id => 'a set => bool => 'a"           ("(3LEAST_\<in>_./ _)" [0, 0, 10] 10)
haftmann@30531
   200
haftmann@30531
   201
translations
wenzelm@35115
   202
  "ALL x:A. P" == "CONST Ball A (%x. P)"
wenzelm@35115
   203
  "EX x:A. P" == "CONST Bex A (%x. P)"
wenzelm@35115
   204
  "EX! x:A. P" => "EX! x. x:A & P"
haftmann@30531
   205
  "LEAST x:A. P" => "LEAST x. x:A & P"
haftmann@30531
   206
wenzelm@19656
   207
syntax (output)
nipkow@14804
   208
  "_setlessAll" :: "[idt, 'a, bool] => bool"  ("(3ALL _<_./ _)"  [0, 0, 10] 10)
nipkow@14804
   209
  "_setlessEx"  :: "[idt, 'a, bool] => bool"  ("(3EX _<_./ _)"  [0, 0, 10] 10)
nipkow@14804
   210
  "_setleAll"   :: "[idt, 'a, bool] => bool"  ("(3ALL _<=_./ _)" [0, 0, 10] 10)
nipkow@14804
   211
  "_setleEx"    :: "[idt, 'a, bool] => bool"  ("(3EX _<=_./ _)" [0, 0, 10] 10)
webertj@20217
   212
  "_setleEx1"   :: "[idt, 'a, bool] => bool"  ("(3EX! _<=_./ _)" [0, 0, 10] 10)
nipkow@14804
   213
nipkow@14804
   214
syntax (xsymbols)
nipkow@14804
   215
  "_setlessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<subset>_./ _)"  [0, 0, 10] 10)
nipkow@14804
   216
  "_setlessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<subset>_./ _)"  [0, 0, 10] 10)
nipkow@14804
   217
  "_setleAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<subseteq>_./ _)" [0, 0, 10] 10)
nipkow@14804
   218
  "_setleEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<subseteq>_./ _)" [0, 0, 10] 10)
webertj@20217
   219
  "_setleEx1"   :: "[idt, 'a, bool] => bool"   ("(3\<exists>!_\<subseteq>_./ _)" [0, 0, 10] 10)
nipkow@14804
   220
wenzelm@19656
   221
syntax (HOL output)
nipkow@14804
   222
  "_setlessAll" :: "[idt, 'a, bool] => bool"   ("(3! _<_./ _)"  [0, 0, 10] 10)
nipkow@14804
   223
  "_setlessEx"  :: "[idt, 'a, bool] => bool"   ("(3? _<_./ _)"  [0, 0, 10] 10)
nipkow@14804
   224
  "_setleAll"   :: "[idt, 'a, bool] => bool"   ("(3! _<=_./ _)" [0, 0, 10] 10)
nipkow@14804
   225
  "_setleEx"    :: "[idt, 'a, bool] => bool"   ("(3? _<=_./ _)" [0, 0, 10] 10)
webertj@20217
   226
  "_setleEx1"   :: "[idt, 'a, bool] => bool"   ("(3?! _<=_./ _)" [0, 0, 10] 10)
nipkow@14804
   227
nipkow@14804
   228
translations
haftmann@30531
   229
 "\<forall>A\<subset>B. P"   =>  "ALL A. A \<subset> B --> P"
haftmann@30531
   230
 "\<exists>A\<subset>B. P"   =>  "EX A. A \<subset> B & P"
haftmann@30531
   231
 "\<forall>A\<subseteq>B. P"   =>  "ALL A. A \<subseteq> B --> P"
haftmann@30531
   232
 "\<exists>A\<subseteq>B. P"   =>  "EX A. A \<subseteq> B & P"
haftmann@30531
   233
 "\<exists>!A\<subseteq>B. P"  =>  "EX! A. A \<subseteq> B & P"
nipkow@14804
   234
wenzelm@60758
   235
print_translation \<open>
wenzelm@52143
   236
  let
wenzelm@52143
   237
    val All_binder = Mixfix.binder_name @{const_syntax All};
wenzelm@52143
   238
    val Ex_binder = Mixfix.binder_name @{const_syntax Ex};
wenzelm@52143
   239
    val impl = @{const_syntax HOL.implies};
wenzelm@52143
   240
    val conj = @{const_syntax HOL.conj};
wenzelm@52143
   241
    val sbset = @{const_syntax subset};
wenzelm@52143
   242
    val sbset_eq = @{const_syntax subset_eq};
wenzelm@52143
   243
wenzelm@52143
   244
    val trans =
wenzelm@52143
   245
     [((All_binder, impl, sbset), @{syntax_const "_setlessAll"}),
wenzelm@52143
   246
      ((All_binder, impl, sbset_eq), @{syntax_const "_setleAll"}),
wenzelm@52143
   247
      ((Ex_binder, conj, sbset), @{syntax_const "_setlessEx"}),
wenzelm@52143
   248
      ((Ex_binder, conj, sbset_eq), @{syntax_const "_setleEx"})];
wenzelm@52143
   249
wenzelm@52143
   250
    fun mk v (v', T) c n P =
wenzelm@52143
   251
      if v = v' andalso not (Term.exists_subterm (fn Free (x, _) => x = v | _ => false) n)
wenzelm@52143
   252
      then Syntax.const c $ Syntax_Trans.mark_bound_body (v', T) $ n $ P
wenzelm@52143
   253
      else raise Match;
wenzelm@52143
   254
wenzelm@52143
   255
    fun tr' q = (q, fn _ =>
wenzelm@52143
   256
      (fn [Const (@{syntax_const "_bound"}, _) $ Free (v, Type (@{type_name set}, _)),
wenzelm@52143
   257
          Const (c, _) $
wenzelm@52143
   258
            (Const (d, _) $ (Const (@{syntax_const "_bound"}, _) $ Free (v', T)) $ n) $ P] =>
wenzelm@52143
   259
          (case AList.lookup (op =) trans (q, c, d) of
wenzelm@52143
   260
            NONE => raise Match
wenzelm@52143
   261
          | SOME l => mk v (v', T) l n P)
wenzelm@52143
   262
        | _ => raise Match));
wenzelm@52143
   263
  in
wenzelm@52143
   264
    [tr' All_binder, tr' Ex_binder]
wenzelm@52143
   265
  end
wenzelm@60758
   266
\<close>
wenzelm@60758
   267
wenzelm@60758
   268
wenzelm@60758
   269
text \<open>
wenzelm@61799
   270
  \medskip Translate between \<open>{e | x1...xn. P}\<close> and \<open>{u. EX x1..xn. u = e & P}\<close>; \<open>{y. EX x1..xn. y = e & P}\<close> is
wenzelm@61799
   271
  only translated if \<open>[0..n] subset bvs(e)\<close>.
wenzelm@60758
   272
\<close>
wenzelm@11979
   273
wenzelm@35115
   274
syntax
wenzelm@35115
   275
  "_Setcompr" :: "'a => idts => bool => 'a set"    ("(1{_ |/_./ _})")
wenzelm@35115
   276
wenzelm@60758
   277
parse_translation \<open>
wenzelm@11979
   278
  let
wenzelm@42284
   279
    val ex_tr = snd (Syntax_Trans.mk_binder_tr ("EX ", @{const_syntax Ex}));
wenzelm@3947
   280
wenzelm@35115
   281
    fun nvars (Const (@{syntax_const "_idts"}, _) $ _ $ idts) = nvars idts + 1
wenzelm@11979
   282
      | nvars _ = 1;
wenzelm@11979
   283
wenzelm@52143
   284
    fun setcompr_tr ctxt [e, idts, b] =
wenzelm@11979
   285
      let
haftmann@38864
   286
        val eq = Syntax.const @{const_syntax HOL.eq} $ Bound (nvars idts) $ e;
haftmann@38795
   287
        val P = Syntax.const @{const_syntax HOL.conj} $ eq $ b;
wenzelm@52143
   288
        val exP = ex_tr ctxt [idts, P];
wenzelm@44241
   289
      in Syntax.const @{const_syntax Collect} $ absdummy dummyT exP end;
wenzelm@11979
   290
wenzelm@35115
   291
  in [(@{syntax_const "_Setcompr"}, setcompr_tr)] end;
wenzelm@60758
   292
\<close>
wenzelm@60758
   293
wenzelm@60758
   294
print_translation \<open>
wenzelm@42284
   295
 [Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Ball} @{syntax_const "_Ball"},
wenzelm@42284
   296
  Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax Bex} @{syntax_const "_Bex"}]
wenzelm@61799
   297
\<close> \<comment> \<open>to avoid eta-contraction of body\<close>
wenzelm@60758
   298
wenzelm@60758
   299
print_translation \<open>
nipkow@13763
   300
let
wenzelm@42284
   301
  val ex_tr' = snd (Syntax_Trans.mk_binder_tr' (@{const_syntax Ex}, "DUMMY"));
nipkow@13763
   302
wenzelm@52143
   303
  fun setcompr_tr' ctxt [Abs (abs as (_, _, P))] =
nipkow@13763
   304
    let
wenzelm@35115
   305
      fun check (Const (@{const_syntax Ex}, _) $ Abs (_, _, P), n) = check (P, n + 1)
haftmann@38795
   306
        | check (Const (@{const_syntax HOL.conj}, _) $
haftmann@38864
   307
              (Const (@{const_syntax HOL.eq}, _) $ Bound m $ e) $ P, n) =
nipkow@13763
   308
            n > 0 andalso m = n andalso not (loose_bvar1 (P, n)) andalso
haftmann@33038
   309
            subset (op =) (0 upto (n - 1), add_loose_bnos (e, 0, []))
wenzelm@35115
   310
        | check _ = false;
clasohm@923
   311
wenzelm@11979
   312
        fun tr' (_ $ abs) =
wenzelm@52143
   313
          let val _ $ idts $ (_ $ (_ $ _ $ e) $ Q) = ex_tr' ctxt [abs]
wenzelm@35115
   314
          in Syntax.const @{syntax_const "_Setcompr"} $ e $ idts $ Q end;
wenzelm@35115
   315
    in
wenzelm@35115
   316
      if check (P, 0) then tr' P
wenzelm@35115
   317
      else
wenzelm@35115
   318
        let
wenzelm@42284
   319
          val (x as _ $ Free(xN, _), t) = Syntax_Trans.atomic_abs_tr' abs;
wenzelm@35115
   320
          val M = Syntax.const @{syntax_const "_Coll"} $ x $ t;
wenzelm@35115
   321
        in
wenzelm@35115
   322
          case t of
haftmann@38795
   323
            Const (@{const_syntax HOL.conj}, _) $
haftmann@37677
   324
              (Const (@{const_syntax Set.member}, _) $
wenzelm@35115
   325
                (Const (@{syntax_const "_bound"}, _) $ Free (yN, _)) $ A) $ P =>
wenzelm@35115
   326
            if xN = yN then Syntax.const @{syntax_const "_Collect"} $ x $ A $ P else M
wenzelm@35115
   327
          | _ => M
wenzelm@35115
   328
        end
nipkow@13763
   329
    end;
wenzelm@35115
   330
  in [(@{const_syntax Collect}, setcompr_tr')] end;
wenzelm@60758
   331
\<close>
wenzelm@60758
   332
wenzelm@60758
   333
simproc_setup defined_Bex ("EX x:A. P x & Q x") = \<open>
wenzelm@54998
   334
  fn _ => Quantifier1.rearrange_bex
wenzelm@54998
   335
    (fn ctxt =>
wenzelm@54998
   336
      unfold_tac ctxt @{thms Bex_def} THEN
wenzelm@59498
   337
      Quantifier1.prove_one_point_ex_tac ctxt)
wenzelm@60758
   338
\<close>
wenzelm@60758
   339
wenzelm@60758
   340
simproc_setup defined_All ("ALL x:A. P x --> Q x") = \<open>
wenzelm@54998
   341
  fn _ => Quantifier1.rearrange_ball
wenzelm@54998
   342
    (fn ctxt =>
wenzelm@54998
   343
      unfold_tac ctxt @{thms Ball_def} THEN
wenzelm@59498
   344
      Quantifier1.prove_one_point_all_tac ctxt)
wenzelm@60758
   345
\<close>
haftmann@32117
   346
wenzelm@11979
   347
lemma ballI [intro!]: "(!!x. x:A ==> P x) ==> ALL x:A. P x"
wenzelm@11979
   348
  by (simp add: Ball_def)
wenzelm@11979
   349
wenzelm@11979
   350
lemmas strip = impI allI ballI
wenzelm@11979
   351
wenzelm@11979
   352
lemma bspec [dest?]: "ALL x:A. P x ==> x:A ==> P x"
wenzelm@11979
   353
  by (simp add: Ball_def)
wenzelm@11979
   354
wenzelm@60758
   355
text \<open>
wenzelm@11979
   356
  Gives better instantiation for bound:
wenzelm@60758
   357
\<close>
wenzelm@60758
   358
wenzelm@60758
   359
setup \<open>
wenzelm@51717
   360
  map_theory_claset (fn ctxt =>
wenzelm@59498
   361
    ctxt addbefore ("bspec", fn ctxt' => dresolve_tac ctxt' @{thms bspec} THEN' assume_tac ctxt'))
wenzelm@60758
   362
\<close>
wenzelm@60758
   363
wenzelm@60758
   364
ML \<open>
haftmann@32117
   365
structure Simpdata =
haftmann@32117
   366
struct
haftmann@32117
   367
haftmann@32117
   368
open Simpdata;
haftmann@32117
   369
haftmann@32117
   370
val mksimps_pairs = [(@{const_name Ball}, @{thms bspec})] @ mksimps_pairs;
haftmann@32117
   371
haftmann@32117
   372
end;
haftmann@32117
   373
haftmann@32117
   374
open Simpdata;
wenzelm@60758
   375
\<close>
wenzelm@60758
   376
wenzelm@60758
   377
declaration \<open>fn _ =>
wenzelm@45625
   378
  Simplifier.map_ss (Simplifier.set_mksimps (mksimps mksimps_pairs))
wenzelm@60758
   379
\<close>
haftmann@32117
   380
haftmann@32117
   381
lemma ballE [elim]: "ALL x:A. P x ==> (P x ==> Q) ==> (x ~: A ==> Q) ==> Q"
haftmann@32117
   382
  by (unfold Ball_def) blast
haftmann@32117
   383
wenzelm@11979
   384
lemma bexI [intro]: "P x ==> x:A ==> EX x:A. P x"
wenzelm@61799
   385
  \<comment> \<open>Normally the best argument order: @{prop "P x"} constrains the
wenzelm@60758
   386
    choice of @{prop "x:A"}.\<close>
wenzelm@11979
   387
  by (unfold Bex_def) blast
wenzelm@11979
   388
wenzelm@13113
   389
lemma rev_bexI [intro?]: "x:A ==> P x ==> EX x:A. P x"
wenzelm@61799
   390
  \<comment> \<open>The best argument order when there is only one @{prop "x:A"}.\<close>
wenzelm@11979
   391
  by (unfold Bex_def) blast
wenzelm@11979
   392
wenzelm@11979
   393
lemma bexCI: "(ALL x:A. ~P x ==> P a) ==> a:A ==> EX x:A. P x"
wenzelm@11979
   394
  by (unfold Bex_def) blast
wenzelm@11979
   395
wenzelm@11979
   396
lemma bexE [elim!]: "EX x:A. P x ==> (!!x. x:A ==> P x ==> Q) ==> Q"
wenzelm@11979
   397
  by (unfold Bex_def) blast
wenzelm@11979
   398
wenzelm@11979
   399
lemma ball_triv [simp]: "(ALL x:A. P) = ((EX x. x:A) --> P)"
wenzelm@61799
   400
  \<comment> \<open>Trival rewrite rule.\<close>
wenzelm@11979
   401
  by (simp add: Ball_def)
wenzelm@11979
   402
wenzelm@11979
   403
lemma bex_triv [simp]: "(EX x:A. P) = ((EX x. x:A) & P)"
wenzelm@61799
   404
  \<comment> \<open>Dual form for existentials.\<close>
wenzelm@11979
   405
  by (simp add: Bex_def)
wenzelm@11979
   406
wenzelm@11979
   407
lemma bex_triv_one_point1 [simp]: "(EX x:A. x = a) = (a:A)"
wenzelm@11979
   408
  by blast
wenzelm@11979
   409
wenzelm@11979
   410
lemma bex_triv_one_point2 [simp]: "(EX x:A. a = x) = (a:A)"
wenzelm@11979
   411
  by blast
wenzelm@11979
   412
wenzelm@11979
   413
lemma bex_one_point1 [simp]: "(EX x:A. x = a & P x) = (a:A & P a)"
wenzelm@11979
   414
  by blast
wenzelm@11979
   415
wenzelm@11979
   416
lemma bex_one_point2 [simp]: "(EX x:A. a = x & P x) = (a:A & P a)"
wenzelm@11979
   417
  by blast
wenzelm@11979
   418
wenzelm@11979
   419
lemma ball_one_point1 [simp]: "(ALL x:A. x = a --> P x) = (a:A --> P a)"
wenzelm@11979
   420
  by blast
wenzelm@11979
   421
wenzelm@11979
   422
lemma ball_one_point2 [simp]: "(ALL x:A. a = x --> P x) = (a:A --> P a)"
wenzelm@11979
   423
  by blast
wenzelm@11979
   424
haftmann@43818
   425
lemma ball_conj_distrib:
haftmann@43818
   426
  "(\<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
   427
  by blast
haftmann@43818
   428
haftmann@43818
   429
lemma bex_disj_distrib:
haftmann@43818
   430
  "(\<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
   431
  by blast
haftmann@43818
   432
wenzelm@11979
   433
wenzelm@60758
   434
text \<open>Congruence rules\<close>
wenzelm@11979
   435
berghofe@16636
   436
lemma ball_cong:
wenzelm@11979
   437
  "A = B ==> (!!x. x:B ==> P x = Q x) ==>
wenzelm@11979
   438
    (ALL x:A. P x) = (ALL x:B. Q x)"
wenzelm@11979
   439
  by (simp add: Ball_def)
wenzelm@11979
   440
berghofe@16636
   441
lemma strong_ball_cong [cong]:
berghofe@16636
   442
  "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>
berghofe@16636
   443
    (ALL x:A. P x) = (ALL x:B. Q x)"
berghofe@16636
   444
  by (simp add: simp_implies_def Ball_def)
berghofe@16636
   445
berghofe@16636
   446
lemma bex_cong:
wenzelm@11979
   447
  "A = B ==> (!!x. x:B ==> P x = Q x) ==>
wenzelm@11979
   448
    (EX x:A. P x) = (EX x:B. Q x)"
wenzelm@11979
   449
  by (simp add: Bex_def cong: conj_cong)
regensbu@1273
   450
berghofe@16636
   451
lemma strong_bex_cong [cong]:
berghofe@16636
   452
  "A = B ==> (!!x. x:B =simp=> P x = Q x) ==>
berghofe@16636
   453
    (EX x:A. P x) = (EX x:B. Q x)"
berghofe@16636
   454
  by (simp add: simp_implies_def Bex_def cong: conj_cong)
berghofe@16636
   455
hoelzl@59000
   456
lemma bex1_def: "(\<exists>!x\<in>X. P x) \<longleftrightarrow> (\<exists>x\<in>X. P x) \<and> (\<forall>x\<in>X. \<forall>y\<in>X. P x \<longrightarrow> P y \<longrightarrow> x = y)"
hoelzl@59000
   457
  by auto
haftmann@30531
   458
wenzelm@60758
   459
subsection \<open>Basic operations\<close>
wenzelm@60758
   460
wenzelm@60758
   461
subsubsection \<open>Subsets\<close>
haftmann@30531
   462
paulson@33022
   463
lemma subsetI [intro!]: "(\<And>x. x \<in> A \<Longrightarrow> x \<in> B) \<Longrightarrow> A \<subseteq> B"
haftmann@45959
   464
  by (simp add: less_eq_set_def le_fun_def)
haftmann@30352
   465
wenzelm@60758
   466
text \<open>
wenzelm@61799
   467
  \medskip Map the type \<open>'a set => anything\<close> to just @{typ
haftmann@30531
   468
  'a}; for overloading constants whose first argument has type @{typ
haftmann@30531
   469
  "'a set"}.
wenzelm@60758
   470
\<close>
wenzelm@11979
   471
haftmann@30596
   472
lemma subsetD [elim, intro?]: "A \<subseteq> B ==> c \<in> A ==> c \<in> B"
haftmann@45959
   473
  by (simp add: less_eq_set_def le_fun_def)
wenzelm@61799
   474
  \<comment> \<open>Rule in Modus Ponens style.\<close>
haftmann@30531
   475
blanchet@54147
   476
lemma rev_subsetD [intro?]: "c \<in> A ==> A \<subseteq> B ==> c \<in> B"
wenzelm@61799
   477
  \<comment> \<open>The same, with reversed premises for use with \<open>erule\<close> --
wenzelm@61799
   478
      cf \<open>rev_mp\<close>.\<close>
haftmann@30531
   479
  by (rule subsetD)
haftmann@30531
   480
wenzelm@60758
   481
text \<open>
haftmann@30531
   482
  \medskip Converts @{prop "A \<subseteq> B"} to @{prop "x \<in> A ==> x \<in> B"}.
wenzelm@60758
   483
\<close>
haftmann@30531
   484
blanchet@54147
   485
lemma subsetCE [elim]: "A \<subseteq> B ==> (c \<notin> A ==> P) ==> (c \<in> B ==> P) ==> P"
wenzelm@61799
   486
  \<comment> \<open>Classical elimination rule.\<close>
haftmann@45959
   487
  by (auto simp add: less_eq_set_def le_fun_def)
haftmann@30531
   488
blanchet@54147
   489
lemma subset_eq: "A \<le> B = (\<forall>x\<in>A. x \<in> B)" by blast
blanchet@54147
   490
blanchet@54147
   491
lemma contra_subsetD: "A \<subseteq> B ==> c \<notin> B ==> c \<notin> A"
haftmann@30531
   492
  by blast
haftmann@30531
   493
huffman@45121
   494
lemma subset_refl: "A \<subseteq> A"
huffman@45121
   495
  by (fact order_refl) (* already [iff] *)
haftmann@30531
   496
haftmann@30531
   497
lemma subset_trans: "A \<subseteq> B ==> B \<subseteq> C ==> A \<subseteq> C"
haftmann@32081
   498
  by (fact order_trans)
haftmann@32081
   499
haftmann@32081
   500
lemma set_rev_mp: "x:A ==> A \<subseteq> B ==> x:B"
haftmann@32081
   501
  by (rule subsetD)
haftmann@32081
   502
haftmann@32081
   503
lemma set_mp: "A \<subseteq> B ==> x:A ==> x:B"
haftmann@32081
   504
  by (rule subsetD)
haftmann@32081
   505
haftmann@46146
   506
lemma subset_not_subset_eq [code]:
haftmann@46146
   507
  "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
haftmann@46146
   508
  by (fact less_le_not_le)
haftmann@46146
   509
paulson@33044
   510
lemma eq_mem_trans: "a=b ==> b \<in> A ==> a \<in> A"
paulson@33044
   511
  by simp
paulson@33044
   512
haftmann@32081
   513
lemmas basic_trans_rules [trans] =
paulson@33044
   514
  order_trans_rules set_rev_mp set_mp eq_mem_trans
haftmann@30531
   515
haftmann@30531
   516
wenzelm@60758
   517
subsubsection \<open>Equality\<close>
haftmann@30531
   518
haftmann@30531
   519
lemma subset_antisym [intro!]: "A \<subseteq> B ==> B \<subseteq> A ==> A = B"
wenzelm@61799
   520
  \<comment> \<open>Anti-symmetry of the subset relation.\<close>
nipkow@39302
   521
  by (iprover intro: set_eqI subsetD)
haftmann@30531
   522
wenzelm@60758
   523
text \<open>
haftmann@30531
   524
  \medskip Equality rules from ZF set theory -- are they appropriate
haftmann@30531
   525
  here?
wenzelm@60758
   526
\<close>
haftmann@30531
   527
haftmann@30531
   528
lemma equalityD1: "A = B ==> A \<subseteq> B"
krauss@34209
   529
  by simp
haftmann@30531
   530
haftmann@30531
   531
lemma equalityD2: "A = B ==> B \<subseteq> A"
krauss@34209
   532
  by simp
haftmann@30531
   533
wenzelm@60758
   534
text \<open>
wenzelm@61799
   535
  \medskip Be careful when adding this to the claset as \<open>subset_empty\<close> is in the simpset: @{prop "A = {}"} goes to @{prop "{}
haftmann@30531
   536
  \<subseteq> A"} and @{prop "A \<subseteq> {}"} and then back to @{prop "A = {}"}!
wenzelm@60758
   537
\<close>
haftmann@30352
   538
haftmann@30531
   539
lemma equalityE: "A = B ==> (A \<subseteq> B ==> B \<subseteq> A ==> P) ==> P"
krauss@34209
   540
  by simp
haftmann@30531
   541
haftmann@30531
   542
lemma equalityCE [elim]:
haftmann@30531
   543
    "A = B ==> (c \<in> A ==> c \<in> B ==> P) ==> (c \<notin> A ==> c \<notin> B ==> P) ==> P"
haftmann@30531
   544
  by blast
haftmann@30531
   545
haftmann@30531
   546
lemma eqset_imp_iff: "A = B ==> (x : A) = (x : B)"
haftmann@30531
   547
  by simp
haftmann@30531
   548
haftmann@30531
   549
lemma eqelem_imp_iff: "x = y ==> (x : A) = (y : A)"
haftmann@30531
   550
  by simp
haftmann@30531
   551
haftmann@30531
   552
wenzelm@60758
   553
subsubsection \<open>The empty set\<close>
haftmann@41082
   554
haftmann@41082
   555
lemma empty_def:
haftmann@41082
   556
  "{} = {x. False}"
haftmann@45959
   557
  by (simp add: bot_set_def bot_fun_def)
haftmann@41082
   558
haftmann@41082
   559
lemma empty_iff [simp]: "(c : {}) = False"
haftmann@41082
   560
  by (simp add: empty_def)
haftmann@41082
   561
haftmann@41082
   562
lemma emptyE [elim!]: "a : {} ==> P"
haftmann@41082
   563
  by simp
haftmann@41082
   564
haftmann@41082
   565
lemma empty_subsetI [iff]: "{} \<subseteq> A"
wenzelm@61799
   566
    \<comment> \<open>One effect is to delete the ASSUMPTION @{prop "{} <= A"}\<close>
haftmann@41082
   567
  by blast
haftmann@41082
   568
haftmann@41082
   569
lemma equals0I: "(!!y. y \<in> A ==> False) ==> A = {}"
haftmann@41082
   570
  by blast
haftmann@41082
   571
haftmann@41082
   572
lemma equals0D: "A = {} ==> a \<notin> A"
wenzelm@61799
   573
    \<comment> \<open>Use for reasoning about disjointness: \<open>A Int B = {}\<close>\<close>
haftmann@41082
   574
  by blast
haftmann@41082
   575
haftmann@41082
   576
lemma ball_empty [simp]: "Ball {} P = True"
haftmann@41082
   577
  by (simp add: Ball_def)
haftmann@41082
   578
haftmann@41082
   579
lemma bex_empty [simp]: "Bex {} P = False"
haftmann@41082
   580
  by (simp add: Bex_def)
haftmann@41082
   581
haftmann@41082
   582
wenzelm@60758
   583
subsubsection \<open>The universal set -- UNIV\<close>
haftmann@30531
   584
haftmann@32264
   585
abbreviation UNIV :: "'a set" where
haftmann@32264
   586
  "UNIV \<equiv> top"
haftmann@32135
   587
haftmann@32135
   588
lemma UNIV_def:
haftmann@32117
   589
  "UNIV = {x. True}"
haftmann@45959
   590
  by (simp add: top_set_def top_fun_def)
haftmann@32081
   591
haftmann@30531
   592
lemma UNIV_I [simp]: "x : UNIV"
haftmann@30531
   593
  by (simp add: UNIV_def)
haftmann@30531
   594
wenzelm@61799
   595
declare UNIV_I [intro]  \<comment> \<open>unsafe makes it less likely to cause problems\<close>
haftmann@30531
   596
haftmann@30531
   597
lemma UNIV_witness [intro?]: "EX x. x : UNIV"
haftmann@30531
   598
  by simp
haftmann@30531
   599
huffman@45121
   600
lemma subset_UNIV: "A \<subseteq> UNIV"
huffman@45121
   601
  by (fact top_greatest) (* already simp *)
haftmann@30531
   602
wenzelm@60758
   603
text \<open>
wenzelm@61799
   604
  \medskip Eta-contracting these two rules (to remove \<open>P\<close>)
haftmann@30531
   605
  causes them to be ignored because of their interaction with
haftmann@30531
   606
  congruence rules.
wenzelm@60758
   607
\<close>
haftmann@30531
   608
haftmann@30531
   609
lemma ball_UNIV [simp]: "Ball UNIV P = All P"
haftmann@30531
   610
  by (simp add: Ball_def)
haftmann@30531
   611
haftmann@30531
   612
lemma bex_UNIV [simp]: "Bex UNIV P = Ex P"
haftmann@30531
   613
  by (simp add: Bex_def)
haftmann@30531
   614
haftmann@30531
   615
lemma UNIV_eq_I: "(\<And>x. x \<in> A) \<Longrightarrow> UNIV = A"
haftmann@30531
   616
  by auto
haftmann@30531
   617
haftmann@30531
   618
lemma UNIV_not_empty [iff]: "UNIV ~= {}"
haftmann@30531
   619
  by (blast elim: equalityE)
haftmann@30531
   620
nipkow@51334
   621
lemma empty_not_UNIV[simp]: "{} \<noteq> UNIV"
nipkow@51334
   622
by blast
nipkow@51334
   623
wenzelm@60758
   624
subsubsection \<open>The Powerset operator -- Pow\<close>
haftmann@30531
   625
haftmann@32077
   626
definition Pow :: "'a set => 'a set set" where
haftmann@32077
   627
  Pow_def: "Pow A = {B. B \<le> A}"
haftmann@32077
   628
haftmann@30531
   629
lemma Pow_iff [iff]: "(A \<in> Pow B) = (A \<subseteq> B)"
haftmann@30531
   630
  by (simp add: Pow_def)
haftmann@30531
   631
haftmann@30531
   632
lemma PowI: "A \<subseteq> B ==> A \<in> Pow B"
haftmann@30531
   633
  by (simp add: Pow_def)
haftmann@30531
   634
haftmann@30531
   635
lemma PowD: "A \<in> Pow B ==> A \<subseteq> B"
haftmann@30531
   636
  by (simp add: Pow_def)
haftmann@30531
   637
haftmann@30531
   638
lemma Pow_bottom: "{} \<in> Pow B"
haftmann@30531
   639
  by simp
haftmann@30531
   640
haftmann@30531
   641
lemma Pow_top: "A \<in> Pow A"
krauss@34209
   642
  by simp
haftmann@30531
   643
hoelzl@40703
   644
lemma Pow_not_empty: "Pow A \<noteq> {}"
hoelzl@40703
   645
  using Pow_top by blast
haftmann@30531
   646
haftmann@41076
   647
wenzelm@60758
   648
subsubsection \<open>Set complement\<close>
haftmann@30531
   649
haftmann@30531
   650
lemma Compl_iff [simp]: "(c \<in> -A) = (c \<notin> A)"
haftmann@45959
   651
  by (simp add: fun_Compl_def uminus_set_def)
haftmann@30531
   652
haftmann@30531
   653
lemma ComplI [intro!]: "(c \<in> A ==> False) ==> c \<in> -A"
haftmann@45959
   654
  by (simp add: fun_Compl_def uminus_set_def) blast
clasohm@923
   655
wenzelm@60758
   656
text \<open>
haftmann@30531
   657
  \medskip This form, with negated conclusion, works well with the
haftmann@30531
   658
  Classical prover.  Negated assumptions behave like formulae on the
wenzelm@60758
   659
  right side of the notional turnstile ...\<close>
haftmann@30531
   660
haftmann@30531
   661
lemma ComplD [dest!]: "c : -A ==> c~:A"
haftmann@45959
   662
  by simp
haftmann@30531
   663
haftmann@30531
   664
lemmas ComplE = ComplD [elim_format]
haftmann@30531
   665
haftmann@45959
   666
lemma Compl_eq: "- A = {x. ~ x : A}"
haftmann@45959
   667
  by blast
haftmann@30531
   668
haftmann@30531
   669
wenzelm@60758
   670
subsubsection \<open>Binary intersection\<close>
haftmann@41082
   671
haftmann@41082
   672
abbreviation inter :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "Int" 70) where
haftmann@41082
   673
  "op Int \<equiv> inf"
haftmann@41082
   674
haftmann@41082
   675
notation (xsymbols)
haftmann@41082
   676
  inter  (infixl "\<inter>" 70)
haftmann@41082
   677
haftmann@41082
   678
lemma Int_def:
haftmann@41082
   679
  "A \<inter> B = {x. x \<in> A \<and> x \<in> B}"
haftmann@45959
   680
  by (simp add: inf_set_def inf_fun_def)
haftmann@41082
   681
haftmann@41082
   682
lemma Int_iff [simp]: "(c : A Int B) = (c:A & c:B)"
haftmann@41082
   683
  by (unfold Int_def) blast
haftmann@41082
   684
haftmann@41082
   685
lemma IntI [intro!]: "c:A ==> c:B ==> c : A Int B"
haftmann@41082
   686
  by simp
haftmann@41082
   687
haftmann@41082
   688
lemma IntD1: "c : A Int B ==> c:A"
haftmann@41082
   689
  by simp
haftmann@41082
   690
haftmann@41082
   691
lemma IntD2: "c : A Int B ==> c:B"
haftmann@41082
   692
  by simp
haftmann@41082
   693
haftmann@41082
   694
lemma IntE [elim!]: "c : A Int B ==> (c:A ==> c:B ==> P) ==> P"
haftmann@41082
   695
  by simp
haftmann@41082
   696
haftmann@41082
   697
lemma mono_Int: "mono f \<Longrightarrow> f (A \<inter> B) \<subseteq> f A \<inter> f B"
haftmann@41082
   698
  by (fact mono_inf)
haftmann@41082
   699
haftmann@41082
   700
wenzelm@60758
   701
subsubsection \<open>Binary union\<close>
haftmann@30531
   702
haftmann@32683
   703
abbreviation union :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" (infixl "Un" 65) where
haftmann@41076
   704
  "union \<equiv> sup"
haftmann@32081
   705
haftmann@32081
   706
notation (xsymbols)
haftmann@32135
   707
  union  (infixl "\<union>" 65)
haftmann@32081
   708
haftmann@32135
   709
lemma Un_def:
haftmann@32135
   710
  "A \<union> B = {x. x \<in> A \<or> x \<in> B}"
haftmann@45959
   711
  by (simp add: sup_set_def sup_fun_def)
haftmann@32081
   712
haftmann@30531
   713
lemma Un_iff [simp]: "(c : A Un B) = (c:A | c:B)"
haftmann@30531
   714
  by (unfold Un_def) blast
haftmann@30531
   715
haftmann@30531
   716
lemma UnI1 [elim?]: "c:A ==> c : A Un B"
haftmann@30531
   717
  by simp
haftmann@30531
   718
haftmann@30531
   719
lemma UnI2 [elim?]: "c:B ==> c : A Un B"
haftmann@30531
   720
  by simp
haftmann@30531
   721
wenzelm@60758
   722
text \<open>
haftmann@30531
   723
  \medskip Classical introduction rule: no commitment to @{prop A} vs
haftmann@30531
   724
  @{prop B}.
wenzelm@60758
   725
\<close>
wenzelm@11979
   726
haftmann@30531
   727
lemma UnCI [intro!]: "(c~:B ==> c:A) ==> c : A Un B"
haftmann@30531
   728
  by auto
haftmann@30531
   729
haftmann@30531
   730
lemma UnE [elim!]: "c : A Un B ==> (c:A ==> P) ==> (c:B ==> P) ==> P"
haftmann@30531
   731
  by (unfold Un_def) blast
haftmann@30531
   732
haftmann@32117
   733
lemma insert_def: "insert a B = {x. x = a} \<union> B"
haftmann@45959
   734
  by (simp add: insert_compr Un_def)
haftmann@32081
   735
haftmann@32081
   736
lemma mono_Un: "mono f \<Longrightarrow> f A \<union> f B \<subseteq> f (A \<union> B)"
haftmann@32683
   737
  by (fact mono_sup)
haftmann@32081
   738
haftmann@30531
   739
wenzelm@60758
   740
subsubsection \<open>Set difference\<close>
haftmann@30531
   741
haftmann@30531
   742
lemma Diff_iff [simp]: "(c : A - B) = (c:A & c~:B)"
haftmann@45959
   743
  by (simp add: minus_set_def fun_diff_def)
haftmann@30531
   744
haftmann@30531
   745
lemma DiffI [intro!]: "c : A ==> c ~: B ==> c : A - B"
haftmann@30531
   746
  by simp
haftmann@30531
   747
haftmann@30531
   748
lemma DiffD1: "c : A - B ==> c : A"
haftmann@30531
   749
  by simp
haftmann@30531
   750
haftmann@30531
   751
lemma DiffD2: "c : A - B ==> c : B ==> P"
haftmann@30531
   752
  by simp
haftmann@30531
   753
haftmann@30531
   754
lemma DiffE [elim!]: "c : A - B ==> (c:A ==> c~:B ==> P) ==> P"
haftmann@30531
   755
  by simp
haftmann@30531
   756
haftmann@30531
   757
lemma set_diff_eq: "A - B = {x. x : A & ~ x : B}" by blast
haftmann@30531
   758
haftmann@30531
   759
lemma Compl_eq_Diff_UNIV: "-A = (UNIV - A)"
haftmann@30531
   760
by blast
haftmann@30531
   761
haftmann@30531
   762
wenzelm@60758
   763
subsubsection \<open>Augmenting a set -- @{const insert}\<close>
haftmann@30531
   764
haftmann@30531
   765
lemma insert_iff [simp]: "(a : insert b A) = (a = b | a:A)"
haftmann@30531
   766
  by (unfold insert_def) blast
haftmann@30531
   767
haftmann@30531
   768
lemma insertI1: "a : insert a B"
haftmann@30531
   769
  by simp
haftmann@30531
   770
haftmann@30531
   771
lemma insertI2: "a : B ==> a : insert b B"
haftmann@30531
   772
  by simp
haftmann@30531
   773
haftmann@30531
   774
lemma insertE [elim!]: "a : insert b A ==> (a = b ==> P) ==> (a:A ==> P) ==> P"
haftmann@30531
   775
  by (unfold insert_def) blast
haftmann@30531
   776
haftmann@30531
   777
lemma insertCI [intro!]: "(a~:B ==> a = b) ==> a: insert b B"
wenzelm@61799
   778
  \<comment> \<open>Classical introduction rule.\<close>
haftmann@30531
   779
  by auto
haftmann@30531
   780
haftmann@30531
   781
lemma subset_insert_iff: "(A \<subseteq> insert x B) = (if x:A then A - {x} \<subseteq> B else A \<subseteq> B)"
haftmann@30531
   782
  by auto
haftmann@30531
   783
haftmann@30531
   784
lemma set_insert:
haftmann@30531
   785
  assumes "x \<in> A"
haftmann@30531
   786
  obtains B where "A = insert x B" and "x \<notin> B"
haftmann@30531
   787
proof
haftmann@30531
   788
  from assms show "A = insert x (A - {x})" by blast
haftmann@30531
   789
next
haftmann@30531
   790
  show "x \<notin> A - {x}" by blast
haftmann@30531
   791
qed
haftmann@30531
   792
haftmann@30531
   793
lemma insert_ident: "x ~: A ==> x ~: B ==> (insert x A = insert x B) = (A = B)"
haftmann@30531
   794
by auto
haftmann@30531
   795
nipkow@44744
   796
lemma insert_eq_iff: assumes "a \<notin> A" "b \<notin> B"
nipkow@44744
   797
shows "insert a A = insert b B \<longleftrightarrow>
nipkow@44744
   798
  (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
   799
  (is "?L \<longleftrightarrow> ?R")
nipkow@44744
   800
proof
nipkow@44744
   801
  assume ?L
nipkow@44744
   802
  show ?R
nipkow@44744
   803
  proof cases
wenzelm@60758
   804
    assume "a=b" with assms \<open>?L\<close> show ?R by (simp add: insert_ident)
nipkow@44744
   805
  next
nipkow@44744
   806
    assume "a\<noteq>b"
nipkow@44744
   807
    let ?C = "A - {b}"
nipkow@44744
   808
    have "A = insert b ?C \<and> b \<notin> ?C \<and> B = insert a ?C \<and> a \<notin> ?C"
wenzelm@60758
   809
      using assms \<open>?L\<close> \<open>a\<noteq>b\<close> by auto
wenzelm@60758
   810
    thus ?R using \<open>a\<noteq>b\<close> by auto
nipkow@44744
   811
  qed
nipkow@44744
   812
next
haftmann@46128
   813
  assume ?R thus ?L by (auto split: if_splits)
nipkow@44744
   814
qed
nipkow@44744
   815
Andreas@60057
   816
lemma insert_UNIV: "insert x UNIV = UNIV"
Andreas@60057
   817
by auto
Andreas@60057
   818
wenzelm@60758
   819
subsubsection \<open>Singletons, using insert\<close>
haftmann@30531
   820
blanchet@54147
   821
lemma singletonI [intro!]: "a : {a}"
wenzelm@61799
   822
    \<comment> \<open>Redundant? But unlike \<open>insertCI\<close>, it proves the subgoal immediately!\<close>
haftmann@30531
   823
  by (rule insertI1)
haftmann@30531
   824
blanchet@54147
   825
lemma singletonD [dest!]: "b : {a} ==> b = a"
haftmann@30531
   826
  by blast
haftmann@30531
   827
haftmann@30531
   828
lemmas singletonE = singletonD [elim_format]
haftmann@30531
   829
haftmann@30531
   830
lemma singleton_iff: "(b : {a}) = (b = a)"
haftmann@30531
   831
  by blast
haftmann@30531
   832
haftmann@30531
   833
lemma singleton_inject [dest!]: "{a} = {b} ==> a = b"
haftmann@30531
   834
  by blast
haftmann@30531
   835
blanchet@54147
   836
lemma singleton_insert_inj_eq [iff]:
haftmann@30531
   837
     "({b} = insert a A) = (a = b & A \<subseteq> {b})"
haftmann@30531
   838
  by blast
haftmann@30531
   839
blanchet@54147
   840
lemma singleton_insert_inj_eq' [iff]:
haftmann@30531
   841
     "(insert a A = {b}) = (a = b & A \<subseteq> {b})"
haftmann@30531
   842
  by blast
haftmann@30531
   843
haftmann@30531
   844
lemma subset_singletonD: "A \<subseteq> {x} ==> A = {} | A = {x}"
haftmann@30531
   845
  by fast
haftmann@30531
   846
haftmann@30531
   847
lemma singleton_conv [simp]: "{x. x = a} = {a}"
haftmann@30531
   848
  by blast
haftmann@30531
   849
haftmann@30531
   850
lemma singleton_conv2 [simp]: "{x. a = x} = {a}"
haftmann@30531
   851
  by blast
haftmann@30531
   852
bulwahn@46504
   853
lemma diff_single_insert: "A - {x} \<subseteq> B ==> A \<subseteq> insert x B"
haftmann@30531
   854
  by blast
haftmann@30531
   855
haftmann@30531
   856
lemma doubleton_eq_iff: "({a,b} = {c,d}) = (a=c & b=d | a=d & b=c)"
haftmann@30531
   857
  by (blast elim: equalityE)
haftmann@30531
   858
nipkow@53364
   859
lemma Un_singleton_iff:
nipkow@53364
   860
  "(A \<union> B = {x}) = (A = {} \<and> B = {x} \<or> A = {x} \<and> B = {} \<or> A = {x} \<and> B = {x})"
nipkow@53364
   861
by auto
nipkow@53364
   862
nipkow@53364
   863
lemma singleton_Un_iff:
nipkow@53364
   864
  "({x} = A \<union> B) = (A = {} \<and> B = {x} \<or> A = {x} \<and> B = {} \<or> A = {x} \<and> B = {x})"
nipkow@53364
   865
by auto
wenzelm@11979
   866
haftmann@56014
   867
wenzelm@60758
   868
subsubsection \<open>Image of a set under a function\<close>
wenzelm@60758
   869
wenzelm@60758
   870
text \<open>
haftmann@32077
   871
  Frequently @{term b} does not have the syntactic form of @{term "f x"}.
wenzelm@60758
   872
\<close>
haftmann@32077
   873
haftmann@56014
   874
definition image :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'b set" (infixr "`" 90)
haftmann@56014
   875
where
haftmann@56014
   876
  "f ` A = {y. \<exists>x\<in>A. y = f x}"
haftmann@56014
   877
haftmann@56014
   878
lemma image_eqI [simp, intro]:
haftmann@56014
   879
  "b = f x \<Longrightarrow> x \<in> A \<Longrightarrow> b \<in> f ` A"
haftmann@32077
   880
  by (unfold image_def) blast
haftmann@32077
   881
haftmann@56014
   882
lemma imageI:
haftmann@56014
   883
  "x \<in> A \<Longrightarrow> f x \<in> f ` A"
haftmann@32077
   884
  by (rule image_eqI) (rule refl)
haftmann@32077
   885
haftmann@56014
   886
lemma rev_image_eqI:
haftmann@56014
   887
  "x \<in> A \<Longrightarrow> b = f x \<Longrightarrow> b \<in> f ` A"
wenzelm@61799
   888
  \<comment> \<open>This version's more effective when we already have the
wenzelm@60758
   889
    required @{term x}.\<close>
haftmann@56014
   890
  by (rule image_eqI)
haftmann@32077
   891
haftmann@32077
   892
lemma imageE [elim!]:
wenzelm@61799
   893
  assumes "b \<in> (\<lambda>x. f x) ` A" \<comment> \<open>The eta-expansion gives variable-name preservation.\<close>
haftmann@56014
   894
  obtains x where "b = f x" and "x \<in> A"
haftmann@56014
   895
  using assms by (unfold image_def) blast
haftmann@32077
   896
haftmann@51173
   897
lemma Compr_image_eq:
haftmann@51173
   898
  "{x \<in> f ` A. P x} = f ` {x \<in> A. P (f x)}"
haftmann@51173
   899
  by auto
haftmann@51173
   900
haftmann@56014
   901
lemma image_Un:
haftmann@56014
   902
  "f ` (A \<union> B) = f ` A \<union> f ` B"
haftmann@32077
   903
  by blast
haftmann@32077
   904
haftmann@56014
   905
lemma image_iff:
haftmann@56014
   906
  "z \<in> f ` A \<longleftrightarrow> (\<exists>x\<in>A. z = f x)"
haftmann@56014
   907
  by blast
haftmann@56014
   908
haftmann@56014
   909
lemma image_subsetI:
haftmann@56014
   910
  "(\<And>x. x \<in> A \<Longrightarrow> f x \<in> B) \<Longrightarrow> f ` A \<subseteq> B"
wenzelm@61799
   911
  \<comment> \<open>Replaces the three steps \<open>subsetI\<close>, \<open>imageE\<close>,
wenzelm@61799
   912
    \<open>hypsubst\<close>, but breaks too many existing proofs.\<close>
haftmann@32077
   913
  by blast
wenzelm@11979
   914
haftmann@56014
   915
lemma image_subset_iff:
haftmann@56014
   916
  "f ` A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. f x \<in> B)"
wenzelm@61799
   917
  \<comment> \<open>This rewrite rule would confuse users if made default.\<close>
haftmann@56014
   918
  by blast
haftmann@56014
   919
haftmann@56014
   920
lemma subset_imageE:
haftmann@56014
   921
  assumes "B \<subseteq> f ` A"
haftmann@56014
   922
  obtains C where "C \<subseteq> A" and "B = f ` C"
haftmann@56014
   923
proof -
haftmann@56014
   924
  from assms have "B = f ` {a \<in> A. f a \<in> B}" by fast
haftmann@56014
   925
  moreover have "{a \<in> A. f a \<in> B} \<subseteq> A" by blast
haftmann@56014
   926
  ultimately show thesis by (blast intro: that)
haftmann@56014
   927
qed
haftmann@56014
   928
haftmann@56014
   929
lemma subset_image_iff:
haftmann@56014
   930
  "B \<subseteq> f ` A \<longleftrightarrow> (\<exists>AA\<subseteq>A. B = f ` AA)"
haftmann@56014
   931
  by (blast elim: subset_imageE)
haftmann@56014
   932
haftmann@56014
   933
lemma image_ident [simp]:
haftmann@56014
   934
  "(\<lambda>x. x) ` Y = Y"
haftmann@56014
   935
  by blast
haftmann@56014
   936
haftmann@56014
   937
lemma image_empty [simp]:
haftmann@56014
   938
  "f ` {} = {}"
haftmann@56014
   939
  by blast
haftmann@56014
   940
haftmann@56014
   941
lemma image_insert [simp]:
haftmann@56014
   942
  "f ` insert a B = insert (f a) (f ` B)"
haftmann@56014
   943
  by blast
haftmann@56014
   944
haftmann@56014
   945
lemma image_constant:
haftmann@56014
   946
  "x \<in> A \<Longrightarrow> (\<lambda>x. c) ` A = {c}"
haftmann@56014
   947
  by auto
haftmann@56014
   948
haftmann@56014
   949
lemma image_constant_conv:
haftmann@56014
   950
  "(\<lambda>x. c) ` A = (if A = {} then {} else {c})"
haftmann@56014
   951
  by auto
haftmann@56014
   952
haftmann@56014
   953
lemma image_image:
haftmann@56014
   954
  "f ` (g ` A) = (\<lambda>x. f (g x)) ` A"
haftmann@56014
   955
  by blast
haftmann@56014
   956
haftmann@56014
   957
lemma insert_image [simp]:
haftmann@56014
   958
  "x \<in> A ==> insert (f x) (f ` A) = f ` A"
haftmann@56014
   959
  by blast
haftmann@56014
   960
haftmann@56014
   961
lemma image_is_empty [iff]:
haftmann@56014
   962
  "f ` A = {} \<longleftrightarrow> A = {}"
haftmann@56014
   963
  by blast
haftmann@56014
   964
haftmann@56014
   965
lemma empty_is_image [iff]:
haftmann@56014
   966
  "{} = f ` A \<longleftrightarrow> A = {}"
haftmann@56014
   967
  by blast
haftmann@56014
   968
haftmann@56014
   969
lemma image_Collect:
haftmann@56014
   970
  "f ` {x. P x} = {f x | x. P x}"
wenzelm@61799
   971
  \<comment> \<open>NOT suitable as a default simprule: the RHS isn't simpler than the LHS,
haftmann@56014
   972
      with its implicit quantifier and conjunction.  Also image enjoys better
wenzelm@60758
   973
      equational properties than does the RHS.\<close>
haftmann@56014
   974
  by blast
haftmann@56014
   975
haftmann@56014
   976
lemma if_image_distrib [simp]:
haftmann@56014
   977
  "(\<lambda>x. if P x then f x else g x) ` S
haftmann@56014
   978
    = (f ` (S \<inter> {x. P x})) \<union> (g ` (S \<inter> {x. \<not> P x}))"
haftmann@56077
   979
  by auto
haftmann@56014
   980
haftmann@56014
   981
lemma image_cong:
haftmann@56014
   982
  "M = N \<Longrightarrow> (\<And>x. x \<in> N \<Longrightarrow> f x = g x) \<Longrightarrow> f ` M = g ` N"
haftmann@56014
   983
  by (simp add: image_def)
haftmann@56014
   984
haftmann@56014
   985
lemma image_Int_subset:
haftmann@56014
   986
  "f ` (A \<inter> B) \<subseteq> f ` A \<inter> f ` B"
haftmann@56014
   987
  by blast
haftmann@56014
   988
haftmann@56014
   989
lemma image_diff_subset:
haftmann@56014
   990
  "f ` A - f ` B \<subseteq> f ` (A - B)"
haftmann@56014
   991
  by blast
haftmann@56014
   992
lp15@59504
   993
lemma Setcompr_eq_image: "{f x | x. x \<in> A} = f ` A"
lp15@59504
   994
  by blast
lp15@59504
   995
haftmann@56014
   996
lemma ball_imageD:
haftmann@56014
   997
  assumes "\<forall>x\<in>f ` A. P x"
haftmann@56014
   998
  shows "\<forall>x\<in>A. P (f x)"
haftmann@56014
   999
  using assms by simp
haftmann@56014
  1000
haftmann@56014
  1001
lemma bex_imageD:
haftmann@56014
  1002
  assumes "\<exists>x\<in>f ` A. P x"
haftmann@56014
  1003
  shows "\<exists>x\<in>A. P (f x)"
haftmann@56014
  1004
  using assms by auto
haftmann@56014
  1005
haftmann@56014
  1006
wenzelm@60758
  1007
text \<open>
haftmann@32077
  1008
  \medskip Range of a function -- just a translation for image!
wenzelm@60758
  1009
\<close>
haftmann@32077
  1010
haftmann@56014
  1011
abbreviation range :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b set"
wenzelm@61799
  1012
where \<comment> "of function"
haftmann@56014
  1013
  "range f \<equiv> f ` UNIV"
haftmann@56014
  1014
haftmann@56014
  1015
lemma range_eqI:
haftmann@56014
  1016
  "b = f x \<Longrightarrow> b \<in> range f"
haftmann@56014
  1017
  by simp
haftmann@56014
  1018
haftmann@56014
  1019
lemma rangeI:
haftmann@56014
  1020
  "f x \<in> range f"
haftmann@32077
  1021
  by simp
haftmann@32077
  1022
haftmann@56014
  1023
lemma rangeE [elim?]:
haftmann@56014
  1024
  "b \<in> range (\<lambda>x. f x) \<Longrightarrow> (\<And>x. b = f x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@56014
  1025
  by (rule imageE)
haftmann@56014
  1026
haftmann@56014
  1027
lemma full_SetCompr_eq:
haftmann@56014
  1028
  "{u. \<exists>x. u = f x} = range f"
haftmann@56014
  1029
  by auto
haftmann@56014
  1030
lp15@59506
  1031
lemma range_composition:
haftmann@56014
  1032
  "range (\<lambda>x. f (g x)) = f ` range g"
haftmann@56077
  1033
  by auto
haftmann@56014
  1034
haftmann@32077
  1035
wenzelm@61799
  1036
subsubsection \<open>Some rules with \<open>if\<close>\<close>
wenzelm@61799
  1037
wenzelm@61799
  1038
text\<open>Elimination of \<open>{x. \<dots> & x=t & \<dots>}\<close>.\<close>
haftmann@32081
  1039
haftmann@32081
  1040
lemma Collect_conv_if: "{x. x=a & P x} = (if P a then {a} else {})"
haftmann@32117
  1041
  by auto
haftmann@32081
  1042
haftmann@32081
  1043
lemma Collect_conv_if2: "{x. a=x & P x} = (if P a then {a} else {})"
haftmann@32117
  1044
  by auto
haftmann@32081
  1045
wenzelm@60758
  1046
text \<open>
wenzelm@61799
  1047
  Rewrite rules for boolean case-splitting: faster than \<open>split_if [split]\<close>.
wenzelm@60758
  1048
\<close>
haftmann@32081
  1049
haftmann@32081
  1050
lemma split_if_eq1: "((if Q then x else y) = b) = ((Q --> x = b) & (~ Q --> y = b))"
haftmann@32081
  1051
  by (rule split_if)
haftmann@32081
  1052
haftmann@32081
  1053
lemma split_if_eq2: "(a = (if Q then x else y)) = ((Q --> a = x) & (~ Q --> a = y))"
haftmann@32081
  1054
  by (rule split_if)
haftmann@32081
  1055
wenzelm@60758
  1056
text \<open>
wenzelm@61799
  1057
  Split ifs on either side of the membership relation.  Not for \<open>[simp]\<close> -- can cause goals to blow up!
wenzelm@60758
  1058
\<close>
haftmann@32081
  1059
haftmann@32081
  1060
lemma split_if_mem1: "((if Q then x else y) : b) = ((Q --> x : b) & (~ Q --> y : b))"
haftmann@32081
  1061
  by (rule split_if)
haftmann@32081
  1062
haftmann@32081
  1063
lemma split_if_mem2: "(a : (if Q then x else y)) = ((Q --> a : x) & (~ Q --> a : y))"
haftmann@32081
  1064
  by (rule split_if [where P="%S. a : S"])
haftmann@32081
  1065
haftmann@32081
  1066
lemmas split_ifs = if_bool_eq_conj split_if_eq1 split_if_eq2 split_if_mem1 split_if_mem2
haftmann@32081
  1067
haftmann@32081
  1068
(*Would like to add these, but the existing code only searches for the
haftmann@37677
  1069
  outer-level constant, which in this case is just Set.member; we instead need
haftmann@32081
  1070
  to use term-nets to associate patterns with rules.  Also, if a rule fails to
haftmann@32081
  1071
  apply, then the formula should be kept.
haftmann@34974
  1072
  [("uminus", Compl_iff RS iffD1), ("minus", [Diff_iff RS iffD1]),
haftmann@32081
  1073
   ("Int", [IntD1,IntD2]),
haftmann@32081
  1074
   ("Collect", [CollectD]), ("Inter", [InterD]), ("INTER", [INT_D])]
haftmann@32081
  1075
 *)
haftmann@32081
  1076
haftmann@32081
  1077
wenzelm@60758
  1078
subsection \<open>Further operations and lemmas\<close>
wenzelm@60758
  1079
wenzelm@60758
  1080
subsubsection \<open>The ``proper subset'' relation\<close>
haftmann@32135
  1081
blanchet@54147
  1082
lemma psubsetI [intro!]: "A \<subseteq> B ==> A \<noteq> B ==> A \<subset> B"
haftmann@32135
  1083
  by (unfold less_le) blast
haftmann@32135
  1084
blanchet@54147
  1085
lemma psubsetE [elim!]:
haftmann@32135
  1086
    "[|A \<subset> B;  [|A \<subseteq> B; ~ (B\<subseteq>A)|] ==> R|] ==> R"
haftmann@32135
  1087
  by (unfold less_le) blast
haftmann@32135
  1088
haftmann@32135
  1089
lemma psubset_insert_iff:
haftmann@32135
  1090
  "(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
  1091
  by (auto simp add: less_le subset_insert_iff)
haftmann@32135
  1092
haftmann@32135
  1093
lemma psubset_eq: "(A \<subset> B) = (A \<subseteq> B & A \<noteq> B)"
haftmann@32135
  1094
  by (simp only: less_le)
haftmann@32135
  1095
haftmann@32135
  1096
lemma psubset_imp_subset: "A \<subset> B ==> A \<subseteq> B"
haftmann@32135
  1097
  by (simp add: psubset_eq)
haftmann@32135
  1098
haftmann@32135
  1099
lemma psubset_trans: "[| A \<subset> B; B \<subset> C |] ==> A \<subset> C"
haftmann@32135
  1100
apply (unfold less_le)
haftmann@32135
  1101
apply (auto dest: subset_antisym)
haftmann@32135
  1102
done
haftmann@32135
  1103
haftmann@32135
  1104
lemma psubsetD: "[| A \<subset> B; c \<in> A |] ==> c \<in> B"
haftmann@32135
  1105
apply (unfold less_le)
haftmann@32135
  1106
apply (auto dest: subsetD)
haftmann@32135
  1107
done
haftmann@32135
  1108
haftmann@32135
  1109
lemma psubset_subset_trans: "A \<subset> B ==> B \<subseteq> C ==> A \<subset> C"
haftmann@32135
  1110
  by (auto simp add: psubset_eq)
haftmann@32135
  1111
haftmann@32135
  1112
lemma subset_psubset_trans: "A \<subseteq> B ==> B \<subset> C ==> A \<subset> C"
haftmann@32135
  1113
  by (auto simp add: psubset_eq)
haftmann@32135
  1114
haftmann@32135
  1115
lemma psubset_imp_ex_mem: "A \<subset> B ==> \<exists>b. b \<in> (B - A)"
haftmann@32135
  1116
  by (unfold less_le) blast
haftmann@32135
  1117
haftmann@32135
  1118
lemma atomize_ball:
haftmann@32135
  1119
    "(!!x. x \<in> A ==> P x) == Trueprop (\<forall>x\<in>A. P x)"
haftmann@32135
  1120
  by (simp only: Ball_def atomize_all atomize_imp)
haftmann@32135
  1121
haftmann@32135
  1122
lemmas [symmetric, rulify] = atomize_ball
haftmann@32135
  1123
  and [symmetric, defn] = atomize_ball
haftmann@32135
  1124
hoelzl@40703
  1125
lemma image_Pow_mono:
haftmann@56014
  1126
  assumes "f ` A \<subseteq> B"
haftmann@56014
  1127
  shows "image f ` Pow A \<subseteq> Pow B"
haftmann@56014
  1128
  using assms by blast
hoelzl@40703
  1129
hoelzl@40703
  1130
lemma image_Pow_surj:
hoelzl@40703
  1131
  assumes "f ` A = B"
haftmann@56014
  1132
  shows "image f ` Pow A = Pow B"
haftmann@56014
  1133
  using assms by (blast elim: subset_imageE)
haftmann@56014
  1134
hoelzl@40703
  1135
wenzelm@60758
  1136
subsubsection \<open>Derived rules involving subsets.\<close>
wenzelm@60758
  1137
wenzelm@61799
  1138
text \<open>\<open>insert\<close>.\<close>
haftmann@32135
  1139
haftmann@32135
  1140
lemma subset_insertI: "B \<subseteq> insert a B"
haftmann@32135
  1141
  by (rule subsetI) (erule insertI2)
haftmann@32135
  1142
haftmann@32135
  1143
lemma subset_insertI2: "A \<subseteq> B \<Longrightarrow> A \<subseteq> insert b B"
haftmann@32135
  1144
  by blast
haftmann@32135
  1145
haftmann@32135
  1146
lemma subset_insert: "x \<notin> A ==> (A \<subseteq> insert x B) = (A \<subseteq> B)"
haftmann@32135
  1147
  by blast
haftmann@32135
  1148
haftmann@32135
  1149
wenzelm@60758
  1150
text \<open>\medskip Finite Union -- the least upper bound of two sets.\<close>
haftmann@32135
  1151
haftmann@32135
  1152
lemma Un_upper1: "A \<subseteq> A \<union> B"
huffman@36009
  1153
  by (fact sup_ge1)
haftmann@32135
  1154
haftmann@32135
  1155
lemma Un_upper2: "B \<subseteq> A \<union> B"
huffman@36009
  1156
  by (fact sup_ge2)
haftmann@32135
  1157
haftmann@32135
  1158
lemma Un_least: "A \<subseteq> C ==> B \<subseteq> C ==> A \<union> B \<subseteq> C"
huffman@36009
  1159
  by (fact sup_least)
haftmann@32135
  1160
haftmann@32135
  1161
wenzelm@60758
  1162
text \<open>\medskip Finite Intersection -- the greatest lower bound of two sets.\<close>
haftmann@32135
  1163
haftmann@32135
  1164
lemma Int_lower1: "A \<inter> B \<subseteq> A"
huffman@36009
  1165
  by (fact inf_le1)
haftmann@32135
  1166
haftmann@32135
  1167
lemma Int_lower2: "A \<inter> B \<subseteq> B"
huffman@36009
  1168
  by (fact inf_le2)
haftmann@32135
  1169
haftmann@32135
  1170
lemma Int_greatest: "C \<subseteq> A ==> C \<subseteq> B ==> C \<subseteq> A \<inter> B"
huffman@36009
  1171
  by (fact inf_greatest)
haftmann@32135
  1172
haftmann@32135
  1173
wenzelm@60758
  1174
text \<open>\medskip Set difference.\<close>
haftmann@32135
  1175
haftmann@32135
  1176
lemma Diff_subset: "A - B \<subseteq> A"
haftmann@32135
  1177
  by blast
haftmann@32135
  1178
haftmann@32135
  1179
lemma Diff_subset_conv: "(A - B \<subseteq> C) = (A \<subseteq> B \<union> C)"
haftmann@32135
  1180
by blast
haftmann@32135
  1181
haftmann@32135
  1182
wenzelm@60758
  1183
subsubsection \<open>Equalities involving union, intersection, inclusion, etc.\<close>
wenzelm@60758
  1184
wenzelm@61799
  1185
text \<open>\<open>{}\<close>.\<close>
haftmann@32135
  1186
haftmann@32135
  1187
lemma Collect_const [simp]: "{s. P} = (if P then UNIV else {})"
wenzelm@61799
  1188
  \<comment> \<open>supersedes \<open>Collect_False_empty\<close>\<close>
haftmann@32135
  1189
  by auto
haftmann@32135
  1190
haftmann@32135
  1191
lemma subset_empty [simp]: "(A \<subseteq> {}) = (A = {})"
huffman@45121
  1192
  by (fact bot_unique)
haftmann@32135
  1193
haftmann@32135
  1194
lemma not_psubset_empty [iff]: "\<not> (A < {})"
huffman@45121
  1195
  by (fact not_less_bot) (* FIXME: already simp *)
haftmann@32135
  1196
haftmann@32135
  1197
lemma Collect_empty_eq [simp]: "(Collect P = {}) = (\<forall>x. \<not> P x)"
haftmann@32135
  1198
by blast
haftmann@32135
  1199
haftmann@32135
  1200
lemma empty_Collect_eq [simp]: "({} = Collect P) = (\<forall>x. \<not> P x)"
haftmann@32135
  1201
by blast
haftmann@32135
  1202
haftmann@32135
  1203
lemma Collect_neg_eq: "{x. \<not> P x} = - {x. P x}"
haftmann@32135
  1204
  by blast
haftmann@32135
  1205
haftmann@32135
  1206
lemma Collect_disj_eq: "{x. P x | Q x} = {x. P x} \<union> {x. Q x}"
haftmann@32135
  1207
  by blast
haftmann@32135
  1208
haftmann@32135
  1209
lemma Collect_imp_eq: "{x. P x --> Q x} = -{x. P x} \<union> {x. Q x}"
haftmann@32135
  1210
  by blast
haftmann@32135
  1211
haftmann@32135
  1212
lemma Collect_conj_eq: "{x. P x & Q x} = {x. P x} \<inter> {x. Q x}"
haftmann@32135
  1213
  by blast
haftmann@32135
  1214
lp15@59506
  1215
lemma Collect_mono_iff: "Collect P \<subseteq> Collect Q \<longleftrightarrow> (\<forall>x. P x \<longrightarrow> Q x)"
lp15@59504
  1216
  by blast
lp15@59504
  1217
haftmann@32135
  1218
wenzelm@61799
  1219
text \<open>\medskip \<open>insert\<close>.\<close>
haftmann@32135
  1220
haftmann@32135
  1221
lemma insert_is_Un: "insert a A = {a} Un A"
wenzelm@61799
  1222
  \<comment> \<open>NOT SUITABLE FOR REWRITING since \<open>{a} == insert a {}\<close>\<close>
haftmann@32135
  1223
  by blast
haftmann@32135
  1224
haftmann@32135
  1225
lemma insert_not_empty [simp]: "insert a A \<noteq> {}"
haftmann@32135
  1226
  by blast
haftmann@32135
  1227
wenzelm@45607
  1228
lemmas empty_not_insert = insert_not_empty [symmetric]
haftmann@32135
  1229
declare empty_not_insert [simp]
haftmann@32135
  1230
haftmann@32135
  1231
lemma insert_absorb: "a \<in> A ==> insert a A = A"
wenzelm@61799
  1232
  \<comment> \<open>\<open>[simp]\<close> causes recursive calls when there are nested inserts\<close>
wenzelm@61799
  1233
  \<comment> \<open>with \emph{quadratic} running time\<close>
haftmann@32135
  1234
  by blast
haftmann@32135
  1235
haftmann@32135
  1236
lemma insert_absorb2 [simp]: "insert x (insert x A) = insert x A"
haftmann@32135
  1237
  by blast
haftmann@32135
  1238
haftmann@32135
  1239
lemma insert_commute: "insert x (insert y A) = insert y (insert x A)"
haftmann@32135
  1240
  by blast
haftmann@32135
  1241
haftmann@32135
  1242
lemma insert_subset [simp]: "(insert x A \<subseteq> B) = (x \<in> B & A \<subseteq> B)"
haftmann@32135
  1243
  by blast
haftmann@32135
  1244
haftmann@32135
  1245
lemma mk_disjoint_insert: "a \<in> A ==> \<exists>B. A = insert a B & a \<notin> B"
wenzelm@61799
  1246
  \<comment> \<open>use new \<open>B\<close> rather than \<open>A - {a}\<close> to avoid infinite unfolding\<close>
haftmann@32135
  1247
  apply (rule_tac x = "A - {a}" in exI, blast)
haftmann@32135
  1248
  done
haftmann@32135
  1249
haftmann@32135
  1250
lemma insert_Collect: "insert a (Collect P) = {u. u \<noteq> a --> P u}"
haftmann@32135
  1251
  by auto
haftmann@32135
  1252
haftmann@32135
  1253
lemma insert_inter_insert[simp]: "insert a A \<inter> insert a B = insert a (A \<inter> B)"
haftmann@32135
  1254
  by blast
haftmann@32135
  1255
blanchet@54147
  1256
lemma insert_disjoint [simp]:
haftmann@32135
  1257
 "(insert a A \<inter> B = {}) = (a \<notin> B \<and> A \<inter> B = {})"
haftmann@32135
  1258
 "({} = insert a A \<inter> B) = (a \<notin> B \<and> {} = A \<inter> B)"
haftmann@32135
  1259
  by auto
haftmann@32135
  1260
blanchet@54147
  1261
lemma disjoint_insert [simp]:
haftmann@32135
  1262
 "(B \<inter> insert a A = {}) = (a \<notin> B \<and> B \<inter> A = {})"
haftmann@32135
  1263
 "({} = A \<inter> insert b B) = (b \<notin> A \<and> {} = A \<inter> B)"
haftmann@32135
  1264
  by auto
haftmann@32135
  1265
haftmann@32135
  1266
wenzelm@61799
  1267
text \<open>\medskip \<open>Int\<close>\<close>
haftmann@32135
  1268
huffman@45121
  1269
lemma Int_absorb: "A \<inter> A = A"
huffman@45121
  1270
  by (fact inf_idem) (* already simp *)
haftmann@32135
  1271
haftmann@32135
  1272
lemma Int_left_absorb: "A \<inter> (A \<inter> B) = A \<inter> B"
huffman@36009
  1273
  by (fact inf_left_idem)
haftmann@32135
  1274
haftmann@32135
  1275
lemma Int_commute: "A \<inter> B = B \<inter> A"
huffman@36009
  1276
  by (fact inf_commute)
haftmann@32135
  1277
haftmann@32135
  1278
lemma Int_left_commute: "A \<inter> (B \<inter> C) = B \<inter> (A \<inter> C)"
huffman@36009
  1279
  by (fact inf_left_commute)
haftmann@32135
  1280
haftmann@32135
  1281
lemma Int_assoc: "(A \<inter> B) \<inter> C = A \<inter> (B \<inter> C)"
huffman@36009
  1282
  by (fact inf_assoc)
haftmann@32135
  1283
haftmann@32135
  1284
lemmas Int_ac = Int_assoc Int_left_absorb Int_commute Int_left_commute
wenzelm@61799
  1285
  \<comment> \<open>Intersection is an AC-operator\<close>
haftmann@32135
  1286
haftmann@32135
  1287
lemma Int_absorb1: "B \<subseteq> A ==> A \<inter> B = B"
huffman@36009
  1288
  by (fact inf_absorb2)
haftmann@32135
  1289
haftmann@32135
  1290
lemma Int_absorb2: "A \<subseteq> B ==> A \<inter> B = A"
huffman@36009
  1291
  by (fact inf_absorb1)
haftmann@32135
  1292
huffman@45121
  1293
lemma Int_empty_left: "{} \<inter> B = {}"
huffman@45121
  1294
  by (fact inf_bot_left) (* already simp *)
haftmann@32135
  1295
huffman@45121
  1296
lemma Int_empty_right: "A \<inter> {} = {}"
huffman@45121
  1297
  by (fact inf_bot_right) (* already simp *)
haftmann@32135
  1298
haftmann@32135
  1299
lemma disjoint_eq_subset_Compl: "(A \<inter> B = {}) = (A \<subseteq> -B)"
haftmann@32135
  1300
  by blast
haftmann@32135
  1301
haftmann@32135
  1302
lemma disjoint_iff_not_equal: "(A \<inter> B = {}) = (\<forall>x\<in>A. \<forall>y\<in>B. x \<noteq> y)"
haftmann@32135
  1303
  by blast
haftmann@32135
  1304
huffman@45121
  1305
lemma Int_UNIV_left: "UNIV \<inter> B = B"
huffman@45121
  1306
  by (fact inf_top_left) (* already simp *)
haftmann@32135
  1307
huffman@45121
  1308
lemma Int_UNIV_right: "A \<inter> UNIV = A"
huffman@45121
  1309
  by (fact inf_top_right) (* already simp *)
haftmann@32135
  1310
haftmann@32135
  1311
lemma Int_Un_distrib: "A \<inter> (B \<union> C) = (A \<inter> B) \<union> (A \<inter> C)"
huffman@36009
  1312
  by (fact inf_sup_distrib1)
haftmann@32135
  1313
haftmann@32135
  1314
lemma Int_Un_distrib2: "(B \<union> C) \<inter> A = (B \<inter> A) \<union> (C \<inter> A)"
huffman@36009
  1315
  by (fact inf_sup_distrib2)
haftmann@32135
  1316
blanchet@54147
  1317
lemma Int_UNIV [simp]: "(A \<inter> B = UNIV) = (A = UNIV & B = UNIV)"
huffman@45121
  1318
  by (fact inf_eq_top_iff) (* already simp *)
haftmann@32135
  1319
blanchet@54147
  1320
lemma Int_subset_iff [simp]: "(C \<subseteq> A \<inter> B) = (C \<subseteq> A & C \<subseteq> B)"
huffman@36009
  1321
  by (fact le_inf_iff)
haftmann@32135
  1322
haftmann@32135
  1323
lemma Int_Collect: "(x \<in> A \<inter> {x. P x}) = (x \<in> A & P x)"
haftmann@32135
  1324
  by blast
haftmann@32135
  1325
haftmann@32135
  1326
wenzelm@61799
  1327
text \<open>\medskip \<open>Un\<close>.\<close>
haftmann@32135
  1328
huffman@45121
  1329
lemma Un_absorb: "A \<union> A = A"
huffman@45121
  1330
  by (fact sup_idem) (* already simp *)
haftmann@32135
  1331
haftmann@32135
  1332
lemma Un_left_absorb: "A \<union> (A \<union> B) = A \<union> B"
huffman@36009
  1333
  by (fact sup_left_idem)
haftmann@32135
  1334
haftmann@32135
  1335
lemma Un_commute: "A \<union> B = B \<union> A"
huffman@36009
  1336
  by (fact sup_commute)
haftmann@32135
  1337
haftmann@32135
  1338
lemma Un_left_commute: "A \<union> (B \<union> C) = B \<union> (A \<union> C)"
huffman@36009
  1339
  by (fact sup_left_commute)
haftmann@32135
  1340
haftmann@32135
  1341
lemma Un_assoc: "(A \<union> B) \<union> C = A \<union> (B \<union> C)"
huffman@36009
  1342
  by (fact sup_assoc)
haftmann@32135
  1343
haftmann@32135
  1344
lemmas Un_ac = Un_assoc Un_left_absorb Un_commute Un_left_commute
wenzelm@61799
  1345
  \<comment> \<open>Union is an AC-operator\<close>
haftmann@32135
  1346
haftmann@32135
  1347
lemma Un_absorb1: "A \<subseteq> B ==> A \<union> B = B"
huffman@36009
  1348
  by (fact sup_absorb2)
haftmann@32135
  1349
haftmann@32135
  1350
lemma Un_absorb2: "B \<subseteq> A ==> A \<union> B = A"
huffman@36009
  1351
  by (fact sup_absorb1)
haftmann@32135
  1352
huffman@45121
  1353
lemma Un_empty_left: "{} \<union> B = B"
huffman@45121
  1354
  by (fact sup_bot_left) (* already simp *)
haftmann@32135
  1355
huffman@45121
  1356
lemma Un_empty_right: "A \<union> {} = A"
huffman@45121
  1357
  by (fact sup_bot_right) (* already simp *)
haftmann@32135
  1358
huffman@45121
  1359
lemma Un_UNIV_left: "UNIV \<union> B = UNIV"
huffman@45121
  1360
  by (fact sup_top_left) (* already simp *)
haftmann@32135
  1361
huffman@45121
  1362
lemma Un_UNIV_right: "A \<union> UNIV = UNIV"
huffman@45121
  1363
  by (fact sup_top_right) (* already simp *)
haftmann@32135
  1364
haftmann@32135
  1365
lemma Un_insert_left [simp]: "(insert a B) \<union> C = insert a (B \<union> C)"
haftmann@32135
  1366
  by blast
haftmann@32135
  1367
haftmann@32135
  1368
lemma Un_insert_right [simp]: "A \<union> (insert a B) = insert a (A \<union> B)"
haftmann@32135
  1369
  by blast
haftmann@32135
  1370
haftmann@32135
  1371
lemma Int_insert_left:
haftmann@32135
  1372
    "(insert a B) Int C = (if a \<in> C then insert a (B \<inter> C) else B \<inter> C)"
haftmann@32135
  1373
  by auto
haftmann@32135
  1374
nipkow@32456
  1375
lemma Int_insert_left_if0[simp]:
nipkow@32456
  1376
    "a \<notin> C \<Longrightarrow> (insert a B) Int C = B \<inter> C"
nipkow@32456
  1377
  by auto
nipkow@32456
  1378
nipkow@32456
  1379
lemma Int_insert_left_if1[simp]:
nipkow@32456
  1380
    "a \<in> C \<Longrightarrow> (insert a B) Int C = insert a (B Int C)"
nipkow@32456
  1381
  by auto
nipkow@32456
  1382
haftmann@32135
  1383
lemma Int_insert_right:
haftmann@32135
  1384
    "A \<inter> (insert a B) = (if a \<in> A then insert a (A \<inter> B) else A \<inter> B)"
haftmann@32135
  1385
  by auto
haftmann@32135
  1386
nipkow@32456
  1387
lemma Int_insert_right_if0[simp]:
nipkow@32456
  1388
    "a \<notin> A \<Longrightarrow> A Int (insert a B) = A Int B"
nipkow@32456
  1389
  by auto
nipkow@32456
  1390
nipkow@32456
  1391
lemma Int_insert_right_if1[simp]:
nipkow@32456
  1392
    "a \<in> A \<Longrightarrow> A Int (insert a B) = insert a (A Int B)"
nipkow@32456
  1393
  by auto
nipkow@32456
  1394
haftmann@32135
  1395
lemma Un_Int_distrib: "A \<union> (B \<inter> C) = (A \<union> B) \<inter> (A \<union> C)"
huffman@36009
  1396
  by (fact sup_inf_distrib1)
haftmann@32135
  1397
haftmann@32135
  1398
lemma Un_Int_distrib2: "(B \<inter> C) \<union> A = (B \<union> A) \<inter> (C \<union> A)"
huffman@36009
  1399
  by (fact sup_inf_distrib2)
haftmann@32135
  1400
haftmann@32135
  1401
lemma Un_Int_crazy:
haftmann@32135
  1402
    "(A \<inter> B) \<union> (B \<inter> C) \<union> (C \<inter> A) = (A \<union> B) \<inter> (B \<union> C) \<inter> (C \<union> A)"
haftmann@32135
  1403
  by blast
haftmann@32135
  1404
haftmann@32135
  1405
lemma subset_Un_eq: "(A \<subseteq> B) = (A \<union> B = B)"
huffman@36009
  1406
  by (fact le_iff_sup)
haftmann@32135
  1407
haftmann@32135
  1408
lemma Un_empty [iff]: "(A \<union> B = {}) = (A = {} & B = {})"
huffman@45121
  1409
  by (fact sup_eq_bot_iff) (* FIXME: already simp *)
haftmann@32135
  1410
blanchet@54147
  1411
lemma Un_subset_iff [simp]: "(A \<union> B \<subseteq> C) = (A \<subseteq> C & B \<subseteq> C)"
huffman@36009
  1412
  by (fact le_sup_iff)
haftmann@32135
  1413
haftmann@32135
  1414
lemma Un_Diff_Int: "(A - B) \<union> (A \<inter> B) = A"
haftmann@32135
  1415
  by blast
haftmann@32135
  1416
haftmann@32135
  1417
lemma Diff_Int2: "A \<inter> C - B \<inter> C = A \<inter> C - B"
haftmann@32135
  1418
  by blast
haftmann@32135
  1419
haftmann@32135
  1420
wenzelm@60758
  1421
text \<open>\medskip Set complement\<close>
haftmann@32135
  1422
haftmann@32135
  1423
lemma Compl_disjoint [simp]: "A \<inter> -A = {}"
huffman@36009
  1424
  by (fact inf_compl_bot)
haftmann@32135
  1425
haftmann@32135
  1426
lemma Compl_disjoint2 [simp]: "-A \<inter> A = {}"
huffman@36009
  1427
  by (fact compl_inf_bot)
haftmann@32135
  1428
haftmann@32135
  1429
lemma Compl_partition: "A \<union> -A = UNIV"
huffman@36009
  1430
  by (fact sup_compl_top)
haftmann@32135
  1431
haftmann@32135
  1432
lemma Compl_partition2: "-A \<union> A = UNIV"
huffman@36009
  1433
  by (fact compl_sup_top)
haftmann@32135
  1434
huffman@45121
  1435
lemma double_complement: "- (-A) = (A::'a set)"
huffman@45121
  1436
  by (fact double_compl) (* already simp *)
haftmann@32135
  1437
huffman@45121
  1438
lemma Compl_Un: "-(A \<union> B) = (-A) \<inter> (-B)"
huffman@45121
  1439
  by (fact compl_sup) (* already simp *)
haftmann@32135
  1440
huffman@45121
  1441
lemma Compl_Int: "-(A \<inter> B) = (-A) \<union> (-B)"
huffman@45121
  1442
  by (fact compl_inf) (* already simp *)
haftmann@32135
  1443
haftmann@32135
  1444
lemma subset_Compl_self_eq: "(A \<subseteq> -A) = (A = {})"
haftmann@32135
  1445
  by blast
haftmann@32135
  1446
haftmann@32135
  1447
lemma Un_Int_assoc_eq: "((A \<inter> B) \<union> C = A \<inter> (B \<union> C)) = (C \<subseteq> A)"
wenzelm@61799
  1448
  \<comment> \<open>Halmos, Naive Set Theory, page 16.\<close>
haftmann@32135
  1449
  by blast
haftmann@32135
  1450
huffman@45121
  1451
lemma Compl_UNIV_eq: "-UNIV = {}"
huffman@45121
  1452
  by (fact compl_top_eq) (* already simp *)
haftmann@32135
  1453
huffman@45121
  1454
lemma Compl_empty_eq: "-{} = UNIV"
huffman@45121
  1455
  by (fact compl_bot_eq) (* already simp *)
haftmann@32135
  1456
haftmann@32135
  1457
lemma Compl_subset_Compl_iff [iff]: "(-A \<subseteq> -B) = (B \<subseteq> A)"
huffman@45121
  1458
  by (fact compl_le_compl_iff) (* FIXME: already simp *)
haftmann@32135
  1459
haftmann@32135
  1460
lemma Compl_eq_Compl_iff [iff]: "(-A = -B) = (A = (B::'a set))"
huffman@45121
  1461
  by (fact compl_eq_compl_iff) (* FIXME: already simp *)
haftmann@32135
  1462
krauss@44490
  1463
lemma Compl_insert: "- insert x A = (-A) - {x}"
krauss@44490
  1464
  by blast
krauss@44490
  1465
wenzelm@60758
  1466
text \<open>\medskip Bounded quantifiers.
haftmann@32135
  1467
haftmann@32135
  1468
  The following are not added to the default simpset because
wenzelm@61799
  1469
  (a) they duplicate the body and (b) there are no similar rules for \<open>Int\<close>.\<close>
haftmann@32135
  1470
haftmann@32135
  1471
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
  1472
  by blast
haftmann@32135
  1473
haftmann@32135
  1474
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
  1475
  by blast
haftmann@32135
  1476
haftmann@32135
  1477
wenzelm@60758
  1478
text \<open>\medskip Set difference.\<close>
haftmann@32135
  1479
haftmann@32135
  1480
lemma Diff_eq: "A - B = A \<inter> (-B)"
haftmann@32135
  1481
  by blast
haftmann@32135
  1482
blanchet@54147
  1483
lemma Diff_eq_empty_iff [simp]: "(A - B = {}) = (A \<subseteq> B)"
haftmann@32135
  1484
  by blast
haftmann@32135
  1485
haftmann@32135
  1486
lemma Diff_cancel [simp]: "A - A = {}"
haftmann@32135
  1487
  by blast
haftmann@32135
  1488
haftmann@32135
  1489
lemma Diff_idemp [simp]: "(A - B) - B = A - (B::'a set)"
haftmann@32135
  1490
by blast
haftmann@32135
  1491
haftmann@32135
  1492
lemma Diff_triv: "A \<inter> B = {} ==> A - B = A"
haftmann@32135
  1493
  by (blast elim: equalityE)
haftmann@32135
  1494
haftmann@32135
  1495
lemma empty_Diff [simp]: "{} - A = {}"
haftmann@32135
  1496
  by blast
haftmann@32135
  1497
haftmann@32135
  1498
lemma Diff_empty [simp]: "A - {} = A"
haftmann@32135
  1499
  by blast
haftmann@32135
  1500
haftmann@32135
  1501
lemma Diff_UNIV [simp]: "A - UNIV = {}"
haftmann@32135
  1502
  by blast
haftmann@32135
  1503
blanchet@54147
  1504
lemma Diff_insert0 [simp]: "x \<notin> A ==> A - insert x B = A - B"
haftmann@32135
  1505
  by blast
haftmann@32135
  1506
haftmann@32135
  1507
lemma Diff_insert: "A - insert a B = A - B - {a}"
wenzelm@61799
  1508
  \<comment> \<open>NOT SUITABLE FOR REWRITING since \<open>{a} == insert a 0\<close>\<close>
haftmann@32135
  1509
  by blast
haftmann@32135
  1510
haftmann@32135
  1511
lemma Diff_insert2: "A - insert a B = A - {a} - B"
wenzelm@61799
  1512
  \<comment> \<open>NOT SUITABLE FOR REWRITING since \<open>{a} == insert a 0\<close>\<close>
haftmann@32135
  1513
  by blast
haftmann@32135
  1514
haftmann@32135
  1515
lemma insert_Diff_if: "insert x A - B = (if x \<in> B then A - B else insert x (A - B))"
haftmann@32135
  1516
  by auto
haftmann@32135
  1517
haftmann@32135
  1518
lemma insert_Diff1 [simp]: "x \<in> B ==> insert x A - B = A - B"
haftmann@32135
  1519
  by blast
haftmann@32135
  1520
haftmann@32135
  1521
lemma insert_Diff_single[simp]: "insert a (A - {a}) = insert a A"
haftmann@32135
  1522
by blast
haftmann@32135
  1523
haftmann@32135
  1524
lemma insert_Diff: "a \<in> A ==> insert a (A - {a}) = A"
haftmann@32135
  1525
  by blast
haftmann@32135
  1526
haftmann@32135
  1527
lemma Diff_insert_absorb: "x \<notin> A ==> (insert x A) - {x} = A"
haftmann@32135
  1528
  by auto
haftmann@32135
  1529
haftmann@32135
  1530
lemma Diff_disjoint [simp]: "A \<inter> (B - A) = {}"
haftmann@32135
  1531
  by blast
haftmann@32135
  1532
haftmann@32135
  1533
lemma Diff_partition: "A \<subseteq> B ==> A \<union> (B - A) = B"
haftmann@32135
  1534
  by blast
haftmann@32135
  1535
haftmann@32135
  1536
lemma double_diff: "A \<subseteq> B ==> B \<subseteq> C ==> B - (C - A) = A"
haftmann@32135
  1537
  by blast
haftmann@32135
  1538
haftmann@32135
  1539
lemma Un_Diff_cancel [simp]: "A \<union> (B - A) = A \<union> B"
haftmann@32135
  1540
  by blast
haftmann@32135
  1541
haftmann@32135
  1542
lemma Un_Diff_cancel2 [simp]: "(B - A) \<union> A = B \<union> A"
haftmann@32135
  1543
  by blast
haftmann@32135
  1544
haftmann@32135
  1545
lemma Diff_Un: "A - (B \<union> C) = (A - B) \<inter> (A - C)"
haftmann@32135
  1546
  by blast
haftmann@32135
  1547
haftmann@32135
  1548
lemma Diff_Int: "A - (B \<inter> C) = (A - B) \<union> (A - C)"
haftmann@32135
  1549
  by blast
haftmann@32135
  1550
paulson@61518
  1551
lemma Diff_Diff_Int: "A - (A - B) = A \<inter> B"
paulson@61518
  1552
  by blast
paulson@61518
  1553
haftmann@32135
  1554
lemma Un_Diff: "(A \<union> B) - C = (A - C) \<union> (B - C)"
haftmann@32135
  1555
  by blast
haftmann@32135
  1556
haftmann@32135
  1557
lemma Int_Diff: "(A \<inter> B) - C = A \<inter> (B - C)"
haftmann@32135
  1558
  by blast
haftmann@32135
  1559
haftmann@32135
  1560
lemma Diff_Int_distrib: "C \<inter> (A - B) = (C \<inter> A) - (C \<inter> B)"
haftmann@32135
  1561
  by blast
haftmann@32135
  1562
haftmann@32135
  1563
lemma Diff_Int_distrib2: "(A - B) \<inter> C = (A \<inter> C) - (B \<inter> C)"
haftmann@32135
  1564
  by blast
haftmann@32135
  1565
haftmann@32135
  1566
lemma Diff_Compl [simp]: "A - (- B) = A \<inter> B"
haftmann@32135
  1567
  by auto
haftmann@32135
  1568
haftmann@32135
  1569
lemma Compl_Diff_eq [simp]: "- (A - B) = -A \<union> B"
haftmann@32135
  1570
  by blast
haftmann@32135
  1571
haftmann@32135
  1572
wenzelm@60758
  1573
text \<open>\medskip Quantification over type @{typ bool}.\<close>
haftmann@32135
  1574
haftmann@32135
  1575
lemma bool_induct: "P True \<Longrightarrow> P False \<Longrightarrow> P x"
haftmann@32135
  1576
  by (cases x) auto
haftmann@32135
  1577
haftmann@32135
  1578
lemma all_bool_eq: "(\<forall>b. P b) \<longleftrightarrow> P True \<and> P False"
haftmann@32135
  1579
  by (auto intro: bool_induct)
haftmann@32135
  1580
haftmann@32135
  1581
lemma bool_contrapos: "P x \<Longrightarrow> \<not> P False \<Longrightarrow> P True"
haftmann@32135
  1582
  by (cases x) auto
haftmann@32135
  1583
haftmann@32135
  1584
lemma ex_bool_eq: "(\<exists>b. P b) \<longleftrightarrow> P True \<or> P False"
haftmann@32135
  1585
  by (auto intro: bool_contrapos)
haftmann@32135
  1586
blanchet@54147
  1587
lemma UNIV_bool: "UNIV = {False, True}"
haftmann@43866
  1588
  by (auto intro: bool_induct)
haftmann@43866
  1589
wenzelm@61799
  1590
text \<open>\medskip \<open>Pow\<close>\<close>
haftmann@32135
  1591
haftmann@32135
  1592
lemma Pow_empty [simp]: "Pow {} = {{}}"
haftmann@32135
  1593
  by (auto simp add: Pow_def)
haftmann@32135
  1594
nipkow@60161
  1595
lemma Pow_singleton_iff [simp]: "Pow X = {Y} \<longleftrightarrow> X = {} \<and> Y = {}"
nipkow@60161
  1596
by blast
nipkow@60161
  1597
haftmann@32135
  1598
lemma Pow_insert: "Pow (insert a A) = Pow A \<union> (insert a ` Pow A)"
wenzelm@55143
  1599
  by (blast intro: image_eqI [where ?x = "u - {a}" for u])
haftmann@32135
  1600
haftmann@32135
  1601
lemma Pow_Compl: "Pow (- A) = {-B | B. A \<in> Pow B}"
wenzelm@55143
  1602
  by (blast intro: exI [where ?x = "- u" for u])
haftmann@32135
  1603
haftmann@32135
  1604
lemma Pow_UNIV [simp]: "Pow UNIV = UNIV"
haftmann@32135
  1605
  by blast
haftmann@32135
  1606
haftmann@32135
  1607
lemma Un_Pow_subset: "Pow A \<union> Pow B \<subseteq> Pow (A \<union> B)"
haftmann@32135
  1608
  by blast
haftmann@32135
  1609
haftmann@32135
  1610
lemma Pow_Int_eq [simp]: "Pow (A \<inter> B) = Pow A \<inter> Pow B"
haftmann@32135
  1611
  by blast
haftmann@32135
  1612
haftmann@32135
  1613
wenzelm@60758
  1614
text \<open>\medskip Miscellany.\<close>
haftmann@32135
  1615
haftmann@32135
  1616
lemma set_eq_subset: "(A = B) = (A \<subseteq> B & B \<subseteq> A)"
haftmann@32135
  1617
  by blast
haftmann@32135
  1618
blanchet@54147
  1619
lemma subset_iff: "(A \<subseteq> B) = (\<forall>t. t \<in> A --> t \<in> B)"
haftmann@32135
  1620
  by blast
haftmann@32135
  1621
haftmann@32135
  1622
lemma subset_iff_psubset_eq: "(A \<subseteq> B) = ((A \<subset> B) | (A = B))"
haftmann@32135
  1623
  by (unfold less_le) blast
haftmann@32135
  1624
haftmann@32135
  1625
lemma all_not_in_conv [simp]: "(\<forall>x. x \<notin> A) = (A = {})"
haftmann@32135
  1626
  by blast
haftmann@32135
  1627
haftmann@32135
  1628
lemma ex_in_conv: "(\<exists>x. x \<in> A) = (A \<noteq> {})"
haftmann@32135
  1629
  by blast
haftmann@32135
  1630
haftmann@43967
  1631
lemma ball_simps [simp, no_atp]:
haftmann@43967
  1632
  "\<And>A P Q. (\<forall>x\<in>A. P x \<or> Q) \<longleftrightarrow> ((\<forall>x\<in>A. P x) \<or> Q)"
haftmann@43967
  1633
  "\<And>A P Q. (\<forall>x\<in>A. P \<or> Q x) \<longleftrightarrow> (P \<or> (\<forall>x\<in>A. Q x))"
haftmann@43967
  1634
  "\<And>A P Q. (\<forall>x\<in>A. P \<longrightarrow> Q x) \<longleftrightarrow> (P \<longrightarrow> (\<forall>x\<in>A. Q x))"
haftmann@43967
  1635
  "\<And>A P Q. (\<forall>x\<in>A. P x \<longrightarrow> Q) \<longleftrightarrow> ((\<exists>x\<in>A. P x) \<longrightarrow> Q)"
haftmann@43967
  1636
  "\<And>P. (\<forall>x\<in>{}. P x) \<longleftrightarrow> True"
haftmann@43967
  1637
  "\<And>P. (\<forall>x\<in>UNIV. P x) \<longleftrightarrow> (\<forall>x. P x)"
haftmann@43967
  1638
  "\<And>a B P. (\<forall>x\<in>insert a B. P x) \<longleftrightarrow> (P a \<and> (\<forall>x\<in>B. P x))"
haftmann@43967
  1639
  "\<And>P Q. (\<forall>x\<in>Collect Q. P x) \<longleftrightarrow> (\<forall>x. Q x \<longrightarrow> P x)"
haftmann@43967
  1640
  "\<And>A P f. (\<forall>x\<in>f`A. P x) \<longleftrightarrow> (\<forall>x\<in>A. P (f x))"
haftmann@43967
  1641
  "\<And>A P. (\<not> (\<forall>x\<in>A. P x)) \<longleftrightarrow> (\<exists>x\<in>A. \<not> P x)"
haftmann@43967
  1642
  by auto
haftmann@43967
  1643
haftmann@43967
  1644
lemma bex_simps [simp, no_atp]:
haftmann@43967
  1645
  "\<And>A P Q. (\<exists>x\<in>A. P x \<and> Q) \<longleftrightarrow> ((\<exists>x\<in>A. P x) \<and> Q)"
haftmann@43967
  1646
  "\<And>A P Q. (\<exists>x\<in>A. P \<and> Q x) \<longleftrightarrow> (P \<and> (\<exists>x\<in>A. Q x))"
haftmann@43967
  1647
  "\<And>P. (\<exists>x\<in>{}. P x) \<longleftrightarrow> False"
haftmann@43967
  1648
  "\<And>P. (\<exists>x\<in>UNIV. P x) \<longleftrightarrow> (\<exists>x. P x)"
haftmann@43967
  1649
  "\<And>a B P. (\<exists>x\<in>insert a B. P x) \<longleftrightarrow> (P a | (\<exists>x\<in>B. P x))"
haftmann@43967
  1650
  "\<And>P Q. (\<exists>x\<in>Collect Q. P x) \<longleftrightarrow> (\<exists>x. Q x \<and> P x)"
haftmann@43967
  1651
  "\<And>A P f. (\<exists>x\<in>f`A. P x) \<longleftrightarrow> (\<exists>x\<in>A. P (f x))"
haftmann@43967
  1652
  "\<And>A P. (\<not>(\<exists>x\<in>A. P x)) \<longleftrightarrow> (\<forall>x\<in>A. \<not> P x)"
haftmann@43967
  1653
  by auto
haftmann@43967
  1654
haftmann@32135
  1655
wenzelm@60758
  1656
subsubsection \<open>Monotonicity of various operations\<close>
haftmann@32135
  1657
haftmann@32135
  1658
lemma image_mono: "A \<subseteq> B ==> f`A \<subseteq> f`B"
haftmann@32135
  1659
  by blast
haftmann@32135
  1660
haftmann@32135
  1661
lemma Pow_mono: "A \<subseteq> B ==> Pow A \<subseteq> Pow B"
haftmann@32135
  1662
  by blast
haftmann@32135
  1663
haftmann@32135
  1664
lemma insert_mono: "C \<subseteq> D ==> insert a C \<subseteq> insert a D"
haftmann@32135
  1665
  by blast
haftmann@32135
  1666
haftmann@32135
  1667
lemma Un_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<union> B \<subseteq> C \<union> D"
huffman@36009
  1668
  by (fact sup_mono)
haftmann@32135
  1669
haftmann@32135
  1670
lemma Int_mono: "A \<subseteq> C ==> B \<subseteq> D ==> A \<inter> B \<subseteq> C \<inter> D"
huffman@36009
  1671
  by (fact inf_mono)
haftmann@32135
  1672
haftmann@32135
  1673
lemma Diff_mono: "A \<subseteq> C ==> D \<subseteq> B ==> A - B \<subseteq> C - D"
haftmann@32135
  1674
  by blast
haftmann@32135
  1675
haftmann@32135
  1676
lemma Compl_anti_mono: "A \<subseteq> B ==> -B \<subseteq> -A"
huffman@36009
  1677
  by (fact compl_mono)
haftmann@32135
  1678
wenzelm@60758
  1679
text \<open>\medskip Monotonicity of implications.\<close>
haftmann@32135
  1680
haftmann@32135
  1681
lemma in_mono: "A \<subseteq> B ==> x \<in> A --> x \<in> B"
haftmann@32135
  1682
  apply (rule impI)
haftmann@32135
  1683
  apply (erule subsetD, assumption)
haftmann@32135
  1684
  done
haftmann@32135
  1685
haftmann@32135
  1686
lemma conj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 & P2) --> (Q1 & Q2)"
haftmann@32135
  1687
  by iprover
haftmann@32135
  1688
haftmann@32135
  1689
lemma disj_mono: "P1 --> Q1 ==> P2 --> Q2 ==> (P1 | P2) --> (Q1 | Q2)"
haftmann@32135
  1690
  by iprover
haftmann@32135
  1691
haftmann@32135
  1692
lemma imp_mono: "Q1 --> P1 ==> P2 --> Q2 ==> (P1 --> P2) --> (Q1 --> Q2)"
haftmann@32135
  1693
  by iprover
haftmann@32135
  1694
haftmann@32135
  1695
lemma imp_refl: "P --> P" ..
haftmann@32135
  1696
berghofe@33935
  1697
lemma not_mono: "Q --> P ==> ~ P --> ~ Q"
berghofe@33935
  1698
  by iprover
berghofe@33935
  1699
haftmann@32135
  1700
lemma ex_mono: "(!!x. P x --> Q x) ==> (EX x. P x) --> (EX x. Q x)"
haftmann@32135
  1701
  by iprover
haftmann@32135
  1702
haftmann@32135
  1703
lemma all_mono: "(!!x. P x --> Q x) ==> (ALL x. P x) --> (ALL x. Q x)"
haftmann@32135
  1704
  by iprover
haftmann@32135
  1705
haftmann@32135
  1706
lemma Collect_mono: "(!!x. P x --> Q x) ==> Collect P \<subseteq> Collect Q"
haftmann@32135
  1707
  by blast
haftmann@32135
  1708
haftmann@32135
  1709
lemma Int_Collect_mono:
haftmann@32135
  1710
    "A \<subseteq> B ==> (!!x. x \<in> A ==> P x --> Q x) ==> A \<inter> Collect P \<subseteq> B \<inter> Collect Q"
haftmann@32135
  1711
  by blast
haftmann@32135
  1712
haftmann@32135
  1713
lemmas basic_monos =
haftmann@32135
  1714
  subset_refl imp_refl disj_mono conj_mono
haftmann@32135
  1715
  ex_mono Collect_mono in_mono
haftmann@32135
  1716
haftmann@32135
  1717
lemma eq_to_mono: "a = b ==> c = d ==> b --> d ==> a --> c"
haftmann@32135
  1718
  by iprover
haftmann@32135
  1719
haftmann@32135
  1720
wenzelm@60758
  1721
subsubsection \<open>Inverse image of a function\<close>
haftmann@32135
  1722
haftmann@35416
  1723
definition vimage :: "('a => 'b) => 'b set => 'a set" (infixr "-`" 90) where
haftmann@37767
  1724
  "f -` B == {x. f x : B}"
haftmann@32135
  1725
haftmann@32135
  1726
lemma vimage_eq [simp]: "(a : f -` B) = (f a : B)"
haftmann@32135
  1727
  by (unfold vimage_def) blast
haftmann@32135
  1728
haftmann@32135
  1729
lemma vimage_singleton_eq: "(a : f -` {b}) = (f a = b)"
haftmann@32135
  1730
  by simp
haftmann@32135
  1731
haftmann@32135
  1732
lemma vimageI [intro]: "f a = b ==> b:B ==> a : f -` B"
haftmann@32135
  1733
  by (unfold vimage_def) blast
haftmann@32135
  1734
haftmann@32135
  1735
lemma vimageI2: "f a : A ==> a : f -` A"
haftmann@32135
  1736
  by (unfold vimage_def) fast
haftmann@32135
  1737
haftmann@32135
  1738
lemma vimageE [elim!]: "a: f -` B ==> (!!x. f a = x ==> x:B ==> P) ==> P"
haftmann@32135
  1739
  by (unfold vimage_def) blast
haftmann@32135
  1740
haftmann@32135
  1741
lemma vimageD: "a : f -` A ==> f a : A"
haftmann@32135
  1742
  by (unfold vimage_def) fast
haftmann@32135
  1743
haftmann@32135
  1744
lemma vimage_empty [simp]: "f -` {} = {}"
haftmann@32135
  1745
  by blast
haftmann@32135
  1746
haftmann@32135
  1747
lemma vimage_Compl: "f -` (-A) = -(f -` A)"
haftmann@32135
  1748
  by blast
haftmann@32135
  1749
haftmann@32135
  1750
lemma vimage_Un [simp]: "f -` (A Un B) = (f -` A) Un (f -` B)"
haftmann@32135
  1751
  by blast
haftmann@32135
  1752
haftmann@32135
  1753
lemma vimage_Int [simp]: "f -` (A Int B) = (f -` A) Int (f -` B)"
haftmann@32135
  1754
  by fast
haftmann@32135
  1755
haftmann@32135
  1756
lemma vimage_Collect_eq [simp]: "f -` Collect P = {y. P (f y)}"
haftmann@32135
  1757
  by blast
haftmann@32135
  1758
haftmann@32135
  1759
lemma vimage_Collect: "(!!x. P (f x) = Q x) ==> f -` (Collect P) = Collect Q"
haftmann@32135
  1760
  by blast
haftmann@32135
  1761
haftmann@32135
  1762
lemma vimage_insert: "f-`(insert a B) = (f-`{a}) Un (f-`B)"
wenzelm@61799
  1763
  \<comment> \<open>NOT suitable for rewriting because of the recurrence of @{term "{a}"}.\<close>
haftmann@32135
  1764
  by blast
haftmann@32135
  1765
haftmann@32135
  1766
lemma vimage_Diff: "f -` (A - B) = (f -` A) - (f -` B)"
haftmann@32135
  1767
  by blast
haftmann@32135
  1768
haftmann@32135
  1769
lemma vimage_UNIV [simp]: "f -` UNIV = UNIV"
haftmann@32135
  1770
  by blast
haftmann@32135
  1771
haftmann@32135
  1772
lemma vimage_mono: "A \<subseteq> B ==> f -` A \<subseteq> f -` B"
wenzelm@61799
  1773
  \<comment> \<open>monotonicity\<close>
haftmann@32135
  1774
  by blast
haftmann@32135
  1775
blanchet@54147
  1776
lemma vimage_image_eq: "f -` (f ` A) = {y. EX x:A. f x = f y}"
haftmann@32135
  1777
by (blast intro: sym)
haftmann@32135
  1778
haftmann@32135
  1779
lemma image_vimage_subset: "f ` (f -` A) <= A"
haftmann@32135
  1780
by blast
haftmann@32135
  1781
haftmann@32135
  1782
lemma image_vimage_eq [simp]: "f ` (f -` A) = A Int range f"
haftmann@32135
  1783
by blast
haftmann@32135
  1784
lp15@55775
  1785
lemma image_subset_iff_subset_vimage: "f ` A \<subseteq> B \<longleftrightarrow> A \<subseteq> f -` B"
lp15@59506
  1786
  by blast
lp15@55775
  1787
paulson@33533
  1788
lemma vimage_const [simp]: "((\<lambda>x. c) -` A) = (if c \<in> A then UNIV else {})"
paulson@33533
  1789
  by auto
paulson@33533
  1790
wenzelm@52143
  1791
lemma vimage_if [simp]: "((\<lambda>x. if x \<in> B then c else d) -` A) =
paulson@33533
  1792
   (if c \<in> A then (if d \<in> A then UNIV else B)
wenzelm@52143
  1793
    else if d \<in> A then -B else {})"
wenzelm@52143
  1794
  by (auto simp add: vimage_def)
paulson@33533
  1795
hoelzl@35576
  1796
lemma vimage_inter_cong:
hoelzl@35576
  1797
  "(\<And> w. w \<in> S \<Longrightarrow> f w = g w) \<Longrightarrow> f -` y \<inter> S = g -` y \<inter> S"
hoelzl@35576
  1798
  by auto
hoelzl@35576
  1799
haftmann@43898
  1800
lemma vimage_ident [simp]: "(%x. x) -` Y = Y"
haftmann@43898
  1801
  by blast
haftmann@32135
  1802
haftmann@32135
  1803
wenzelm@60758
  1804
subsubsection \<open>Getting the Contents of a Singleton Set\<close>
haftmann@32135
  1805
haftmann@39910
  1806
definition the_elem :: "'a set \<Rightarrow> 'a" where
haftmann@39910
  1807
  "the_elem X = (THE x. X = {x})"
haftmann@32135
  1808
haftmann@39910
  1809
lemma the_elem_eq [simp]: "the_elem {x} = x"
haftmann@39910
  1810
  by (simp add: the_elem_def)
haftmann@32135
  1811
haftmann@56740
  1812
lemma the_elem_image_unique:
haftmann@56740
  1813
  assumes "A \<noteq> {}"
haftmann@56740
  1814
  assumes *: "\<And>y. y \<in> A \<Longrightarrow> f y = f x"
haftmann@56740
  1815
  shows "the_elem (f ` A) = f x"
haftmann@56740
  1816
unfolding the_elem_def proof (rule the1_equality)
wenzelm@60758
  1817
  from \<open>A \<noteq> {}\<close> obtain y where "y \<in> A" by auto
haftmann@56740
  1818
  with * have "f x = f y" by simp
wenzelm@60758
  1819
  with \<open>y \<in> A\<close> have "f x \<in> f ` A" by blast
haftmann@56740
  1820
  with * show "f ` A = {f x}" by auto
haftmann@56740
  1821
  then show "\<exists>!x. f ` A = {x}" by auto
haftmann@56740
  1822
qed
haftmann@56740
  1823
haftmann@32135
  1824
wenzelm@60758
  1825
subsubsection \<open>Least value operator\<close>
haftmann@32135
  1826
haftmann@32135
  1827
lemma Least_mono:
haftmann@32135
  1828
  "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y
haftmann@32135
  1829
    ==> (LEAST y. y : f ` S) = f (LEAST x. x : S)"
wenzelm@61799
  1830
    \<comment> \<open>Courtesy of Stephan Merz\<close>
haftmann@32135
  1831
  apply clarify
haftmann@32135
  1832
  apply (erule_tac P = "%x. x : S" in LeastI2_order, fast)
haftmann@32135
  1833
  apply (rule LeastI2_order)
haftmann@32135
  1834
  apply (auto elim: monoD intro!: order_antisym)
haftmann@32135
  1835
  done
haftmann@32135
  1836
haftmann@32135
  1837
wenzelm@60758
  1838
subsubsection \<open>Monad operation\<close>
haftmann@32135
  1839
haftmann@45959
  1840
definition bind :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b set) \<Rightarrow> 'b set" where
haftmann@45959
  1841
  "bind A f = {x. \<exists>B \<in> f`A. x \<in> B}"
haftmann@32135
  1842
haftmann@45959
  1843
hide_const (open) bind
haftmann@45959
  1844
haftmann@46036
  1845
lemma bind_bind:
haftmann@46036
  1846
  fixes A :: "'a set"
haftmann@46036
  1847
  shows "Set.bind (Set.bind A B) C = Set.bind A (\<lambda>x. Set.bind (B x) C)"
haftmann@46036
  1848
  by (auto simp add: bind_def)
haftmann@46036
  1849
haftmann@46036
  1850
lemma empty_bind [simp]:
haftmann@46128
  1851
  "Set.bind {} f = {}"
haftmann@46036
  1852
  by (simp add: bind_def)
haftmann@46036
  1853
haftmann@46036
  1854
lemma nonempty_bind_const:
haftmann@46036
  1855
  "A \<noteq> {} \<Longrightarrow> Set.bind A (\<lambda>_. B) = B"
haftmann@46036
  1856
  by (auto simp add: bind_def)
haftmann@46036
  1857
haftmann@46036
  1858
lemma bind_const: "Set.bind A (\<lambda>_. B) = (if A = {} then {} else B)"
haftmann@46036
  1859
  by (auto simp add: bind_def)
haftmann@46036
  1860
Andreas@60057
  1861
lemma bind_singleton_conv_image: "Set.bind A (\<lambda>x. {f x}) = f ` A"
Andreas@60057
  1862
  by(auto simp add: bind_def)
haftmann@45959
  1863
wenzelm@60758
  1864
subsubsection \<open>Operations for execution\<close>
haftmann@45986
  1865
haftmann@45986
  1866
definition is_empty :: "'a set \<Rightarrow> bool" where
haftmann@46127
  1867
  [code_abbrev]: "is_empty A \<longleftrightarrow> A = {}"
haftmann@45986
  1868
haftmann@45986
  1869
hide_const (open) is_empty
haftmann@45986
  1870
haftmann@45986
  1871
definition remove :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
haftmann@46127
  1872
  [code_abbrev]: "remove x A = A - {x}"
haftmann@45986
  1873
haftmann@45986
  1874
hide_const (open) remove
haftmann@45986
  1875
haftmann@46128
  1876
lemma member_remove [simp]:
haftmann@46128
  1877
  "x \<in> Set.remove y A \<longleftrightarrow> x \<in> A \<and> x \<noteq> y"
haftmann@46128
  1878
  by (simp add: remove_def)
haftmann@46128
  1879
kuncar@49757
  1880
definition filter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
kuncar@49757
  1881
  [code_abbrev]: "filter P A = {a \<in> A. P a}"
kuncar@49757
  1882
kuncar@49757
  1883
hide_const (open) filter
kuncar@49757
  1884
kuncar@49757
  1885
lemma member_filter [simp]:
kuncar@49757
  1886
  "x \<in> Set.filter P A \<longleftrightarrow> x \<in> A \<and> P x"
kuncar@49757
  1887
  by (simp add: filter_def)
haftmann@46128
  1888
haftmann@45986
  1889
instantiation set :: (equal) equal
haftmann@45986
  1890
begin
haftmann@45986
  1891
haftmann@45986
  1892
definition
haftmann@45986
  1893
  "HOL.equal A B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
haftmann@45986
  1894
haftmann@45986
  1895
instance proof
haftmann@45986
  1896
qed (auto simp add: equal_set_def)
haftmann@45986
  1897
haftmann@45986
  1898
end
haftmann@45986
  1899
haftmann@46127
  1900
wenzelm@60758
  1901
text \<open>Misc\<close>
haftmann@32135
  1902
lp15@61306
  1903
definition "pairwise R S \<longleftrightarrow> (\<forall>x \<in> S. \<forall>y\<in> S. x\<noteq>y \<longrightarrow> R x y)"
lp15@61306
  1904
haftmann@45152
  1905
hide_const (open) member not_member
haftmann@32135
  1906
haftmann@32135
  1907
lemmas equalityI = subset_antisym
haftmann@32135
  1908
wenzelm@60758
  1909
ML \<open>
haftmann@32135
  1910
val Ball_def = @{thm Ball_def}
haftmann@32135
  1911
val Bex_def = @{thm Bex_def}
haftmann@32135
  1912
val CollectD = @{thm CollectD}
haftmann@32135
  1913
val CollectE = @{thm CollectE}
haftmann@32135
  1914
val CollectI = @{thm CollectI}
haftmann@32135
  1915
val Collect_conj_eq = @{thm Collect_conj_eq}
haftmann@32135
  1916
val Collect_mem_eq = @{thm Collect_mem_eq}
haftmann@32135
  1917
val IntD1 = @{thm IntD1}
haftmann@32135
  1918
val IntD2 = @{thm IntD2}
haftmann@32135
  1919
val IntE = @{thm IntE}
haftmann@32135
  1920
val IntI = @{thm IntI}
haftmann@32135
  1921
val Int_Collect = @{thm Int_Collect}
haftmann@32135
  1922
val UNIV_I = @{thm UNIV_I}
haftmann@32135
  1923
val UNIV_witness = @{thm UNIV_witness}
haftmann@32135
  1924
val UnE = @{thm UnE}
haftmann@32135
  1925
val UnI1 = @{thm UnI1}
haftmann@32135
  1926
val UnI2 = @{thm UnI2}
haftmann@32135
  1927
val ballE = @{thm ballE}
haftmann@32135
  1928
val ballI = @{thm ballI}
haftmann@32135
  1929
val bexCI = @{thm bexCI}
haftmann@32135
  1930
val bexE = @{thm bexE}
haftmann@32135
  1931
val bexI = @{thm bexI}
haftmann@32135
  1932
val bex_triv = @{thm bex_triv}
haftmann@32135
  1933
val bspec = @{thm bspec}
haftmann@32135
  1934
val contra_subsetD = @{thm contra_subsetD}
haftmann@32135
  1935
val equalityCE = @{thm equalityCE}
haftmann@32135
  1936
val equalityD1 = @{thm equalityD1}
haftmann@32135
  1937
val equalityD2 = @{thm equalityD2}
haftmann@32135
  1938
val equalityE = @{thm equalityE}
haftmann@32135
  1939
val equalityI = @{thm equalityI}
haftmann@32135
  1940
val imageE = @{thm imageE}
haftmann@32135
  1941
val imageI = @{thm imageI}
haftmann@32135
  1942
val image_Un = @{thm image_Un}
haftmann@32135
  1943
val image_insert = @{thm image_insert}
haftmann@32135
  1944
val insert_commute = @{thm insert_commute}
haftmann@32135
  1945
val insert_iff = @{thm insert_iff}
haftmann@32135
  1946
val mem_Collect_eq = @{thm mem_Collect_eq}
haftmann@32135
  1947
val rangeE = @{thm rangeE}
haftmann@32135
  1948
val rangeI = @{thm rangeI}
haftmann@32135
  1949
val range_eqI = @{thm range_eqI}
haftmann@32135
  1950
val subsetCE = @{thm subsetCE}
haftmann@32135
  1951
val subsetD = @{thm subsetD}
haftmann@32135
  1952
val subsetI = @{thm subsetI}
haftmann@32135
  1953
val subset_refl = @{thm subset_refl}
haftmann@32135
  1954
val subset_trans = @{thm subset_trans}
haftmann@32135
  1955
val vimageD = @{thm vimageD}
haftmann@32135
  1956
val vimageE = @{thm vimageE}
haftmann@32135
  1957
val vimageI = @{thm vimageI}
haftmann@32135
  1958
val vimageI2 = @{thm vimageI2}
haftmann@32135
  1959
val vimage_Collect = @{thm vimage_Collect}
haftmann@32135
  1960
val vimage_Int = @{thm vimage_Int}
haftmann@32135
  1961
val vimage_Un = @{thm vimage_Un}
wenzelm@60758
  1962
\<close>
haftmann@32135
  1963
haftmann@32077
  1964
end
haftmann@46853
  1965