src/HOL/Algebra/FiniteProduct.thy
author wenzelm
Sun Mar 21 15:57:40 2010 +0100 (2010-03-21)
changeset 35847 19f1f7066917
parent 35416 d8d7d1b785af
child 35848 5443079512ea
permissions -rw-r--r--
eliminated old constdefs;
wenzelm@14706
     1
(*  Title:      HOL/Algebra/FiniteProduct.thy
ballarin@13936
     2
    Author:     Clemens Ballarin, started 19 November 2002
ballarin@13936
     3
ballarin@13936
     4
This file is largely based on HOL/Finite_Set.thy.
ballarin@13936
     5
*)
ballarin@13936
     6
ballarin@20318
     7
theory FiniteProduct imports Group begin
ballarin@20318
     8
ballarin@13936
     9
ballarin@27717
    10
subsection {* Product Operator for Commutative Monoids *}
ballarin@20318
    11
ballarin@27717
    12
subsubsection {* Inductive Definition of a Relation for Products over Sets *}
ballarin@13936
    13
paulson@14750
    14
text {* Instantiation of locale @{text LC} of theory @{text Finite_Set} is not
paulson@14750
    15
  possible, because here we have explicit typing rules like 
paulson@14750
    16
  @{text "x \<in> carrier G"}.  We introduce an explicit argument for the domain
wenzelm@14651
    17
  @{text D}. *}
ballarin@13936
    18
berghofe@23746
    19
inductive_set
ballarin@13936
    20
  foldSetD :: "['a set, 'b => 'a => 'a, 'a] => ('b set * 'a) set"
berghofe@23746
    21
  for D :: "'a set" and f :: "'b => 'a => 'a" and e :: 'a
berghofe@23746
    22
  where
paulson@14750
    23
    emptyI [intro]: "e \<in> D ==> ({}, e) \<in> foldSetD D f e"
berghofe@23746
    24
  | insertI [intro]: "[| x ~: A; f x y \<in> D; (A, y) \<in> foldSetD D f e |] ==>
paulson@14750
    25
                      (insert x A, f x y) \<in> foldSetD D f e"
ballarin@13936
    26
paulson@14750
    27
inductive_cases empty_foldSetDE [elim!]: "({}, x) \<in> foldSetD D f e"
ballarin@13936
    28
haftmann@35416
    29
definition foldD :: "['a set, 'b => 'a => 'a, 'a, 'b set] => 'a" where
paulson@14750
    30
  "foldD D f e A == THE x. (A, x) \<in> foldSetD D f e"
ballarin@13936
    31
ballarin@13936
    32
lemma foldSetD_closed:
paulson@14750
    33
  "[| (A, z) \<in> foldSetD D f e ; e \<in> D; !!x y. [| x \<in> A; y \<in> D |] ==> f x y \<in> D 
paulson@14750
    34
      |] ==> z \<in> D";
berghofe@23746
    35
  by (erule foldSetD.cases) auto
ballarin@13936
    36
ballarin@13936
    37
lemma Diff1_foldSetD:
paulson@14750
    38
  "[| (A - {x}, y) \<in> foldSetD D f e; x \<in> A; f x y \<in> D |] ==>
paulson@14750
    39
   (A, f x y) \<in> foldSetD D f e"
ballarin@13936
    40
  apply (erule insert_Diff [THEN subst], rule foldSetD.intros)
ballarin@13936
    41
    apply auto
ballarin@13936
    42
  done
ballarin@13936
    43
paulson@14750
    44
lemma foldSetD_imp_finite [simp]: "(A, x) \<in> foldSetD D f e ==> finite A"
ballarin@13936
    45
  by (induct set: foldSetD) auto
ballarin@13936
    46
ballarin@13936
    47
lemma finite_imp_foldSetD:
paulson@14750
    48
  "[| finite A; e \<in> D; !!x y. [| x \<in> A; y \<in> D |] ==> f x y \<in> D |] ==>
paulson@14750
    49
   EX x. (A, x) \<in> foldSetD D f e"
berghofe@22265
    50
proof (induct set: finite)
ballarin@13936
    51
  case empty then show ?case by auto
ballarin@13936
    52
next
nipkow@15328
    53
  case (insert x F)
paulson@14750
    54
  then obtain y where y: "(F, y) \<in> foldSetD D f e" by auto
paulson@14750
    55
  with insert have "y \<in> D" by (auto dest: foldSetD_closed)
paulson@14750
    56
  with y and insert have "(insert x F, f x y) \<in> foldSetD D f e"
ballarin@13936
    57
    by (intro foldSetD.intros) auto
ballarin@13936
    58
  then show ?case ..
ballarin@13936
    59
qed
ballarin@13936
    60
ballarin@20318
    61
