src/HOL/Library/SetsAndFunctions.thy
author berghofe
Wed May 07 10:59:24 2008 +0200 (2008-05-07)
changeset 26814 b3e8d5ec721d
parent 25764 878c37886eed
child 27368 9f90ac19e32b
permissions -rwxr-xr-x
Replaced + and * on sets by \<oplus> and \<otimes>, to avoid clash with
definitions of + and * on functions.
wenzelm@16932
     1
(*  Title:      HOL/Library/SetsAndFunctions.thy
wenzelm@19736
     2
    ID:         $Id$
avigad@16908
     3
    Author:     Jeremy Avigad and Kevin Donnelly
avigad@16908
     4
*)
avigad@16908
     5
avigad@16908
     6
header {* Operations on sets and functions *}
avigad@16908
     7
avigad@16908
     8
theory SetsAndFunctions
haftmann@25691
     9
imports ATP_Linkup
avigad@16908
    10
begin
avigad@16908
    11
wenzelm@19736
    12
text {*
avigad@16908
    13
This library lifts operations like addition and muliplication to sets and
avigad@16908
    14
functions of appropriate types. It was designed to support asymptotic
wenzelm@17161
    15
calculations. See the comments at the top of theory @{text BigO}.
avigad@16908
    16
*}
avigad@16908
    17
wenzelm@19736
    18
subsection {* Basic definitions *}
avigad@16908
    19
haftmann@25594
    20
definition
berghofe@26814
    21
  set_plus :: "('a::plus) set => 'a set => 'a set"  (infixl "\<oplus>" 65) where
berghofe@26814
    22
  "A \<oplus> B == {c. EX a:A. EX b:B. c = a + b}"
haftmann@25594
    23
haftmann@25594
    24
instantiation "fun" :: (type, plus) plus
haftmann@25594
    25
begin
avigad@16908
    26
haftmann@25594
    27
definition
haftmann@25594
    28
  func_plus: "f + g == (%x. f x + g x)"
haftmann@25594
    29
haftmann@25594
    30
instance ..
haftmann@25594
    31
haftmann@25594
    32
end
haftmann@25594
    33
haftmann@25594
    34
definition
berghofe@26814
    35
  set_times :: "('a::times) set => 'a set => 'a set"  (infixl "\<otimes>" 70) where
berghofe@26814
    36
  "A \<otimes> B == {c. EX a:A. EX b:B. c = a * b}"
haftmann@25594
    37
haftmann@25594
    38
instantiation "fun" :: (type, times) times
haftmann@25594
    39
begin
haftmann@25594
    40
haftmann@25594
    41
definition
haftmann@25594
    42
  func_times: "f * g == (%x. f x * g x)"
avigad@16908
    43
haftmann@25594
    44
instance ..
haftmann@25594
    45
haftmann@25594
    46
end
haftmann@25594
    47
haftmann@25594
    48
haftmann@25594
    49
instantiation "fun" :: (type, zero) zero
haftmann@25594
    50
begin
haftmann@25594
    51
haftmann@25594
    52
definition
haftmann@25594
    53
  func_zero: "0::(('a::type) => ('b::zero)) == %x. 0"
haftmann@25594
    54
haftmann@25594
    55
instance ..
haftmann@25594
    56
haftmann@25594
    57
end
haftmann@25594
    58
haftmann@25594
    59
instantiation "fun" :: (type, one) one
haftmann@25594
    60
begin
haftmann@25594
    61
haftmann@25594
    62
definition
avigad@16908
    63
  func_one: "1::(('a::type) => ('b::one)) == %x. 1"
haftmann@25594
    64
haftmann@25594
    65
instance ..
haftmann@25594
    66
haftmann@25594
    67
end
avigad@16908
    68
wenzelm@19736
    69
definition
wenzelm@21404
    70
  elt_set_plus :: "'a::plus => 'a set => 'a set"  (infixl "+o" 70) where
wenzelm@19736
    71
  "a +o B = {c. EX b:B. c = a + b}"
avigad@16908
    72
wenzelm@21404
    73
definition
wenzelm@21404
    74
  elt_set_times :: "'a::times => 'a set => 'a set"  (infixl "*o" 80) where
