src/HOL/Finite_Set.thy
author haftmann
Thu Mar 11 14:38:13 2010 +0100 (2010-03-11)
changeset 35722 69419a09a7ff
parent 35719 99b6152aedf5
child 35796 2d44d2a1f68e
permissions -rw-r--r--
moved cardinality to Finite_Set as far as appropriate; added locales for fold_image
wenzelm@12396
     1
(*  Title:      HOL/Finite_Set.thy
wenzelm@12396
     2
    Author:     Tobias Nipkow, Lawrence C Paulson and Markus Wenzel
avigad@16775
     3
                with contributions by Jeremy Avigad
wenzelm@12396
     4
*)
wenzelm@12396
     5
wenzelm@12396
     6
header {* Finite sets *}
wenzelm@12396
     7
nipkow@15131
     8
theory Finite_Set
haftmann@35719
     9
imports Power Option
nipkow@15131
    10
begin
wenzelm@12396
    11
nipkow@15392
    12
subsection {* Definition and basic properties *}
wenzelm@12396
    13
berghofe@23736
    14
inductive finite :: "'a set => bool"
berghofe@22262
    15
  where
berghofe@22262
    16
    emptyI [simp, intro!]: "finite {}"
berghofe@22262
    17
  | insertI [simp, intro!]: "finite A ==> finite (insert a A)"
wenzelm@12396
    18
nipkow@13737
    19
lemma ex_new_if_finite: -- "does not depend on def of finite at all"
wenzelm@14661
    20
  assumes "\<not> finite (UNIV :: 'a set)" and "finite A"
wenzelm@14661
    21
  shows "\<exists>a::'a. a \<notin> A"
wenzelm@14661
    22
proof -
haftmann@28823
    23
  from assms have "A \<noteq> UNIV" by blast
wenzelm@14661
    24
  thus ?thesis by blast
wenzelm@14661
    25
qed
wenzelm@12396
    26
berghofe@22262
    27
lemma finite_induct [case_names empty insert, induct set: finite]:
wenzelm@12396
    28
  "finite F ==>
nipkow@15327
    29
    P {} ==> (!!x F. finite F ==> x \<notin> F ==> P F ==> P (insert x F)) ==> P F"
wenzelm@12396
    30
  -- {* Discharging @{text "x \<notin> F"} entails extra work. *}
wenzelm@12396
    31
proof -
wenzelm@13421
    32
  assume "P {}" and
nipkow@15327
    33
    insert: "!!x F. finite F ==> x \<notin> F ==> P F ==> P (insert x F)"
wenzelm@12396
    34
  assume "finite F"
wenzelm@12396
    35
  thus "P F"
wenzelm@12396
    36
  proof induct
wenzelm@23389
    37
    show "P {}" by fact
nipkow@15327
    38
    fix x F assume F: "finite F" and P: "P F"
wenzelm@12396
    39
    show "P (insert x F)"
wenzelm@12396
    40
    proof cases
wenzelm@12396
    41
      assume "x \<in> F"
wenzelm@12396
    42
      hence "insert x F = F" by (rule insert_absorb)
wenzelm@12396
    43
      with P show ?thesis by (simp only:)
wenzelm@12396
    44
    next
wenzelm@12396
    45
      assume "x \<notin> F"
wenzelm@12396
    46
      from F this P show ?thesis by (rule insert)
wenzelm@12396
    47
    qed
wenzelm@12396
    48
  qed
wenzelm@12396
    49
qed
wenzelm@12396
    50
nipkow@15484
    51
lemma finite_ne_induct[case_names singleton insert, consumes 2]:
nipkow@15484
    52
assumes fin: "finite F" shows "F \<noteq> {} \<Longrightarrow>
nipkow@15484
    53
 \<lbrakk> \<And>x. P{x};
nipkow@15484
    54
   \<And>x F. \<lbrakk> finite F; F \<noteq> {}; x \<notin> F; P F \<rbrakk> \<Longrightarrow> P (insert x F) \<rbrakk>
nipkow@15484
    55
 \<Longrightarrow> P F"
nipkow@15484
    56
using fin
nipkow@15484
    57
proof induct
nipkow@15484
    58
  case empty thus ?case by simp
nipkow@15484
    59
next
nipkow@15484
    60
  case (insert x F)
nipkow@15484
    61
  show ?case
nipkow@15484
    62
  proof cases
wenzelm@23389
    63
    assume "F = {}"
wenzelm@23389
    64
    thus ?thesis using `P {x}` by simp
nipkow@15484
    65
  next
wenzelm@23389
    66
    assume "F \<noteq> {}"
wenzelm@23389
    67
    thus ?thesis using insert by blast
nipkow@15484
    68
  qed
nipkow@15484
    69
qed
nipkow@15484
    70
wenzelm@12396
    71
lemma finite_subset_induct [consumes 2, case_names empty insert]:
wenzelm@23389
    72
  assumes "finite F" and "F \<subseteq> A"
wenzelm@23389
    73
    and empty: "P {}"
wenzelm@23389
    74
    and insert: "!!a F. finite F ==> a \<in> A ==> a \<notin> F ==> P F ==> P (insert a F)"
wenzelm@23389
    75
  shows "P F"
wenzelm@12396
    76
proof -
wenzelm@23389
    77
  from `finite F` and `F \<subseteq> A`
wenzelm@23389
    78
  show ?thesis
wenzelm@12396
    79
  proof induct
wenzelm@23389
    80
    show "P {}" by fact
wenzelm@23389
    81
  next
wenzelm@23389
    82
    fix x F
wenzelm@23389
    83
    assume "finite F" and "x \<notin> F" and
wenzelm@23389
    84
      P: "F \<subseteq> A ==> P F" and i: "insert x F \<subseteq> A"
wenzelm@12396
    85
    show "P (insert x F)"
wenzelm@12396
    86
    proof (rule insert)
wenzelm@12396
    87
      from i show "x \<in> A" by blast
wenzelm@12396
    88
      from i have "F \<subseteq> A" by blast
wenzelm@12396
    89
      with P show "P F" .
wenzelm@23389
    90
      show "finite F" by fact
wenzelm@23389
    91
      show "x \<notin> F" by fact
wenzelm@12396
    92
    qed
wenzelm@12396
    93
  qed
wenzelm@12396
    94
qed
wenzelm@12396
    95
nipkow@32006
    96
nipkow@29923
    97
text{* A finite choice principle. Does not need the SOME choice operator. *}
nipkow@29923
    98
lemma finite_set_choice:
nipkow@29923
    99
  "finite A \<Longrightarrow> ALL x:A. (EX y. P x y) \<Longrightarrow> EX f. ALL x:A. P x (f x)"
nipkow@29923
   100
proof (induct set: finite)
nipkow@29923
   101
  case empty thus ?case by simp
nipkow@29923
   102
next
nipkow@29923
   103
  case (insert a A)
nipkow@29923
   104
  then obtain f b where f: "ALL x:A. P x (f x)" and ab: "P a b" by auto
nipkow@29923
   105
  show ?case (is "EX f. ?P f")
nipkow@29923
   106
  proof
nipkow@29923
   107
    show "?P(%x. if x = a then b else f x)" using f ab by auto
nipkow@29923
   108
  qed
nipkow@29923
   109
qed
nipkow@29923
   110
haftmann@23878
   111
nipkow@15392
   112
text{* Finite sets are the images of initial segments of natural numbers: *}
nipkow@15392
   113
paulson@15510
   114
lemma finite_imp_nat_seg_image_inj_on:
paulson@15510
   115
  assumes fin: "finite A" 
paulson@15510
   116
  shows "\<exists> (n::nat) f. A = f ` {i. i<n} & inj_on f {i. i<n}"
nipkow@15392
   117
using fin
nipkow@15392
   118
proof induct
nipkow@15392
   119
  case empty
paulson@15510
   120
  show ?case  
paulson@15510
   121
  proof show "\<exists>f. {} = f ` {i::nat. i < 0} & inj_on f {i. i<0}" by simp 
paulson@15510
   122
  qed
nipkow@15392
   123
next
nipkow@15392
   124
  case (insert a A)
wenzelm@23389
   125
  have notinA: "a \<notin> A" by fact
paulson@15510
   126
  from insert.hyps obtain n f
paulson@15510
   127
    where "A = f ` {i::nat. i < n}" "inj_on f {i. i < n}" by blast
paulson@15510
   128
  hence "insert a A = f(n:=a) ` {i. i < Suc n}"
paulson@15510
   129
        "inj_on (f(n:=a)) {i. i < Suc n}" using notinA
paulson@15510
   130
    by (auto simp add: image_def Ball_def inj_on_def less_Suc_eq)
nipkow@15392
   131
  thus ?case by blast
nipkow@15392
   132
qed
nipkow@15392
   133
nipkow@15392
   134
lemma nat_seg_image_imp_finite:
nipkow@15392
   135
  "!!f A. A = f ` {i::nat. i<n} \<Longrightarrow> finite A"
nipkow@15392
   136
proof (induct n)
nipkow@15392
   137
  case 0 thus ?case by simp
nipkow@15392
   138
next
nipkow@15392
   139
  case (Suc n)
nipkow@15392
   140
  let ?B = "f ` {i. i < n}"
nipkow@15392
   141
  have finB: "finite ?B" by(rule Suc.hyps[OF refl])
nipkow@15392
   142
  show ?case
nipkow@15392
   143
  proof cases
nipkow@15392
   144
    assume "\<exists>k<n. f n = f k"
nipkow@15392
   145
    hence "A = ?B" using Suc.prems by(auto simp:less_Suc_eq)
nipkow@15392
   146
    thus ?thesis using finB by simp
nipkow@15392
   147
  next
nipkow@15392
   148
    assume "\<not>(\<exists> k<n. f n = f k)"
nipkow@15392
   149
    hence "A = insert (f n) ?B" using Suc.prems by(auto simp:less_Suc_eq)
nipkow@15392
   150
    thus ?thesis using finB by simp
nipkow@15392
   151
  qed
nipkow@15392
   152
qed
nipkow@15392
   153
nipkow@15392
   154
lemma finite_conv_nat_seg_image:
nipkow@15392
   155
  "finite A = (\<exists> (n::nat) f. A = f ` {i::nat. i<n})"
paulson@15510
   156
by(blast intro: nat_seg_image_imp_finite dest: finite_imp_nat_seg_image_inj_on)
nipkow@15392
   157
nipkow@32988
   158
lemma finite_imp_inj_to_nat_seg:
nipkow@32988
   159
assumes "finite A"
nipkow@32988
   160
shows "EX f n::nat. f`A = {i. i<n} & inj_on f A"
nipkow@32988
   161
proof -
nipkow@32988
   162
  from finite_imp_nat_seg_image_inj_on[OF `finite A`]
nipkow@32988
   163
  obtain f and n::nat where bij: "bij_betw f {i. i<n} A"
nipkow@32988
   164
    by (auto simp:bij_betw_def)
nipkow@33057
   165
  let ?f = "the_inv_into {i. i<n} f"
nipkow@32988
   166
  have "inj_on ?f A & ?f ` A = {i. i<n}"
nipkow@33057
   167
    by (fold bij_betw_def) (rule bij_betw_the_inv_into[OF bij])
nipkow@32988
   168
  thus ?thesis by blast
nipkow@32988
   169
qed
nipkow@32988
   170
nipkow@29920
   171
lemma finite_Collect_less_nat[iff]: "finite{n::nat. n<k}"
nipkow@29920
   172
by(fastsimp simp: finite_conv_nat_seg_image)
nipkow@29920
   173
haftmann@26441
   174
nipkow@15392
   175
subsubsection{* Finiteness and set theoretic constructions *}
nipkow@15392
   176
wenzelm@12396
   177
lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
nipkow@29901
   178
by (induct set: finite) simp_all
wenzelm@12396
   179
wenzelm@12396
   180
lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
wenzelm@12396
   181
  -- {* Every subset of a finite set is finite. *}
wenzelm@12396
   182
proof -
wenzelm@12396
   183
  assume "finite B"
wenzelm@12396
   184
  thus "!!A. A \<subseteq> B ==> finite A"
wenzelm@12396
   185
  proof induct
wenzelm@12396
   186
    case empty
wenzelm@12396
   187
    thus ?case by simp
wenzelm@12396
   188
  next
nipkow@15327
   189
    case (insert x F A)
wenzelm@23389
   190
    have A: "A \<subseteq> insert x F" and r: "A - {x} \<subseteq> F ==> finite (A - {x})" by fact+
wenzelm@12396
   191
    show "finite A"
wenzelm@12396
   192
    proof cases
wenzelm@12396
   193
      assume x: "x \<in> A"
wenzelm@12396
   194
      with A have "A - {x} \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   195
      with r have "finite (A - {x})" .
wenzelm@12396
   196
      hence "finite (insert x (A - {x}))" ..
wenzelm@23389
   197
      also have "insert x (A - {x}) = A" using x by (rule insert_Diff)
wenzelm@12396
   198
      finally show ?thesis .
wenzelm@12396
   199
    next
wenzelm@23389
   200
      show "A \<subseteq> F ==> ?thesis" by fact
wenzelm@12396
   201
      assume "x \<notin> A"
wenzelm@12396
   202
      with A show "A \<subseteq> F" by (simp add: subset_insert_iff)
wenzelm@12396
   203
    qed
wenzelm@12396
   204
  qed
wenzelm@12396
   205
qed
wenzelm@12396
   206
huffman@34111
   207
lemma rev_finite_subset: "finite B ==> A \<subseteq> B ==> finite A"
huffman@34111
   208
by (rule finite_subset)
huffman@34111
   209
wenzelm@12396
   210
lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
nipkow@29901
   211
by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
nipkow@29901
   212
nipkow@29916
   213
lemma finite_Collect_disjI[simp]:
nipkow@29901
   214
  "finite{x. P x | Q x} = (finite{x. P x} & finite{x. Q x})"
nipkow@29901
   215
by(simp add:Collect_disj_eq)
wenzelm@12396
   216
wenzelm@12396
   217
lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
wenzelm@12396
   218
  -- {* The converse obviously fails. *}
nipkow@29901
   219
by (blast intro: finite_subset)
nipkow@29901
   220
nipkow@29916
   221
lemma finite_Collect_conjI [simp, intro]:
nipkow@29901
   222
  "finite{x. P x} | finite{x. Q x} ==> finite{x. P x & Q x}"
nipkow@29901
   223
  -- {* The converse obviously fails. *}
nipkow@29901
   224
by(simp add:Collect_conj_eq)
wenzelm@12396
   225
nipkow@29920
   226
lemma finite_Collect_le_nat[iff]: "finite{n::nat. n<=k}"
nipkow@29920
   227
by(simp add: le_eq_less_or_eq)
nipkow@29920
   228
wenzelm@12396
   229
lemma finite_insert [simp]: "finite (insert a A) = finite A"
wenzelm@12396
   230
  apply (subst insert_is_Un)
paulson@14208
   231
  apply (simp only: finite_Un, blast)
wenzelm@12396
   232
  done
wenzelm@12396
   233
nipkow@15281
   234
lemma finite_Union[simp, intro]:
nipkow@15281
   235
 "\<lbrakk> finite A; !!M. M \<in> A \<Longrightarrow> finite M \<rbrakk> \<Longrightarrow> finite(\<Union>A)"
nipkow@15281
   236
by (induct rule:finite_induct) simp_all
nipkow@15281
   237
nipkow@31992
   238
lemma finite_Inter[intro]: "EX A:M. finite(A) \<Longrightarrow> finite(Inter M)"
nipkow@31992
   239
by (blast intro: Inter_lower finite_subset)
nipkow@31992
   240
nipkow@31992
   241
lemma finite_INT[intro]: "EX x:I. finite(A x) \<Longrightarrow> finite(INT x:I. A x)"
nipkow@31992
   242
by (blast intro: INT_lower finite_subset)
nipkow@31992
   243
wenzelm@12396
   244
lemma finite_empty_induct:
wenzelm@23389
   245
  assumes "finite A"
wenzelm@23389
   246
    and "P A"
wenzelm@23389
   247
    and "!!a A. finite A ==> a:A ==> P A ==> P (A - {a})"
wenzelm@23389
   248
  shows "P {}"
wenzelm@12396
   249
proof -
wenzelm@12396
   250
  have "P (A - A)"
wenzelm@12396
   251
  proof -
wenzelm@23389
   252
    {
wenzelm@23389
   253
      fix c b :: "'a set"
wenzelm@23389
   254
      assume c: "finite c" and b: "finite b"
wenzelm@32960
   255
        and P1: "P b" and P2: "!!x y. finite y ==> x \<in> y ==> P y ==> P (y - {x})"
wenzelm@23389
   256
      have "c \<subseteq> b ==> P (b - c)"
wenzelm@32960
   257
        using c
wenzelm@23389
   258
      proof induct
wenzelm@32960
   259
        case empty
wenzelm@32960
   260
        from P1 show ?case by simp
wenzelm@23389
   261
      next
wenzelm@32960
   262
        case (insert x F)
wenzelm@32960
   263
        have "P (b - F - {x})"
wenzelm@32960
   264
        proof (rule P2)
wenzelm@23389
   265
          from _ b show "finite (b - F)" by (rule finite_subset) blast
wenzelm@23389
   266
          from insert show "x \<in> b - F" by simp
wenzelm@23389
   267
          from insert show "P (b - F)" by simp
wenzelm@32960
   268
        qed
wenzelm@32960
   269
        also have "b - F - {x} = b - insert x F" by (rule Diff_insert [symmetric])
wenzelm@32960
   270
        finally show ?case .
wenzelm@12396
   271
      qed
wenzelm@23389
   272
    }
wenzelm@23389
   273
    then show ?thesis by this (simp_all add: assms)