ballarin@27717
    62
text {* Left-Commutative Operations *}
ballarin@13936
    63
ballarin@13936
    64
locale LCD =
ballarin@13936
    65
  fixes B :: "'b set"
ballarin@13936
    66
  and D :: "'a set"
ballarin@13936
    67
  and f :: "'b => 'a => 'a"    (infixl "\<cdot>" 70)
ballarin@13936
    68
  assumes left_commute:
paulson@14750
    69
    "[| x \<in> B; y \<in> B; z \<in> D |] ==> x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
paulson@14750
    70
  and f_closed [simp, intro!]: "!!x y. [| x \<in> B; y \<in> D |] ==> f x y \<in> D"
ballarin@13936
    71
ballarin@13936
    72
lemma (in LCD) foldSetD_closed [dest]:
paulson@14750
    73
  "(A, z) \<in> foldSetD D f e ==> z \<in> D";
berghofe@23746
    74
  by (erule foldSetD.cases) auto
ballarin@13936
    75
ballarin@13936
    76
lemma (in LCD) Diff1_foldSetD:
paulson@14750
    77
  "[| (A - {x}, y) \<in> foldSetD D f e; x \<in> A; A \<subseteq> B |] ==>
paulson@14750
    78
  (A, f x y) \<in> foldSetD D f e"
paulson@14750
    79
  apply (subgoal_tac "x \<in> B")
ballarin@13936
    80
   prefer 2 apply fast
ballarin@13936
    81
  apply (erule insert_Diff [THEN subst], rule foldSetD.intros)
ballarin@13936
    82
    apply auto
ballarin@13936
    83
  done
ballarin@13936
    84
ballarin@13936
    85
lemma (in LCD) foldSetD_imp_finite [simp]:
paulson@14750
    86
  "(A, x) \<in> foldSetD D f e ==> finite A"
ballarin@13936
    87
  by (induct set: foldSetD) auto
ballarin@13936
    88
ballarin@13936
    89
lemma (in LCD) finite_imp_foldSetD:
paulson@14750
    90
  "[| finite A; A \<subseteq> B; e \<in> D |] ==> EX x. (A, x) \<in> foldSetD D f e"
berghofe@22265
    91
proof (induct set: finite)
ballarin@13936
    92
  case empty then show ?case by auto
ballarin@13936
    93
next
nipkow@15328
    94
  case (insert x F)
paulson@14750
    95
  then obtain y where y: "(F, y) \<in> foldSetD D f e" by auto
paulson@14750
    96
  with insert have "y \<in> D" by auto
paulson@14750
    97
  with y and insert have "(insert x F, f x y) \<in> foldSetD D f e"
ballarin@13936
    98
    by (intro foldSetD.intros) auto
ballarin@13936
    99
  then show ?case ..
ballarin@13936
   100
qed
ballarin@13936
   101
ballarin@13936
   102
lemma (in LCD) foldSetD_determ_aux:
paulson@14750
   103
  "e \<in> D ==> \<forall>A x. A \<subseteq> B & card A < n --> (A, x) \<in> foldSetD D f e -->
paulson@14750
   104
    (\<forall>y. (A, y) \<in> foldSetD D f e --> y = x)"
ballarin@13936
   105
  apply (induct n)
ballarin@13936
   106
   apply (auto simp add: less_Suc_eq) (* slow *)
ballarin@13936
   107
  apply (erule foldSetD.cases)
ballarin@13936
   108
   apply blast
ballarin@13936
   109
  apply (erule foldSetD.cases)
ballarin@13936
   110
   apply blast
ballarin@13936
   111
  apply clarify
ballarin@13936
   112
  txt {* force simplification of @{text "card A < card (insert ...)"}. *}
ballarin@13936
   113
  apply (erule rev_mp)
ballarin@13936
   114
  apply (simp add: less_Suc_eq_le)
ballarin@13936
   115
  apply (rule impI)
berghofe@23746
   116
  apply (rename_tac xa Aa ya xb Ab yb, case_tac "xa = xb")
ballarin@13936
   117
   apply (subgoal_tac "Aa = Ab")
ballarin@13936
   118
    prefer 2 apply (blast elim!: equalityE)
ballarin@13936
   119
   apply blast
ballarin@13936
   120
  txt {* case @{prop "xa \<notin> xb"}. *}
paulson@14750
   121
  apply (subgoal_tac "Aa - {xb} = Ab - {xa} & xb \<in> Aa & xa \<in> Ab")
ballarin@13936
   122
   prefer 2 apply (blast elim!: equalityE)
ballarin@13936
   123
  apply clarify
ballarin@13936
   124
  apply (subgoal_tac "Aa = insert xb Ab - {xa}")
ballarin@13936
   125
   prefer 2 apply blast
