{-# LANGUAGE ScopedTypeVariables #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Control.Lens.Internal.Prism
-- Copyright   :  (C) 2012-2016 Edward Kmett
-- License     :  BSD-style (see the file LICENSE)
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  experimental
-- Portability :  non-portable
--
----------------------------------------------------------------------------
module Control.Lens.Internal.Prism
  ( Market(..)
  , Market'
  ) where

import Prelude ()

import Control.Lens.Internal.Prelude
import Control.Lens.Internal.Coerce

------------------------------------------------------------------------------
-- Prism: Market
------------------------------------------------------------------------------

-- | This type is used internally by the 'Control.Lens.Prism.Prism' code to
-- provide efficient access to the two parts of a 'Prism'.
data Market a b s t = Market (b -> t) (s -> Either t a)

-- | @type 'Market'' a s t = 'Market' a a s t@
type Market' a = Market a a

instance Functor (Market a b s) where
  fmap :: (a -> b) -> Market a b s a -> Market a b s b
fmap a -> b
f (Market b -> a
bt s -> Either a a
seta) = (b -> b) -> (s -> Either b a) -> Market a b s b
forall a b s t. (b -> t) -> (s -> Either t a) -> Market a b s t
Market (a -> b
f (a -> b) -> (b -> a) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
bt) ((a -> Either b a) -> (a -> Either b a) -> Either a a -> Either b a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (b -> Either b a
forall a b. a -> Either a b
Left (b -> Either b a) -> (a -> b) -> a -> Either b a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) a -> Either b a
forall a b. b -> Either a b
Right (Either a a -> Either b a) -> (s -> Either a a) -> s -> Either b a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Either a a
seta)
  {-# INLINE fmap #-}

instance Profunctor (Market a b) where
  dimap :: (a -> b) -> (c -> d) -> Market a b b c -> Market a b a d
dimap a -> b
f c -> d
g (Market b -> c
bt b -> Either c a
seta) = (b -> d) -> (a -> Either d a) -> Market a b a d
forall a b s t. (b -> t) -> (s -> Either t a) -> Market a b s t
Market (c -> d
g (c -> d) -> (b -> c) -> b -> d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> c
bt) ((c -> Either d a) -> (a -> Either d a) -> Either c a -> Either d a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (d -> Either d a
forall a b. a -> Either a b
Left (d -> Either d a) -> (c -> d) -> c -> Either d a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> d
g) a -> Either d a
forall a b. b -> Either a b
Right (Either c a -> Either d a) -> (a -> Either c a) -> a -> Either d a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Either c a
seta (b -> Either c a) -> (a -> b) -> a -> Either c a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
  {-# INLINE dimap #-}
  lmap :: (a -> b) -> Market a b b c -> Market a b a c
lmap a -> b
f (Market b -> c
bt b -> Either c a
seta) = (b -> c) -> (a -> Either c a) -> Market a b a c
forall a b s t. (b -> t) -> (s -> Either t a) -> Market a b s t
Market b -> c
bt (b -> Either c a
seta (b -> Either c a) -> (a -> b) -> a -> Either c a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
  {-# INLINE lmap #-}
  rmap :: (b -> c) -> Market a b a b -> Market a b a c
rmap b -> c
f (Market b -> b
bt a -> Either b a
seta) = (b -> c) -> (a -> Either c a) -> Market a b a c
forall a b s t. (b -> t) -> (s -> Either t a) -> Market a b s t
Market (b -> c
f (b -> c) -> (b -> b) -> b -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> b
bt) ((b -> Either c a) -> (a -> Either c a) -> Either b a -> Either c a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (c -> Either c a
forall a b. a -> Either a b
Left (c -> Either c a) -> (b -> c) -> b -> Either c a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> c
f) a -> Either c a
forall a b. b -> Either a b
Right (Either b a -> Either c a) -> (a -> Either b a) -> a -> Either c a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either b a
seta)
  {-# INLINE rmap #-}

  #. :: q b c -> Market a b a b -> Market a b a c
(#.) q b c
_ = Market a b a b -> Market a b a c
forall a b. Coercible a b => b -> a
coerce'
  {-# INLINE (#.) #-}
  .# :: Market a b b c -> q a b -> Market a b a c
(.#) Market a b b c
p q a b
_ = Market a b b c -> Market a b a c
coerce Market a b b c
p
  {-# INLINE (.#) #-}

instance Choice (Market a b) where
  left' :: Market a b a b -> Market a b (Either a c) (Either b c)
left' (Market b -> b
bt a -> Either b a
seta) = (b -> Either b c)
-> (Either a c -> Either (Either b c) a)
-> Market a b (Either a c) (Either b c)
forall a b s t. (b -> t) -> (s -> Either t a) -> Market a b s t
Market (b -> Either b c
forall a b. a -> Either a b
Left (b -> Either b c) -> (b -> b) -> b -> Either b c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> b
bt) ((Either a c -> Either (Either b c) a)
 -> Market a b (Either a c) (Either b c))
-> (Either a c -> Either (Either b c) a)
-> Market a b (Either a c) (Either b c)
forall a b. (a -> b) -> a -> b
$ \Either a c
sc -> case Either a c
sc of
    Left a
s -> case a -> Either b a
seta a
s of
      Left b
t -> Either b c -> Either (Either b c) a
forall a b. a -> Either a b
Left (b -> Either b c
forall a b. a -> Either a b
Left b
t)
      Right a
a -> a -> Either (Either b c) a
forall a b. b -> Either a b
Right a
a
    Right c
c -> Either b c -> Either (Either b c) a
forall a b. a -> Either a b
Left (c -> Either b c
forall a b. b -> Either a b
Right c
c)
  {-# INLINE left' #-}
  right' :: Market a b a b -> Market a b (Either c a) (Either c b)
right' (Market b -> b
bt a -> Either b a
seta) = (b -> Either c b)
-> (Either c a -> Either (Either c b) a)
-> Market a b (Either c a) (Either c b)
forall a b s t. (b -> t) -> (s -> Either t a) -> Market a b s t
Market (b -> Either c b
forall a b. b -> Either a b
Right (b -> Either c b) -> (b -> b) -> b -> Either c b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> b
bt) ((Either c a -> Either (Either c b) a)
 -> Market a b (Either c a) (Either c b))
-> (Either c a -> Either (Either c b) a)
-> Market a b (Either c a) (Either c b)
forall a b. (a -> b) -> a -> b
$ \Either c a
cs -> case Either c a
cs of
    Left c
c -> Either c b -> Either (Either c b) a
forall a b. a -> Either a b
Left (c -> Either c b
forall a b. a -> Either a b
Left c
c)
    Right a
s -> case a -> Either b a
seta a
s of
      Left b
t -> Either c b -> Either (Either c b) a
forall a b. a -> Either a b
Left (b -> Either c b
forall a b. b -> Either a b
Right b
t)
      Right a
a -> a -> Either (Either c b) a
forall a b. b -> Either a b
Right a
a
  {-# INLINE right' #-}