papa-0.7.0: src/Papa/Base/Data/Left.hs
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | The name @left@ (classy optics, rule 4): the value of the @Left@ side of a
-- sum.
--
-- Its target is a type variable, so the name has no identity instance
-- (rule 4). Its instance for 'Either', a library type, is in this module
-- (rule 15).
--
-- @since 0.7.0
module Papa.Base.Data.Left (
LeftLike (..),
) where
import Control.Lens (Choice, Optic, _Left)
import Data.Either (Either)
import Data.Type.Equality (type (~))
import Prelude (Applicative)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genEither = Gen.either (Gen.int (Range.linear 0 9)) Gen.alpha
-- | A field or a constructor named @left@.
--
-- ==== __Examples__
--
-- >>> preview left (Left 'x' :: Either Char Int)
-- Just 'x'
--
-- >>> preview left (Right 1 :: Either Char Int)
-- Nothing
--
-- >>> review left 'x' :: Either Char Int
-- Left 'x'
--
-- Prism laws:
--
-- >>> check $ withTests 100 $ property $ do x <- forAll (Gen.int (Range.linear 0 9)); preview left (review left x :: Either Int Char) === Just x
-- ...passed 100 tests.
-- True
--
-- >>> check $ withTests 100 $ property $ do e <- forAll genEither; for_ (preview left e) (\x -> review left x === e)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
class LeftLike p f x x' a a' | x -> a where
left ::
Optic p f x x' a a'
-- Prism
instance (Choice p, Applicative f, x' ~ Either a' b) => LeftLike p f (Either a b) x' a a' where
left =
_Left
{-# INLINE left #-}