paulson@14750
   126
  apply (subgoal_tac "card Aa \<le> card Ab")
ballarin@13936
   127
   prefer 2
ballarin@13936
   128
   apply (rule Suc_le_mono [THEN subst])
ballarin@13936
   129
   apply (simp add: card_Suc_Diff1)
ballarin@13936
   130
  apply (rule_tac A1 = "Aa - {xb}" in finite_imp_foldSetD [THEN exE])
ballarin@13936
   131
     apply (blast intro: foldSetD_imp_finite finite_Diff)
ballarin@13936
   132
    apply best
ballarin@13936
   133
   apply assumption
ballarin@13936
   134
  apply (frule (1) Diff1_foldSetD)
ballarin@13936
   135
   apply best
ballarin@13936
   136
  apply (subgoal_tac "ya = f xb x")
ballarin@13936
   137
   prefer 2
paulson@14750
   138
   apply (subgoal_tac "Aa \<subseteq> B")
ballarin@13936
   139
    prefer 2 apply best (* slow *)
ballarin@13936
   140
   apply (blast del: equalityCE)
paulson@14750
   141
  apply (subgoal_tac "(Ab - {xa}, x) \<in> foldSetD D f e")
ballarin@13936
   142
   prefer 2 apply simp
ballarin@13936
   143
  apply (subgoal_tac "yb = f xa x")
ballarin@13936
   144
   prefer 2 
ballarin@13936
   145
   apply (blast del: equalityCE dest: Diff1_foldSetD)
ballarin@13936
   146
  apply (simp (no_asm_simp))
ballarin@13936
   147
  apply (rule left_commute)
ballarin@13936
   148
    apply assumption
ballarin@13936
   149
   apply best (* slow *)
ballarin@13936
   150
  apply best
ballarin@13936
   151
  done
ballarin@13936
   152
ballarin@13936
   153
lemma (in LCD) foldSetD_determ:
paulson@14750
   154
  "[| (A, x) \<in> foldSetD D f e; (A, y) \<in> foldSetD D f e; e \<in> D; A \<subseteq> B |]
ballarin@13936
   155
  ==> y = x"
ballarin@13936
   156
  by (blast intro: foldSetD_determ_aux [rule_format])
ballarin@13936
   157
ballarin@13936
   158
lemma (in LCD) foldD_equality:
paulson@14750
   159
  "[| (A, y) \<in> foldSetD D f e; e \<in> D; A \<subseteq> B |] ==> foldD D f e A = y"
ballarin@13936
   160
  by (unfold foldD_def) (blast intro: foldSetD_determ)
ballarin@13936
   161
ballarin@13936
   162
lemma foldD_empty [simp]:
paulson@14750
   163
  "e \<in> D ==> foldD D f e {} = e"
ballarin@13936
   164
  by (unfold foldD_def) blast
ballarin@13936
   165
ballarin@13936
   166
lemma (in LCD) foldD_insert_aux:
paulson@14750
   167
  "[| x ~: A; x \<in> B; e \<in> D; A \<subseteq> B |] ==>
paulson@14750
   168
    ((insert x A, v) \<in> foldSetD D f e) =
paulson@14750
   169
    (EX y. (A, y) \<in> foldSetD D f e & v = f x y)"
ballarin@13936
   170
  apply auto
ballarin@13936
   171
  apply (rule_tac A1 = A in finite_imp_foldSetD [THEN exE])
ballarin@13936
   172
     apply (fastsimp dest: foldSetD_imp_finite)
ballarin@13936
   173
    apply assumption
ballarin@13936
   174
   apply assumption
ballarin@13936
   175
  apply (blast intro: foldSetD_determ)
ballarin@13936
   176
  done
ballarin@13936
   177
ballarin@13936
   178
lemma (in LCD) foldD_insert:
paulson@14750
   179
    "[| finite A; x ~: A; x \<in> B; e \<in> D; A \<subseteq> B |] ==>
ballarin@13936
   180
     foldD D f e (insert x A) = f x (foldD D f e A)"
ballarin@13936
   181
  apply (unfold foldD_def)
ballarin@13936
   182
  apply (simp add: foldD_insert_aux)
ballarin@13936
   183
  apply (rule the_equality)
ballarin@13936
   184
   apply (auto intro: finite_imp_foldSetD
ballarin@13936
   185
     cong add: conj_cong simp add: foldD_def [symmetric] foldD_equality)
ballarin@13936
   186
  done
ballarin@13936
   187
ballarin@13936
   188
lemma (in LCD) foldD_closed [simp]:
paulson@14750
   189
  "[| finite A; e \<in> D; A \<subseteq> B |] ==> foldD D f e A \<in> D"
berghofe@22265
   190
