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