{-# LANGUAGE AllowAmbiguousTypes #-}
module Data.Effect.Chronicle where
import Data.Functor (($>))
import Data.These (These (That, These, This))
data ChronicleF c a where
Dictate :: c -> ChronicleF c ()
Confess :: c -> ChronicleF c a
data ChronicleH c f a where
Memento :: f a -> ChronicleH c f (Either c a)
Absolve :: a -> f a -> ChronicleH c f a
Condemn :: f a -> ChronicleH c f a
makeEffect [''ChronicleF] [''ChronicleH]
chronicle :: (ChronicleF c <: f, Applicative f) => These c a -> f a
chronicle :: forall c (f :: * -> *) a.
(ChronicleF c <: f, Applicative f) =>
These c a -> f a
chronicle = \case
This c
c -> c -> f a
forall c a (f :: * -> *). SendFOE (ChronicleF c) f => c -> f a
confess c
c
That a
x -> a -> f a
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
These c
c a
x -> c -> f ()
forall c (f :: * -> *). SendFOE (ChronicleF c) f => c -> f ()
dictate c
c f () -> a -> f a
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> a
x