Skip to content

Commit 24c05d2

Browse files
MatthewDaggittandreasabel
authored andcommitted
Make reasoning modular by adding new Reasoning.Syntax module (#2152)
1 parent 7cdd4a5 commit 24c05d2

File tree

32 files changed

+856
-644
lines changed

32 files changed

+856
-644
lines changed

CHANGELOG.md

+28
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Bug-fixes
3333
in `Function.Construct.Composition` had their arguments in the wrong order. They have
3434
been flipped so they can actually be used as a composition operator.
3535

36+
* The combinators `_≃⟨_⟩_` and `_≃˘⟨_⟩_` in `Data.Rational.Properties.≤-Reasoning`
37+
now correctly accepts proofs of type `_≃_` instead of the previous proofs of type `_≡_`.
38+
3639
* The following operators were missing a fixity declaration, which has now
3740
been fixed -
3841
```
@@ -536,6 +539,10 @@ Non-backwards compatible changes
536539
3. Finally, if the above approaches are not viable then you may be forced to explicitly
537540
use `cong` combined with a lemma that proves the old reduction behaviour.
538541
542+
* Similarly, in order to prevent reduction, the equality `_≃_` in `Data.Rational.Base`
543+
has been made into a data type with the single constructor `*≡*`. The destructor `drop-*≡*`
544+
has been added to `Data.Rational.Properties`.
545+
539546
### Change to the definition of `LeftCancellative` and `RightCancellative`
540547
541548
* The definitions of the types for cancellativity in `Algebra.Definitions` previously
@@ -1222,6 +1229,15 @@ Major improvements
12221229

12231230
* In `Relation.Binary.Reasoning.Base.Triple`, added a new parameter `<-irrefl : Irreflexive _≈_ _<_`
12241231

1232+
### More modular design of equational reasoning.
1233+
1234+
* Have introduced a new module `Relation.Binary.Reasoning.Syntax` which exports
1235+
a range of modules containing pre-existing reasoning combinator syntax.
1236+
1237+
* This makes it possible to add new or rename existing reasoning combinators to a
1238+
pre-existing `Reasoning` module in just a couple of lines
1239+
(e.g. see `∣-Reasoning` in `Data.Nat.Divisibility`)
1240+
12251241
Deprecated modules
12261242
------------------
12271243

@@ -1793,6 +1809,11 @@ Deprecated names
17931809
sym-↔ ↦ ↔-sym
17941810
```
17951811

1812+
* In `Function.Related.Propositional.Reasoning`:
1813+
```agda
1814+
_↔⟨⟩_ ↦ _≡⟨⟩_
1815+
```
1816+
17961817
* In `Foreign.Haskell.Either` and `Foreign.Haskell.Pair`:
17971818
```
17981819
toForeign ↦ Foreign.Haskell.Coerce.coerce
@@ -2685,6 +2706,8 @@ Additions to existing modules
26852706
toℕ-inverseˡ : Inverseˡ _≡_ _≡_ toℕ fromℕ
26862707
toℕ-inverseʳ : Inverseʳ _≡_ _≡_ toℕ fromℕ
26872708
toℕ-inverseᵇ : Inverseᵇ _≡_ _≡_ toℕ fromℕ
2709+
2710+
<-asym : Asymmetric _<_
26882711
```
26892712

26902713
* Added a new pattern synonym to `Data.Nat.Divisibility.Core`:
@@ -2854,6 +2877,7 @@ Additions to existing modules
28542877
```agda
28552878
↥ᵘ-toℚᵘ : ↥ᵘ (toℚᵘ p) ≡ ↥ p
28562879
↧ᵘ-toℚᵘ : ↧ᵘ (toℚᵘ p) ≡ ↧ p
2880+
↥p≡↥q≡0⇒p≡q : ↥ p ≡ 0ℤ → ↥ q ≡ 0ℤ → p ≡ q
28572881
28582882
_≥?_ : Decidable _≥_
28592883
_>?_ : Decidable _>_
@@ -2878,6 +2902,10 @@ Additions to existing modules
28782902

28792903
* Added new definitions in `Data.Rational.Unnormalised.Properties`:
28802904
```agda
2905+
↥p≡0⇒p≃0 : ↥ p ≡ 0ℤ → p ≃ 0ℚᵘ
2906+
p≃0⇒↥p≡0 : p ≃ 0ℚᵘ → ↥ p ≡ 0ℤ
2907+
↥p≡↥q≡0⇒p≃q : ↥ p ≡ 0ℤ → ↥ q ≡ 0ℤ → p ≃ q
2908+
28812909
+-*-rawNearSemiring : RawNearSemiring 0ℓ 0ℓ
28822910
+-*-rawSemiring : RawSemiring 0ℓ 0ℓ
28832911

src/Codata/Guarded/Stream/Relation/Binary/Pointwise.agda

+4
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,14 @@ module _ {A : Set a} where
127127

128128
------------------------------------------------------------------------
129129
-- Pointwise DSL
130+
--
130131
-- A guardedness check does not play well with compositional proofs.
131132
-- Luckily we can learn from Nils Anders Danielsson's
132133
-- Beating the Productivity Checker Using Embedded Languages
133134
-- and design a little compositional DSL to define such proofs
135+
--
136+
-- NOTE: also because of the guardedness check we can't use the standard
137+
-- `Relation.Binary.Reasoning.Syntax` approach.
134138

135139
module pw-Reasoning (S : Setoid a ℓ) where
136140
private module S = Setoid S

src/Codata/Musical/Colist.agda

+16-16
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import Relation.Binary.Construct.FromRel as Ind
3434
import Relation.Binary.Reasoning.Preorder as PreR
3535
import Relation.Binary.Reasoning.PartialOrder as POR
3636
open import Relation.Binary.PropositionalEquality as P using (_≡_)
37+
open import Relation.Binary.Reasoning.Syntax
3738
open import Relation.Nullary.Reflects using (invert)
3839
open import Relation.Nullary
3940
open import Relation.Nullary.Negation
@@ -165,12 +166,11 @@ Any-∈ {P = P} = mk↔ₛ′
165166
module ⊑-Reasoning {a} {A : Set a} where
166167
private module Base = POR (⊑-Poset A)
167168

168-
open Base public
169-
hiding (step-<; begin-strict_; step-≤)
169+
open Base public hiding (step-<; step-≤)
170+
171+
open ⊑-syntax _IsRelatedTo_ _IsRelatedTo_ Base.≤-go public
172+
open ⊏-syntax _IsRelatedTo_ _IsRelatedTo_ Base.<-go public
170173

171-
infixr 2 step-⊑
172-
step-⊑ = Base.step-≤
173-
syntax step-⊑ x ys⊑zs xs⊑ys = x ⊑⟨ xs⊑ys ⟩ ys⊑zs
174174

175175
-- The subset relation forms a preorder.
176176

@@ -179,22 +179,22 @@ module ⊑-Reasoning {a} {A : Set a} where
179179
(λ xs≈ys ⊑⇒⊆ (⊑P.reflexive xs≈ys))
180180
where module ⊑P = Poset (⊑-Poset A)
181181

182+
-- Example uses:
183+
--
184+
-- x∈zs : x ∈ zs
185+
-- x∈zs =
186+
-- x ∈⟨ x∈xs ⟩
187+
-- xs ⊆⟨ xs⊆ys ⟩
188+
-- ys ≡⟨ ys≡zs ⟩
189+
-- zs ∎
182190
module ⊆-Reasoning {A : Set a} where
183191
private module Base = PreR (⊆-Preorder A)
184192

185-
open Base public
186-
hiding (step-∼)
187-
188-
infixr 2 step-⊆
189-
infix 1 step-∈
190-
191-
step-⊆ = Base.step-∼
193+
open Base public hiding (step-∼)
192194

193-
step-∈ : (x : A) {xs ys} xs IsRelatedTo ys x ∈ xs x ∈ ys
194-
step-∈ x xs⊆ys x∈xs = (begin xs⊆ys) x∈xs
195+
open begin-membership-syntax _IsRelatedTo_ _∈_ (λ x begin x)
196+
open ⊆-syntax _IsRelatedTo_ _IsRelatedTo_ Base.∼-go public
195197

196-
syntax step-⊆ xs ys⊆zs xs⊆ys = xs ⊆⟨ xs⊆ys ⟩ ys⊆zs
197-
syntax step-∈ x xs⊆ys x∈xs = x ∈⟨ x∈xs ⟩ xs⊆ys
198198

199199
-- take returns a prefix.
200200
take-⊑ : n (xs : Colist A)

src/Data/Container/Relation/Unary/Any/Properties.agda

+13-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ open import Relation.Binary.Core using (REL)
2626
open import Relation.Binary.PropositionalEquality as P
2727
using (_≡_; _≗_; refl)
2828

29-
open Related.EquationalReasoning hiding (_≡⟨_⟩_)
3029
private
3130
module ×⊎ {k ℓ} = CommutativeSemiring (×-⊎-commutativeSemiring k ℓ)
3231

@@ -71,6 +70,7 @@ module _ {s p} {C : Container s p} {x} {X : Set x}
7170
(∃ λ x x ∈ xs₁ × P₁ x) ∼⟨ Σ.cong ↔-refl (xs₁≈xs₂ ×-cong P₁↔P₂ _) ⟩
7271
(∃ λ x x ∈ xs₂ × P₂ x) ↔⟨ SK-sym (↔∈ C) ⟩
7372
◇ C P₂ xs₂ ∎
73+
where open Related.EquationalReasoning
7474

7575
-- Nested occurrences of ◇ can sometimes be swapped.
7676

@@ -95,6 +95,7 @@ module _ {s₁ s₂ p₁ p₂} {C₁ : Container s₁ p₁} {C₂ : Container s
9595
(∃ λ y y ∈ ys × ∃ λ x x ∈ xs × P x y) ↔⟨ Σ.cong ↔-refl (Σ.cong ↔-refl (SK-sym (↔∈ C₁))) ⟩
9696
(∃ λ y y ∈ ys × ◇ _ (flip P y) xs) ↔⟨ SK-sym (↔∈ C₂) ⟩
9797
◇ _ (λ y ◇ _ (flip P y) xs) ys ∎
98+
where open Related.EquationalReasoning
9899

99100
-- Nested occurrences of ◇ can sometimes be flattened.
100101

@@ -162,9 +163,10 @@ module _ {s p} (C : Container s p) {x y} {X : Set x} {Y : Set y}
162163
map↔∘ : {xs : ⟦ C ⟧ X} (f : X Y) ◇ C P (map f xs) ↔ ◇ C (P ∘′ f) xs
163164
map↔∘ {xs} f =
164165
◇ C P (map f xs) ↔⟨ ↔Σ C ⟩
165-
∃ (P ∘′ proj₂ (map f xs)) ⟨⟩
166+
∃ (P ∘′ proj₂ (map f xs)) ⟨⟩
166167
∃ (P ∘′ f ∘′ proj₂ xs) ↔⟨ SK-sym (↔Σ C) ⟩
167168
◇ C (P ∘′ f) xs ∎
169+
where open Related.EquationalReasoning
168170

169171
-- Membership in a mapped container can be expressed without reference
170172
-- to map.
@@ -178,6 +180,7 @@ module _ {s p} (C : Container s p) {x y} {X : Set x} {Y : Set y}
178180
y ∈ map f xs ↔⟨ map↔∘ C (y ≡_) f ⟩
179181
◇ C (λ x y ≡ f x) xs ↔⟨ ↔∈ C ⟩
180182
∃ (λ x x ∈ xs × y ≡ f x) ∎
183+
where open Related.EquationalReasoning
181184

182185
-- map is a congruence for bag and set equality and related preorders.
183186

@@ -193,6 +196,7 @@ module _ {s p} (C : Container s p) {x y} {X : Set x} {Y : Set y}
193196
◇ C (λ y x ≡ f₂ y) xs₂ ↔⟨ SK-sym (map↔∘ C (_≡_ x) f₂) ⟩
194197
x ∈ map f₂ xs₂ ∎
195198
where
199+
open Related.EquationalReasoning
196200
helper : y (x ≡ f₁ y) ↔ (x ≡ f₂ y)
197201
helper y rewrite f₁≗f₂ y = ↔-refl
198202

@@ -205,7 +209,7 @@ module _ {s₁ s₂ p₁ p₂} {C₁ : Container s₁ p₁} {C₂ : Container s
205209
remove-linear {xs} m = mk↔ₛ′ t f t∘f f∘t
206210
where
207211
open _≃_
208-
open P.≡-Reasoning renaming (_∎ to _∎′)
212+
open P.≡-Reasoning
209213

210214
position⊸m : {s} Position C₂ (shape⊸ m s) ≃ Position C₁ s
211215
position⊸m = ↔⇒≃ (position⊸ m)
@@ -259,7 +263,7 @@ module _ {s₁ s₂ p₁ p₂} {C₁ : Container s₁ p₁} {C₂ : Container s
259263

260264
P.subst (P ∘ proj₂ xs) P.refl p ≡⟨⟩
261265

262-
p ∎)
266+
p ∎)
263267
)
264268

265269
t∘f : t ∘ f ≗ id
@@ -279,7 +283,7 @@ module _ {s₁ s₂ p₁ p₂} {C₁ : Container s₁ p₁} {C₂ : Container s
279283
(P.trans-symˡ (right-inverse-of position⊸m _)) ⟩
280284
P.subst (P ∘ proj₂ xs) P.refl p ≡⟨⟩
281285

282-
p ∎)
286+
p ∎)
283287
)
284288

