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