Skip to content

Commit 85a5475

Browse files
dancewithheartlemastero
andauthored
Add tactic-style ring solvers for rational and unnormalised rational numbers (#2965)
* add Data/Rational/Unnormalised/Tactic/RingSolver * add Data.Rational.Tactic.RingSolver * update changelog - tactic ring solvers for rational numbers in Changelog * add README.Data.Rational * add README.Data.Rational.Unnormalised * solver example in README.Data.Rational.Unnormalised * solver example in README.Data.Rational * property 0≡?-weak : (p : ℚ) → Maybe (0ℚ ≡ p) moved from RingSolver * 0≃?-weak : (p : ℚᵘ) → Maybe (0ℚᵘ ≃ p) moved from RingSolver * Fix examples: Use _≡_ for Data.Rational.Tactic.RingSolver, and _≃_ for Data.Rational.Unnormalised.Tactic.RingSolver * Use matching unnormalised constants in README solver example --------- Co-authored-by: lemastero <piotr.paradzinski@iohk.io>
1 parent eafa834 commit 85a5475

7 files changed

Lines changed: 145 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ Deprecated names
139139
New modules
140140
-----------
141141

142+
* Added tactic ring solvers for rational numbers (issue #1879):
143+
`Data.Rational.Tactic.RingSolver`,
144+
`Data.Rational.Unnormalised.Tactic.RingSolver`.
145+
142146
* `Algebra.Construct.Sub.Group` for the definition of subgroups.
143147

144148
* `Algebra.Module.Construct.Sub.Bimodule` for the definition of subbimodules.

doc/README/Data/Rational.agda

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Some examples showing where the rational numbers and some related
5+
-- operations and properties are defined, and how they can be used
6+
------------------------------------------------------------------------
7+
8+
{-# OPTIONS --cubical-compatible #-}
9+
10+
module README.Data.Rational where
11+
12+
open import Data.Integer using (+_)
13+
open import Data.Rational
14+
open import Data.Rational.Properties
15+
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
16+
17+
1/4 :
18+
1/4 = + 1 / 4
19+
20+
3/4 :
21+
3/4 = + 3 / 4
22+
23+
expr :
24+
expr = (1/4 + ½) * 1ℚ - 0ℚ
25+
26+
eqEx : expr ≡ 3/4
27+
eqEx = refl
28+
29+
open import Data.Rational.Tactic.RingSolver
30+
31+
lemma : (x y : ℚ) x + y + 1/4 + ½ ≡ 3/4 + y + x
32+
lemma = solve-∀
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module README.Data.Rational.Unnormalised where
2+
3+
open import Data.Integer using (+_)
4+
open import Data.Rational.Unnormalised
5+
open import Data.Rational.Unnormalised.Properties
6+
open import Relation.Binary.PropositionalEquality using (refl)
7+
8+
1/4 : ℚᵘ
9+
1/4 = + 1 / 4
10+
11+
3/4 : ℚᵘ
12+
3/4 = + 3 / 4
13+
14+
6/8 : ℚᵘ
15+
6/8 = + 6 / 8
16+
17+
expr : ℚᵘ
18+
expr = (1/4 + ½) * 1ℚᵘ - 0ℚᵘ
19+
20+
eqEx : expr ≃ 3/4
21+
eqEx = *≡* refl
22+
23+
open import Data.Rational.Unnormalised.Tactic.RingSolver
24+
25+
lemma₁ : (x y : ℚᵘ) x + y + 1/4 + ½ ≃ 6/8 + y + x
26+
lemma₁ = solve-∀

src/Data/Rational/Properties.agda

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import Algebra.Lattice.Morphism.LatticeMonomorphism as LatticeMonomorphisms
2727
import Algebra.Properties.CommutativeSemigroup as CommSemigroupProperties
2828
import Algebra.Properties.Group as GroupProperties
2929
open import Data.Bool.Base using (T; true; false)
30+
open import Data.Maybe.Base using (Maybe; just; nothing)
3031
open import Data.Integer.Base as ℤ using (ℤ; +_; -[1+_]; +[1+_]; +0; 0ℤ; 1ℤ; _◃_)
3132
open import Data.Integer.Coprimality using (coprime-divisor)
3233
import Data.Integer.Properties as ℤ
@@ -190,6 +191,11 @@ drop-*≡* (*≡* eq) = eq
190191
p≡0⇒↥p≡0 : p p ≡ 0ℚ ↥ p ≡ 0ℤ
191192
p≡0⇒↥p≡0 p refl = refl
192193

194+
0≡?-weak : (p : ℚ) Maybe (0ℚ ≡ p)
195+
0≡?-weak p with ↥ p ℤ.≟ 0ℤ
196+
... | yes ↥p≡0 = just (sym (↥p≡0⇒p≡0 p ↥p≡0))
197+
... | no _ = nothing
198+
193199
↥p≡↥q≡0⇒p≡q : p q ↥ p ≡ 0ℤ ↥ q ≡ 0ℤ p ≡ q
194200
↥p≡↥q≡0⇒p≡q p q ↥p≡0 ↥q≡0 = trans (↥p≡0⇒p≡0 p ↥p≡0) (sym (↥p≡0⇒p≡0 q ↥q≡0))
195201

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Automatic solvers for equations over rationals
5+
------------------------------------------------------------------------
6+
7+
-- See README.Tactic.RingSolver for examples of how to use this solver
8+
9+
{-# OPTIONS --cubical-compatible --safe #-}
10+
11+
module Data.Rational.Tactic.RingSolver where
12+
13+
open import Agda.Builtin.Reflection using (Term; TC)
14+
open import Data.Rational.Properties using (+-*-commutativeRing; 0≡?-weak)
15+
open import Level using (0ℓ)
16+
open import Data.Unit.Base using (⊤)
17+
18+
import Tactic.RingSolver as Solver using (solve-macro; solve-∀-macro)
19+
import Tactic.RingSolver.Core.AlmostCommutativeRing as ACR
20+
21+
------------------------------------------------------------------------
22+
-- A module for automatically solving propositional equalities
23+
-- containing _+_ and _*_
24+
25+
ring : ACR.AlmostCommutativeRing 0ℓ 0ℓ
26+
ring = ACR.fromCommutativeRing
27+
+-*-commutativeRing
28+
0≡?-weak
29+
30+
macro
31+
solve-∀ : Term TC ⊤
32+
solve-∀ = Solver.solve-∀-macro (quote ring)
33+
34+
macro
35+
solve : Term Term TC ⊤
36+
solve n = Solver.solve-macro n (quote ring)

src/Data/Rational/Unnormalised/Properties.agda

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ open import Algebra.Construct.NaturalChoice.Base
2828
import Algebra.Construct.NaturalChoice.MinMaxOp as MinMaxOp
2929
import Algebra.Lattice.Construct.NaturalChoice.MinMaxOp as LatticeMinMaxOp
3030
open import Data.Bool.Base using (T; true; false)
31+
open import Data.Maybe.Base using (Maybe; just; nothing)
3132
open import Data.Nat.Base as ℕ using (suc; pred)
3233
import Data.Nat.Properties as ℕ
3334
using (≤-refl; +-comm; +-identityʳ; +-assoc
@@ -185,6 +186,10 @@ p≃0⇒↥p≡0 p (*≡* eq) = begin
185186
↥p≡↥q≡0⇒p≃q : p q ↥ p ≡ 0ℤ ↥ q ≡ 0ℤ p ≃ q
186187
↥p≡↥q≡0⇒p≃q p q ↥p≡0 ↥q≡0 = ≃-trans (↥p≡0⇒p≃0 p ↥p≡0) (≃-sym (↥p≡0⇒p≃0 _ ↥q≡0))
187188

189+
0≃?-weak : (p : ℚᵘ) Maybe (0ℚᵘ ≃ p)
190+
0≃?-weak p with ↥ p ℤ.≟ 0ℤ
191+
... | yes ↥p≡0 = just (≃-sym (↥p≡0⇒p≃0 p ↥p≡0))
192+
... | no _ = nothing
188193

189194
------------------------------------------------------------------------
190195
-- Properties of -_
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Automatic solvers for equations over unnormalised rationals
5+
------------------------------------------------------------------------
6+
7+
-- See README.Tactic.RingSolver for examples of how to use this solver
8+
9+
{-# OPTIONS --cubical-compatible --safe #-}
10+
11+
module Data.Rational.Unnormalised.Tactic.RingSolver where
12+
13+
open import Agda.Builtin.Reflection using (Term; TC)
14+
open import Data.Rational.Unnormalised.Properties using (+-*-commutativeRing; 0≃?-weak)
15+
open import Level using (0ℓ)
16+
open import Data.Unit.Base using (⊤)
17+
18+
import Tactic.RingSolver as Solver using (solve-∀-macro; solve-macro)
19+
import Tactic.RingSolver.Core.AlmostCommutativeRing as ACR
20+
21+
------------------------------------------------------------------------
22+
-- A module for automatically solving propositional equivalences
23+
-- containing _+_ and _*_
24+
25+
ring : ACR.AlmostCommutativeRing 0ℓ 0ℓ
26+
ring = ACR.fromCommutativeRing
27+
+-*-commutativeRing
28+
0≃?-weak
29+
30+
macro
31+
solve-∀ : Term TC ⊤
32+
solve-∀ = Solver.solve-∀-macro (quote ring)
33+
34+
macro
35+
solve : Term Term TC ⊤
36+
solve n = Solver.solve-macro n (quote ring)

0 commit comments

Comments
 (0)