wenzelm@12396
   274
  qed
wenzelm@23389
   275
  then show ?thesis by simp
wenzelm@12396
   276
qed
wenzelm@12396
   277
nipkow@29901
   278
lemma finite_Diff [simp]: "finite A ==> finite (A - B)"
nipkow@29901
   279
by (rule Diff_subset [THEN finite_subset])
nipkow@29901
   280
nipkow@29901
   281
lemma finite_Diff2 [simp]:
nipkow@29901
   282
  assumes "finite B" shows "finite (A - B) = finite A"
nipkow@29901
   283
proof -
nipkow@29901
   284
  have "finite A \<longleftrightarrow> finite((A-B) Un (A Int B))" by(simp add: Un_Diff_Int)
nipkow@29901
   285
  also have "\<dots> \<longleftrightarrow> finite(A-B)" using `finite B` by(simp)
nipkow@29901
   286
  finally show ?thesis ..
nipkow@29901
   287
qed
nipkow@29901
   288
nipkow@29901
   289
lemma finite_compl[simp]:
nipkow@29901
   290
  "finite(A::'a set) \<Longrightarrow> finite(-A) = finite(UNIV::'a set)"
nipkow@29901
   291
by(simp add:Compl_eq_Diff_UNIV)
wenzelm@12396
   292
nipkow@29916
   293
lemma finite_Collect_not[simp]:
nipkow@29903
   294
  "finite{x::'a. P x} \<Longrightarrow> finite{x. ~P x} = finite(UNIV::'a set)"
nipkow@29903
   295
by(simp add:Collect_neg_eq)
nipkow@29903
   296
wenzelm@12396
   297
lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
wenzelm@12396
   298
  apply (subst Diff_insert)
wenzelm@12396
   299
  apply (case_tac "a : A - B")
wenzelm@12396
   300
   apply (rule finite_insert [symmetric, THEN trans])
paulson@14208
   301
   apply (subst insert_Diff, simp_all)
wenzelm@12396
   302
  done
wenzelm@12396
   303
wenzelm@12396
   304
nipkow@15392
   305
text {* Image and Inverse Image over Finite Sets *}
paulson@13825
   306
paulson@13825
   307
lemma finite_imageI[simp]: "finite F ==> finite (h ` F)"
paulson@13825
   308
  -- {* The image of a finite set is finite. *}
berghofe@22262
   309
  by (induct set: finite) simp_all
paulson@13825
   310
haftmann@31768
   311
lemma finite_image_set [simp]:
haftmann@31768
   312
  "finite {x. P x} \<Longrightarrow> finite { f x | x. P x }"
haftmann@31768
   313
  by (simp add: image_Collect [symmetric])
haftmann@31768
   314
paulson@14430
   315
lemma finite_surj: "finite A ==> B <= f ` A ==> finite B"
paulson@14430
   316
  apply (frule finite_imageI)
paulson@14430
   317
  apply (erule finite_subset, assumption)
paulson@14430
   318
  done
paulson@14430
   319
paulson@13825
   320
lemma finite_range_imageI:
paulson@13825
   321
    "finite (range g) ==> finite (range (%x. f (g x)))"
huffman@27418
   322
  apply (drule finite_imageI, simp add: range_composition)
paulson@13825
   323
  done
paulson@13825
   324
wenzelm@12396
   325
lemma finite_imageD: "finite (f`A) ==> inj_on f A ==> finite A"
wenzelm@12396
   326
proof -
wenzelm@12396
   327
  have aux: "!!A. finite (A - {}) = finite A" by simp
wenzelm@12396
   328
  fix B :: "'a set"
wenzelm@12396
   329
  assume "finite B"
wenzelm@12396
   330
  thus "!!A. f`A = B ==> inj_on f A ==> finite A"
wenzelm@12396
   331
    apply induct
wenzelm@12396
   332
     apply simp
wenzelm@12396
   333
    apply (subgoal_tac "EX y:A. f y = x & F = f ` (A - {y})")
wenzelm@12396
   334
     apply clarify
wenzelm@12396
   335
     apply (simp (no_asm_use) add: inj_on_def)
paulson@14208
   336
     apply (blast dest!: aux [THEN iffD1], atomize)
wenzelm@12396
   337
    apply (erule_tac V = "ALL A. ?PP (A)" in thin_rl)
paulson@14208
   338
    apply (frule subsetD [OF equalityD2 insertI1], clarify)
wenzelm@12396
   339
    apply (rule_tac x = xa in bexI)
wenzelm@12396
   340
     apply (simp_all add: inj_on_image_set_diff)
wenzelm@12396
   341
    done
wenzelm@12396
   342
qed (rule refl)
wenzelm@12396
   343
wenzelm@12396
   344
paulson@13825
   345
lemma inj_vimage_singleton: "inj f ==> f-`{a} \<subseteq> {THE x. f x = a}"
paulson@13825
   346
  -- {* The inverse image of a singleton under an injective function
paulson@13825
   347
         is included in a singleton. *}
paulson@14430
   348
  apply (auto simp add: inj_on_def)
paulson@14430
   349
  apply (blast intro: the_equality [symmetric])
paulson@13825
   350
  done
paulson@13825
   351
paulson@13825
   352
lemma finite_vimageI: "[|finite F; inj h|] ==> finite (h -` F)"
paulson@13825
   353
  -- {* The inverse image of a finite set under an injective function
paulson@13825
   354
         is finite. *}
berghofe@22262
   355
  apply (induct set: finite)
wenzelm@21575
   356
   apply simp_all
paulson@14430
   357
  apply (subst vimage_insert)
huffman@35216
   358
  apply (simp add: finite_subset [OF inj_vimage_singleton])
paulson@13825
   359
  done
paulson@13825
   360
huffman@34111
   361
lemma finite_vimageD:
huffman@34111
   362
  assumes fin: "finite (h -` F)" and surj: "surj h"
huffman@34111
   363
  shows "finite F"
huffman@34111
   364
proof -
huffman@34111
   365
  have "finite (h ` (h -` F))" using fin by (rule finite_imageI)
huffman@34111
   366
  also have "h ` (h -` F) = F" using surj by (rule surj_image_vimage_eq)
huffman@34111
   367
  finally show "finite F" .
huffman@34111
   368
qed
huffman@34111
   369
huffman@34111
   370
lemma finite_vimage_iff: "bij h \<Longrightarrow> finite (h -` F) \<longleftrightarrow> finite F"
huffman@34111
   371
  unfolding bij_def by (auto elim: finite_vimageD finite_vimageI)
huffman@34111
   372
paulson@13825
   373
nipkow@15392
   374
text {* The finite UNION of finite sets *}
wenzelm@12396
   375
wenzelm@12396
   376
lemma finite_UN_I: "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (UN a:A. B a)"
berghofe@22262
   377
  by (induct set: finite) simp_all
wenzelm@12396
   378
wenzelm@12396
   379
text {*
wenzelm@12396
   380
  Strengthen RHS to
paulson@14430
   381
  @{prop "((ALL x:A. finite (B x)) & finite {x. x:A & B x \<noteq> {}})"}?
wenzelm@12396
   382
wenzelm@12396
   383
  We'd need to prove
paulson@14430
   384
  @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x \<noteq> {}}"}
wenzelm@12396
   385
  by induction. *}
wenzelm@12396
   386
nipkow@29918
   387
lemma finite_UN [simp]:
nipkow@29918
   388
  "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
nipkow@29918
   389
by (blast intro: finite_UN_I finite_subset)
wenzelm@12396
   390
nipkow@29920
   391
lemma finite_Collect_bex[simp]: "finite A \<Longrightarrow>
nipkow@29920
   392
  finite{x. EX y:A. Q x y} = (ALL y:A. finite{x. Q x y})"
nipkow@29920
   393
apply(subgoal_tac "{x. EX y:A. Q x y} = UNION A (%y. {x. Q x y})")
nipkow@29920
   394
 apply auto
nipkow@29920
   395
done
nipkow@29920
   396
nipkow@29920
   397
lemma finite_Collect_bounded_ex[simp]: "finite{y. P y} \<Longrightarrow>
nipkow@29920
   398
  finite{x. EX y. P y & Q x y} = (ALL y. P y \<longrightarrow> finite{x. Q x y})"
nipkow@29920
   399
apply(subgoal_tac "{x. EX y. P y & Q x y} = UNION {y. P y} (%y. {x. Q x y})")
nipkow@29920
   400
 apply auto
nipkow@29920
   401
done
nipkow@29920
   402
nipkow@29920
   403
nipkow@17022
   404
lemma finite_Plus: "[| finite A; finite B |] ==> finite (A <+> B)"
nipkow@17022
   405
by (simp add: Plus_def)
nipkow@17022
   406
nipkow@31080
   407
lemma finite_PlusD: 
nipkow@31080
   408
  fixes A :: "'a set" and B :: "'b set"
nipkow@31080
   409
  assumes fin: "finite (A <+> B)"
nipkow@31080
   410
  shows "finite A" "finite B"
nipkow@31080
   411
proof -
nipkow@31080
   412
  have "Inl ` A \<subseteq> A <+> B" by auto
nipkow@31080
   413
  hence "finite (Inl ` A :: ('a + 'b) set)" using fin by(rule finite_subset)
nipkow@31080
   414
  thus "finite A" by(rule finite_imageD)(auto intro: inj_onI)
nipkow@31080
   415
next
nipkow@31080
   416
  have "Inr ` B \<subseteq> A <+> B" by auto
nipkow@31080
   417
  hence "finite (Inr ` B :: ('a + 'b) set)" using fin by(rule finite_subset)
nipkow@31080
   418
  thus "finite B" by(rule finite_imageD)(auto intro: inj_onI)
nipkow@31080
   419
qed
nipkow@31080
   420
nipkow@31080
   421
lemma finite_Plus_iff[simp]: "finite (A <+> B) \<longleftrightarrow> finite A \<and> finite B"
nipkow@31080
   422
by(auto intro: finite_PlusD finite_Plus)
nipkow@31080
   423
nipkow@31080
   424
lemma finite_Plus_UNIV_iff[simp]:
nipkow@31080
   425
  "finite (UNIV :: ('a + 'b) set) =
nipkow@31080
   426
  (finite (UNIV :: 'a set) & finite (UNIV :: 'b set))"
nipkow@31080
   427
by(subst UNIV_Plus_UNIV[symmetric])(rule finite_Plus_iff)
nipkow@31080
   428
nipkow@31080
   429
nipkow@15392
   430
text {* Sigma of finite sets *}
wenzelm@12396
   431
wenzelm@12396
   432
lemma finite_SigmaI [simp]:
wenzelm@12396
   433
    "finite A ==> (!!a. a:A ==> finite (B a)) ==> finite (SIGMA a:A. B a)"
wenzelm@12396
   434
  by (unfold Sigma_def) (blast intro!: finite_UN_I)
wenzelm@12396
   435
nipkow@15402
   436
lemma finite_cartesian_product: "[| finite A; finite B |] ==>
nipkow@15402
   437
    finite (A <*> B)"
nipkow@15402
   438
  by (rule finite_SigmaI)
nipkow@15402
   439
wenzelm@12396
   440
lemma finite_Prod_UNIV:
wenzelm@12396
   441
    "finite (UNIV::'a set) ==> finite (UNIV::'b set) ==> finite (UNIV::('a * 'b) set)"
wenzelm@12396
   442
  apply (subgoal_tac "(UNIV:: ('a * 'b) set) = Sigma UNIV (%x. UNIV)")
wenzelm@12396
   443
   apply (erule ssubst)
paulson@14208
   444
   apply (erule finite_SigmaI, auto)
wenzelm@12396
   445
  done
wenzelm@12396
   446
paulson@15409
   447
lemma finite_cartesian_productD1:
paulson@15409
   448
     "[| finite (A <*> B); B \<noteq> {} |] ==> finite A"
paulson@15409
   449
apply (auto simp add: finite_conv_nat_seg_image) 
paulson@15409
   450
apply (drule_tac x=n in spec) 
paulson@15409
   451
apply (drule_tac x="fst o f" in spec) 
paulson@15409
   452
apply (auto simp add: o_def) 
paulson@15409
   453
 prefer 2 apply (force dest!: equalityD2) 
paulson@15409
   454
apply (drule equalityD1) 
paulson@15409
   455
apply (rename_tac y x)
paulson@15409
   456
apply (subgoal_tac "\<exists>k. k<n & f k = (x,y)") 
paulson@15409
   457
 prefer 2 apply force
paulson@15409
   458
apply clarify
paulson@15409
   459
apply (rule_tac x=k in image_eqI, auto)
paulson@15409
   460
done
paulson@15409
   461
paulson@15409
   462
lemma finite_cartesian_productD2:
paulson@15409
   463
     "[| finite (A <*> B); A \<noteq> {} |] ==> finite B"
paulson@15409
   464
apply (auto simp add: finite_conv_nat_seg_image) 
paulson@15409
   465
apply (drule_tac x=n in spec) 
paulson@15409
   466
apply (drule_tac x="snd o f" in spec) 
paulson@15409
   467
apply (auto simp add: o_def) 
paulson@15409
   468
 prefer 2 apply (force dest!: equalityD2) 
paulson@15409
   469
apply (drule equalityD1)
paulson@15409
   470
apply (rename_tac x y)
paulson@15409
   471
apply (subgoal_tac "\<exists>k. k<n & f k = (x,y)") 
paulson@15409
   472
 prefer 2 apply force
paulson@15409
   473
apply clarify
paulson@15409
   474
apply (rule_tac x=k in image_eqI, auto)
paulson@15409
   475
done
paulson@15409
   476
paulson@15409
   477
nipkow@15392
   478
text {* The powerset of a finite set *}
wenzelm@12396
   479
wenzelm@12396
   480
lemma finite_Pow_iff [iff]: "finite (Pow A) = finite A"
wenzelm@12396
   481
proof
wenzelm@12396
   482
  assume "finite (Pow A)"
wenzelm@12396
   483
  with _ have "finite ((%x. {x}) ` A)" by (rule finite_subset) blast
wenzelm@12396
   484
  thus "finite A" by (rule finite_imageD [unfolded inj_on_def]) simp
wenzelm@12396
   485
next
wenzelm@12396
   486
  assume "finite A"
wenzelm@12396
   487
  thus "finite (Pow A)"
huffman@35216
   488
    by induct (simp_all add: Pow_insert)
wenzelm@12396
   489
qed
wenzelm@12396
   490
nipkow@29916
   491
lemma finite_Collect_subsets[simp,intro]: "finite A \<Longrightarrow> finite{B. B \<subseteq> A}"
nipkow@29916
   492
by(simp add: Pow_def[symmetric])
nipkow@15392
   493
nipkow@29918
   494
nipkow@15392
   495
lemma finite_UnionD: "finite(\<Union>A) \<Longrightarrow> finite A"
nipkow@15392
   496
by(blast intro: finite_subset[OF subset_Pow_Union])
nipkow@15392
   497
nipkow@15392
   498
nipkow@31441
   499
lemma finite_subset_image:
nipkow@31441
   500
  assumes "finite B"
nipkow@31441
   501
  shows "B \<subseteq> f ` A \<Longrightarrow> \<exists>C\<subseteq>A. finite C \<and> B = f ` C"
nipkow@31441
   502
using assms proof(induct)
nipkow@31441
   503
  case empty thus ?case by simp
nipkow@31441
   504
next
nipkow@31441
   505
  case insert thus ?case
nipkow@31441
   506
    by (clarsimp simp del: image_insert simp add: image_insert[symmetric])
nipkow@31441
   507
       blast
nipkow@31441
   508
qed
nipkow@31441
   509
nipkow@31441
   510
haftmann@26441
   511
subsection {* Class @{text finite}  *}
haftmann@26041
   512
haftmann@26041
   513
setup {* Sign.add_path "finite" *} -- {*FIXME: name tweaking*}
haftmann@29797
   514
class finite =
haftmann@26041
   515
  assumes finite_UNIV: "finite (UNIV \<Colon> 'a set)"
haftmann@26041
   516
setup {* Sign.parent_path *}
haftmann@26041
   517
hide const finite
haftmann@26041
   518
huffman@27430
   519
context finite
huffman@27430
   520
begin
huffman@27430
   521
huffman@27430
   522
lemma finite [simp]: "finite (A \<Colon> 'a set)"
haftmann@26441
   523
  by (rule subset_UNIV finite_UNIV finite_subset)+
haftmann@26041
   524
huffman@27430
   525
end
huffman@27430
   526
haftmann@26146
   527
lemma UNIV_unit [noatp]:
haftmann@26041
   528
  "UNIV = {()}" by auto
haftmann@26041
   529
haftmann@35719
   530
instance unit :: finite proof
haftmann@35719
   531
qed (simp add: UNIV_unit)
haftmann@26146
   532
haftmann@26146
   533
lemma UNIV_bool [noatp]:
haftmann@26041
   534
  "UNIV = {False, True}" by auto
haftmann@26041
   535
haftmann@35719
   536
instance bool :: finite proof
haftmann@35719
   537
qed (simp add: UNIV_bool)
haftmann@35719
   538
haftmann@35719
   539
instance * :: (finite, finite) finite proof
haftmann@35719
   540
qed (simp only: UNIV_Times_UNIV [symmetric] finite_cartesian_product finite)
haftmann@26146
   541
haftmann@35719
   542
lemma finite_option_UNIV [simp]:
haftmann@35719
   543
  "finite (UNIV :: 'a option set) = finite (UNIV :: 'a set)"
haftmann@35719
   544
  by (auto simp add: UNIV_option_conv elim: finite_imageD intro: inj_Some)
haftmann@35719
   545
haftmann@35719
   546
instance option :: (finite) finite proof
haftmann@35719
   547
