src/HOL/Library/SetsAndFunctions.thy
author chaieb
Mon Jun 11 11:06:04 2007 +0200 (2007-06-11)
changeset 23315 df3a7e9ebadb
parent 21404 eb85850d3eb7
child 23477 f4b83f03cac9
permissions -rwxr-xr-x
tuned Proof
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
avigad@16908
     9
imports Main
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
wenzelm@17161
    20
instance set :: (plus) plus ..
krauss@20523
    21
instance "fun" :: (type, plus) plus ..
avigad@16908
    22
avigad@16908
    23
defs (overloaded)
avigad@16908
    24
  func_plus: "f + g == (%x. f x + g x)"
avigad@16908
    25
  set_plus: "A + B == {c. EX a:A. EX b:B. c = a + b}"
avigad@16908
    26
wenzelm@17161
    27
instance set :: (times) times ..
krauss@20523
    28
instance "fun" :: (type, times) times ..
avigad@16908
    29
avigad@16908
    30
defs (overloaded)
wenzelm@19736
    31
  func_times: "f * g == (%x. f x * g x)"
avigad@16908
    32
  set_times:"A * B == {c. EX a:A. EX b:B. c = a * b}"
avigad@16908
    33
krauss@20523
    34
instance "fun" :: (type, minus) minus ..
avigad@16908
    35
avigad@16908
    36
defs (overloaded)
avigad@16908
    37
  func_minus: "- f == (%x. - f x)"
wenzelm@19736
    38
  func_diff: "f - g == %x. f x - g x"
avigad@16908
    39
krauss@20523
    40
instance "fun" :: (type, zero) zero ..
wenzelm@17161
    41
instance set :: (zero) zero ..
avigad@16908
    42
avigad@16908
    43
defs (overloaded)
avigad@16908
    44
  func_zero: "0::(('a::type) => ('b::zero)) == %x. 0"
avigad@16908
    45
  set_zero: "0::('a::zero)set == {0}"
avigad@16908
    46
krauss@20523
    47
instance "fun" :: (type, one) one ..
wenzelm@17161
    48
instance set :: (one) one ..
avigad@16908
    49
avigad@16908
    50
defs (overloaded)
avigad@16908
    51
  func_one: "1::(('a::type) => ('b::one)) == %x. 1"
avigad@16908
    52
  set_one: "1::('a::one)set == {1}"
avigad@16908
    53
wenzelm@19736
    54
definition
wenzelm@21404
    55
  elt_set_plus :: "'a::plus => 'a set => 'a set"  (infixl "+o" 70) where
wenzelm@19736
    56
  "a +o B = {c. EX b:B. c = a + b}"
avigad@16908
    57
wenzelm@21404
    58
definition
wenzelm@21404
    59
  elt_set_times :: "'a::times => 'a set => 'a set"  (infixl "*o" 80) where
wenzelm@19736
    60
  "a *o B = {c. EX b:B. c = a * b}"
avigad@16908
    61
wenzelm@19656
    62
abbreviation (input)
wenzelm@21404
    63
  elt_set_eq :: "'a => 'a set => bool"  (infix "=o" 50) where
wenzelm@19380
    64
  "x =o A == x : A"
avigad@16908
    65
krauss@20523
    66
instance "fun" :: (type,semigroup_add)semigroup_add
wenzelm@19380
    67
  by default (auto simp add: func_plus add_assoc)
avigad@16908
    68
krauss@20523
    69
instance "fun" :: (type,comm_monoid_add)comm_monoid_add
wenzelm@19380
    70
  by default (auto simp add: func_zero func_plus add_ac)
avigad@16908
    71
krauss@20523
    72
instance "fun" :: (type,ab_group_add)ab_group_add
wenzelm@19736
    73
  apply default
wenzelm@19736
    74
   apply (simp add: func_minus func_plus func_zero)
avigad@16908
    75
  apply (simp add: func_minus func_plus func_diff diff_minus)
wenzelm@19736
    76
  done
avigad@16908
    77
krauss@20523
    78
instance "fun" :: (type,semigroup_mult)semigroup_mult
wenzelm@19736
    79
  apply default
avigad@16908
    80
  apply (auto simp add: func_times mult_assoc)
wenzelm@19736
    81
  done
avigad@16908
    82
krauss@20523
    83
instance "fun" :: (type,comm_monoid_mult)comm_monoid_mult
wenzelm@19736
    84
  apply default
wenzelm@19736
    85
   apply (auto simp add: func_one func_times mult_ac)
