# Pastebin Ty5NHw5K data X a = X {someVal :: Int, otherVal :: a} deriving Generic data SomeX = IntX (X Int) | DoubleX (X Double) deriving Generic class GHasSomeVal (f :: Type -> Type) where gsomeVal :: Lens' (f x) Int instance GHasSomeVal (K1 R (X a)) where gsomeVal = _K1 . #someVal instance GHasSomeVal f => GHasSomeVal (M1 i c f) where gsomeVal = _M1 . gsomeVal instance (GHasSomeVal f, GHasSomeVal g) => GHasSomeVal (f :+: g) where gsomeVal f = \case L1 x -> L1 <$> gsomeVal f x R1 x -> R1 <$> gsomeVal f x theSomeVal :: (Generic s, GHasSomeVal (Rep s)) => Lens' s Int theSomeVal = generic . gsomeVal