src/HOL/Finite_Set.thy
author haftmann
Wed Mar 10 16:53:27 2010 +0100 (2010-03-10)
changeset 35719 99b6152aedf5
parent 35577 43b93e294522
child 35722 69419a09a7ff
permissions -rw-r--r--
split off theory Big_Operators from theory Finite_Set
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@35719
  1477
  assumes eq_fold: "F A s = Finite_Set.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@35719
  1499
  have "\<And>s. Finite_Set.fold f s (insert x A) = Finite_Set.fold f (f x s) A" .
haftmann@35719
  1500
  then 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@35719
  1516
proof (cases "x \<in> A")
haftmann@35719
  1517
  case True with assms show ?thesis by (simp add: remove insert_absorb)
haftmann@35719
  1518
next
haftmann@35719
  1519
  case False with assms show ?thesis by simp
haftmann@35719
  1520
qed
haftmann@35719
  1521
haftmann@35719
  1522
lemma commute_comp':
haftmann@35719
  1523
  assumes "finite A"
haftmann@35719
  1524
  shows "f x \<circ> F A = F A \<circ> f x"
haftmann@35719
  1525
proof (rule ext)
haftmann@35719
  1526
  fix s
haftmann@35719
  1527
  from assms show "(f x \<circ> F A) s = (F A \<circ> f x) s"
haftmann@35719
  1528
    by (induct A arbitrary: s) (simp_all add: fun_left_commute)
haftmann@35719
  1529
qed
haftmann@35719
  1530
haftmann@35719
  1531
lemma fun_left_commute':
haftmann@35719
  1532
  assumes "finite A"
haftmann@35719
  1533
  shows "f x (F A s) = F A (f x s)"
haftmann@35719
  1534
  using commute_comp' assms by (simp add: expand_fun_eq)
haftmann@35719
  1535
haftmann@35719
  1536
lemma union:
haftmann@35719
  1537
  assumes "finite A" and "finite B"
haftmann@35719
  1538
  and "A \<inter> B = {}"
haftmann@35719
  1539
  shows "F (A \<union> B) = F A \<circ> F B"
haftmann@35719
  1540
using `finite A` `A \<inter> B = {}` proof (induct A)
haftmann@35719
  1541
  case empty show ?case by simp
haftmann@35719
  1542
next
haftmann@35719
  1543
  case (insert x A)
haftmann@35719
  1544
  then have "A \<inter> B = {}" by auto
haftmann@35719
  1545
  with insert(3) have "F (A \<union> B) = F A \<circ> F B" .
haftmann@35719
  1546
  moreover from insert have "x \<notin> B" by simp
haftmann@35719
  1547
  moreover from `finite A` `finite B` have fin: "finite (A \<union> B)" by simp
haftmann@35719
  1548
  moreover from `x \<notin> A` `x \<notin> B` have "x \<notin> A \<union> B" by simp
haftmann@35719
  1549
  ultimately show ?case by (simp add: fun_left_commute')
haftmann@35719
  1550
qed
haftmann@35719
  1551
haftmann@34007
  1552
end
haftmann@35719
  1553
haftmann@35719
  1554
locale folding_idem = folding +
haftmann@35719
  1555
  assumes idem_comp: "f x \<circ> f x = f x"
haftmann@35719
  1556
begin
haftmann@35719
  1557
haftmann@35719
  1558
declare insert [simp del]
haftmann@35719
  1559
haftmann@35719
  1560
lemma fun_idem:
haftmann@35719
  1561
  "f x (f x s) = f x s"
haftmann@35719
  1562
  using idem_comp [of x] by (simp add: expand_fun_eq)
haftmann@35719
  1563
haftmann@35719
  1564
lemma fun_left_comm_idem:
haftmann@35719
  1565
  "fun_left_comm_idem f"
haftmann@35719
  1566
proof
haftmann@35719
  1567
qed (fact fun_left_commute fun_idem)+
haftmann@35719
  1568
haftmann@35719
  1569
lemma insert_idem [simp]:
haftmann@35719
  1570
  assumes "finite A"
haftmann@35719
  1571
  shows "F (insert x A) = F A \<circ> f x"
haftmann@35719
  1572
proof -
haftmann@35719
  1573
  interpret fun_left_comm_idem f by (fact fun_left_comm_idem)
haftmann@35719
  1574
  from fold_insert_idem2 assms
haftmann@35719
  1575
  have "\<And>s. Finite_Set.fold f s (insert x A) = Finite_Set.fold f (f x s) A" .
haftmann@35719
  1576
  then show ?thesis by (simp add: eq_fold expand_fun_eq)
haftmann@35719
  1577
qed
haftmann@35719
  1578
haftmann@35719
  1579
lemma union_idem:
haftmann@35719
  1580
  assumes "finite A" and "finite B"
haftmann@35719
  1581
  shows "F (A \<union> B) = F A \<circ> F B"
haftmann@35719
  1582
using `finite A` proof (induct A)
haftmann@35719
  1583
  case empty show ?case by simp
haftmann@35719
  1584
next
haftmann@35719
  1585
  case (insert x A)
haftmann@35719
  1586
  from insert(3) have "F (A \<union> B) = F A \<circ> F B" .
haftmann@35719
  1587
  moreover from `finite A` `finite B` have fin: "finite (A \<union> B)" by simp
haftmann@35719
  1588
  ultimately show ?case by (simp add: fun_left_commute')
haftmann@35719
  1589
qed
haftmann@35719
  1590
haftmann@35719
  1591
end
haftmann@35719
  1592
haftmann@35719
  1593
end