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

Refactor Data.List.Relation.Binary.Permutation.*, part I #2333

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
7ab1da5
move `steps` towards deprecation in `Homogeneous`
jamesmckinna Mar 28, 2024
4ece6bb
deprecate `steps`; refactor `Setoid` proofs and equaiotnal reasoning …
jamesmckinna Mar 28, 2024
38f22e4
extensive refactoring
jamesmckinna Mar 28, 2024
9399636
tidy up
jamesmckinna Mar 28, 2024
c6f701f
add equivalence with `Setoid` representation
jamesmckinna Mar 28, 2024
b37ba6f
removed buggy `PermutationReasoning` syntax
jamesmckinna Mar 28, 2024
791baf9
refactored; removed buggy `PermutationReasoning` syntax
jamesmckinna Mar 28, 2024
ff7845a
`CHANGELOG`
jamesmckinna Mar 28, 2024
a3d0b8e
final fix-ups
jamesmckinna Mar 28, 2024
2950a39
tighten `import`s
jamesmckinna Mar 28, 2024
69ca8fd
tighten `import`s
jamesmckinna Mar 28, 2024
5f0051a
redundant constructor aliases
jamesmckinna Mar 28, 2024
5461356
fix-up `Reasoning` steps with the alias
jamesmckinna Mar 28, 2024
8b44fd8
use aliases
jamesmckinna Mar 28, 2024
7918a8d
more `import` tightening
jamesmckinna Mar 28, 2024
00a335a
refactor: encapsulate and tighten up
jamesmckinna Mar 28, 2024
91feb30
avoid `PermutationReasoning` custom combinators
jamesmckinna Mar 28, 2024
227043d
fix up `CHANGELOG`
jamesmckinna Mar 28, 2024
4df26ef
encapsulate helper function
jamesmckinna Mar 28, 2024
0bf8e38
revert changes
jamesmckinna Mar 28, 2024
60a1958
review comments
jamesmckinna Mar 28, 2024
fd80b0b
`fix-whitespace`
jamesmckinna Mar 28, 2024
1781ea3
toned down the comment on `steps`
jamesmckinna Mar 29, 2024
7c2bc35
remove use of infix `insert`
jamesmckinna Mar 29, 2024
2e032a5
revert other deprecation
jamesmckinna Mar 29, 2024
4ad9fef
no need for qualification
jamesmckinna Mar 29, 2024
83dc6c7
remove deprecation banner
jamesmckinna Mar 29, 2024
246bf5c
three paras of commentary on the new transitivity proofs
jamesmckinna Mar 29, 2024
39bb784
missing entry
jamesmckinna Mar 30, 2024
b7dff80
missing terminator
jamesmckinna Mar 30, 2024
ce4d72f
response to review comments
jamesmckinna Apr 6, 2024
c8bf0a6
`with` to `let`
jamesmckinna Apr 7, 2024
e5701d9
Merge branch 'master' into permutation-refactor
jamesmckinna Apr 16, 2024
a40a26f
Merge branch 'master' into permutation-refactor
jamesmckinna Apr 24, 2024
cbef1f8
fixed `BagAndSetEquality`
jamesmckinna Apr 24, 2024
9eb7000
Merge branch 'master' into permutation-refactor
jamesmckinna May 14, 2024
a5e2849
Merge branch 'master' into permutation-refactor
jamesmckinna May 15, 2024
26371ae
Merge branch 'master' into permutation-refactor
jamesmckinna Sep 3, 2024
dd296a6
fixed qualified `import` bug introduced during merge conflict resolution
jamesmckinna Sep 3, 2024
7f84607
Merge branch 'master' into permutation-refactor
jamesmckinna Sep 9, 2024
92c14a8
Update CHANGELOG.md
jamesmckinna Sep 9, 2024
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
52 changes: 52 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ Deprecated names
_-_ ↦ _//_
```

* In `Data.List.Relation.Binary.Permutation.Setoid`:
```agda
steps ↦ Data.List.Relation.Binary.Permutation.Homogeneous.steps
```

* In `Data.List.Relation.Binary.Permutation.Setoid.Properties`:
```agda
split ↦ ↭-split
```
with a more informative type, and a simple renaming
```agda
foldr-commMonoid ↦ foldr-pres-↭
```

* In `Data.Nat.Divisibility.Core`:
```agda
*-pres-∣ ↦ Data.Nat.Divisibility.*-pres-∣
Expand Down Expand Up @@ -262,6 +276,42 @@ Additions to existing modules
reverse-downFrom : reverse (downFrom n) ≡ upTo n
```

* In `Data.List.Relation.Binary.Permutation.Homogeneous`:
```agda
steps : Permutation R xs ys → ℕ
```

* In `Data.List.Relation.Binary.Permutation.Propositional`:
constructor aliases
```agda
↭-refl : Reflexive _↭_
↭-prep : ∀ x → xs ↭ ys → x ∷ xs ↭ x ∷ ys
↭-swap : ∀ x y → xs ↭ ys → x ∷ y ∷ xs ↭ y ∷ x ∷ ys
```
and properties
```agda
↭-pointwise : _≋_ ⇒ _↭_
↭⇒↭ₛ : _↭_ ⇒ _↭ₛ_
↭ₛ⇒↭ : _↭ₛ_ ⇒ _↭_
```
where `_↭ₛ_` is the `Setoid (setoid _)` instance of `Permutation`

* In `Data.List.Relation.Binary.Permutation.Setoid`:
```agda
↭-pointwise : _≋_ ⇒ _↭_
↭-transˡ-≋ : LeftTrans _≋_ _↭_
↭-transʳ-≋ : RightTrans _↭_ _≋_
```

* In `Data.List.Relation.Binary.Permutation.Setoid.Properties`:
```agda
↭-split : xs ↭ (as ++[ v ]++ bs) →
∃₂ λ ps qs → xs ≋ (ps ++[ v ]++ qs) × (ps ++ qs) ↭ (as ++ bs)
drop-∷ : x ∷ xs ↭ x ∷ ys → xs ↭ ys