285289
-- Linear endomorphisms are identity functions if bag equality is used.
@@ -290,6 +294,7 @@ module _ {s p} {C : Container s p} {x} {X : Set x} where
290294
linear-identity {xs} m {x} =
291295
x ∈ ⟪ m ⟫⊸ xs ↔⟨ remove-linear (_≡_ x) m ⟩
292296
x ∈ xs ∎
297+
where open Related.EquationalReasoning
293298

294299
-- If join can be expressed using a linear morphism (in a certain
295300
-- way), then it can be absorbed by the predicate.
@@ -307,4 +312,6 @@ module _ {s₁ s₂ s₃ p₁ p₂ p₃}
307312
◇ C₃ P (⟪ join ⟫⊸ xss′) ↔⟨ remove-linear P join ⟩
308313
◇ (C₁ C.∘ C₂) P xss′ ↔⟨ SK-sym $ flatten P xss ⟩
309314
◇ C₁ (◇ C₂ P) xss ∎
310-
where xss′ = Inverse.from (Composition.correct C₁ C₂) xss
315+
where
316+
open Related.EquationalReasoning
317+
xss′ = Inverse.from (Composition.correct C₁ C₂) xss

src/Data/Integer/Divisibility/Signed.agda

+4-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ open import Relation.Binary.PropositionalEquality
3030
import Relation.Binary.Reasoning.Preorder as PreorderReasoning
3131
open import Relation.Nullary.Decidable using (yes; no)
3232
import Relation.Nullary.Decidable as DEC
33+
open import Relation.Binary.Reasoning.Syntax
3334

