Repository navigation
Description
I found that the current way of defining a class that has a superclass does not play nice with instance search. Here is a small example using the Alternative typeclass and its ApplicativeZero superclass:
open import Data.List.Base using (List; []; _∷_; [_])
open import Function.Base
open import Level using (Level)
open import Category.Functor renaming (RawFunctor to Functor)
open import Category.Applicative
renaming ( RawApplicative to Applicative
; RawApplicativeZero to ApplicativeZero
; RawAlternative to Alternative
)
open Functor {{...}} using (_<$>_)
open Applicative {{...}} using ()
open ApplicativeZero {{...}} using () renaming (∅ to empty)
open Alternative {{...}} using () renaming (_∣_ to _<|>_)
variable
ℓ ℓ′ : Level
A B C : Set ℓ
F M : Set ℓ → Set ℓ′
choice1 : {{_ : Alternative F}} → List (F A) → F A
choice1 [] = empty
where instance applicativeZeroF = Alternative.applicativeZero it
choice1 (f ∷ []) = f
choice1 (f ∷ fs) = f <|> choice1 fsThis example is accepted, but it is awkward to have to declare the instance applicativeZeroF locally.
In the Agda user manual, it is recommended to use instance fields for encoding superclasses: https://agda.readthedocs.io/en/v2.6.2/language/record-types.html#instance-fields. Unfortunately Agda currently does not provide a way to change a normal field to an instance field when importing a module. So the only solution I can see is to change the definition of these record types in the standard library to use instance fields from the beginning. Making these fields into instance fields should not change anything for people not using instance search, except that the type of the record constructor changes. But actually RawIAlternative currently does not have a record constructor, so AFAICT the change should be 100% backwards compatible.
Also pinging @UlfNorell since he might know alternative solutions.
Activity
MatthewDaggitt commentedon Jun 28, 2021
So concretely you'd be proposing turning the definition of
RawIApplicativeZerointo the following?I have no particular problems with that. My main question is how this would be extended to other superclasses? For instance in
Relation.Binary.TypeClassesit would be nice to be able to get_≟_fromDecTotalOrderas well. And eventually I guess the algebra classes. Should allIsXstructures within otherIsXstructures be made instance arguments? Presumably we're going to have start thinking aboutoverlappingkeywords as well.jespercockx commentedon Jun 28, 2021
You're right, these would eventually have to be changed as well to be (more) useful with instance search. And having
overlapon these fields would indeed be useful as well. Like I said, it is a bit unfortunate that Agda doesn't provide a way to make fields into instance fields post hoc. Perhaps it would be worthwhile to consider if we could add such a feature to Agda instead of changing the library.jespercockx commentedon Jun 28, 2021
Some superclasses (such as the
Functorsuperclass ofApplicative) are not defined as fields at all, but are instead defined in terms of the functions of the subclass. So here the strategy of turning fields into instance fields would not even apply. Perhaps there is some way this "forward search" part of the instance search could be made more extensible.omelkonian commentedon May 3, 2022
I think it's important to contrast two different approaches to declaring superclasses and choose one for the standard library after careful consideration.
Here's an example of the
Eqtypeclass and two subclasses:Approach (1): superclass as an instance field
overlap ⦃ super ⦄ : Eq AApproach (2): superclass as an instance parameter
⦃ _ : Eq A ⦄⦃ _ : Eq A ⦄ → ⦃ DecEq A ⦄ → ...MatthewDaggitt commentedon Sep 12, 2022
We're planning to reorganise
Effect.Functoretc. in v2.0 and will come back to this after this...