-
Notifications
You must be signed in to change notification settings - Fork 239
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
lemastero
wants to merge
7
commits into
agda:master
Choose a base branch
from
lemastero:rational-ringsolver
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
673f120
add Data/Rational/Unnormalised/Tactic/RingSolver
12af5a6
add Data.Rational.Tactic.RingSolver
f52de13
update changelog - add RingSolver
c571e20
add README.Data.Rational
61f9a13
add README.Data.Rational.Unnormalised
26bdf87
WIP solver example in README.Data.Rational.Unnormalised
04928e9
WIP solver example in README.Data.Rational
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- Some examples showing where the rational numbers and some related | ||
-- operations and properties are defined, and how they can be used | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible #-} | ||
|
||
module README.Data.Rational where | ||
|
||
-- The rational numbers and various arithmetic operations are defined in | ||
-- Data.Rational. | ||
|
||
open import Data.Integer using (+_) | ||
open import Data.Rational | ||
open import Data.Rational.Properties | ||
|
||
1/4 : ℚ | ||
1/4 = + 1 / 4 | ||
|
||
3/4 : ℚ | ||
3/4 = + 3 / 4 | ||
|
||
-- Some binary operators are also defined, including addition, | ||
-- subtraction and multiplication. | ||
|
||
expr : ℚ | ||
expr = (1/4 + ½) * 1ℚ - 0ℚ | ||
|
||
-- We can use PropositionalEquality with rational numbers | ||
|
||
open import Relation.Binary.PropositionalEquality -- using (_≡_; refl) | ||
|
||
eqEx : expr ≡ 3/4 | ||
eqEx = refl | ||
|
||
-- or use equality defined for rational numbers | ||
|
||
eqEx' : expr ≃ 3/4 | ||
eqEx' = *≡* refl | ||
|
||
-- we can automaticaly prove equations using RingSolver | ||
|
||
open import Data.Rational.Tactic.RingSolver | ||
|
||
lemma : ∀ (x y : ℚ) → x + y + 1/4 + ½ ≃ 3/4 + y + x | ||
{- | ||
Malformed call to solve.Expected target type to be like: ∀ x y → x + y ≈ y + x.Instead: _19 | ||
when checking that the expression unquote solve-∀ has type _19 | ||
-} | ||
lemma = {! solve-∀ !} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- Some examples showing where the unnormalised rational numbers and some | ||
-- related operations and properties are defined, and how they can be used | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible #-} | ||
|
||
module README.Data.Rational.Unnormalised where | ||
|
||
-- The rational numbers and various arithmetic operations are defined in | ||
-- Data.Rational. | ||
|
||
open import Data.Integer using (+_) | ||
open import Data.Rational.Unnormalised | ||
open import Data.Rational.Unnormalised.Properties | ||
open import Relation.Binary.PropositionalEquality using (_≡_; refl) | ||
|
||
1/4 : ℚᵘ | ||
1/4 = + 1 / 4 | ||
|
||
3/4 : ℚᵘ | ||
3/4 = + 3 / 4 | ||
|
||
-- Some binary operators are also defined, including addition, | ||
-- subtraction and multiplication. | ||
|
||
expr : ℚᵘ | ||
expr = (1/4 + ½) * 1ℚᵘ - 0ℚᵘ | ||
|
||
-- We can use defined for rational numbers | ||
|
||
expr2 : expr ≃ 3/4 | ||
expr2 = *≡* refl | ||
|
||
-- We can automatically proove equations using Ring | ||
|
||
open import Data.Rational.Unnormalised.Tactic.RingSolver | ||
|
||
lemma₁ : ∀ (x y : ℚᵘ) → (x + y) ≡ (y + x) -- TODO should we use ≃ | ||
{- | ||
TODO fails with: | ||
Malformed call to solve.Expected target type to be like: ∀ x y → x + y ≈ y + x.Instead: _25 | ||
when checking that the expression unquote solve-∀ has type _25 | ||
-} | ||
lemma₁ = {! solve-∀ !} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
macro | ||
solve-∀ : Term → TC ⊤ | ||
solve-∀ = Solver.solve-∀-macro (quote ring) | ||
|
||
macro | ||
solve : Term → Term → TC ⊤ | ||
solve n = Solver.solve-macro n (quote ring) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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) inData.Rational.Properties
.Suggested name for such a refactored
f
:isZero-weakly-decidable
?That way, the
import
s for your solver modules then become very much simplified, in favour of those that are already currently used by theRational.*
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.