3435
------------------------------------------------------------------------
3536
-- Type
@@ -118,15 +119,11 @@ open _∣_ using (quotient) public
118119
-- Divisibility reasoning
119120

120121
module ∣-Reasoning where
121-
private
122-
module Base = PreorderReasoning ∣-preorder
122+
private module Base = PreorderReasoning ∣-preorder
123123

124-
open Base public
125-
hiding (step-∼; step-≈; step-≈˘)
124+
open Base public hiding (step-∼; step-≈; step-≈˘)
126125

127-
infixr 2 step-∣
128-
step-∣ = Base.step-∼
129-
syntax step-∣ x y∣z x∣y = x ∣⟨ x∣y ⟩ y∣z
126+
open ∣-syntax _IsRelatedTo_ _IsRelatedTo_ ∼-go public
130127

131128
------------------------------------------------------------------------
132129
-- Other properties of _∣_

src/Data/Integer/Properties.agda

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ i≮i = <-irrefl refl
364364
module ≤-Reasoning where
365365
open import Relation.Binary.Reasoning.Base.Triple
366366
≤-isPreorder
367-
<-irrefl
367+
<-asym
368368
<-trans
369369
(resp₂ _<_)
370370
<⇒≤

src/Data/List/Relation/Binary/BagAndSetEquality.agda