foldr-pres-↭ : (IsCommutativeMonoid _≈_ _∙_ ε) → (foldr _∙_ ε) Preserves _↭_ ⟶ _≈_
jamesmckinna marked this conversation as resolved.
Show resolved Hide resolved
```

* In `Data.List.Relation.Unary.All.Properties`:
```agda
All-catMaybes⁺ : All (Maybe.All P) xs → All P (catMaybes xs)
Expand Down Expand Up @@ -351,6 +401,8 @@ Additions to existing modules

* Added new proofs to `Data.List.Relation.Binary.Permutation.Propositional.Properties`:
```agda
Any-resp-[σ∘σ⁻¹] : (σ : xs ↭ ys) (iy : Any P ys) →
Any-resp-↭ (trans (↭-sym σ) σ) iy ≡ iy
product-↭ : product Preserves _↭_ ⟶ _≡_
```

Expand Down
25 changes: 7 additions & 18 deletions src/Data/List/Relation/Binary/BagAndSetEquality.agda
Original file line number Diff line number Diff line change
Expand Up @@ -566,29 +566,18 @@ drop-cons {x = x} {xs} {ys} x∷xs≈x∷ys =


------------------------------------------------------------------------
-- Relationships to other relations
-- Relationships to propositional permutation

↭⇒∼bag : _↭_ {A = A} ⇒ _∼[ bag ]_
↭⇒∼bag xs↭ys {v} = mk↔ₛ′ (to xs↭ys) (from xs↭ys) (to∘from xs↭ys) (from∘to xs↭ys)
where
to : ∀ {xs ys} → xs ↭ ys → v ∈ xs → v ∈ ys
to xs↭ys = ∈-resp-↭ xs↭ys

from : ∀ {xs ys} → xs ↭ ys → v ∈ ys → v ∈ xs
from xs↭ys = ∈-resp-↭ (↭-sym xs↭ys)

