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