{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE KindSignatures      #-}
{-# LANGUAGE PatternSynonyms     #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE TypeInType          #-}
{-# LANGUAGE TypeOperators       #-}

-- |
-- Module      : Data.Type.Functor.XProduct
-- Copyright   : (c) Justin Le 2018
-- License     : BSD3
--
-- Maintainer  : justin@jle.im
-- Stability   : experimental
-- Portability : non-portable
--
-- Generalize "Data.Vinyl.XRec": provides a version of products in
-- "Data.Type.Functor.Product" that "erases" newtype wrappers and other
-- syntactical noise.
--
-- "Data.Type.Functor.Product" is the "main functionality", but this module
-- provides an alternative interface that may be more convenient in some
-- situations, in the same way that 'XRec' can be more convenient than
-- 'Rec' in some situations.
--
module Data.Type.Functor.XProduct (
    XProd
  , fromXProd
  , toXProd
  -- * Functions
  , mapProdX, mapProdXEndo
  , imapProdX, zipWithProdX
  , ixProdX, traverseProdX, traverseProdXEndo, itraverseProdX
  , foldMapProdX, ifoldMapProdX
  -- * Instances
  , XRec, pattern (::&), pattern XRNil
  , XMaybe, pattern XNothing, pattern XJust
  , XEither, pattern XLeft, pattern XRight
  , XNERec, pattern (::&|)
  , XTup, pattern XTup
  , XIdentity, pattern XIdentity
  ) where

import           Data.Functor.Identity
import           Data.Kind
import           Data.List.NonEmpty        (NonEmpty(..))
import           Data.Singletons
import           Data.Type.Functor.Product
import           Data.Vinyl
import           Data.Vinyl.XRec
import           Lens.Micro
import qualified Data.Vinyl.Functor        as V

-- | Generalize 'XRec' to work over any foldable @f@ that implements
-- 'FProd'.  See 'Prod' and 'FProd' for more information.
type XProd f g = (Prod f (XData g) :: f k -> Type)

-- | Convert an 'XProd' back into a regular ol' 'Prod'.
fromXProd :: forall f g as. (FProd f, PureProdC f (IsoHKD g) as) => XProd f g as -> Prod f g as
fromXProd :: forall {k} (f :: * -> *) (g :: k -> *) (as :: f k).
(FProd f, PureProdC f (IsoHKD g) as) =>
XProd f g as -> Prod f g as
fromXProd = forall (f :: * -> *) {k} (g :: k -> *) (h :: k -> *) (j :: k -> *)
       (as :: f k).
FProd f =>
(forall (a :: k). g a -> h a -> j a)
-> Prod f g as -> Prod f h as -> Prod f j as
zipWithProd (\(V.Lift XData g a -> g a
u) XData g a
x -> XData g a -> g a
u XData g a
x)
              (forall {k} (f :: * -> *) (c :: k -> Constraint) (as :: f k)
       (g :: k -> *).
PureProdC f c as =>
(forall (a :: k). c a => g a) -> Prod f g as
pureProdC @_ @(IsoHKD g) (forall l l' k (op :: l -> l' -> *) (f :: k -> l) (g :: k -> l')
       (x :: k).
op (f x) (g x) -> Lift op f g x
V.Lift (forall {k} (f :: k -> *) (a :: k). IsoHKD f a => HKD f a -> f a
unHKD forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (t :: k -> *) (a :: k). XData t a -> HKD t a
unX)))

-- | Convert a 'Prod' into a fancy 'XProd'.
toXProd :: forall f g as. (FProd f, PureProdC f (IsoHKD g) as) => Prod f g as -> XProd f g as
toXProd :: forall {k} (f :: * -> *) (g :: k -> *) (as :: f k).
(FProd f, PureProdC f (IsoHKD g) as) =>
Prod f g as -> XProd f g as
toXProd = forall (f :: * -> *) {k} (g :: k -> *) (h :: k -> *) (j :: k -> *)
       (as :: f k).
FProd f =>
(forall (a :: k). g a -> h a -> j a)
-> Prod f g as -> Prod f h as -> Prod f j as
zipWithProd (\(V.Lift g a -> XData g a
u) g a
x -> g a -> XData g a
u g a
x)
              (forall {k} (f :: * -> *) (c :: k -> Constraint) (as :: f k)
       (g :: k -> *).
PureProdC f c as =>
(forall (a :: k). c a => g a) -> Prod f g as
pureProdC @_ @(IsoHKD g) (forall l l' k (op :: l -> l' -> *) (f :: k -> l) (g :: k -> l')
       (x :: k).
op (f x) (g x) -> Lift op f g x
V.Lift (forall {k} (t :: k -> *) (a :: k). HKD t a -> XData t a
XData forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (f :: k -> *) (a :: k). IsoHKD f a => f a -> HKD f a
toHKD)))

-- | Convenient wrapper over 'mapProd' that lets you deal with the
-- "simplified" inner types.  Generalizes 'rmapX'.
mapProdX
    :: forall f g h as. FProd f
    => (forall a. HKD g a -> HKD h a)
    -> XProd f g as
    -> XProd f h as
mapProdX :: forall {k} (f :: * -> *) (g :: k -> *) (h :: k -> *) (as :: f k).
FProd f =>
(forall (a :: k). HKD g a -> HKD h a)
-> XProd f g as -> XProd f h as
mapProdX forall (a :: k). HKD g a -> HKD h a
f = forall {k} (f :: * -> *) (g :: k -> *) (h :: k -> *) (as :: f k).
FProd f =>
(forall (a :: k). g a -> h a) -> Prod f g as -> Prod f h as
mapProd forall a b. (a -> b) -> a -> b
$ \(XData HKD g a
x :: XData g a) -> forall {k} (t :: k -> *) (a :: k). HKD t a -> XData t a
XData (forall (a :: k). HKD g a -> HKD h a
f @a HKD g a
x)

-- | A version of 'mapProdX' that doesn't change the context @g@; this can
-- be easier for type inference in some situations.  Generalizes
-- 'rmapXEndo'.
mapProdXEndo
    :: forall f g as. FProd f
    => (forall a. HKD g a -> HKD g a)
    -> XProd f g as
    -> XProd f g as
mapProdXEndo :: forall {k} (f :: * -> *) (g :: k -> *) (as :: f k).
FProd f =>
(forall (a :: k). HKD g a -> HKD g a)
-> XProd f g as -> XProd f g as
mapProdXEndo forall (a :: k). HKD g a -> HKD g a
f = forall {k} (f :: * -> *) (g :: k -> *) (h :: k -> *) (as :: f k).
FProd f =>
(forall (a :: k). g a -> h a) -> Prod f g as -> Prod f h as
mapProd forall a b. (a -> b) -> a -> b
$ \(XData HKD g a
x :: XData g a) -> forall {k} (t :: k -> *) (a :: k). HKD t a -> XData t a
XData (forall (a :: k). HKD g a -> HKD g a
f @a HKD g a
x)

-- | A version of 'mapProdX' that passes along the index 'Elem' with each
-- value.  This can help with type inference in some situations.
imapProdX
    :: forall f g h as. FProd f
    => (forall a. Elem f as a -> HKD g a -> HKD h a)
    -> XProd f g as
    -> XProd f h as
imapProdX :: forall {k} (f :: * -> *) (g :: k -> *) (h :: k -> *) (as :: f k).
FProd f =>
(forall (a :: k). Elem f as a -> HKD g a -> HKD h a)
-> XProd f g as -> XProd f h as
imapProdX forall (a :: k). Elem f as a -> HKD g a -> HKD h a
f = forall {k} (f :: * -> *) (as :: f k) (g :: k -> *) (h :: k -> *).
FProd f =>
(forall (a :: k). Elem f as a -> g a -> h a)
-> Prod f g as -> Prod f h as
imapProd forall a b. (a -> b) -> a -> b
$ \Elem f as a
i -> forall {k} (t :: k -> *) (a :: k). HKD t a -> XData t a
XData forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: k). Elem f as a -> HKD g a -> HKD h a
f Elem f as a
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (t :: k -> *) (a :: k). XData t a -> HKD t a
unX

-- | Zip two 'XProd's together by supplying a function that works on their
-- simplified 'HKD' values.
zipWithProdX
    :: forall f g h j as. FProd f
    => (forall a. HKD g a -> HKD h a -> HKD j a)
    -> XProd f g as
    -> XProd f h as
    -> XProd f j as
zipWithProdX :: forall {k} (f :: * -> *) (g :: k -> *) (h :: k -> *) (j :: k -> *)
       (as :: f k).
FProd f =>
(forall (a :: k). HKD g a -> HKD h a -> HKD j a)
-> XProd f g as -> XProd f h as -> XProd f j as
zipWithProdX forall (a :: k). HKD g a -> HKD h a -> HKD j a
f = forall (f :: * -> *) {k} (g :: k -> *) (h :: k -> *) (j :: k -> *)
       (as :: f k).
FProd f =>
(forall (a :: k). g a -> h a -> j a)
-> Prod f g as -> Prod f h as -> Prod f j as
zipWithProd forall a b. (a -> b) -> a -> b
$ \(XData HKD g a
x :: XData g a) (XData HKD h a
y) -> forall {k} (t :: k -> *) (a :: k). HKD t a -> XData t a
XData (forall (a :: k). HKD g a -> HKD h a -> HKD j a
f @a HKD g a
x HKD h a
y)

-- | Given an index into an 'XProd', provides a lens into the simplified
-- item that that index points to.
ixProdX
    :: FProd f
    => Elem f as a
    -> Lens' (XProd f g as) (HKD g a)
ixProdX :: forall {k} (f :: * -> *) (as :: f k) (a :: k) (g :: k -> *).
FProd f =>
Elem f as a -> Lens' (XProd f g as) (HKD g a)
ixProdX Elem f as a
i = forall (f :: * -> *) {k} (as :: f k) (a :: k) (g :: k -> *).
FProd f =>
Elem f as a -> Lens' (Prod f g as) (g a)
ixProd Elem f as a
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\HKD g a -> f (HKD g a)
f (XData HKD g a
x) -> forall {k} (t :: k -> *) (a :: k). HKD t a -> XData t a
XData forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HKD g a -> f (HKD g a)
f HKD g a
x)

-- | Convenient wrapper over 'traverseProd' that lets you deal with the
-- "simplified" inner types.
traverseProdX
    :: forall f g h m as. (FProd f, Applicative m)
    => (forall a. HKD g a -> m (HKD h a))
    -> XProd f g as
    -> m (XProd f h as)
traverseProdX :: forall {k} (f :: * -> *) (g :: k -> *) (h :: k -> *) (m :: * -> *)
       (as :: f k).
(FProd f, Applicative m) =>
(forall (a :: k). HKD g a -> m (HKD h a))
-> XProd f g as -> m (XProd f h as)
traverseProdX forall (a :: k). HKD g a -> m (HKD h a)
f = forall (f :: * -> *) {k} (g :: k -> *) (h :: k -> *) (as :: f k)
       (m :: * -> *).
(FProd f, Applicative m) =>
(forall (a :: k). g a -> m (h a)) -> Prod f g as -> m (Prod f h as)
traverseProd forall a b. (a -> b) -> a -> b
$ \(XData HKD g a
x :: XData g a) -> forall {k} (t :: k -> *) (a :: k). HKD t a -> XData t a
XData forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (a :: k). HKD g a -> m (HKD h a)
f @a HKD g a
x

-- | A version of 'traverseProdX' that doesn't change the context @g@; this can
-- be easier for type inference in some situations.
traverseProdXEndo
    :: forall f g m as. (FProd f, Applicative m)
    => (forall a. HKD g a -> m (HKD g a))
    -> XProd f g as
    -> m (XProd f g as)
traverseProdXEndo :: forall {k} (f :: * -> *) (g :: k -> *) (m :: * -> *) (as :: f k).
(FProd f, Applicative m) =>
(forall (a :: k). HKD g a -> m (HKD g a))
-> XProd f g as -> m (XProd f g as)
traverseProdXEndo forall (a :: k). HKD g a -> m (HKD g a)
f = forall (f :: * -> *) {k} (g :: k -> *) (h :: k -> *) (as :: f k)
       (m :: * -> *).
(FProd f, Applicative m) =>
(forall (a :: k). g a -> m (h a)) -> Prod f g as -> m (Prod f h as)
traverseProd forall a b. (a -> b) -> a -> b
$ \(XData HKD g a
x :: XData g a) -> forall {k} (t :: k -> *) (a :: k). HKD t a -> XData t a
XData forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (a :: k). HKD g a -> m (HKD g a)
f @a HKD g a
x

-- | A version of 'traverseProdX' that passes along the index 'Elem' with
-- each value.  This can help with type inference in some situations.
itraverseProdX
    :: forall f g h m as. (FProd f, Applicative m)
    => (forall a. Elem f as a -> HKD g a -> m (HKD h a))
    -> XProd f g as
    -> m (XProd f h as)
itraverseProdX :: forall {k} (f :: * -> *) (g :: k -> *) (h :: k -> *) (m :: * -> *)
       (as :: f k).
(FProd f, Applicative m) =>
(forall (a :: k). Elem f as a -> HKD g a -> m (HKD h a))
-> XProd f g as -> m (XProd f h as)
itraverseProdX forall (a :: k). Elem f as a -> HKD g a -> m (HKD h a)
f = forall {k} (f :: * -> *) (m :: * -> *) (as :: f k) (g :: k -> *)
       (h :: k -> *).
(FProd f, Applicative m) =>
(forall (a :: k). Elem f as a -> g a -> m (h a))
-> Prod f g as -> m (Prod f h as)
itraverseProd forall a b. (a -> b) -> a -> b
$ \Elem f as a
i -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {k} (t :: k -> *) (a :: k). HKD t a -> XData t a
XData forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: k). Elem f as a -> HKD g a -> m (HKD h a)
f Elem f as a
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (t :: k -> *) (a :: k). XData t a -> HKD t a
unX