wenzelm@19736
    86
  done
avigad@16908
    87
krauss@20523
    88
instance "fun" :: (type,comm_ring_1)comm_ring_1
wenzelm@19736
    89
  apply default
wenzelm@19736
    90
   apply (auto simp add: func_plus func_times func_minus func_diff ext
wenzelm@19736
    91
     func_one func_zero ring_eq_simps)
avigad@16908
    92
  apply (drule fun_cong)
avigad@16908
    93
  apply simp
wenzelm@19736
    94
  done
avigad@16908
    95
avigad@16908
    96
instance set :: (semigroup_add)semigroup_add
wenzelm@19736
    97
  apply default
avigad@16908
    98
  apply (unfold set_plus)
avigad@16908
    99
  apply (force simp add: add_assoc)
wenzelm@19736
   100
  done
avigad@16908
   101
avigad@16908
   102
instance set :: (semigroup_mult)semigroup_mult
wenzelm@19736
   103
  apply default
avigad@16908
   104
  apply (unfold set_times)
avigad@16908
   105
  apply (force simp add: mult_assoc)
wenzelm@19736
   106
  done
avigad@16908
   107
avigad@16908
   108
instance set :: (comm_monoid_add)comm_monoid_add
wenzelm@19736
   109
  apply default
wenzelm@19736
   110
   apply (unfold set_plus)
wenzelm@19736
   111
   apply (force simp add: add_ac)
avigad@16908
   112
  apply (unfold set_zero)
avigad@16908
   113
  apply force
wenzelm@19736
   114
  done
avigad@16908
   115
avigad@16908
   116
instance set :: (comm_monoid_mult)comm_monoid_mult
wenzelm@19736
   117
  apply default
wenzelm@19736
   118
   apply (unfold set_times)
wenzelm@19736
   119
   apply (force simp add: mult_ac)
avigad@16908
   120
  apply (unfold set_one)
avigad@16908
   121
  apply force
wenzelm@19736
   122
  done
wenzelm@19736
   123
avigad@16908
   124
avigad@16908
   125
subsection {* Basic properties *}
avigad@16908
   126
wenzelm@19736
   127
lemma set_plus_intro [intro]: "a : C ==> b : D ==> a + b : C + D"
wenzelm@19736
   128
  by (auto simp add: set_plus)
avigad@16908
   129
avigad@16908
   130
lemma set_plus_intro2 [intro]: "b : C ==> a + b : a +o C"
wenzelm@19736
   131
  by (auto simp add: elt_set_plus_def)
avigad@16908
   132
wenzelm@19736
   133
lemma set_plus_rearrange: "((a::'a::comm_monoid_add) +o C) +
wenzelm@19736
   134
    (b +o D) = (a + b) +o (C + D)"
avigad@16908
   135
  apply (auto simp add: elt_set_plus_def set_plus add_ac)
wenzelm@19736
   136
   apply (rule_tac x = "ba + bb" in exI)
avigad@16908
   137
  apply (auto simp add: add_ac)
avigad@16908
   138
  apply (rule_tac x = "aa + a" in exI)
avigad@16908
   139
  apply (auto simp add: add_ac)
wenzelm@19736
   140
  done
avigad@16908
   141
wenzelm@19736
   142
lemma set_plus_rearrange2: "(a::'a::semigroup_add) +o (b +o C) =
wenzelm@19736
   143
    (a + b) +o C"
wenzelm@19736
   144
  by (auto simp add: elt_set_plus_def add_assoc)
avigad@16908
   145
wenzelm@19736
   146
lemma set_plus_rearrange3: "((a::'a::semigroup_add) +o B) + C =
wenzelm@19736
   147
    a +o (B + C)"
avigad@16908
   148
  apply (auto simp add: elt_set_plus_def set_plus)
wenzelm@19736
   149
   apply (blast intro: add_ac)
avigad@16908
   150
  apply (rule_tac x = "a + aa" in exI)
avigad@16908
   151
  apply (rule conjI)
wenzelm@19736
   152
   apply (rule_tac x = "aa" in bexI)
wenzelm@19736
   153
    apply auto
avigad@16908
   154
  apply (rule_tac x = "ba" in bexI)
wenzelm@19736
   155
   apply (auto simp add: add_ac)
wenzelm@19736
   156
  done
avigad@16908
   157
wenzelm@19736
   158
theorem set_plus_rearrange4: "C + ((a::'a::comm_monoid_add) +o D) =
wenzelm@19736
   159
    a +o (C + D)"
