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