wenzelm@19736
    75
  "a *o B = {c. EX b:B. c = a * b}"
avigad@16908
    76
wenzelm@19656
    77
abbreviation (input)
wenzelm@21404
    78
  elt_set_eq :: "'a => 'a set => bool"  (infix "=o" 50) where
wenzelm@19380
    79
  "x =o A == x : A"
avigad@16908
    80
krauss@20523
    81
instance "fun" :: (type,semigroup_add)semigroup_add
wenzelm@19380
    82
  by default (auto simp add: func_plus add_assoc)
avigad@16908
    83
krauss@20523
    84
instance "fun" :: (type,comm_monoid_add)comm_monoid_add
wenzelm@19380
    85
  by default (auto simp add: func_zero func_plus add_ac)
avigad@16908
    86
krauss@20523
    87
instance "fun" :: (type,ab_group_add)ab_group_add
wenzelm@19736
    88
  apply default
berghofe@26814
    89
   apply (simp add: fun_Compl_def func_plus func_zero)
berghofe@26814
    90
  apply (simp add: fun_Compl_def func_plus fun_diff_def diff_minus)
wenzelm@19736
    91
  done
avigad@16908
    92
krauss@20523
    93
instance "fun" :: (type,semigroup_mult)semigroup_mult
wenzelm@19736
    94
  apply default
avigad@16908
    95
  apply (auto simp add: func_times mult_assoc)
wenzelm@19736
    96
  done
avigad@16908
    97
krauss@20523
    98
instance "fun" :: (type,comm_monoid_mult)comm_monoid_mult
wenzelm@19736
    99
  apply default
wenzelm@19736
   100
   apply (auto simp add: func_one func_times mult_ac)
wenzelm@19736
   101
  done
avigad@16908
   102
krauss@20523
   103
instance "fun" :: (type,comm_ring_1)comm_ring_1
wenzelm@19736
   104
  apply default
berghofe@26814
   105
   apply (auto simp add: func_plus func_times fun_Compl_def fun_diff_def ext
nipkow@23477
   106
     func_one func_zero ring_simps)
avigad@16908
   107
  apply (drule fun_cong)
avigad@16908
   108
  apply simp
wenzelm@19736
   109
  done
avigad@16908
   110
berghofe@26814
   111
interpretation set_semigroup_add: semigroup_add ["op \<oplus> :: ('a::semigroup_add) set => 'a set => 'a set"]
wenzelm@19736
   112
  apply default
berghofe@26814
   113
  apply (unfold set_plus_def)
avigad@16908
   114
  apply (force simp add: add_assoc)
wenzelm@19736
   115
  done
avigad@16908
   116
berghofe@26814
   117
interpretation set_semigroup_mult: semigroup_mult ["op \<otimes> :: ('a::semigroup_mult) set => 'a set => 'a set"]
wenzelm@19736
   118
  apply default
berghofe@26814
   119
  apply (unfold set_times_def)
avigad@16908
   120
  apply (force simp add: mult_assoc)
wenzelm@19736
   121
  done
avigad@16908
   122
berghofe@26814
   123
interpretation set_comm_monoid_add: comm_monoid_add ["{0}" "op \<oplus> :: ('a::comm_monoid_add) set => 'a set => 'a set"]
wenzelm@19736
   124
  apply default
berghofe@26814
   125
   apply (unfold set_plus_def)
wenzelm@19736
   126
   apply (force simp add: add_ac)
avigad@16908
   127
  apply force
wenzelm@19736
   128
  done
avigad@16908
   129
berghofe@26814
   130
interpretation set_comm_monoid_mult: comm_monoid_mult ["{1}" "op \<otimes> :: ('a::comm_monoid_mult) set => 'a set => 'a set"]
wenzelm@19736
   131
  apply default
berghofe@26814
   132
   apply (unfold set_times_def)
wenzelm@19736
   133
   apply (force simp add: mult_ac)
avigad@16908
   134
  apply force
wenzelm@19736
   135
  done
wenzelm@19736
   136
avigad@16908
   137
avigad@16908
   138