avigad@16908
   160
  apply (auto intro!: subsetI simp add: elt_set_plus_def set_plus add_ac)
wenzelm@19736
   161
   apply (rule_tac x = "aa + ba" in exI)
wenzelm@19736
   162
   apply (auto simp add: add_ac)
wenzelm@19736
   163
  done
avigad@16908
   164
avigad@16908
   165
theorems set_plus_rearranges = set_plus_rearrange set_plus_rearrange2
avigad@16908
   166
  set_plus_rearrange3 set_plus_rearrange4
avigad@16908
   167
avigad@16908
   168
lemma set_plus_mono [intro!]: "C <= D ==> a +o C <= a +o D"
wenzelm@19736
   169
  by (auto simp add: elt_set_plus_def)
avigad@16908
   170
wenzelm@19736
   171
lemma set_plus_mono2 [intro]: "(C::('a::plus) set) <= D ==> E <= F ==>
avigad@16908
   172
    C + E <= D + F"
wenzelm@19736
   173
  by (auto simp add: set_plus)
avigad@16908
   174
avigad@16908
   175
lemma set_plus_mono3 [intro]: "a : C ==> a +o D <= C + D"
wenzelm@19736
   176
  by (auto simp add: elt_set_plus_def set_plus)
avigad@16908
   177
wenzelm@19736
   178
lemma set_plus_mono4 [intro]: "(a::'a::comm_monoid_add) : C ==>
wenzelm@19736
   179
    a +o D <= D + C"
wenzelm@19736
   180
  by (auto simp add: elt_set_plus_def set_plus add_ac)
avigad@16908
   181
avigad@16908
   182
lemma set_plus_mono5: "a:C ==> B <= D ==> a +o B <= C + D"
avigad@16908
   183
  apply (subgoal_tac "a +o B <= a +o D")
wenzelm@19736
   184
   apply (erule order_trans)
wenzelm@19736
   185
   apply (erule set_plus_mono3)
avigad@16908
   186
  apply (erule set_plus_mono)
wenzelm@19736
   187
  done
avigad@16908
   188
wenzelm@19736
   189
lemma set_plus_mono_b: "C <= D ==> x : a +o C
avigad@16908
   190
    ==> x : a +o D"
avigad@16908
   191
  apply (frule set_plus_mono)
avigad@16908
   192
  apply auto
wenzelm@19736
   193
  done
avigad@16908
   194
wenzelm@19736
   195
lemma set_plus_mono2_b: "C <= D ==> E <= F ==> x : C + E ==>
avigad@16908
   196
    x : D + F"
avigad@16908
   197
  apply (frule set_plus_mono2)
wenzelm@19736
   198
   prefer 2
wenzelm@19736
   199
   apply force
avigad@16908
   200
  apply assumption
wenzelm@19736
   201
  done
avigad@16908
   202
avigad@16908
   203
lemma set_plus_mono3_b: "a : C ==> x : a +o D ==> x : C + D"
avigad@16908
   204
  apply (frule set_plus_mono3)
avigad@16908
   205
  apply auto
wenzelm@19736
   206
  done
avigad@16908
   207
wenzelm@19736
   208
lemma set_plus_mono4_b: "(a::'a::comm_monoid_add) : C ==>
wenzelm@19736
   209
    x : a +o D ==> x : D + C"
avigad@16908
   210
  apply (frule set_plus_mono4)
avigad@16908
   211
  apply auto
wenzelm@19736
   212
  done
avigad@16908
   213
avigad@16908
   214
lemma set_zero_plus [simp]: "(0::'a::comm_monoid_add) +o C = C"
wenzelm@19736
   215
  by (auto simp add: elt_set_plus_def)
avigad@16908
   216
avigad@16908
   217
lemma set_zero_plus2: "(0::'a::comm_monoid_add) : A ==> B <= A + B"
avigad@16908
   218
  apply (auto intro!: subsetI simp add: set_plus)
avigad@16908
   219
  apply (rule_tac x = 0 in bexI)
wenzelm@19736
   220
   apply (rule_tac x = x in bexI)
wenzelm@19736
   221
    apply (auto simp add: add_ac)
wenzelm@19736
   222
  done
avigad@16908
   223
avigad@16908
   224
lemma set_plus_imp_minus: "(a::'a::ab_group_add) : b +o C ==> (a - b) : C"
wenzelm@19736
   225
  by (auto simp add: elt_set_plus_def add_ac diff_minus)
avigad@16908
   226
avigad@16908
   227