qed (simp add: UNIV_option_conv)
haftmann@26146
   548
haftmann@26041
   549
lemma inj_graph: "inj (%f. {(x, y). y = f x})"
haftmann@26041
   550
  by (rule inj_onI, auto simp add: expand_set_eq expand_fun_eq)
haftmann@26041
   551
haftmann@26146
   552
instance "fun" :: (finite, finite) finite
haftmann@26146
   553
proof
haftmann@26041
   554
  show "finite (UNIV :: ('a => 'b) set)"
haftmann@26041
   555
  proof (rule finite_imageD)
haftmann@26041
   556
    let ?graph = "%f::'a => 'b. {(x, y). y = f x}"
berghofe@26792
   557
    have "range ?graph \<subseteq> Pow UNIV" by simp
berghofe@26792
   558
    moreover have "finite (Pow (UNIV :: ('a * 'b) set))"
berghofe@26792
   559
      by (simp only: finite_Pow_iff finite)
berghofe@26792
   560
    ultimately show "finite (range ?graph)"
berghofe@26792
   561
      by (rule finite_subset)
haftmann@26041
   562
    show "inj ?graph" by (rule inj_graph)
haftmann@26041
   563
  qed
haftmann@26041
   564
qed
haftmann@26041
   565
haftmann@35719
   566
instance "+" :: (finite, finite) finite proof
haftmann@35719
   567
qed (simp only: UNIV_Plus_UNIV [symmetric] finite_Plus finite)
haftmann@27981
   568
haftmann@26041
   569
nipkow@15392
   570
subsection {* A fold functional for finite sets *}
nipkow@15392
   571
nipkow@15392
   572
text {* The intended behaviour is
wenzelm@31916
   573
@{text "fold f z {x\<^isub>1, ..., x\<^isub>n} = f x\<^isub>1 (\<dots> (f x\<^isub>n z)\<dots>)"}
nipkow@28853
   574
if @{text f} is ``left-commutative'':
nipkow@15392
   575
*}
nipkow@15392
   576
nipkow@28853
   577
locale fun_left_comm =
nipkow@28853
   578
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
nipkow@28853
   579
  assumes fun_left_comm: "f x (f y z) = f y (f x z)"
nipkow@28853
   580
begin
nipkow@28853
   581
nipkow@28853
   582
text{* On a functional level it looks much nicer: *}
nipkow@28853
   583
nipkow@28853
   584
lemma fun_comp_comm:  "f x \<circ> f y = f y \<circ> f x"
nipkow@28853
   585
by (simp add: fun_left_comm expand_fun_eq)
nipkow@28853
   586
nipkow@28853
   587
end
nipkow@28853
   588
nipkow@28853
   589
inductive fold_graph :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool"
nipkow@28853
   590
for f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b" and z :: 'b where
nipkow@28853
   591
  emptyI [intro]: "fold_graph f z {} z" |
nipkow@28853
   592
  insertI [intro]: "x \<notin> A \<Longrightarrow> fold_graph f z A y
nipkow@28853
   593
      \<Longrightarrow> fold_graph f z (insert x A) (f x y)"
nipkow@28853
   594
nipkow@28853
   595
inductive_cases empty_fold_graphE [elim!]: "fold_graph f z {} x"
nipkow@28853
   596
nipkow@28853
   597
definition fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b" where
nipkow@28853
   598
[code del]: "fold f z A = (THE y. fold_graph f z A y)"
nipkow@15392
   599
paulson@15498
   600
text{*A tempting alternative for the definiens is
nipkow@28853
   601
@{term "if finite A then THE y. fold_graph f z A y else e"}.
paulson@15498
   602
It allows the removal of finiteness assumptions from the theorems
nipkow@28853
   603
@{text fold_comm}, @{text fold_reindex} and @{text fold_distrib}.
nipkow@28853
   604
The proofs become ugly. It is not worth the effort. (???) *}
nipkow@28853
   605
nipkow@28853
   606
nipkow@28853
   607
lemma Diff1_fold_graph:
nipkow@28853
   608
  "fold_graph f z (A - {x}) y \<Longrightarrow> x \<in> A \<Longrightarrow> fold_graph f z A (f x y)"
nipkow@28853
   609
by (erule insert_Diff [THEN subst], rule fold_graph.intros, auto)
nipkow@28853
   610
nipkow@28853
   611
lemma fold_graph_imp_finite: "fold_graph f z A x \<Longrightarrow> finite A"
nipkow@28853
   612
by (induct set: fold_graph) auto
nipkow@28853
   613
nipkow@28853
   614
lemma finite_imp_fold_graph: "finite A \<Longrightarrow> \<exists>x. fold_graph f z A x"
nipkow@28853
   615
by (induct set: finite) auto
nipkow@28853
   616
nipkow@28853
   617
nipkow@28853
   618
subsubsection{*From @{const fold_graph} to @{term fold}*}
nipkow@15392
   619
paulson@15510
   620
lemma image_less_Suc: "h ` {i. i < Suc m} = insert (h m) (h ` {i. i < m})"
wenzelm@19868
   621
  by (auto simp add: less_Suc_eq) 
paulson@15510
   622
paulson@15510
   623
lemma insert_image_inj_on_eq:
paulson@15510
   624
     "[|insert (h m) A = h ` {i. i < Suc m}; h m \<notin> A; 
paulson@15510
   625
        inj_on h {i. i < Suc m}|] 
paulson@15510
   626
      ==> A = h ` {i. i < m}"
paulson@15510
   627
apply (auto simp add: image_less_Suc inj_on_def)
paulson@15510
   628
apply (blast intro: less_trans) 
paulson@15510
   629
done
paulson@15510
   630
paulson@15510
   631
lemma insert_inj_onE:
paulson@15510
   632
  assumes aA: "insert a A = h`{i::nat. i<n}" and anot: "a \<notin> A" 
paulson@15510
   633
      and inj_on: "inj_on h {i::nat. i<n}"
paulson@15510
   634
  shows "\<exists>hm m. inj_on hm {i::nat. i<m} & A = hm ` {i. i<m} & m < n"
paulson@15510
   635
proof (cases n)
paulson@15510
   636
  case 0 thus ?thesis using aA by auto
paulson@15510
   637
next
paulson@15510
   638
  case (Suc m)
wenzelm@23389
   639
  have nSuc: "n = Suc m" by fact
paulson@15510
   640
  have mlessn: "m<n" by (simp add: nSuc)
paulson@15532
   641
  from aA obtain k where hkeq: "h k = a" and klessn: "k<n" by (blast elim!: equalityE)
nipkow@27165
   642
  let ?hm = "Fun.swap k m h"
paulson@15520
   643
  have inj_hm: "inj_on ?hm {i. i < n}" using klessn mlessn 
huffman@35216
   644
    by (simp add: inj_on)
paulson@15510
   645
  show ?thesis
paulson@15520
   646
  proof (intro exI conjI)
paulson@15520
   647
    show "inj_on ?hm {i. i < m}" using inj_hm
paulson@15510
   648
      by (auto simp add: nSuc less_Suc_eq intro: subset_inj_on)
paulson@15520
   649
    show "m<n" by (rule mlessn)
paulson@15520
   650
    show "A = ?hm ` {i. i < m}" 
paulson@15520
   651
    proof (rule insert_image_inj_on_eq)
nipkow@27165
   652
      show "inj_on (Fun.swap k m h) {i. i < Suc m}" using inj_hm nSuc by simp
paulson@15520
   653
      show "?hm m \<notin> A" by (simp add: swap_def hkeq anot) 
paulson@15520
   654
      show "insert (?hm m) A = ?hm ` {i. i < Suc m}"
wenzelm@32960
   655
        using aA hkeq nSuc klessn
wenzelm@32960
   656
        by (auto simp add: swap_def image_less_Suc fun_upd_image 
wenzelm@32960
   657
                           less_Suc_eq inj_on_image_set_diff [OF inj_on])
nipkow@15479
   658
    qed
nipkow@15479
   659
  qed
nipkow@15479
   660
qed
nipkow@15479
   661
nipkow@28853
   662
context fun_left_comm
haftmann@26041
   663
begin
haftmann@26041
   664
nipkow@28853
   665
lemma fold_graph_determ_aux:
nipkow@28853
   666
  "A = h`{i::nat. i<n} \<Longrightarrow> inj_on h {i. i<n}
nipkow@28853
   667
   \<Longrightarrow> fold_graph f z A x \<Longrightarrow> fold_graph f z A x'
nipkow@15392
   668
   \<Longrightarrow> x' = x"
nipkow@28853
   669
