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