lemma set_minus_imp_plus: "(a::'a::ab_group_add) - b : C ==> a : b +o C"
avigad@16908
   228
  apply (auto simp add: elt_set_plus_def add_ac diff_minus)
avigad@16908
   229
  apply (subgoal_tac "a = (a + - b) + b")
wenzelm@19736
   230
   apply (rule bexI, assumption, assumption)
avigad@16908
   231
  apply (auto simp add: add_ac)
wenzelm@19736
   232
  done
avigad@16908
   233
avigad@16908
   234
lemma set_minus_plus: "((a::'a::ab_group_add) - b : C) = (a : b +o C)"
wenzelm@19736
   235
  by (rule iffI, rule set_minus_imp_plus, assumption, rule set_plus_imp_minus,
avigad@16908
   236
    assumption)
avigad@16908
   237
wenzelm@19736
   238
lemma set_times_intro [intro]: "a : C ==> b : D ==> a * b : C * D"
wenzelm@19736
   239
  by (auto simp add: set_times)
avigad@16908
   240
avigad@16908
   241
lemma set_times_intro2 [intro!]: "b : C ==> a * b : a *o C"
wenzelm@19736
   242
  by (auto simp add: elt_set_times_def)
avigad@16908
   243
wenzelm@19736
   244
lemma set_times_rearrange: "((a::'a::comm_monoid_mult) *o C) *
wenzelm@19736
   245
    (b *o D) = (a * b) *o (C * D)"
avigad@16908
   246
  apply (auto simp add: elt_set_times_def set_times)
wenzelm@19736
   247
   apply (rule_tac x = "ba * bb" in exI)
wenzelm@19736
   248
   apply (auto simp add: mult_ac)
avigad@16908
   249
  apply (rule_tac x = "aa * a" in exI)
avigad@16908
   250
  apply (auto simp add: mult_ac)
wenzelm@19736
   251
  done
avigad@16908
   252
wenzelm@19736
   253
lemma set_times_rearrange2: "(a::'a::semigroup_mult) *o (b *o C) =
wenzelm@19736
   254
    (a * b) *o C"
wenzelm@19736
   255
  by (auto simp add: elt_set_times_def mult_assoc)
avigad@16908
   256
wenzelm@19736
   257
lemma set_times_rearrange3: "((a::'a::semigroup_mult) *o B) * C =
wenzelm@19736
   258
    a *o (B * C)"
avigad@16908
   259
  apply (auto simp add: elt_set_times_def set_times)
wenzelm@19736
   260
   apply (blast intro: mult_ac)
avigad@16908
   261
  apply (rule_tac x = "a * aa" in exI)
avigad@16908
   262
  apply (rule conjI)
wenzelm@19736
   263
   apply (rule_tac x = "aa" in bexI)
wenzelm@19736
   264
    apply auto
avigad@16908
   265
  apply (rule_tac x = "ba" in bexI)
wenzelm@19736
   266
   apply (auto simp add: mult_ac)
wenzelm@19736
   267
  done
avigad@16908
   268
wenzelm@19736
   269
theorem set_times_rearrange4: "C * ((a::'a::comm_monoid_mult) *o D) =
wenzelm@19736
   270
    a *o (C * D)"
wenzelm@19736
   271
  apply (auto intro!: subsetI simp add: elt_set_times_def set_times
avigad@16908
   272
    mult_ac)
wenzelm@19736
   273
   apply (rule_tac x = "aa * ba" in exI)
wenzelm@19736
   274
   apply (auto simp add: mult_ac)
wenzelm@19736
   275
  done
avigad@16908
   276
avigad@16908
   277
theorems set_times_rearranges = set_times_rearrange set_times_rearrange2
avigad@16908
   278
  set_times_rearrange3 set_times_rearrange4
avigad@16908
   279
avigad@16908
   280
lemma set_times_mono [intro]: "C <= D ==> a *o C <= a *o D"
wenzelm@19736
   281
  by (auto simp add: elt_set_times_def)
avigad@16908
   282
wenzelm@19736
   283
lemma set_times_mono2 [intro]: "(C::('a::times) set) <= D ==> E <= F ==>
avigad@16908
   284
    C * E <= D * F"
wenzelm@19736
   285
  by (auto simp add: set_times)
avigad@16908
   286
avigad@16908
   287
lemma set_times_mono3 [intro]: "a : C ==> a *o D <= C * D"
wenzelm@19736
   288
  by (auto simp add: elt_set_times_def set_times)
avigad@16908
   289
wenzelm@19736
   290
