Debian Codegraph @

src/Algebra/Properties/Monoid/Mult/TCOptimised.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Multiplication over a monoid (i.e. repeated addition) optimised for
5 -- type checking.
6 ------------------------------------------------------------------------
7
8 {-# OPTIONS --cubical-compatible --safe #-}
9
10 open import Algebra.Bundles using (Monoid)
11 open import Data.Nat.Base as ℕ using (ℕ; zero; suc)
12 open import Relation.Binary.Core using (_Preserves_⟶_; _Preserves₂_⟶_⟶_)
13 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
14
15 module Algebra.Properties.Monoid.Mult.TCOptimised
16 {a ℓ} (M : Monoid a ℓ) where
17
18 open Monoid M renaming
19 ( _∙_ to _+_
20 ; ∙-cong to +-cong
21 ; ∙-congˡ to +-congˡ
22 ; ∙-congʳ to +-congʳ
23 ; identityˡ to +-identityˡ
24 ; identityʳ to +-identityʳ
25 ; assoc to +-assoc
26 ; ε to 0#
27 )
28
29 open import Algebra.Properties.Monoid.Mult M as U
30 using () renaming (_×_ to _×ᵤ_)
31
32 open import Relation.Binary.Reasoning.Setoid setoid
33
34 ------------------------------------------------------------------------
35 -- Definition
36
37 open import Algebra.Definitions.RawMonoid rawMonoid public
38 using () renaming (_×′_ to _×_)
39
40 ------------------------------------------------------------------------
41 -- Properties
42
43 1+× : ∀ n x → suc n × x ≈ x + n × x
44 1+× 0 x = sym (+-identityʳ x)
45 1+× 1 x = refl
46 1+× (suc n@(suc _)) x = begin
47 (suc n × x) + x ≈⟨ +-congʳ (1+× n x) ⟩
48 (x + n × x) + x ≈⟨ +-assoc x (n × x) x ⟩
49 x + (n × x + x) ∎
50
51 -- The unoptimised (_×ᵤ_) and optimised (_×_) versions of multiplication
52 -- are extensionally equal (up to the setoid equivalence).
53
54 ×ᵤ≈× : ∀ n x → n ×ᵤ x ≈ n × x
55 ×ᵤ≈× 0 x = refl
56 ×ᵤ≈× (suc n) x = begin
57 x + n ×ᵤ x ≈⟨ +-congˡ (×ᵤ≈× n x) ⟩
58 x + n × x ≈⟨ 1+× n x ⟨
59 suc n × x ∎
60
61 -- _×_ is homomorphic with respect to _ℕ.+_/_+_.
62
63 ×-homo-+ : ∀ c m n → (m ℕ.+ n) × c ≈ m × c + n × c
64 ×-homo-+ c m n = begin
65 (m ℕ.+ n) × c ≈⟨ ×ᵤ≈× (m ℕ.+ n) c ⟨
66 (m ℕ.+ n) ×ᵤ c ≈⟨ U.×-homo-+ c m n ⟩
67 m ×ᵤ c + n ×ᵤ c ≈⟨ +-cong (×ᵤ≈× m c) (×ᵤ≈× n c) ⟩
68 m × c + n × c ∎
69
70 -- _×_ preserves equality.
71
72 ×-congʳ : ∀ n → (n ×_) Preserves _≈_ ⟶ _≈_
73 ×-congʳ 0 x≈y = refl
74 ×-congʳ 1 x≈y = x≈y
75 ×-congʳ (suc n@(suc _)) x≈y = +-cong (×-congʳ n x≈y) x≈y
76
77 ×-cong : _×_ Preserves₂ _≡_ ⟶ _≈_ ⟶ _≈_
78 ×-cong {n} ≡.refl x≈y = ×-congʳ n x≈y
79
80 ×-assocˡ : ∀ x m n → m × (n × x) ≈ (m ℕ.* n) × x
81 ×-assocˡ x m n = begin
82 m × (n × x) ≈⟨ ×-congʳ m (×ᵤ≈× n x) ⟨
83 m × (n ×ᵤ x) ≈⟨ ×ᵤ≈× m (n ×ᵤ x) ⟨
84 m ×ᵤ (n ×ᵤ x) ≈⟨ U.×-assocˡ x m n ⟩
85 (m ℕ.* n) ×ᵤ x ≈⟨ ×ᵤ≈× (m ℕ.* n) x ⟩
86 (m ℕ.* n) × x ∎