module Control.Lens.Internal.Prism
  ( Market(..)
  , Market'
  ) where
import Data.Profunctor
#ifndef SAFE
import Data.Profunctor.Unsafe
import Control.Lens.Internal.Coerce
#endif
data Market a b s t = Market (b -> t) (s -> Either t a)
type Market' a = Market a a
instance Functor (Market a b s) where
  fmap f (Market bt seta) = Market (f . bt) (either (Left . f) Right . seta)
  
instance Profunctor (Market a b) where
  dimap f g (Market bt seta) = Market (g . bt) (either (Left . g) Right . seta . f)
  
  lmap f (Market bt seta) = Market bt (seta . f)
  
  rmap f (Market bt seta) = Market (f . bt) (either (Left . f) Right . seta)
  
#ifndef SAFE
  ( #. ) _ = coerce'
  
  ( .# ) p _ = coerce p
  
#endif
instance Choice (Market a b) where
  left' (Market bt seta) = Market (Left . bt) $ \sc -> case sc of
    Left s -> case seta s of
      Left t -> Left (Left t)
      Right a -> Right a
    Right c -> Left (Right c)
  
  right' (Market bt seta) = Market (Right . bt) $ \cs -> case cs of
    Left c -> Left (Left c)
    Right s -> case seta s of
      Left t -> Left (Right t)
      Right a -> Right a