proof (induct set: finite)
ballarin@13936
   191
  case empty then show ?case by (simp add: foldD_empty)
ballarin@13936
   192
next
ballarin@13936
   193
  case insert then show ?case by (simp add: foldD_insert)
ballarin@13936
   194
qed
ballarin@13936
   195
ballarin@13936
   196
lemma (in LCD) foldD_commute:
paulson@14750
   197
  "[| finite A; x \<in> B; e \<in> D; A \<subseteq> B |] ==>
ballarin@13936
   198
   f x (foldD D f e A) = foldD D f (f x e) A"
berghofe@22265
   199
  apply (induct set: finite)
ballarin@13936
   200
   apply simp
ballarin@13936
   201
  apply (auto simp add: left_commute foldD_insert)
ballarin@13936
   202
  done
ballarin@13936
   203
ballarin@13936
   204
lemma Int_mono2:
paulson@14750
   205
  "[| A \<subseteq> C; B \<subseteq> C |] ==> A Int B \<subseteq> C"
ballarin@13936
   206
  by blast
ballarin@13936
   207
ballarin@13936
   208
lemma (in LCD) foldD_nest_Un_Int:
paulson@14750
   209
  "[| finite A; finite C; e \<in> D; A \<subseteq> B; C \<subseteq> B |] ==>
ballarin@13936
   210
   foldD D f (foldD D f e C) A = foldD D f (foldD D f e (A Int C)) (A Un C)"
berghofe@22265
   211
  apply (induct set: finite)
ballarin@13936
   212
   apply simp
ballarin@13936
   213
  apply (simp add: foldD_insert foldD_commute Int_insert_left insert_absorb
haftmann@32693
   214
    Int_mono2)
ballarin@13936
   215
  done
ballarin@13936
   216
ballarin@13936
   217
lemma (in LCD) foldD_nest_Un_disjoint:
paulson@14750
   218
  "[| finite A; finite B; A Int B = {}; e \<in> D; A \<subseteq> B; C \<subseteq> B |]
ballarin@13936
   219
    ==> foldD D f e (A Un B) = foldD D f (foldD D f e B) A"
ballarin@13936
   220
  by (simp add: foldD_nest_Un_Int)
ballarin@13936
   221
ballarin@13936
   222
-- {* Delete rules to do with @{text foldSetD} relation. *}
ballarin@13936
   223
ballarin@13936
   224
declare foldSetD_imp_finite [simp del]
ballarin@13936
   225
  empty_foldSetDE [rule del]
ballarin@13936
   226
  foldSetD.intros [rule del]
ballarin@13936
   227
declare (in LCD)
ballarin@13936
   228
  foldSetD_closed [rule del]
ballarin@13936
   229
ballarin@20318
   230
ballarin@27717
   231
text {* Commutative Monoids *}
ballarin@13936
   232
ballarin@13936
   233
text {*
ballarin@13936
   234
  We enter a more restrictive context, with @{text "f :: 'a => 'a => 'a"}
ballarin@13936
   235
  instead of @{text "'b => 'a => 'a"}.
ballarin@13936
   236
*}
ballarin@13936
   237
ballarin@13936
   238
locale ACeD =
ballarin@13936
   239
  fixes D :: "'a set"
ballarin@13936
   240
    and f :: "'a => 'a => 'a"    (infixl "\<cdot>" 70)
ballarin@13936
   241
    and e :: 'a
paulson@14750
   242
  assumes ident [simp]: "x \<in> D ==> x \<cdot> e = x"
paulson@14750
   243
    and commute: "[| x \<in> D; y \<in> D |] ==> x \<cdot> y = y \<cdot> x"
paulson@14750
   244
    and assoc: "[| x \<in> D; y \<in> D; z \<in> D |] ==> (x \<cdot> y) \<cdot> z = x \<cdot> (y \<cdot> z)"
paulson@14750
   245
    and e_closed [simp]: "e \<in> D"
paulson@14750
   246
    and f_closed [simp]: "[| x \<in> D; y \<in> D |] ==> x \<cdot> y \<in> D"
ballarin@13936
   247
ballarin@13936
   248
lemma (in ACeD) left_commute:
paulson@14750
   249
  "[| x \<in> D; y \<in> D; z \<in> D |] ==> x \<cdot> (y \<cdot> z) = y \<cdot> (x \<cdot> z)"
ballarin@13936
   250
proof -
paulson@14750
   251
  assume D: "x \<in> D" "y \<in> D" "z \<in> D"
ballarin@13936
   252
  then have "x \<cdot> (y \<cdot> z) = (y \<cdot> z) \<cdot> x" by (simp add: commute)
ballarin@13936
   253
  also from D have "... = y \<cdot> (z \<cdot> x)" by (simp add: assoc)