subsection {* Basic properties *}
avigad@16908
   139
berghofe@26814
   140
lemma set_plus_intro [intro]: "a : C ==> b : D ==> a + b : C \<oplus> D"
berghofe@26814
   141
  by (auto simp add: set_plus_def)
avigad@16908
   142
avigad@16908
   143
lemma set_plus_intro2 [intro]: "b : C ==> a + b : a +o C"
wenzelm@19736
   144
  by (auto simp add: elt_set_plus_def)
avigad@16908
   145
berghofe@26814
   146
lemma set_plus_rearrange: "((a::'a::comm_monoid_add) +o C) \<oplus>
berghofe@26814
   147
    (b +o D) = (a + b) +o (C \<oplus> D)"
berghofe@26814
   148
  apply (auto simp add: elt_set_plus_def set_plus_def add_ac)
wenzelm@19736
   149
   apply (rule_tac x = "ba + bb" in exI)
avigad@16908
   150
  apply (auto simp add: add_ac)
avigad@16908
   151
  apply (rule_tac x = "aa + a" in exI)
avigad@16908
   152
  apply (auto simp add: add_ac)
wenzelm@19736
   153
  done
avigad@16908
   154
wenzelm@19736
   155
lemma set_plus_rearrange2: "(a::'a::semigroup_add) +o (b +o C) =
wenzelm@19736
   156
    (a + b) +o C"
wenzelm@19736
   157
  by (auto simp add: elt_set_plus_def add_assoc)
avigad@16908
   158
berghofe@26814
   159
lemma set_plus_rearrange3: "((a::'a::semigroup_add) +o B) \<oplus> C =
berghofe@26814
   160
    a +o (B \<oplus> C)"
berghofe@26814
   161
  apply (auto simp add: elt_set_plus_def set_plus_def)
wenzelm@19736
   162
   apply (blast intro: add_ac)
avigad@16908
   163
  apply (rule_tac x = "a + aa" in exI)
avigad@16908
   164
  apply (rule conjI)
wenzelm@19736
   165
   apply (rule_tac x = "aa" in bexI)
wenzelm@19736
   166
    apply auto
avigad@16908
   167
  apply (rule_tac x = "ba" in bexI)
wenzelm@19736
   168
   apply (auto simp add: add_ac)
wenzelm@19736
   169
  done
avigad@16908
   170
berghofe@26814
   171
theorem set_plus_rearrange4: "C \<oplus> ((a::'a::comm_monoid_add) +o D) =
berghofe@26814
   172
    a +o (C \<oplus> D)"
berghofe@26814
   173
  apply (auto intro!: subsetI simp add: elt_set_plus_def set_plus_def add_ac)
wenzelm@19736
   174
   apply (rule_tac x = "aa + ba" in exI)
wenzelm@19736
   175
   apply (auto simp add: add_ac)
wenzelm@19736
   176
  done
avigad@16908
   177
avigad@16908
   178
theorems set_plus_rearranges = set_plus_rearrange set_plus_rearrange2
avigad@16908
   179
  set_plus_rearrange3 set_plus_rearrange4
avigad@16908
   180
avigad@16908
   181
lemma set_plus_mono [intro!]: "C <= D ==> a +o C <= a +o D"
wenzelm@19736
   182
  by (auto simp add: elt_set_plus_def)
avigad@16908
   183
wenzelm@19736
   184
lemma set_plus_mono2 [intro]: "(C::('a::plus) set) <= D ==> E <= F ==>
berghofe@26814
   185
    C \<oplus> E <= D \<oplus> F"
berghofe@26814
   186
  by (auto simp add: set_plus_def)
avigad@16908
   187
berghofe@26814
   188
lemma set_plus_mono3 [intro]: "a : C ==> a +o D <= C \<oplus> D"
berghofe@26814
   189
  by (auto simp add: elt_set_plus_def set_plus_def)
avigad@16908
   190
wenzelm@19736
   191
lemma set_plus_mono4 [intro]: "(a::'a::comm_monoid_add) : C ==>
berghofe@26814
   192
    a +o D <= D \<oplus> C"
