Skip to content

'Gradual' decidable equality combinators #803

Closed
@gallais

Description

@gallais

When you define decidable equality for e.g.

data Rose (A : Set) : Set where
  node : List (Rose A)  Rose A

you can't reuse Data.List.Properties' ≡-dec because it makes the
termination checker scream. However you would not have to redo all the
work if only we provided

∷-dec : Dec (x ≡ y)  Dec (xs ≡ ys)  Dec (x ∷ xs ≡ y ∷ ys)
∷-dec x≟y xs≟ys = map′ (uncurry (cong₂ _∷_)) ∷-injective (x≟y ×-dec xs≟ys)

Indeed mutually defining

_≟_  : Decidable {A = Rose A} _≡_
_≟s_ : Decidable {A = List (Rose A)} _≡_

then becomes easy:

node xs ≟ node ys = map′ (cong node) node-injective (xs ≟s ys)

[]       ≟s []       = yes refl
(x ∷ xs) ≟s (y ∷ ys) = ∷-dec (x ≟ y) (xs ≟s ys)

[] ≟s (x ∷ ys) = no (λ ())
(x ∷ xs) ≟s [] = no (λ ())

I have used this trick twice in #799 to simplify decidable equality proofs
involving Arg and Abs and there are probably numerous other places where
we can define these combinators in the stdlib.

Edit: self-contained gist

Activity

added a commit that references this issue on Jun 6, 2019

[ re #803 ] 'gradual' decidable equality combinator for list

added a commit that references this issue on Jun 15, 2019

[ fix #698 ] Make reflection module type check under `--safe` (#799)

jamesmckinna

jamesmckinna commented on Jan 10, 2024

@jamesmckinna
Contributor

Towards a general solution to deriving (Eq)-style automated support?
(I'm told that this is a frequently-solved problem among commercial Agda developers)

JacquesCarette

JacquesCarette commented on Jan 10, 2024

@JacquesCarette
Contributor

'commercial Agda developers' ???

jamesmckinna

jamesmckinna commented on Jan 28, 2024

@jamesmckinna
Contributor

Was this issue effectively put to bed by #811 ? If so, we should close, but perhaps document the pattern in the library design/style guide documentation?

JacquesCarette

JacquesCarette commented on Jan 28, 2024

@JacquesCarette
Contributor

Agreed. Can we even test its use internally, or would that introduce dependencies that are a bit over the top?

jamesmckinna

jamesmckinna commented on Jan 31, 2024

@jamesmckinna
Contributor

Stub content added to doc/style-guide.md in #2270, so closing this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @gallais@JacquesCarette@jamesmckinna

        Issue actions

          'Gradual' decidable equality combinators · Issue #803 · agda/agda-stdlib