ballarin@13936
   254
  also from D have "z \<cdot> x = x \<cdot> z" by (simp add: commute)
ballarin@13936
   255
  finally show ?thesis .
ballarin@13936
   256
qed
ballarin@13936
   257
ballarin@13936
   258
lemmas (in ACeD) AC = assoc commute left_commute
ballarin@13936
   259
paulson@14750
   260
lemma (in ACeD) left_ident [simp]: "x \<in> D ==> e \<cdot> x = x"
ballarin@13936
   261
proof -
wenzelm@23350
   262
  assume "x \<in> D"
wenzelm@23350
   263
  then have "x \<cdot> e = x" by (rule ident)
wenzelm@23350
   264
  with `x \<in> D` show ?thesis by (simp add: commute)
ballarin@13936
   265
qed
ballarin@13936
   266
ballarin@13936
   267
lemma (in ACeD) foldD_Un_Int:
paulson@14750
   268
  "[| finite A; finite B; A \<subseteq> D; B \<subseteq> D |] ==>
ballarin@13936
   269
    foldD D f e A \<cdot> foldD D f e B =
ballarin@13936
   270
    foldD D f e (A Un B) \<cdot> foldD D f e (A Int B)"
berghofe@22265
   271
  apply (induct set: finite)
ballarin@13936
   272
   apply (simp add: left_commute LCD.foldD_closed [OF LCD.intro [of D]])
ballarin@13936
   273
  apply (simp add: AC insert_absorb Int_insert_left
ballarin@13936
   274
    LCD.foldD_insert [OF LCD.intro [of D]]
ballarin@13936
   275
    LCD.foldD_closed [OF LCD.intro [of D]]
haftmann@32693
   276
    Int_mono2)
ballarin@13936
   277
  done
ballarin@13936
   278
ballarin@13936
   279
lemma (in ACeD) foldD_Un_disjoint:
paulson@14750
   280
  "[| finite A; finite B; A Int B = {}; A \<subseteq> D; B \<subseteq> D |] ==>
ballarin@13936
   281
    foldD D f e (A Un B) = foldD D f e A \<cdot> foldD D f e B"
ballarin@13936
   282
  by (simp add: foldD_Un_Int
haftmann@32693
   283
    left_commute LCD.foldD_closed [OF LCD.intro [of D]])
ballarin@13936
   284
ballarin@20318
   285
ballarin@27717
   286
subsubsection {* Products over Finite Sets *}
ballarin@13936
   287
wenzelm@35847
   288
definition
wenzelm@35847
   289
  finprod :: "[('b, 'm) monoid_scheme, 'a => 'b, 'a set] => 'b" where
wenzelm@35847
   290
  "finprod G f A ==
wenzelm@35847
   291
    if finite A
wenzelm@35847
   292
    then foldD (carrier G) (mult G o f) \<one>\<^bsub>G\<^esub> A
wenzelm@35847
   293
    else undefined"
ballarin@13936
   294
wenzelm@14651
   295
syntax
wenzelm@14651
   296
  "_finprod" :: "index => idt => 'a set => 'b => 'b"
wenzelm@14666
   297
      ("(3\<Otimes>__:_. _)" [1000, 0, 51, 10] 10)
wenzelm@14651
   298
syntax (xsymbols)
wenzelm@14651
   299
  "_finprod" :: "index => idt => 'a set => 'b => 'b"
wenzelm@14666
   300
      ("(3\<Otimes>__\<in>_. _)" [1000, 0, 51, 10] 10)
wenzelm@14651
   301
syntax (HTML output)
wenzelm@14651
   302
  "_finprod" :: "index => idt => 'a set => 'b => 'b"
wenzelm@14666
   303
      ("(3\<Otimes>__\<in>_. _)" [1000, 0, 51, 10] 10)
wenzelm@14651
   304
translations
wenzelm@35054
   305
  "\<Otimes>\<index>i:A. b" == "CONST finprod \<struct>\<index> (%i. b) A"
ballarin@15095
   306
  -- {* Beware of argument permutation! *}
ballarin@13936
   307
ballarin@13936
   308
lemma (in comm_monoid) finprod_empty [simp]: 
ballarin@13936
   309
  "finprod G f {} = \<one>"
ballarin@13936
   310
  by (simp add: finprod_def)
ballarin@13936
   311
ballarin@13936
   312
declare funcsetI [intro]
ballarin@13936
   313
  funcset_mem [dest]
ballarin@13936
   314
ballarin@27933
   315
context comm_monoid begin
ballarin@27933
   316
ballarin@27933
   317
lemma finprod_insert [simp]:
ballarin@13936
   318
  "[| finite F; a \<notin> F; f \<in> F -> carrier G; f a \<in> carrier G |] ==>
