Debian Codegraph @

src/Algebra/Solver/Monoid/Solver.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 import Algebra.Solver.Monoid.Expression as Expression
11
12 module Algebra.Solver.Monoid.Solver {a c ℓ} (M : Monoid c ℓ)
13 (open Monoid M using (rawMonoid; setoid; _≈_))
14 (open Expression rawMonoid using (Expr; Env; var; ⟦_⟧; NormalAPI))
15 (N : NormalAPI a) where
16
17 open import Data.Maybe.Base as Maybe
18 using (Maybe; From-just; from-just)
19 open import Data.Nat.Base using (ℕ)
20 open import Function.Base using (_∘_; _$_)
21 open import Relation.Binary.Consequences using (dec⇒weaklyDec)
22 open import Relation.Binary.PropositionalEquality.Core using (_≡_; cong)
23 import Relation.Binary.Reflection as Reflection
24
25 open NormalAPI N
26
27 private
28 variable
29 n : ℕ
30 module
31 R = Reflection setoid var ⟦_⟧ (⟦_⟧⇓ ∘ normalise) correct
32
33
34 ------------------------------------------------------------------------
35 -- Proof procedures
36
37 -- Re-export the existing solver machinery from `Reflection`
38
39 open R public
40 using (solve; _⊜_)
41
42 -- We can also give a sound, but not necessarily complete, procedure
43 -- for determining if two expressions have the same semantics.
44
45 prove′ : ∀ e₁ e₂ → Maybe ((ρ : Env n) → ⟦ e₁ ⟧ ρ ≈ ⟦ e₂ ⟧ ρ)
46 prove′ e₁ e₂ = Maybe.map lemma $ dec⇒weaklyDec _≟_ (normalise e₁) (normalise e₂)
47 where
48 open import Relation.Binary.Reasoning.Setoid setoid
49 lemma : normalise e₁ ≡ normalise e₂ → ∀ ρ → ⟦ e₁ ⟧ ρ ≈ ⟦ e₂ ⟧ ρ
50 lemma eq ρ = R.prove ρ e₁ e₂ $ begin
51 ⟦ normalise e₁ ⟧⇓ ρ ≡⟨ cong (λ e → ⟦ e ⟧⇓ ρ) eq ⟩
52 ⟦ normalise e₂ ⟧⇓ ρ ∎
53
54 -- This procedure can then be combined with from-just.
55
56 prove : ∀ n (e₁ e₂ : Expr n) → From-just (prove′ e₁ e₂)
57 prove _ e₁ e₂ = from-just $ prove′ e₁ e₂