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