kcsongor / generic-lens

Generically derive traversals, lenses, and prisms.
437 stars 53 forks source link

Const and Identity instances for `HasAny` #148

Open srid opened 2 years ago

srid commented 2 years ago

https://hackage.haskell.org/package/generic-optics-2.2.1.0/docs/Data-Generics-Product-Any.html

Basically,

instance {-# OVERLAPPING #-} HasAny () s s () () where
  the = united

instance HasAny s s s s s where
  the = castOptic equality

Usage:

>>> human ^. the @()
()  -- Always type checks and returns ()

>>> human ^. the @Human == human
True  -- ^ equivalent to `id`

Do these make sense? With this in place, I am able to simplify the API of https://github.com/EmaApps/ema/pull/108

srid commented 2 years ago

If breaking the self-referential record fields case is a concern (which is valid) we can derive on types from the k -> Type kind instead, viz.:

instance HasAny (Const () :: k -> Type) s s () () where
  the = united

instance HasAny (Identity :: Type -> Type) s s s s where
  the = castOptic equality