proof (induct n arbitrary: A x x' h rule: less_induct)
paulson@15510
   670
  case (less n)
nipkow@28853
   671
  have IH: "\<And>m h A x x'. m < n \<Longrightarrow> A = h ` {i. i<m}
nipkow@28853
   672
      \<Longrightarrow> inj_on h {i. i<m} \<Longrightarrow> fold_graph f z A x
nipkow@28853
   673
      \<Longrightarrow> fold_graph f z A x' \<Longrightarrow> x' = x" by fact
nipkow@28853
   674
  have Afoldx: "fold_graph f z A x" and Afoldx': "fold_graph f z A x'"
nipkow@28853
   675
    and A: "A = h`{i. i<n}" and injh: "inj_on h {i. i<n}" by fact+
nipkow@28853
   676
  show ?case
nipkow@28853
   677
  proof (rule fold_graph.cases [OF Afoldx])
nipkow@28853
   678
    assume "A = {}" and "x = z"
nipkow@28853
   679
    with Afoldx' show "x' = x" by auto
nipkow@28853
   680
  next
nipkow@28853
   681
    fix B b u
nipkow@28853
   682
    assume AbB: "A = insert b B" and x: "x = f b u"
nipkow@28853
   683
      and notinB: "b \<notin> B" and Bu: "fold_graph f z B u"
nipkow@28853
   684
    show "x'=x" 
nipkow@28853
   685
    proof (rule fold_graph.cases [OF Afoldx'])
nipkow@28853
   686
      assume "A = {}" and "x' = z"
nipkow@28853
   687
      with AbB show "x' = x" by blast
nipkow@15392
   688
    next
nipkow@28853
   689
      fix C c v
nipkow@28853
   690
      assume AcC: "A = insert c C" and x': "x' = f c v"
nipkow@28853
   691
        and notinC: "c \<notin> C" and Cv: "fold_graph f z C v"
nipkow@28853
   692
      from A AbB have Beq: "insert b B = h`{i. i<n}" by simp
nipkow@28853
   693
      from insert_inj_onE [OF Beq notinB injh]
nipkow@28853
   694
      obtain hB mB where inj_onB: "inj_on hB {i. i < mB}" 
nipkow@28853
   695
        and Beq: "B = hB ` {i. i < mB}" and lessB: "mB < n" by auto 
nipkow@28853
   696
      from A AcC have Ceq: "insert c C = h`{i. i<n}" by simp
nipkow@28853
   697
      from insert_inj_onE [OF Ceq notinC injh]
nipkow@28853
   698
      obtain hC mC where inj_onC: "inj_on hC {i. i < mC}"
nipkow@28853
   699
        and Ceq: "C = hC ` {i. i < mC}" and lessC: "mC < n" by auto 
nipkow@28853
   700
      show "x'=x"
nipkow@28853
   701
      proof cases
nipkow@28853
   702
        assume "b=c"
wenzelm@32960
   703
        then moreover have "B = C" using AbB AcC notinB notinC by auto
wenzelm@32960
   704
        ultimately show ?thesis  using Bu Cv x x' IH [OF lessC Ceq inj_onC]
nipkow@28853
   705
          by auto
nipkow@15392
   706
      next
wenzelm@32960
   707
        assume diff: "b \<noteq> c"
wenzelm@32960
   708
        let ?D = "B - {c}"
wenzelm@32960
   709
        have B: "B = insert c ?D" and C: "C = insert b ?D"
wenzelm@32960
   710
          using AbB AcC notinB notinC diff by(blast elim!:equalityE)+
wenzelm@32960
   711
        have "finite A" by(rule fold_graph_imp_finite [OF Afoldx])
wenzelm@32960
   712
        with AbB have "finite ?D" by simp
wenzelm@32960
   713
        then obtain d where Dfoldd: "fold_graph f z ?D d"
wenzelm@32960
   714
          using finite_imp_fold_graph by iprover
wenzelm@32960
   715
        moreover have cinB: "c \<in> B" using B by auto
wenzelm@32960
   716
        ultimately have "fold_graph f z B (f c d)" by(rule Diff1_fold_graph)
wenzelm@32960
   717
        hence "f c d = u" by (rule IH [OF lessB Beq inj_onB Bu]) 
nipkow@28853
   718
        moreover have "f b d = v"
wenzelm@32960
   719
        proof (rule IH[OF lessC Ceq inj_onC Cv])
wenzelm@32960
   720
          show "fold_graph f z C (f b d)" using C notinB Dfoldd by fastsimp
wenzelm@32960
   721
        qed
wenzelm@32960
   722
        ultimately show ?thesis
nipkow@28853
   723
          using fun_left_comm [of c b] x x' by (auto simp add: o_def)
nipkow@15392
   724
      qed
nipkow@15392
   725
    qed
nipkow@15392
   726
  qed
nipkow@28853
   727
qed
nipkow@28853
   728
nipkow@28853
   729
lemma fold_graph_determ:
nipkow@28853
   730
  "fold_graph f z A x \<Longrightarrow> fold_graph f z A y \<Longrightarrow> y = x"
nipkow@28853
   731
apply (frule fold_graph_imp_finite [THEN finite_imp_nat_seg_image_inj_on]) 
nipkow@28853
   732
apply (blast intro: fold_graph_determ_aux [rule_format])
nipkow@15392
   733
done
nipkow@15392
   734
nipkow@28853
   735
lemma fold_equality:
nipkow@28853
   736
  "fold_graph f z A y \<Longrightarrow> fold f z A = y"
nipkow@28853
   737
by (unfold fold_def) (blast intro: fold_graph_determ)
nipkow@15392
   738
nipkow@15392
   739
text{* The base case for @{text fold}: *}
nipkow@15392
   740
nipkow@28853
   741
lemma (in -) fold_empty [simp]: "fold f z {} = z"
nipkow@28853
   742
by (unfold fold_def) blast
nipkow@28853
   743
nipkow@28853
   744
text{* The various recursion equations for @{const fold}: *}
nipkow@28853
   745
nipkow@28853
   746
lemma fold_insert_aux: "x \<notin> A
nipkow@28853
   747
  \<Longrightarrow> fold_graph f z (insert x A) v \<longleftrightarrow>
nipkow@28853
   748
      (\<exists>y. fold_graph f z A y \<and> v = f x y)"
nipkow@28853
   749
apply auto
nipkow@28853
   750
apply (rule_tac A1 = A and f1 = f in finite_imp_fold_graph [THEN exE])
nipkow@28853
   751
 apply (fastsimp dest: fold_graph_imp_finite)
nipkow@28853
   752
apply (blast intro: fold_graph_determ)
nipkow@28853
   753
done
nipkow@15392
   754
haftmann@26041
   755
lemma fold_insert [simp]:
nipkow@28853
   756
  "finite A ==> x \<notin> A ==> fold f z (insert x A) = f x (fold f z A)"
nipkow@28853
   757
apply (simp add: fold_def fold_insert_aux)
nipkow@28853
   758
apply (rule the_equality)
nipkow@28853
   759
 apply (auto intro: finite_imp_fold_graph
nipkow@28853
   760
        cong add: conj_cong simp add: fold_def[symmetric] fold_equality)
nipkow@28853
   761
done
nipkow@28853
   762
nipkow@28853
   763
lemma fold_fun_comm:
nipkow@28853
   764
  "finite A \<Longrightarrow> f x (fold f z A) = fold f (f x z) A"
nipkow@28853
   765
proof (induct rule: finite_induct)
nipkow@28853
   766
  case empty then show ?case by simp
nipkow@28853
   767
next
nipkow@28853
   768
  case (insert y A) then show ?case
nipkow@28853
   769
    by (simp add: fun_left_comm[of x])
nipkow@28853
   770
qed
nipkow@28853
   771
nipkow@28853
   772
lemma fold_insert2:
nipkow@28853
   773
  "finite A \<Longrightarrow> x \<notin> A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
huffman@35216
   774
by (simp add: fold_fun_comm)
nipkow@15392
   775
haftmann@26041
   776
lemma fold_rec:
nipkow@28853
   777
assumes "finite A" and "x \<in> A"
nipkow@28853
   778
shows "fold f z A = f x (fold f z (A - {x}))"
nipkow@28853
   779
proof -
nipkow@28853
   780
  have A: "A = insert x (A - {x})" using `x \<in> A` by blast
nipkow@28853
   781
  then have "fold f z A = fold f z (insert x (A - {x}))" by simp
nipkow@28853
   782
  also have "\<dots> = f x (fold f z (A - {x}))"
nipkow@28853
   783
    by (rule fold_insert) (simp add: `finite A`)+
nipkow@15535
   784
  finally show ?thesis .
nipkow@15535
   785
qed
nipkow@15535
   786
nipkow@28853
   787
lemma fold_insert_remove:
nipkow@28853
   788
  assumes "finite A"
nipkow@28853
   789
  shows "fold f z (insert x A) = f x (fold f z (A - {x}))"
nipkow@28853
   790
proof -
nipkow@28853
   791
  from `finite A` have "finite (insert x A)" by auto
nipkow@28853
   792
  moreover have "x \<in> insert x A" by auto
nipkow@28853
   793
  ultimately have "fold f z (insert x A) = f x (fold f z (insert x A - {x}))"
nipkow@28853
   794
    by (rule fold_rec)
nipkow@28853
   795
  then show ?thesis by simp
nipkow@28853
   796
qed
nipkow@28853
   797
haftmann@26041
   798
end
nipkow@15392
   799
nipkow@15480
   800
text{* A simplified version for idempotent functions: *}
nipkow@15480
   801
nipkow@28853
   802
locale fun_left_comm_idem = fun_left_comm +
nipkow@28853
   803
  assumes fun_left_idem: "f x (f x z) = f x z"
haftmann@26041
   804
begin
haftmann@26041
   805
nipkow@28853
   806
text{* The nice version: *}
nipkow@28853
   807
lemma fun_comp_idem : "f x o f x = f x"
nipkow@28853
   808
by (simp add: fun_left_idem expand_fun_eq)
nipkow@28853
   809
haftmann@26041
   810
lemma fold_insert_idem:
nipkow@28853
   811
  assumes fin: "finite A"
nipkow@28853
   812
  shows "fold f z (insert x A) = f x (fold f z A)"
nipkow@15480
   813
proof cases
nipkow@28853
   814
  assume "x \<in> A"
nipkow@28853
   815
  then obtain B where "A = insert x B" and "x \<notin> B" by (rule set_insert)
nipkow@28853
   816
  then show ?thesis using assms by (simp add:fun_left_idem)
nipkow@15480
   817
next
nipkow@28853
   818
  assume "x \<notin> A" then show ?thesis using assms by simp
nipkow@15480
   819
qed
nipkow@15480
   820
nipkow@28853
   821
declare fold_insert[simp del] fold_insert_idem[simp]
nipkow@28853
   822
nipkow@28853
   823
lemma fold_insert_idem2:
nipkow@28853
   824
  "finite A \<Longrightarrow> fold f z (insert x A) = fold f (f x z) A"
nipkow@28853
   825
by(simp add:fold_fun_comm)
nipkow@15484
   826
haftmann@26041
   827
end
haftmann@26041
   828
nipkow@31992
   829
context ab_semigroup_idem_mult
nipkow@31992
   830
begin
nipkow@31992
   831
nipkow@31992
   832
lemma fun_left_comm_idem: "fun_left_comm_idem(op *)"
nipkow@31992
   833
apply unfold_locales
huffman@35216
   834
 apply (rule mult_left_commute)
huffman@35216
   835
apply (rule mult_left_idem)
nipkow@31992
   836
done
nipkow@31992
   837
nipkow@31992
   838
end
nipkow@31992
   839
haftmann@35028
   840
context semilattice_inf
nipkow@31992
   841
begin
nipkow@31992
   842
nipkow@31992
   843
lemma ab_semigroup_idem_mult_inf: "ab_semigroup_idem_mult inf"
nipkow@31992
   844
proof qed (rule inf_assoc inf_commute inf_idem)+
nipkow@31992
   845
nipkow@31992
   846
lemma fold_inf_insert[simp]: "finite A \<Longrightarrow> fold inf b (insert a A) = inf a (fold inf b A)"
nipkow@31992
   847
by(rule fun_left_comm_idem.fold_insert_idem[OF ab_semigroup_idem_mult.fun_left_comm_idem[OF ab_semigroup_idem_mult_inf]])
nipkow@31992
   848
nipkow@31992
   849
lemma inf_le_fold_inf: "finite A \<Longrightarrow> ALL a:A. b \<le> a \<Longrightarrow> inf b c \<le> fold inf c A"
haftmann@32064
   850
by (induct pred: finite) (auto intro: le_infI1)
nipkow@31992
   851
nipkow@31992
   852
lemma fold_inf_le_inf: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> fold inf b A \<le> inf a b"
nipkow@31992
   853
proof(induct arbitrary: a pred:finite)
nipkow@31992
   854
  case empty thus ?case by simp
nipkow@31992
   855
next
nipkow@31992
   856
  case (insert x A)
nipkow@31992
   857
  show ?case
nipkow@31992
   858
  proof cases
nipkow@31992
   859
    assume "A = {}" thus ?thesis using insert by simp
nipkow@31992
   860
  next
haftmann@32064
   861
    assume "A \<noteq> {}" thus ?thesis using insert by (auto intro: le_infI2)
nipkow@31992
   862
  qed
nipkow@31992
   863
qed
nipkow@31992
   864
nipkow@31992
   865
end
nipkow@31992
   866
haftmann@35028
   867
context semilattice_sup
nipkow@31992
   868
begin
nipkow@31992
   869
nipkow@31992
   870
lemma ab_semigroup_idem_mult_sup: "ab_semigroup_idem_mult sup"
haftmann@35028
   871
by (rule semilattice_inf.ab_semigroup_idem_mult_inf)(rule dual_semilattice)
nipkow@31992
   872
nipkow@31992
   873
lemma fold_sup_insert[simp]: "finite A \<Longrightarrow> fold sup b (insert a A) = sup a (fold sup b A)"
haftmann@35028
   874
by(rule semilattice_inf.fold_inf_insert)(rule dual_semilattice)
nipkow@31992
   875
nipkow@31992
   876
lemma fold_sup_le_sup: "finite A \<Longrightarrow> ALL a:A. a \<le> b \<Longrightarrow> fold sup c A \<le> sup b c"
haftmann@35028
   877
by(rule semilattice_inf.inf_le_fold_inf)(rule dual_semilattice)
nipkow@31992
   878
nipkow@31992
   879
lemma sup_le_fold_sup: "finite A \<Longrightarrow> a \<in> A \<Longrightarrow> sup a b \<le> fold sup b A"
haftmann@35028
   880
by(rule semilattice_inf.fold_inf_le_inf)(rule dual_semilattice)
nipkow@31992
   881
nipkow@31992
   882
end
nipkow@31992
   883
nipkow@31992
   884
nipkow@28853
   885
subsubsection{* The derived combinator @{text fold_image} *}
nipkow@28853
   886
nipkow@28853
   887
definition fold_image :: "('b \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
nipkow@28853
   888
where "fold_image f g = fold (%x y. f (g x) y)"
nipkow@28853
   889
nipkow@28853
   890
lemma fold_image_empty[simp]: "fold_image f g z {} = z"
nipkow@28853
   891
by(simp add:fold_image_def)
nipkow@15392
   892
haftmann@26041
   893
context ab_semigroup_mult
haftmann@26041
   894
begin
haftmann@26041
   895
nipkow@28853
   896
lemma fold_image_insert[simp]:
nipkow@28853
   897
assumes "finite A" and "a \<notin> A"
nipkow@28853
   898
shows "fold_image times g z (insert a A) = g a * (fold_image times g z A)"
nipkow@28853
   899
proof -
ballarin@29223
   900
  interpret I: fun_left_comm "%x y. (g x) * y"
nipkow@28853
   901
    by unfold_locales (simp add: mult_ac)
nipkow@31992
   902
  show ?thesis using assms by(simp add:fold_image_def)
nipkow@28853
   903
qed
nipkow@28853
   904
nipkow@28853
   905
(*
haftmann@26041
   906
lemma fold_commute:
haftmann@26041
   907
  "finite A ==> (!!z. x * (fold times g z A) = fold times g (x * z) A)"
berghofe@22262
   908
  apply (induct set: finite)
wenzelm@21575
   909
   apply simp
haftmann@26041
   910
  apply (simp add: mult_left_commute [of x])
nipkow@15392
   911
  done
nipkow@15392
   912
haftmann@26041
   913
lemma fold_nest_Un_Int:
nipkow@15392
   914
  "finite A ==> finite B
haftmann@26041
   915
    ==> fold times g (fold times g z B) A = fold times g (fold times g z (A Int B)) (A Un B)"
berghofe@22262
   916
  apply (induct set: finite)
wenzelm@21575
   917
   apply simp
nipkow@15392
   918
  apply (simp add: fold_commute Int_insert_left insert_absorb)
nipkow@15392
   919
  done
nipkow@15392
   920
haftmann@26041
   921
lemma fold_nest_Un_disjoint:
nipkow@15392
   922
  "finite A ==> finite B ==> A Int B = {}
haftmann@26041
   923
    ==> fold times g z (A Un B) = fold times g (fold times g z B) A"
nipkow@15392
   924
  by (simp add: fold_nest_Un_Int)
nipkow@28853
   925
*)
nipkow@28853
   926
nipkow@28853
   927
lemma fold_image_reindex:
paulson@15487
   928
assumes fin: "finite A"
nipkow@28853
   929
shows "inj_on h A \<Longrightarrow> fold_image times g z (h`A) = fold_image times (g\<circ>h) z A"
nipkow@31992
   930
using fin by induct auto
nipkow@15392
   931
nipkow@28853
   932
(*
haftmann@26041
   933
text{*
haftmann@26041
   934
  Fusion theorem, as described in Graham Hutton's paper,
haftmann@26041
   935
  A Tutorial on the Universality and Expressiveness of Fold,
haftmann@26041
   936
  JFP 9:4 (355-372), 1999.
haftmann@26041
   937
*}
haftmann@26041
   938
haftmann@26041
   939
lemma fold_fusion:
ballarin@27611
   940
  assumes "ab_semigroup_mult g"
haftmann@26041
   941
  assumes fin: "finite A"
haftmann@26041
   942
    and hyp: "\<And>x y. h (g x y) = times x (h y)"
haftmann@26041
   943
  shows "h (fold g j w A) = fold times j (h w) A"
ballarin@27611
   944
proof -
ballarin@29223
   945
  class_interpret ab_semigroup_mult [g] by fact
ballarin@27611
   946
  show ?thesis using fin hyp by (induct set: finite) simp_all
ballarin@27611
   947
qed
nipkow@28853
   948
*)
nipkow@28853
   949
nipkow@28853
   950
lemma fold_image_cong:
nipkow@28853
   951
  "finite A \<Longrightarrow>
nipkow@28853
   952
  (!!x. x:A ==> g x = h x) ==> fold_image times g z A = fold_image times h z A"
nipkow@28853
   953
apply (subgoal_tac "ALL C. C <= A --> (ALL x:C. g x = h x) --> fold_image times g z C = fold_image times h z C")
nipkow@28853
   954
 apply simp
nipkow@28853
   955
apply (erule finite_induct, simp)
nipkow@28853
   956
apply (simp add: subset_insert_iff, clarify)
nipkow@28853
   957
apply (subgoal_tac "finite C")
nipkow@28853
   958
 prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
nipkow@28853
   959
apply (subgoal_tac "C = insert x (C - {x})")
nipkow@28853
   960
 prefer 2 apply blast
nipkow@28853
   961
apply (erule ssubst)
nipkow@28853
   962
apply (drule spec)
nipkow@28853
   963
apply (erule (1) notE impE)
nipkow@28853
   964
apply (simp add: Ball_def del: insert_Diff_single)
nipkow@28853
   965
done
nipkow@15392
   966
haftmann@26041
   967
end
haftmann@26041
   968
haftmann@26041
   969
context comm_monoid_mult
haftmann@26041
   970
begin
haftmann@26041
   971
nipkow@28853
   972
lemma fold_image_Un_Int:
haftmann@26041
   973
  "finite A ==> finite B ==>
nipkow@28853
   974
    fold_image times g 1 A * fold_image times g 1 B =
nipkow@28853
   975
    fold_image times g 1 (A Un B) * fold_image times g 1 (A Int B)"
nipkow@28853
   976
by (induct set: finite) 
nipkow@28853
   977
   (auto simp add: mult_ac insert_absorb Int_insert_left)
haftmann@26041
   978
haftmann@26041
   979
corollary fold_Un_disjoint:
haftmann@26041
   980
  "finite A ==> finite B ==> A Int B = {} ==>
nipkow@28853
   981
   fold_image times g 1 (A Un B) =
nipkow@28853
   982
   fold_image times g 1 A * fold_image times g 1 B"
nipkow@28853
   983
by (simp add: fold_image_Un_Int)
nipkow@28853
   984
nipkow@28853
   985
lemma fold_image_UN_disjoint:
haftmann@26041
   986
  "\<lbrakk> finite I; ALL i:I. finite (A i);
haftmann@26041
   987
     ALL i:I. ALL j:I. i \<noteq> j --> A i Int A j = {} \<rbrakk>
nipkow@28853
   988
   \<Longrightarrow> fold_image times g 1 (UNION I A) =
nipkow@28853
   989
       fold_image times (%i. fold_image times g 1 (A i)) 1 I"
nipkow@28853
   990
apply (induct set: finite, simp, atomize)
nipkow@28853
   991
apply (subgoal_tac "ALL i:F. x \<noteq> i")
nipkow@28853
   992
 prefer 2 apply blast
nipkow@28853
   993
apply (subgoal_tac "A x Int UNION F A = {}")
nipkow@28853
   994
 prefer 2 apply blast
nipkow@28853
   995
apply (simp add: fold_Un_disjoint)
nipkow@28853
   996
done
nipkow@28853
   997
nipkow@28853
   998
lemma fold_image_Sigma: "finite A ==> ALL x:A. finite (B x) ==>
nipkow@28853
   999
  fold_image times (%x. fold_image times (g x) 1 (B x)) 1 A =
nipkow@28853
  1000
  fold_image times (split g) 1 (SIGMA x:A. B x)"
nipkow@15392
  1001
apply (subst Sigma_def)
nipkow@28853
  1002
apply (subst fold_image_UN_disjoint, assumption, simp)
nipkow@15392
  1003
 apply blast
nipkow@28853
  1004
apply (erule fold_image_cong)
nipkow@28853
  1005
apply (subst fold_image_UN_disjoint, simp, simp)
nipkow@15392
  1006
 apply blast
paulson@15506
  1007
apply simp
nipkow@15392
  1008
done
nipkow@15392
  1009
nipkow@28853
  1010
lemma fold_image_distrib: "finite A \<Longrightarrow>
nipkow@28853
  1011
   fold_image times (%x. g x * h x) 1 A =
nipkow@28853
  1012
   fold_image times g 1 A *  fold_image times h 1 A"
nipkow@28853
  1013
by (erule finite_induct) (simp_all add: mult_ac)
haftmann@26041
  1014
chaieb@30260
  1015
lemma fold_image_related: 
chaieb@30260
  1016
  assumes Re: "R e e" 
chaieb@30260
  1017
  and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" 
chaieb@30260
  1018
  and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
chaieb@30260
  1019
  shows "R (fold_image (op *) h e S) (fold_image (op *) g e S)"
chaieb@30260
  1020
  using fS by (rule finite_subset_induct) (insert assms, auto)
chaieb@30260
  1021
chaieb@30260
  1022
lemma  fold_image_eq_general:
chaieb@30260
  1023
  assumes fS: "finite S"
chaieb@30260
  1024
  and h: "\<forall>y\<in>S'. \<exists>!x. x\<in> S \<and> h(x) = y" 
chaieb@30260
  1025
  and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2(h x) = f1 x"
chaieb@30260
  1026
  shows "fold_image (op *) f1 e S = fold_image (op *) f2 e S'"
chaieb@30260
  1027
proof-
chaieb@30260
  1028
  from h f12 have hS: "h ` S = S'" by auto
chaieb@30260
  1029
  {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
chaieb@30260
  1030
    from f12 h H  have "x = y" by auto }
chaieb@30260
  1031
  hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
chaieb@30260
  1032
  from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto 
chaieb@30260
  1033
  from hS have "fold_image (op *) f2 e S' = fold_image (op *) f2 e (h ` S)" by simp
chaieb@30260
  1034
  also have "\<dots> = fold_image (op *) (f2 o h) e S" 
chaieb@30260
  1035
    using fold_image_reindex[OF fS hinj, of f2 e] .
chaieb@30260
  1036
  also have "\<dots> = fold_image (op *) f1 e S " using th fold_image_cong[OF fS, of "f2 o h" f1 e]
chaieb@30260
  1037
    by blast
chaieb@30260
  1038
  finally show ?thesis ..
chaieb@30260
  1039
qed
chaieb@30260
  1040
chaieb@30260
  1041
lemma fold_image_eq_general_inverses:
chaieb@30260
  1042
  assumes fS: "finite S" 
chaieb@30260
  1043
  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
chaieb@30260
  1044
  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x  \<and> g (h x) = f x"
chaieb@30260
  1045
  shows "fold_image (op *) f e S = fold_image (op *) g e T"
chaieb@30260
  1046
  (* metis solves it, but not yet available here *)
chaieb@30260
  1047
  apply (rule fold_image_eq_general[OF fS, of T h g f e])
chaieb@30260
  1048
  apply (rule ballI)
chaieb@30260
  1049
  apply (frule kh)
chaieb@30260
  1050
  apply (rule ex1I[])
chaieb@30260
  1051
  apply blast
chaieb@30260
  1052
  apply clarsimp
chaieb@30260
  1053
  apply (drule hk) apply simp
chaieb@30260
  1054
  apply (rule sym)
chaieb@30260
  1055
  apply (erule conjunct1[OF conjunct2[OF hk]])
chaieb@30260
  1056
  apply (rule ballI)
chaieb@30260
  1057
  apply (drule  hk)
chaieb@30260
  1058
  apply blast
chaieb@30260
  1059
  done
chaieb@30260
  1060
haftmann@26041
  1061
end
haftmann@22917
  1062
nipkow@25162
  1063
nipkow@15392
  1064
subsection{* A fold functional for non-empty sets *}
nipkow@15392
  1065
nipkow@15392
  1066
text{* Does not require start value. *}
wenzelm@12396
  1067
berghofe@23736
  1068
inductive
berghofe@22262
  1069
  fold1Set :: "('a => 'a => 'a) => 'a set => 'a => bool"
berghofe@22262
  1070
  for f :: "'a => 'a => 'a"
berghofe@22262
  1071
where
paulson@15506
  1072
  fold1Set_insertI [intro]:
nipkow@28853
  1073
   "\<lbrakk> fold_graph f a A x; a \<notin> A \<rbrakk> \<Longrightarrow> fold1Set f (insert a A) x"
wenzelm@12396
  1074
haftmann@35416
  1075
definition fold1 :: "('a => 'a => 'a) => 'a set => 'a" where
berghofe@22262
  1076
  "fold1 f A == THE x. fold1Set f A x"
paulson@15506
  1077
paulson@15506
  1078
lemma fold1Set_nonempty:
haftmann@22917
  1079
  "fold1Set f A x \<Longrightarrow> A \<noteq> {}"
nipkow@28853
  1080
by(erule fold1Set.cases, simp_all)
nipkow@15392
  1081
berghofe@23736
  1082
inductive_cases empty_fold1SetE [elim!]: "fold1Set f {} x"
berghofe@23736
  1083
berghofe@23736
  1084
inductive_cases insert_fold1SetE [elim!]: "fold1Set f (insert a X) x"
berghofe@22262
  1085
berghofe@22262
  1086
berghofe@22262
  1087
lemma fold1Set_sing [iff]: "(fold1Set f {a} b) = (a = b)"
huffman@35216
  1088
by (blast elim: fold_graph.cases)
nipkow@15392
  1089
haftmann@22917
  1090
lemma fold1_singleton [simp]: "fold1 f {a} = a"
nipkow@28853
  1091
by (unfold fold1_def) blast
wenzelm@12396
  1092
paulson@15508
  1093
lemma finite_nonempty_imp_fold1Set:
berghofe@22262
  1094
  "\<lbrakk> finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> EX x. fold1Set f A x"
paulson@15508
  1095
apply (induct A rule: finite_induct)
nipkow@28853
  1096
apply (auto dest: finite_imp_fold_graph [of _ f])
paulson@15508
  1097
done
paulson@15506
  1098
nipkow@28853
  1099
text{*First, some lemmas about @{const fold_graph}.*}
nipkow@15392
  1100
haftmann@26041
  1101
context ab_semigroup_mult
haftmann@26041
  1102
begin
haftmann@26041
  1103
nipkow@28853
  1104
lemma fun_left_comm: "fun_left_comm(op *)"
nipkow@28853
  1105
by unfold_locales (simp add: mult_ac)
nipkow@28853
  1106
nipkow@28853
  1107
lemma fold_graph_insert_swap:
nipkow@28853
  1108
assumes fold: "fold_graph times (b::'a) A y" and "b \<notin> A"
nipkow@28853
  1109
shows "fold_graph times z (insert b A) (z * y)"
nipkow@28853
  1110
proof -
ballarin@29223
  1111
  interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
nipkow@28853
  1112
from assms show ?thesis
nipkow@28853
  1113
proof (induct rule: fold_graph.induct)
haftmann@26041
  1114
  case emptyI thus ?case by (force simp add: fold_insert_aux mult_commute)
paulson@15508
  1115
next
berghofe@22262
  1116
  case (insertI x A y)
nipkow@28853
  1117
    have "fold_graph times z (insert x (insert b A)) (x * (z * y))"
paulson@15521
  1118
      using insertI by force  --{*how does @{term id} get unfolded?*}
haftmann@26041
  1119
    thus ?case by (simp add: insert_commute mult_ac)
paulson@15508
  1120
qed
nipkow@28853
  1121
qed
nipkow@28853
  1122
nipkow@28853
  1123
lemma fold_graph_permute_diff:
nipkow@28853
  1124
assumes fold: "fold_graph times b A x"
nipkow@28853
  1125
shows "!!a. \<lbrakk>a \<in> A; b \<notin> A\<rbrakk> \<Longrightarrow> fold_graph times a (insert b (A-{a})) x"
paulson@15508
  1126
using fold
nipkow@28853
  1127
proof (induct rule: fold_graph.induct)
paulson@15508
  1128
  case emptyI thus ?case by simp
paulson@15508
  1129
next
berghofe@22262
  1130
  case (insertI x A y)
paulson@15521
  1131
  have "a = x \<or> a \<in> A" using insertI by simp
paulson@15521
  1132
  thus ?case
paulson@15521
  1133
  proof
paulson@15521
  1134
    assume "a = x"
paulson@15521
  1135
    with insertI show ?thesis
nipkow@28853
  1136
      by (simp add: id_def [symmetric], blast intro: fold_graph_insert_swap)
paulson@15521
  1137
  next
paulson@15521
  1138
    assume ainA: "a \<in> A"
nipkow@28853
  1139
    hence "fold_graph times a (insert x (insert b (A - {a}))) (x * y)"
nipkow@28853
  1140
      using insertI by force
paulson@15521
  1141
    moreover
paulson@15521
  1142
    have "insert x (insert b (A - {a})) = insert b (insert x A - {a})"
paulson@15521
  1143
      using ainA insertI by blast
nipkow@28853
  1144
    ultimately show ?thesis by simp
paulson@15508
  1145
  qed
paulson@15508
  1146
qed
paulson@15508
  1147
haftmann@26041
  1148
lemma fold1_eq_fold:
nipkow@28853
  1149
assumes "finite A" "a \<notin> A" shows "fold1 times (insert a A) = fold times a A"
nipkow@28853
  1150
proof -
ballarin@29223
  1151
  interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
nipkow@28853
  1152
  from assms show ?thesis
nipkow@28853
  1153
apply (simp add: fold1_def fold_def)
paulson@15508
  1154
apply (rule the_equality)
nipkow@28853
  1155
apply (best intro: fold_graph_determ theI dest: finite_imp_fold_graph [of _ times])
paulson@15508
  1156
apply (rule sym, clarify)
paulson@15508
  1157
apply (case_tac "Aa=A")
huffman@35216
  1158
 apply (best intro: fold_graph_determ)
nipkow@28853
  1159
apply (subgoal_tac "fold_graph times a A x")
huffman@35216
  1160
 apply (best intro: fold_graph_determ)
nipkow@28853
  1161
apply (subgoal_tac "insert aa (Aa - {a}) = A")
nipkow@28853
  1162
 prefer 2 apply (blast elim: equalityE)
nipkow@28853
  1163
apply (auto dest: fold_graph_permute_diff [where a=a])
paulson@15508
  1164
done
nipkow@28853
  1165
qed
paulson@15508
  1166
paulson@15521
  1167
lemma nonempty_iff: "(A \<noteq> {}) = (\<exists>x B. A = insert x B & x \<notin> B)"
paulson@15521
  1168
apply safe
nipkow@28853
  1169
 apply simp
nipkow@28853
  1170
 apply (drule_tac x=x in spec)
nipkow@28853
  1171
 apply (drule_tac x="A-{x}" in spec, auto)
paulson@15508
  1172
done
paulson@15508
  1173
haftmann@26041
  1174
lemma fold1_insert:
paulson@15521
  1175
  assumes nonempty: "A \<noteq> {}" and A: "finite A" "x \<notin> A"
haftmann@26041
  1176
  shows "fold1 times (insert x A) = x * fold1 times A"
paulson@15521
  1177
proof -
ballarin@29223
  1178
  interpret fun_left_comm "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a" by (rule fun_left_comm)
nipkow@28853
  1179
  from nonempty obtain a A' where "A = insert a A' & a ~: A'"
paulson@15521
  1180
    by (auto simp add: nonempty_iff)
paulson@15521
  1181
  with A show ?thesis
nipkow@28853
  1182
    by (simp add: insert_commute [of x] fold1_eq_fold eq_commute)
paulson@15521
  1183
qed
paulson@15521
  1184
haftmann@26041
  1185
end
haftmann@26041
  1186
haftmann@26041
  1187
context ab_semigroup_idem_mult
haftmann@26041
  1188
begin
haftmann@26041
  1189
haftmann@26041
  1190
lemma fold1_insert_idem [simp]:
paulson@15521
  1191
  assumes nonempty: "A \<noteq> {}" and A: "finite A" 
haftmann@26041
  1192
  shows "fold1 times (insert x A) = x * fold1 times A"
paulson@15521
  1193
proof -
ballarin@29223
  1194
  interpret fun_left_comm_idem "op *::'a \<Rightarrow> 'a \<Rightarrow> 'a"
nipkow@28853
  1195
    by (rule fun_left_comm_idem)
nipkow@28853
  1196
  from nonempty obtain a A' where A': "A = insert a A' & a ~: A'"
paulson@15521
  1197
    by (auto simp add: nonempty_iff)
paulson@15521
  1198
  show ?thesis
paulson@15521
  1199
  proof cases
paulson@15521
  1200
    assume "a = x"
nipkow@28853
  1201
    thus ?thesis
paulson@15521
  1202
    proof cases
paulson@15521
  1203
      assume "A' = {}"
huffman@35216
  1204
      with prems show ?thesis by simp
paulson@15521
  1205
    next
paulson@15521
  1206
      assume "A' \<noteq> {}"
paulson@15521
  1207
      with prems show ?thesis
huffman@35216
  1208
        by (simp add: fold1_insert mult_assoc [symmetric])
paulson@15521
  1209
    qed
paulson@15521
  1210
  next
paulson@15521
  1211
    assume "a \<noteq> x"
paulson@15521
  1212
    with prems show ?thesis
huffman@35216
  1213
      by (simp add: insert_commute fold1_eq_fold)
paulson@15521
  1214
  qed
paulson@15521
  1215
qed
paulson@15506
  1216
haftmann@26041
  1217
lemma hom_fold1_commute:
haftmann@26041
  1218
assumes hom: "!!x y. h (x * y) = h x * h y"
haftmann@26041
  1219
and N: "finite N" "N \<noteq> {}" shows "h (fold1 times N) = fold1 times (h ` N)"
haftmann@22917
  1220
using N proof (induct rule: finite_ne_induct)
haftmann@22917
  1221
  case singleton thus ?case by simp
haftmann@22917
  1222
next
haftmann@22917
  1223
  case (insert n N)
haftmann@26041
  1224
  then have "h (fold1 times (insert n N)) = h (n * fold1 times N)" by simp
haftmann@26041
  1225
  also have "\<dots> = h n * h (fold1 times N)" by(rule hom)
haftmann@26041
  1226
  also have "h (fold1 times N) = fold1 times (h ` N)" by(rule insert)
haftmann@26041
  1227
  also have "times (h n) \<dots> = fold1 times (insert (h n) (h ` N))"
haftmann@22917
  1228
    using insert by(simp)
haftmann@22917
  1229
  also have "insert (h n) (h ` N) = h ` insert n N" by simp
haftmann@22917
  1230
  finally show ?case .
haftmann@22917
  1231
qed
haftmann@22917
  1232
haftmann@32679
  1233
lemma fold1_eq_fold_idem:
haftmann@32679
  1234
  assumes "finite A"
haftmann@32679
  1235
  shows "fold1 times (insert a A) = fold times a A"
haftmann@32679
  1236
proof (cases "a \<in> A")
haftmann@32679
  1237
  case False
haftmann@32679
  1238
  with assms show ?thesis by (simp add: fold1_eq_fold)
haftmann@32679
  1239
next
haftmann@32679
  1240
  interpret fun_left_comm_idem times by (fact fun_left_comm_idem)
haftmann@32679
  1241
  case True then obtain b B
haftmann@32679
  1242
    where A: "A = insert a B" and "a \<notin> B" by (rule set_insert)
haftmann@32679
  1243
  with assms have "finite B" by auto
haftmann@32679
  1244
  then have "fold times a (insert a B) = fold times (a * a) B"
haftmann@32679
  1245
    using `a \<notin> B` by (rule fold_insert2)
haftmann@32679
  1246
  then show ?thesis
haftmann@32679
  1247
    using `a \<notin> B` `finite B` by (simp add: fold1_eq_fold A)
haftmann@32679
  1248
qed
haftmann@32679
  1249
haftmann@26041
  1250
end
haftmann@26041
  1251
paulson@15506
  1252
paulson@15508
  1253
text{* Now the recursion rules for definitions: *}
paulson@15508
  1254
haftmann@22917
  1255
lemma fold1_singleton_def: "g = fold1 f \<Longrightarrow> g {a} = a"
huffman@35216
  1256
by simp
paulson@15508
  1257
haftmann@26041
  1258
lemma (in ab_semigroup_mult) fold1_insert_def:
haftmann@26041
  1259
  "\<lbrakk> g = fold1 times; finite A; x \<notin> A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x * g A"
haftmann@26041
  1260
by (simp add:fold1_insert)
haftmann@26041
  1261
haftmann@26041
  1262
lemma (in ab_semigroup_idem_mult) fold1_insert_idem_def:
haftmann@26041
  1263
  "\<lbrakk> g = fold1 times; finite A; A \<noteq> {} \<rbrakk> \<Longrightarrow> g (insert x A) = x * g A"
haftmann@26041
  1264
by simp
paulson@15508
  1265
paulson@15508
  1266
subsubsection{* Determinacy for @{term fold1Set} *}
paulson@15508
  1267
nipkow@28853
  1268
(*Not actually used!!*)
nipkow@28853
  1269
(*
haftmann@26041
  1270
context ab_semigroup_mult
haftmann@26041
  1271
begin
haftmann@26041
  1272
nipkow@28853
  1273
lemma fold_graph_permute:
nipkow@28853
  1274
  "[|fold_graph times id b (insert a A) x; a \<notin> A; b \<notin> A|]
nipkow@28853
  1275
   ==> fold_graph times id a (insert b A) x"
haftmann@26041
  1276
apply (cases "a=b") 
nipkow@28853
  1277
apply (auto dest: fold_graph_permute_diff) 
paulson@15506
  1278
done
nipkow@15376
  1279
haftmann@26041
  1280
lemma fold1Set_determ:
haftmann@26041
  1281
  "fold1Set times A x ==> fold1Set times A y ==> y = x"
paulson@15506
  1282
proof (clarify elim!: fold1Set.cases)
paulson@15506
  1283
  fix A x B y a b
nipkow@28853
  1284
  assume Ax: "fold_graph times id a A x"
nipkow@28853
  1285
  assume By: "fold_graph times id b B y"
paulson@15506
  1286
  assume anotA:  "a \<notin> A"
paulson@15506
  1287
  assume bnotB:  "b \<notin> B"
paulson@15506
  1288
  assume eq: "insert a A = insert b B"
paulson@15506
  1289
  show "y=x"
paulson@15506
  1290
  proof cases
paulson@15506
  1291
    assume same: "a=b"
paulson@15506
  1292
    hence "A=B" using anotA bnotB eq by (blast elim!: equalityE)
nipkow@28853
  1293
    thus ?thesis using Ax By same by (blast intro: fold_graph_determ)
nipkow@15392
  1294
  next
paulson@15506
  1295
    assume diff: "a\<noteq>b"
paulson@15506
  1296
    let ?D = "B - {a}"
paulson@15506
  1297
    have B: "B = insert a ?D" and A: "A = insert b ?D"
paulson@15506
  1298
     and aB: "a \<in> B" and bA: "b \<in> A"
paulson@15506
  1299
      using eq anotA bnotB diff by (blast elim!:equalityE)+
paulson@15506
  1300
    with aB bnotB By
nipkow@28853
  1301
    have "fold_graph times id a (insert b ?D) y" 
nipkow@28853
  1302
      by (auto intro: fold_graph_permute simp add: insert_absorb)
paulson@15506
  1303
    moreover
nipkow@28853
  1304
    have "fold_graph times id a (insert b ?D) x"
paulson@15506
  1305
      by (simp add: A [symmetric] Ax) 
nipkow@28853
  1306
    ultimately show ?thesis by (blast intro: fold_graph_determ) 
nipkow@15392
  1307
  qed
wenzelm@12396
  1308
qed
wenzelm@12396
  1309
haftmann@26041
  1310
lemma fold1Set_equality: "fold1Set times A y ==> fold1 times A = y"
paulson@15506
  1311
  by (unfold fold1_def) (blast intro: fold1Set_determ)
paulson@15506
  1312
haftmann@26041
  1313
end
nipkow@28853
  1314
*)
haftmann@26041
  1315
paulson@15506
  1316
declare
nipkow@28853
  1317
  empty_fold_graphE [rule del]  fold_graph.intros [rule del]
paulson@15506
  1318
  empty_fold1SetE [rule del]  insert_fold1SetE [rule del]
ballarin@19931
  1319
  -- {* No more proofs involve these relations. *}
nipkow@15376
  1320
haftmann@26041
  1321
subsubsection {* Lemmas about @{text fold1} *}
haftmann@26041
  1322
haftmann@26041
  1323
context ab_semigroup_mult
haftmann@22917
  1324
begin
haftmann@22917
  1325
haftmann@26041
  1326
lemma fold1_Un:
nipkow@15484
  1327
assumes A: "finite A" "A \<noteq> {}"
nipkow@15484
  1328
shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow> A Int B = {} \<Longrightarrow>
haftmann@26041
  1329
       fold1 times (A Un B) = fold1 times A * fold1 times B"
haftmann@26041
  1330
using A by (induct rule: finite_ne_induct)
haftmann@26041
  1331
  (simp_all add: fold1_insert mult_assoc)
haftmann@26041
  1332
haftmann@26041
  1333
lemma fold1_in:
haftmann@26041
  1334
  assumes A: "finite (A)" "A \<noteq> {}" and elem: "\<And>x y. x * y \<in> {x,y}"
haftmann@26041
  1335
  shows "fold1 times A \<in> A"
nipkow@15484
  1336
using A
nipkow@15484
  1337
proof (induct rule:finite_ne_induct)
paulson@15506
  1338
  case singleton thus ?case by simp
nipkow@15484
  1339
next
nipkow@15484
  1340
  case insert thus ?case using elem by (force simp add:fold1_insert)
nipkow@15484
  1341
qed
nipkow@15484
  1342
haftmann@26041
  1343
end
haftmann@26041
  1344
haftmann@26041
  1345
lemma (in ab_semigroup_idem_mult) fold1_Un2:
nipkow@15497
  1346
assumes A: "finite A" "A \<noteq> {}"
haftmann@26041
  1347
shows "finite B \<Longrightarrow> B \<noteq> {} \<Longrightarrow>
haftmann@26041
  1348
       fold1 times (A Un B) = fold1 times A * fold1 times B"
nipkow@15497
  1349
using A
haftmann@26041
  1350
proof(induct rule:finite_ne_induct)
nipkow@15497
  1351
  case singleton thus ?case by simp
nipkow@15484
  1352
next
haftmann@26041
  1353
  case insert thus ?case by (simp add: mult_assoc)
nipkow@18423
  1354
qed
nipkow@18423
  1355
nipkow@18423
  1356
haftmann@31453
  1357
subsection {* Expressing set operations via @{const fold} *}
haftmann@31453
  1358
haftmann@31453
  1359
lemma (in fun_left_comm) fun_left_comm_apply:
haftmann@31453
  1360
  "fun_left_comm (\<lambda>x. f (g x))"
haftmann@31453
  1361
proof
haftmann@31453
  1362
qed (simp_all add: fun_left_comm)
haftmann@31453
  1363
haftmann@31453
  1364
lemma (in fun_left_comm_idem) fun_left_comm_idem_apply:
haftmann@31453
  1365
  "fun_left_comm_idem (\<lambda>x. f (g x))"
haftmann@31453
  1366
  by (rule fun_left_comm_idem.intro, rule fun_left_comm_apply, unfold_locales)
haftmann@31453
  1367
    (simp_all add: fun_left_idem)
haftmann@31453
  1368
haftmann@31453
  1369
lemma fun_left_comm_idem_insert:
haftmann@31453
  1370
  "fun_left_comm_idem insert"
haftmann@31453
  1371
proof
haftmann@31453
  1372
qed auto
haftmann@31453
  1373
haftmann@31453
  1374
lemma fun_left_comm_idem_remove:
haftmann@31453
  1375
  "fun_left_comm_idem (\<lambda>x A. A - {x})"
haftmann@31453
  1376
proof
haftmann@31453
  1377
qed auto
haftmann@31453
  1378
haftmann@35028
  1379
lemma (in semilattice_inf) fun_left_comm_idem_inf:
haftmann@34007
  1380
  "fun_left_comm_idem inf"
haftmann@31453
  1381
proof
haftmann@34007
  1382
qed (auto simp add: inf_left_commute)
haftmann@34007
  1383
haftmann@35028
  1384
lemma (in semilattice_sup) fun_left_comm_idem_sup:
haftmann@34007
  1385
  "fun_left_comm_idem sup"
haftmann@31453
  1386
proof
haftmann@34007
  1387
qed (auto simp add: sup_left_commute)
haftmann@31453
  1388
haftmann@31453
  1389
lemma union_fold_insert:
haftmann@31453
  1390
  assumes "finite A"
haftmann@31453
  1391
  shows "A \<union> B = fold insert B A"
haftmann@31453
  1392
proof -
haftmann@31453
  1393
  interpret fun_left_comm_idem insert by (fact fun_left_comm_idem_insert)
haftmann@31453
  1394
  from `finite A` show ?thesis by (induct A arbitrary: B) simp_all
haftmann@31453
  1395
qed
haftmann@31453
  1396
haftmann@31453
  1397
lemma minus_fold_remove:
haftmann@31453
  1398
  assumes "finite A"
haftmann@31453
  1399
  shows "B - A = fold (\<lambda>x A. A - {x}) B A"
haftmann@31453
  1400
proof -
haftmann@31453
  1401
  interpret fun_left_comm_idem "\<lambda>x A. A - {x}" by (fact fun_left_comm_idem_remove)
haftmann@31453
  1402
  from `finite A` show ?thesis by (induct A arbitrary: B) auto
haftmann@31453
  1403
qed
haftmann@31453
  1404
haftmann@34007
  1405
context complete_lattice
haftmann@34007
  1406
begin
haftmann@34007
  1407
haftmann@34007
  1408
lemma inf_Inf_fold_inf:
haftmann@31453
  1409
  assumes "finite A"
haftmann@34007
  1410
  shows "inf B (Inf A) = fold inf B A"
haftmann@31453
  1411
proof -
haftmann@34007
  1412
  interpret fun_left_comm_idem inf by (fact fun_left_comm_idem_inf)
haftmann@31453
  1413
  from `finite A` show ?thesis by (induct A arbitrary: B)
haftmann@34007
  1414
    (simp_all add: Inf_empty Inf_insert inf_commute fold_fun_comm)
haftmann@31453
  1415
qed
haftmann@31453
  1416
haftmann@34007
  1417
lemma sup_Sup_fold_sup:
haftmann@31453
  1418
  assumes "finite A"
haftmann@34007
  1419
  shows "sup B (Sup A) = fold sup B A"
haftmann@31453
  1420
proof -
haftmann@34007
  1421
  interpret fun_left_comm_idem sup by (fact fun_left_comm_idem_sup)
haftmann@31453
  1422
  from `finite A` show ?thesis by (induct A arbitrary: B)
haftmann@34007
  1423
    (simp_all add: Sup_empty Sup_insert sup_commute fold_fun_comm)
haftmann@31453
  1424
qed
haftmann@31453
  1425
haftmann@34007
  1426
lemma Inf_fold_inf:
haftmann@31453
  1427
  assumes "finite A"
haftmann@34007
  1428
  shows "Inf A = fold inf top A"
haftmann@34007
  1429
  using assms inf_Inf_fold_inf [of A top] by (simp add: inf_absorb2)
haftmann@34007
  1430
haftmann@34007
  1431
lemma Sup_fold_sup:
haftmann@31453
  1432
  assumes "finite A"
haftmann@34007
  1433
  shows "Sup A = fold sup bot A"
haftmann@34007
  1434
  using assms sup_Sup_fold_sup [of A bot] by (simp add: sup_absorb2)
haftmann@34007
  1435
haftmann@34007
  1436
lemma inf_INFI_fold_inf:
haftmann@31453
  1437
  assumes "finite A"
haftmann@34007
  1438
  shows "inf B (INFI A f) = fold (\<lambda>A. inf (f A)) B A" (is "?inf = ?fold") 
haftmann@31453
  1439
proof (rule sym)
haftmann@34007
  1440
  interpret fun_left_comm_idem inf by (fact fun_left_comm_idem_inf)
haftmann@34007
  1441
  interpret fun_left_comm_idem "\<lambda>A. inf (f A)" by (fact fun_left_comm_idem_apply)
haftmann@34007
  1442
  from `finite A` show "?fold = ?inf"
haftmann@34007
  1443
  by (induct A arbitrary: B)
haftmann@34007
  1444
    (simp_all add: INFI_def Inf_empty Inf_insert inf_left_commute)
haftmann@31453
  1445
qed
haftmann@31453
  1446
haftmann@34007
  1447
lemma sup_SUPR_fold_sup:
haftmann@31453
  1448
  assumes "finite A"
haftmann@34007
  1449
  shows "sup B (SUPR A f) = fold (\<lambda>A. sup (f A)) B A" (is "?sup = ?fold") 
haftmann@34007
  1450
proof (rule sym)
haftmann@34007
  1451
  interpret fun_left_comm_idem sup by (fact fun_left_comm_idem_sup)
haftmann@34007
  1452
  interpret fun_left_comm_idem "\<lambda>A. sup (f A)" by (fact fun_left_comm_idem_apply)
haftmann@34007
  1453
  from `finite A` show "?fold = ?sup"
haftmann@34007
  1454
  by (induct A arbitrary: B)
haftmann@34007
  1455
    (simp_all add: SUPR_def Sup_empty Sup_insert sup_left_commute)
haftmann@34007
  1456
qed
haftmann@34007
  1457
haftmann@34007
  1458
lemma INFI_fold_inf:
haftmann@31453
  1459
  assumes "finite A"
haftmann@34007
  1460
  shows "INFI A f = fold (\<lambda>A. inf (f A)) top A"
haftmann@34007
  1461
  using assms inf_INFI_fold_inf [of A top] by simp
haftmann@34007
  1462
haftmann@34007
  1463
lemma SUPR_fold_sup:
haftmann@34007
  1464
  assumes "finite A"
haftmann@34007
  1465
  shows "SUPR A f = fold (\<lambda>A. sup (f A)) bot A"
haftmann@34007
  1466
  using assms sup_SUPR_fold_sup [of A bot] by simp
haftmann@31453
  1467
haftmann@25571
  1468
end
haftmann@34007
  1469
haftmann@35719
  1470
haftmann@35719
  1471
subsection {* Locales as mini-packages *}
haftmann@35719
  1472
haftmann@35719
  1473
locale folding =
haftmann@35719
  1474
  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'b"
haftmann@35719
  1475
  fixes F :: "'a set \<Rightarrow> 'b \<Rightarrow> 'b"
haftmann@35719
  1476
  assumes commute_comp: "f x \<circ> f y = f y \<circ> f x"
haftmann@35722
  1477
  assumes eq_fold: "finite A \<Longrightarrow> F A s = fold f s A"
haftmann@35719
  1478
begin
haftmann@35719
  1479
haftmann@35719
  1480
lemma fun_left_commute:
haftmann@35719
  1481
  "f x (f y s) = f y (f x s)"
haftmann@35719
  1482
  using commute_comp [of x y] by (simp add: expand_fun_eq)
haftmann@35719
  1483
haftmann@35719
  1484
lemma fun_left_comm:
haftmann@35719
  1485
  "fun_left_comm f"
haftmann@35719
  1486
proof
haftmann@35719
  1487
qed (fact fun_left_commute)
haftmann@35719
  1488
haftmann@35719
  1489
lemma empty [simp]:
haftmann@35719
  1490
  "F {} = id"
haftmann@35719
  1491
  by (simp add: eq_fold expand_fun_eq)
haftmann@35719
  1492
haftmann@35719
  1493
lemma insert [simp]:
haftmann@35719
  1494
  assumes "finite A" and "x \<notin> A"
haftmann@35719
  1495
  shows "F (insert x A) = F A \<circ> f x"
haftmann@35719
  1496
proof -
haftmann@35719
  1497
  interpret fun_left_comm f by (fact fun_left_comm)
haftmann@35719
  1498
  from fold_insert2 assms
haftmann@35722
  1499
  have "\<And>s. fold f s (insert x A) = fold f (f x s) A" .
haftmann@35722
  1500
  with `finite A` show ?thesis by (simp add: eq_fold expand_fun_eq)
haftmann@35719
  1501
qed
haftmann@35719
  1502
haftmann@35719
  1503
lemma remove:
haftmann@35719
  1504
  assumes "finite A" and "x \<in> A"
haftmann@35719
  1505
  shows "F A = F (A - {x}) \<circ> f x"
haftmann@35719
  1506
proof -
haftmann@35719
  1507
  from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
haftmann@35719
  1508
    by (auto dest: mk_disjoint_insert)
haftmann@35719
  1509
  moreover from `finite A` this have "finite B" by simp
haftmann@35719
  1510
  ultimately show ?thesis by simp
haftmann@35719
  1511
qed
haftmann@35719
  1512
haftmann@35719
  1513
lemma insert_remove:
haftmann@35719
  1514
  assumes "finite A"
haftmann@35719
  1515
  shows "F (insert x A) = F (A - {x}) \<circ> f x"
haftmann@35722
  1516
  using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
haftmann@35719
  1517
haftmann@35719
  1518
lemma commute_comp':
haftmann@35719
  1519
  assumes "finite A"
haftmann@35719
  1520
  shows "f x \<circ> F A = F A \<circ> f x"
haftmann@35719
  1521
proof (rule ext)
haftmann@35719
  1522
  fix s
haftmann@35719
  1523
  from assms show "(f x \<circ> F A) s = (F A \<circ> f x) s"
haftmann@35719
  1524
    by (induct A arbitrary: s) (simp_all add: fun_left_commute)
haftmann@35719
  1525
qed
haftmann@35719
  1526
haftmann@35719
  1527
lemma fun_left_commute':
haftmann@35719
  1528
  assumes "finite A"
haftmann@35719
  1529
  shows "f x (F A s) = F A (f x s)"
haftmann@35719
  1530
  using commute_comp' assms by (simp add: expand_fun_eq)
haftmann@35719
  1531
haftmann@35719
  1532
lemma union:
haftmann@35719
  1533
  assumes "finite A" and "finite B"
haftmann@35719
  1534
  and "A \<inter> B = {}"
haftmann@35719
  1535
  shows "F (A \<union> B) = F A \<circ> F B"
haftmann@35719
  1536
using `finite A` `A \<inter> B = {}` proof (induct A)
haftmann@35719
  1537
  case empty show ?case by simp
haftmann@35719
  1538
next
haftmann@35719
  1539
  case (insert x A)
haftmann@35719
  1540
  then have "A \<inter> B = {}" by auto
haftmann@35719
  1541
  with insert(3) have "F (A \<union> B) = F A \<circ> F B" .
haftmann@35719
  1542
  moreover from insert have "x \<notin> B" by simp
haftmann@35719
  1543
  moreover from `finite A` `finite B` have fin: "finite (A \<union> B)" by simp
haftmann@35719
  1544
  moreover from `x \<notin> A` `x \<notin> B` have "x \<notin> A \<union> B" by simp
haftmann@35719
  1545
  ultimately show ?case by (simp add: fun_left_commute')
haftmann@35719
  1546
qed
haftmann@35719
  1547
haftmann@34007
  1548
end
haftmann@35719
  1549
haftmann@35719
  1550
locale folding_idem = folding +
haftmann@35719
  1551
  assumes idem_comp: "f x \<circ> f x = f x"
haftmann@35719
  1552
begin
haftmann@35719
  1553
haftmann@35719
  1554
declare insert [simp del]
haftmann@35719
  1555
haftmann@35719
  1556
lemma fun_idem:
haftmann@35719
  1557
  "f x (f x s) = f x s"
haftmann@35719
  1558
  using idem_comp [of x] by (simp add: expand_fun_eq)
haftmann@35719
  1559
haftmann@35719
  1560
lemma fun_left_comm_idem:
haftmann@35719
  1561
  "fun_left_comm_idem f"
haftmann@35719
  1562
proof
haftmann@35719
  1563
qed (fact fun_left_commute fun_idem)+
haftmann@35719
  1564
haftmann@35719
  1565
lemma insert_idem [simp]:
haftmann@35719
  1566
  assumes "finite A"
haftmann@35719
  1567
  shows "F (insert x A) = F A \<circ> f x"
haftmann@35719
  1568
proof -
haftmann@35719
  1569
  interpret fun_left_comm_idem f by (fact fun_left_comm_idem)
haftmann@35719
  1570
  from fold_insert_idem2 assms
haftmann@35722
  1571
  have "\<And>s. fold f s (insert x A) = fold f (f x s) A" .
haftmann@35722
  1572
  with assms show ?thesis by (simp add: eq_fold expand_fun_eq)
haftmann@35719
  1573
qed
haftmann@35719
  1574
haftmann@35719
  1575
lemma union_idem:
haftmann@35719
  1576
  assumes "finite A" and "finite B"
haftmann@35719
  1577
  shows "F (A \<union> B) = F A \<circ> F B"
haftmann@35719
  1578
using `finite A` proof (induct A)
haftmann@35719
  1579
  case empty show ?case by simp
haftmann@35719
  1580
next
haftmann@35719
  1581
  case (insert x A)
haftmann@35719
  1582
  from insert(3) have "F (A \<union> B) = F A \<circ> F B" .
haftmann@35719
  1583
  moreover from `finite A` `finite B` have fin: "finite (A \<union> B)" by simp
haftmann@35719
  1584
  ultimately show ?case by (simp add: fun_left_commute')
haftmann@35719
  1585
qed
haftmann@35719
  1586
haftmann@35719
  1587
end
haftmann@35719
  1588
haftmann@35722
  1589
no_notation (in times) times (infixl "*" 70)
haftmann@35722
  1590
no_notation (in one) Groups.one ("1")
haftmann@35722
  1591
haftmann@35722
  1592
locale folding_image_simple = comm_monoid +
haftmann@35722
  1593
  fixes g :: "('b \<Rightarrow> 'a)"
haftmann@35722
  1594
  fixes F :: "'b set \<Rightarrow> 'a"
haftmann@35722
  1595
  assumes eq_fold: "finite A \<Longrightarrow> F A = fold_image f g 1 A"
haftmann@35722
  1596
begin
haftmann@35722
  1597
haftmann@35722
  1598
lemma empty [simp]:
haftmann@35722
  1599
  "F {} = 1"
haftmann@35722
  1600
  by (simp add: eq_fold)
haftmann@35722
  1601
haftmann@35722
  1602
lemma insert [simp]:
haftmann@35722
  1603
  assumes "finite A" and "x \<notin> A"
haftmann@35722
  1604
  shows "F (insert x A) = g x * F A"
haftmann@35722
  1605
proof -
haftmann@35722
  1606
  interpret fun_left_comm "%x y. (g x) * y" proof
haftmann@35722
  1607
  qed (simp add: ac_simps)
haftmann@35722
  1608
  with assms have "fold_image (op *) g 1 (insert x A) = g x * fold_image (op *) g 1 A"
haftmann@35722
  1609
    by (simp add: fold_image_def)
haftmann@35722
  1610
  with `finite A` show ?thesis by (simp add: eq_fold)
haftmann@35722
  1611
qed
haftmann@35722
  1612
haftmann@35722
  1613
lemma remove:
haftmann@35722
  1614
  assumes "finite A" and "x \<in> A"
haftmann@35722
  1615
  shows "F A = g x * F (A - {x})"
haftmann@35722
  1616
proof -
haftmann@35722
  1617
  from `x \<in> A` obtain B where A: "A = insert x B" and "x \<notin> B"
haftmann@35722
  1618
    by (auto dest: mk_disjoint_insert)
haftmann@35722
  1619
  moreover from `finite A` this have "finite B" by simp
haftmann@35722
  1620
  ultimately show ?thesis by simp
haftmann@35722
  1621
qed
haftmann@35722
  1622
haftmann@35722
  1623
lemma insert_remove:
haftmann@35722
  1624
  assumes "finite A"
haftmann@35722
  1625
  shows "F (insert x A) = g x * F (A - {x})"
haftmann@35722
  1626
  using assms by (cases "x \<in> A") (simp_all add: remove insert_absorb)
haftmann@35722
  1627
haftmann@35722
  1628
lemma union_inter:
haftmann@35722
  1629
  assumes "finite A" and "finite B"
haftmann@35722
  1630
  shows "F A * F B = F (A \<union> B) * F (A \<inter> B)"
haftmann@35722
  1631
using assms proof (induct A)
haftmann@35722
  1632
  case empty then show ?case by simp
haftmann@35722
  1633
next
haftmann@35722
  1634
  case (insert x A) then show ?case
haftmann@35722
  1635
    by (auto simp add: insert_absorb Int_insert_left commute [of _ "g x"] assoc left_commute)
haftmann@35722
  1636
qed
haftmann@35722
  1637
haftmann@35722
  1638
corollary union_disjoint:
haftmann@35722
  1639
  assumes "finite A" and "finite B"
haftmann@35722
  1640
  assumes "A \<inter> B = {}"
haftmann@35722
  1641
  shows "F (A \<union> B) = F A * F B"
haftmann@35722
  1642
  using assms by (simp add: union_inter)
haftmann@35722
  1643
haftmann@35719
  1644
end
haftmann@35722
  1645
haftmann@35722
  1646
locale folding_image = comm_monoid +
haftmann@35722
  1647
  fixes F :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
haftmann@35722
  1648
  assumes eq_fold: "\<And>g. finite A \<Longrightarrow> F g A = fold_image f g 1 A"
haftmann@35722
  1649
haftmann@35722
  1650
sublocale folding_image < folding_image_simple "op *" 1 g "F g" proof
haftmann@35722
  1651
qed (fact eq_fold)
haftmann@35722
  1652
haftmann@35722
  1653
context folding_image
haftmann@35722
  1654
begin
haftmann@35722
  1655
haftmann@35722
  1656
lemma reindex:
haftmann@35722
  1657
  assumes "finite A" and "inj_on h A"
haftmann@35722
  1658
  shows "F g (h ` A) = F (g \<circ> h) A"
haftmann@35722
  1659
  using assms by (induct A) auto
haftmann@35722
  1660
haftmann@35722
  1661
lemma cong:
haftmann@35722
  1662
  assumes "finite A" and "\<And>x. x \<in> A \<Longrightarrow> g x = h x"
haftmann@35722
  1663
  shows "F g A = F h A"
haftmann@35722
  1664
proof -
haftmann@35722
  1665
  from assms have "ALL C. C <= A --> (ALL x:C. g x = h x) --> F g C = F h C"
haftmann@35722
  1666
  apply - apply (erule finite_induct) apply simp
haftmann@35722
  1667
  apply (simp add: subset_insert_iff, clarify)
haftmann@35722
  1668
  apply (subgoal_tac "finite C")
haftmann@35722
  1669
  prefer 2 apply (blast dest: finite_subset [COMP swap_prems_rl])
haftmann@35722
  1670
  apply (subgoal_tac "C = insert x (C - {x})")
haftmann@35722
  1671
  prefer 2 apply blast
haftmann@35722
  1672
  apply (erule ssubst)
haftmann@35722
  1673
  apply (drule spec)
haftmann@35722
  1674
  apply (erule (1) notE impE)
haftmann@35722
  1675
  apply (simp add: Ball_def del: insert_Diff_single)
haftmann@35722
  1676
  done
haftmann@35722
  1677
  with assms show ?thesis by simp
haftmann@35722
  1678
qed
haftmann@35722
  1679
haftmann@35722
  1680
lemma UNION_disjoint:
haftmann@35722
  1681
  assumes "finite I" and "\<forall>i\<in>I. finite (A i)"
haftmann@35722
  1682
  and "\<forall>i\<in>I. \<forall>j\<in>I. i \<noteq> j \<longrightarrow> A i \<inter> A j = {}"
haftmann@35722
  1683
  shows "F g (UNION I A) = F (F g \<circ> A) I"
haftmann@35722
  1684
apply (insert assms)
haftmann@35722
  1685
apply (induct set: finite, simp, atomize)
haftmann@35722
  1686
apply (subgoal_tac "\<forall>i\<in>Fa. x \<noteq> i")
haftmann@35722
  1687
 prefer 2 apply blast
haftmann@35722
  1688
apply (subgoal_tac "A x Int UNION Fa A = {}")
haftmann@35722
  1689
 prefer 2 apply blast
haftmann@35722
  1690
apply (simp add: union_disjoint)
haftmann@35722
  1691
done
haftmann@35722
  1692
haftmann@35722
  1693
lemma distrib:
haftmann@35722
  1694
  assumes "finite A"
haftmann@35722
  1695
  shows "F (\<lambda>x. g x * h x) A = F g A * F h A"
haftmann@35722
  1696
  using assms by (rule finite_induct) (simp_all add: assoc commute left_commute)
haftmann@35722
  1697
haftmann@35722
  1698
lemma related: 
haftmann@35722
  1699
  assumes Re: "R 1 1" 
haftmann@35722
  1700
  and Rop: "\<forall>x1 y1 x2 y2. R x1 x2 \<and> R y1 y2 \<longrightarrow> R (x1 * y1) (x2 * y2)" 
haftmann@35722
  1701
  and fS: "finite S" and Rfg: "\<forall>x\<in>S. R (h x) (g x)"
haftmann@35722
  1702
  shows "R (F h S) (F g S)"
haftmann@35722
  1703
  using fS by (rule finite_subset_induct) (insert assms, auto)
haftmann@35722
  1704
haftmann@35722
  1705
lemma eq_general:
haftmann@35722
  1706
  assumes fS: "finite S"
haftmann@35722
  1707
  and h: "\<forall>y\<in>S'. \<exists>!x. x \<in> S \<and> h x = y" 
haftmann@35722
  1708
  and f12:  "\<forall>x\<in>S. h x \<in> S' \<and> f2 (h x) = f1 x"
haftmann@35722
  1709
  shows "F f1 S = F f2 S'"
haftmann@35722
  1710
proof-
haftmann@35722
  1711
  from h f12 have hS: "h ` S = S'" by blast
haftmann@35722
  1712
  {fix x y assume H: "x \<in> S" "y \<in> S" "h x = h y"
haftmann@35722
  1713
    from f12 h H  have "x = y" by auto }
haftmann@35722
  1714
  hence hinj: "inj_on h S" unfolding inj_on_def Ex1_def by blast
haftmann@35722
  1715
  from f12 have th: "\<And>x. x \<in> S \<Longrightarrow> (f2 \<circ> h) x = f1 x" by auto 
haftmann@35722
  1716
  from hS have "F f2 S' = F f2 (h ` S)" by simp
haftmann@35722
  1717
  also have "\<dots> = F (f2 o h) S" using reindex [OF fS hinj, of f2] .
haftmann@35722
  1718
  also have "\<dots> = F f1 S " using th cong [OF fS, of "f2 o h" f1]
haftmann@35722
  1719
    by blast
haftmann@35722
  1720
  finally show ?thesis ..
haftmann@35722
  1721
qed
haftmann@35722
  1722
haftmann@35722
  1723
lemma eq_general_inverses:
haftmann@35722
  1724
  assumes fS: "finite S" 
haftmann@35722
  1725
  and kh: "\<And>y. y \<in> T \<Longrightarrow> k y \<in> S \<and> h (k y) = y"
haftmann@35722
  1726
  and hk: "\<And>x. x \<in> S \<Longrightarrow> h x \<in> T \<and> k (h x) = x \<and> g (h x) = j x"
haftmann@35722
  1727
  shows "F j S = F g T"
haftmann@35722
  1728
  (* metis solves it, but not yet available here *)
haftmann@35722
  1729
  apply (rule eq_general [OF fS, of T h g j])
haftmann@35722
  1730
  apply (rule ballI)
haftmann@35722
  1731
  apply (frule kh)
haftmann@35722
  1732
  apply (rule ex1I[])
haftmann@35722
  1733
  apply blast
haftmann@35722
  1734
  apply clarsimp
haftmann@35722
  1735
  apply (drule hk) apply simp
haftmann@35722
  1736
  apply (rule sym)
haftmann@35722
  1737
  apply (erule conjunct1[OF conjunct2[OF hk]])
haftmann@35722
  1738
  apply (rule ballI)
haftmann@35722
  1739
  apply (drule hk)
haftmann@35722
  1740
  apply blast
haftmann@35722
  1741
  done
haftmann@35722
  1742
haftmann@35722
  1743
end
haftmann@35722
  1744
haftmann@35722
  1745
notation (in times) times (infixl "*" 70)
haftmann@35722
  1746
notation (in one) Groups.one ("1")
haftmann@35722
  1747
haftmann@35722
  1748
haftmann@35722
  1749
subsection {* Finite cardinality *}
haftmann@35722
  1750
haftmann@35722
  1751
text {* This definition, although traditional, is ugly to work with:
haftmann@35722
  1752
@{text "card A == LEAST n. EX f. A = {f i | i. i < n}"}.
haftmann@35722
  1753
But now that we have @{text fold_image} things are easy:
haftmann@35722
  1754
*}
haftmann@35722
  1755
haftmann@35722
  1756
definition card :: "'a set \<Rightarrow> nat" where
haftmann@35722
  1757
  "card A = (if finite A then fold_image (op +) (\<lambda>x. 1) 0 A else 0)"
haftmann@35722
  1758
haftmann@35722
  1759
interpretation card!: folding_image_simple "op +" 0 "\<lambda>x. 1" card proof
haftmann@35722
  1760
qed (simp add: card_def)
haftmann@35722
  1761
haftmann@35722
  1762
lemma card_infinite [simp]:
haftmann@35722
  1763
  "\<not> finite A \<Longrightarrow> card A = 0"
haftmann@35722
  1764
  by (simp add: card_def)
haftmann@35722
  1765
haftmann@35722
  1766
lemma card_empty:
haftmann@35722
  1767
  "card {} = 0"
haftmann@35722
  1768
  by (fact card.empty)
haftmann@35722
  1769
haftmann@35722
  1770
lemma card_insert_disjoint:
haftmann@35722
  1771
  "finite A ==> x \<notin> A ==> card (insert x A) = Suc (card A)"
haftmann@35722
  1772
  by simp
haftmann@35722
  1773
haftmann@35722
  1774
lemma card_insert_if:
haftmann@35722
  1775
  "finite A ==> card (insert x A) = (if x \<in> A then card A else Suc (card A))"
haftmann@35722
  1776
  by auto (simp add: card.insert_remove card.remove)
haftmann@35722
  1777
haftmann@35722
  1778
lemma card_ge_0_finite:
haftmann@35722
  1779
  "card A > 0 \<Longrightarrow> finite A"
haftmann@35722
  1780
  by (rule ccontr) simp
haftmann@35722
  1781
haftmann@35722
  1782
lemma card_0_eq [simp, noatp]:
haftmann@35722
  1783
  "finite A \<Longrightarrow> card A = 0 \<longleftrightarrow> A = {}"
haftmann@35722
  1784
  by (auto dest: mk_disjoint_insert)
haftmann@35722
  1785
haftmann@35722
  1786
lemma finite_UNIV_card_ge_0:
haftmann@35722
  1787
  "finite (UNIV :: 'a set) \<Longrightarrow> card (UNIV :: 'a set) > 0"
haftmann@35722
  1788
  by (rule ccontr) simp
haftmann@35722
  1789
haftmann@35722
  1790
lemma card_eq_0_iff:
haftmann@35722
  1791
  "card A = 0 \<longleftrightarrow> A = {} \<or> \<not> finite A"
haftmann@35722
  1792
  by auto
haftmann@35722
  1793
haftmann@35722
  1794
lemma card_gt_0_iff:
haftmann@35722
  1795
  "0 < card A \<longleftrightarrow> A \<noteq> {} \<and> finite A"
haftmann@35722
  1796
  by (simp add: neq0_conv [symmetric] card_eq_0_iff) 
haftmann@35722
  1797
haftmann@35722
  1798
lemma card_Suc_Diff1: "finite A ==> x: A ==> Suc (card (A - {x})) = card A"
haftmann@35722
  1799
apply(rule_tac t = A in insert_Diff [THEN subst], assumption)
haftmann@35722
  1800
apply(simp del:insert_Diff_single)
haftmann@35722
  1801
done
haftmann@35722
  1802
haftmann@35722
  1803
lemma card_Diff_singleton:
haftmann@35722
  1804
  "finite A ==> x: A ==> card (A - {x}) = card A - 1"
haftmann@35722
  1805
by (simp add: card_Suc_Diff1 [symmetric])
haftmann@35722
  1806
haftmann@35722
  1807
lemma card_Diff_singleton_if:
haftmann@35722
  1808
  "finite A ==> card (A-{x}) = (if x : A then card A - 1 else card A)"
haftmann@35722
  1809
by (simp add: card_Diff_singleton)
haftmann@35722
  1810
haftmann@35722
  1811
lemma card_Diff_insert[simp]:
haftmann@35722
  1812
assumes "finite A" and "a:A" and "a ~: B"
haftmann@35722
  1813
shows "card(A - insert a B) = card(A - B) - 1"
haftmann@35722
  1814
proof -
haftmann@35722
  1815
  have "A - insert a B = (A - B) - {a}" using assms by blast
haftmann@35722
  1816
  then show ?thesis using assms by(simp add:card_Diff_singleton)
haftmann@35722
  1817
qed
haftmann@35722
  1818
haftmann@35722
  1819
lemma card_insert: "finite A ==> card (insert x A) = Suc (card (A - {x}))"
haftmann@35722
  1820
by (simp add: card_insert_if card_Suc_Diff1 del:card_Diff_insert)
haftmann@35722
  1821
haftmann@35722
  1822
lemma card_insert_le: "finite A ==> card A <= card (insert x A)"
haftmann@35722
  1823
by (simp add: card_insert_if)
haftmann@35722
  1824
haftmann@35722
  1825
lemma card_mono:
haftmann@35722
  1826
  assumes "finite B" and "A \<subseteq> B"
haftmann@35722
  1827
  shows "card A \<le> card B"
haftmann@35722
  1828
proof -
haftmann@35722
  1829
  from assms have "finite A" by (auto intro: finite_subset)
haftmann@35722
  1830
  then show ?thesis using assms proof (induct A arbitrary: B)
haftmann@35722
  1831
    case empty then show ?case by simp
haftmann@35722
  1832
  next
haftmann@35722
  1833
    case (insert x A)
haftmann@35722
  1834
    then have "x \<in> B" by simp
haftmann@35722
  1835
    from insert have "A \<subseteq> B - {x}" and "finite (B - {x})" by auto
haftmann@35722
  1836
    with insert.hyps have "card A \<le> card (B - {x})" by auto
haftmann@35722
  1837
    with `finite A` `x \<notin> A` `finite B` `x \<in> B` show ?case by simp (simp only: card.remove)
haftmann@35722
  1838
  qed
haftmann@35722
  1839
qed
haftmann@35722
  1840
haftmann@35722
  1841
lemma card_seteq: "finite B ==> (!!A. A <= B ==> card B <= card A ==> A = B)"
haftmann@35722
  1842
apply (induct set: finite, simp, clarify)
haftmann@35722
  1843
apply (subgoal_tac "finite A & A - {x} <= F")
haftmann@35722
  1844
 prefer 2 apply (blast intro: finite_subset, atomize)
haftmann@35722
  1845
apply (drule_tac x = "A - {x}" in spec)
haftmann@35722
  1846
apply (simp add: card_Diff_singleton_if split add: split_if_asm)
haftmann@35722
  1847
apply (case_tac "card A", auto)
haftmann@35722
  1848
done
haftmann@35722
  1849
haftmann@35722
  1850
lemma psubset_card_mono: "finite B ==> A < B ==> card A < card B"
haftmann@35722
  1851
apply (simp add: psubset_eq linorder_not_le [symmetric])
haftmann@35722
  1852
apply (blast dest: card_seteq)
haftmann@35722
  1853
done
haftmann@35722
  1854
haftmann@35722
  1855
lemma card_Un_Int: "finite A ==> finite B
haftmann@35722
  1856
    ==> card A + card B = card (A Un B) + card (A Int B)"
haftmann@35722
  1857
  by (fact card.union_inter)
haftmann@35722
  1858
haftmann@35722
  1859
lemma card_Un_disjoint: "finite A ==> finite B
haftmann@35722
  1860
    ==> A Int B = {} ==> card (A Un B) = card A + card B"
haftmann@35722
  1861
  by (fact card.union_disjoint)
haftmann@35722
  1862
haftmann@35722
  1863
lemma card_Diff_subset:
haftmann@35722
  1864
  assumes "finite B" and "B \<subseteq> A"
haftmann@35722
  1865
  shows "card (A - B) = card A - card B"
haftmann@35722
  1866
proof (cases "finite A")
haftmann@35722
  1867
  case False with assms show ?thesis by simp
haftmann@35722
  1868
next
haftmann@35722
  1869
  case True with assms show ?thesis by (induct B arbitrary: A) simp_all
haftmann@35722
  1870
qed
haftmann@35722
  1871
haftmann@35722
  1872
lemma card_Diff_subset_Int:
haftmann@35722
  1873
  assumes AB: "finite (A \<inter> B)" shows "card (A - B) = card A - card (A \<inter> B)"
haftmann@35722
  1874
proof -
haftmann@35722
  1875
  have "A - B = A - A \<inter> B" by auto
haftmann@35722
  1876
  thus ?thesis
haftmann@35722
  1877
    by (simp add: card_Diff_subset AB) 
haftmann@35722
  1878
qed
haftmann@35722
  1879
haftmann@35722
  1880
lemma card_Diff1_less: "finite A ==> x: A ==> card (A - {x}) < card A"
haftmann@35722
  1881
apply (rule Suc_less_SucD)
haftmann@35722
  1882
apply (simp add: card_Suc_Diff1 del:card_Diff_insert)
haftmann@35722
  1883
done
haftmann@35722
  1884
haftmann@35722
  1885
lemma card_Diff2_less:
haftmann@35722
  1886
  "finite A ==> x: A ==> y: A ==> card (A - {x} - {y}) < card A"
haftmann@35722
  1887
apply (case_tac "x = y")
haftmann@35722
  1888
 apply (simp add: card_Diff1_less del:card_Diff_insert)
haftmann@35722
  1889
apply (rule less_trans)
haftmann@35722
  1890
 prefer 2 apply (auto intro!: card_Diff1_less simp del:card_Diff_insert)
haftmann@35722
  1891
done
haftmann@35722
  1892
haftmann@35722
  1893
lemma card_Diff1_le: "finite A ==> card (A - {x}) <= card A"
haftmann@35722
  1894
apply (case_tac "x : A")
haftmann@35722
  1895
 apply (simp_all add: card_Diff1_less less_imp_le)
haftmann@35722
  1896
done
haftmann@35722
  1897
haftmann@35722
  1898
lemma card_psubset: "finite B ==> A \<subseteq> B ==> card A < card B ==> A < B"
haftmann@35722
  1899
by (erule psubsetI, blast)
haftmann@35722
  1900
haftmann@35722
  1901
lemma insert_partition:
haftmann@35722
  1902
  "\<lbrakk> x \<notin> F; \<forall>c1 \<in> insert x F. \<forall>c2 \<in> insert x F. c1 \<noteq> c2 \<longrightarrow> c1 \<inter> c2 = {} \<rbrakk>
haftmann@35722
  1903
  \<Longrightarrow> x \<inter> \<Union> F = {}"
haftmann@35722
  1904
by auto
haftmann@35722
  1905
haftmann@35722
  1906
lemma finite_psubset_induct[consumes 1, case_names psubset]:
haftmann@35722
  1907
  assumes "finite A" and "!!A. finite A \<Longrightarrow> (!!B. finite B \<Longrightarrow> B \<subset> A \<Longrightarrow> P(B)) \<Longrightarrow> P(A)" shows "P A"
haftmann@35722
  1908
using assms(1)
haftmann@35722
  1909
proof (induct A rule: measure_induct_rule[where f=card])
haftmann@35722
  1910
  case (less A)
haftmann@35722
  1911
  show ?case
haftmann@35722
  1912
  proof(rule assms(2)[OF less(2)])
haftmann@35722
  1913
    fix B assume "finite B" "B \<subset> A"
haftmann@35722
  1914
    show "P B" by(rule less(1)[OF psubset_card_mono[OF less(2) `B \<subset> A`] `finite B`])
haftmann@35722
  1915
  qed
haftmann@35722
  1916
qed
haftmann@35722
  1917
haftmann@35722
  1918
text{* main cardinality theorem *}
haftmann@35722
  1919
lemma card_partition [rule_format]:
haftmann@35722
  1920
  "finite C ==>
haftmann@35722
  1921
     finite (\<Union> C) -->
haftmann@35722
  1922
     (\<forall>c\<in>C. card c = k) -->
haftmann@35722
  1923
     (\<forall>c1 \<in> C. \<forall>c2 \<in> C. c1 \<noteq> c2 --> c1 \<inter> c2 = {}) -->
haftmann@35722
  1924
     k * card(C) = card (\<Union> C)"
haftmann@35722
  1925
apply (erule finite_induct, simp)
haftmann@35722
  1926
apply (simp add: card_Un_disjoint insert_partition 
haftmann@35722
  1927
       finite_subset [of _ "\<Union> (insert x F)"])
haftmann@35722
  1928
done
haftmann@35722
  1929
haftmann@35722
  1930
lemma card_eq_UNIV_imp_eq_UNIV:
haftmann@35722
  1931
  assumes fin: "finite (UNIV :: 'a set)"
haftmann@35722
  1932
  and card: "card A = card (UNIV :: 'a set)"
haftmann@35722
  1933
  shows "A = (UNIV :: 'a set)"
haftmann@35722
  1934
proof
haftmann@35722
  1935
  show "A \<subseteq> UNIV" by simp
haftmann@35722
  1936
  show "UNIV \<subseteq> A"
haftmann@35722
  1937
  proof
haftmann@35722
  1938
    fix x
haftmann@35722
  1939
    show "x \<in> A"
haftmann@35722
  1940
    proof (rule ccontr)
haftmann@35722
  1941
      assume "x \<notin> A"
haftmann@35722
  1942
      then have "A \<subset> UNIV" by auto
haftmann@35722
  1943
      with fin have "card A < card (UNIV :: 'a set)" by (fact psubset_card_mono)
haftmann@35722
  1944
      with card show False by simp
haftmann@35722
  1945
    qed
haftmann@35722
  1946
  qed
haftmann@35722
  1947
qed
haftmann@35722
  1948
haftmann@35722
  1949
text{*The form of a finite set of given cardinality*}
haftmann@35722
  1950
haftmann@35722
  1951
lemma card_eq_SucD:
haftmann@35722
  1952
assumes "card A = Suc k"
haftmann@35722
  1953
shows "\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={})"
haftmann@35722
  1954
proof -
haftmann@35722
  1955
  have fin: "finite A" using assms by (auto intro: ccontr)
haftmann@35722
  1956
  moreover have "card A \<noteq> 0" using assms by auto
haftmann@35722
  1957
  ultimately obtain b where b: "b \<in> A" by auto
haftmann@35722
  1958
  show ?thesis
haftmann@35722
  1959
  proof (intro exI conjI)
haftmann@35722
  1960
    show "A = insert b (A-{b})" using b by blast
haftmann@35722
  1961
    show "b \<notin> A - {b}" by blast
haftmann@35722
  1962
    show "card (A - {b}) = k" and "k = 0 \<longrightarrow> A - {b} = {}"
haftmann@35722
  1963
      using assms b fin by(fastsimp dest:mk_disjoint_insert)+
haftmann@35722
  1964
  qed
haftmann@35722
  1965
qed
haftmann@35722
  1966
haftmann@35722
  1967
lemma card_Suc_eq:
haftmann@35722
  1968
  "(card A = Suc k) =
haftmann@35722
  1969
   (\<exists>b B. A = insert b B & b \<notin> B & card B = k & (k=0 \<longrightarrow> B={}))"
haftmann@35722
  1970
apply(rule iffI)
haftmann@35722
  1971
 apply(erule card_eq_SucD)
haftmann@35722
  1972
apply(auto)
haftmann@35722
  1973
apply(subst card_insert)
haftmann@35722
  1974
 apply(auto intro:ccontr)
haftmann@35722
  1975
done
haftmann@35722
  1976
haftmann@35722
  1977
lemma finite_fun_UNIVD2:
haftmann@35722
  1978
  assumes fin: "finite (UNIV :: ('a \<Rightarrow> 'b) set)"
haftmann@35722
  1979
  shows "finite (UNIV :: 'b set)"
haftmann@35722
  1980
proof -
haftmann@35722
  1981
  from fin have "finite (range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary))"
haftmann@35722
  1982
    by(rule finite_imageI)
haftmann@35722
  1983
  moreover have "UNIV = range (\<lambda>f :: 'a \<Rightarrow> 'b. f arbitrary)"
haftmann@35722
  1984
    by(rule UNIV_eq_I) auto
haftmann@35722
  1985
  ultimately show "finite (UNIV :: 'b set)" by simp
haftmann@35722
  1986
qed
haftmann@35722
  1987
haftmann@35722
  1988
lemma card_UNIV_unit: "card (UNIV :: unit set) = 1"
haftmann@35722
  1989
  unfolding UNIV_unit by simp
haftmann@35722
  1990
haftmann@35722
  1991
haftmann@35722
  1992
subsubsection {* Cardinality of image *}
haftmann@35722
  1993
haftmann@35722
  1994
lemma card_image_le: "finite A ==> card (f ` A) <= card A"
haftmann@35722
  1995
apply (induct set: finite)
haftmann@35722
  1996
 apply simp
haftmann@35722
  1997
apply (simp add: le_SucI card_insert_if)
haftmann@35722
  1998
done
haftmann@35722
  1999
haftmann@35722
  2000
lemma card_image:
haftmann@35722
  2001
  assumes "inj_on f A"
haftmann@35722
  2002
  shows "card (f ` A) = card A"
haftmann@35722
  2003
proof (cases "finite A")
haftmann@35722
  2004
  case True then show ?thesis using assms by (induct A) simp_all
haftmann@35722
  2005
next
haftmann@35722
  2006
  case False then have "\<not> finite (f ` A)" using assms by (auto dest: finite_imageD)
haftmann@35722
  2007
  with False show ?thesis by simp
haftmann@35722
  2008
qed
haftmann@35722
  2009
haftmann@35722
  2010
lemma bij_betw_same_card: "bij_betw f A B \<Longrightarrow> card A = card B"
haftmann@35722
  2011
by(auto simp: card_image bij_betw_def)
haftmann@35722
  2012
haftmann@35722
  2013
lemma endo_inj_surj: "finite A ==> f ` A \<subseteq> A ==> inj_on f A ==> f ` A = A"
haftmann@35722
  2014
by (simp add: card_seteq card_image)
haftmann@35722
  2015
haftmann@35722
  2016
lemma eq_card_imp_inj_on:
haftmann@35722
  2017
  "[| finite A; card(f ` A) = card A |] ==> inj_on f A"
haftmann@35722
  2018
apply (induct rule:finite_induct)
haftmann@35722
  2019
apply simp
haftmann@35722
  2020
apply(frule card_image_le[where f = f])
haftmann@35722
  2021
apply(simp add:card_insert_if split:if_splits)
haftmann@35722
  2022
done
haftmann@35722
  2023
haftmann@35722
  2024
lemma inj_on_iff_eq_card:
haftmann@35722
  2025
  "finite A ==> inj_on f A = (card(f ` A) = card A)"
haftmann@35722
  2026
by(blast intro: card_image eq_card_imp_inj_on)
haftmann@35722
  2027
haftmann@35722
  2028
haftmann@35722
  2029
lemma card_inj_on_le:
haftmann@35722
  2030
  "[|inj_on f A; f ` A \<subseteq> B; finite B |] ==> card A \<le> card B"
haftmann@35722
  2031
apply (subgoal_tac "finite A") 
haftmann@35722
  2032
 apply (force intro: card_mono simp add: card_image [symmetric])
haftmann@35722
  2033
apply (blast intro: finite_imageD dest: finite_subset) 
haftmann@35722
  2034
done
haftmann@35722
  2035
haftmann@35722
  2036
lemma card_bij_eq:
haftmann@35722
  2037
  "[|inj_on f A; f ` A \<subseteq> B; inj_on g B; g ` B \<subseteq> A;
haftmann@35722
  2038
     finite A; finite B |] ==> card A = card B"
haftmann@35722
  2039
by (auto intro: le_antisym card_inj_on_le)
haftmann@35722
  2040
haftmann@35722
  2041
haftmann@35722
  2042
subsubsection {* Cardinality of sums *}
haftmann@35722
  2043
haftmann@35722
  2044
lemma card_Plus:
haftmann@35722
  2045
  assumes "finite A" and "finite B"
haftmann@35722
  2046
  shows "card (A <+> B) = card A + card B"
haftmann@35722
  2047
proof -
haftmann@35722
  2048
  have "Inl`A \<inter> Inr`B = {}" by fast
haftmann@35722
  2049
  with assms show ?thesis
haftmann@35722
  2050
    unfolding Plus_def
haftmann@35722
  2051
    by (simp add: card_Un_disjoint card_image)
haftmann@35722
  2052
qed
haftmann@35722
  2053
haftmann@35722
  2054
lemma card_Plus_conv_if:
haftmann@35722
  2055
  "card (A <+> B) = (if finite A \<and> finite B then card A + card B else 0)"
haftmann@35722
  2056
  by (auto simp add: card_Plus)
haftmann@35722
  2057
haftmann@35722
  2058
haftmann@35722
  2059
subsubsection {* Cardinality of the Powerset *}
haftmann@35722
  2060
haftmann@35722
  2061
lemma card_Pow: "finite A ==> card (Pow A) = Suc (Suc 0) ^ card A"  (* FIXME numeral 2 (!?) *)
haftmann@35722
  2062
apply (induct set: finite)
haftmann@35722
  2063
 apply (simp_all add: Pow_insert)
haftmann@35722
  2064
apply (subst card_Un_disjoint, blast)
haftmann@35722
  2065
  apply (blast intro: finite_imageI, blast)
haftmann@35722
  2066
apply (subgoal_tac "inj_on (insert x) (Pow F)")
haftmann@35722
  2067
 apply (simp add: card_image Pow_insert)
haftmann@35722
  2068
apply (unfold inj_on_def)
haftmann@35722
  2069
apply (blast elim!: equalityE)
haftmann@35722
  2070
done
haftmann@35722
  2071
haftmann@35722
  2072
text {* Relates to equivalence classes.  Based on a theorem of F. Kammüller.  *}
haftmann@35722
  2073
haftmann@35722
  2074
lemma dvd_partition:
haftmann@35722
  2075
  "finite (Union C) ==>
haftmann@35722
  2076
    ALL c : C. k dvd card c ==>
haftmann@35722
  2077
    (ALL c1: C. ALL c2: C. c1 \<noteq> c2 --> c1 Int c2 = {}) ==>
haftmann@35722
  2078
  k dvd card (Union C)"
haftmann@35722
  2079
apply(frule finite_UnionD)
haftmann@35722
  2080
apply(rotate_tac -1)
haftmann@35722
  2081
apply (induct set: finite, simp_all, clarify)
haftmann@35722
  2082
apply (subst card_Un_disjoint)
haftmann@35722
  2083
   apply (auto simp add: disjoint_eq_subset_Compl)
haftmann@35722
  2084
done
haftmann@35722
  2085
haftmann@35722
  2086
haftmann@35722
  2087
subsubsection {* Relating injectivity and surjectivity *}
haftmann@35722
  2088
haftmann@35722
  2089
lemma finite_surj_inj: "finite(A) \<Longrightarrow> A <= f`A \<Longrightarrow> inj_on f A"
haftmann@35722
  2090
apply(rule eq_card_imp_inj_on, assumption)
haftmann@35722
  2091
apply(frule finite_imageI)
haftmann@35722
  2092
apply(drule (1) card_seteq)
haftmann@35722
  2093
 apply(erule card_image_le)
haftmann@35722
  2094
apply simp
haftmann@35722
  2095
done
haftmann@35722
  2096
haftmann@35722
  2097
lemma finite_UNIV_surj_inj: fixes f :: "'a \<Rightarrow> 'a"
haftmann@35722
  2098
shows "finite(UNIV:: 'a set) \<Longrightarrow> surj f \<Longrightarrow> inj f"
haftmann@35722
  2099
by (blast intro: finite_surj_inj subset_UNIV dest:surj_range)
haftmann@35722
  2100
haftmann@35722
  2101
lemma finite_UNIV_inj_surj: fixes f :: "'a \<Rightarrow> 'a"
haftmann@35722
  2102
shows "finite(UNIV:: 'a set) \<Longrightarrow> inj f \<Longrightarrow> surj f"
haftmann@35722
  2103
by(fastsimp simp:surj_def dest!: endo_inj_surj)
haftmann@35722
  2104
haftmann@35722
  2105
corollary infinite_UNIV_nat[iff]: "~finite(UNIV::nat set)"
haftmann@35722
  2106
proof
haftmann@35722
  2107
  assume "finite(UNIV::nat set)"
haftmann@35722
  2108
  with finite_UNIV_inj_surj[of Suc]
haftmann@35722
  2109
  show False by simp (blast dest: Suc_neq_Zero surjD)
haftmann@35722
  2110
qed
haftmann@35722
  2111
haftmann@35722
  2112
(* Often leads to bogus ATP proofs because of reduced type information, hence noatp *)
haftmann@35722
  2113
lemma infinite_UNIV_char_0[noatp]:
haftmann@35722
  2114
  "\<not> finite (UNIV::'a::semiring_char_0 set)"
haftmann@35722
  2115
proof
haftmann@35722
  2116
  assume "finite (UNIV::'a set)"
haftmann@35722
  2117
  with subset_UNIV have "finite (range of_nat::'a set)"
haftmann@35722
  2118
    by (rule finite_subset)
haftmann@35722
  2119
  moreover have "inj (of_nat::nat \<Rightarrow> 'a)"
haftmann@35722
  2120
    by (simp add: inj_on_def)
haftmann@35722
  2121
  ultimately have "finite (UNIV::nat set)"
haftmann@35722
  2122
    by (rule finite_imageD)
haftmann@35722
  2123
  then show "False"
haftmann@35722
  2124
    by simp
haftmann@35722
  2125
qed
haftmann@35722
  2126
haftmann@35722
  2127
end