src/HOL/Library/Permutations.thy
author wenzelm
Fri, 21 Apr 2017 21:36:49 +0200
changeset 65548 b7caa2b8bdbf
parent 65342 e32eb488c3a3
child 65552 f533820e7248
permissions -rw-r--r--
removed pointless document;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 39302
diff changeset
     1
(*  Title:      HOL/Library/Permutations.thy
b460124855b8 tuned headers;
wenzelm
parents: 39302
diff changeset
     2
    Author:     Amine Chaieb, University of Cambridge
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
     3
*)
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
     4
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
     5
section \<open>Permutations, both general and specifically on finite sets.\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
     6
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
     7
theory Permutations
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
     8
  imports Binomial Multiset Disjoint_Sets
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
     9
begin
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    10
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
    11
subsection \<open>Transpositions\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    12
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    13
lemma swap_id_idempotent [simp]: "Fun.swap a b id \<circ> Fun.swap a b id = id"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    14
  by (rule ext) (auto simp add: Fun.swap_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    15
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    16
lemma inv_swap_id: "inv (Fun.swap a b id) = Fun.swap a b id"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    17
  by (rule inv_unique_comp) simp_all
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    18
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    19
lemma swap_id_eq: "Fun.swap a b id x = (if x = a then b else if x = b then a else x)"
56545
8f1e7596deb7 more operations and lemmas
haftmann
parents: 54681
diff changeset
    20
  by (simp add: Fun.swap_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    21
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
    22
lemma bij_inv_eq_iff: "bij p \<Longrightarrow> x = inv p y \<longleftrightarrow> p x = y"
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
    23
  using surj_f_inv_f[of p] by (auto simp add: bij_def)
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
    24
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
    25
lemma bij_swap_comp:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    26
  assumes "bij p"
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
    27
  shows "Fun.swap a b id \<circ> p = Fun.swap (inv p a) (inv p b) p"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    28
  using surj_f_inv_f[OF bij_is_surj[OF \<open>bij p\<close>]]
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    29
  by (simp add: fun_eq_iff Fun.swap_def bij_inv_eq_iff[OF \<open>bij p\<close>])
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
    30
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    31
lemma bij_swap_compose_bij:
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    32
  assumes "bij p"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    33
  shows "bij (Fun.swap a b id \<circ> p)"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    34
  by (simp only: bij_swap_comp[OF \<open>bij p\<close>] bij_swap_iff \<open>bij p\<close>)
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
    35
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    36
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
    37
subsection \<open>Basic consequences of the definition\<close>
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    38
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    39
definition permutes  (infixr "permutes" 41)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    40
  where "(p permutes S) \<longleftrightarrow> (\<forall>x. x \<notin> S \<longrightarrow> p x = x) \<and> (\<forall>y. \<exists>!x. p x = y)"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    41
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    42
lemma permutes_in_image: "p permutes S \<Longrightarrow> p x \<in> S \<longleftrightarrow> x \<in> S"
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    43
  unfolding permutes_def by metis
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
    44
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    45
lemma permutes_not_in: "f permutes S \<Longrightarrow> x \<notin> S \<Longrightarrow> f x = x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    46
  by (auto simp: permutes_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    47
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    48
lemma permutes_image: "p permutes S \<Longrightarrow> p ` S = S"
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
    49
  unfolding permutes_def
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39198
diff changeset
    50
  apply (rule set_eqI)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    51
  apply (simp add: image_iff)
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    52
  apply metis
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    53
  done
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    54
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    55
lemma permutes_inj: "p permutes S \<Longrightarrow> inj p"
64966
d53d7ca3303e added inj_def (redundant, analogous to surj_def, bij_def);
wenzelm
parents: 64543
diff changeset
    56
  unfolding permutes_def inj_def by blast
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    57
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
    58
lemma permutes_inj_on: "f permutes S \<Longrightarrow> inj_on f A"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    59
  by (auto simp: permutes_def inj_on_def)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
    60
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    61
lemma permutes_surj: "p permutes s \<Longrightarrow> surj p"
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
    62
  unfolding permutes_def surj_def by metis
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    63
60601
6e83d94760c4 added lemma
nipkow
parents: 60500
diff changeset
    64
lemma permutes_bij: "p permutes s \<Longrightarrow> bij p"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    65
  unfolding bij_def by (metis permutes_inj permutes_surj)
60601
6e83d94760c4 added lemma
nipkow
parents: 60500
diff changeset
    66
59474
4475b1a0141d related permutations with bij functions
hoelzl
parents: 58881
diff changeset
    67
lemma permutes_imp_bij: "p permutes S \<Longrightarrow> bij_betw p S S"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    68
  by (metis UNIV_I bij_betw_subset permutes_bij permutes_image subsetI)
59669
de7792ea4090 renaming HOL/Fact.thy -> Binomial.thy
paulson <lp15@cam.ac.uk>
parents: 59474
diff changeset
    69
59474
4475b1a0141d related permutations with bij functions
hoelzl
parents: 58881
diff changeset
    70
lemma bij_imp_permutes: "bij_betw p S S \<Longrightarrow> (\<And>x. x \<notin> S \<Longrightarrow> p x = x) \<Longrightarrow> p permutes S"
4475b1a0141d related permutations with bij functions
hoelzl
parents: 58881
diff changeset
    71
  unfolding permutes_def bij_betw_def inj_on_def
4475b1a0141d related permutations with bij functions
hoelzl
parents: 58881
diff changeset
    72
  by auto (metis image_iff)+
4475b1a0141d related permutations with bij functions
hoelzl
parents: 58881
diff changeset
    73
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    74
lemma permutes_inv_o:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    75
  assumes permutes: "p permutes S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    76
  shows "p \<circ> inv p = id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    77
    and "inv p \<circ> p = id"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    78
  using permutes_inj[OF permutes] permutes_surj[OF permutes]
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    79
  unfolding inj_iff[symmetric] surj_iff[symmetric] by blast+
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    80
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
    81
lemma permutes_inverses:
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    82
  fixes p :: "'a \<Rightarrow> 'a"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    83
  assumes permutes: "p permutes S"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    84
  shows "p (inv p x) = x"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    85
    and "inv p (p x) = x"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    86
  using permutes_inv_o[OF permutes, unfolded fun_eq_iff o_def] by auto
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    87
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
    88
lemma permutes_subset: "p permutes S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> p permutes T"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    89
  unfolding permutes_def by blast
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    90
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    91
lemma permutes_empty[simp]: "p permutes {} \<longleftrightarrow> p = id"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    92
  by (auto simp add: fun_eq_iff permutes_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    93
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    94
lemma permutes_sing[simp]: "p permutes {a} \<longleftrightarrow> p = id"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    95
  by (simp add: fun_eq_iff permutes_def) metis  (*somewhat slow*)
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
    96
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    97
lemma permutes_univ: "p permutes UNIV \<longleftrightarrow> (\<forall>y. \<exists>!x. p x = y)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
    98
  by (simp add: permutes_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
    99
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   100
lemma permutes_inv_eq: "p permutes S \<Longrightarrow> inv p y = x \<longleftrightarrow> p x = y"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   101
  unfolding permutes_def inv_def
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   102
  apply auto
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   103
  apply (erule allE[where x=y])
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   104
  apply (erule allE[where x=y])
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   105
  apply (rule someI_ex)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   106
  apply blast
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   107
  apply (rule some1_equality)
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   108
  apply blast
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   109
  apply blast
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   110
  done
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   111
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   112
lemma permutes_swap_id: "a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> Fun.swap a b id permutes S"
56545
8f1e7596deb7 more operations and lemmas
haftmann
parents: 54681
diff changeset
   113
  unfolding permutes_def Fun.swap_def fun_upd_def by auto metis
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   114
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   115
lemma permutes_superset: "p permutes S \<Longrightarrow> (\<forall>x \<in> S - T. p x = x) \<Longrightarrow> p permutes T"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   116
  by (simp add: Ball_def permutes_def) metis
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   117
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   118
(* Next three lemmas contributed by Lukas Bulwahn *)
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   119
lemma permutes_bij_inv_into:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   120
  fixes A :: "'a set"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   121
    and B :: "'b set"
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   122
  assumes "p permutes A"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   123
    and "bij_betw f A B"
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   124
  shows "(\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) permutes B"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   125
proof (rule bij_imp_permutes)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   126
  from assms have "bij_betw p A A" "bij_betw f A B" "bij_betw (inv_into A f) B A"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   127
    by (auto simp add: permutes_imp_bij bij_betw_inv_into)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   128
  then have "bij_betw (f \<circ> p \<circ> inv_into A f) B B"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   129
    by (simp add: bij_betw_trans)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   130
  then show "bij_betw (\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) B B"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   131
    by (subst bij_betw_cong[where g="f \<circ> p \<circ> inv_into A f"]) auto
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   132
next
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   133
  fix x
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   134
  assume "x \<notin> B"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   135
  then show "(if x \<in> B then f (p (inv_into A f x)) else x) = x" by auto
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   136
qed
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   137
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   138
lemma permutes_image_mset:
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   139
  assumes "p permutes A"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   140
  shows "image_mset p (mset_set A) = mset_set A"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   141
  using assms by (metis image_mset_mset_set bij_betw_imp_inj_on permutes_imp_bij permutes_image)
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   142
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   143
lemma permutes_implies_image_mset_eq:
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   144
  assumes "p permutes A" "\<And>x. x \<in> A \<Longrightarrow> f x = f' (p x)"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   145
  shows "image_mset f' (mset_set A) = image_mset f (mset_set A)"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   146
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   147
  have "f x = f' (p x)" if "x \<in># mset_set A" for x
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   148
    using assms(2)[of x] that by (cases "finite A") auto
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   149
  with assms have "image_mset f (mset_set A) = image_mset (f' \<circ> p) (mset_set A)"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   150
    by (auto intro!: image_mset_cong)
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   151
  also have "\<dots> = image_mset f' (image_mset p (mset_set A))"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   152
    by (simp add: image_mset.compositionality)
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   153
  also have "\<dots> = image_mset f' (mset_set A)"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   154
  proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   155
    from assms permutes_image_mset have "image_mset p (mset_set A) = mset_set A"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   156
      by blast
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   157
    then show ?thesis by simp
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   158
  qed
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   159
  finally show ?thesis ..
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   160
qed
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   161
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   162
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   163
subsection \<open>Group properties\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   164
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   165
lemma permutes_id: "id permutes S"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   166
  by (simp add: permutes_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   167
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   168
lemma permutes_compose: "p permutes S \<Longrightarrow> q permutes S \<Longrightarrow> q \<circ> p permutes S"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   169
  unfolding permutes_def o_def by metis
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   170
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   171
lemma permutes_inv:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   172
  assumes "p permutes S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   173
  shows "inv p permutes S"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   174
  using assms unfolding permutes_def permutes_inv_eq[OF assms] by metis
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   175
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   176
lemma permutes_inv_inv:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   177
  assumes "p permutes S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   178
  shows "inv (inv p) = p"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   179
  unfolding fun_eq_iff permutes_inv_eq[OF assms] permutes_inv_eq[OF permutes_inv[OF assms]]
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   180
  by blast
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   181
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
   182
lemma permutes_invI:
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   183
  assumes perm: "p permutes S"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   184
    and inv: "\<And>x. x \<in> S \<Longrightarrow> p' (p x) = x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   185
    and outside: "\<And>x. x \<notin> S \<Longrightarrow> p' x = x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   186
  shows "inv p = p'"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   187
proof
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   188
  show "inv p x = p' x" for x
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   189
  proof (cases "x \<in> S")
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   190
    case True
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   191
    from assms have "p' x = p' (p (inv p x))"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   192
      by (simp add: permutes_inverses)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   193
    also from permutes_inv[OF perm] True have "\<dots> = inv p x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   194
      by (subst inv) (simp_all add: permutes_in_image)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   195
    finally show ?thesis ..
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   196
  next
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   197
    case False
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   198
    with permutes_inv[OF perm] show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   199
      by (simp_all add: outside permutes_not_in)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   200
  qed
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   201
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   202
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   203
lemma permutes_vimage: "f permutes A \<Longrightarrow> f -` A = A"
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
   204
  by (simp add: bij_vimage_eq_inv_image permutes_bij permutes_image[OF permutes_inv])
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   205
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   206
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   207
subsection \<open>The number of permutations on a finite set\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   208
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
   209
lemma permutes_insert_lemma:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   210
  assumes "p permutes (insert a S)"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   211
  shows "Fun.swap a (p a) id \<circ> p permutes S"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   212
  apply (rule permutes_superset[where S = "insert a S"])
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   213
  apply (rule permutes_compose[OF assms])
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   214
  apply (rule permutes_swap_id, simp)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   215
  using permutes_in_image[OF assms, of a]
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   216
  apply simp
56545
8f1e7596deb7 more operations and lemmas
haftmann
parents: 54681
diff changeset
   217
  apply (auto simp add: Ball_def Fun.swap_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   218
  done
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   219
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   220
lemma permutes_insert: "{p. p permutes (insert a S)} =
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   221
  (\<lambda>(b, p). Fun.swap a b id \<circ> p) ` {(b, p). b \<in> insert a S \<and> p \<in> {p. p permutes S}}"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   222
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   223
  have "p permutes insert a S \<longleftrightarrow>
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   224
    (\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S)" for p
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   225
  proof -
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   226
    have "\<exists>b q. p = Fun.swap a b id \<circ> q \<and> b \<in> insert a S \<and> q permutes S"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   227
      if p: "p permutes insert a S"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   228
    proof -
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   229
      let ?b = "p a"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   230
      let ?q = "Fun.swap a (p a) id \<circ> p"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   231
      have *: "p = Fun.swap a ?b id \<circ> ?q"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   232
        by (simp add: fun_eq_iff o_assoc)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   233
      have **: "?b \<in> insert a S"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   234
        unfolding permutes_in_image[OF p] by simp
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   235
      from permutes_insert_lemma[OF p] * ** show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   236
       by blast
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   237
    qed
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   238
    moreover have "p permutes insert a S"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   239
      if bq: "p = Fun.swap a b id \<circ> q" "b \<in> insert a S" "q permutes S" for b q
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   240
    proof -
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   241
      from permutes_subset[OF bq(3), of "insert a S"] have q: "q permutes insert a S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   242
        by auto
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   243
      have a: "a \<in> insert a S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   244
        by simp
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   245
      from bq(1) permutes_compose[OF q permutes_swap_id[OF a bq(2)]] show ?thesis
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   246
        by simp
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   247
    qed
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   248
    ultimately show ?thesis by blast
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   249
  qed
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   250
  then show ?thesis by auto
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   251
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   252
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   253
lemma card_permutations:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   254
  assumes "card S = n"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   255
    and "finite S"
33715
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   256
  shows "card {p. p permutes S} = fact n"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   257
  using assms(2,1)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   258
proof (induct arbitrary: n)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   259
  case empty
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   260
  then show ?case by simp
33715
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   261
next
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   262
  case (insert x F)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   263
  {
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   264
    fix n
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   265
    assume card_insert: "card (insert x F) = n"
33715
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   266
    let ?xF = "{p. p permutes insert x F}"
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   267
    let ?pF = "{p. p permutes F}"
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   268
    let ?pF' = "{(b, p). b \<in> insert x F \<and> p \<in> ?pF}"
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   269
    let ?g = "(\<lambda>(b, p). Fun.swap x b id \<circ> p)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   270
    have xfgpF': "?xF = ?g ` ?pF'"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   271
      by (rule permutes_insert[of x F])
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   272
    from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have Fs: "card F = n - 1"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   273
      by auto
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   274
    from \<open>finite F\<close> insert.hyps Fs have pFs: "card ?pF = fact (n - 1)"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   275
      by auto
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   276
    then have "finite ?pF"
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59669
diff changeset
   277
      by (auto intro: card_ge_0_finite)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   278
    with \<open>finite F\<close> card_insert have pF'f: "finite ?pF'"
61424
c3658c18b7bc prod_case as canonical name for product type eliminator
haftmann
parents: 60601
diff changeset
   279
      apply (simp only: Collect_case_prod Collect_mem_eq)
33715
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   280
      apply (rule finite_cartesian_product)
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   281
      apply simp_all
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   282
      done
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   283
33715
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   284
    have ginj: "inj_on ?g ?pF'"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   285
    proof -
33715
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   286
      {
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   287
        fix b p c q
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   288
        assume bp: "(b, p) \<in> ?pF'"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   289
        assume cq: "(c, q) \<in> ?pF'"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   290
        assume eq: "?g (b, p) = ?g (c, q)"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   291
        from bp cq have pF: "p permutes F" and qF: "q permutes F"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   292
          by auto
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   293
        from pF \<open>x \<notin> F\<close> eq have "b = ?g (b, p) x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   294
          by (auto simp: permutes_def Fun.swap_def fun_upd_def fun_eq_iff)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   295
        also from qF \<open>x \<notin> F\<close> eq have "\<dots> = ?g (c, q) x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   296
          by (auto simp: swap_def fun_upd_def fun_eq_iff)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   297
        also from qF \<open>x \<notin> F\<close> have "\<dots> = c"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   298
          by (auto simp: permutes_def Fun.swap_def fun_upd_def fun_eq_iff)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   299
        finally have "b = c" .
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   300
        then have "Fun.swap x b id = Fun.swap x c id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   301
          by simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   302
        with eq have "Fun.swap x b id \<circ> p = Fun.swap x b id \<circ> q"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   303
          by simp
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   304
        then have "Fun.swap x b id \<circ> (Fun.swap x b id \<circ> p) = Fun.swap x b id \<circ> (Fun.swap x b id \<circ> q)"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   305
          by simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   306
        then have "p = q"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   307
          by (simp add: o_assoc)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   308
        with \<open>b = c\<close> have "(b, p) = (c, q)"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   309
          by simp
33715
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   310
      }
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   311
      then show ?thesis
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   312
        unfolding inj_on_def by blast
33715
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   313
    qed
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   314
    from \<open>x \<notin> F\<close> \<open>finite F\<close> card_insert have "n \<noteq> 0"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   315
      by auto
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   316
    then have "\<exists>m. n = Suc m"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   317
      by presburger
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   318
    then obtain m where n: "n = Suc m"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   319
      by blast
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   320
    from pFs card_insert have *: "card ?xF = fact n"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   321
      unfolding xfgpF' card_image[OF ginj]
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   322
      using \<open>finite F\<close> \<open>finite ?pF\<close>
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   323
      by (simp only: Collect_case_prod Collect_mem_eq card_cartesian_product) (simp add: n)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   324
    from finite_imageI[OF pF'f, of ?g] have xFf: "finite ?xF"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   325
      by (simp add: xfgpF' n)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   326
    from * have "card ?xF = fact n"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   327
      unfolding xFf by blast
33715
8cce3a34c122 removed hassize predicate
hoelzl
parents: 33057
diff changeset
   328
  }
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   329
  with insert show ?case by simp
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   330
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   331
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   332
lemma finite_permutations:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   333
  assumes "finite S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   334
  shows "finite {p. p permutes S}"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   335
  using card_permutations[OF refl assms] by (auto intro: card_ge_0_finite)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   336
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   337
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   338
subsection \<open>Permutations of index set for iterated operations\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   339
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 49739
diff changeset
   340
lemma (in comm_monoid_set) permute:
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 49739
diff changeset
   341
  assumes "p permutes S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   342
  shows "F g S = F (g \<circ> p) S"
51489
f738e6dbd844 fundamental revision of big operators on sets
haftmann
parents: 49739
diff changeset
   343
proof -
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   344
  from \<open>p permutes S\<close> have "inj p"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   345
    by (rule permutes_inj)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   346
  then have "inj_on p S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   347
    by (auto intro: subset_inj_on)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   348
  then have "F g (p ` S) = F (g \<circ> p) S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   349
    by (rule reindex)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   350
  moreover from \<open>p permutes S\<close> have "p ` S = S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   351
    by (rule permutes_image)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   352
  ultimately show ?thesis
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   353
    by simp
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   354
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   355
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   356
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   357
subsection \<open>Various combinations of transpositions with 2, 1 and 0 common elements\<close>
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   358
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   359
lemma swap_id_common:" a \<noteq> c \<Longrightarrow> b \<noteq> c \<Longrightarrow>
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   360
  Fun.swap a b id \<circ> Fun.swap a c id = Fun.swap b c id \<circ> Fun.swap a b id"
56545
8f1e7596deb7 more operations and lemmas
haftmann
parents: 54681
diff changeset
   361
  by (simp add: fun_eq_iff Fun.swap_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   362
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   363
lemma swap_id_common': "a \<noteq> b \<Longrightarrow> a \<noteq> c \<Longrightarrow>
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   364
  Fun.swap a c id \<circ> Fun.swap b c id = Fun.swap b c id \<circ> Fun.swap a b id"
56545
8f1e7596deb7 more operations and lemmas
haftmann
parents: 54681
diff changeset
   365
  by (simp add: fun_eq_iff Fun.swap_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   366
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   367
lemma swap_id_independent: "a \<noteq> c \<Longrightarrow> a \<noteq> d \<Longrightarrow> b \<noteq> c \<Longrightarrow> b \<noteq> d \<Longrightarrow>
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   368
  Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap c d id \<circ> Fun.swap a b id"
56545
8f1e7596deb7 more operations and lemmas
haftmann
parents: 54681
diff changeset
   369
  by (simp add: fun_eq_iff Fun.swap_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   370
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   371
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   372
subsection \<open>Permutations as transposition sequences\<close>
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   373
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   374
inductive swapidseq :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> bool"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   375
  where
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   376
    id[simp]: "swapidseq 0 id"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   377
  | comp_Suc: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (Fun.swap a b id \<circ> p)"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   378
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   379
declare id[unfolded id_def, simp]
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   380
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   381
definition "permutation p \<longleftrightarrow> (\<exists>n. swapidseq n p)"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   382
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   383
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   384
subsection \<open>Some closure properties of the set of permutations, with lengths\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   385
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   386
lemma permutation_id[simp]: "permutation id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   387
  unfolding permutation_def by (rule exI[where x=0]) simp
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   388
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   389
declare permutation_id[unfolded id_def, simp]
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   390
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   391
lemma swapidseq_swap: "swapidseq (if a = b then 0 else 1) (Fun.swap a b id)"
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   392
  apply clarsimp
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   393
  using comp_Suc[of 0 id a b]
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   394
  apply simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   395
  done
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   396
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   397
lemma permutation_swap_id: "permutation (Fun.swap a b id)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   398
proof (cases "a = b")
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   399
  case True
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   400
  then show ?thesis by simp
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   401
next
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   402
  case False
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   403
  then show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   404
    unfolding permutation_def
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   405
    using swapidseq_swap[of a b] by blast
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   406
qed
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   407
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   408
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   409
lemma swapidseq_comp_add: "swapidseq n p \<Longrightarrow> swapidseq m q \<Longrightarrow> swapidseq (n + m) (p \<circ> q)"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   410
proof (induct n p arbitrary: m q rule: swapidseq.induct)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   411
  case (id m q)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   412
  then show ?case by simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   413
next
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   414
  case (comp_Suc n p a b m q)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   415
  have eq: "Suc n + m = Suc (n + m)"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   416
    by arith
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   417
  show ?case
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   418
    apply (simp only: eq comp_assoc)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   419
    apply (rule swapidseq.comp_Suc)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   420
    using comp_Suc.hyps(2)[OF comp_Suc.prems] comp_Suc.hyps(3)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   421
     apply blast+
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   422
    done
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   423
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   424
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   425
lemma permutation_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> permutation (p \<circ> q)"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   426
  unfolding permutation_def using swapidseq_comp_add[of _ p _ q] by metis
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   427
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   428
lemma swapidseq_endswap: "swapidseq n p \<Longrightarrow> a \<noteq> b \<Longrightarrow> swapidseq (Suc n) (p \<circ> Fun.swap a b id)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   429
  by (induct n p rule: swapidseq.induct)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   430
    (use swapidseq_swap[of a b] in \<open>auto simp add: comp_assoc intro: swapidseq.comp_Suc\<close>)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   431
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   432
lemma swapidseq_inverse_exists: "swapidseq n p \<Longrightarrow> \<exists>q. swapidseq n q \<and> p \<circ> q = id \<and> q \<circ> p = id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   433
proof (induct n p rule: swapidseq.induct)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   434
  case id
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   435
  then show ?case
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   436
    by (rule exI[where x=id]) simp
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
   437
next
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   438
  case (comp_Suc n p a b)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   439
  from comp_Suc.hyps obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   440
    by blast
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   441
  let ?q = "q \<circ> Fun.swap a b id"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   442
  note H = comp_Suc.hyps
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   443
  from swapidseq_swap[of a b] H(3) have *: "swapidseq 1 (Fun.swap a b id)"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   444
    by simp
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   445
  from swapidseq_comp_add[OF q(1) *] have **: "swapidseq (Suc n) ?q"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   446
    by simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   447
  have "Fun.swap a b id \<circ> p \<circ> ?q = Fun.swap a b id \<circ> (p \<circ> q) \<circ> Fun.swap a b id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   448
    by (simp add: o_assoc)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   449
  also have "\<dots> = id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   450
    by (simp add: q(2))
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   451
  finally have ***: "Fun.swap a b id \<circ> p \<circ> ?q = id" .
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   452
  have "?q \<circ> (Fun.swap a b id \<circ> p) = q \<circ> (Fun.swap a b id \<circ> Fun.swap a b id) \<circ> p"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   453
    by (simp only: o_assoc)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   454
  then have "?q \<circ> (Fun.swap a b id \<circ> p) = id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   455
    by (simp add: q(3))
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   456
  with ** *** show ?case
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   457
    by blast
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   458
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   459
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   460
lemma swapidseq_inverse:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   461
  assumes "swapidseq n p"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   462
  shows "swapidseq n (inv p)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   463
  using swapidseq_inverse_exists[OF assms] inv_unique_comp[of p] by auto
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   464
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   465
lemma permutation_inverse: "permutation p \<Longrightarrow> permutation (inv p)"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   466
  using permutation_def swapidseq_inverse by blast
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   467
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   468
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   469
subsection \<open>The identity map only has even transposition sequences\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   470
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   471
lemma symmetry_lemma:
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   472
  assumes "\<And>a b c d. P a b c d \<Longrightarrow> P a b d c"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   473
    and "\<And>a b c d. a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow>
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   474
      a = c \<and> b = d \<or> a = c \<and> b \<noteq> d \<or> a \<noteq> c \<and> b = d \<or> a \<noteq> c \<and> a \<noteq> d \<and> b \<noteq> c \<and> b \<noteq> d \<Longrightarrow>
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   475
      P a b c d"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   476
  shows "\<And>a b c d. a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>  P a b c d"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   477
  using assms by metis
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   478
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   479
lemma swap_general: "a \<noteq> b \<Longrightarrow> c \<noteq> d \<Longrightarrow>
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   480
  Fun.swap a b id \<circ> Fun.swap c d id = id \<or>
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   481
  (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   482
    Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id)"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   483
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   484
  assume neq: "a \<noteq> b" "c \<noteq> d"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   485
  have "a \<noteq> b \<longrightarrow> c \<noteq> d \<longrightarrow>
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   486
    (Fun.swap a b id \<circ> Fun.swap c d id = id \<or>
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   487
      (\<exists>x y z. x \<noteq> a \<and> y \<noteq> a \<and> z \<noteq> a \<and> x \<noteq> y \<and>
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   488
        Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id))"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   489
    apply (rule symmetry_lemma[where a=a and b=b and c=c and d=d])
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   490
     apply (simp_all only: swap_commute)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   491
    apply (case_tac "a = c \<and> b = d")
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   492
     apply (clarsimp simp only: swap_commute swap_id_idempotent)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   493
    apply (case_tac "a = c \<and> b \<noteq> d")
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   494
     apply (rule disjI2)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   495
     apply (rule_tac x="b" in exI)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   496
     apply (rule_tac x="d" in exI)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   497
     apply (rule_tac x="b" in exI)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   498
     apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   499
    apply (case_tac "a \<noteq> c \<and> b = d")
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   500
     apply (rule disjI2)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   501
     apply (rule_tac x="c" in exI)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   502
     apply (rule_tac x="d" in exI)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   503
     apply (rule_tac x="c" in exI)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   504
     apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   505
    apply (rule disjI2)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   506
    apply (rule_tac x="c" in exI)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   507
    apply (rule_tac x="d" in exI)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   508
    apply (rule_tac x="b" in exI)
56545
8f1e7596deb7 more operations and lemmas
haftmann
parents: 54681
diff changeset
   509
    apply (clarsimp simp add: fun_eq_iff Fun.swap_def)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   510
    done
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   511
  with neq show ?thesis by metis
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   512
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   513
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   514
lemma swapidseq_id_iff[simp]: "swapidseq 0 p \<longleftrightarrow> p = id"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   515
  using swapidseq.cases[of 0 p "p = id"] by auto
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   516
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   517
lemma swapidseq_cases: "swapidseq n p \<longleftrightarrow>
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   518
    n = 0 \<and> p = id \<or> (\<exists>a b q m. n = Suc m \<and> p = Fun.swap a b id \<circ> q \<and> swapidseq m q \<and> a \<noteq> b)"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   519
  apply (rule iffI)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   520
   apply (erule swapidseq.cases[of n p])
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   521
    apply simp
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   522
   apply (rule disjI2)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   523
   apply (rule_tac x= "a" in exI)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   524
   apply (rule_tac x= "b" in exI)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   525
   apply (rule_tac x= "pa" in exI)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   526
   apply (rule_tac x= "na" in exI)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   527
   apply simp
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   528
  apply auto
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   529
  apply (rule comp_Suc, simp_all)
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   530
  done
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   531
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   532
lemma fixing_swapidseq_decrease:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   533
  assumes "swapidseq n p"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   534
    and "a \<noteq> b"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   535
    and "(Fun.swap a b id \<circ> p) a = a"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   536
  shows "n \<noteq> 0 \<and> swapidseq (n - 1) (Fun.swap a b id \<circ> p)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   537
  using assms
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   538
proof (induct n arbitrary: p a b)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   539
  case 0
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   540
  then show ?case
56545
8f1e7596deb7 more operations and lemmas
haftmann
parents: 54681
diff changeset
   541
    by (auto simp add: Fun.swap_def fun_upd_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   542
next
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   543
  case (Suc n p a b)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   544
  from Suc.prems(1) swapidseq_cases[of "Suc n" p]
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   545
  obtain c d q m where
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   546
    cdqm: "Suc n = Suc m" "p = Fun.swap c d id \<circ> q" "swapidseq m q" "c \<noteq> d" "n = m"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   547
    by auto
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   548
  consider "Fun.swap a b id \<circ> Fun.swap c d id = id"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   549
    | x y z where "x \<noteq> a" "y \<noteq> a" "z \<noteq> a" "x \<noteq> y"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   550
      "Fun.swap a b id \<circ> Fun.swap c d id = Fun.swap x y id \<circ> Fun.swap a z id"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   551
    using swap_general[OF Suc.prems(2) cdqm(4)] by metis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   552
  then show ?case
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   553
  proof cases
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   554
    case 1
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   555
    then show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   556
      by (simp only: cdqm o_assoc) (simp add: cdqm)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   557
  next
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   558
    case prems: 2
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   559
    then have az: "a \<noteq> z"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   560
      by simp
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   561
    from prems have *: "(Fun.swap x y id \<circ> h) a = a \<longleftrightarrow> h a = a" for h
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   562
      by (simp add: Fun.swap_def)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   563
    from cdqm(2) have "Fun.swap a b id \<circ> p = Fun.swap a b id \<circ> (Fun.swap c d id \<circ> q)"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   564
      by simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   565
    then have "Fun.swap a b id \<circ> p = Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   566
      by (simp add: o_assoc prems)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   567
    then have "(Fun.swap a b id \<circ> p) a = (Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   568
      by simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   569
    then have "(Fun.swap x y id \<circ> (Fun.swap a z id \<circ> q)) a = a"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   570
      unfolding Suc by metis
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   571
    then have "(Fun.swap a z id \<circ> q) a = a"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   572
      by (simp only: *)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   573
    from Suc.hyps[OF cdqm(3)[ unfolded cdqm(5)[symmetric]] az this]
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   574
    have **: "swapidseq (n - 1) (Fun.swap a z id \<circ> q)" "n \<noteq> 0"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   575
      by blast+
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   576
    from \<open>n \<noteq> 0\<close> have ***: "Suc n - 1 = Suc (n - 1)"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   577
      by auto
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   578
    show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   579
      apply (simp only: cdqm(2) prems o_assoc ***)
49739
13aa6d8268ec consolidated names of theorems on composition;
haftmann
parents: 45922
diff changeset
   580
      apply (simp only: Suc_not_Zero simp_thms comp_assoc)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   581
      apply (rule comp_Suc)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   582
      using ** prems
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   583
       apply blast+
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   584
      done
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   585
  qed
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   586
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   587
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
   588
lemma swapidseq_identity_even:
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   589
  assumes "swapidseq n (id :: 'a \<Rightarrow> 'a)"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   590
  shows "even n"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   591
  using \<open>swapidseq n id\<close>
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   592
proof (induct n rule: nat_less_induct)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   593
  case H: (1 n)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   594
  consider "n = 0"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   595
    | a b :: 'a and q m where "n = Suc m" "id = Fun.swap a b id \<circ> q" "swapidseq m q" "a \<noteq> b"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   596
    using H(2)[unfolded swapidseq_cases[of n id]] by auto
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   597
  then show ?case
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   598
  proof cases
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   599
    case 1
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   600
    then show ?thesis by presburger
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   601
  next
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   602
    case h: 2
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   603
    from fixing_swapidseq_decrease[OF h(3,4), unfolded h(2)[symmetric]]
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   604
    have m: "m \<noteq> 0" "swapidseq (m - 1) (id :: 'a \<Rightarrow> 'a)"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   605
      by auto
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   606
    from h m have mn: "m - 1 < n"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   607
      by arith
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   608
    from H(1)[rule_format, OF mn m(2)] h(1) m(1) show ?thesis
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   609
      by presburger
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   610
  qed
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   611
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   612
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   613
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   614
subsection \<open>Therefore we have a welldefined notion of parity\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   615
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   616
definition "evenperm p = even (SOME n. swapidseq n p)"
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   617
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   618
lemma swapidseq_even_even:
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   619
  assumes m: "swapidseq m p"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   620
    and n: "swapidseq n p"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   621
  shows "even m \<longleftrightarrow> even n"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   622
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   623
  from swapidseq_inverse_exists[OF n] obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   624
    by blast
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   625
  from swapidseq_identity_even[OF swapidseq_comp_add[OF m q(1), unfolded q]] show ?thesis
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   626
    by arith
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   627
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   628
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   629
lemma evenperm_unique:
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   630
  assumes p: "swapidseq n p"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   631
    and n:"even n = b"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   632
  shows "evenperm p = b"
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   633
  unfolding n[symmetric] evenperm_def
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   634
  apply (rule swapidseq_even_even[where p = p])
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   635
   apply (rule someI[where x = n])
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   636
  using p
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   637
   apply blast+
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   638
  done
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   639
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   640
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   641
subsection \<open>And it has the expected composition properties\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   642
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   643
lemma evenperm_id[simp]: "evenperm id = True"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   644
  by (rule evenperm_unique[where n = 0]) simp_all
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   645
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   646
lemma evenperm_swap: "evenperm (Fun.swap a b id) = (a = b)"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   647
  by (rule evenperm_unique[where n="if a = b then 0 else 1"]) (simp_all add: swapidseq_swap)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   648
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
   649
lemma evenperm_comp:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   650
  assumes "permutation p" "permutation q"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   651
  shows "evenperm (p \<circ> q) \<longleftrightarrow> evenperm p = evenperm q"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   652
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   653
  from assms obtain n m where n: "swapidseq n p" and m: "swapidseq m q"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   654
    unfolding permutation_def by blast
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   655
  have "even (n + m) \<longleftrightarrow> (even n \<longleftrightarrow> even m)"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   656
    by arith
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   657
  from evenperm_unique[OF n refl] evenperm_unique[OF m refl]
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   658
    and evenperm_unique[OF swapidseq_comp_add[OF n m] this] show ?thesis
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   659
    by blast
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   660
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   661
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   662
lemma evenperm_inv:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   663
  assumes "permutation p"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   664
  shows "evenperm (inv p) = evenperm p"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   665
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   666
  from assms obtain n where n: "swapidseq n p"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   667
    unfolding permutation_def by blast
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   668
  show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   669
    by (rule evenperm_unique[OF swapidseq_inverse[OF n] evenperm_unique[OF n refl, symmetric]])
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   670
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   671
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   672
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   673
subsection \<open>A more abstract characterization of permutations\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   674
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   675
lemma bij_iff: "bij f \<longleftrightarrow> (\<forall>x. \<exists>!y. f y = x)"
64966
d53d7ca3303e added inj_def (redundant, analogous to surj_def, bij_def);
wenzelm
parents: 64543
diff changeset
   676
  unfolding bij_def inj_def surj_def
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   677
  apply auto
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   678
   apply metis
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   679
  apply metis
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   680
  done
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   681
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
   682
lemma permutation_bijective:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   683
  assumes "permutation p"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   684
  shows "bij p"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   685
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   686
  from assms obtain n where n: "swapidseq n p"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   687
    unfolding permutation_def by blast
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   688
  from swapidseq_inverse_exists[OF n] obtain q where q: "swapidseq n q" "p \<circ> q = id" "q \<circ> p = id"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   689
    by blast
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   690
  then show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   691
    unfolding bij_iff
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   692
    apply (auto simp add: fun_eq_iff)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   693
    apply metis
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   694
    done
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
   695
qed
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   696
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   697
lemma permutation_finite_support:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   698
  assumes "permutation p"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   699
  shows "finite {x. p x \<noteq> x}"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   700
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   701
  from assms obtain n where "swapidseq n p"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   702
    unfolding permutation_def by blast
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   703
  then show ?thesis
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   704
  proof (induct n p rule: swapidseq.induct)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   705
    case id
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   706
    then show ?case by simp
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   707
  next
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   708
    case (comp_Suc n p a b)
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   709
    let ?S = "insert a (insert b {x. p x \<noteq> x})"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   710
    from comp_Suc.hyps(2) have *: "finite ?S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   711
      by simp
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   712
    from \<open>a \<noteq> b\<close> have **: "{x. (Fun.swap a b id \<circ> p) x \<noteq> x} \<subseteq> ?S"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   713
      by (auto simp: Fun.swap_def)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   714
    show ?case
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   715
      by (rule finite_subset[OF ** *])
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   716
  qed
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   717
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   718
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
   719
lemma permutation_lemma:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   720
  assumes "finite S"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   721
    and "bij p"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   722
    and "\<forall>x. x\<notin> S \<longrightarrow> p x = x"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   723
  shows "permutation p"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   724
  using assms
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   725
proof (induct S arbitrary: p rule: finite_induct)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   726
  case empty
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   727
  then show ?case
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   728
    by simp
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   729
next
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   730
  case (insert a F p)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   731
  let ?r = "Fun.swap a (p a) id \<circ> p"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   732
  let ?q = "Fun.swap a (p a) id \<circ> ?r"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   733
  have *: "?r a = a"
56545
8f1e7596deb7 more operations and lemmas
haftmann
parents: 54681
diff changeset
   734
    by (simp add: Fun.swap_def)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   735
  from insert * have **: "\<forall>x. x \<notin> F \<longrightarrow> ?r x = x"
64966
d53d7ca3303e added inj_def (redundant, analogous to surj_def, bij_def);
wenzelm
parents: 64543
diff changeset
   736
    by (metis bij_pointE comp_apply id_apply insert_iff swap_apply(3))
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   737
  have "bij ?r"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   738
    by (rule bij_swap_compose_bij[OF insert(4)])
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   739
  have "permutation ?r"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   740
    by (rule insert(3)[OF \<open>bij ?r\<close> **])
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   741
  then have "permutation ?q"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   742
    by (simp add: permutation_compose permutation_swap_id)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   743
  then show ?case
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   744
    by (simp add: o_assoc)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   745
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   746
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
   747
lemma permutation: "permutation p \<longleftrightarrow> bij p \<and> finite {x. p x \<noteq> x}"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   748
  (is "?lhs \<longleftrightarrow> ?b \<and> ?f")
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   749
proof
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   750
  assume ?lhs
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   751
  with permutation_bijective permutation_finite_support show "?b \<and> ?f"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   752
    by auto
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   753
next
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   754
  assume "?b \<and> ?f"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   755
  then have "?f" "?b" by blast+
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   756
  from permutation_lemma[OF this] show ?lhs
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   757
    by blast
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   758
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   759
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   760
lemma permutation_inverse_works:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   761
  assumes "permutation p"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   762
  shows "inv p \<circ> p = id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   763
    and "p \<circ> inv p = id"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   764
  using permutation_bijective [OF assms] by (auto simp: bij_def inj_iff surj_iff)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   765
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   766
lemma permutation_inverse_compose:
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   767
  assumes p: "permutation p"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   768
    and q: "permutation q"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   769
  shows "inv (p \<circ> q) = inv q \<circ> inv p"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   770
proof -
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   771
  note ps = permutation_inverse_works[OF p]
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   772
  note qs = permutation_inverse_works[OF q]
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   773
  have "p \<circ> q \<circ> (inv q \<circ> inv p) = p \<circ> (q \<circ> inv q) \<circ> inv p"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   774
    by (simp add: o_assoc)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   775
  also have "\<dots> = id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   776
    by (simp add: ps qs)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   777
  finally have *: "p \<circ> q \<circ> (inv q \<circ> inv p) = id" .
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   778
  have "inv q \<circ> inv p \<circ> (p \<circ> q) = inv q \<circ> (inv p \<circ> p) \<circ> q"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   779
    by (simp add: o_assoc)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   780
  also have "\<dots> = id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   781
    by (simp add: ps qs)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   782
  finally have **: "inv q \<circ> inv p \<circ> (p \<circ> q) = id" .
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   783
  show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   784
    by (rule inv_unique_comp[OF * **])
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   785
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   786
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   787
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   788
subsection \<open>Relation to \<open>permutes\<close>\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   789
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   790
lemma permutation_permutes: "permutation p \<longleftrightarrow> (\<exists>S. finite S \<and> p permutes S)"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   791
  unfolding permutation permutes_def bij_iff[symmetric]
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   792
  apply (rule iffI, clarify)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   793
   apply (rule exI[where x="{x. p x \<noteq> x}"])
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   794
   apply simp
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   795
  apply clarsimp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   796
  apply (rule_tac B="S" in finite_subset)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   797
   apply auto
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   798
  done
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   799
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   800
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   801
subsection \<open>Hence a sort of induction principle composing by swaps\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   802
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   803
lemma permutes_induct: "finite S \<Longrightarrow> P id \<Longrightarrow>
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   804
  (\<And>a b p. a \<in> S \<Longrightarrow> b \<in> S \<Longrightarrow> P p \<Longrightarrow> P p \<Longrightarrow> permutation p \<Longrightarrow> P (Fun.swap a b id \<circ> p)) \<Longrightarrow>
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   805
  (\<And>p. p permutes S \<Longrightarrow> P p)"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   806
proof (induct S rule: finite_induct)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   807
  case empty
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   808
  then show ?case by auto
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
   809
next
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   810
  case (insert x F p)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   811
  let ?r = "Fun.swap x (p x) id \<circ> p"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   812
  let ?q = "Fun.swap x (p x) id \<circ> ?r"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   813
  have qp: "?q = p"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   814
    by (simp add: o_assoc)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   815
  from permutes_insert_lemma[OF insert.prems(3)] insert have Pr: "P ?r"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   816
    by blast
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
   817
  from permutes_in_image[OF insert.prems(3), of x]
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   818
  have pxF: "p x \<in> insert x F"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   819
    by simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   820
  have xF: "x \<in> insert x F"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   821
    by simp
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   822
  have rp: "permutation ?r"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   823
    unfolding permutation_permutes
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   824
    using insert.hyps(1) permutes_insert_lemma[OF insert.prems(3)]
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   825
    by blast
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   826
  from insert.prems(2)[OF xF pxF Pr Pr rp] qp show ?case
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   827
    by (simp only:)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   828
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   829
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   830
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   831
subsection \<open>Sign of a permutation as a real number\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   832
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   833
definition "sign p = (if evenperm p then (1::int) else -1)"
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   834
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   835
lemma sign_nz: "sign p \<noteq> 0"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   836
  by (simp add: sign_def)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   837
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   838
lemma sign_id: "sign id = 1"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   839
  by (simp add: sign_def)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   840
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   841
lemma sign_inverse: "permutation p \<Longrightarrow> sign (inv p) = sign p"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   842
  by (simp add: sign_def evenperm_inv)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   843
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   844
lemma sign_compose: "permutation p \<Longrightarrow> permutation q \<Longrightarrow> sign (p \<circ> q) = sign p * sign q"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   845
  by (simp add: sign_def evenperm_comp)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   846
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   847
lemma sign_swap_id: "sign (Fun.swap a b id) = (if a = b then 1 else -1)"
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   848
  by (simp add: sign_def evenperm_swap)
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   849
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   850
lemma sign_idempotent: "sign p * sign p = 1"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   851
  by (simp add: sign_def)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   852
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
   853
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   854
subsection \<open>Permuting a list\<close>
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   855
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   856
text \<open>This function permutes a list by applying a permutation to the indices.\<close>
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   857
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   858
definition permute_list :: "(nat \<Rightarrow> nat) \<Rightarrow> 'a list \<Rightarrow> 'a list"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   859
  where "permute_list f xs = map (\<lambda>i. xs ! (f i)) [0..<length xs]"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   860
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
   861
lemma permute_list_map:
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   862
  assumes "f permutes {..<length xs}"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   863
  shows "permute_list f (map g xs) = map g (permute_list f xs)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   864
  using permutes_in_image[OF assms] by (auto simp: permute_list_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   865
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   866
lemma permute_list_nth:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   867
  assumes "f permutes {..<length xs}" "i < length xs"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   868
  shows "permute_list f xs ! i = xs ! f i"
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
   869
  using permutes_in_image[OF assms(1)] assms(2)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   870
  by (simp add: permute_list_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   871
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   872
lemma permute_list_Nil [simp]: "permute_list f [] = []"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   873
  by (simp add: permute_list_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   874
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   875
lemma length_permute_list [simp]: "length (permute_list f xs) = length xs"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   876
  by (simp add: permute_list_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   877
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
   878
lemma permute_list_compose:
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   879
  assumes "g permutes {..<length xs}"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   880
  shows "permute_list (f \<circ> g) xs = permute_list g (permute_list f xs)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   881
  using assms[THEN permutes_in_image] by (auto simp add: permute_list_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   882
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   883
lemma permute_list_ident [simp]: "permute_list (\<lambda>x. x) xs = xs"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   884
  by (simp add: permute_list_def map_nth)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   885
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   886
lemma permute_list_id [simp]: "permute_list id xs = xs"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   887
  by (simp add: id_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   888
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   889
lemma mset_permute_list [simp]:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   890
  fixes xs :: "'a list"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   891
  assumes "f permutes {..<length xs}"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   892
  shows "mset (permute_list f xs) = mset xs"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   893
proof (rule multiset_eqI)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   894
  fix y :: 'a
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   895
  from assms have [simp]: "f x < length xs \<longleftrightarrow> x < length xs" for x
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   896
    using permutes_in_image[OF assms] by auto
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   897
  have "count (mset (permute_list f xs)) y = card ((\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs})"
64543
6b13586ef1a2 remove typo in bij_swap_compose_bij theorem name; tune proof
bulwahn
parents: 64284
diff changeset
   898
    by (simp add: permute_list_def count_image_mset atLeast0LessThan)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   899
  also have "(\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs} = f -` {i. i < length xs \<and> y = xs ! i}"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   900
    by auto
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   901
  also from assms have "card \<dots> = card {i. i < length xs \<and> y = xs ! i}"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   902
    by (intro card_vimage_inj) (auto simp: permutes_inj permutes_surj)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   903
  also have "\<dots> = count (mset xs) y"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   904
    by (simp add: count_mset length_filter_conv_card)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   905
  finally show "count (mset (permute_list f xs)) y = count (mset xs) y"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   906
    by simp
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   907
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   908
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
   909
lemma set_permute_list [simp]:
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   910
  assumes "f permutes {..<length xs}"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   911
  shows "set (permute_list f xs) = set xs"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   912
  by (rule mset_eq_setD[OF mset_permute_list]) fact
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   913
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   914
lemma distinct_permute_list [simp]:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   915
  assumes "f permutes {..<length xs}"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   916
  shows "distinct (permute_list f xs) = distinct xs"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   917
  by (simp add: distinct_count_atmost_1 assms)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   918
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
   919
lemma permute_list_zip:
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   920
  assumes "f permutes A" "A = {..<length xs}"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   921
  assumes [simp]: "length xs = length ys"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   922
  shows "permute_list f (zip xs ys) = zip (permute_list f xs) (permute_list f ys)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   923
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   924
  from permutes_in_image[OF assms(1)] assms(2) have *: "f i < length ys \<longleftrightarrow> i < length ys" for i
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   925
    by simp
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   926
  have "permute_list f (zip xs ys) = map (\<lambda>i. zip xs ys ! f i) [0..<length ys]"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   927
    by (simp_all add: permute_list_def zip_map_map)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   928
  also have "\<dots> = map (\<lambda>(x, y). (xs ! f x, ys ! f y)) (zip [0..<length ys] [0..<length ys])"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   929
    by (intro nth_equalityI) (simp_all add: *)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   930
  also have "\<dots> = zip (permute_list f xs) (permute_list f ys)"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   931
    by (simp_all add: permute_list_def zip_map_map)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   932
  finally show ?thesis .
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   933
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   934
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
   935
lemma map_of_permute:
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   936
  assumes "\<sigma> permutes fst ` set xs"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   937
  shows "map_of xs \<circ> \<sigma> = map_of (map (\<lambda>(x,y). (inv \<sigma> x, y)) xs)"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   938
    (is "_ = map_of (map ?f _)")
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   939
proof
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   940
  from assms have "inj \<sigma>" "surj \<sigma>"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   941
    by (simp_all add: permutes_inj permutes_surj)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   942
  then show "(map_of xs \<circ> \<sigma>) x = map_of (map ?f xs) x" for x
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   943
    by (induct xs) (auto simp: inv_f_f surj_f_inv_f)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   944
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   945
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
   946
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   947
subsection \<open>More lemmas about permutations\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
   948
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   949
text \<open>The following few lemmas were contributed by Lukas Bulwahn.\<close>
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   950
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   951
lemma count_image_mset_eq_card_vimage:
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   952
  assumes "finite A"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   953
  shows "count (image_mset f (mset_set A)) b = card {a \<in> A. f a = b}"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   954
  using assms
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   955
proof (induct A)
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   956
  case empty
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   957
  show ?case by simp
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   958
next
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   959
  case (insert x F)
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   960
  show ?case
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   961
  proof (cases "f x = b")
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   962
    case True
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   963
    with insert.hyps
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   964
    have "count (image_mset f (mset_set (insert x F))) b = Suc (card {a \<in> F. f a = f x})"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   965
      by auto
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   966
    also from insert.hyps(1,2) have "\<dots> = card (insert x {a \<in> F. f a = f x})"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   967
      by simp
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   968
    also from \<open>f x = b\<close> have "card (insert x {a \<in> F. f a = f x}) = card {a \<in> insert x F. f a = b}"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   969
      by (auto intro: arg_cong[where f="card"])
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   970
    finally show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   971
      using insert by auto
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   972
  next
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   973
    case False
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   974
    then have "{a \<in> F. f a = b} = {a \<in> insert x F. f a = b}"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   975
      by auto
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   976
    with insert False show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   977
      by simp
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   978
  qed
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   979
qed
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
   980
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   981
(* Prove image_mset_eq_implies_permutes *)
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   982
lemma image_mset_eq_implies_permutes:
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   983
  fixes f :: "'a \<Rightarrow> 'b"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   984
  assumes "finite A"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   985
    and mset_eq: "image_mset f (mset_set A) = image_mset f' (mset_set A)"
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   986
  obtains p where "p permutes A" and "\<forall>x\<in>A. f x = f' (p x)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
   987
proof -
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   988
  from \<open>finite A\<close> have [simp]: "finite {a \<in> A. f a = (b::'b)}" for f b by auto
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   989
  have "f ` A = f' ` A"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   990
  proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   991
    from \<open>finite A\<close> have "f ` A = f ` (set_mset (mset_set A))"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   992
      by simp
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   993
    also have "\<dots> = f' ` set_mset (mset_set A)"
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   994
      by (metis mset_eq multiset.set_map)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   995
    also from \<open>finite A\<close> have "\<dots> = f' ` A"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
   996
      by simp
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   997
    finally show ?thesis .
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   998
  qed
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
   999
  have "\<forall>b\<in>(f ` A). \<exists>p. bij_betw p {a \<in> A. f a = b} {a \<in> A. f' a = b}"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1000
  proof
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1001
    fix b
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1002
    from mset_eq have "count (image_mset f (mset_set A)) b = count (image_mset f' (mset_set A)) b"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1003
      by simp
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1004
    with \<open>finite A\<close> have "card {a \<in> A. f a = b} = card {a \<in> A. f' a = b}"
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1005
      by (simp add: count_image_mset_eq_card_vimage)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1006
    then show "\<exists>p. bij_betw p {a\<in>A. f a = b} {a \<in> A. f' a = b}"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1007
      by (intro finite_same_card_bij) simp_all
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1008
  qed
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1009
  then have "\<exists>p. \<forall>b\<in>f ` A. bij_betw (p b) {a \<in> A. f a = b} {a \<in> A. f' a = b}"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1010
    by (rule bchoice)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1011
  then obtain p where p: "\<forall>b\<in>f ` A. bij_betw (p b) {a \<in> A. f a = b} {a \<in> A. f' a = b}" ..
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1012
  define p' where "p' = (\<lambda>a. if a \<in> A then p (f a) a else a)"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1013
  have "p' permutes A"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1014
  proof (rule bij_imp_permutes)
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1015
    have "disjoint_family_on (\<lambda>i. {a \<in> A. f' a = i}) (f ` A)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1016
      by (auto simp: disjoint_family_on_def)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1017
    moreover
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1018
    have "bij_betw (\<lambda>a. p (f a) a) {a \<in> A. f a = b} {a \<in> A. f' a = b}" if "b \<in> f ` A" for b
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1019
      using p that by (subst bij_betw_cong[where g="p b"]) auto
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1020
    ultimately
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1021
    have "bij_betw (\<lambda>a. p (f a) a) (\<Union>b\<in>f ` A. {a \<in> A. f a = b}) (\<Union>b\<in>f ` A. {a \<in> A. f' a = b})"
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1022
      by (rule bij_betw_UNION_disjoint)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1023
    moreover have "(\<Union>b\<in>f ` A. {a \<in> A. f a = b}) = A"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1024
      by auto
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1025
    moreover from \<open>f ` A = f' ` A\<close> have "(\<Union>b\<in>f ` A. {a \<in> A. f' a = b}) = A"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1026
      by auto
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1027
    ultimately show "bij_betw p' A A"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1028
      unfolding p'_def by (subst bij_betw_cong[where g="(\<lambda>a. p (f a) a)"]) auto
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1029
  next
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1030
    show "\<And>x. x \<notin> A \<Longrightarrow> p' x = x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1031
      by (simp add: p'_def)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1032
  qed
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1033
  moreover from p have "\<forall>x\<in>A. f x = f' (p' x)"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1034
    unfolding p'_def using bij_betwE by fastforce
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1035
  ultimately show ?thesis ..
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1036
qed
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1037
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1038
lemma mset_set_upto_eq_mset_upto: "mset_set {..<n} = mset [0..<n]"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1039
  by (induct n) (auto simp: add.commute lessThan_Suc)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1040
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1041
(* and derive the existing property: *)
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1042
lemma mset_eq_permutation:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1043
  fixes xs ys :: "'a list"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1044
  assumes mset_eq: "mset xs = mset ys"
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1045
  obtains p where "p permutes {..<length ys}" "permute_list p ys = xs"
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1046
proof -
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1047
  from mset_eq have length_eq: "length xs = length ys"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1048
    by (rule mset_eq_length)
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1049
  have "mset_set {..<length ys} = mset [0..<length ys]"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1050
    by (rule mset_set_upto_eq_mset_upto)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1051
  with mset_eq length_eq have "image_mset (\<lambda>i. xs ! i) (mset_set {..<length ys}) =
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1052
    image_mset (\<lambda>i. ys ! i) (mset_set {..<length ys})"
63921
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1053
    by (metis map_nth mset_map)
0a5184877cb7 Additions to permutations (contributed by Lukas Bulwahn)
eberlm <eberlm@in.tum.de>
parents: 63539
diff changeset
  1054
  from image_mset_eq_implies_permutes[OF _ this]
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1055
  obtain p where p: "p permutes {..<length ys}" and "\<forall>i\<in>{..<length ys}. xs ! i = ys ! (p i)"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1056
    by auto
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1057
  with length_eq have "permute_list p ys = xs"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1058
    by (auto intro!: nth_equalityI simp: permute_list_nth)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1059
  with p show thesis ..
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1060
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1061
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1062
lemma permutes_natset_le:
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1063
  fixes S :: "'a::wellorder set"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1064
  assumes "p permutes S"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1065
    and "\<forall>i \<in> S. p i \<le> i"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1066
  shows "p = id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1067
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1068
  have "p n = n" for n
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1069
    using assms
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1070
  proof (induct n arbitrary: S rule: less_induct)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1071
    case (less n)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1072
    show ?case
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1073
    proof (cases "n \<in> S")
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1074
      case False
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1075
      with less(2) show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1076
        unfolding permutes_def by metis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1077
    next
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1078
      case True
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1079
      with less(3) have "p n < n \<or> p n = n"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1080
        by auto
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1081
      then show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1082
      proof
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1083
        assume "p n < n"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1084
        with less have "p (p n) = p n"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1085
          by metis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1086
        with permutes_inj[OF less(2)] have "p n = n"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1087
          unfolding inj_def by blast
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1088
        with \<open>p n < n\<close> have False
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1089
          by simp
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1090
        then show ?thesis ..
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1091
      qed
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1092
    qed
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1093
  qed
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1094
  then show ?thesis by (auto simp: fun_eq_iff)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1095
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1096
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1097
lemma permutes_natset_ge:
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1098
  fixes S :: "'a::wellorder set"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1099
  assumes p: "p permutes S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1100
    and le: "\<forall>i \<in> S. p i \<ge> i"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1101
  shows "p = id"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1102
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1103
  have "i \<ge> inv p i" if "i \<in> S" for i
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1104
  proof -
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1105
    from that permutes_in_image[OF permutes_inv[OF p]] have "inv p i \<in> S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1106
      by simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1107
    with le have "p (inv p i) \<ge> inv p i"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1108
      by blast
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1109
    with permutes_inverses[OF p] show ?thesis
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1110
      by simp
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1111
  qed
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1112
  then have "\<forall>i\<in>S. inv p i \<le> i"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1113
    by blast
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1114
  from permutes_natset_le[OF permutes_inv[OF p] this] have "inv p = inv id"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1115
    by simp
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
  1116
  then show ?thesis
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1117
    apply (subst permutes_inv_inv[OF p, symmetric])
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1118
    apply (rule inv_unique_comp)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1119
     apply simp_all
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1120
    done
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1121
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1122
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1123
lemma image_inverse_permutations: "{inv p |p. p permutes S} = {p. p permutes S}"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1124
  apply (rule set_eqI)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1125
  apply auto
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1126
  using permutes_inv_inv permutes_inv
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1127
   apply auto
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1128
  apply (rule_tac x="inv x" in exI)
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1129
  apply auto
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1130
  done
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1131
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
  1132
lemma image_compose_permutations_left:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1133
  assumes "q permutes S"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1134
  shows "{q \<circ> p |p. p permutes S} = {p. p permutes S}"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1135
  apply (rule set_eqI)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1136
  apply auto
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1137
   apply (rule permutes_compose)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1138
  using assms
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1139
    apply auto
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1140
  apply (rule_tac x = "inv q \<circ> x" in exI)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1141
  apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1142
  done
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1143
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1144
lemma image_compose_permutations_right:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1145
  assumes "q permutes S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1146
  shows "{p \<circ> q | p. p permutes S} = {p . p permutes S}"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1147
  apply (rule set_eqI)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1148
  apply auto
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1149
   apply (rule permutes_compose)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1150
  using assms
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1151
    apply auto
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1152
  apply (rule_tac x = "x \<circ> inv q" in exI)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1153
  apply (simp add: o_assoc permutes_inv permutes_compose permutes_inv_o comp_assoc)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1154
  done
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1155
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1156
lemma permutes_in_seg: "p permutes {1 ..n} \<Longrightarrow> i \<in> {1..n} \<Longrightarrow> 1 \<le> p i \<and> p i \<le> n"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1157
  by (simp add: permutes_def) metis
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1158
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1159
lemma sum_permutations_inverse: "sum f {p. p permutes S} = sum (\<lambda>p. f(inv p)) {p. p permutes S}"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1160
  (is "?lhs = ?rhs")
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1161
proof -
30036
3a074e3a9a18 generalize some lemmas
huffman
parents: 29840
diff changeset
  1162
  let ?S = "{p . p permutes S}"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1163
  have *: "inj_on inv ?S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1164
  proof (auto simp add: inj_on_def)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1165
    fix q r
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1166
    assume q: "q permutes S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1167
      and r: "r permutes S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1168
      and qr: "inv q = inv r"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1169
    then have "inv (inv q) = inv (inv r)"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1170
      by simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1171
    with permutes_inv_inv[OF q] permutes_inv_inv[OF r] show "q = r"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1172
      by metis
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1173
  qed
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1174
  have **: "inv ` ?S = ?S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1175
    using image_inverse_permutations by blast
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1176
  have ***: "?rhs = sum (f \<circ> inv) ?S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1177
    by (simp add: o_def)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1178
  from sum.reindex[OF *, of f] show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1179
    by (simp only: ** ***)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1180
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1181
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1182
lemma setum_permutations_compose_left:
30036
3a074e3a9a18 generalize some lemmas
huffman
parents: 29840
diff changeset
  1183
  assumes q: "q permutes S"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63921
diff changeset
  1184
  shows "sum f {p. p permutes S} = sum (\<lambda>p. f(q \<circ> p)) {p. p permutes S}"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1185
  (is "?lhs = ?rhs")
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1186
proof -
30036
3a074e3a9a18 generalize some lemmas
huffman
parents: 29840
diff changeset
  1187
  let ?S = "{p. p permutes S}"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1188
  have *: "?rhs = sum (f \<circ> (op \<circ> q)) ?S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1189
    by (simp add: o_def)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1190
  have **: "inj_on (op \<circ> q) ?S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1191
  proof (auto simp add: inj_on_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1192
    fix p r
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1193
    assume "p permutes S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1194
      and r: "r permutes S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1195
      and rp: "q \<circ> p = q \<circ> r"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1196
    then have "inv q \<circ> q \<circ> p = inv q \<circ> q \<circ> r"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1197
      by (simp add: comp_assoc)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1198
    with permutes_inj[OF q, unfolded inj_iff] show "p = r"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1199
      by simp
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1200
  qed
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1201
  have "(op \<circ> q) ` ?S = ?S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1202
    using image_compose_permutations_left[OF q] by auto
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1203
  with * sum.reindex[OF **, of f] show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1204
    by (simp only:)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1205
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1206
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1207
lemma sum_permutations_compose_right:
30036
3a074e3a9a18 generalize some lemmas
huffman
parents: 29840
diff changeset
  1208
  assumes q: "q permutes S"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63921
diff changeset
  1209
  shows "sum f {p. p permutes S} = sum (\<lambda>p. f(p \<circ> q)) {p. p permutes S}"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1210
  (is "?lhs = ?rhs")
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1211
proof -
30036
3a074e3a9a18 generalize some lemmas
huffman
parents: 29840
diff changeset
  1212
  let ?S = "{p. p permutes S}"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1213
  have *: "?rhs = sum (f \<circ> (\<lambda>p. p \<circ> q)) ?S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1214
    by (simp add: o_def)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1215
  have **: "inj_on (\<lambda>p. p \<circ> q) ?S"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1216
  proof (auto simp add: inj_on_def)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1217
    fix p r
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1218
    assume "p permutes S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1219
      and r: "r permutes S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1220
      and rp: "p \<circ> q = r \<circ> q"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1221
    then have "p \<circ> (q \<circ> inv q) = r \<circ> (q \<circ> inv q)"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1222
      by (simp add: o_assoc)
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1223
    with permutes_surj[OF q, unfolded surj_iff] show "p = r"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1224
      by simp
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1225
  qed
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1226
  from image_compose_permutations_right[OF q] have "(\<lambda>p. p \<circ> q) ` ?S = ?S"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1227
    by auto
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1228
  with * sum.reindex[OF **, of f] show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1229
    by (simp only:)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1230
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1231
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1232
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
  1233
subsection \<open>Sum over a set of permutations (could generalize to iteration)\<close>
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1234
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63921
diff changeset
  1235
lemma sum_over_permutations_insert:
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1236
  assumes fS: "finite S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1237
    and aS: "a \<notin> S"
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63921
diff changeset
  1238
  shows "sum f {p. p permutes (insert a S)} =
b9a1486e79be setsum -> sum
nipkow
parents: 63921
diff changeset
  1239
    sum (\<lambda>b. sum (\<lambda>q. f (Fun.swap a b id \<circ> q)) {p. p permutes S}) (insert a S)"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1240
proof -
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1241
  have *: "\<And>f a b. (\<lambda>(b, p). f (Fun.swap a b id \<circ> p)) = f \<circ> (\<lambda>(b,p). Fun.swap a b id \<circ> p)"
39302
d7728f65b353 renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents: 39198
diff changeset
  1242
    by (simp add: fun_eq_iff)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1243
  have **: "\<And>P Q. {(a, b). a \<in> P \<and> b \<in> Q} = P \<times> Q"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1244
    by blast
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
  1245
  show ?thesis
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1246
    unfolding * ** sum.cartesian_product permutes_insert
64267
b9a1486e79be setsum -> sum
nipkow
parents: 63921
diff changeset
  1247
  proof (rule sum.reindex)
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1248
    let ?f = "(\<lambda>(b, y). Fun.swap a b id \<circ> y)"
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1249
    let ?P = "{p. p permutes S}"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1250
    {
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1251
      fix b c p q
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1252
      assume b: "b \<in> insert a S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1253
      assume c: "c \<in> insert a S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1254
      assume p: "p permutes S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1255
      assume q: "q permutes S"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1256
      assume eq: "Fun.swap a b id \<circ> p = Fun.swap a c id \<circ> q"
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1257
      from p q aS have pa: "p a = a" and qa: "q a = a"
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32456
diff changeset
  1258
        unfolding permutes_def by metis+
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1259
      from eq have "(Fun.swap a b id \<circ> p) a  = (Fun.swap a c id \<circ> q) a"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1260
        by simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1261
      then have bc: "b = c"
56545
8f1e7596deb7 more operations and lemmas
haftmann
parents: 54681
diff changeset
  1262
        by (simp add: permutes_def pa qa o_def fun_upd_def Fun.swap_def id_def
62390
842917225d56 more canonical names
nipkow
parents: 61424
diff changeset
  1263
            cong del: if_weak_cong split: if_split_asm)
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1264
      from eq[unfolded bc] have "(\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> p) =
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1265
        (\<lambda>p. Fun.swap a c id \<circ> p) (Fun.swap a c id \<circ> q)" by simp
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1266
      then have "p = q"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1267
        unfolding o_assoc swap_id_idempotent by simp
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1268
      with bc have "b = c \<and> p = q"
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1269
        by blast
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1270
    }
30488
5c4c3a9e9102 remove trailing spaces
huffman
parents: 30267
diff changeset
  1271
    then show "inj_on ?f (insert a S \<times> ?P)"
54681
8a8e6db7f391 tuned proofs;
wenzelm
parents: 53374
diff changeset
  1272
      unfolding inj_on_def by clarify metis
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1273
  qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1274
qed
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1275
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1276
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1277
subsection \<open>Constructing permutations from association lists\<close>
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1278
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1279
definition list_permutes :: "('a \<times> 'a) list \<Rightarrow> 'a set \<Rightarrow> bool"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1280
  where "list_permutes xs A \<longleftrightarrow>
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1281
    set (map fst xs) \<subseteq> A \<and>
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1282
    set (map snd xs) = set (map fst xs) \<and>
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1283
    distinct (map fst xs) \<and>
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1284
    distinct (map snd xs)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1285
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1286
lemma list_permutesI [simp]:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1287
  assumes "set (map fst xs) \<subseteq> A" "set (map snd xs) = set (map fst xs)" "distinct (map fst xs)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1288
  shows "list_permutes xs A"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1289
proof -
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1290
  from assms(2,3) have "distinct (map snd xs)"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1291
    by (intro card_distinct) (simp_all add: distinct_card del: set_map)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1292
  with assms show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1293
    by (simp add: list_permutes_def)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1294
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1295
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1296
definition permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1297
  where "permutation_of_list xs x = (case map_of xs x of None \<Rightarrow> x | Some y \<Rightarrow> y)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1298
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1299
lemma permutation_of_list_Cons:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1300
  "permutation_of_list ((x, y) # xs) x' = (if x = x' then y else permutation_of_list xs x')"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1301
  by (simp add: permutation_of_list_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1302
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1303
fun inverse_permutation_of_list :: "('a \<times> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1304
  where
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1305
    "inverse_permutation_of_list [] x = x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1306
  | "inverse_permutation_of_list ((y, x') # xs) x =
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1307
      (if x = x' then y else inverse_permutation_of_list xs x)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1308
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1309
declare inverse_permutation_of_list.simps [simp del]
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1310
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1311
lemma inj_on_map_of:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1312
  assumes "distinct (map snd xs)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1313
  shows "inj_on (map_of xs) (set (map fst xs))"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1314
proof (rule inj_onI)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1315
  fix x y
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1316
  assume xy: "x \<in> set (map fst xs)" "y \<in> set (map fst xs)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1317
  assume eq: "map_of xs x = map_of xs y"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1318
  from xy obtain x' y' where x'y': "map_of xs x = Some x'" "map_of xs y = Some y'"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1319
    by (cases "map_of xs x"; cases "map_of xs y") (simp_all add: map_of_eq_None_iff)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1320
  moreover from x'y' have *: "(x, x') \<in> set xs" "(y, y') \<in> set xs"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1321
    by (force dest: map_of_SomeD)+
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1322
  moreover from * eq x'y' have "x' = y'"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1323
    by simp
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1324
  ultimately show "x = y"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1325
    using assms by (force simp: distinct_map dest: inj_onD[of _ _ "(x,x')" "(y,y')"])
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1326
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1327
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1328
lemma inj_on_the: "None \<notin> A \<Longrightarrow> inj_on the A"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1329
  by (auto simp: inj_on_def option.the_def split: option.splits)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1330
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1331
lemma inj_on_map_of':
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1332
  assumes "distinct (map snd xs)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1333
  shows "inj_on (the \<circ> map_of xs) (set (map fst xs))"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1334
  by (intro comp_inj_on inj_on_map_of assms inj_on_the)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1335
    (force simp: eq_commute[of None] map_of_eq_None_iff)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1336
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1337
lemma image_map_of:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1338
  assumes "distinct (map fst xs)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1339
  shows "map_of xs ` set (map fst xs) = Some ` set (map snd xs)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1340
  using assms by (auto simp: rev_image_eqI)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1341
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1342
lemma the_Some_image [simp]: "the ` Some ` A = A"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1343
  by (subst image_image) simp
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1344
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1345
lemma image_map_of':
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1346
  assumes "distinct (map fst xs)"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1347
  shows "(the \<circ> map_of xs) ` set (map fst xs) = set (map snd xs)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1348
  by (simp only: image_comp [symmetric] image_map_of assms the_Some_image)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1349
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1350
lemma permutation_of_list_permutes [simp]:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1351
  assumes "list_permutes xs A"
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1352
  shows "permutation_of_list xs permutes A"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1353
    (is "?f permutes _")
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1354
proof (rule permutes_subset[OF bij_imp_permutes])
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1355
  from assms show "set (map fst xs) \<subseteq> A"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1356
    by (simp add: list_permutes_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1357
  from assms have "inj_on (the \<circ> map_of xs) (set (map fst xs))" (is ?P)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1358
    by (intro inj_on_map_of') (simp_all add: list_permutes_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1359
  also have "?P \<longleftrightarrow> inj_on ?f (set (map fst xs))"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1360
    by (intro inj_on_cong)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1361
      (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1362
  finally have "bij_betw ?f (set (map fst xs)) (?f ` set (map fst xs))"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1363
    by (rule inj_on_imp_bij_betw)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1364
  also from assms have "?f ` set (map fst xs) = (the \<circ> map_of xs) ` set (map fst xs)"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1365
    by (intro image_cong refl)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1366
      (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits)
64284
f3b905b2eee2 HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
hoelzl
parents: 64267
diff changeset
  1367
  also from assms have "\<dots> = set (map fst xs)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1368
    by (subst image_map_of') (simp_all add: list_permutes_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1369
  finally show "bij_betw ?f (set (map fst xs)) (set (map fst xs))" .
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1370
qed (force simp: permutation_of_list_def dest!: map_of_SomeD split: option.splits)+
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1371
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1372
lemma eval_permutation_of_list [simp]:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1373
  "permutation_of_list [] x = x"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1374
  "x = x' \<Longrightarrow> permutation_of_list ((x',y)#xs) x = y"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1375
  "x \<noteq> x' \<Longrightarrow> permutation_of_list ((x',y')#xs) x = permutation_of_list xs x"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1376
  by (simp_all add: permutation_of_list_def)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1377
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1378
lemma eval_inverse_permutation_of_list [simp]:
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1379
  "inverse_permutation_of_list [] x = x"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1380
  "x = x' \<Longrightarrow> inverse_permutation_of_list ((y,x')#xs) x = y"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1381
  "x \<noteq> x' \<Longrightarrow> inverse_permutation_of_list ((y',x')#xs) x = inverse_permutation_of_list xs x"
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1382
  by (simp_all add: inverse_permutation_of_list.simps)
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1383
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1384
lemma permutation_of_list_id: "x \<notin> set (map fst xs) \<Longrightarrow> permutation_of_list xs x = x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1385
  by (induct xs) (auto simp: permutation_of_list_Cons)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1386
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1387
lemma permutation_of_list_unique':
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1388
  "distinct (map fst xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1389
  by (induct xs) (force simp: permutation_of_list_Cons)+
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1390
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1391
lemma permutation_of_list_unique:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1392
  "list_permutes xs A \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> permutation_of_list xs x = y"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1393
  by (intro permutation_of_list_unique') (simp_all add: list_permutes_def)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1394
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1395
lemma inverse_permutation_of_list_id:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1396
  "x \<notin> set (map snd xs) \<Longrightarrow> inverse_permutation_of_list xs x = x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1397
  by (induct xs) auto
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1398
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1399
lemma inverse_permutation_of_list_unique':
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1400
  "distinct (map snd xs) \<Longrightarrow> (x, y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1401
  by (induct xs) (force simp: inverse_permutation_of_list.simps)+
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1402
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1403
lemma inverse_permutation_of_list_unique:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1404
  "list_permutes xs A \<Longrightarrow> (x,y) \<in> set xs \<Longrightarrow> inverse_permutation_of_list xs y = x"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1405
  by (intro inverse_permutation_of_list_unique') (simp_all add: list_permutes_def)
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1406
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1407
lemma inverse_permutation_of_list_correct:
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1408
  fixes A :: "'a set"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1409
  assumes "list_permutes xs A"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1410
  shows "inverse_permutation_of_list xs = inv (permutation_of_list xs)"
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1411
proof (rule ext, rule sym, subst permutes_inv_eq)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1412
  from assms show "permutation_of_list xs permutes A"
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1413
    by simp
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1414
  show "permutation_of_list xs (inverse_permutation_of_list xs x) = x" for x
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1415
  proof (cases "x \<in> set (map snd xs)")
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1416
    case True
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1417
    then obtain y where "(y, x) \<in> set xs" by auto
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1418
    with assms show ?thesis
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1419
      by (simp add: inverse_permutation_of_list_unique permutation_of_list_unique)
65342
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1420
  next
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1421
    case False
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1422
    with assms show ?thesis
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1423
      by (auto simp: list_permutes_def inverse_permutation_of_list_id permutation_of_list_id)
e32eb488c3a3 misc tuning and modernization;
wenzelm
parents: 64966
diff changeset
  1424
  qed
63099
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1425
qed
af0e964aad7b Moved material from AFP/Randomised_Social_Choice to distribution
eberlm
parents: 62390
diff changeset
  1426
29840
cfab6a76aa13 Permutations, both general and specifically on finite sets.
chaieb
parents:
diff changeset
  1427
end