serokell / universum

:milky_way: Prelude written in @Serokell
MIT License
176 stars 28 forks source link

Make 'Element' an associated type family #58

Closed chshersh closed 6 years ago

chshersh commented 7 years ago

It was suggested under comments to this answer on SO.

chshersh commented 6 years ago

@neongreen @int-index Could you please comment on this?

int-index commented 6 years ago

I do not have a strong opinion either way. An associated type family should work for this use case.

chshersh commented 6 years ago

One more reason to have associated type family:

We can implement WrappedList wrapper in more general way:

newtype WrappedList t = WrappedList t

instance ToList t => ToList (WrappedList t) where
    type Element (WrappedList t) = Element t

Now it's not possible because of conflicting instance:

type instance Element (f a) = a
int-index commented 6 years ago

type instance Element (f a) = a

Do we have this instance at top level at the moment? Yes, it should be made into a default definition of the associated type family.

chshersh commented 6 years ago

It's not possible to implement this in current universum design because we have {-# OVERLAPPABLE #-} instance and open type families can't be overlappable (as @int-index explained me). Related SO question:

https://stackoverflow.com/questions/47816268/how-to-implement-default-associated-type-family-for-overlappable-instance-monom