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