+12-17
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ open import Function.Related.TypeIsomorphisms
3737
open import Function.Properties.Inverse using (↔-sym; ↔-trans; to-from)
3838
open import Level using (Level)
3939
open import Relation.Binary.Core using (_⇒_)
40+
open import Relation.Binary.Definitions using (Trans)
4041
open import Relation.Binary.Bundles using (Preorder; Setoid)
4142
import Relation.Binary.Reasoning.Setoid as EqR
4243
import Relation.Binary.Reasoning.Preorder as PreorderReasoning
4344
open import Relation.Binary.PropositionalEquality as P
4445
using (_≡_; _≢_; _≗_; refl)
46+
open import Relation.Binary.Reasoning.Syntax
4547
open import Relation.Nullary
4648
open import Data.List.Membership.Propositional.Properties
4749

@@ -90,29 +92,22 @@ bag-=⇒ xs≈ys = ↔⇒ xs≈ys
9092
------------------------------------------------------------------------
9193
-- "Equational" reasoning for _⊆_ along with an additional relatedness
9294

93-
module ⊆-Reasoning where
94-
private
95-
module PreOrder {a} {A : Set a} = PreorderReasoning (⊆-preorder A)
95+
module ⊆-Reasoning {A : Set a} where
96+
private module Base = PreorderReasoning (⊆-preorder A)
9697