from∘to : ∀ {xs ys} (p : xs ↭ ys) (q : v ∈ xs) → from p (to p q) ≡ q
from∘to = ∈-resp-[σ⁻¹∘σ]

to∘from : ∀ {xs ys} (p : xs ↭ ys) (q : v ∈ ys) → to p (from p q) ≡ q
to∘from p with res ← from∘to (↭-sym p) rewrite ↭-sym-involutive p = res
↭⇒∼bag xs↭ys {v} =
mk↔ₛ′ (∈-resp-↭ xs↭ys) (∈-resp-↭ (↭-sym xs↭ys)) (∈-resp-[σ∘σ⁻¹] xs↭ys) (∈-resp-[σ⁻¹∘σ] xs↭ys)

∼bag⇒↭ : _∼[ bag ]_ ⇒ _↭_ {A = A}
∼bag⇒↭ {A = A} {[]} eq with refl ← empty-unique (↔-sym eq) = refl
∼bag⇒↭ {A = A} {[]} eq with refl ← empty-unique (↔-sym eq) = ↭-refl
∼bag⇒↭ {A = A} {x ∷ xs} eq
with zs₁ , zs₂ , p ← ∈-∃++ (Inverse.to (eq {x}) (here ≡.refl)) rewrite p = begin
x ∷ xs <⟨ ∼bag⇒↭ (drop-cons (↔-trans eq (comm zs₁ (x ∷ zs₂)))) ⟩
x ∷ (zs₂ ++ zs₁) <⟨ ++-comm zs₂ zs₁ ⟩
with zs₁ , zs₂ , refl ← ∈-∃++ (Inverse.to (eq {x}) (here ≡.refl)) = begin
x ∷ xs ↭⟨ ↭-prep x (∼bag⇒↭ (drop-cons (↔-trans eq (comm zs₁ (x ∷ zs₂))))) ⟩
x ∷ (zs₂ ++ zs₁) ↭⟨ ↭-prep x (++-comm zs₂ zs₁)
x ∷ (zs₁ ++ zs₂) ↭⟨ shift x zs₁ zs₂ ⟨
zs₁ ++ x ∷ zs₂ ∎
where
Expand Down
15 changes: 13 additions & 2 deletions src/Data/List/Relation/Binary/Permutation/Homogeneous.agda
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module Data.List.Relation.Binary.Permutation.Homogeneous where
open import Data.List.Base using (List; _∷_)
open import Data.List.Relation.Binary.Pointwise.Base as Pointwise
using (Pointwise)
open import Data.List.Relation.Binary.Pointwise.Properties as Pointwise
using (symmetric)
import Data.List.Relation.Binary.Pointwise.Properties as Pointwise
open import Data.Nat.Base using (ℕ; suc; _+_)
open import Level using (Level; _⊔_)
open import Relation.Binary.Core using (Rel; _⇒_)
open import Relation.Binary.Bundles using (Setoid)
Expand Down Expand Up @@ -59,3 +59,14 @@ map R⇒S (refl xs∼ys) = refl (Pointwise.map R⇒S xs∼ys)
map R⇒S (prep e xs∼ys) = prep (R⇒S e) (map R⇒S xs∼ys)
map R⇒S (swap e₁ e₂ xs∼ys) = swap (R⇒S e₁) (R⇒S e₂) (map R⇒S xs∼ys)
map R⇒S (trans xs∼ys ys∼zs) = trans (map R⇒S xs∼ys) (map R⇒S ys∼zs)

------------------------------------------------------------------------
-- Steps moves here towards eventual deprecation, because it is
-- * representation-dependent
-- * now obsolete wrt `Setoid.Properties.{dropMiddleElement,split}`
jamesmckinna marked this conversation as resolved.
Show resolved Hide resolved

