{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Polysemy.Uncontrolled
(
Uncontrolled (..),
send,
receive,
runUncontrolledAsState,
runUncontrolledAsStateSem,
runUncontrolledAsInputOutput,
adaptUncontrolledPure,
adaptUncontrolledSem,
runInputAsUncontrolled,
runOutputAsUncontrolled,
runMethodologyAsUncontrolled,
)
where
import Polysemy
import Polysemy.Input
import Polysemy.Methodology
import Polysemy.Output
import Polysemy.State
data Uncontrolled c b m a where
Send :: c -> Uncontrolled c b m ()
Receive :: Uncontrolled c b m b
makeSem ''Uncontrolled
runUncontrolledAsState :: forall s b c r a. Members '[State s] r => (c -> s) -> (s -> b) -> Sem (Uncontrolled c b ': r) a -> Sem r a
runUncontrolledAsState :: forall s b c (r :: EffectRow) a.
Members '[State s] r =>
(c -> s) -> (s -> b) -> Sem (Uncontrolled c b : r) a -> Sem r a
runUncontrolledAsState c -> s
f s -> b
g = forall s b c (r :: EffectRow) a.
Members '[State s] r =>
(c -> Sem r s)
-> (s -> Sem r b) -> Sem (Uncontrolled c b : r) a -> Sem r a
runUncontrolledAsStateSem (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> s
f) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> b
g)
{-# INLINE runUncontrolledAsState #-}
runUncontrolledAsStateSem :: forall s b c r a. Members '[State s] r => (c -> Sem r s) -> (s -> Sem r b) -> Sem (Uncontrolled c b ': r) a -> Sem r a
runUncontrolledAsStateSem :: forall s b c (r :: EffectRow) a.
Members '[State s] r =>
(c -> Sem r s)
-> (s -> Sem r b) -> Sem (Uncontrolled c b : r) a -> Sem r a
runUncontrolledAsStateSem c -> Sem r s
f s -> Sem r b
g = forall (e :: Effect) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret forall a b. (a -> b) -> a -> b
$ \case
Send c
c -> c -> Sem r s
f c
c forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall s (r :: EffectRow). Member (State s) r => s -> Sem r ()
put
Uncontrolled c b (Sem rInitial) x
Receive -> forall s (r :: EffectRow). Member (State s) r => Sem r s
get forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> Sem r b
g
{-# INLINE runUncontrolledAsStateSem #-}
runUncontrolledAsInputOutput :: Members '[Input b, Output c] r => Sem (Uncontrolled c b ': r) a -> Sem r a
runUncontrolledAsInputOutput :: forall b c (r :: EffectRow) a.
Members '[Input b, Output c] r =>
Sem (Uncontrolled c b : r) a -> Sem r a
runUncontrolledAsInputOutput = forall (e :: Effect) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret forall a b. (a -> b) -> a -> b
$ \case
Send c
c -> forall o (r :: EffectRow). Member (Output o) r => o -> Sem r ()
output c
c
Uncontrolled c b (Sem rInitial) x
Receive -> forall i (r :: EffectRow). Member (Input i) r => Sem r i
input
{-# INLINE runUncontrolledAsInputOutput #-}
adaptUncontrolledPure :: Members '[Uncontrolled c' b'] r => (c -> c') -> (b' -> b) -> Sem (Uncontrolled c b ': r) a -> Sem r a
adaptUncontrolledPure :: forall c' b' (r :: EffectRow) c b a.
Members '[Uncontrolled c' b'] r =>
(c -> c') -> (b' -> b) -> Sem (Uncontrolled c b : r) a -> Sem r a
adaptUncontrolledPure c -> c'
f b' -> b
g = forall c b c' b' (r :: EffectRow) a.
Members '[Uncontrolled c' b'] r =>
(c -> Sem r c')
-> (b' -> Sem r b) -> Sem (Uncontrolled c b : r) a -> Sem r a
adaptUncontrolledSem (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. c -> c'
f) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. b' -> b
g)
{-# INLINE adaptUncontrolledPure #-}
adaptUncontrolledSem :: forall c b c' b' r a. Members '[Uncontrolled c' b'] r => (c -> Sem r c') -> (b' -> Sem r b) -> Sem (Uncontrolled c b ': r) a -> Sem r a
adaptUncontrolledSem :: forall c b c' b' (r :: EffectRow) a.
Members '[Uncontrolled c' b'] r =>
(c -> Sem r c')
-> (b' -> Sem r b) -> Sem (Uncontrolled c b : r) a -> Sem r a
adaptUncontrolledSem c -> Sem r c'
f b' -> Sem r b
g = forall (e :: Effect) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret forall a b. (a -> b) -> a -> b
$ \case
Send c
c -> c -> Sem r c'
f c
c forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall c b (r :: EffectRow).
Member (Uncontrolled c b) r =>
c -> Sem r ()
send @c' @b'
Uncontrolled c b (Sem rInitial) x
Receive -> forall c b (r :: EffectRow). Member (Uncontrolled c b) r => Sem r b
receive @c' @b' forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b' -> Sem r b
g
{-# INLINE adaptUncontrolledSem #-}
runInputAsUncontrolled :: forall c b r a. Members '[Uncontrolled c b] r => Sem (Input b ': r) a -> Sem r a
runInputAsUncontrolled :: forall c b (r :: EffectRow) a.
Members '[Uncontrolled c b] r =>
Sem (Input b : r) a -> Sem r a
runInputAsUncontrolled = forall (e :: Effect) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret forall a b. (a -> b) -> a -> b
$ \case
Input b (Sem rInitial) x
Input -> forall c b (r :: EffectRow). Member (Uncontrolled c b) r => Sem r b
receive @c @b
{-# INLINE runInputAsUncontrolled #-}
runOutputAsUncontrolled :: forall c b r a. Members '[Uncontrolled c b] r => Sem (Output c ': r) a -> Sem r a
runOutputAsUncontrolled :: forall c b (r :: EffectRow) a.
Members '[Uncontrolled c b] r =>
Sem (Output c : r) a -> Sem r a
runOutputAsUncontrolled = forall (e :: Effect) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret forall a b. (a -> b) -> a -> b
$ \case
Output c
c -> forall c b (r :: EffectRow).
Member (Uncontrolled c b) r =>
c -> Sem r ()
send @c @b c
c
{-# INLINE runOutputAsUncontrolled #-}
runMethodologyAsUncontrolled :: forall c b r a. Members '[Uncontrolled b c] r => Sem (Methodology b c ': r) a -> Sem r a
runMethodologyAsUncontrolled :: forall c b (r :: EffectRow) a.
Members '[Uncontrolled b c] r =>
Sem (Methodology b c : r) a -> Sem r a
runMethodologyAsUncontrolled = forall (e :: Effect) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret forall a b. (a -> b) -> a -> b
$ \case
Process b
b -> forall c b (r :: EffectRow).
Member (Uncontrolled c b) r =>
c -> Sem r ()
send @b @c b
b forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall c b (r :: EffectRow). Member (Uncontrolled c b) r => Sem r b
receive @b @c
{-# INLINE runMethodologyAsUncontrolled #-}