{-|
Module      : Polysemy.Vinyl
License     : MIT
Maintainer  : dan.firth@homotopic.tech
Stability   : experimental

Extra functions for using vinyl records with polysemy.
-}
{-# LANGUAGE BlockArguments      #-}
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE PolyKinds           #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE TypeOperators       #-}
module Polysemy.Vinyl (
  rContramapInput
, rContramapInput'
, rMapOutput
, rMapOutput'
, separateRecInput
, separateRecInput'
, stripRecInput
, endRecInput
) where

import Control.Arrow
import Control.Applicative
import Data.Vinyl
import Polysemy
import Polysemy.Extra
import Polysemy.Input
import Polysemy.Output

-- | Map an `Input` containing a `Rec` contravariantly via a natural transformation.
-- Uses `rmap`.
--
-- @since 0.1.0.0
rContramapInput :: (RMap xs, Members '[Input (Rec f xs)] r)
                => (forall y. f y -> g y)
                   -- ^ A natural transformation from f to g.
                -> Sem (Input (Rec g xs) ': r) a
                -> Sem r a
rContramapInput :: (forall (y :: u). f y -> g y)
-> Sem (Input (Rec g xs) : r) a -> Sem r a
rContramapInput forall (y :: u). f y -> g y
k = (Rec f xs -> Rec g xs) -> Sem (Input (Rec g xs) : r) a -> Sem r a
forall i i' (r :: [(* -> *) -> * -> *]) a.
Members '[Input i'] r =>
(i' -> i) -> Sem (Input i : r) a -> Sem r a
contramapInput ((forall (y :: u). f y -> g y) -> Rec f xs -> Rec g xs
forall u (rs :: [u]) (f :: u -> *) (g :: u -> *).
RMap rs =>
(forall (x :: u). f x -> g x) -> Rec f rs -> Rec g rs
rmap forall (y :: u). f y -> g y
k)
{-# INLINE rContramapInput #-}

-- | Reinterpreting version of `rContramapInput`.
--
-- @since 0.1.0.0
rContramapInput' :: RMap xs
                 => (forall y. f y -> g y)
                    -- ^ A natural transformation from f to g.
                 -> Sem (Input (Rec g xs) ': r) a
                 -> Sem (Input (Rec f xs) ': r) a
rContramapInput' :: (forall (y :: u). f y -> g y)
-> Sem (Input (Rec g xs) : r) a -> Sem (Input (Rec f xs) : r) a
rContramapInput' forall (y :: u). f y -> g y
k = Sem (Input (Rec g xs) : r) a
-> Sem (Input (Rec g xs) : Input (Rec f xs) : r) a
forall (e2 :: (* -> *) -> * -> *) (e1 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : r) a -> Sem (e1 : e2 : r) a
raiseUnder (Sem (Input (Rec g xs) : r) a
 -> Sem (Input (Rec g xs) : Input (Rec f xs) : r) a)
-> (Sem (Input (Rec g xs) : Input (Rec f xs) : r) a
    -> Sem (Input (Rec f xs) : r) a)
-> Sem (Input (Rec g xs) : r) a
-> Sem (Input (Rec f xs) : r) a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (y :: u). f y -> g y)
-> Sem (Input (Rec g xs) : Input (Rec f xs) : r) a
-> Sem (Input (Rec f xs) : r) a
forall u (xs :: [u]) (f :: u -> *) (r :: [(* -> *) -> * -> *])
       (g :: u -> *) a.
(RMap xs, Members '[Input (Rec f xs)] r) =>
(forall (y :: u). f y -> g y)
-> Sem (Input (Rec g xs) : r) a -> Sem r a
rContramapInput forall (y :: u). f y -> g y
k
{-# INLINE rContramapInput' #-}

-- | Map an `Output` containing a `Rec` covariantly via a natural transformation.
-- Uses `rmap`.
--
-- @since 0.1.0.0
rMapOutput :: (RMap xs, Members '[Output (Rec g xs)] r)
           => (forall y. f y -> g y)
              -- ^ A natural transformation from f to g.
           -> Sem (Output (Rec f xs) ': r) a
           -> Sem r a
rMapOutput :: (forall (y :: u). f y -> g y)
-> Sem (Output (Rec f xs) : r) a -> Sem r a
rMapOutput forall (y :: u). f y -> g y
k = (Rec f xs -> Rec g xs) -> Sem (Output (Rec f xs) : r) a -> Sem r a
forall o' (r :: [(* -> *) -> * -> *]) o a.
Members '[Output o'] r =>
(o -> o') -> Sem (Output o : r) a -> Sem r a
mapOutput ((forall (y :: u). f y -> g y) -> Rec f xs -> Rec g xs
forall u (rs :: [u]) (f :: u -> *) (g :: u -> *).
RMap rs =>
(forall (x :: u). f x -> g x) -> Rec f rs -> Rec g rs
rmap forall (y :: u). f y -> g y
k)
{-# INLINE rMapOutput #-}

-- | Reinterpreting version of `rMapOutput`.
--
-- @since 0.1.0.0
rMapOutput' :: RMap xs
            => (forall y. f y -> g y)
               -- ^ A natural transformation from f to g.
            -> Sem (Output (Rec f xs) ': r) a
            -> Sem (Output (Rec g xs) ': r) a
rMapOutput' :: (forall (y :: u). f y -> g y)
-> Sem (Output (Rec f xs) : r) a -> Sem (Output (Rec g xs) : r) a
rMapOutput' forall (y :: u). f y -> g y
k = Sem (Output (Rec f xs) : r) a
-> Sem (Output (Rec f xs) : Output (Rec g xs) : r) a
forall (e2 :: (* -> *) -> * -> *) (e1 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
Sem (e1 : r) a -> Sem (e1 : e2 : r) a
raiseUnder (Sem (Output (Rec f xs) : r) a
 -> Sem (Output (Rec f xs) : Output (Rec g xs) : r) a)
-> (Sem (Output (Rec f xs) : Output (Rec g xs) : r) a
    -> Sem (Output (Rec g xs) : r) a)
-> Sem (Output (Rec f xs) : r) a
-> Sem (Output (Rec g xs) : r) a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall (y :: u). f y -> g y)
-> Sem (Output (Rec f xs) : Output (Rec g xs) : r) a
-> Sem (Output (Rec g xs) : r) a
forall u (xs :: [u]) (g :: u -> *) (r :: [(* -> *) -> * -> *])
       (f :: u -> *) a.
(RMap xs, Members '[Output (Rec g xs)] r) =>
(forall (y :: u). f y -> g y)
-> Sem (Output (Rec f xs) : r) a -> Sem r a
rMapOutput forall (y :: u). f y -> g y
k
{-# INLINE rMapOutput' #-}

-- | Separate one of the fields of an `Input` `Rec` into its own `Input`.
--
-- @since 0.1.2.0
separateRecInput :: forall f x xs r a.
                    Members '[ Input (Rec f xs)
                             , Input (f x)] r
                 => Sem (Input (Rec f (x ': xs)) ': r) a
                 -> Sem r a
separateRecInput :: Sem (Input (Rec f (x : xs)) : r) a -> Sem r a
separateRecInput = (forall x (rInitial :: [(* -> *) -> * -> *]).
 Input (Rec f (x : xs)) (Sem rInitial) x -> Sem r x)
-> Sem (Input (Rec f (x : xs)) : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
FirstOrder e "interpret" =>
(forall x (rInitial :: [(* -> *) -> * -> *]).
 e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret \case
  Input (Rec f (x : xs)) (Sem rInitial) x
Input -> (f x -> Rec f xs -> Rec f (x : xs))
-> Sem r (f x) -> Sem r (Rec f xs) -> Sem r (Rec f (x : xs))
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 f x -> Rec f xs -> Rec f (x : xs)
forall u (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
(:&) (forall (r :: [(* -> *) -> * -> *]).
MemberWithError (Input (f x)) r =>
Sem r (f x)
forall i (r :: [(* -> *) -> * -> *]).
MemberWithError (Input i) r =>
Sem r i
input @(f x)) (forall (r :: [(* -> *) -> * -> *]).
MemberWithError (Input (Rec f xs)) r =>
Sem r (Rec f xs)
forall i (r :: [(* -> *) -> * -> *]).
MemberWithError (Input i) r =>
Sem r i
input @(Rec f xs))
{-# INLINE separateRecInput #-}

-- | Reinterpreting version of `separateRecInput`. This assumes you want to handle
-- the separated case first.
--
-- @since 0.1.2.0
separateRecInput' :: forall f x xs r a.
                     Sem (Input (Rec f (x ': xs)) ': r) a
                  -> Sem (Input (f x) ': Input (Rec f xs) ': r) a
separateRecInput' :: Sem (Input (Rec f (x : xs)) : r) a
-> Sem (Input (f x) : Input (Rec f xs) : r) a
separateRecInput'= (forall (rInitial :: [(* -> *) -> * -> *]) x.
 Input (Rec f (x : xs)) (Sem rInitial) x
 -> Sem (Input (f x) : Input (Rec f xs) : r) x)
-> Sem (Input (Rec f (x : xs)) : r) a
-> Sem (Input (f x) : Input (Rec f xs) : r) a
forall (e1 :: (* -> *) -> * -> *) (e2 :: (* -> *) -> * -> *)
       (e3 :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
FirstOrder e1 "reinterpret2" =>
(forall (rInitial :: [(* -> *) -> * -> *]) x.
 e1 (Sem rInitial) x -> Sem (e2 : e3 : r) x)
-> Sem (e1 : r) a -> Sem (e2 : e3 : r) a
reinterpret2 \case
  Input (Rec f (x : xs)) (Sem rInitial) x
Input -> (f x -> Rec f xs -> Rec f (x : xs))
-> Sem (Input (f x) : Input (Rec f xs) : r) (f x)
-> Sem (Input (f x) : Input (Rec f xs) : r) (Rec f xs)
-> Sem (Input (f x) : Input (Rec f xs) : r) (Rec f (x : xs))
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 f x -> Rec f xs -> Rec f (x : xs)
forall u (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
(:&) (forall (r :: [(* -> *) -> * -> *]).
MemberWithError (Input (f x)) r =>
Sem r (f x)
forall i (r :: [(* -> *) -> * -> *]).
MemberWithError (Input i) r =>
Sem r i
input @(f x)) (Sem (Input (Rec f xs) : r) (Rec f xs)
-> Sem (Input (f x) : Input (Rec f xs) : r) (Rec f xs)
forall (e :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
Sem r a -> Sem (e : r) a
raise (Sem (Input (Rec f xs) : r) (Rec f xs)
 -> Sem (Input (f x) : Input (Rec f xs) : r) (Rec f xs))
-> Sem (Input (Rec f xs) : r) (Rec f xs)
-> Sem (Input (f x) : Input (Rec f xs) : r) (Rec f xs)
forall a b. (a -> b) -> a -> b
$ forall (r :: [(* -> *) -> * -> *]).
MemberWithError (Input (Rec f xs)) r =>
Sem r (Rec f xs)
forall i (r :: [(* -> *) -> * -> *]).
MemberWithError (Input i) r =>
Sem r i
input @(Rec f xs))
{-# INLINE separateRecInput' #-}

-- | Like `separateRecInput`, but places the remainer of the `Rec` at the head
-- of the list while pushing the case into the stack. This is useful when you
-- want to eliminate the record first by repeated applications of `stripRecInput`.
--
-- @since 0.1.2.0
stripRecInput :: forall f x xs r a.
                 Members '[Input (f x)] (Input (Rec f xs) ': r)
              => Sem (Input (Rec f (x ': xs)) ': r) a
              -> Sem (Input (Rec f xs) ':  r) a
stripRecInput :: Sem (Input (Rec f (x : xs)) : r) a -> Sem (Input (Rec f xs) : r) a
stripRecInput = (forall (rInitial :: [(* -> *) -> * -> *]) x.
 Input (Rec f (x : xs)) (Sem rInitial) x
 -> Sem (Input (Rec f xs) : r) x)
-> Sem (Input (Rec f (x : xs)) : r) a
-> Sem (Input (Rec f xs) : r) a
forall (e1 :: (* -> *) -> * -> *) (e2 :: (* -> *) -> * -> *)
       (r :: [(* -> *) -> * -> *]) a.
FirstOrder e1 "reinterpret" =>
(forall (rInitial :: [(* -> *) -> * -> *]) x.
 e1 (Sem rInitial) x -> Sem (e2 : r) x)
-> Sem (e1 : r) a -> Sem (e2 : r) a
reinterpret \case
  Input (Rec f (x : xs)) (Sem rInitial) x
Input -> (f x -> Rec f xs -> Rec f (x : xs))
-> Sem (Input (Rec f xs) : r) (f x)
-> Sem (Input (Rec f xs) : r) (Rec f xs)
-> Sem (Input (Rec f xs) : r) (Rec f (x : xs))
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 f x -> Rec f xs -> Rec f (x : xs)
forall u (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
(:&) (forall (r :: [(* -> *) -> * -> *]).
MemberWithError (Input (f x)) r =>
Sem r (f x)
forall i (r :: [(* -> *) -> * -> *]).
MemberWithError (Input i) r =>
Sem r i
input @(f x)) (forall (r :: [(* -> *) -> * -> *]).
MemberWithError (Input (Rec f xs)) r =>
Sem r (Rec f xs)
forall i (r :: [(* -> *) -> * -> *]).
MemberWithError (Input i) r =>
Sem r i
input @(Rec f xs))
{-# INLINE stripRecInput #-}

-- | Discard a depleted `Rec` `Input` by returning `RNil`.
--
-- @since 0.1.2.0
endRecInput :: Sem (Input (Rec f '[]) ': r) a -> Sem r a
endRecInput :: Sem (Input (Rec f '[]) : r) a -> Sem r a
endRecInput = (forall x (rInitial :: [(* -> *) -> * -> *]).
 Input (Rec f '[]) (Sem rInitial) x -> Sem r x)
-> Sem (Input (Rec f '[]) : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: [(* -> *) -> * -> *]) a.
FirstOrder e "interpret" =>
(forall x (rInitial :: [(* -> *) -> * -> *]).
 e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret \case
  Input (Rec f '[]) (Sem rInitial) x
Input -> Rec f '[] -> Sem r (Rec f '[])
forall (m :: * -> *) a. Monad m => a -> m a
return (Rec f '[] -> Sem r (Rec f '[])) -> Rec f '[] -> Sem r (Rec f '[])
forall a b. (a -> b) -> a -> b
$ Rec f '[]
forall u (a :: u -> *). Rec a '[]
RNil
{-# INLINE endRecInput #-}