-- | Convenient wrapper over 'foldMapProd' that lets you deal with the
-- "simplified" inner types.
foldMapProdX
    :: forall f g m as. (FProd f, Monoid m)
    => (forall a. HKD g a -> m)
    -> XProd f g as
    -> m
foldMapProdX :: forall {k} (f :: * -> *) (g :: k -> *) m (as :: f k).
(FProd f, Monoid m) =>
(forall (a :: k). HKD g a -> m) -> XProd f g as -> m
foldMapProdX forall (a :: k). HKD g a -> m
f = forall {k} (f :: * -> *) m (g :: k -> *) (as :: f k).
(FProd f, Monoid m) =>
(forall (a :: k). g a -> m) -> Prod f g as -> m
foldMapProd forall a b. (a -> b) -> a -> b
$ \(XData HKD g a
x :: XData g a) -> forall (a :: k). HKD g a -> m
f @a HKD g a
x

-- | A version of 'foldMapProdX' that passes along the index 'Elem' with
-- each value.  This can help with type inference in some situations.
ifoldMapProdX
    :: forall f g m as. (FProd f, Monoid m)
    => (forall a. Elem f as a -> HKD g a -> m)
    -> XProd f g as
    -> m
ifoldMapProdX :: forall {k} (f :: * -> *) (g :: k -> *) m (as :: f k).
(FProd f, Monoid m) =>
(forall (a :: k). Elem f as a -> HKD g a -> m) -> XProd f g as -> m
ifoldMapProdX forall (a :: k). Elem f as a -> HKD g a -> m
f = forall {k} (f :: * -> *) m (as :: f k) (g :: k -> *).
(FProd f, Monoid m) =>
(forall (a :: k). Elem f as a -> g a -> m) -> Prod f g as -> m
ifoldMapProd forall a b. (a -> b) -> a -> b
$ \Elem f as a
i -> forall (a :: k). Elem f as a -> HKD g a -> m
f Elem f as a
i forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (t :: k -> *) (a :: k). XData t a -> HKD t a
unX