berghofe@26814
   193
  by (auto simp add: elt_set_plus_def set_plus_def add_ac)
avigad@16908
   194
berghofe@26814
   195
lemma set_plus_mono5: "a:C ==> B <= D ==> a +o B <= C \<oplus> D"
avigad@16908
   196
  apply (subgoal_tac "a +o B <= a +o D")
wenzelm@19736
   197
   apply (erule order_trans)
wenzelm@19736
   198
   apply (erule set_plus_mono3)
avigad@16908
   199
  apply (erule set_plus_mono)
wenzelm@19736
   200
  done
avigad@16908
   201
wenzelm@19736
   202
lemma set_plus_mono_b: "C <= D ==> x : a +o C
avigad@16908
   203
    ==> x : a +o D"
avigad@16908
   204
  apply (frule set_plus_mono)
avigad@16908
   205
  apply auto
wenzelm@19736
   206
  done
avigad@16908
   207
berghofe@26814
   208
lemma set_plus_mono2_b: "C <= D ==> E <= F ==> x : C \<oplus> E ==>
berghofe@26814
   209
    x : D \<oplus> F"
avigad@16908
   210
  apply (frule set_plus_mono2)
wenzelm@19736
   211
   prefer 2
wenzelm@19736
   212
   apply force
avigad@16908
   213
  apply assumption
wenzelm@19736
   214
  done
avigad@16908
   215
berghofe@26814
   216
lemma set_plus_mono3_b: "a : C ==> x : a +o D ==> x : C \<oplus> D"
avigad@16908
   217
  apply (frule set_plus_mono3)
avigad@16908
   218
  apply auto
wenzelm@19736
   219
  done
avigad@16908
   220
wenzelm@19736
   221
lemma set_plus_mono4_b: "(a::'a::comm_monoid_add) : C ==>
berghofe@26814
   222
    x : a +o D ==> x : D \<oplus> C"
avigad@16908
   223
  apply (frule set_plus_mono4)
avigad@16908
   224
  apply auto
wenzelm@19736
   225
  done
avigad@16908
   226
avigad@16908
   227
lemma set_zero_plus [simp]: "(0::'a::comm_monoid_add) +o C = C"
wenzelm@19736
   228
  by (auto simp add: elt_set_plus_def)
avigad@16908
   229
berghofe@26814
   230
lemma set_zero_plus2: "(0::'a::comm_monoid_add) : A ==> B <= A \<oplus> B"
berghofe@26814
   231
  apply (auto intro!: subsetI simp add: set_plus_def)
avigad@16908
   232
  apply (rule_tac x = 0 in bexI)
wenzelm@19736
   233
   apply (rule_tac x = x in bexI)
wenzelm@19736
   234
    apply (auto simp add: add_ac)
wenzelm@19736
   235
  done
avigad@16908
   236
avigad@16908
   237
lemma set_plus_imp_minus: "(a::'a::ab_group_add) : b +o C ==> (a - b) : C"
wenzelm@19736
   238
  by (auto simp add: elt_set_plus_def add_ac diff_minus)
avigad@16908
   239
avigad@16908
   240
lemma set_minus_imp_plus: "(a::'a::ab_group_add) - b : C ==> a : b +o C"
avigad@16908
   241
  apply (auto simp add: elt_set_plus_def add_ac diff_minus)
avigad@16908
   242
  apply (subgoal_tac "a = (a + - b) + b")
wenzelm@19736
   243
   apply (rule bexI, assumption, assumption)
avigad@16908
   244
  apply (auto simp add: add_ac)
wenzelm@19736
   245
  done
avigad@16908
   246
avigad@16908
   247
lemma set_minus_plus: "((a::'a::ab_group_add) - b : C) = (a : b +o C)"
wenzelm@19736
   248
  by (rule iffI, rule set_minus_imp_plus, assumption, rule set_plus_imp_minus,
avigad@16908
   249
    assumption)
avigad@16908
   250
berghofe@26814
   251
lemma set_times_intro [intro]: "a : C ==> b : D ==> a * b : C \<otimes> D"
berghofe@26814
   252
  by (auto simp add: set_times_def)
avigad@16908
   253
avigad@16908
   254