ballarin@13936
   319
   finprod G f (insert a F) = f a \<otimes> finprod G f F"
ballarin@13936
   320
  apply (rule trans)
ballarin@13936
   321
   apply (simp add: finprod_def)
ballarin@13936
   322
  apply (rule trans)
ballarin@13936
   323
   apply (rule LCD.foldD_insert [OF LCD.intro [of "insert a F"]])
ballarin@13936
   324
         apply simp
ballarin@13936
   325
         apply (rule m_lcomm)
ballarin@13936
   326
           apply fast
ballarin@13936
   327
          apply fast
ballarin@13936
   328
         apply assumption
ballarin@13936
   329
        apply (fastsimp intro: m_closed)
ballarin@13936
   330
       apply simp+
ballarin@13936
   331
   apply fast
ballarin@13936
   332
  apply (auto simp add: finprod_def)
ballarin@13936
   333
  done
ballarin@13936
   334
ballarin@27933
   335
lemma finprod_one [simp]:
wenzelm@14651
   336
  "finite A ==> (\<Otimes>i:A. \<one>) = \<one>"
berghofe@22265
   337
proof (induct set: finite)
ballarin@13936
   338
  case empty show ?case by simp
ballarin@13936
   339
next
nipkow@15328
   340
  case (insert a A)
ballarin@13936
   341
  have "(%i. \<one>) \<in> A -> carrier G" by auto
ballarin@13936
   342
  with insert show ?case by simp
ballarin@13936
   343
qed
ballarin@13936
   344
ballarin@27933
   345
lemma finprod_closed [simp]:
ballarin@13936
   346
  fixes A
ballarin@13936
   347
  assumes fin: "finite A" and f: "f \<in> A -> carrier G" 
ballarin@13936
   348
  shows "finprod G f A \<in> carrier G"
ballarin@13936
   349
using fin f
ballarin@13936
   350
proof induct
ballarin@13936
   351
  case empty show ?case by simp
ballarin@13936
   352
next
nipkow@15328
   353
  case (insert a A)
ballarin@13936
   354
  then have a: "f a \<in> carrier G" by fast
ballarin@13936
   355
  from insert have A: "f \<in> A -> carrier G" by fast
ballarin@13936
   356
  from insert A a show ?case by simp
ballarin@13936
   357
qed
ballarin@13936
   358
ballarin@13936
   359
lemma funcset_Int_left [simp, intro]:
ballarin@13936
   360
  "[| f \<in> A -> C; f \<in> B -> C |] ==> f \<in> A Int B -> C"
ballarin@13936
   361
  by fast
ballarin@13936
   362
ballarin@13936
   363
lemma funcset_Un_left [iff]:
ballarin@13936
   364
  "(f \<in> A Un B -> C) = (f \<in> A -> C & f \<in> B -> C)"
ballarin@13936
   365
  by fast
ballarin@13936
   366
ballarin@27933
   367
lemma finprod_Un_Int:
ballarin@13936
   368
  "[| finite A; finite B; g \<in> A -> carrier G; g \<in> B -> carrier G |] ==>
ballarin@13936
   369
     finprod G g (A Un B) \<otimes> finprod G g (A Int B) =
ballarin@13936
   370
     finprod G g A \<otimes> finprod G g B"
ballarin@13936
   371
-- {* The reversed orientation looks more natural, but LOOPS as a simprule! *}
berghofe@22265
   372
proof (induct set: finite)
ballarin@13936
   373
  case empty then show ?case by (simp add: finprod_closed)
ballarin@13936
   374
next
nipkow@15328
   375
  case (insert a A)
ballarin@13936
   376
  then have a: "g a \<in> carrier G" by fast
ballarin@13936
   377
  from insert have A: "g \<in> A -> carrier G" by fast
ballarin@13936
   378
  from insert A a show ?case
ballarin@13936
   379
    by (simp add: m_ac Int_insert_left insert_absorb finprod_closed
haftmann@32693
   380
          Int_mono2) 
ballarin@13936
   381
qed
ballarin@13936
   382
ballarin@27933
   383
lemma finprod_Un_disjoint:
ballarin@13936
   384
  "[| finite A; finite B; A Int B = {};
ballarin@13936
   385
      g \<in> A -> carrier G; g \<in> B -> carrier G |]
ballarin@13936
   386
   ==> finprod G g (A Un B) = finprod G g A \<otimes> finprod G g B"
ballarin@13936
   387
  apply (subst finprod_Un_Int [symmetric])
ballarin@13936
   388
      apply (auto simp add: finprod_closed)
ballarin@13936
   389
  done
ballarin@13936
   390
ballarin@27933
   391