lemma set_times_mono4 [intro]: "(a::'a::comm_monoid_mult) : C ==>
wenzelm@19736
   291
    a *o D <= D * C"
wenzelm@19736
   292
  by (auto simp add: elt_set_times_def set_times mult_ac)
avigad@16908
   293
avigad@16908
   294
lemma set_times_mono5: "a:C ==> B <= D ==> a *o B <= C * D"
avigad@16908
   295
  apply (subgoal_tac "a *o B <= a *o D")
wenzelm@19736
   296
   apply (erule order_trans)
wenzelm@19736
   297
   apply (erule set_times_mono3)
avigad@16908
   298
  apply (erule set_times_mono)
wenzelm@19736
   299
  done
avigad@16908
   300
wenzelm@19736
   301
lemma set_times_mono_b: "C <= D ==> x : a *o C
avigad@16908
   302
    ==> x : a *o D"
avigad@16908
   303
  apply (frule set_times_mono)
avigad@16908
   304
  apply auto
wenzelm@19736
   305
  done
avigad@16908
   306
wenzelm@19736
   307
lemma set_times_mono2_b: "C <= D ==> E <= F ==> x : C * E ==>
avigad@16908
   308
    x : D * F"
avigad@16908
   309
  apply (frule set_times_mono2)
wenzelm@19736
   310
   prefer 2
wenzelm@19736
   311
   apply force
avigad@16908
   312
  apply assumption
wenzelm@19736
   313
  done
avigad@16908
   314
avigad@16908
   315
lemma set_times_mono3_b: "a : C ==> x : a *o D ==> x : C * D"
avigad@16908
   316
  apply (frule set_times_mono3)
avigad@16908
   317
  apply auto
wenzelm@19736
   318
  done
avigad@16908
   319
wenzelm@19736
   320
lemma set_times_mono4_b: "(a::'a::comm_monoid_mult) : C ==>
wenzelm@19736
   321
    x : a *o D ==> x : D * C"
avigad@16908
   322
  apply (frule set_times_mono4)
avigad@16908
   323
  apply auto
wenzelm@19736
   324
  done
avigad@16908
   325
avigad@16908
   326
lemma set_one_times [simp]: "(1::'a::comm_monoid_mult) *o C = C"
wenzelm@19736
   327
  by (auto simp add: elt_set_times_def)
avigad@16908
   328
wenzelm@19736
   329
lemma set_times_plus_distrib: "(a::'a::semiring) *o (b +o C)=
wenzelm@19736
   330
    (a * b) +o (a *o C)"
wenzelm@19736
   331
  by (auto simp add: elt_set_plus_def elt_set_times_def ring_distrib)
avigad@16908
   332
wenzelm@19736
   333
lemma set_times_plus_distrib2: "(a::'a::semiring) *o (B + C) =
wenzelm@19736
   334
    (a *o B) + (a *o C)"
avigad@16908
   335
  apply (auto simp add: set_plus elt_set_times_def ring_distrib)
wenzelm@19736
   336
   apply blast
avigad@16908
   337
  apply (rule_tac x = "b + bb" in exI)
avigad@16908
   338
  apply (auto simp add: ring_distrib)
wenzelm@19736
   339
  done
avigad@16908
   340
wenzelm@19736
   341
lemma set_times_plus_distrib3: "((a::'a::semiring) +o C) * D <=
avigad@16908
   342
    a *o D + C * D"
wenzelm@19736
   343
  apply (auto intro!: subsetI simp add:
wenzelm@19736
   344
    elt_set_plus_def elt_set_times_def set_times
avigad@16908
   345
    set_plus ring_distrib)
avigad@16908
   346
  apply auto
wenzelm@19736
   347
  done
avigad@16908
   348
wenzelm@19380
   349
theorems set_times_plus_distribs =
wenzelm@19380
   350
  set_times_plus_distrib
avigad@16908
   351
  set_times_plus_distrib2
avigad@16908
   352
wenzelm@19736
   353
lemma set_neg_intro: "(a::'a::ring_1) : (- 1) *o C ==>
wenzelm@19736
   354
    - a : C"
wenzelm@19736
   355
  by (auto simp add: elt_set_times_def)
avigad@16908
   356
avigad@16908
   357
lemma set_neg_intro2: "(a::'a::ring_1) : C ==>
avigad@16908
   358
    - a : (- 1) *o C"
wenzelm@19736
   359
  by (auto simp add: elt_set_times_def)
wenzelm@19736
   360
avigad@16908
   361
end