steps : ∀ {R : Rel A r} {xs ys} → Permutation R xs ys → ℕ
steps (refl _) = 1
JacquesCarette marked this conversation as resolved.
Show resolved Hide resolved
steps (prep _ xs↭ys) = suc (steps xs↭ys)
steps (swap _ _ xs↭ys) = suc (steps xs↭ys)
steps (trans xs↭ys ys↭zs) = steps xs↭ys + steps ys↭zs
65 changes: 53 additions & 12 deletions src/Data/List/Relation/Binary/Permutation/Propositional.agda
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ module Data.List.Relation.Binary.Permutation.Propositional
{a} {A : Set a} where

open import Data.List.Base using (List; []; _∷_)
open import Data.List.Relation.Binary.Equality.Propositional using (_≋_; ≋⇒≡)
open import Relation.Binary.Core using (Rel; _⇒_)
open import Relation.Binary.Bundles using (Setoid)
open import Relation.Binary.Structures using (IsEquivalence)
open import Relation.Binary.Definitions using (Reflexive; Transitive)
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
open import Relation.Binary.PropositionalEquality as ≡ using (_≡_; refl)
import Relation.Binary.Reasoning.Setoid as EqReasoning
open import Relation.Binary.Reasoning.Syntax

import Data.List.Relation.Binary.Permutation.Setoid as Permutation

private
variable
x y z v w : A
ws xs ys zs : List A

------------------------------------------------------------------------
-- An inductive definition of permutation

Expand All @@ -30,21 +38,32 @@ open import Relation.Binary.Reasoning.Syntax
infix 3 _↭_

data _↭_ : Rel (List A) a where
refl : ∀ {xs} → xs ↭ xs
prep : ∀ {xs ys} x → xs ↭ ys → x ∷ xs ↭ x ∷ ys
swap : ∀ {xs ys} x y → xs ↭ ys → x ∷ y ∷ xs ↭ y ∷ x ∷ ys
trans : ∀ {xs ys zs} → xs ↭ ys → ys ↭ zs → xs ↭ zs
refl : xs ↭ xs
prep : ∀ x → xs ↭ ys → x ∷ xs ↭ x ∷ ys
swap : ∀ x y → xs ↭ ys → x ∷ y ∷ xs ↭ y ∷ x ∷ ys
trans : xs ↭ ys → ys ↭ zs → xs ↭ zs

-- Constructor aliases

↭-refl : Reflexive _↭_
↭-refl = refl

↭-prep : ∀ x → xs ↭ ys → x ∷ xs ↭ x ∷ ys
↭-prep = prep

↭-swap : ∀ x y → xs ↭ ys → x ∷ y ∷ xs ↭ y ∷ x ∷ ys
↭-swap = swap
Comment on lines +48 to +55
Copy link
Member

Choose a reason for hiding this comment

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

As always I am going to be annoying and complain that I don't like prefixed
names because users can either use the short names or import the module
qualified if they need to manually disambiguate.

The same goes for split being renamed to ↭-split (although in that case
the name split is already pretty poor IMO).

Copy link
Contributor Author

@jamesmckinna jamesmckinna Apr 6, 2024

Choose a reason for hiding this comment

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

@gallais I don't find the comment annoying!
What I find annoying is the the abstraction failure when explicitly exporting constructors from an inductive definition which should remain (externally, to clients) abstract, IMHO (cf. z<s in Data.Nat.Base, and 0<1+n in Data.Nat.Properties, not that I/we are entirely consistent in enforcing this...)
Since this is also part of a greater project of refactoring Permutation towards completing #1761 / #1354 I wanted to make sure that in Properties, references to the constructors were abstracted on those lines before proceeding further.

As regards ↭-split, this is not a renaming of split, but a significant generalisation which permits a much improved proof of dropMiddleElement... so a cognate name change is needed to reflect the 'better' typing! As for giving it a better name, I'm open to suggestions!


------------------------------------------------------------------------
-- _↭_ is an equivalence

↭-reflexive : _≡_ ⇒ _↭_
↭-reflexive refl = refl
↭-reflexive refl = ↭-refl

