Skip to content

Commit 5827d71

Browse files
mwageringelMatthewDaggitt
authored andcommitted
Relax decidability module parameter of Algebra.Solver.Ring (#511)
1 parent 38409f0 commit 5827d71

File tree

13 files changed

+132
-169
lines changed

13 files changed

+132
-169
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ Splitting up `Data.Maybe` into the standard hierarchy.
4343
Eq-isDecEquivalence ↦ isDecEquivalence
4444
```
4545

46+
#### Refactoring of ring solvers
47+
48+
* In the ring solvers below, the `Decidable` module parameter was replaced by a
49+
strictly weaker parameter `Relation.Binary.Core.WeaklyDecidable`, which makes
50+
the ring solvers more versatile.
51+
```
52+
Algebra.Solver.Ring
53+
Algebra.Solver.Ring.NaturalCoefficients
54+
```
55+
56+
* Added `_:×_` operator to `Algebra.Solver.Ring`.
57+
58+
* Created a module `Algebra.Solver.Ring.NaturalCoefficients.Default` that
59+
instantiates the solver for any `CommutativeSemiring`.
60+
61+
#### Other
62+
63+
* Moved `_≟_` from `Data.Bool.Base` to `Data.Bool.Properties`. Backwards
64+
compatibility has been (nearly completely) preserved by having `Data.Bool`
65+
publicly re-export `_≟_`.
66+
4667
Other major changes
4768
-------------------
4869

src/Algebra/Solver/IdempotentCommutativeMonoid.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
open import Algebra
1010

11-
open import Data.Bool.Base as Bool using (Bool; true; false; if_then_else_; _∨_)
11+
open import Data.Bool as Bool using (Bool; true; false; if_then_else_; _∨_)
1212
open import Data.Fin using (Fin; zero; suc)
1313
open import Data.Maybe as Maybe
1414
using (Maybe; decToMaybe; From-just; from-just)

src/Algebra/Solver/Ring.agda

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
open import Algebra
1313
open import Algebra.Solver.Ring.AlmostCommutativeRing
14-
15-
open import Relation.Binary
14+
open import Relation.Binary.Core using (WeaklyDecidable)
1615

1716
module Algebra.Solver.Ring
1817
{r₁ r₂ r₃}
1918
(Coeff : RawRing r₁) -- Coefficient "ring".
2019
(R : AlmostCommutativeRing r₂ r₃) -- Main "ring".
2120
(morphism : Coeff -Raw-AlmostCommutative⟶ R)
22-
(_coeff≟_ : Decidable (Induced-equivalence morphism))
21+
(_coeff≟_ : WeaklyDecidable (Induced-equivalence morphism))
2322
where
2423

2524
open import Algebra.Solver.Ring.Lemmas Coeff R morphism
@@ -40,11 +39,12 @@ import Relation.Binary.Reflection as Reflection
4039
open import Data.Nat.Base using (ℕ; suc; zero)
4140
open import Data.Fin using (Fin; zero; suc)
4241
open import Data.Vec using (Vec; []; _∷_; lookup)
42+
open import Data.Maybe.Base using (just; nothing)
4343
open import Function
4444
open import Level using (_⊔_)
4545

4646
infix 9 :-_ -H_ -N_
47-
infixr 9 _:^_ _^N_
47+
infixr 9 _:×_ _:^_ _^N_
4848
infix 8 _*x+_ _*x+HN_ _*x+H_
4949
infixl 8 _:*_ _*N_ _*H_ _*NH_ _*HN_
5050
infixl 7 _:+_ _:-_ _+H_ _+N_
@@ -77,6 +77,10 @@ _:*_ = op [*]
7777
_:-_ : {n} Polynomial n Polynomial n Polynomial n
7878
x :- y = x :+ :- y
7979

80+
_:×_ : {n} Polynomial n Polynomial n
81+
zero :× p = con C.0#
82+
suc m :× p = p :+ m :× p
83+
8084
-- Semantics.
8185

8286
sem : Op Op₂ Carrier
@@ -161,24 +165,24 @@ mutual
161165

162166
mutual
163167

164-
-- Equality is decidable.
168+
-- Equality is weakly decidable.
165169

166-
_≟H_ : {n} Decidable (_≈H_ {n = n})
167-
∅ ≟H ∅ = yes
168-
∅ ≟H (_ *x+ _) = no λ()
169-
(_ *x+ _) ≟H ∅ = no λ()
170+
_≟H_ : {n} WeaklyDecidable (_≈H_ {n = n})
171+
∅ ≟H ∅ = just
172+
∅ ≟H (_ *x+ _) = nothing
173+
(_ *x+ _) ≟H ∅ = nothing
170174
(p₁ *x+ c₁) ≟H (p₂ *x+ c₂) with p₁ ≟H p₂ | c₁ ≟N c₂
171-
... | yes p₁≈p₂ | yes c₁≈c₂ = yes (p₁≈p₂ *x+ c₁≈c₂)
172-
... | _ | no c₁≉c₂ = no λ { (_ *x+ c₁≈c₂) c₁≉c₂ c₁≈c₂ }
173-
... | no p₁≉p₂ | _ = no λ { (p₁≈p₂ *x+ _) p₁≉p₂ p₁≈p₂ }
175+
... | just p₁≈p₂ | just c₁≈c₂ = just (p₁≈p₂ *x+ c₁≈c₂)
176+
... | _ | nothing = nothing
177+
... | nothing | _ = nothing
174178

175-
_≟N_ : {n} Decidable (_≈N_ {n = n})
179+
_≟N_ : {n} WeaklyDecidable (_≈N_ {n = n})
176180
con c₁ ≟N con c₂ with c₁ coeff≟ c₂
177-
... | yes c₁≈c₂ = yes (con c₁≈c₂)
178-
... | no c₁≉c₂ = no λ { (con c₁≈c₂) c₁≉c₂ c₁≈c₂}
181+
... | just c₁≈c₂ = just (con c₁≈c₂)
182+
... | nothing = nothing
179183
poly p₁ ≟N poly p₂ with p₁ ≟H p₂
180-
... | yes p₁≈p₂ = yes (poly p₁≈p₂)
181-
... | no p₁≉p₂ = no λ { (poly p₁≈p₂) p₁≉p₂ p₁≈p₂ }
184+
... | just p₁≈p₂ = just (poly p₁≈p₂)
185+
... | nothing = nothing
182186

183187
mutual
184188

@@ -226,8 +230,8 @@ mutual
226230
_*x+HN_ : {n} HNF (suc n) Normal n HNF (suc n)
227231
(p *x+ c′) *x+HN c = (p *x+ c′) *x+ c
228232
∅ *x+HN c with c ≟N 0N
229-
... | yes c≈0 =
230-
... | no c≉0 = ∅ *x+ c
233+
... | just c≈0 =
234+
... | nothing = ∅ *x+ c
231235

232236
mutual
233237

@@ -254,14 +258,14 @@ mutual
254258
_*NH_ : {n} Normal n HNF (suc n) HNF (suc n)
255259
c *NH ∅ =
256260
c *NH (p *x+ c′) with c ≟N 0N
257-
... | yes c≈0 =
258-
... | no c≉0 = (c *NH p) *x+ (c *N c′)
261+
... | just c≈0 =
262+
... | nothing = (c *NH p) *x+ (c *N c′)
259263

260264
_*HN_ : {n} HNF (suc n) Normal n HNF (suc n)
261265
∅ *HN c =
262266
(p *x+ c′) *HN c with c ≟N 0N
263-
... | yes c≈0 =
264-
... | no c≉0 = (p *HN c) *x+ (c′ *N c)
267+
... | just c≈0 =
268+
... | nothing = (p *HN c) *x+ (c′ *N c)
265269

266270
_*H_ : {n} HNF (suc n) HNF (suc n) HNF (suc n)
267271
∅ *H _ =
@@ -342,17 +346,17 @@ normalise (:- t) = -N normalise t
342346
ρ ⟦ p *x+HN c ⟧H ρ ≈ ⟦ p *x+ c ⟧H ρ
343347
*x+HN≈*x+ (p *x+ c′) c ρ = refl
344348
*x+HN≈*x+ ∅ c (x ∷ ρ) with c ≟N 0N
345-
... | yes c≈0 = begin
349+
... | just c≈0 = begin
346350
0# ≈⟨ 0≈⟦0⟧ c≈0 ρ ⟩
347351
⟦ c ⟧N ρ ≈⟨ sym $ lemma₆ _ _ ⟩
348352
0# * x + ⟦ c ⟧N ρ ∎
349-
... | no c≉0 = refl
353+
... | nothing = refl
350354

351355
∅*x+HN-homo : {n} (c : Normal n) x ρ
352356
⟦ ∅ *x+HN c ⟧H (x ∷ ρ) ≈ ⟦ c ⟧N ρ
353357
∅*x+HN-homo c x ρ with c ≟N 0N
354-
... | yes c≈0 = 0≈⟦0⟧ c≈0 ρ
355-
... | no c≉0 = lemma₆ _ _
358+
... | just c≈0 = 0≈⟦0⟧ c≈0 ρ
359+
... | nothing = lemma₆ _ _
356360

357361
mutual
358362

@@ -396,11 +400,11 @@ mutual
396400
⟦ c *NH p ⟧H (x ∷ ρ) ≈ ⟦ c ⟧N ρ * ⟦ p ⟧H (x ∷ ρ)
397401
*NH-homo c ∅ x ρ = sym (*-zeroʳ _)
398402
*NH-homo c (p *x+ c′) x ρ with c ≟N 0N
399-
... | yes c≈0 = begin
403+
... | just c≈0 = begin
400404
0# ≈⟨ sym (*-zeroˡ _) ⟩
401405
0# * (⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) ≈⟨ 0≈⟦0⟧ c≈0 ρ ⟨ *-cong ⟩ refl ⟩
402406
⟦ c ⟧N ρ * (⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) ∎
403-
... | no c≉0 = begin
407+
... | nothing = begin
404408
⟦ c *NH p ⟧H (x ∷ ρ) * x + ⟦ c *N c′ ⟧N ρ ≈⟨ (*NH-homo c p x ρ ⟨ *-cong ⟩ refl) ⟨ +-cong ⟩ *N-homo c c′ ρ ⟩
405409
(⟦ c ⟧N ρ * ⟦ p ⟧H (x ∷ ρ)) * x + (⟦ c ⟧N ρ * ⟦ c′ ⟧N ρ) ≈⟨ lemma₃ _ _ _ _ ⟩
406410
⟦ c ⟧N ρ * (⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) ∎
@@ -410,11 +414,11 @@ mutual
410414
⟦ p *HN c ⟧H (x ∷ ρ) ≈ ⟦ p ⟧H (x ∷ ρ) * ⟦ c ⟧N ρ
411415
*HN-homo ∅ c x ρ = sym (*-zeroˡ _)
412416
*HN-homo (p *x+ c′) c x ρ with c ≟N 0N
413-
... | yes c≈0 = begin
417+
... | just c≈0 = begin
414418
0# ≈⟨ sym (*-zeroʳ _) ⟩
415419
(⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) * 0# ≈⟨ refl ⟨ *-cong ⟩ 0≈⟦0⟧ c≈0 ρ ⟩
416420
(⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) * ⟦ c ⟧N ρ ∎
417-
... | no c≉0 = begin
421+
... | nothing = begin
418422
⟦ p *HN c ⟧H (x ∷ ρ) * x + ⟦ c′ *N c ⟧N ρ ≈⟨ (*HN-homo p c x ρ ⟨ *-cong ⟩ refl) ⟨ +-cong ⟩ *N-homo c′ c ρ ⟩
419423
(⟦ p ⟧H (x ∷ ρ) * ⟦ c ⟧N ρ) * x + (⟦ c′ ⟧N ρ * ⟦ c ⟧N ρ) ≈⟨ lemma₂ _ _ _ _ ⟩
420424
(⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) * ⟦ c ⟧N ρ ∎

src/Algebra/Solver/Ring/NaturalCoefficients.agda

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,21 @@
77

88
open import Algebra
99
import Algebra.Operations.Semiring as SemiringOps
10-
open import Relation.Nullary
10+
open import Data.Maybe.Base using (Maybe; just; nothing; map)
1111

1212
module Algebra.Solver.Ring.NaturalCoefficients
1313
{r₁ r₂}
1414
(R : CommutativeSemiring r₁ r₂)
1515
(dec : let open CommutativeSemiring R
1616
open SemiringOps semiring in
17-
m n Dec (m × 1# ≈ n × 1#)) where
17+
m n Maybe (m × 1# ≈ n × 1#)) where
1818

1919
import Algebra.Solver.Ring
2020
open import Algebra.Solver.Ring.AlmostCommutativeRing
2121
open import Data.Nat.Base as ℕ
2222
open import Data.Product using (module Σ)
2323
open import Function
2424
import Relation.Binary.EqReasoning
25-
import Relation.Nullary.Decidable as Dec
2625

2726
open CommutativeSemiring R
2827
open SemiringOps semiring
@@ -60,8 +59,8 @@ private
6059

6160
-- Equality of certain expressions can be decided.
6261

63-
dec′ : m n Dec (m ×′ 1# ≈ n ×′ 1#)
64-
dec′ m n = Dec.map to from (dec m n)
62+
dec′ : m n Maybe (m ×′ 1# ≈ n ×′ 1#)
63+
dec′ m n = map to (dec m n)
6564
where
6665
to : m × 1# ≈ n × 1# m ×′ 1# ≈ n ×′ 1#
6766
to m≈n = begin
@@ -70,13 +69,6 @@ private
7069
n × 1# ≈⟨ ×≈×′ n 1# ⟩
7170
n ×′ 1# ∎
7271

73-
from : m ×′ 1# ≈ n ×′ 1# m × 1# ≈ n × 1#
74-
from m≈n = begin
75-
m × 1# ≈⟨ ×≈×′ m 1# ⟩
76-
m ×′ 1# ≈⟨ m≈n ⟩
77-
n ×′ 1# ≈⟨ sym $ ×≈×′ n 1# ⟩
78-
n × 1# ∎
79-
8072
-- The instantiation.
8173

8274
open Algebra.Solver.Ring _ _ homomorphism dec′ public
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Instantiates the natural coefficients ring solver, using coefficient
5+
-- equality induced by ℕ.
6+
--
7+
-- This is sufficient for proving equalities that are independent of the
8+
-- characteristic. In particular, this is enough for equalities in rings of
9+
-- characteristic 0.
10+
------------------------------------------------------------------------
11+
12+
open import Algebra
13+
14+
module Algebra.Solver.Ring.NaturalCoefficients.Default
15+
{r₁ r₂} (R : CommutativeSemiring r₁ r₂) where
16+
17+
import Algebra.Operations.Semiring as SemiringOps
18+
open import Data.Maybe.Base using (Maybe; map)
19+
open import Data.Nat using (_≟_)
20+
open import Relation.Binary.Consequences using (dec⟶weaklyDec)
21+
import Relation.Binary.PropositionalEquality as P
22+
23+
open CommutativeSemiring R
24+
open SemiringOps semiring
25+
26+
private
27+
dec : m n Maybe (m × 1# ≈ n × 1#)
28+
dec m n = map (λ { P.refl refl }) (dec⟶weaklyDec _≟_ m n)
29+
30+
open import Algebra.Solver.Ring.NaturalCoefficients R dec public

src/Algebra/Solver/Ring/Simple.agda

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
open import Algebra.Solver.Ring.AlmostCommutativeRing
99
open import Relation.Binary
10+
open import Relation.Binary.Consequences using (dec⟶weaklyDec)
1011

1112
module Algebra.Solver.Ring.Simple
1213
{r₁ r₂} (R : AlmostCommutativeRing r₁ r₂)
@@ -15,4 +16,4 @@ module Algebra.Solver.Ring.Simple
1516

1617
open AlmostCommutativeRing R
1718
import Algebra.Solver.Ring as RS
18-
open RS rawRing R (-raw-almostCommutative⟶ R) _≟_ public
19+
open RS rawRing R (-raw-almostCommutative⟶ R) (dec⟶weaklyDec _≟_) public

src/Data/Bool.agda

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ open import Relation.Binary.PropositionalEquality as PropEq
1616

1717
open import Data.Bool.Base public
1818

19+
------------------------------------------------------------------------
20+
-- Publicly re-export queries
21+
22+
open import Data.Bool.Properties public
23+
using (_≟_)
24+
1925
------------------------------------------------------------------------
2026
-- Some properties
2127

src/Data/Bool/Base.agda

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ module Data.Bool.Base where
88
open import Data.Unit.Base using (⊤)
99
open import Data.Empty
1010
open import Relation.Nullary
11-
open import Relation.Binary.Core
12-
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
1311

1412
infixr 6 _∧_
1513
infixr 5 _∨_ _xor_
@@ -49,14 +47,3 @@ false ∨ b = b
4947
_xor_ : Bool Bool Bool
5048
true xor b = not b
5149
false xor b = b
52-
53-
------------------------------------------------------------------------
54-
-- Queries
55-
56-
infix 4 _≟_
57-
58-
_≟_ : Decidable {A = Bool} _≡_
59-
true ≟ true = yes refl
60-
false ≟ false = yes refl
61-
true ≟ false = no λ()
62-
false ≟ true = no λ()

src/Data/Bool/Properties.agda

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,35 @@
77
module Data.Bool.Properties where
88

99
open import Algebra
10-
open import Data.Bool
10+
open import Data.Bool.Base
1111
open import Data.Empty
1212
open import Data.Product
1313
open import Data.Sum
1414
open import Function
1515
open import Function.Equality using (_⟨$⟩_)
1616
open import Function.Equivalence
1717
using (_⇔_; equivalence; module Equivalence)
18+
open import Relation.Binary.Core using (Decidable)
1819
open import Relation.Binary.PropositionalEquality
1920
hiding ([_]; proof-irrelevance)
21+
open import Relation.Nullary using (yes; no)
2022
open import Relation.Unary using (Irrelevant)
2123

2224
open import Algebra.FunctionProperties (_≡_ {A = Bool})
2325
open import Algebra.Structures (_≡_ {A = Bool})
2426
open ≡-Reasoning
2527

28+
------------------------------------------------------------------------
29+
-- Queries
30+
31+
infix 4 _≟_
32+
33+
_≟_ : Decidable {A = Bool} _≡_
34+
true ≟ true = yes refl
35+
false ≟ false = yes refl
36+
true ≟ false = no λ()
37+
false ≟ true = no λ()
38+
2639
------------------------------------------------------------------------
2740
-- Properties of _∨_
2841

src/Data/Fin/Subset/Properties.agda

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import Algebra.Structures as AlgebraicStructures
1212
import Algebra.Properties.Lattice as L
1313
import Algebra.Properties.DistributiveLattice as DL
1414
import Algebra.Properties.BooleanAlgebra as BA
15-
open import Data.Bool.Base using (_≟_)
1615
open import Data.Bool.Properties
1716
open import Data.Fin using (Fin; suc; zero)
1817
open import Data.Fin.Subset

0 commit comments

Comments
 (0)