lemma finprod_multf:
ballarin@13936
   392
  "[| finite A; f \<in> A -> carrier G; g \<in> A -> carrier G |] ==>
ballarin@13936
   393
   finprod G (%x. f x \<otimes> g x) A = (finprod G f A \<otimes> finprod G g A)"
berghofe@22265
   394
proof (induct set: finite)
ballarin@13936
   395
  case empty show ?case by simp
ballarin@13936
   396
next
nipkow@15328
   397
  case (insert a A) then
paulson@14750
   398
  have fA: "f \<in> A -> carrier G" by fast
paulson@14750
   399
  from insert have fa: "f a \<in> carrier G" by fast
paulson@14750
   400
  from insert have gA: "g \<in> A -> carrier G" by fast
paulson@14750
   401
  from insert have ga: "g a \<in> carrier G" by fast
paulson@14750
   402
  from insert have fgA: "(%x. f x \<otimes> g x) \<in> A -> carrier G"
ballarin@13936
   403
    by (simp add: Pi_def)
ballarin@15095
   404
  show ?case
ballarin@15095
   405
    by (simp add: insert fA fa gA ga fgA m_ac)
ballarin@13936
   406
qed
ballarin@13936
   407
ballarin@27933
   408
lemma finprod_cong':
paulson@14750
   409
  "[| A = B; g \<in> B -> carrier G;
paulson@14750
   410
      !!i. i \<in> B ==> f i = g i |] ==> finprod G f A = finprod G g B"
ballarin@13936
   411
proof -
paulson@14750
   412
  assume prems: "A = B" "g \<in> B -> carrier G"
paulson@14750
   413
    "!!i. i \<in> B ==> f i = g i"
ballarin@13936
   414
  show ?thesis
ballarin@13936
   415
  proof (cases "finite B")
ballarin@13936
   416
    case True
paulson@14750
   417
    then have "!!A. [| A = B; g \<in> B -> carrier G;
paulson@14750
   418
      !!i. i \<in> B ==> f i = g i |] ==> finprod G f A = finprod G g B"
ballarin@13936
   419
    proof induct
ballarin@13936
   420
      case empty thus ?case by simp
ballarin@13936
   421
    next
nipkow@15328
   422
      case (insert x B)
ballarin@13936
   423
      then have "finprod G f A = finprod G f (insert x B)" by simp
ballarin@13936
   424
      also from insert have "... = f x \<otimes> finprod G f B"
ballarin@13936
   425
      proof (intro finprod_insert)
wenzelm@32960
   426
        show "finite B" by fact
ballarin@13936
   427
      next
wenzelm@32960
   428
        show "x ~: B" by fact
ballarin@13936
   429
      next
wenzelm@32960
   430
        assume "x ~: B" "!!i. i \<in> insert x B \<Longrightarrow> f i = g i"
wenzelm@32960
   431
          "g \<in> insert x B \<rightarrow> carrier G"
wenzelm@32960
   432
        thus "f \<in> B -> carrier G" by fastsimp
ballarin@13936
   433
      next
wenzelm@32960
   434
        assume "x ~: B" "!!i. i \<in> insert x B \<Longrightarrow> f i = g i"
wenzelm@32960
   435
          "g \<in> insert x B \<rightarrow> carrier G"
wenzelm@32960
   436
        thus "f x \<in> carrier G" by fastsimp
ballarin@13936
   437
      qed
ballarin@13936
   438
      also from insert have "... = g x \<otimes> finprod G g B" by fastsimp
ballarin@13936
   439
      also from insert have "... = finprod G g (insert x B)"
ballarin@13936
   440
      by (intro finprod_insert [THEN sym]) auto
ballarin@13936
   441
      finally show ?case .
ballarin@13936
   442
    qed
ballarin@13936
   443
    with prems show ?thesis by simp
ballarin@13936
   444
  next
ballarin@13936
   445
    case False with prems show ?thesis by (simp add: finprod_def)
ballarin@13936
   446
  qed
ballarin@13936
   447
qed
ballarin@13936
   448
ballarin@27933
   449
lemma finprod_cong:
paulson@14750
   450
  "[| A = B; f \<in> B -> carrier G = True;
paulson@14750
   451
      !!i. i \<in> B ==> f i = g i |] ==> finprod G f A = finprod G g B"
ballarin@14213
   452
  (* This order of prems is slightly faster (3%) than the last two swapped. *)
