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