97-
open PreOrder public
98+
open Base public
9899
hiding (step-≈; step-≈˘; step-∼)
100+
renaming (∼-go to ⊆-go)
99101

100-
infixr 2 step-∼ step-⊆
101-
infix 1 step-∈
102+
open begin-membership-syntax _IsRelatedTo_ _∈_ (λ x begin x) public
103+
open ⊆-syntax _IsRelatedTo_ _IsRelatedTo_ ⊆-go public
102104

103-
step-⊆ = PreOrder.step-∼
105+
module _ {k : Related.ForwardKind} where
106+
∼-go : Trans _∼[ ⌊ k ⌋→ ]_ _IsRelatedTo_ _IsRelatedTo_
107+
∼-go eq = ⊆-go (⇒→ eq)
104108

105-
step-∈ : x {xs ys : List A}
106-
xs IsRelatedTo ys x ∈ xs x ∈ ys
107-
step-∈ x xs⊆ys x∈xs = (begin xs⊆ys) x∈xs
109+
open ∼-syntax _IsRelatedTo_ _IsRelatedTo_ ∼-go public
108110

109-
step-∼ : {k} xs {ys zs : List A}
110-
ys IsRelatedTo zs xs ∼[ ⌊ k ⌋→ ] ys xs IsRelatedTo zs
111-
step-∼ xs ys⊆zs xs≈ys = step-⊆ xs ys⊆zs (⇒→ xs≈ys)
112-
113-
syntax step-∈ x xs⊆ys x∈xs = x ∈⟨ x∈xs ⟩ xs⊆ys
114-
syntax step-∼ xs ys⊆zs xs≈ys = xs ∼⟨ xs≈ys ⟩ ys⊆zs
115-
syntax step-⊆ xs ys⊆zs xs⊆ys = xs ⊆⟨ xs⊆ys ⟩ ys⊆zs
116111

117112
------------------------------------------------------------------------
118113
-- Congruence lemmas

src/Data/List/Relation/Binary/Permutation/Propositional.agda

+7-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ open import Relation.Binary.Structures using (IsEquivalence)
1616
open import Relation.Binary.Definitions using (Reflexive; Transitive)
1717
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
1818
import Relation.Binary.Reasoning.Setoid as EqReasoning
19+
open import Relation.Binary.Reasoning.Syntax
1920

2021
------------------------------------------------------------------------
2122
-- An inductive definition of permutation
@@ -73,16 +74,15 @@ data _↭_ : Rel (List A) a where
7374

7475
module PermutationReasoning where
7576

76-
private
77-
module Base = EqReasoning ↭-setoid
77+
private module Base = EqReasoning ↭-setoid
7878

79-
open EqReasoning ↭-setoid public
80-
hiding (step-≈; step-≈˘)
79+
open Base public hiding (step-≈; step-≈˘)
8180

82-
infixr 2 step-↭ step-↭˘ step-swap step-prep
81+
open ↭-syntax _IsRelatedTo_ _IsRelatedTo_ Base.∼-go ↭-sym public
8382

84-
step-↭ = Base.step-≈
85-
step-↭˘ = Base.step-≈˘
83+
-- Some extra combinators that allow us to skip certain elements
84+
85+
infixr 2 step-swap step-prep
8686

8787
-- Skip reasoning on the first element
8888
step-prep : x xs {ys zs : List A} (x ∷ ys) IsRelatedTo zs
@@ -94,7 +94,5 @@ module PermutationReasoning where
9494
xs ↭ ys (x ∷ y ∷ xs) IsRelatedTo zs
9595
step-swap x y xs rel xs↭ys = relTo (trans (swap x y xs↭ys) (begin rel))
9696

97-
syntax step-↭ x y↭z x↭y = x ↭⟨ x↭y ⟩ y↭z
98-
syntax step-↭˘ x y↭z y↭x = x ↭˘⟨ y↭x ⟩ y↭z
9997
syntax step-prep x xs y↭z x↭y = x ∷ xs <⟨ x↭y ⟩ y↭z
10098
syntax step-swap x y xs y↭z x↭y = x ∷ y ∷ xs <<⟨ x↭y ⟩ y↭z

0 commit comments

Comments
 (0)