7
|
1 module Symmetric where
|
0
|
2
|
|
3 open import Level hiding ( suc ; zero )
|
|
4 open import Algebra
|
|
5 open import Algebra.Structures
|
37
|
6 open import Data.Fin hiding ( _<_ ; _≤_ ; _-_ ; _+_ )
|
35
|
7 open import Data.Fin.Properties hiding ( <-cmp ; <-trans ; ≤-trans )
|
3
|
8 open import Data.Product
|
0
|
9 open import Data.Fin.Permutation
|
|
10 open import Function hiding (id ; flip)
|
|
11 open import Function.Inverse as Inverse using (_↔_; Inverse; _InverseOf_)
|
|
12 open import Function.LeftInverse using ( _LeftInverseOf_ )
|
|
13 open import Function.Equality using (Π)
|
17
|
14 open import Data.Nat -- using (ℕ; suc; zero; s≤s ; z≤n )
|
|
15 open import Data.Nat.Properties -- using (<-trans)
|
16
|
16 open import Relation.Binary.PropositionalEquality
|
|
17 open import Data.List using (List; []; _∷_ ; length)
|
|
18 open import nat
|
0
|
19
|
|
20 fid : {p : ℕ } → Fin p → Fin p
|
|
21 fid x = x
|
|
22
|
|
23 -- Data.Fin.Permutation.id
|
|
24 pid : {p : ℕ } → Permutation p p
|
|
25 pid = permutation fid fid record { left-inverse-of = λ x → refl ; right-inverse-of = λ x → refl }
|
|
26
|
|
27 -- Data.Fin.Permutation.flip
|
|
28 pinv : {p : ℕ } → Permutation p p → Permutation p p
|
|
29 pinv {p} P = permutation (_⟨$⟩ˡ_ P) (_⟨$⟩ʳ_ P ) record { left-inverse-of = λ x → inverseʳ P ; right-inverse-of = λ x → inverseˡ P }
|
|
30
|
3
|
31 record _=p=_ {p : ℕ } ( x y : Permutation p p ) : Set where
|
|
32 field
|
|
33 peq : ( q : Fin p ) → x ⟨$⟩ʳ q ≡ y ⟨$⟩ʳ q
|
0
|
34
|
3
|
35 open _=p=_
|
0
|
36
|
3
|
37 prefl : {p : ℕ } { x : Permutation p p } → x =p= x
|
|
38 peq (prefl {p} {x}) q = refl
|
1
|
39
|
3
|
40 psym : {p : ℕ } { x y : Permutation p p } → x =p= y → y =p= x
|
|
41 peq (psym {p} {x} {y} eq ) q = sym (peq eq q)
|
1
|
42
|
3
|
43 ptrans : {p : ℕ } { x y z : Permutation p p } → x =p= y → y =p= z → x =p= z
|
|
44 peq (ptrans {p} {x} {y} x=y y=z ) q = trans (peq x=y q) (peq y=z q)
|
0
|
45
|
7
|
46 Symmetric : ℕ → Group Level.zero Level.zero
|
|
47 Symmetric p = record {
|
0
|
48 Carrier = Permutation p p
|
3
|
49 ; _≈_ = _=p=_
|
0
|
50 ; _∙_ = _∘ₚ_
|
|
51 ; ε = pid
|
|
52 ; _⁻¹ = pinv
|
3
|
53 ; isGroup = record { isMonoid = record { isSemigroup = record { isMagma = record {
|
|
54 isEquivalence = record {refl = prefl ; trans = ptrans ; sym = psym }
|
0
|
55 ; ∙-cong = presp }
|
|
56 ; assoc = passoc }
|
3
|
57 ; identity = ( (λ q → record { peq = λ q → refl } ) , (λ q → record { peq = λ q → refl } )) }
|
|
58 ; inverse = ( (λ x → record { peq = λ q → inverseʳ x} ) , (λ x → record { peq = λ q → inverseˡ x} ))
|
|
59 ; ⁻¹-cong = λ i=j → record { peq = λ q → p-inv i=j q }
|
0
|
60 }
|
|
61 } where
|
3
|
62 presp : {x y u v : Permutation p p } → x =p= y → u =p= v → (x ∘ₚ u) =p= (y ∘ₚ v)
|
|
63 presp {x} {y} {u} {v} x=y u=v = record { peq = λ q → lemma4 q } where
|
|
64 lemma4 : (q : Fin p) → ((x ∘ₚ u) ⟨$⟩ʳ q) ≡ ((y ∘ₚ v) ⟨$⟩ʳ q)
|
|
65 lemma4 q = trans (cong (λ k → Inverse.to u Π.⟨$⟩ k) (peq x=y q) ) (peq u=v _ )
|
|
66 passoc : (x y z : Permutation p p) → ((x ∘ₚ y) ∘ₚ z) =p= (x ∘ₚ (y ∘ₚ z))
|
|
67 passoc x y z = record { peq = λ q → refl }
|
|
68 p-inv : {i j : Permutation p p} → i =p= j → (q : Fin p) → pinv i ⟨$⟩ʳ q ≡ pinv j ⟨$⟩ʳ q
|
|
69 p-inv {i} {j} i=j q = begin
|
4
|
70 i ⟨$⟩ˡ q ≡⟨ cong (λ k → i ⟨$⟩ˡ k) (sym (inverseʳ j) ) ⟩
|
|
71 i ⟨$⟩ˡ ( j ⟨$⟩ʳ ( j ⟨$⟩ˡ q )) ≡⟨ cong (λ k → i ⟨$⟩ˡ k) (sym (peq i=j _ )) ⟩
|
|
72 i ⟨$⟩ˡ ( i ⟨$⟩ʳ ( j ⟨$⟩ˡ q )) ≡⟨ inverseˡ i ⟩
|
|
73 j ⟨$⟩ˡ q
|
3
|
74 ∎ where open ≡-Reasoning
|
0
|
75
|
16
|
76 open import Relation.Nullary
|
|
77 open import Data.Empty
|
17
|
78 open import Relation.Binary.Core
|
|
79 open import fin
|
16
|
80
|
38
|
81 -- An inductive construction of permutation
|
34
|
82
|
|
83 pprep : {n : ℕ } → Permutation n n → Permutation (suc n) (suc n)
|
|
84 pprep {n} perm = permutation p→ p← record { left-inverse-of = piso→ ; right-inverse-of = piso← } where
|
33
|
85 p→ : Fin (suc n) → Fin (suc n)
|
34
|
86 p→ zero = zero
|
|
87 p→ (suc x) = suc ( perm ⟨$⟩ˡ x)
|
33
|
88
|
34
|
89 p← : Fin (suc n) → Fin (suc n)
|
|
90 p← zero = zero
|
|
91 p← (suc x) = suc ( perm ⟨$⟩ʳ x)
|
|
92
|
|
93 piso← : (x : Fin (suc n)) → p→ ( p← x ) ≡ x
|
|
94 piso← zero = refl
|
35
|
95 piso← (suc x) = cong (λ k → suc k ) (inverseˡ perm)
|
33
|
96
|
34
|
97 piso→ : (x : Fin (suc n)) → p← ( p→ x ) ≡ x
|
|
98 piso→ zero = refl
|
35
|
99 piso→ (suc x) = cong (λ k → suc k ) (inverseʳ perm)
|
33
|
100
|
34
|
101 pswap : {n : ℕ } → Permutation n n → Permutation (suc (suc n)) (suc (suc n ))
|
|
102 pswap {n} perm = permutation p→ p← record { left-inverse-of = piso→ ; right-inverse-of = piso← } where
|
|
103 p→ : Fin (suc (suc n)) → Fin (suc (suc n))
|
|
104 p→ zero = suc zero
|
|
105 p→ (suc zero) = zero
|
|
106 p→ (suc (suc x)) = suc ( suc ( perm ⟨$⟩ˡ x) )
|
18
|
107
|
34
|
108 p← : Fin (suc (suc n)) → Fin (suc (suc n))
|
|
109 p← zero = suc zero
|
|
110 p← (suc zero) = zero
|
|
111 p← (suc (suc x)) = suc ( suc ( perm ⟨$⟩ʳ x) )
|
|
112
|
|
113 piso← : (x : Fin (suc (suc n)) ) → p→ ( p← x ) ≡ x
|
|
114 piso← zero = refl
|
|
115 piso← (suc zero) = refl
|
35
|
116 piso← (suc (suc x)) = cong (λ k → suc (suc k) ) (inverseˡ perm)
|
16
|
117
|
34
|
118 piso→ : (x : Fin (suc (suc n)) ) → p← ( p→ x ) ≡ x
|
|
119 piso→ zero = refl
|
|
120 piso→ (suc zero) = refl
|
35
|
121 piso→ (suc (suc x)) = cong (λ k → suc (suc k) ) (inverseʳ perm)
|
34
|
122
|
|
123 -- enumeration
|
|
124
|
|
125 psawpn : {n m : ℕ} → suc m < n → Permutation n n
|
|
126 psawpn {suc zero} {m} (s≤s ())
|
|
127 psawpn {suc n} {m} (s≤s (s≤s x)) = pswap pid
|
|
128
|
35
|
129 pfill : { n m : ℕ } → m ≤ n → Permutation m m → Permutation n n
|
|
130 pfill {n} {m} m≤n perm = pfill1 (n - m) (n-m<n n m ) (subst (λ k → Permutation k k ) (n-n-m=m m≤n ) perm) where
|
|
131 pfill1 : (i : ℕ ) → i ≤ n → Permutation (n - i) (n - i) → Permutation n n
|
|
132 pfill1 0 _ perm = perm
|
|
133 pfill1 (suc i) i<n perm = pfill1 i (≤to< i<n) (subst (λ k → Permutation k k ) (si-sn=i-n i<n ) ( pprep perm ) )
|
34
|
134
|
|
135 eperm : {n m : ℕ} → m < n → Permutation n n → Permutation (suc n) (suc n)
|
|
136 eperm {zero} ()
|
35
|
137 eperm {n} {0} (s≤s z≤n) perm = pprep perm
|
37
|
138 eperm {n} {suc m} (s≤s m<n) perm = eperm1 m 2 lemm3 (pswap {0} pid ) (pprep perm) where
|
|
139 lemm3 : 2 + m ≤ suc n
|
|
140 lemm3 = ≤-trans (s≤s m<n) refl-≤s
|
|
141 eperm1 : (m i : ℕ ) → i + m ≤ suc n → Permutation i i → Permutation (suc n)(suc n)→ Permutation (suc n)(suc n)
|
|
142 eperm1 zero i i<ssm sw perm = perm ∘ₚ ( pfill (subst (λ k → k ≤ suc n) (+-comm i _) i<ssm) sw ) -- i + zero ≤ suc (suc n) → i ≤ suc (suc n)
|
|
143 eperm1 (suc m) i i<ssm sw perm = eperm1 m (suc i) (lemm4 i<ssm ) (pprep sw) perm where
|
|
144 lemm4 : i + suc m ≤ suc n → suc i + m ≤ suc n
|
|
145 lemm4 lt = begin
|
|
146 suc i + m ≡⟨ cong (λ k → suc k ) ( +-comm i _ ) ⟩
|
|
147 suc m + i ≡⟨ +-comm (suc m) _ ⟩
|
|
148 i + suc m ≤⟨ lt ⟩
|
|
149 suc n
|
|
150 ∎ where open ≤-Reasoning
|
|
151
|
35
|
152
|
37
|
153 finpid : (n i : ℕ ) → i < n → List (Fin n)
|
|
154 finpid (suc n) zero _ = fromℕ≤ {zero} (s≤s z≤n) ∷ []
|
|
155 finpid (suc n) (suc i) (s≤s lt) = fromℕ≤ (s≤s lt) ∷ finpid (suc n) i (<-trans lt a<sa)
|
|
156
|
|
157 fpid : (n : ℕ ) → List (Fin n)
|
|
158 fpid 0 = []
|
|
159 fpid (suc j) = finpid (suc j) j a<sa where
|
|
160
|
|
161 plist : {n : ℕ} → Permutation n n → List ℕ
|
|
162 plist {0} perm = []
|
|
163 plist {suc j} perm = plist1 j a<sa where
|
|
164 n = suc j
|
|
165 plist1 : (i : ℕ ) → i < n → List ℕ
|
|
166 plist1 zero _ = toℕ ( perm ⟨$⟩ˡ (fromℕ≤ {zero} (s≤s z≤n))) ∷ []
|
|
167 plist1 (suc i) (s≤s lt) = toℕ ( perm ⟨$⟩ˡ (fromℕ≤ (s≤s lt))) ∷ plist1 i (<-trans lt a<sa)
|
|
168
|
|
169 test = eperm {3} ( s≤s ( s≤s z≤n )) ( eperm (s≤s z≤n) pid )
|
38
|
170
|
|
171 NL : (n : ℕ ) → Set
|
|
172 NL 0 = ℕ
|
|
173 NL (suc n) = List ( NL n )
|
|
174
|
|
175 pls : (n : ℕ ) → List (List ℕ )
|
|
176 pls n = pls1 n n lem0 where
|
|
177 lem0 : {n : ℕ } → n ≤ n
|
|
178 lem0 {zero} = z≤n
|
|
179 lem0 {suc n} = s≤s lem0
|
|
180 lem1 : {i n : ℕ } → i ≤ n → i < suc n
|
|
181 lem1 z≤n = s≤s z≤n
|
|
182 lem1 (s≤s lt) = s≤s (lem1 lt)
|
|
183 lem2 : {i n : ℕ } → i ≤ n → i ≤ suc n
|
|
184 lem2 i≤n = ≤-trans i≤n ( refl-≤s )
|
|
185 pls2 : ( i n : ℕ ) → (i<n : i ≤ n ) → Permutation (suc n) (suc n)
|
|
186 pls2 0 n i≤<n = pid
|
|
187 pls2 (suc i) (suc n) (s≤s i≤n) = eperm {suc n} {i} (lem1 i≤n) ( pls2 i n i≤n)
|
|
188 pls1 : ( i n : ℕ ) → i ≤ n → List (List ℕ)
|
|
189 pls1 0 n _ = []
|
|
190 pls1 (suc i) n (s≤s i≤n) = plist ( pls2 i n (lem2 i≤n)) ∷ pls1 i n (lem2 i≤n)
|