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