module LensDefs
(module LensDefs,
module Control.Applicative,
Choice,
Profunctor,
Coercible)
where
import Data.Profunctor
import Data.Profunctor.Unsafe
import Control.Applicative
import Control.Monad.Identity
import Unsafe.Coerce
#if __GLASGOW_HASKELL__ > 707
import GHC.Exts(Coercible)
#else
import GHC.Exts(Constraint)
type Coercible a b = (() :: Constraint)
#endif
simple :: p a (f a) -> p a (f a)
simple = id
infixl 1 &
x & f = f x
infixr 4 %~
l %~ f = \t -> runIdentity $ l (rmap Identity f) t
iso :: (Profunctor p, Functor f)
=> (s -> a) -> (b -> t)
-> p a (f b) -> p s (f t)
iso sa bt = dimap sa (fmap bt)
isoNewtype :: (Profunctor p, Functor f,
Coercible b t,
Coercible a s)
=> (s -> a) -> (b -> t)
-> p a (f b) -> p s (f t)
isoNewtype sa _bt x = coerceBT x .# sa
where coerceBT :: p a (f b) -> p a (f t)
coerceBT = unsafeCoerce
prism :: (b -> t) -> (s -> Either t a)
-> (forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t))
prism bt seta = dimap seta (either pure (fmap bt)) . right'
prism' :: (a -> s) -> (s -> Maybe a)
-> (forall p f. (Choice p, Applicative f) => p a (f a) -> p s (f s))
prism' bs sma = prism bs (\s -> maybe (Left s) Right (sma s))