{-# LANGUAGE FlexibleContexts #-}

-- | A variant of 'Traversable' for 'Hyper.Type.HyperType's
module Hyper.Class.Traversable
    ( HTraversable (..)
    , ContainedH (..)
    , _ContainedH
    , htraverse
    , htraverse1
    ) where

import Control.Lens (iso)
import GHC.Generics
import GHC.Generics.Lens (generic1, _M1, _Rec1)
import Hyper.Class.Foldable (HFoldable)
import Hyper.Class.Functor (HFunctor (..), hmapped1)
import Hyper.Class.Nodes (HNodes (..), HWitness)
import Hyper.Type (AHyperType, type (#))

import Hyper.Internal.Prelude

-- | A 'Hyper.Type.HyperType' containing a tree inside an action.
--
-- Used to express 'hsequence'.
newtype ContainedH f p (h :: AHyperType) = MkContainedH {forall (f :: * -> *) (p :: AHyperType -> *) (h :: AHyperType).
ContainedH f p h -> f (p h)
runContainedH :: f (p h)}

-- | An 'Iso' for the 'ContainedH' @newtype@
{-# INLINE _ContainedH #-}
_ContainedH ::
    Iso
        (ContainedH f0 p0 # k0)
        (ContainedH f1 p1 # k1)
        (f0 (p0 # k0))
        (f1 (p1 # k1))
_ContainedH :: forall (f0 :: * -> *) (p0 :: AHyperType -> *)
       (k0 :: AHyperType -> *) (f1 :: * -> *) (p1 :: AHyperType -> *)
       (k1 :: AHyperType -> *).
Iso
  (ContainedH f0 p0 # k0)
  (ContainedH f1 p1 # k1)
  (f0 (p0 # k0))
  (f1 (p1 # k1))
_ContainedH = forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso forall (f :: * -> *) (p :: AHyperType -> *) (h :: AHyperType).
ContainedH f p h -> f (p h)
runContainedH forall (f :: * -> *) (p :: AHyperType -> *) (h :: AHyperType).
f (p h) -> ContainedH f p h
MkContainedH

-- | A variant of 'Traversable' for 'Hyper.Type.HyperType's
class (HFunctor h, HFoldable h) => HTraversable h where
    -- | 'HTraversable' variant of 'sequenceA'
    hsequence ::
        Applicative f =>
        h # ContainedH f p ->
        f (h # p)
    {-# INLINE hsequence #-}
    default hsequence ::
        (Generic1 h, HTraversable (Rep1 h), Applicative f) =>
        h # ContainedH f p ->
        f (h # p)
    hsequence = forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> *) (a :: k1) (b :: k2).
(Generic1 f, Generic1 g) =>
Iso (f a) (g b) (Rep1 f a) (Rep1 g b)
generic1 forall (h :: AHyperType -> *) (f :: * -> *) (p :: AHyperType -> *).
(HTraversable h, Applicative f) =>
(h # ContainedH f p) -> f (h # p)
hsequence

instance HTraversable (Const a) where
    {-# INLINE hsequence #-}
    hsequence :: forall (f :: * -> *) (p :: AHyperType -> *).
Applicative f =>
(Const a # ContainedH f p) -> f (Const a # p)
hsequence (Const a
x) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall {k} a (b :: k). a -> Const a b
Const a
x)

instance (HTraversable a, HTraversable b) => HTraversable (a :*: b) where
    {-# INLINE hsequence #-}
    hsequence :: forall (f :: * -> *) (p :: AHyperType -> *).
Applicative f =>
((a :*: b) # ContainedH f p) -> f ((a :*: b) # p)
hsequence (a ('AHyperType (ContainedH f p))
x :*: b ('AHyperType (ContainedH f p))
y) = forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
(:*:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (h :: AHyperType -> *) (f :: * -> *) (p :: AHyperType -> *).
(HTraversable h, Applicative f) =>
(h # ContainedH f p) -> f (h # p)
hsequence a ('AHyperType (ContainedH f p))
x forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (h :: AHyperType -> *) (f :: * -> *) (p :: AHyperType -> *).
(HTraversable h, Applicative f) =>
(h # ContainedH f p) -> f (h # p)
hsequence b ('AHyperType (ContainedH f p))
y

instance (HTraversable a, HTraversable b) => HTraversable (a :+: b) where
    {-# INLINE hsequence #-}
    hsequence :: forall (f :: * -> *) (p :: AHyperType -> *).
Applicative f =>
((a :+: b) # ContainedH f p) -> f ((a :+: b) # p)
hsequence (L1 a ('AHyperType (ContainedH f p))
x) = forall (h :: AHyperType -> *) (f :: * -> *) (p :: AHyperType -> *).
(HTraversable h, Applicative f) =>
(h # ContainedH f p) -> f (h # p)
hsequence a ('AHyperType (ContainedH f p))
x forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1
    hsequence (R1 b ('AHyperType (ContainedH f p))
x) = forall (h :: AHyperType -> *) (f :: * -> *) (p :: AHyperType -> *).
(HTraversable h, Applicative f) =>
(h # ContainedH f p) -> f (h # p)
hsequence b ('AHyperType (ContainedH f p))
x forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1

instance HTraversable h => HTraversable (M1 i m h) where
    {-# INLINE hsequence #-}
    hsequence :: forall (f :: * -> *) (p :: AHyperType -> *).
Applicative f =>
(M1 i m h # ContainedH f p) -> f (M1 i m h # p)
hsequence = forall {k1} {k2} i (c :: Meta) (f :: k1 -> *) (p :: k1) j
       (d :: Meta) (g :: k2 -> *) (q :: k2).
Iso (M1 i c f p) (M1 j d g q) (f p) (g q)
_M1 forall (h :: AHyperType -> *) (f :: * -> *) (p :: AHyperType -> *).
(HTraversable h, Applicative f) =>
(h # ContainedH f p) -> f (h # p)
hsequence

instance HTraversable h => HTraversable (Rec1 h) where
    {-# INLINE hsequence #-}
    hsequence :: forall (f :: * -> *) (p :: AHyperType -> *).
Applicative f =>
(Rec1 h # ContainedH f p) -> f (Rec1 h # p)
hsequence = forall {k1} {k2} (f :: k1 -> *) (p :: k1) (g :: k2 -> *) (q :: k2).
Iso (Rec1 f p) (Rec1 g q) (f p) (g q)
_Rec1 forall (h :: AHyperType -> *) (f :: * -> *) (p :: AHyperType -> *).
(HTraversable h, Applicative f) =>
(h # ContainedH f p) -> f (h # p)
hsequence

-- | 'HTraversable' variant of 'traverse'
{-# INLINE htraverse #-}
htraverse ::
    (Applicative f, HTraversable h) =>
    (forall n. HWitness h n -> p # n -> f (q # n)) ->
    h # p ->
    f (h # q)
htraverse :: forall (f :: * -> *) (h :: AHyperType -> *) (p :: AHyperType -> *)
       (q :: AHyperType -> *).
(Applicative f, HTraversable h) =>
(forall (n :: AHyperType -> *).
 HWitness h n -> (p # n) -> f (q # n))
-> (h # p) -> f (h # q)
htraverse forall (n :: AHyperType -> *). HWitness h n -> (p # n) -> f (q # n)
f = forall (h :: AHyperType -> *) (f :: * -> *) (p :: AHyperType -> *).
(HTraversable h, Applicative f) =>
(h # ContainedH f p) -> f (h # p)
hsequence forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (h :: AHyperType -> *) (p :: AHyperType -> *)
       (q :: AHyperType -> *).
HFunctor h =>
(forall (n :: AHyperType -> *). HWitness h n -> (p # n) -> q # n)
-> (h # p) -> h # q
hmap (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (f :: * -> *) (p :: AHyperType -> *) (h :: AHyperType).
f (p h) -> ContainedH f p h
MkContainedH forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (n :: AHyperType -> *). HWitness h n -> (p # n) -> f (q # n)
f)

-- | 'HTraversable' variant of 'traverse' for 'Hyper.Type.HyperType's with a single node type.
--
-- It is a valid 'Traversal' as it avoids using @RankNTypes@.
{-# INLINE htraverse1 #-}
htraverse1 ::
    (HTraversable h, HNodesConstraint h ((~) n)) =>
    Traversal (h # p) (h # q) (p # n) (q # n)
htraverse1 :: forall (h :: AHyperType -> *) (n :: AHyperType -> *)
       (p :: AHyperType -> *) (q :: AHyperType -> *).
(HTraversable h, HNodesConstraint h ((~) n)) =>
Traversal (h # p) (h # q) (p # n) (q # n)
htraverse1 (p # n) -> f (q # n)
f = forall (h :: AHyperType -> *) (f :: * -> *) (p :: AHyperType -> *).
(HTraversable h, Applicative f) =>
(h # ContainedH f p) -> f (h # p)
hsequence forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (h :: AHyperType -> *) (n :: AHyperType -> *)
       (p :: AHyperType -> *) (q :: AHyperType -> *).
(HFunctor h, HNodesConstraint h ((~) n)) =>
Setter (h # p) (h # q) (p # n) (q # n)
hmapped1 forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ forall (f :: * -> *) (p :: AHyperType -> *) (h :: AHyperType).
f (p h) -> ContainedH f p h
MkContainedH forall b c a. (b -> c) -> (a -> b) -> a -> c
. (p # n) -> f (q # n)
f)