lemma set_times_intro2 [intro!]: "b : C ==> a * b : a *o C"
wenzelm@19736
   255
  by (auto simp add: elt_set_times_def)
avigad@16908
   256
berghofe@26814
   257
lemma set_times_rearrange: "((a::'a::comm_monoid_mult) *o C) \<otimes>
berghofe@26814
   258
    (b *o D) = (a * b) *o (C \<otimes> D)"
berghofe@26814
   259
  apply (auto simp add: elt_set_times_def set_times_def)
wenzelm@19736
   260
   apply (rule_tac x = "ba * bb" in exI)
wenzelm@19736
   261
   apply (auto simp add: mult_ac)
avigad@16908
   262
  apply (rule_tac x = "aa * a" in exI)
avigad@16908
   263
  apply (auto simp add: mult_ac)
wenzelm@19736
   264
  done
avigad@16908
   265
wenzelm@19736
   266
lemma set_times_rearrange2: "(a::'a::semigroup_mult) *o (b *o C) =
wenzelm@19736
   267
    (a * b) *o C"
wenzelm@19736
   268
  by (auto simp add: elt_set_times_def mult_assoc)
avigad@16908
   269
berghofe@26814
   270
lemma set_times_rearrange3: "((a::'a::semigroup_mult) *o B) \<otimes> C =
berghofe@26814
   271
    a *o (B \<otimes> C)"
berghofe@26814
   272
  apply (auto simp add: elt_set_times_def set_times_def)
wenzelm@19736
   273
   apply (blast intro: mult_ac)
avigad@16908
   274
  apply (rule_tac x = "a * aa" in exI)
avigad@16908
   275
  apply (rule conjI)
wenzelm@19736
   276
   apply (rule_tac x = "aa" in bexI)
wenzelm@19736
   277
    apply auto
avigad@16908
   278
  apply (rule_tac x = "ba" in bexI)
wenzelm@19736
   279
   apply (auto simp add: mult_ac)
wenzelm@19736
   280
  done
avigad@16908
   281
berghofe@26814
   282
theorem set_times_rearrange4: "C \<otimes> ((a::'a::comm_monoid_mult) *o D) =
berghofe@26814
   283
    a *o (C \<otimes> D)"
berghofe@26814
   284
  apply (auto intro!: subsetI simp add: elt_set_times_def set_times_def
avigad@16908
   285
    mult_ac)
wenzelm@19736
   286
   apply (rule_tac x = "aa * ba" in exI)
wenzelm@19736
   287
   apply (auto simp add: mult_ac)
wenzelm@19736
   288
  done
avigad@16908
   289
avigad@16908
   290
theorems set_times_rearranges = set_times_rearrange set_times_rearrange2
avigad@16908
   291
  set_times_rearrange3 set_times_rearrange4
avigad@16908
   292
avigad@16908
   293
lemma set_times_mono [intro]: "C <= D ==> a *o C <= a *o D"
wenzelm@19736
   294
  by (auto simp add: elt_set_times_def)
avigad@16908
   295
wenzelm@19736
   296
lemma set_times_mono2 [intro]: "(C::('a::times) set) <= D ==> E <= F ==>
berghofe@26814
   297
    C \<otimes> E <= D \<otimes> F"
berghofe@26814
   298
  by (auto simp add: set_times_def)
avigad@16908
   299
berghofe@26814
   300
lemma set_times_mono3 [intro]: "a : C ==> a *o D <= C \<otimes> D"
berghofe@26814
   301
  by (auto simp add: elt_set_times_def set_times_def)
avigad@16908
   302
wenzelm@19736
   303
lemma set_times_mono4 [intro]: "(a::'a::comm_monoid_mult) : C ==>
berghofe@26814
   304
    a *o D <= D \<otimes> C"
berghofe@26814
   305
  by (auto simp add: elt_set_times_def set_times_def mult_ac)
avigad@16908
   306
berghofe@26814
   307
lemma set_times_mono5: "a:C ==> B <= D ==> a *o B <= C \<otimes> D"
avigad@16908
   308
  apply (subgoal_tac "a *o B <= a *o D")
