{-# OPTIONS --allow-unsolved-metas #-} module Putil where open import Level hiding ( suc ; zero ) open import Algebra open import Algebra.Structures open import Data.Fin hiding ( _<_ ; _≤_ ; _-_ ; _+_ ; _≟_) open import Data.Fin.Properties hiding ( <-trans ; ≤-trans ; ≤-irrelevant ; _≟_ ) renaming ( <-cmp to <-fcmp ) open import Data.Fin.Permutation open import Function hiding (id ; flip) open import Function.Inverse as Inverse using (_↔_; Inverse; _InverseOf_) open import Function.LeftInverse using ( _LeftInverseOf_ ) open import Function.Equality using (Π) open import Data.Nat -- using (ℕ; suc; zero; s≤s ; z≤n ) open import Data.Nat.Properties -- using (<-trans) open import Relation.Binary.PropositionalEquality hiding ( [_] ) open import Data.List using (List; []; _∷_ ; length ; _++_ ; head ; tail ) renaming (reverse to rev ) open import nat open import Symmetric open import Relation.Nullary open import Data.Empty open import Relation.Binary.Core open import Relation.Binary.Definitions open import fin -- An inductive construction of permutation pprep : {n : ℕ } → Permutation n n → Permutation (suc n) (suc n) pprep {n} perm = permutation p→ p← record { left-inverse-of = piso→ ; right-inverse-of = piso← } where p→ : Fin (suc n) → Fin (suc n) p→ zero = zero p→ (suc x) = suc ( perm ⟨$⟩ʳ x) p← : Fin (suc n) → Fin (suc n) p← zero = zero p← (suc x) = suc ( perm ⟨$⟩ˡ x) piso← : (x : Fin (suc n)) → p→ ( p← x ) ≡ x piso← zero = refl piso← (suc x) = cong (λ k → suc k ) (inverseʳ perm) piso→ : (x : Fin (suc n)) → p← ( p→ x ) ≡ x piso→ zero = refl piso→ (suc x) = cong (λ k → suc k ) (inverseˡ perm) pswap : {n : ℕ } → Permutation n n → Permutation (suc (suc n)) (suc (suc n )) pswap {n} perm = permutation p→ p← record { left-inverse-of = piso→ ; right-inverse-of = piso← } where p→ : Fin (suc (suc n)) → Fin (suc (suc n)) p→ zero = suc zero p→ (suc zero) = zero p→ (suc (suc x)) = suc ( suc ( perm ⟨$⟩ʳ x) ) p← : Fin (suc (suc n)) → Fin (suc (suc n)) p← zero = suc zero p← (suc zero) = zero p← (suc (suc x)) = suc ( suc ( perm ⟨$⟩ˡ x) ) piso← : (x : Fin (suc (suc n)) ) → p→ ( p← x ) ≡ x piso← zero = refl piso← (suc zero) = refl piso← (suc (suc x)) = cong (λ k → suc (suc k) ) (inverseʳ perm) piso→ : (x : Fin (suc (suc n)) ) → p← ( p→ x ) ≡ x piso→ zero = refl piso→ (suc zero) = refl piso→ (suc (suc x)) = cong (λ k → suc (suc k) ) (inverseˡ perm) psawpn : {n : ℕ} → 1 < n → Permutation n n psawpn {suc zero} (s≤s ()) psawpn {suc n} (s≤s (s≤s x)) = pswap pid pfill : { n m : ℕ } → m ≤ n → Permutation m m → Permutation n n pfill {n} {m} m≤n perm = pfill1 (n - m) (n-m ¬a ¬b c = x p← : Fin (suc (suc n)) → Fin (suc (suc n)) p← zero = fromℕ< (s≤s (s≤s m≤n)) p← (suc x) with <-cmp (toℕ x) (suc m) ... | tri< a ¬b ¬c = fromℕ< (≤-trans (fin ¬a ¬b c = suc x mm : toℕ (fromℕ< {suc m} {suc (suc n)} (s≤s (s≤s m≤n))) ≡ suc m mm = toℕ-fromℕ< (s≤s (s≤s m≤n)) mma : (x : Fin (suc n) ) → suc (toℕ x) ≤ suc m → toℕ ( fromℕ< (≤-trans (fin ¬a ¬b c = p16 (suc x) refl where p16 : (y : Fin (suc (suc n))) → y ≡ suc x → p← y ≡ suc x p16 zero eq = ⊥-elim ( nat-≡< (cong (λ k → suc (toℕ k) ) eq) (s≤s (s≤s (z≤n)))) p16 (suc y) eq with <-cmp (toℕ y) (suc m) -- suc (suc m) < toℕ (suc x) ... | tri< a ¬b ¬c = ⊥-elim ( nat-≡< refl ( ≤-trans c (subst (λ k → k < suc m) p17 a )) ) where -- x = suc m case, c : suc (suc m) ≤ suc (toℕ x), a : suc (toℕ y) ≤ suc m, suc y ≡ suc x p17 : toℕ y ≡ toℕ x p17 with <-cmp (toℕ y) (toℕ x) | cong toℕ eq ... | tri< a ¬b ¬c | seq = ⊥-elim ( nat-≡< seq (s≤s a) ) ... | tri≈ ¬a b ¬c | seq = b ... | tri> ¬a ¬b c | seq = ⊥-elim ( nat-≡< (sym seq) (s≤s c)) ... | tri≈ ¬a b ¬c = eq ... | tri> ¬a ¬b c₁ = eq ... | tri< a ¬b ¬c = p10 (fromℕ< (≤-trans (s≤s a) (s≤s (s≤s m≤n) ))) refl where p10 : (y : Fin (suc (suc n)) ) → y ≡ fromℕ< (≤-trans (s≤s a) (s≤s (s≤s m≤n) )) → p← y ≡ suc x p10 zero () p10 (suc y) eq = p15 where p12 : toℕ y ≡ suc (toℕ x) p12 = begin toℕ y ≡⟨ cong (λ k → Data.Nat.pred (toℕ k)) eq ⟩ toℕ (fromℕ< (≤-trans a (s≤s m≤n))) ≡⟨ toℕ-fromℕ< {suc (toℕ x)} {suc n} (≤-trans a (s≤s m≤n)) ⟩ suc (toℕ x) ∎ p15 : p← (suc y) ≡ suc x p15 with <-cmp (toℕ y) (suc m) -- eq : suc y ≡ suc (suc (fromℕ< (≤-pred (≤-trans a (s≤s m≤n))))) , a : suc x < suc m ... | tri< a₁ ¬b ¬c = p11 where p11 : fromℕ< (≤-trans (fin y = suc x → toℕ y < suc m ... | tri> ¬a ¬b c = ⊥-elim ( nat-<> c (subst (λ k → k < suc m) (sym p12) a )) piso← : (x : Fin (suc (suc n)) ) → p→ ( p← x ) ≡ x piso← zero with <-cmp (toℕ (fromℕ< (s≤s (s≤s m≤n)))) (suc m) | mm ... | tri< a ¬b ¬c | t = ⊥-elim ( ¬b t ) ... | tri≈ ¬a b ¬c | t = refl ... | tri> ¬a ¬b c | t = ⊥-elim ( ¬b t ) piso← (suc x) with <-cmp (toℕ x) (suc m) ... | tri> ¬a ¬b c with <-cmp (toℕ (suc x)) (suc m) ... | tri< a ¬b₁ ¬c = ⊥-elim ( nat-<> a (<-trans c a ¬a₁ ¬b₁ c₁ = refl piso← (suc x) | tri≈ ¬a b ¬c with <-cmp (toℕ (suc x)) (suc m) ... | tri< a ¬b ¬c₁ = ⊥-elim ( nat-≡< b (<-trans a ¬a₁ ¬b c = refl piso← (suc x) | tri< a ¬b ¬c with <-cmp (toℕ ( fromℕ< (≤-trans (fin ¬a ¬b₁ c = ⊥-elim ( ¬a (s≤s (mma x a))) ... | tri< a₁ ¬b₁ ¬c₁ = p0 where p2 : suc (suc (toℕ x)) ≤ suc (suc n) p2 = s≤s (fin ¬a ¬b c = ⊥-elim (nat-≤> c q ¬a ¬b c = ⊥-elim (nat-≤> c q