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