packages feed

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

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

-- | The fifth injection of a sum type, as a prism. The sum-type dual of 'Control.Lens.Field5'.
module Data.Lens.Injection.Injection5 (
  Injection5 (..),
) 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 fifth constructor of a sum type.

Where 'Control.Lens._5' is the fifth projection out of a product, '_I5' is the fifth
injection into a sum.

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

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

>>> C6 ^? _I5
Nothing

>>> _I5 # 1 :: T
C5 1
-}
class Injection5 s t a b | s -> a, t -> b, s b -> t, t a -> s where
  _I5 :: Prism s t a b
  default _I5 :: (Generic s, Generic t, GInjection 4 (Rep s) (Rep t) a b) => Prism s t a b
  _I5 =
    injection (Proxy :: Proxy 4)
  {-# INLINE _I5 #-}