Debian Codegraph @

src/Algebra/Solver/Monoid/Normal.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- A solver for equations over monoids
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Algebra.Bundles using (Monoid)
10
11 module Algebra.Solver.Monoid.Normal {c ℓ} (M : Monoid c ℓ) where
12
13 open import Data.Fin.Base using (Fin)
14 import Data.Fin.Properties as Fin
15 open import Data.List.Base using (List; [] ; _∷_; _++_)
16 import Data.List.Relation.Binary.Equality.DecPropositional as ≋
17 open import Data.Nat.Base using (ℕ)
18 open import Data.Vec.Base using (lookup)
19 open import Function.Base using (_∘_; _$_)
20 open import Relation.Binary.Definitions using (DecidableEquality)
21 import Relation.Binary.Reasoning.Setoid as ≈-Reasoning
22 import Relation.Nullary.Decidable as Dec using (map; map′)
23
24 open Monoid M
25 open ≈-Reasoning setoid
26
27 private
28 variable
29 n : ℕ
30
31
32 ------------------------------------------------------------------------
33 -- Monoid expressions
34
35 open import Algebra.Solver.Monoid.Expression rawMonoid public
36
37 ------------------------------------------------------------------------
38 -- Normal forms
39
40 -- A normal form is a list of variables.
41
42 Normal : ℕ → Set _
43 Normal n = List (Fin n)
44
45 -- The semantics of a normal form.
46
47 ⟦_⟧⇓ : Normal n → Env n → Carrier
48 ⟦ [] ⟧⇓ ρ = ε
49 ⟦ x ∷ nf ⟧⇓ ρ = lookup ρ x ∙ ⟦ nf ⟧⇓ ρ
50
51 -- We can decide if two normal forms are /syntactically/ equal.
52
53 infix 5 _≟_
54
55 _≟_ : DecidableEquality (Normal n)
56 nf₁ ≟ nf₂ = Dec.map′ ≋⇒≡ ≡⇒≋ (nf₁ ≋? nf₂)
57 where open ≋ Fin._≟_ using (_≋?_; ≋⇒≡; ≡⇒≋)
58
59 -- A normaliser.
60
61 normalise : Expr n → Normal n
62 normalise (var x) = x ∷ []
63 normalise id = []
64 normalise (e₁ ⊕ e₂) = normalise e₁ ++ normalise e₂
65
66 -- The normaliser is homomorphic with respect to _++_/_∙_.
67
68 comp-correct : ∀ nf₁ nf₂ (ρ : Env n) →
69 ⟦ nf₁ ++ nf₂ ⟧⇓ ρ ≈ (⟦ nf₁ ⟧⇓ ρ ∙ ⟦ nf₂ ⟧⇓ ρ)
70 comp-correct [] nf₂ ρ = begin
71 ⟦ nf₂ ⟧⇓ ρ ≈⟨ identityˡ _ ⟨
72 ε ∙ ⟦ nf₂ ⟧⇓ ρ ∎
73 comp-correct (x ∷ nf₁) nf₂ ρ = begin
74 lookup ρ x ∙ ⟦ nf₁ ++ nf₂ ⟧⇓ ρ ≈⟨ ∙-congˡ (comp-correct nf₁ nf₂ ρ) ⟩
75 lookup ρ x ∙ (⟦ nf₁ ⟧⇓ ρ ∙ ⟦ nf₂ ⟧⇓ ρ) ≈⟨ assoc _ _ _ ⟨
76 lookup ρ x ∙ ⟦ nf₁ ⟧⇓ ρ ∙ ⟦ nf₂ ⟧⇓ ρ ∎
77
78 -- The normaliser preserves the semantics of the expression.
79
80 correct : ∀ e ρ → ⟦ normalise {n = n} e ⟧⇓ ρ ≈ ⟦ e ⟧ ρ
81 correct (var x) ρ = begin
82 lookup ρ x ∙ ε ≈⟨ identityʳ _ ⟩
83 lookup ρ x ∎
84 correct id ρ = begin
85 ε ∎
86 correct (e₁ ⊕ e₂) ρ = begin
87 ⟦ normalise e₁ ++ normalise e₂ ⟧⇓ ρ ≈⟨ comp-correct (normalise e₁) (normalise e₂) ρ ⟩
88 ⟦ normalise e₁ ⟧⇓ ρ ∙ ⟦ normalise e₂ ⟧⇓ ρ ≈⟨ ∙-cong (correct e₁ ρ) (correct e₂ ρ) ⟩
89 ⟦ e₁ ⟧ ρ ∙ ⟦ e₂ ⟧ ρ ∎