packages feed

either-n-0.1.0.0: src/Data/Lens/Injection/Injection4.hs

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# OPTIONS_GHC -Wall #-}

-- | The fourth injection of a sum type, as a prism. The sum-type dual of 'Control.Lens.Field4'.
module Data.Lens.Injection.Injection4 (
  Injection4 (..),
) where

import Control.Lens (Prism)
import Data.Lens.Injection.Generic (GInjection, injection)
import Data.Proxy (Proxy (..))
import GHC.Generics (Generic, Rep)

{- $setup
>>> :set -XDeriveGeneric -XMultiParamTypeClasses -XFlexibleInstances
>>> import Control.Lens((^?), (#))
>>> import GHC.Generics(Generic)
-}

{- | Access the fourth constructor of a sum type.

Where 'Control.Lens._4' is the fourth projection out of a product, '_I4' is the fourth
injection into a sum.

The default implementation is the prism to the fourth constructor of a
'Generic' type, @'injection' (Proxy :: Proxy 3)@.

>>> data T = C1 | C2 | C3 | C4 Int | C5 deriving (Show, Generic)
>>> instance Injection4 T T Int Int
>>> C4 1 ^? _I4
Just 1

>>> C5 ^? _I4
Nothing

>>> _I4 # 1 :: T
C4 1
-}
class Injection4 s t a b | s -> a, t -> b, s b -> t, t a -> s where
  _I4 :: Prism s t a b
  default _I4 :: (Generic s, Generic t, GInjection 3 (Rep s) (Rep t) a b) => Prism s t a b
  _I4 =
    injection (Proxy :: Proxy 3)
  {-# INLINE _I4 #-}