{-# LANGUAGE CPP #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}

#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE Trustworthy #-}
#endif

#if __GLASGOW_HASKELL__ >= 800
{-# LANGUAGE TypeInType #-}
#endif

#include "lens-common.h"

-----------------------------------------------------------------------------
-- |
-- Module      :  Control.Lens.Iso
-- Copyright   :  (C) 2012-16 Edward Kmett
-- License     :  BSD-style (see the file LICENSE)
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  provisional
-- Portability :  Rank2Types, TypeFamilies, FunctionalDependencies
--
----------------------------------------------------------------------------
module Control.Lens.Iso
  (
  -- * Isomorphism Lenses
    Iso, Iso'
  , AnIso, AnIso'
  -- * Isomorphism Construction
  , iso
  -- * Consuming Isomorphisms
  , from
  , cloneIso
  , withIso
  -- * Working with isomorphisms
  , au
  , auf
  , xplat
  , xplatf
  , under
  , mapping
  -- ** Common Isomorphisms
  , simple
  , non, non'
  , anon
  , enum
  , curried, uncurried
  , flipped
  , Swapped(..)
#if __GLASGOW_HASKELL__ >= 710
  , pattern Swapped
#endif
  , Strict(..)
#if __GLASGOW_HASKELL__ >= 710
  , pattern Strict
  , pattern Lazy
#endif
  , lazy
  , Reversing(..)
  , reversed
#if __GLASGOW_HASKELL__ >= 710
  , pattern Reversed
#endif
  , involuted
#if __GLASGOW_HASKELL__ >= 710
  , pattern List
#endif
  -- ** Uncommon Isomorphisms
  , magma
  , imagma
  , Magma
  -- ** Contravariant functors
  , contramapping
  -- * Profunctors
  , Profunctor(dimap,rmap,lmap)
  , dimapping
  , lmapping
  , rmapping
  -- * Bifunctors
  , bimapping
  , firsting
  , seconding
#if __GLASGOW_HASKELL__ >= 708
  -- * Coercions
  , coerced
#endif
  ) where

import Control.Lens.Equality (simple)
import Control.Lens.Getter
import Control.Lens.Fold
import Control.Lens.Internal.Context
import Control.Lens.Internal.Coerce
import Control.Lens.Internal.Indexed
import Control.Lens.Internal.Iso as Iso
import Control.Lens.Internal.Magma
import Control.Lens.Prism
import Control.Lens.Review
import Control.Lens.Type
import Control.Monad.State.Lazy as Lazy
import Control.Monad.State.Strict as Strict
import Control.Monad.Writer.Lazy as Lazy hiding (Product, Sum)
import Control.Monad.Writer.Strict as Strict hiding (Product, Sum)
import Control.Monad.RWS.Lazy as Lazy hiding (Product, Sum)
import Control.Monad.RWS.Strict as Strict hiding (Product, Sum)
import Control.Monad.ST.Lazy as Lazy
import Control.Monad.ST as Strict

import Data.Bifunctor
import Data.Bifunctor.Biff
import Data.Bifunctor.Flip
import Data.Bifunctor.Product
import Data.Bifunctor.Sum
import Data.Bifunctor.Tannen
import Data.ByteString as StrictB hiding (reverse)
import Data.ByteString.Lazy as LazyB hiding (reverse)
import Data.Functor.Identity
import Data.Text as StrictT hiding (reverse)
import Data.Text.Lazy as LazyT hiding (reverse)
import Data.Tuple (swap)
import Data.Maybe
import Data.Profunctor
import Data.Profunctor.Unsafe

#if !(MIN_VERSION_base(4,8,0))
import Data.Functor
#endif

#if __GLASGOW_HASKELL__ >= 708
import Data.Coerce (Coercible)
#if __GLASGOW_HASKELL__ < 710
import Data.Type.Coercion
#endif
#endif

#if __GLASGOW_HASKELL__ >= 710
import qualified GHC.Exts as Exts
#endif

#if __GLASGOW_HASKELL__ >= 800
import GHC.Exts (TYPE)
#endif

#ifdef HLINT
{-# ANN module "HLint: ignore Use on" #-}
#endif

-- $setup
-- >>> :set -XNoOverloadedStrings
-- >>> import Control.Lens
-- >>> import Data.Map as Map
-- >>> import Data.Foldable
-- >>> import Data.Monoid

----------------------------------------------------------------------------
-- Isomorphisms
-----------------------------------------------------------------------------

-- | When you see this as an argument to a function, it expects an 'Iso'.
type AnIso s t a b = Exchange a b a (Identity b) -> Exchange a b s (Identity t)

-- | A 'Simple' 'AnIso'.
type AnIso' s a = AnIso s s a a


-- | Build a simple isomorphism from a pair of inverse functions.
--
-- @
-- 'Control.Lens.Getter.view' ('iso' f g) ≡ f
-- 'Control.Lens.Getter.view' ('Control.Lens.Iso.from' ('iso' f g)) ≡ g
-- 'Control.Lens.Setter.over' ('iso' f g) h ≡ g '.' h '.' f
-- 'Control.Lens.Setter.over' ('Control.Lens.Iso.from' ('iso' f g)) h ≡ f '.' h '.' g
-- @
iso :: (s -> a) -> (b -> t) -> Iso s t a b
iso :: (s -> a) -> (b -> t) -> Iso s t a b
iso s -> a
sa b -> t
bt = (s -> a) -> (f b -> f t) -> p a (f b) -> p s (f t)
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap s -> a
sa ((b -> t) -> f b -> f t
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> t
bt)
{-# INLINE iso #-}

----------------------------------------------------------------------------
-- Consuming Isomorphisms
-----------------------------------------------------------------------------

-- | Invert an isomorphism.
--
-- @
-- 'from' ('from' l) ≡ l
-- @
from :: AnIso s t a b -> Iso b a t s
from :: AnIso s t a b -> Iso b a t s
from AnIso s t a b
l = AnIso s t a b
-> ((s -> a) -> (b -> t) -> p t (f s) -> p b (f a))
-> p t (f s)
-> p b (f a)
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
l (((s -> a) -> (b -> t) -> p t (f s) -> p b (f a))
 -> p t (f s) -> p b (f a))
-> ((s -> a) -> (b -> t) -> p t (f s) -> p b (f a))
-> p t (f s)
-> p b (f a)
forall a b. (a -> b) -> a -> b
$ ((b -> t) -> (s -> a) -> p t (f s) -> p b (f a))
-> (s -> a) -> (b -> t) -> p t (f s) -> p b (f a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (b -> t) -> (s -> a) -> p t (f s) -> p b (f a)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
{-# INLINE from #-}

-- | Extract the two functions, one from @s -> a@ and
-- one from @b -> t@ that characterize an 'Iso'.
#if __GLASGOW_HASKELL__ >= 800
withIso :: forall s t a b rep (r :: TYPE rep).
             AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
#else
withIso :: AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
#endif
withIso :: AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
ai (s -> a) -> (b -> t) -> r
k = case AnIso s t a b
ai ((a -> a) -> (b -> Identity b) -> Exchange a b a (Identity b)
forall a b s t. (s -> a) -> (b -> t) -> Exchange a b s t
Exchange a -> a
forall a. a -> a
id b -> Identity b
forall a. a -> Identity a
Identity) of
  Exchange s -> a
sa b -> Identity t
bt -> (s -> a) -> (b -> t) -> r
k s -> a
sa (Identity t -> t
forall a. Identity a -> a
runIdentity (Identity t -> t) -> (b -> Identity t) -> b -> t
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. b -> Identity t
bt)
{-# INLINE withIso #-}

-- | Convert from 'AnIso' back to any 'Iso'.
--
-- This is useful when you need to store an isomorphism as a data type inside a container
-- and later reconstitute it as an overloaded function.
--
-- See 'Control.Lens.Lens.cloneLens' or 'Control.Lens.Traversal.cloneTraversal' for more information on why you might want to do this.
cloneIso :: AnIso s t a b -> Iso s t a b
cloneIso :: AnIso s t a b -> Iso s t a b
cloneIso AnIso s t a b
k = AnIso s t a b
-> ((s -> a) -> (b -> t) -> p a (f b) -> p s (f t))
-> p a (f b)
-> p s (f t)
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
k (s -> a) -> (b -> t) -> p a (f b) -> p s (f t)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
{-# INLINE cloneIso #-}

-----------------------------------------------------------------------------
-- Isomorphisms families as Lenses
-----------------------------------------------------------------------------

-- | Based on 'Control.Lens.Wrapped.ala' from Conor McBride's work on Epigram.
--
-- This version is generalized to accept any 'Iso', not just a @newtype@.
--
-- >>> au (_Wrapping Sum) foldMap [1,2,3,4]
-- 10
--
-- You may want to think of this combinator as having the following, simpler type:
--
-- @
-- au :: AnIso s t a b -> ((b -> t) -> e -> s) -> e -> a
-- @
--
-- @
-- au = xplat . from
-- @
au :: Functor f => AnIso s t a b -> ((b -> t) -> f s) -> f a
au :: AnIso s t a b -> ((b -> t) -> f s) -> f a
au AnIso s t a b
k = AnIso s t a b
-> ((s -> a) -> (b -> t) -> ((b -> t) -> f s) -> f a)
-> ((b -> t) -> f s)
-> f a
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
k (((s -> a) -> (b -> t) -> ((b -> t) -> f s) -> f a)
 -> ((b -> t) -> f s) -> f a)
-> ((s -> a) -> (b -> t) -> ((b -> t) -> f s) -> f a)
-> ((b -> t) -> f s)
-> f a
forall a b. (a -> b) -> a -> b
$ \ s -> a
sa b -> t
bt (b -> t) -> f s
f -> (s -> a) -> f s -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap s -> a
sa ((b -> t) -> f s
f b -> t
bt)
{-# INLINE au #-}

-- | Based on @ala'@ from Conor McBride's work on Epigram.
--
-- This version is generalized to accept any 'Iso', not just a @newtype@.
--
-- For a version you pass the name of the @newtype@ constructor to, see 'Control.Lens.Wrapped.alaf'.
--
-- >>> auf (_Wrapping Sum) (foldMapOf both) Prelude.length ("hello","world")
-- 10
--
-- Mnemonically, the German /auf/ plays a similar role to /à la/, and the combinator
-- is 'au' with an extra function argument:
--
-- @
-- 'auf' :: 'Iso' s t a b -> ((r -> t) -> e -> s) -> (r -> b) -> e -> a
-- @
--
-- but the signature is general.
--
-- Note: The direction of the 'Iso' required for this function changed in @lens@ 4.18 to match up
-- with the behavior of 'au'. For the old behavior use 'xplatf' or for a version that is compatible
-- across both old and new versions of @lens@ you can just use 'coerce'!
auf :: (Functor f, Functor g) => AnIso s t a b -> (f t -> g s) -> f b -> g a
auf :: AnIso s t a b -> (f t -> g s) -> f b -> g a
auf AnIso s t a b
k f t -> g s
ftgs f b
fb = AnIso s t a b -> ((s -> a) -> (b -> t) -> g a) -> g a
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
k (((s -> a) -> (b -> t) -> g a) -> g a)
-> ((s -> a) -> (b -> t) -> g a) -> g a
forall a b. (a -> b) -> a -> b
$ \s -> a
sa b -> t
bt -> s -> a
sa (s -> a) -> g s -> g a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f t -> g s
ftgs (b -> t
bt (b -> t) -> f b -> f t
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f b
fb)
{-# INLINE auf #-}

-- | @'xplat' = 'au' . 'from'@ but with a nicer signature.
xplat :: Optic (Costar ((->) s)) g s t a b -> ((s -> a) -> g b) -> g t
xplat :: Optic (Costar ((->) s)) g s t a b -> ((s -> a) -> g b) -> g t
xplat Optic (Costar ((->) s)) g s t a b
f (s -> a) -> g b
g = Optic (Costar ((->) s)) g s t a b
-> ((s -> a) -> g b) -> (s -> s) -> g t
forall k k (f :: k -> *) (g :: k -> *) (s :: k) (t :: k) (a :: k)
       (b :: k).
Optic (Costar f) g s t a b -> (f a -> g b) -> f s -> g t
xplatf Optic (Costar ((->) s)) g s t a b
f (s -> a) -> g b
g s -> s
forall a. a -> a
id

-- | @'xplatf' = 'auf' . 'from'@ but with a nicer signature.
--
-- >>> xplatf (_Unwrapping Sum) (foldMapOf both) Prelude.length ("hello","world")
-- 10
--
-- @
-- 'xplatf' :: 'Iso' s t a b -> ((r -> a) -> e -> b) -> (r -> s) -> e -> t
-- @
xplatf :: Optic (Costar f) g s t a b -> (f a -> g b) -> f s -> g t
xplatf :: Optic (Costar f) g s t a b -> (f a -> g b) -> f s -> g t
xplatf = Optic (Costar f) g s t a b -> (f a -> g b) -> f s -> g t
coerce
{-# INLINE xplat #-}

-- | The opposite of working 'Control.Lens.Setter.over' a 'Setter' is working 'under' an isomorphism.
--
-- @
-- 'under' ≡ 'Control.Lens.Setter.over' '.' 'from'
-- @
--
-- @
-- 'under' :: 'Iso' s t a b -> (t -> s) -> b -> a
-- @
under :: AnIso s t a b -> (t -> s) -> b -> a
under :: AnIso s t a b -> (t -> s) -> b -> a
under AnIso s t a b
k = AnIso s t a b
-> ((s -> a) -> (b -> t) -> (t -> s) -> b -> a)
-> (t -> s)
-> b
-> a
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
k (((s -> a) -> (b -> t) -> (t -> s) -> b -> a)
 -> (t -> s) -> b -> a)
-> ((s -> a) -> (b -> t) -> (t -> s) -> b -> a)
-> (t -> s)
-> b
-> a
forall a b. (a -> b) -> a -> b
$ \ s -> a
sa b -> t
bt t -> s
ts -> s -> a
sa (s -> a) -> (b -> s) -> b -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> s
ts (t -> s) -> (b -> t) -> b -> s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> t
bt
{-# INLINE under #-}

-----------------------------------------------------------------------------
-- Isomorphisms
-----------------------------------------------------------------------------

-- | This isomorphism can be used to convert to or from an instance of 'Enum'.
--
-- >>> LT^.from enum
-- 0
--
-- >>> 97^.enum :: Char
-- 'a'
--
-- Note: this is only an isomorphism from the numeric range actually used
-- and it is a bit of a pleasant fiction, since there are questionable
-- 'Enum' instances for 'Double', and 'Float' that exist solely for
-- @[1.0 .. 4.0]@ sugar and the instances for those and 'Integer' don't
-- cover all values in their range.
enum :: Enum a => Iso' Int a
enum :: Iso' Int a
enum = (Int -> a) -> (a -> Int) -> Iso' Int a
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Int -> a
forall a. Enum a => Int -> a
toEnum a -> Int
forall a. Enum a => a -> Int
fromEnum
{-# INLINE enum #-}

-- | This can be used to lift any 'Iso' into an arbitrary 'Functor'.
mapping :: (Functor f, Functor g) => AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
mapping :: AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
mapping AnIso s t a b
k = AnIso s t a b
-> ((s -> a) -> (b -> t) -> p (f a) (f (g b)) -> p (f s) (f (g t)))
-> p (f a) (f (g b))
-> p (f s) (f (g t))
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
k (((s -> a) -> (b -> t) -> p (f a) (f (g b)) -> p (f s) (f (g t)))
 -> p (f a) (f (g b)) -> p (f s) (f (g t)))
-> ((s -> a) -> (b -> t) -> p (f a) (f (g b)) -> p (f s) (f (g t)))
-> p (f a) (f (g b))
-> p (f s) (f (g t))
forall a b. (a -> b) -> a -> b
$ \ s -> a
sa b -> t
bt -> (f s -> f a) -> (g b -> g t) -> Iso (f s) (g t) (f a) (g b)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((s -> a) -> f s -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap s -> a
sa) ((b -> t) -> g b -> g t
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> t
bt)
{-# INLINE mapping #-}

-- | If @v@ is an element of a type @a@, and @a'@ is @a@ sans the element @v@, then @'non' v@ is an isomorphism from
-- @'Maybe' a'@ to @a@.
--
-- @
-- 'non' ≡ 'non'' '.' 'only'
-- @
--
-- Keep in mind this is only a real isomorphism if you treat the domain as being @'Maybe' (a sans v)@.
--
-- This is practically quite useful when you want to have a 'Data.Map.Map' where all the entries should have non-zero values.
--
-- >>> Map.fromList [("hello",1)] & at "hello" . non 0 +~ 2
-- fromList [("hello",3)]
--
-- >>> Map.fromList [("hello",1)] & at "hello" . non 0 -~ 1
-- fromList []
--
-- >>> Map.fromList [("hello",1)] ^. at "hello" . non 0
-- 1
--
-- >>> Map.fromList [] ^. at "hello" . non 0
-- 0
--
-- This combinator is also particularly useful when working with nested maps.
--
-- /e.g./ When you want to create the nested 'Data.Map.Map' when it is missing:
--
-- >>> Map.empty & at "hello" . non Map.empty . at "world" ?~ "!!!"
-- fromList [("hello",fromList [("world","!!!")])]
--
-- and when have deleting the last entry from the nested 'Data.Map.Map' mean that we
-- should delete its entry from the surrounding one:
--
-- >>> fromList [("hello",fromList [("world","!!!")])] & at "hello" . non Map.empty . at "world" .~ Nothing
-- fromList []
--
-- It can also be used in reverse to exclude a given value:
--
-- >>> non 0 # rem 10 4
-- Just 2
--
-- >>> non 0 # rem 10 5
-- Nothing
non :: Eq a => a -> Iso' (Maybe a) a
non :: a -> Iso' (Maybe a) a
non = APrism' a () -> p a (f a) -> p (Maybe a) (f (Maybe a))
forall a. APrism' a () -> Iso' (Maybe a) a
non' (APrism' a () -> p a (f a) -> p (Maybe a) (f (Maybe a)))
-> (a -> APrism' a ())
-> a
-> p a (f a)
-> p (Maybe a) (f (Maybe a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> APrism' a ()
forall a. Eq a => a -> Prism' a ()
only
{-# INLINE non #-}

-- | @'non'' p@ generalizes @'non' (p # ())@ to take any unit 'Prism'
--
-- This function generates an isomorphism between @'Maybe' (a | 'isn't' p a)@ and @a@.
--
-- >>> Map.singleton "hello" Map.empty & at "hello" . non' _Empty . at "world" ?~ "!!!"
-- fromList [("hello",fromList [("world","!!!")])]
--
-- >>> fromList [("hello",fromList [("world","!!!")])] & at "hello" . non' _Empty . at "world" .~ Nothing
-- fromList []
non' :: APrism' a () -> Iso' (Maybe a) a
non' :: APrism' a () -> Iso' (Maybe a) a
non' APrism' a ()
p = (Maybe a -> a) -> (a -> Maybe a) -> Iso' (Maybe a) a
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
def) a -> Maybe a
go where
  def :: a
def                           = AReview a () -> () -> a
forall b (m :: * -> *) t. MonadReader b m => AReview t b -> m t
review (APrism' a () -> Prism a a () ()
forall s t a b. APrism s t a b -> Prism s t a b
clonePrism APrism' a ()
p) ()
  go :: a -> Maybe a
go a
b | Getting Any a () -> a -> Bool
forall s a. Getting Any s a -> s -> Bool
has (APrism' a () -> Prism a a () ()
forall s t a b. APrism s t a b -> Prism s t a b
clonePrism APrism' a ()
p) a
b   = Maybe a
forall a. Maybe a
Nothing
       | Bool
otherwise              = a -> Maybe a
forall a. a -> Maybe a
Just a
b
{-# INLINE non' #-}

-- | @'anon' a p@ generalizes @'non' a@ to take any value and a predicate.
--
-- This function assumes that @p a@ holds @'True'@ and generates an isomorphism between @'Maybe' (a | 'not' (p a))@ and @a@.
--
-- >>> Map.empty & at "hello" . anon Map.empty Map.null . at "world" ?~ "!!!"
-- fromList [("hello",fromList [("world","!!!")])]
--
-- >>> fromList [("hello",fromList [("world","!!!")])] & at "hello" . anon Map.empty Map.null . at "world" .~ Nothing
-- fromList []
anon :: a -> (a -> Bool) -> Iso' (Maybe a) a
anon :: a -> (a -> Bool) -> Iso' (Maybe a) a
anon a
a a -> Bool
p = (Maybe a -> a) -> (a -> Maybe a) -> Iso' (Maybe a) a
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
a) a -> Maybe a
go where
  go :: a -> Maybe a
go a
b | a -> Bool
p a
b       = Maybe a
forall a. Maybe a
Nothing
       | Bool
otherwise = a -> Maybe a
forall a. a -> Maybe a
Just a
b
{-# INLINE anon #-}

-- | The canonical isomorphism for currying and uncurrying a function.
--
-- @
-- 'curried' = 'iso' 'curry' 'uncurry'
-- @
--
-- >>> (fst^.curried) 3 4
-- 3
--
-- >>> view curried fst 3 4
-- 3
curried :: Iso ((a,b) -> c) ((d,e) -> f) (a -> b -> c) (d -> e -> f)
curried :: p (a -> b -> c) (f (d -> e -> f))
-> p ((a, b) -> c) (f ((d, e) -> f))
curried = (((a, b) -> c) -> a -> b -> c)
-> ((d -> e -> f) -> (d, e) -> f)
-> Iso ((a, b) -> c) ((d, e) -> f) (a -> b -> c) (d -> e -> f)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((a, b) -> c) -> a -> b -> c
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (d -> e -> f) -> (d, e) -> f
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry
{-# INLINE curried #-}

-- | The canonical isomorphism for uncurrying and currying a function.
--
-- @
-- 'uncurried' = 'iso' 'uncurry' 'curry'
-- @
--
-- @
-- 'uncurried' = 'from' 'curried'
-- @
--
-- >>> ((+)^.uncurried) (1,2)
-- 3
uncurried :: Iso (a -> b -> c) (d -> e -> f) ((a,b) -> c) ((d,e) -> f)
uncurried :: p ((a, b) -> c) (f ((d, e) -> f))
-> p (a -> b -> c) (f (d -> e -> f))
uncurried = ((a -> b -> c) -> (a, b) -> c)
-> (((d, e) -> f) -> d -> e -> f)
-> Iso (a -> b -> c) (d -> e -> f) ((a, b) -> c) ((d, e) -> f)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (a -> b -> c) -> (a, b) -> c
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((d, e) -> f) -> d -> e -> f
forall a b c. ((a, b) -> c) -> a -> b -> c
curry
{-# INLINE uncurried #-}

-- | The isomorphism for flipping a function.
--
-- >>>((,)^.flipped) 1 2
-- (2,1)
flipped :: Iso (a -> b -> c) (a' -> b' -> c') (b -> a -> c) (b' -> a' -> c')
flipped :: p (b -> a -> c) (f (b' -> a' -> c'))
-> p (a -> b -> c) (f (a' -> b' -> c'))
flipped = ((a -> b -> c) -> b -> a -> c)
-> ((b' -> a' -> c') -> a' -> b' -> c')
-> Iso
     (a -> b -> c) (a' -> b' -> c') (b -> a -> c) (b' -> a' -> c')
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (a -> b -> c) -> b -> a -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip (b' -> a' -> c') -> a' -> b' -> c'
forall a b c. (a -> b -> c) -> b -> a -> c
flip
{-# INLINE flipped #-}

-- | This class provides for symmetric bifunctors.
class Bifunctor p => Swapped p where
  -- |
  -- @
  -- 'swapped' '.' 'swapped' ≡ 'id'
  -- 'first' f '.' 'swapped' = 'swapped' '.' 'second' f
  -- 'second' g '.' 'swapped' = 'swapped' '.' 'first' g
  -- 'bimap' f g '.' 'swapped' = 'swapped' '.' 'bimap' g f
  -- @
  --
  -- >>> (1,2)^.swapped
  -- (2,1)
  swapped :: Iso (p a b) (p c d) (p b a) (p d c)

instance Swapped (,) where
  swapped :: p (b, a) (f (d, c)) -> p (a, b) (f (c, d))
swapped = ((a, b) -> (b, a))
-> ((d, c) -> (c, d)) -> Iso (a, b) (c, d) (b, a) (d, c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (a, b) -> (b, a)
forall a b. (a, b) -> (b, a)
swap (d, c) -> (c, d)
forall a b. (a, b) -> (b, a)
swap

instance Swapped Either where
  swapped :: p (Either b a) (f (Either d c)) -> p (Either a b) (f (Either c d))
swapped = (Either a b -> Either b a)
-> (Either d c -> Either c d)
-> Iso (Either a b) (Either c d) (Either b a) (Either d c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((a -> Either b a) -> (b -> Either b a) -> Either a b -> Either b a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> Either b a
forall a b. b -> Either a b
Right b -> Either b a
forall a b. a -> Either a b
Left) ((d -> Either c d) -> (c -> Either c d) -> Either d c -> Either c d
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either d -> Either c d
forall a b. b -> Either a b
Right c -> Either c d
forall a b. a -> Either a b
Left)

instance (Swapped f, Swapped g) => Swapped (Product f g) where
  swapped :: p (Product f g b a) (f (Product f g d c))
-> p (Product f g a b) (f (Product f g c d))
swapped = (Product f g a b -> Product f g b a)
-> (Product f g d c -> Product f g c d)
-> Iso
     (Product f g a b)
     (Product f g c d)
     (Product f g b a)
     (Product f g d c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Product f g a b -> Product f g b a
forall (f :: * -> * -> *) (g :: * -> * -> *) a b.
(Swapped f, Swapped g) =>
Product f g a b -> Product f g b a
f Product f g d c -> Product f g c d
forall (f :: * -> * -> *) (g :: * -> * -> *) a b.
(Swapped f, Swapped g) =>
Product f g a b -> Product f g b a
f
    where
      f :: Product f g a b -> Product f g b a
f (Pair f a b
x g a b
y) = f b a -> g b a -> Product f g b a
forall k k1 (f :: k -> k1 -> *) (g :: k -> k1 -> *) (a :: k)
       (b :: k1).
f a b -> g a b -> Product f g a b
Pair (f a b
x f a b -> Getting (f b a) (f a b) (f b a) -> f b a
forall s a. s -> Getting a s a -> a
^. Getting (f b a) (f a b) (f b a)
forall (p :: * -> * -> *) a b c d.
Swapped p =>
Iso (p a b) (p c d) (p b a) (p d c)
swapped) (g a b
y g a b -> Getting (g b a) (g a b) (g b a) -> g b a
forall s a. s -> Getting a s a -> a
^. Getting (g b a) (g a b) (g b a)
forall (p :: * -> * -> *) a b c d.
Swapped p =>
Iso (p a b) (p c d) (p b a) (p d c)
swapped)

instance (Swapped p, Swapped q) => Swapped (Sum p q) where
  swapped :: p (Sum p q b a) (f (Sum p q d c))
-> p (Sum p q a b) (f (Sum p q c d))
swapped = (Sum p q a b -> Sum p q b a)
-> (Sum p q d c -> Sum p q c d)
-> Iso (Sum p q a b) (Sum p q c d) (Sum p q b a) (Sum p q d c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Sum p q a b -> Sum p q b a
forall (p :: * -> * -> *) (q :: * -> * -> *) a b.
(Swapped p, Swapped q) =>
Sum p q a b -> Sum p q b a
f Sum p q d c -> Sum p q c d
forall (p :: * -> * -> *) (q :: * -> * -> *) a b.
(Swapped p, Swapped q) =>
Sum p q a b -> Sum p q b a
f
    where
      f :: Sum p q a b -> Sum p q b a
f (L2 p a b
x) = p b a -> Sum p q b a
forall k k1 (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
p a b -> Sum p q a b
L2 (p a b
x p a b -> Getting (p b a) (p a b) (p b a) -> p b a
forall s a. s -> Getting a s a -> a
^. Getting (p b a) (p a b) (p b a)
forall (p :: * -> * -> *) a b c d.
Swapped p =>
Iso (p a b) (p c d) (p b a) (p d c)
swapped)
      f (R2 q a b
x) = q b a -> Sum p q b a
forall k k1 (p :: k -> k1 -> *) (q :: k -> k1 -> *) (a :: k)
       (b :: k1).
q a b -> Sum p q a b
R2 (q a b
x q a b -> Getting (q b a) (q a b) (q b a) -> q b a
forall s a. s -> Getting a s a -> a
^. Getting (q b a) (q a b) (q b a)
forall (p :: * -> * -> *) a b c d.
Swapped p =>
Iso (p a b) (p c d) (p b a) (p d c)
swapped)

instance (Swapped p) => Swapped (Flip p) where
  swapped :: p (Flip p b a) (f (Flip p d c)) -> p (Flip p a b) (f (Flip p c d))
swapped = (Flip p a b -> Flip p b a)
-> (Flip p d c -> Flip p c d)
-> Iso (Flip p a b) (Flip p c d) (Flip p b a) (Flip p d c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Flip p a b -> Flip p b a
forall (p :: * -> * -> *) a b.
Swapped p =>
Flip p a b -> Flip p b a
f Flip p d c -> Flip p c d
forall (p :: * -> * -> *) a b.
Swapped p =>
Flip p a b -> Flip p b a
f
    where
      f :: Flip p a b -> Flip p b a
f (Flip p b a
p) = p a b -> Flip p b a
forall k k1 (p :: k -> k1 -> *) (a :: k1) (b :: k).
p b a -> Flip p a b
Flip (p b a
p p b a -> Getting (p a b) (p b a) (p a b) -> p a b
forall s a. s -> Getting a s a -> a
^. Getting (p a b) (p b a) (p a b)
forall (p :: * -> * -> *) a b c d.
Swapped p =>
Iso (p a b) (p c d) (p b a) (p d c)
swapped)

instance (f ~ g, Functor f, Swapped p) => Swapped (Biff p f g) where
  swapped :: p (Biff p f g b a) (f (Biff p f g d c))
-> p (Biff p f g a b) (f (Biff p f g c d))
swapped = (Biff p f g a b -> Biff p g f b a)
-> (Biff p g f d c -> Biff p f g c d)
-> Iso
     (Biff p f g a b) (Biff p f g c d) (Biff p g f b a) (Biff p g f d c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Biff p f g a b -> Biff p g f b a
forall k2 k3 (p :: * -> * -> *) (f :: k2 -> *) (g :: k3 -> *)
       (a :: k2) (b :: k3).
Swapped p =>
Biff p f g a b -> Biff p g f b a
f Biff p g f d c -> Biff p f g c d
forall k2 k3 (p :: * -> * -> *) (f :: k2 -> *) (g :: k3 -> *)
       (a :: k2) (b :: k3).
Swapped p =>
Biff p f g a b -> Biff p g f b a
f
    where
      f :: Biff p f g a b -> Biff p g f b a
f (Biff p (f a) (g b)
p) = p (g b) (f a) -> Biff p g f b a
forall k k1 k2 k3 (p :: k -> k1 -> *) (f :: k2 -> k)
       (g :: k3 -> k1) (a :: k2) (b :: k3).
p (f a) (g b) -> Biff p f g a b
Biff (p (f a) (g b)
p p (f a) (g b)
-> Getting (p (g b) (f a)) (p (f a) (g b)) (p (g b) (f a))
-> p (g b) (f a)
forall s a. s -> Getting a s a -> a
^. Getting (p (g b) (f a)) (p (f a) (g b)) (p (g b) (f a))
forall (p :: * -> * -> *) a b c d.
Swapped p =>
Iso (p a b) (p c d) (p b a) (p d c)
swapped)

instance (Functor f, Swapped p) => Swapped (Tannen f p) where
  swapped :: p (Tannen f p b a) (f (Tannen f p d c))
-> p (Tannen f p a b) (f (Tannen f p c d))
swapped = (Tannen f p a b -> Tannen f p b a)
-> (Tannen f p d c -> Tannen f p c d)
-> Iso
     (Tannen f p a b) (Tannen f p c d) (Tannen f p b a) (Tannen f p d c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Tannen f p a b -> Tannen f p b a
forall (f :: * -> *) (p :: * -> * -> *) a b.
(Functor f, Swapped p) =>
Tannen f p a b -> Tannen f p b a
f Tannen f p d c -> Tannen f p c d
forall (f :: * -> *) (p :: * -> * -> *) a b.
(Functor f, Swapped p) =>
Tannen f p a b -> Tannen f p b a
f
    where
      f :: Tannen f p a b -> Tannen f p b a
f (Tannen f (p a b)
x) = f (p b a) -> Tannen f p b a
forall k k1 k2 (f :: k -> *) (p :: k1 -> k2 -> k) (a :: k1)
       (b :: k2).
f (p a b) -> Tannen f p a b
Tannen (f (p b a) -> Tannen f p b a) -> f (p b a) -> Tannen f p b a
forall a b. (a -> b) -> a -> b
$ (p a b -> p b a) -> f (p a b) -> f (p b a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (p a b -> Getting (p b a) (p a b) (p b a) -> p b a
forall s a. s -> Getting a s a -> a
^. Getting (p b a) (p a b) (p b a)
forall (p :: * -> * -> *) a b c d.
Swapped p =>
Iso (p a b) (p c d) (p b a) (p d c)
swapped) f (p a b)
x

instance Swapped ((,,) x) where
  swapped :: p (x, b, a) (f (x, d, c)) -> p (x, a, b) (f (x, c, d))
swapped = ((x, a, b) -> (x, b, a))
-> ((x, d, c) -> (x, c, d))
-> Iso (x, a, b) (x, c, d) (x, b, a) (x, d, c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (x, a, b) -> (x, b, a)
forall a c b. (a, c, b) -> (a, b, c)
f (x, d, c) -> (x, c, d)
forall a c b. (a, c, b) -> (a, b, c)
f
    where
      f :: (a, c, b) -> (a, b, c)
f (a
x,c
a,b
b) = (a
x,b
b,c
a)

instance Swapped ((,,,) x y) where
  swapped :: p (x, y, b, a) (f (x, y, d, c)) -> p (x, y, a, b) (f (x, y, c, d))
swapped = ((x, y, a, b) -> (x, y, b, a))
-> ((x, y, d, c) -> (x, y, c, d))
-> Iso (x, y, a, b) (x, y, c, d) (x, y, b, a) (x, y, d, c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (x, y, a, b) -> (x, y, b, a)
forall a b d c. (a, b, d, c) -> (a, b, c, d)
f (x, y, d, c) -> (x, y, c, d)
forall a b d c. (a, b, d, c) -> (a, b, c, d)
f
    where
      f :: (a, b, d, c) -> (a, b, c, d)
f (a
x,b
y,d
a,c
b) = (a
x,b
y,c
b,d
a)

instance Swapped ((,,,,) x y z) where
  swapped :: p (x, y, z, b, a) (f (x, y, z, d, c))
-> p (x, y, z, a, b) (f (x, y, z, c, d))
swapped = ((x, y, z, a, b) -> (x, y, z, b, a))
-> ((x, y, z, d, c) -> (x, y, z, c, d))
-> Iso
     (x, y, z, a, b) (x, y, z, c, d) (x, y, z, b, a) (x, y, z, d, c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (x, y, z, a, b) -> (x, y, z, b, a)
forall a b c e d. (a, b, c, e, d) -> (a, b, c, d, e)
f (x, y, z, d, c) -> (x, y, z, c, d)
forall a b c e d. (a, b, c, e, d) -> (a, b, c, d, e)
f
    where
      f :: (a, b, c, e, d) -> (a, b, c, d, e)
f (a
x,b
y,c
z,e
a,d
b) = (a
x,b
y,c
z,d
b,e
a)

instance Swapped ((,,,,,) x y z w) where
  swapped :: p (x, y, z, w, b, a) (f (x, y, z, w, d, c))
-> p (x, y, z, w, a, b) (f (x, y, z, w, c, d))
swapped = ((x, y, z, w, a, b) -> (x, y, z, w, b, a))
-> ((x, y, z, w, d, c) -> (x, y, z, w, c, d))
-> Iso
     (x, y, z, w, a, b)
     (x, y, z, w, c, d)
     (x, y, z, w, b, a)
     (x, y, z, w, d, c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (x, y, z, w, a, b) -> (x, y, z, w, b, a)
forall a b c d f e. (a, b, c, d, f, e) -> (a, b, c, d, e, f)
f (x, y, z, w, d, c) -> (x, y, z, w, c, d)
forall a b c d f e. (a, b, c, d, f, e) -> (a, b, c, d, e, f)
f
    where
      f :: (a, b, c, d, f, e) -> (a, b, c, d, e, f)
f (a
x,b
y,c
z,d
w,f
a,e
b) = (a
x,b
y,c
z,d
w,e
b,f
a)

instance Swapped ((,,,,,,) x y z w v) where
  swapped :: p (x, y, z, w, v, b, a) (f (x, y, z, w, v, d, c))
-> p (x, y, z, w, v, a, b) (f (x, y, z, w, v, c, d))
swapped = ((x, y, z, w, v, a, b) -> (x, y, z, w, v, b, a))
-> ((x, y, z, w, v, d, c) -> (x, y, z, w, v, c, d))
-> Iso
     (x, y, z, w, v, a, b)
     (x, y, z, w, v, c, d)
     (x, y, z, w, v, b, a)
     (x, y, z, w, v, d, c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (x, y, z, w, v, a, b) -> (x, y, z, w, v, b, a)
forall a b c d e g f.
(a, b, c, d, e, g, f) -> (a, b, c, d, e, f, g)
f (x, y, z, w, v, d, c) -> (x, y, z, w, v, c, d)
forall a b c d e g f.
(a, b, c, d, e, g, f) -> (a, b, c, d, e, f, g)
f
    where
      f :: (a, b, c, d, e, g, f) -> (a, b, c, d, e, f, g)
f (a
x,b
y,c
z,d
w,e
v,g
a,f
b) = (a
x,b
y,c
z,d
w,e
v,f
b,g
a)

-- | Ad hoc conversion between \"strict\" and \"lazy\" versions of a structure,
-- such as 'StrictT.Text' or 'StrictB.ByteString'.
class Strict lazy strict | lazy -> strict, strict -> lazy where
  strict :: Iso' lazy strict

#if __GLASGOW_HASKELL__ >= 710
pattern $bStrict :: t -> s
$mStrict :: forall r s t. Strict s t => s -> (t -> r) -> (Void# -> r) -> r
Strict a <- (view strict -> a) where
  Strict t
a = AReview s t -> t -> s
forall b (m :: * -> *) t. MonadReader b m => AReview t b -> m t
review AReview s t
forall lazy strict. Strict lazy strict => Iso' lazy strict
strict t
a

pattern $bLazy :: t -> s
$mLazy :: forall r t s. Strict t s => s -> (t -> r) -> (Void# -> r) -> r
Lazy a <- (view lazy -> a) where
  Lazy t
a = AReview s t -> t -> s
forall b (m :: * -> *) t. MonadReader b m => AReview t b -> m t
review AReview s t
forall lazy strict. Strict lazy strict => Iso' strict lazy
lazy t
a

pattern $bSwapped :: p d c -> p c d
$mSwapped :: forall r (p :: * -> * -> *) c d.
Swapped p =>
p c d -> (p d c -> r) -> (Void# -> r) -> r
Swapped a <- (view swapped -> a) where
  Swapped p d c
a = AReview (p c d) (p d c) -> p d c -> p c d
forall b (m :: * -> *) t. MonadReader b m => AReview t b -> m t
review AReview (p c d) (p d c)
forall (p :: * -> * -> *) a b c d.
Swapped p =>
Iso (p a b) (p c d) (p b a) (p d c)
swapped p d c
a

pattern $bReversed :: t -> t
$mReversed :: forall r t. Reversing t => t -> (t -> r) -> (Void# -> r) -> r
Reversed a <- (view reversed -> a) where
  Reversed t
a = AReview t t -> t -> t
forall b (m :: * -> *) t. MonadReader b m => AReview t b -> m t
review AReview t t
forall a. Reversing a => Iso' a a
reversed t
a
#endif

instance Strict LazyB.ByteString StrictB.ByteString where
#if MIN_VERSION_bytestring(0,10,0)
  strict :: p ByteString (f ByteString) -> p ByteString (f ByteString)
strict = (ByteString -> ByteString)
-> (ByteString -> ByteString) -> Iso' ByteString ByteString
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ByteString -> ByteString
LazyB.toStrict ByteString -> ByteString
LazyB.fromStrict
#else
  strict = iso (StrictB.concat . LazyB.toChunks) (LazyB.fromChunks . return)
#endif
  {-# INLINE strict #-}

instance Strict LazyT.Text StrictT.Text where
  strict :: p Text (f Text) -> p Text (f Text)
strict = (Text -> Text) -> (Text -> Text) -> Iso' Text Text
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Text -> Text
LazyT.toStrict Text -> Text
LazyT.fromStrict
  {-# INLINE strict #-}

instance Strict (Lazy.StateT s m a) (Strict.StateT s m a) where
  strict :: p (StateT s m a) (f (StateT s m a))
-> p (StateT s m a) (f (StateT s m a))
strict = (StateT s m a -> StateT s m a)
-> (StateT s m a -> StateT s m a)
-> Iso' (StateT s m a) (StateT s m a)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
Strict.StateT ((s -> m (a, s)) -> StateT s m a)
-> (StateT s m a -> s -> m (a, s)) -> StateT s m a -> StateT s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
Lazy.runStateT) ((s -> m (a, s)) -> StateT s m a
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
Lazy.StateT ((s -> m (a, s)) -> StateT s m a)
-> (StateT s m a -> s -> m (a, s)) -> StateT s m a -> StateT s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StateT s m a -> s -> m (a, s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
Strict.runStateT)
  {-# INLINE strict #-}

instance Strict (Lazy.WriterT w m a) (Strict.WriterT w m a) where
  strict :: p (WriterT w m a) (f (WriterT w m a))
-> p (WriterT w m a) (f (WriterT w m a))
strict = (WriterT w m a -> WriterT w m a)
-> (WriterT w m a -> WriterT w m a)
-> Iso' (WriterT w m a) (WriterT w m a)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (m (a, w) -> WriterT w m a
forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
Strict.WriterT (m (a, w) -> WriterT w m a)
-> (WriterT w m a -> m (a, w)) -> WriterT w m a -> WriterT w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterT w m a -> m (a, w)
forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
Lazy.runWriterT) (m (a, w) -> WriterT w m a
forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
Lazy.WriterT (m (a, w) -> WriterT w m a)
-> (WriterT w m a -> m (a, w)) -> WriterT w m a -> WriterT w m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterT w m a -> m (a, w)
forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
Strict.runWriterT)
  {-# INLINE strict #-}

instance Strict (Lazy.RWST r w s m a) (Strict.RWST r w s m a) where
  strict :: p (RWST r w s m a) (f (RWST r w s m a))
-> p (RWST r w s m a) (f (RWST r w s m a))
strict = (RWST r w s m a -> RWST r w s m a)
-> (RWST r w s m a -> RWST r w s m a)
-> Iso' (RWST r w s m a) (RWST r w s m a)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((r -> s -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Strict.RWST ((r -> s -> m (a, s, w)) -> RWST r w s m a)
-> (RWST r w s m a -> r -> s -> m (a, s, w))
-> RWST r w s m a
-> RWST r w s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RWST r w s m a -> r -> s -> m (a, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
Lazy.runRWST) ((r -> s -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
Lazy.RWST ((r -> s -> m (a, s, w)) -> RWST r w s m a)
-> (RWST r w s m a -> r -> s -> m (a, s, w))
-> RWST r w s m a
-> RWST r w s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RWST r w s m a -> r -> s -> m (a, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
Strict.runRWST)
  {-# INLINE strict #-}

instance Strict (Lazy.ST s a) (Strict.ST s a) where
  strict :: p (ST s a) (f (ST s a)) -> p (ST s a) (f (ST s a))
strict = (ST s a -> ST s a) -> (ST s a -> ST s a) -> Iso' (ST s a) (ST s a)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ST s a -> ST s a
forall s a. ST s a -> ST s a
Lazy.lazyToStrictST ST s a -> ST s a
forall s a. ST s a -> ST s a
Lazy.strictToLazyST
  {-# INLINE strict #-}

-- | An 'Iso' between the strict variant of a structure and its lazy
-- counterpart.
--
-- @
-- 'lazy' = 'from' 'strict'
-- @
--
-- See <http://hackage.haskell.org/package/strict-base-types> for an example
-- use.
lazy :: Strict lazy strict => Iso' strict lazy
lazy :: Iso' strict lazy
lazy = AnIso lazy lazy strict strict -> Iso' strict lazy
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso lazy lazy strict strict
forall lazy strict. Strict lazy strict => Iso' lazy strict
strict

-- | An 'Iso' between a list, 'ByteString', 'Text' fragment, etc. and its reversal.
--
-- >>> "live" ^. reversed
-- "evil"
--
-- >>> "live" & reversed %~ ('d':)
-- "lived"
reversed :: Reversing a => Iso' a a
reversed :: Iso' a a
reversed = (a -> a) -> Iso' a a
forall a. (a -> a) -> Iso' a a
involuted a -> a
forall t. Reversing t => t -> t
Iso.reversing

-- | Given a function that is its own inverse, this gives you an 'Iso' using it in both directions.
--
-- @
-- 'involuted' ≡ 'Control.Monad.join' 'iso'
-- @
--
-- >>> "live" ^. involuted reverse
-- "evil"
--
-- >>> "live" & involuted reverse %~ ('d':)
-- "lived"
involuted :: (a -> a) -> Iso' a a
involuted :: (a -> a) -> Iso' a a
involuted a -> a
a = (a -> a) -> (a -> a) -> Iso' a a
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso a -> a
a a -> a
a
{-# INLINE involuted #-}

#if __GLASGOW_HASKELL__ >= 710
pattern $bList :: [Item l] -> l
$mList :: forall r l. IsList l => l -> ([Item l] -> r) -> (Void# -> r) -> r
List a <- (Exts.toList -> a) where
  List [Item l]
a = [Item l] -> l
forall l. IsList l => [Item l] -> l
Exts.fromList [Item l]
a
#endif

------------------------------------------------------------------------------
-- Magma
------------------------------------------------------------------------------

-- | This isomorphism can be used to inspect a 'Traversal' to see how it associates
-- the structure and it can also be used to bake the 'Traversal' into a 'Magma' so
-- that you can traverse over it multiple times.
magma :: LensLike (Mafic a b) s t a b -> Iso s u (Magma Int t b a) (Magma j u c c)
magma :: LensLike (Mafic a b) s t a b
-> Iso s u (Magma Int t b a) (Magma j u c c)
magma LensLike (Mafic a b) s t a b
l = (s -> Magma Int t b a)
-> (Magma j u c c -> u)
-> Iso s u (Magma Int t b a) (Magma j u c c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (Mafic a b t -> Magma Int t b a
forall a b t. Mafic a b t -> Magma Int t b a
runMafic (Mafic a b t -> Magma Int t b a)
-> (s -> Mafic a b t) -> s -> Magma Int t b a
forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
`rmap` LensLike (Mafic a b) s t a b
l a -> Mafic a b b
forall (p :: * -> * -> *) (w :: * -> * -> * -> *) a b.
Sellable p w =>
p a (w a b b)
sell) Magma j u c c -> u
forall i t a. Magma i t a a -> t
runMagma
{-# INLINE magma #-}

-- | This isomorphism can be used to inspect an 'IndexedTraversal' to see how it associates
-- the structure and it can also be used to bake the 'IndexedTraversal' into a 'Magma' so
-- that you can traverse over it multiple times with access to the original indices.
imagma :: Over (Indexed i) (Molten i a b) s t a b -> Iso s t' (Magma i t b a) (Magma j t' c c)
imagma :: Over (Indexed i) (Molten i a b) s t a b
-> Iso s t' (Magma i t b a) (Magma j t' c c)
imagma Over (Indexed i) (Molten i a b) s t a b
l = (s -> Magma i t b a)
-> (Magma j t' c c -> t')
-> Iso s t' (Magma i t b a) (Magma j t' c c)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (Molten i a b t -> Magma i t b a
forall i a b t. Molten i a b t -> Magma i t b a
runMolten (Molten i a b t -> Magma i t b a)
-> (s -> Molten i a b t) -> s -> Magma i t b a
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible c b) =>
q b c -> p a b -> p a c
#. Over (Indexed i) (Molten i a b) s t a b
l Indexed i a (Molten i a b b)
forall (p :: * -> * -> *) (w :: * -> * -> * -> *) a b.
Sellable p w =>
p a (w a b b)
sell) (Molten j c c t' -> t'
forall (w :: * -> * -> * -> *) a t.
IndexedComonad w =>
w a a t -> t
iextract (Molten j c c t' -> t')
-> (Magma j t' c c -> Molten j c c t') -> Magma j t' c c -> t'
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# Magma j t' c c -> Molten j c c t'
forall i a b t. Magma i t b a -> Molten i a b t
Molten)
{-# INLINE imagma #-}

------------------------------------------------------------------------------
-- Contravariant
------------------------------------------------------------------------------

-- | Lift an 'Iso' into a 'Contravariant' functor.
--
-- @
-- contramapping :: 'Contravariant' f => 'Iso' s t a b -> 'Iso' (f a) (f b) (f s) (f t)
-- contramapping :: 'Contravariant' f => 'Iso'' s a -> 'Iso'' (f a) (f s)
-- @
contramapping :: Contravariant f => AnIso s t a b -> Iso (f a) (f b) (f s) (f t)
contramapping :: AnIso s t a b -> Iso (f a) (f b) (f s) (f t)
contramapping AnIso s t a b
f = AnIso s t a b
-> ((s -> a) -> (b -> t) -> p (f s) (f (f t)) -> p (f a) (f (f b)))
-> p (f s) (f (f t))
-> p (f a) (f (f b))
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
f (((s -> a) -> (b -> t) -> p (f s) (f (f t)) -> p (f a) (f (f b)))
 -> p (f s) (f (f t)) -> p (f a) (f (f b)))
-> ((s -> a) -> (b -> t) -> p (f s) (f (f t)) -> p (f a) (f (f b)))
-> p (f s) (f (f t))
-> p (f a) (f (f b))
forall a b. (a -> b) -> a -> b
$ \ s -> a
sa b -> t
bt -> (f a -> f s) -> (f t -> f b) -> Iso (f a) (f b) (f s) (f t)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((s -> a) -> f a -> f s
forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
contramap s -> a
sa) ((b -> t) -> f t -> f b
forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
contramap b -> t
bt)
{-# INLINE contramapping #-}

------------------------------------------------------------------------------
-- Profunctor
------------------------------------------------------------------------------

-- | Lift two 'Iso's into both arguments of a 'Profunctor' simultaneously.
--
-- @
-- dimapping :: 'Profunctor' p => 'Iso' s t a b -> 'Iso' s' t' a' b' -> 'Iso' (p a s') (p b t') (p s a') (p t b')
-- dimapping :: 'Profunctor' p => 'Iso'' s a -> 'Iso'' s' a' -> 'Iso'' (p a s') (p s a')
-- @
dimapping :: (Profunctor p, Profunctor q) => AnIso s t a b -> AnIso s' t' a' b' -> Iso (p a s') (q b t') (p s a') (q t b')
dimapping :: AnIso s t a b
-> AnIso s' t' a' b' -> Iso (p a s') (q b t') (p s a') (q t b')
dimapping AnIso s t a b
f AnIso s' t' a' b'
g = AnIso s t a b
-> ((s -> a)
    -> (b -> t) -> p (p s a') (f (q t b')) -> p (p a s') (f (q b t')))
-> p (p s a') (f (q t b'))
-> p (p a s') (f (q b t'))
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
f (((s -> a)
  -> (b -> t) -> p (p s a') (f (q t b')) -> p (p a s') (f (q b t')))
 -> p (p s a') (f (q t b')) -> p (p a s') (f (q b t')))
-> ((s -> a)
    -> (b -> t) -> p (p s a') (f (q t b')) -> p (p a s') (f (q b t')))
-> p (p s a') (f (q t b'))
-> p (p a s') (f (q b t'))
forall a b. (a -> b) -> a -> b
$ \ s -> a
sa b -> t
bt -> AnIso s' t' a' b'
-> ((s' -> a')
    -> (b' -> t')
    -> p (p s a') (f (q t b'))
    -> p (p a s') (f (q b t')))
-> p (p s a') (f (q t b'))
-> p (p a s') (f (q b t'))
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s' t' a' b'
g (((s' -> a')
  -> (b' -> t')
  -> p (p s a') (f (q t b'))
  -> p (p a s') (f (q b t')))
 -> p (p s a') (f (q t b')) -> p (p a s') (f (q b t')))
-> ((s' -> a')
    -> (b' -> t')
    -> p (p s a') (f (q t b'))
    -> p (p a s') (f (q b t')))
-> p (p s a') (f (q t b'))
-> p (p a s') (f (q b t'))
forall a b. (a -> b) -> a -> b
$ \ s' -> a'
s'a' b' -> t'
b't' ->
  (p a s' -> p s a')
-> (q t b' -> q b t') -> Iso (p a s') (q b t') (p s a') (q t b')
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((s -> a) -> (s' -> a') -> p a s' -> p s a'
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap s -> a
sa s' -> a'
s'a') ((b -> t) -> (b' -> t') -> q t b' -> q b t'
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap b -> t
bt b' -> t'
b't')
{-# INLINE dimapping #-}

-- | Lift an 'Iso' contravariantly into the left argument of a 'Profunctor'.
--
-- @
-- lmapping :: 'Profunctor' p => 'Iso' s t a b -> 'Iso' (p a x) (p b y) (p s x) (p t y)
-- lmapping :: 'Profunctor' p => 'Iso'' s a -> 'Iso'' (p a x) (p s x)
-- @
lmapping :: (Profunctor p, Profunctor q) => AnIso s t a b -> Iso (p a x) (q b y) (p s x) (q t y)
lmapping :: AnIso s t a b -> Iso (p a x) (q b y) (p s x) (q t y)
lmapping AnIso s t a b
f = AnIso s t a b
-> ((s -> a)
    -> (b -> t) -> p (p s x) (f (q t y)) -> p (p a x) (f (q b y)))
-> p (p s x) (f (q t y))
-> p (p a x) (f (q b y))
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
f (((s -> a)
  -> (b -> t) -> p (p s x) (f (q t y)) -> p (p a x) (f (q b y)))
 -> p (p s x) (f (q t y)) -> p (p a x) (f (q b y)))
-> ((s -> a)
    -> (b -> t) -> p (p s x) (f (q t y)) -> p (p a x) (f (q b y)))
-> p (p s x) (f (q t y))
-> p (p a x) (f (q b y))
forall a b. (a -> b) -> a -> b
$ \ s -> a
sa b -> t
bt -> (p a x -> p s x)
-> (q t y -> q b y) -> Iso (p a x) (q b y) (p s x) (q t y)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((s -> a) -> p a x -> p s x
forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap s -> a
sa) ((b -> t) -> q t y -> q b y
forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap b -> t
bt)
{-# INLINE lmapping #-}

-- | Lift an 'Iso' covariantly into the right argument of a 'Profunctor'.
--
-- @
-- rmapping :: 'Profunctor' p => 'Iso' s t a b -> 'Iso' (p x s) (p y t) (p x a) (p y b)
-- rmapping :: 'Profunctor' p => 'Iso'' s a -> 'Iso'' (p x s) (p x a)
-- @
rmapping :: (Profunctor p, Profunctor q) => AnIso s t a b -> Iso (p x s) (q y t) (p x a) (q y b)
rmapping :: AnIso s t a b -> Iso (p x s) (q y t) (p x a) (q y b)
rmapping AnIso s t a b
g = AnIso s t a b
-> ((s -> a)
    -> (b -> t) -> p (p x a) (f (q y b)) -> p (p x s) (f (q y t)))
-> p (p x a) (f (q y b))
-> p (p x s) (f (q y t))
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
g (((s -> a)
  -> (b -> t) -> p (p x a) (f (q y b)) -> p (p x s) (f (q y t)))
 -> p (p x a) (f (q y b)) -> p (p x s) (f (q y t)))
-> ((s -> a)
    -> (b -> t) -> p (p x a) (f (q y b)) -> p (p x s) (f (q y t)))
-> p (p x a) (f (q y b))
-> p (p x s) (f (q y t))
forall a b. (a -> b) -> a -> b
$ \ s -> a
sa b -> t
bt -> (p x s -> p x a)
-> (q y b -> q y t) -> Iso (p x s) (q y t) (p x a) (q y b)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((s -> a) -> p x s -> p x a
forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap s -> a
sa) ((b -> t) -> q y b -> q y t
forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap b -> t
bt)
{-# INLINE rmapping #-}

------------------------------------------------------------------------------
-- Bifunctor
------------------------------------------------------------------------------

-- | Lift two 'Iso's into both arguments of a 'Bifunctor'.
--
-- @
-- bimapping :: 'Bifunctor' p => 'Iso' s t a b -> 'Iso' s' t' a' b' -> 'Iso' (p s s') (p t t') (p a a') (p b b')
-- bimapping :: 'Bifunctor' p => 'Iso'' s a -> 'Iso'' s' a' -> 'Iso'' (p s s') (p a a')
-- @
bimapping :: (Bifunctor f, Bifunctor g) => AnIso s t a b -> AnIso s' t' a' b' -> Iso (f s s') (g t t') (f a a') (g b b')
bimapping :: AnIso s t a b
-> AnIso s' t' a' b' -> Iso (f s s') (g t t') (f a a') (g b b')
bimapping AnIso s t a b
f AnIso s' t' a' b'
g = AnIso s t a b
-> ((s -> a)
    -> (b -> t) -> p (f a a') (f (g b b')) -> p (f s s') (f (g t t')))
-> p (f a a') (f (g b b'))
-> p (f s s') (f (g t t'))
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
f (((s -> a)
  -> (b -> t) -> p (f a a') (f (g b b')) -> p (f s s') (f (g t t')))
 -> p (f a a') (f (g b b')) -> p (f s s') (f (g t t')))
-> ((s -> a)
    -> (b -> t) -> p (f a a') (f (g b b')) -> p (f s s') (f (g t t')))
-> p (f a a') (f (g b b'))
-> p (f s s') (f (g t t'))
forall a b. (a -> b) -> a -> b
$ \ s -> a
sa b -> t
bt -> AnIso s' t' a' b'
-> ((s' -> a')
    -> (b' -> t')
    -> p (f a a') (f (g b b'))
    -> p (f s s') (f (g t t')))
-> p (f a a') (f (g b b'))
-> p (f s s') (f (g t t'))
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s' t' a' b'
g (((s' -> a')
  -> (b' -> t')
  -> p (f a a') (f (g b b'))
  -> p (f s s') (f (g t t')))
 -> p (f a a') (f (g b b')) -> p (f s s') (f (g t t')))
-> ((s' -> a')
    -> (b' -> t')
    -> p (f a a') (f (g b b'))
    -> p (f s s') (f (g t t')))
-> p (f a a') (f (g b b'))
-> p (f s s') (f (g t t'))
forall a b. (a -> b) -> a -> b
$ \s' -> a'
s'a' b' -> t'
b't' ->
  (f s s' -> f a a')
-> (g b b' -> g t t') -> Iso (f s s') (g t t') (f a a') (g b b')
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((s -> a) -> (s' -> a') -> f s s' -> f a a'
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap s -> a
sa s' -> a'
s'a') ((b -> t) -> (b' -> t') -> g b b' -> g t t'
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap b -> t
bt b' -> t'
b't')
{-# INLINE bimapping #-}

-- | Lift an 'Iso' into the first argument of a 'Bifunctor'.
--
-- @
-- firsting :: 'Bifunctor' p => 'Iso' s t a b -> 'Iso' (p s x) (p t y) (p a x) (p b y)
-- firsting :: 'Bifunctor' p => 'Iso'' s a -> 'Iso'' (p s x) (p a x)
-- @
firsting :: (Bifunctor f, Bifunctor g) => AnIso s t a b -> Iso (f s x) (g t y) (f a x) (g b y)
firsting :: AnIso s t a b -> Iso (f s x) (g t y) (f a x) (g b y)
firsting AnIso s t a b
p = AnIso s t a b
-> ((s -> a)
    -> (b -> t) -> p (f a x) (f (g b y)) -> p (f s x) (f (g t y)))
-> p (f a x) (f (g b y))
-> p (f s x) (f (g t y))
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
p (((s -> a)
  -> (b -> t) -> p (f a x) (f (g b y)) -> p (f s x) (f (g t y)))
 -> p (f a x) (f (g b y)) -> p (f s x) (f (g t y)))
-> ((s -> a)
    -> (b -> t) -> p (f a x) (f (g b y)) -> p (f s x) (f (g t y)))
-> p (f a x) (f (g b y))
-> p (f s x) (f (g t y))
forall a b. (a -> b) -> a -> b
$ \ s -> a
sa b -> t
bt -> (f s x -> f a x)
-> (g b y -> g t y) -> Iso (f s x) (g t y) (f a x) (g b y)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((s -> a) -> f s x -> f a x
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first s -> a
sa) ((b -> t) -> g b y -> g t y
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first b -> t
bt)
{-# INLINE firsting #-}

-- | Lift an 'Iso' into the second argument of a 'Bifunctor'. This is
-- essentially the same as 'mapping', but it takes a 'Bifunctor p'
-- constraint instead of a 'Functor (p a)' one.
--
-- @
-- seconding :: 'Bifunctor' p => 'Iso' s t a b -> 'Iso' (p x s) (p y t) (p x a) (p y b)
-- seconding :: 'Bifunctor' p => 'Iso'' s a -> 'Iso'' (p x s) (p x a)
-- @
seconding :: (Bifunctor f, Bifunctor g) => AnIso s t a b -> Iso (f x s) (g y t) (f x a) (g y b)
seconding :: AnIso s t a b -> Iso (f x s) (g y t) (f x a) (g y b)
seconding AnIso s t a b
p = AnIso s t a b
-> ((s -> a)
    -> (b -> t) -> p (f x a) (f (g y b)) -> p (f x s) (f (g y t)))
-> p (f x a) (f (g y b))
-> p (f x s) (f (g y t))
forall s t a b r. AnIso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso AnIso s t a b
p (((s -> a)
  -> (b -> t) -> p (f x a) (f (g y b)) -> p (f x s) (f (g y t)))
 -> p (f x a) (f (g y b)) -> p (f x s) (f (g y t)))
-> ((s -> a)
    -> (b -> t) -> p (f x a) (f (g y b)) -> p (f x s) (f (g y t)))
-> p (f x a) (f (g y b))
-> p (f x s) (f (g y t))
forall a b. (a -> b) -> a -> b
$ \ s -> a
sa b -> t
bt -> (f x s -> f x a)
-> (g y b -> g y t) -> Iso (f x s) (g y t) (f x a) (g y b)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((s -> a) -> f x s -> f x a
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second s -> a
sa) ((b -> t) -> g y b -> g y t
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second b -> t
bt)
{-# INLINE seconding #-}

#if __GLASGOW_HASKELL__ >= 708
-- | Data types that are representationally equal are isomorphic.
--
-- This is only available on GHC 7.8+
--
-- @since 4.13
coerced :: forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
# if __GLASGOW_HASKELL__ >= 710
coerced :: Iso s t a b
coerced p a (f b)
l = (f b -> f t) -> p a (f b) -> p a (f t)
forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap ((b -> t) -> f b -> f t
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> t
forall a b. Coercible a b => b -> a
coerce') p a (f b)
l p a (f t) -> (s -> a) -> p s (f t)
forall (p :: * -> * -> *) a b c (q :: * -> * -> *).
(Profunctor p, Coercible b a) =>
p b c -> q a b -> p a c
.# s -> a
coerce
# else
coerced l = case sym Coercion :: Coercion a s of
              Coercion -> rmap (fmap coerce') l .# coerce
# endif
{-# INLINE coerced #-}
#endif