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