generic-lens icon indicating copy to clipboard operation
generic-lens copied to clipboard

Const and Identity instances for `HasAny`

Open srid opened this issue 3 years ago • 1 comments

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 avatar Jun 30 '22 18:06 srid

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

srid avatar Jun 30 '22 20:06 srid