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