Debian Codegraph @

src/Algebra/Solver/CommutativeMonoid/Normal.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Normal forms in commutative monoids
5 --
6 -- Adapted from Algebra.Solver.Monoid.Normal
7 ------------------------------------------------------------------------
8
9 {-# OPTIONS --cubical-compatible --safe #-}
10
11 open import Algebra.Bundles using (CommutativeMonoid)
12
13 module Algebra.Solver.CommutativeMonoid.Normal {c ℓ} (M : CommutativeMonoid c ℓ) where
14
15 import Algebra.Properties.CommutativeSemigroup as CSProperties
16 using (interchange)
17 import Algebra.Properties.Monoid.Mult as MultProperties
18 using (_×_; ×-homo-1; ×-homo-+)
19 open import Data.Fin.Base using (Fin; zero; suc)
20 open import Data.Nat as ℕ using (ℕ; zero; suc; _+_)
21 open import Data.Vec.Base using (Vec; []; _∷_; lookup; replicate; zipWith)
22 import Data.Vec.Relation.Binary.Pointwise.Inductive as Pointwise
23 using (Pointwise; _∷_; []; Pointwise-≡↔≡; decidable)
24 open import Relation.Binary.Definitions using (DecidableEquality)
25 import Relation.Binary.Reasoning.Setoid as ≈-Reasoning
26 import Relation.Nullary.Decidable as Dec using (map)
27
28 open CommutativeMonoid M
29 open MultProperties monoid using (_×_; ×-homo-1; ×-homo-+)
30 open CSProperties commutativeSemigroup using (interchange)
31 open ≈-Reasoning setoid
32
33 private
34 variable
35 n : ℕ
36
37
38 ------------------------------------------------------------------------
39 -- Monoid expressions
40
41 open import Algebra.Solver.Monoid.Expression rawMonoid public
42 hiding (NormalAPI)
43
44 ------------------------------------------------------------------------
45 -- Normal forms
46
47 -- A normal form is a vector of multiplicities (a bag).
48
49 Normal : ℕ → Set
50 Normal n = Vec ℕ n
51
52 -- The semantics of a normal form.
53
54 ⟦_⟧⇓ : Normal n → Env n → Carrier
55 ⟦ [] ⟧⇓ _ = ε
56 ⟦ n ∷ v ⟧⇓ (a ∷ ρ) = (n × a) ∙ (⟦ v ⟧⇓ ρ)
57
58 -- We can decide if two normal forms are /syntactically/ equal.
59
60 infix 5 _≟_
61
62 _≟_ : DecidableEquality (Normal n)
63 nf₁ ≟ nf₂ = Dec.map Pointwise-≡↔≡ (decidable ℕ._≟_ nf₁ nf₂)
64 where open Pointwise using (Pointwise-≡↔≡; decidable)
65
66 ------------------------------------------------------------------------
67 -- Constructions on normal forms
68
69 -- The empty bag.
70
71 empty : Normal n
72 empty = replicate _ 0
73
74 -- A singleton bag.
75
76 singleton : (i : Fin n) → Normal n
77 singleton zero = 1 ∷ empty
78 singleton (suc i) = 0 ∷ singleton i
79
80 -- The composition of normal forms.
81 infixr 10 _•_
82
83 _•_ : (v w : Normal n) → Normal n
84 _•_ = zipWith _+_
85
86 ------------------------------------------------------------------------
87 -- Correctness of the constructions on normal forms
88
89 -- The empty bag stands for the unit ε.
90
91 empty-correct : (ρ : Env n) → ⟦ empty ⟧⇓ ρ ≈ ε
92 empty-correct [] = refl
93 empty-correct (a ∷ ρ) = begin
94 ε ∙ ⟦ empty ⟧⇓ ρ ≈⟨ identityˡ _ ⟩
95 ⟦ empty ⟧⇓ ρ ≈⟨ empty-correct ρ ⟩
96 ε ∎
97
98 -- The singleton bag stands for a single variable.
99
100 singleton-correct : (x : Fin n) (ρ : Env n) →
101 ⟦ singleton x ⟧⇓ ρ ≈ lookup ρ x
102 singleton-correct zero (x ∷ ρ) = begin
103 (1 × x) ∙ ⟦ empty ⟧⇓ ρ ≈⟨ ∙-congʳ (×-homo-1 _) ⟩
104 x ∙ ⟦ empty ⟧⇓ ρ ≈⟨ ∙-congˡ (empty-correct ρ) ⟩
105 x ∙ ε ≈⟨ identityʳ _ ⟩
106 x ∎
107 singleton-correct (suc x) (m ∷ ρ) = begin
108 ε ∙ ⟦ singleton x ⟧⇓ ρ ≈⟨ identityˡ _ ⟩
109 ⟦ singleton x ⟧⇓ ρ ≈⟨ singleton-correct x ρ ⟩
110 lookup ρ x ∎
111
112 -- Normal form composition corresponds to the composition of the monoid.
113
114 comp-correct : ∀ v w (ρ : Env n) →
115 ⟦ v • w ⟧⇓ ρ ≈ (⟦ v ⟧⇓ ρ ∙ ⟦ w ⟧⇓ ρ)
116 comp-correct [] [] _ = sym (identityˡ _)
117 comp-correct (l ∷ v) (m ∷ w) (a ∷ ρ) = begin
118 ((l + m) × a) ∙ ⟦ v • w ⟧⇓ ρ ≈⟨ ∙-congʳ (×-homo-+ a l m) ⟩
119 (l × a) ∙ (m × a) ∙ ⟦ v • w ⟧⇓ ρ ≈⟨ ∙-congˡ (comp-correct v w ρ) ⟩
120 (l × a) ∙ (m × a) ∙ (⟦ v ⟧⇓ ρ ∙ ⟦ w ⟧⇓ ρ) ≈⟨ interchange _ _ _ _ ⟩
121 ⟦ l ∷ v ⟧⇓ (a ∷ ρ) ∙ ⟦ m ∷ w ⟧⇓ (a ∷ ρ) ∎
122
123 ------------------------------------------------------------------------
124 -- Normalization
125
126 -- A normaliser.
127
128 normalise : Expr n → Normal n
129 normalise (var x) = singleton x
130 normalise id = empty
131 normalise (e₁ ⊕ e₂) = normalise e₁ • normalise e₂
132
133 -- The normaliser preserves the semantics of the expression.
134
135 correct : ∀ e ρ → ⟦ normalise {n = n} e ⟧⇓ ρ ≈ ⟦ e ⟧ ρ
136 correct (var x) ρ = singleton-correct x ρ
137 correct id ρ = empty-correct ρ
138 correct (e₁ ⊕ e₂) ρ = begin
139 ⟦ normalise e₁ • normalise e₂ ⟧⇓ ρ
140 ≈⟨ comp-correct (normalise e₁) (normalise e₂) ρ ⟩
141 ⟦ normalise e₁ ⟧⇓ ρ ∙ ⟦ normalise e₂ ⟧⇓ ρ
142 ≈⟨ ∙-cong (correct e₁ ρ) (correct e₂ ρ) ⟩
143 ⟦ e₁ ⟧ ρ ∙ ⟦ e₂ ⟧ ρ
144 ∎
145
146
147 ------------------------------------------------------------------------
148 -- DEPRECATED NAMES
149 ------------------------------------------------------------------------
150 -- Please use the new names as continuing support for the old names is
151 -- not guaranteed.
152
153 -- Version 2.2
154
155 sg = singleton
156 {-# WARNING_ON_USAGE sg
157 "Warning: sg was deprecated in v2.2.
158 Please use singleton instead."
159 #-}
160 sg-correct = singleton-correct
161 {-# WARNING_ON_USAGE sg-correct
162 "Warning: sg-correct was deprecated in v2.2.
163 Please use singleton-correct instead."
164 #-}