ballarin@14213
   453
  by (rule finprod_cong') force+
ballarin@13936
   454
ballarin@13936
   455
text {*Usually, if this rule causes a failed congruence proof error,
paulson@14750
   456
  the reason is that the premise @{text "g \<in> B -> carrier G"} cannot be shown.
ballarin@13936
   457
  Adding @{thm [source] Pi_def} to the simpset is often useful.
ballarin@13936
   458
  For this reason, @{thm [source] comm_monoid.finprod_cong}
ballarin@13936
   459
  is not added to the simpset by default.
ballarin@13936
   460
*}
ballarin@13936
   461
ballarin@27933
   462
end
ballarin@27933
   463
ballarin@13936
   464
declare funcsetI [rule del]
ballarin@13936
   465
  funcset_mem [rule del]
ballarin@13936
   466
ballarin@27933
   467
context comm_monoid begin
ballarin@27933
   468
ballarin@27933
   469
lemma finprod_0 [simp]:
paulson@14750
   470
  "f \<in> {0::nat} -> carrier G ==> finprod G f {..0} = f 0"
ballarin@13936
   471
by (simp add: Pi_def)
ballarin@13936
   472
ballarin@27933
   473
lemma finprod_Suc [simp]:
paulson@14750
   474
  "f \<in> {..Suc n} -> carrier G ==>
ballarin@13936
   475
   finprod G f {..Suc n} = (f (Suc n) \<otimes> finprod G f {..n})"
ballarin@13936
   476
by (simp add: Pi_def atMost_Suc)
ballarin@13936
   477
ballarin@27933
   478
lemma finprod_Suc2:
paulson@14750
   479
  "f \<in> {..Suc n} -> carrier G ==>
ballarin@13936
   480
   finprod G f {..Suc n} = (finprod G (%i. f (Suc i)) {..n} \<otimes> f 0)"
ballarin@13936
   481
proof (induct n)
ballarin@13936
   482
  case 0 thus ?case by (simp add: Pi_def)
ballarin@13936
   483
next
ballarin@13936
   484
  case Suc thus ?case by (simp add: m_assoc Pi_def)
ballarin@13936
   485
qed
ballarin@13936
   486
ballarin@27933
   487
lemma finprod_mult [simp]:
paulson@14750
   488
  "[| f \<in> {..n} -> carrier G; g \<in> {..n} -> carrier G |] ==>
ballarin@13936
   489
     finprod G (%i. f i \<otimes> g i) {..n::nat} =
ballarin@13936
   490
     finprod G f {..n} \<otimes> finprod G g {..n}"
ballarin@13936
   491
  by (induct n) (simp_all add: m_ac Pi_def)
ballarin@13936
   492
ballarin@27699
   493
(* The following two were contributed by Jeremy Avigad. *)
ballarin@27699
   494
ballarin@27933
   495
lemma finprod_reindex:
ballarin@27699
   496
  assumes fin: "finite A"
ballarin@27699
   497
    shows "f : (h ` A) \<rightarrow> carrier G \<Longrightarrow> 
ballarin@27699
   498
        inj_on h A ==> finprod G f (h ` A) = finprod G (%x. f (h x)) A"
ballarin@27699
   499
  using fin apply induct
ballarin@27699
   500
  apply (auto simp add: finprod_insert Pi_def)
ballarin@27699
   501
done
ballarin@27699
   502
ballarin@27933
   503
lemma finprod_const:
ballarin@27699
   504
  assumes fin [simp]: "finite A"
ballarin@27699
   505
      and a [simp]: "a : carrier G"
ballarin@27699
   506
    shows "finprod G (%x. a) A = a (^) card A"
ballarin@27699
   507
  using fin apply induct
ballarin@27699
   508
  apply force
ballarin@27699
   509
  apply (subst finprod_insert)
ballarin@27699
   510
  apply auto
ballarin@27699
   511
  apply (subst m_comm)
ballarin@27699
   512
  apply auto
ballarin@27699
   513
done
ballarin@27699
   514
ballarin@27933
   515
(* The following lemma was contributed by Jesus Aransay. *)
ballarin@27933
   516
ballarin@27933
   517
lemma finprod_singleton:
ballarin@27933
   518
  assumes i_in_A: "i \<in> A" and fin_A: "finite A" and f_Pi: "f \<in> A \<rightarrow> carrier G"
ballarin@27933
   519
  shows "(\<Otimes>j\<in>A. if i = j then f j else \<one>) = f i"
ballarin@29237
   520
  using i_in_A finprod_insert [of "A - {i}" i "(\<lambda>j. if i = j then f j else \<one>)"]
ballarin@29237
   521
    fin_A f_Pi finprod_one [of "A - {i}"]
ballarin@29237
   522
    finprod_cong [of "A - {i}" "A - {i}" "(\<lambda>j. if i = j then f j else \<one>)" "(\<lambda>i. \<one>)"] 
ballarin@27933
   523
  unfolding Pi_def simp_implies_def by (force simp add: insert_absorb)
ballarin@27933
   524
ballarin@13936
   525
end
ballarin@27933
   526
ballarin@27933
   527
end