-- | 'PMaybe' over 'HKD'-d types.
type XMaybe f    = PMaybe (XData f)

-- | 'PEither' over 'HKD'-d types.
type XEither f   = PEither (XData f)

-- | 'NERec' over 'HKD'-d types.
type XNERec f    = NERec (XData f)

-- | 'PTup' over 'HKD'-d types.
type XTup f      = PTup (XData f)

-- | 'PIdentity' over 'HKD'-d types.
type XIdentity f = PIdentity (XData f)

-- | 'PNothing' for 'XMaybe'.
pattern XNothing :: XMaybe f 'Nothing
pattern $bXNothing :: forall {k} (f :: k -> *). XMaybe f 'Nothing
$mXNothing :: forall {r} {k} {f :: k -> *}.
XMaybe f 'Nothing -> ((# #) -> r) -> ((# #) -> r) -> r
XNothing = PNothing

-- | 'PJust' for 'XMaybe': allows you to provide the simplified type.
pattern XJust :: HKD f a -> XMaybe f ('Just a)
pattern $bXJust :: forall {k} (f :: k -> *) (a :: k). HKD f a -> XMaybe f ('Just a)
$mXJust :: forall {r} {k} {f :: k -> *} {a :: k}.
XMaybe f ('Just a) -> (HKD f a -> r) -> ((# #) -> r) -> r
XJust x = PJust (XData x)

-- | 'PLeft' for 'XEither'.
pattern XLeft :: Sing e -> XEither f ('Left e)
pattern $bXLeft :: forall {k1} {k2} (e :: k1) (f :: k2 -> *).
Sing e -> XEither f ('Left e)
$mXLeft :: forall {r} {k1} {k2} {e :: k1} {f :: k2 -> *}.
XEither f ('Left e) -> (Sing e -> r) -> ((# #) -> r) -> r
XLeft e = PLeft e

-- | 'PRight' for 'XEither': allows you to provide the simplified type.
pattern XRight :: HKD f a -> XEither f ('Right a)
pattern $bXRight :: forall {k} {j} (f :: k -> *) (a :: k).
HKD f a -> XEither f ('Right a)
$mXRight :: forall {r} {k} {j} {f :: k -> *} {a :: k}.
XEither f ('Right a) -> (HKD f a -> r) -> ((# #) -> r) -> r
XRight x = PRight (XData x)

-- | A version of ':&|' that allows you to provide the simplified type, for
-- 'XNERec'.
pattern (::&|) :: HKD f a -> XRec f as -> XNERec f (a ':| as)
pattern x $b::&| :: forall {k} (f :: k -> *) (a :: k) (as :: [k]).
HKD f a -> XRec f as -> XNERec f (a ':| as)
$m::&| :: forall {r} {k} {f :: k -> *} {a :: k} {as :: [k]}.
XNERec f (a ':| as)
-> (HKD f a -> XRec f as -> r) -> ((# #) -> r) -> r
::&| xs = XData x :&| xs

-- | A version of 'PTup' that allows you to provide the simplified type,
-- for 'XTup'.
pattern XTup :: Sing w -> HKD f a -> XTup f '(w, a)
pattern $bXTup :: forall {k1} {k2} (w :: k1) (f :: k2 -> *) (a :: k2).
Sing w -> HKD f a -> XTup f '(w, a)
$mXTup :: forall {r} {k1} {k2} {w :: k1} {f :: k2 -> *} {a :: k2}.
XTup f '(w, a) -> (Sing w -> HKD f a -> r) -> ((# #) -> r) -> r
XTup w x = PTup w (XData x)

-- | A version of 'PIdentity' that allows you to provide the simplified
-- type, for 'XIdentity'.
pattern XIdentity :: HKD f a -> XIdentity f ('Identity a)
pattern $bXIdentity :: forall {k} (f :: k -> *) (a :: k).
HKD f a -> XIdentity f ('Identity a)
$mXIdentity :: forall {r} {k} {f :: k -> *} {a :: k}.
XIdentity f ('Identity a) -> (HKD f a -> r) -> ((# #) -> r) -> r
XIdentity x = PIdentity (XData x)

{-# COMPLETE (::&|)    #-}
{-# COMPLETE XIdentity #-}
{-# COMPLETE XJust     #-}
{-# COMPLETE XLeft     #-}
{-# COMPLETE XNothing  #-}
{-# COMPLETE XRight    #-}
{-# COMPLETE XTup      #-}