wenzelm@19736
   309
   apply (erule order_trans)
wenzelm@19736
   310
   apply (erule set_times_mono3)
avigad@16908
   311
  apply (erule set_times_mono)
wenzelm@19736
   312
  done
avigad@16908
   313
wenzelm@19736
   314
lemma set_times_mono_b: "C <= D ==> x : a *o C
avigad@16908
   315
    ==> x : a *o D"
avigad@16908
   316
  apply (frule set_times_mono)
avigad@16908
   317
  apply auto
wenzelm@19736
   318
  done
avigad@16908
   319
berghofe@26814
   320
lemma set_times_mono2_b: "C <= D ==> E <= F ==> x : C \<otimes> E ==>
berghofe@26814
   321
    x : D \<otimes> F"
avigad@16908
   322
  apply (frule set_times_mono2)
wenzelm@19736
   323
   prefer 2
wenzelm@19736
   324
   apply force
avigad@16908
   325
  apply assumption
wenzelm@19736
   326
  done
avigad@16908
   327
berghofe@26814
   328
lemma set_times_mono3_b: "a : C ==> x : a *o D ==> x : C \<otimes> D"
avigad@16908
   329
  apply (frule set_times_mono3)
avigad@16908
   330
  apply auto
wenzelm@19736
   331
  done
avigad@16908
   332
wenzelm@19736
   333
lemma set_times_mono4_b: "(a::'a::comm_monoid_mult) : C ==>
berghofe@26814
   334
    x : a *o D ==> x : D \<otimes> C"
avigad@16908
   335
  apply (frule set_times_mono4)
avigad@16908
   336
  apply auto
wenzelm@19736
   337
  done
avigad@16908
   338
avigad@16908
   339
lemma set_one_times [simp]: "(1::'a::comm_monoid_mult) *o C = C"
wenzelm@19736
   340
  by (auto simp add: elt_set_times_def)
avigad@16908
   341
wenzelm@19736
   342
lemma set_times_plus_distrib: "(a::'a::semiring) *o (b +o C)=
wenzelm@19736
   343
    (a * b) +o (a *o C)"
nipkow@23477
   344
  by (auto simp add: elt_set_plus_def elt_set_times_def ring_distribs)
avigad@16908
   345
berghofe@26814
   346
lemma set_times_plus_distrib2: "(a::'a::semiring) *o (B \<oplus> C) =
berghofe@26814
   347
    (a *o B) \<oplus> (a *o C)"
berghofe@26814
   348
  apply (auto simp add: set_plus_def elt_set_times_def ring_distribs)
wenzelm@19736
   349
   apply blast
avigad@16908
   350
  apply (rule_tac x = "b + bb" in exI)
nipkow@23477
   351
  apply (auto simp add: ring_distribs)
wenzelm@19736
   352
  done
avigad@16908
   353
berghofe@26814
   354
lemma set_times_plus_distrib3: "((a::'a::semiring) +o C) \<otimes> D <=
berghofe@26814
   355
    a *o D \<oplus> C \<otimes> D"
wenzelm@19736
   356
  apply (auto intro!: subsetI simp add:
berghofe@26814
   357
    elt_set_plus_def elt_set_times_def set_times_def
berghofe@26814
   358
    set_plus_def ring_distribs)
avigad@16908
   359
  apply auto
wenzelm@19736
   360
  done
avigad@16908
   361
wenzelm@19380
   362
theorems set_times_plus_distribs =
wenzelm@19380
   363
  set_times_plus_distrib
avigad@16908
   364
  set_times_plus_distrib2
avigad@16908
   365
wenzelm@19736
   366
lemma set_neg_intro: "(a::'a::ring_1) : (- 1) *o C ==>
wenzelm@19736
   367
    - a : C"
wenzelm@19736
   368
  by (auto simp add: elt_set_times_def)
avigad@16908
   369
avigad@16908
   370
lemma set_neg_intro2: "(a::'a::ring_1) : C ==>
avigad@16908
   371
    - a : (- 1) *o C"
wenzelm@19736
   372
  by (auto simp add: elt_set_times_def)
wenzelm@19736
   373
avigad@16908
   374
end