{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE GADTs                     #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE Rank2Types                #-}
{-# LANGUAGE ScopedTypeVariables       #-}
{-# LANGUAGE TypeFamilies              #-}
{-# LANGUAGE TypeFamilyDependencies    #-}
{-# LANGUAGE TypeOperators             #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Data.Generics.Internal.Profunctor.Iso
-- Copyright   :  (C) 2019 Csongor Kiss
-- License     :  BSD3
-- Maintainer  :  Csongor Kiss <kiss.csongor.kiss@gmail.com>
-- Stability   :  experimental
-- Portability :  non-portable
--
-- Internal lens helpers. Only exported for Haddock
--
-----------------------------------------------------------------------------
module Data.Generics.Internal.Profunctor.Iso where

import Data.Profunctor        (Profunctor(..))
import Data.Profunctor.Unsafe ((#.), (.#))
import GHC.Generics           ((:*:)(..), (:+:)(..), Generic(..), M1(..), K1(..), Rep)
import Data.Coerce
import Data.Generics.Internal.GenericN (Rec (..))

import qualified Data.Generics.Internal.VL.Iso as VL

type Iso s t a b
  = forall p. (Profunctor p) => p a b -> p s t

type Iso' s a = Iso s s a a

-- | A type and its generic representation are isomorphic
repIso :: (Generic a, Generic b) => Iso a b (Rep a x) (Rep b x)
repIso :: Iso a b (Rep a x) (Rep b x)
repIso = (a -> Rep a x) -> (Rep b x -> b) -> Iso a b (Rep a x) (Rep b x)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso a -> Rep a x
forall a x. Generic a => a -> Rep a x
from Rep b x -> b
forall a x. Generic a => Rep a x -> a
to

-- | 'M1' is just a wrapper around `f p`
--mIso :: Iso' (M1 i c f p) (f p)
mIso :: Iso (M1 i c f p) (M1 i c g p) (f p) (g p)
mIso :: p (f p) (g p) -> p (M1 i c f p) (M1 i c g p)
mIso = (M1 i c f p -> f p)
-> (g p -> M1 i c g p) -> Iso (M1 i c f p) (M1 i c g p) (f p) (g p)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso M1 i c f p -> f p
forall i (c :: Meta) k (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1 g p -> M1 i c g p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1
{-# INLINE mIso #-}

kIso :: Iso (K1 r a p) (K1 r b p) a b
kIso :: p a b -> p (K1 r a p) (K1 r b p)
kIso = (K1 r a p -> a) -> (b -> K1 r b p) -> Iso (K1 r a p) (K1 r b p) a b
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso K1 r a p -> a
forall i c k (p :: k). K1 i c p -> c
unK1 b -> K1 r b p
forall k i c (p :: k). c -> K1 i c p
K1
{-# INLINE kIso #-}

recIso :: Iso (Rec r a p) (Rec r b p) a b
recIso :: p a b -> p (Rec r a p) (Rec r b p)
recIso = (Rec r a p -> a)
-> (b -> Rec r b p) -> Iso (Rec r a p) (Rec r b p) a b
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (K1 R a p -> a
forall i c k (p :: k). K1 i c p -> c
unK1 (K1 R a p -> a) -> (Rec r a p -> K1 R a p) -> Rec r a p -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rec r a p -> K1 R a p
forall p a k (x :: k). Rec p a x -> K1 R a x
unRec) (K1 R b p -> Rec r b p
forall k p a (x :: k). K1 R a x -> Rec p a x
Rec (K1 R b p -> Rec r b p) -> (b -> K1 R b p) -> b -> Rec r b p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> K1 R b p
forall k i c (p :: k). c -> K1 i c p
K1)
{-# INLINE recIso #-}

sumIso :: Iso ((a :+: b) x) ((a' :+: b') x) (Either (a x) (b x)) (Either (a' x) (b' x))
sumIso :: p (Either (a x) (b x)) (Either (a' x) (b' x))
-> p ((:+:) a b x) ((:+:) a' b' x)
sumIso = ((:+:) a b x -> Either (a x) (b x))
-> (Either (a' x) (b' x) -> (:+:) a' b' x)
-> Iso
     ((:+:) a b x)
     ((:+:) a' b' x)
     (Either (a x) (b x))
     (Either (a' x) (b' x))
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (:+:) a b x -> Either (a x) (b x)
forall (f :: * -> *) (g :: * -> *) p.
(:+:) f g p -> Either (f p) (g p)
back Either (a' x) (b' x) -> (:+:) a' b' x
forall (f :: * -> *) p (g :: * -> *).
Either (f p) (g p) -> (:+:) f g p
forth
  where forth :: Either (f p) (g p) -> (:+:) f g p
forth (Left l :: f p
l)  = f p -> (:+:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 f p
l
        forth (Right r :: g p
r) = g p -> (:+:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 g p
r
        back :: (:+:) f g p -> Either (f p) (g p)
back (L1 l :: f p
l) = f p -> Either (f p) (g p)
forall a b. a -> Either a b
Left f p
l
        back (R1 r :: g p
r) = g p -> Either (f p) (g p)
forall a b. b -> Either a b
Right g p
r
{-# INLINE sumIso #-}

prodIso :: Iso ((a :*: b) x) ((a' :*: b') x) (a x, b x) (a' x, b' x)
prodIso :: p (a x, b x) (a' x, b' x) -> p ((:*:) a b x) ((:*:) a' b' x)
prodIso = ((:*:) a b x -> (a x, b x))
-> ((a' x, b' x) -> (:*:) a' b' x)
-> Iso ((:*:) a b x) ((:*:) a' b' x) (a x, b x) (a' x, b' x)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\(a :: a x
a :*: b :: b x
b) -> (a x
a, b x
b)) (\(a :: a' x
a, b :: b' x
b) -> (a' x
a a' x -> b' x -> (:*:) a' b' x
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: b' x
b))

assoc3 :: Iso ((a, b), c) ((a', b'), c') (a, (b, c)) (a', (b', c'))
assoc3 :: p (a, (b, c)) (a', (b', c')) -> p ((a, b), c) ((a', b'), c')
assoc3 = (((a, b), c) -> (a, (b, c)))
-> ((a', (b', c')) -> ((a', b'), c'))
-> Iso ((a, b), c) ((a', b'), c') (a, (b, c)) (a', (b', c'))
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\((a :: a
a, b :: b
b), c :: c
c) -> (a
a, (b
b, c
c))) (\(a :: a'
a, (b :: b'
b, c :: c'
c)) -> ((a'
a, b'
b), c'
c))

--------------------------------------------------------------------------------
-- Iso stuff

fromIso :: Iso s t a b -> Iso b a t s
fromIso :: Iso s t a b -> Iso b a t s
fromIso l :: Iso s t a b
l = Iso s t a b
-> ((s -> a) -> (b -> t) -> p t s -> p b a) -> p t s -> p b a
forall s t a b r. Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso Iso s t a b
l (((s -> a) -> (b -> t) -> p t s -> p b a) -> p t s -> p b a)
-> ((s -> a) -> (b -> t) -> p t s -> p b a) -> p t s -> p b a
forall a b. (a -> b) -> a -> b
$ \ sa :: s -> a
sa bt :: b -> t
bt -> (b -> t) -> (s -> a) -> Iso b a t s
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso b -> t
bt s -> a
sa
{-# INLINE fromIso #-}

iso :: (s -> a) -> (b -> t) -> Iso s t a b
iso :: (s -> a) -> (b -> t) -> Iso s t a b
iso = (s -> a) -> (b -> t) -> p a b -> p s t
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap
{-# INLINE iso #-}

iso2isovl :: Iso s t a b -> VL.Iso s t a b
iso2isovl :: Iso s t a b -> Iso s t a b
iso2isovl _iso :: Iso s t a b
_iso = Iso 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. Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso Iso s t a b
_iso (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
VL.iso
{-# INLINE iso2isovl #-}

withIso :: Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso :: Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso ai :: Iso s t a b
ai k :: (s -> a) -> (b -> t) -> r
k = case Exchange a b a b -> Exchange a b s t
Iso s t a b
ai ((a -> a) -> (b -> b) -> Exchange a b a b
forall a b s t. (s -> a) -> (b -> t) -> Exchange a b s t
Exchange a -> a
forall a. a -> a
id b -> b
forall a. a -> a
id) of
  Exchange sa :: s -> a
sa bt :: b -> t
bt -> (s -> a) -> (b -> t) -> r
k s -> a
sa b -> t
bt

pairing :: Iso s t a b -> Iso s' t' a' b' -> Iso (s, s') (t, t') (a, a') (b, b')
pairing :: Iso s t a b
-> Iso s' t' a' b' -> Iso (s, s') (t, t') (a, a') (b, b')
pairing f :: Iso s t a b
f g :: Iso s' t' a' b'
g = Iso s t a b
-> ((s -> a) -> (b -> t) -> p (a, a') (b, b') -> p (s, s') (t, t'))
-> p (a, a') (b, b')
-> p (s, s') (t, t')
forall s t a b r. Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso Iso s t a b
f (((s -> a) -> (b -> t) -> p (a, a') (b, b') -> p (s, s') (t, t'))
 -> p (a, a') (b, b') -> p (s, s') (t, t'))
-> ((s -> a) -> (b -> t) -> p (a, a') (b, b') -> p (s, s') (t, t'))
-> p (a, a') (b, b')
-> p (s, s') (t, t')
forall a b. (a -> b) -> a -> b
$ \ sa :: s -> a
sa bt :: b -> t
bt -> Iso s' t' a' b'
-> ((s' -> a')
    -> (b' -> t') -> p (a, a') (b, b') -> p (s, s') (t, t'))
-> p (a, a') (b, b')
-> p (s, s') (t, t')
forall s t a b r. Iso s t a b -> ((s -> a) -> (b -> t) -> r) -> r
withIso Iso s' t' a' b'
g (((s' -> a')
  -> (b' -> t') -> p (a, a') (b, b') -> p (s, s') (t, t'))
 -> p (a, a') (b, b') -> p (s, s') (t, t'))
-> ((s' -> a')
    -> (b' -> t') -> p (a, a') (b, b') -> p (s, s') (t, t'))
-> p (a, a') (b, b')
-> p (s, s') (t, t')
forall a b. (a -> b) -> a -> b
$ \s'a' :: s' -> a'
s'a' b't' :: b' -> t'
b't' ->
  ((s, s') -> (a, a'))
-> ((b, b') -> (t, t')) -> Iso (s, s') (t, t') (a, a') (b, b')
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso ((s -> a) -> (s' -> a') -> (s, s') -> (a, a')
forall t a t b. (t -> a) -> (t -> b) -> (t, t) -> (a, b)
bmap s -> a
sa s' -> a'
s'a') ((b -> t) -> (b' -> t') -> (b, b') -> (t, t')
forall t a t b. (t -> a) -> (t -> b) -> (t, t) -> (a, b)
bmap b -> t
bt b' -> t'
b't')
  where bmap :: (t -> a) -> (t -> b) -> (t, t) -> (a, b)
bmap f' :: t -> a
f' g' :: t -> b
g' (a :: t
a, b :: t
b) = (t -> a
f' t
a, t -> b
g' t
b)

data Exchange a b s t = Exchange (s -> a) (b -> t)

instance Functor (Exchange a b s) where
  fmap :: (a -> b) -> Exchange a b s a -> Exchange a b s b
fmap f :: a -> b
f (Exchange sa :: s -> a
sa bt :: b -> a
bt) = (s -> a) -> (b -> b) -> Exchange a b s b
forall a b s t. (s -> a) -> (b -> t) -> Exchange a b s t
Exchange s -> a
sa (a -> b
f (a -> b) -> (b -> a) -> b -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a
bt)
  {-# INLINE fmap #-}

instance Profunctor (Exchange a b) where
  dimap :: (a -> b) -> (c -> d) -> Exchange a b b c -> Exchange a b a d
dimap f :: a -> b
f g :: c -> d
g (Exchange sa :: b -> a
sa bt :: b -> c
bt) = (a -> a) -> (b -> d) -> Exchange a b a d
forall a b s t. (s -> a) -> (b -> t) -> Exchange a b s t
Exchange (b -> a
sa (b -> a) -> (a -> b) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) (c -> d
g (c -> d) -> (b -> c) -> b -> d
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> c
bt)
  {-# INLINE dimap #-}
  lmap :: (a -> b) -> Exchange a b b c -> Exchange a b a c
lmap f :: a -> b
f (Exchange sa :: b -> a
sa bt :: b -> c
bt) = (a -> a) -> (b -> c) -> Exchange a b a c
forall a b s t. (s -> a) -> (b -> t) -> Exchange a b s t
Exchange (b -> a
sa (b -> a) -> (a -> b) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) b -> c
bt
  {-# INLINE lmap #-}
  rmap :: (b -> c) -> Exchange a b a b -> Exchange a b a c
rmap f :: b -> c
f (Exchange sa :: a -> a
sa bt :: b -> b
bt) = (a -> a) -> (b -> c) -> Exchange a b a c
forall a b s t. (s -> a) -> (b -> t) -> Exchange a b s t
Exchange a -> a
sa (b -> c
f (b -> c) -> (b -> b) -> b -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> b
bt)
  {-# INLINE rmap #-}
  ( #. ) _ = Exchange a b a b -> Exchange a b a c
forall a b. Coercible a b => a -> b
coerce
  {-# INLINE ( #. ) #-}
  ( .# ) p :: Exchange a b b c
p _ = Exchange a b b c -> Exchange a b a c
forall a b. Coercible a b => a -> b
coerce Exchange a b b c
p
  {-# INLINE ( .# ) #-}