|
| 1 | +------------------------------------------------------------------------ |
| 2 | +-- The Agda standard library |
| 3 | +-- |
| 4 | +-- Some properties about parities |
| 5 | +------------------------------------------------------------------------ |
| 6 | + |
| 7 | +{-# OPTIONS --without-K --safe #-} |
| 8 | + |
| 9 | +module Data.Parity.HomomorphismNatParity where |
| 10 | + |
| 11 | +open import Data.Nat.Base as ℕ using (zero; suc; parity) |
| 12 | +open import Data.Parity.Base as ℙ using (0ℙ; 1ℙ; _⁻¹) |
| 13 | +open import Data.Parity.Properties using (⁻¹-inverts) |
| 14 | +open import Relation.Binary.PropositionalEquality |
| 15 | + using (_≡_; refl; sym; cong; module ≡-Reasoning) |
| 16 | + |
| 17 | +open import Algebra.Morphism.Structures |
| 18 | + using (IsMagmaHomomorphism; IsMonoidHomomorphism |
| 19 | + ; IsNearSemiringHomomorphism; IsSemiringHomomorphism) |
| 20 | + |
| 21 | +------------------------------------------------------------------------ |
| 22 | +-- relating Nat and Parity |
| 23 | + |
| 24 | +-- successor and (_⁻¹) |
| 25 | + |
| 26 | +suc-homo-⁻¹ : ∀ n → (parity (suc n)) ⁻¹ ≡ parity n |
| 27 | +suc-homo-⁻¹ zero = refl |
| 28 | +suc-homo-⁻¹ (suc n) = ⁻¹-inverts (suc-homo-⁻¹ n) |
| 29 | + |
| 30 | +-- parity is a _+_ homomorphism |
| 31 | + |
| 32 | ++-homo-+ : ∀ m n → parity (m ℕ.+ n) ≡ parity m ℙ.+ parity n |
| 33 | ++-homo-+ zero n = refl |
| 34 | ++-homo-+ (suc m) n = begin |
| 35 | + parity (suc m ℕ.+ n) ≡⟨ suc-+-homo-⁻¹ m n ⟩ |
| 36 | + (parity m) ⁻¹ ℙ.+ parity n ≡⟨ cong (ℙ._+ parity n) (suc-homo-⁻¹ (suc m)) ⟩ |
| 37 | + parity (suc m) ℙ.+ parity n ∎ |
| 38 | + where |
| 39 | + open ≡-Reasoning |
| 40 | + suc-+-homo-⁻¹ : ∀ m n → parity (suc m ℕ.+ n) ≡ (parity m) ⁻¹ ℙ.+ parity n |
| 41 | + suc-+-homo-⁻¹ zero n = sym (suc-homo-⁻¹ (suc n)) |
| 42 | + suc-+-homo-⁻¹ (suc m) n = begin |
| 43 | + parity (suc (suc m) ℕ.+ n) ≡⟨⟩ |
| 44 | + parity (m ℕ.+ n) ≡⟨ +-homo-+ m n ⟩ |
| 45 | + parity m ℙ.+ parity n ≡⟨ cong (ℙ._+ parity n) (sym (suc-homo-⁻¹ m)) ⟩ |
| 46 | + (parity (suc m)) ⁻¹ ℙ.+ (parity n) ∎ |
| 47 | + |
| 48 | +-- parity is a _*_ homomorphism |
| 49 | + |
| 50 | +*-homo-* : ∀ m n → parity (m ℕ.* n) ≡ parity m ℙ.* parity n |
| 51 | +*-homo-* zero n = refl |
| 52 | +*-homo-* (suc m) n = begin |
| 53 | + parity (suc m ℕ.* n) ≡⟨⟩ |
| 54 | + parity (n ℕ.+ m ℕ.* n) ≡⟨ +-homo-+ n (m ℕ.* n) ⟩ |
| 55 | + q ℙ.+ parity (m ℕ.* n) ≡⟨ cong (q ℙ.+_) (*-homo-* m n) ⟩ |
| 56 | + q ℙ.+ (p ℙ.* q) ≡⟨ lemma p q ⟩ |
| 57 | + ((p ⁻¹) ℙ.* q) ≡⟨⟩ |
| 58 | + ((parity m) ⁻¹ ℙ.* q) ≡⟨ cong (ℙ._* q) (suc-homo-⁻¹ (suc m)) ⟩ |
| 59 | + parity (suc m) ℙ.* q ≡⟨⟩ |
| 60 | + parity (suc m) ℙ.* parity n ∎ |
| 61 | + where |
| 62 | + open ≡-Reasoning |
| 63 | + p = parity m |
| 64 | + q = parity n |
| 65 | + -- this lemma simplifies things a lot |
| 66 | + lemma : ∀ p q → q ℙ.+ (p ℙ.* q) ≡ (p ⁻¹) ℙ.* q |
| 67 | + lemma 0ℙ 0ℙ = refl |
| 68 | + lemma 0ℙ 1ℙ = refl |
| 69 | + lemma 1ℙ 0ℙ = refl |
| 70 | + lemma 1ℙ 1ℙ = refl |
| 71 | + |
| 72 | +------------------------------------------------------------------------ |
| 73 | +-- parity is a Semiring homomorphism |
| 74 | + |
| 75 | +parity-isMagmaHomomorphism : IsMagmaHomomorphism ℕ.+-rawMagma ℙ.+-rawMagma parity |
| 76 | +parity-isMagmaHomomorphism = record |
| 77 | + { isRelHomomorphism = record |
| 78 | + { cong = cong parity } |
| 79 | + ; homo = +-homo-+ |
| 80 | + } |
| 81 | + |
| 82 | +parity-isMonoidHomomorphism : IsMonoidHomomorphism ℕ.+-0-rawMonoid ℙ.+-0-rawMonoid parity |
| 83 | +parity-isMonoidHomomorphism = record |
| 84 | + { isMagmaHomomorphism = parity-isMagmaHomomorphism |
| 85 | + ; ε-homo = refl |
| 86 | + } |
| 87 | + |
| 88 | +parity-isNearSemiringHomomorphism : IsNearSemiringHomomorphism ℕ.+-*-rawNearSemiring ℙ.+-*-rawNearSemiring parity |
| 89 | +parity-isNearSemiringHomomorphism = record |
| 90 | + { +-isMonoidHomomorphism = parity-isMonoidHomomorphism |
| 91 | + ; *-homo = *-homo-* |
| 92 | + } |
| 93 | + |
| 94 | +parity-isSemiringHomomorphism : IsSemiringHomomorphism ℕ.+-*-rawSemiring ℙ.+-*-rawSemiring parity |
| 95 | +parity-isSemiringHomomorphism = record |
| 96 | + { isNearSemiringHomomorphism = parity-isNearSemiringHomomorphism |
| 97 | + ; 1#-homo = refl |
| 98 | + } |
0 commit comments