↭-refl : Reflexive _↭_
↭-refl = refl
↭-pointwise : _≋_ ⇒ _↭_
jamesmckinna marked this conversation as resolved.
Show resolved Hide resolved
↭-pointwise xs≋ys = ↭-reflexive (≋⇒≡ xs≋ys)

↭-sym : ∀ {xs ys} → xs ↭ ys → ys ↭ xs
↭-sym : xs ↭ ys → ys ↭ xs
↭-sym refl = refl
↭-sym (prep x xs↭ys) = prep x (↭-sym xs↭ys)
↭-sym (swap x y xs↭ys) = swap y x (↭-sym xs↭ys)
Expand All @@ -58,7 +77,7 @@ data _↭_ : Rel (List A) a where

↭-isEquivalence : IsEquivalence _↭_
↭-isEquivalence = record
{ refl = refl
{ refl = ↭-refl
; sym = ↭-sym
; trans = ↭-trans
}
Expand All @@ -68,6 +87,28 @@ data _↭_ : Rel (List A) a where
{ isEquivalence = ↭-isEquivalence
}

------------------------------------------------------------------------
-- _↭_ is equivalent to `Setoid`-based permutation
JacquesCarette marked this conversation as resolved.
Show resolved Hide resolved

private
open module ↭ₛ = Permutation (≡.setoid A)
using ()
renaming (_↭_ to _↭ₛ_)

↭⇒↭ₛ : xs ↭ ys → xs ↭ₛ ys
↭⇒↭ₛ refl = ↭ₛ.↭-refl
↭⇒↭ₛ (prep x p) = ↭ₛ.↭-prep x (↭⇒↭ₛ p)
↭⇒↭ₛ (swap x y p) = ↭ₛ.↭-swap x y (↭⇒↭ₛ p)
↭⇒↭ₛ (trans p q) = ↭ₛ.↭-trans (↭⇒↭ₛ p) (↭⇒↭ₛ q)


↭ₛ⇒↭ : _↭ₛ_ ⇒ _↭_
↭ₛ⇒↭ (↭ₛ.refl xs≋ys) = ↭-pointwise xs≋ys
↭ₛ⇒↭ (↭ₛ.prep refl p) = ↭-prep _ (↭ₛ⇒↭ p)
↭ₛ⇒↭ (↭ₛ.swap refl refl p) = ↭-swap _ _ (↭ₛ⇒↭ p)
↭ₛ⇒↭ (↭ₛ.trans p q) = ↭-trans (↭ₛ⇒↭ p) (↭ₛ⇒↭ q)


------------------------------------------------------------------------
-- A reasoning API to chain permutation proofs and allow "zooming in"
-- to localised reasoning.
Expand All @@ -89,12 +130,12 @@ module PermutationReasoning where
-- Skip reasoning on the first element
step-prep : ∀ x xs {ys zs : List A} → (x ∷ ys) IsRelatedTo zs →
xs ↭ ys → (x ∷ xs) IsRelatedTo zs
step-prep x xs rel xs↭ys = relTo (trans (prep x xs↭ys) (begin rel))
step-prep x xs rel xs↭ys = ↭-go (↭-prep x xs↭ys) rel
JacquesCarette marked this conversation as resolved.
Show resolved Hide resolved

-- Skip reasoning about the first two elements
step-swap : ∀ x y xs {ys zs : List A} → (y ∷ x ∷ ys) IsRelatedTo zs →
xs ↭ ys → (x ∷ y ∷ xs) IsRelatedTo zs
step-swap x y xs rel xs↭ys = relTo (trans (swap x y xs↭ys) (begin rel))
step-swap x y xs rel xs↭ys = ↭-go (↭-swap x y xs↭ys) rel

syntax step-prep x xs y↭z x↭y = x ∷ xs <⟨ x↭y ⟩ y↭z
syntax step-swap x y xs y↭z x↭y = x ∷ y ∷ xs <<⟨ x↭y ⟩ y↭z
Loading
Loading