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

[ add ] Relation.Binary._Reflects_⟶_ as a companion to _Preserves_⟶_ #2566

Closed
wants to merge 4 commits into from
Closed
Changes from all 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -85,3 +85,8 @@ Additions to existing modules
quasiring : Quasiring c ℓ → Quasiring (a ⊔ c) (a ⊔ ℓ)
commutativeRing : CommutativeRing c ℓ → CommutativeRing (a ⊔ c) (a ⊔ ℓ)
```

* In `Relation.Binary.Core`:
```agda
_Reflects_⟶_ : (A → B) → Rel B ℓ₁ → Rel A ℓ₂ → Set _
```
4 changes: 2 additions & 2 deletions src/Function/Definitions.agda
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ module Function.Definitions where

open import Data.Product.Base using (∃; _×_)
open import Level using (Level)
open import Relation.Binary.Core using (Rel)
open import Relation.Binary.Core using (Rel; _Reflects_⟶_)

private
variable
@@ -31,7 +31,7 @@ module _
Congruent f = ∀ {x y} → x ≈₁ y → f x ≈₂ f y

Injective : (A → B) → Set _
Injective f = ∀ {x y} → f x ≈₂ f y → x ≈₁ y
Injective = _Reflects _≈₂_ ⟶ _≈₁_
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm kind of against this change personally. I read Injective f = ∀ {x y} → f x ≈₂ f y → x ≈₁ y and I understand exactly what it says.

I read Injective = _Reflects _≈₂_ ⟶ _≈₁_ and I have to do the following:

  1. Expand the missing function to Injective f = f Reflects _≈₂_ ⟶ _≈₁_ (non-trivial for beginners)
  2. Expand Reflects to (_≈₁_ on f) ⇒ _≈₂_ which is equally baffling.
  3. Expand on ...
  4. Expand _⇒_...
  5. Only now do I get to the actual definition.

All of these definitions are in different modules and Agda currently provides no easy method of expanding them. In my mind the very small benefit of this change is outweighed by the quite large decrease in usability!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@MatthewDaggitt very sharp critique from the ux perspective (I think perhaps we should have ux as a new label category for tagging issues/PRs), for which all thanks.

As the preamble to this PR indicates, I wanted this concept/definition in stdlib so that (eventually) all the Cancellative properties could be rephrased in terms of it. Currently, under #1436 / #2573 we only treat the cancellative properties for equality... if you will, perhaps as another, more directly graspable concept, we should get rid of Cancellative and friends altogether in favour of saying that eg m +_ is Injective on Nat with _≡_ (that would be the other way to go).

It's perhaps the case that my ideological/reformatory zeal on [DRY] issues leads to (over-)generalisations like those considered here (or most recently, in #2581 ), and that reflects what is (probably!) the greater abstraction power of higher-order languages such as Agda's type theory relative to the abstraction power of users (esp. wrt definitional expansion across modules, as you indicate so forcefully).

Against your suggestion, my own preference would be to record as a comment, the 'conventional' usage as being a definitional expansion of the abstract one. Something like

  Injective : (A  B)  Set _
  Injective = _Reflects _≈₂_ ⟶ _≈₁_
-- i.e.  Injective f = ∀ {x y} → f x ≈₂ f y → x ≈₁ y

Of course, that would be a break from existing practice in the library, as well as opening the door to bit-rot if/when definitions change, but here I don't think we are at quite such a risk... precisely because the definition is so 'stable'?

Copy link
Contributor Author

@jamesmckinna jamesmckinna Feb 11, 2025

Choose a reason for hiding this comment

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

All of that said, happy to revert this particular instance of the definition (it was introduced above as 'illustrative'), but then what about Cancellative in all its variety?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think adding non-type checked comments is very susceptible to bit rot as you say. I would be more open to a private definition that performed an equality check.

However, I'm still not entirely sure what advantages the generalisation brings in this case. It's not like we have either a) a set of properties/theory about these kinds of definitions in general or b) lots of definitions in this form? Injective, Cancellative, anything else?

Ditto, I'm not sure that I would be keen on replacing the definitions of Cancellative for exactly the same reasons...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess I would still try (but ) to justify the addition

  • on formal grounds, as the 'opposite' of Respects
  • on abstraction grounds (I clearly prefer greater degrees of abstraction, yes ,for its own sake, so I'll try to keep that in mind in future)

but this particular stone seems to be getting harder to push uphill. I'll stop!


Surjective : (A → B) → Set _
Surjective f = ∀ y → ∃ λ x → ∀ {z} → z ≈₁ x → f z ≈₂ y
3 changes: 3 additions & 0 deletions src/Relation/Binary/Core.agda
Original file line number Diff line number Diff line change
@@ -60,6 +60,9 @@ P =[ f ]⇒ Q = P ⇒ (Q on f)
_Preserves_⟶_ : (A → B) → Rel A ℓ₁ → Rel B ℓ₂ → Set _
f Preserves P ⟶ Q = P =[ f ]⇒ Q

_Reflects_⟶_ : (A → B) → Rel B ℓ₁ → Rel A ℓ₂ → Set _
f Reflects Q ⟶ P = (Q on f) ⇒ P

-- A binary variant of _Preserves_⟶_.

_Preserves₂_⟶_⟶_ : (A → B → C) → Rel A ℓ₁ → Rel B ℓ₂ → Rel C ℓ₃ → Set _