Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Add Rational RingSolver #2215

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,12 @@ New modules
Data.Rational.Unnormalised.Show
```

* Added RingSolver for Data.Rational (issue #1879):
```
Data.Rational.Tactic.RingSolver
Data.Rational.Unnormalised.Tactic.RingSolver
```

* Membership relations for maps and sets
```
Data.Tree.AVL.Map.Membership.Propositional
Expand Down
46 changes: 46 additions & 0 deletions src/Data/Rational/Tactic/RingSolver.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
------------------------------------------------------------------------
-- The Agda standard library
--
-- Automatic solvers for equations over rationals
------------------------------------------------------------------------

-- See README.Integer for examples of how to use this solver

{-# OPTIONS --cubical-compatible --safe #-}

module Data.Rational.Tactic.RingSolver where

open import Agda.Builtin.Reflection using (Term; TC)

open import Agda.Builtin.Int using (Int; negsuc; pos)
open import Data.Nat.Base using (zero; suc)
open import Data.Maybe.Base using (Maybe; just; nothing)
open import Data.Rational.Base using (ℚ; 0ℚ; mkℚ)
open import Data.Rational.Properties using (+-*-commutativeRing)
open import Level using (0ℓ)
open import Data.Unit using (⊤)
open import Relation.Binary.PropositionalEquality using (_≡_; refl)

import Tactic.RingSolver as Solver using (solve-macro; solve-∀-macro)
import Tactic.RingSolver.Core.AlmostCommutativeRing as ACR

------------------------------------------------------------------------
-- A module for automatically solving propositional equivalences
-- containing _+_ and _*_

ring : ACR.AlmostCommutativeRing 0ℓ 0ℓ
ring = ACR.fromCommutativeRing +-*-commutativeRing f
where
f : (x : ℚ) → Maybe (0ℚ ≡ x)
f (mkℚ (pos 0) 0 _) = just refl
f (mkℚ (pos 0) (suc _) _) = nothing
f (mkℚ (pos (suc _)) _ _) = nothing
f (mkℚ (negsuc _) _ _) = nothing
Comment on lines +34 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be strongly tempted to lift this out as a definition in Data.Rational.Base, or else as a property (cf. Relation.Binary.WeaklyDecidable, for which we currently lack analogues at arity 0, 1) in Data.Rational.Properties.

Suggested name for such a refactored f: isZero-weakly-decidable?

That way, the imports for your solver modules then become very much simplified, in favour of those that are already currently used by the Rational.* modules...

... and there's (potentially) a downstream benefit in being able to reuse the definition of the corresponding Unnormalised function in the definition of this one.


macro
solve-∀ : Term → TC ⊤
solve-∀ = Solver.solve-∀-macro (quote ring)

macro
solve : Term → Term → TC ⊤
solve n = Solver.solve-macro n (quote ring)
45 changes: 45 additions & 0 deletions src/Data/Rational/Unnormalised/Tactic/RingSolver.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
------------------------------------------------------------------------
-- The Agda standard library
--
-- Automatic solvers for equations over unnormalised rationals
------------------------------------------------------------------------

-- See README.Integer for examples of how to use this solver

{-# OPTIONS --cubical-compatible --safe #-}

module Data.Rational.Unnormalised.Tactic.RingSolver where

open import Agda.Builtin.Reflection

open import Agda.Builtin.Int using (Int; negsuc; pos)
open import Data.Nat.Base using (zero; suc)
open import Data.Maybe.Base using (Maybe; just; nothing)
open import Data.Rational.Unnormalised.Base using (ℚᵘ; 0ℚᵘ; _≃_; mkℚᵘ; *≡*)
open import Data.Rational.Unnormalised.Properties using (+-*-commutativeRing)
open import Level using (0ℓ)
open import Data.Unit using (⊤)
open import Relation.Binary.PropositionalEquality using (refl)

import Tactic.RingSolver as Solver
import Tactic.RingSolver.Core.AlmostCommutativeRing as ACR

------------------------------------------------------------------------
-- A module for automatically solving propositional equivalences
-- containing _+_ and _*_

ring : ACR.AlmostCommutativeRing 0ℓ 0ℓ
ring = ACR.fromCommutativeRing +-*-commutativeRing f
where
f : (x : ℚᵘ) → Maybe (0ℚᵘ ≃ x)
f (mkℚᵘ (pos zero) _) = just (*≡* refl)
f (mkℚᵘ (pos (suc _)) _) = nothing
f (mkℚᵘ (negsuc _) _) = nothing
Comment on lines +33 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.!


macro
solve-∀ : Term → TC ⊤
solve-∀ = Solver.solve-∀-macro (quote ring)

macro
solve : Term → Term → TC ⊤
solve n = Solver.solve-macro n (quote ring)