{-# LANGUAGE TemplateHaskell #-}
module Polysemy.Trace
(
Trace (..)
, trace
, traceToIO
, runTraceList
, ignoreTrace
, traceToOutput
, outputToTrace
) where
import Polysemy
import Polysemy.Output
data Trace m a where
Trace :: String -> Trace m ()
makeSem ''Trace
traceToIO :: Member (Embed IO) r => Sem (Trace ': r) a -> Sem r a
traceToIO :: Sem (Trace : r) a -> Sem r a
traceToIO = (forall x (rInitial :: EffectRow).
Trace (Sem rInitial) x -> Sem r x)
-> Sem (Trace : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall x (rInitial :: EffectRow). e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret ((forall x (rInitial :: EffectRow).
Trace (Sem rInitial) x -> Sem r x)
-> Sem (Trace : r) a -> Sem r a)
-> (forall x (rInitial :: EffectRow).
Trace (Sem rInitial) x -> Sem r x)
-> Sem (Trace : r) a
-> Sem r a
forall a b. (a -> b) -> a -> b
$ \case
Trace m -> IO () -> Sem r ()
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
embed (IO () -> Sem r ()) -> IO () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ String -> IO ()
putStrLn String
m
{-# INLINE traceToIO #-}
ignoreTrace :: Sem (Trace ': r) a -> Sem r a
ignoreTrace :: Sem (Trace : r) a -> Sem r a
ignoreTrace = (forall x (rInitial :: EffectRow).
Trace (Sem rInitial) x -> Sem r x)
-> Sem (Trace : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall x (rInitial :: EffectRow). e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret ((forall x (rInitial :: EffectRow).
Trace (Sem rInitial) x -> Sem r x)
-> Sem (Trace : r) a -> Sem r a)
-> (forall x (rInitial :: EffectRow).
Trace (Sem rInitial) x -> Sem r x)
-> Sem (Trace : r) a
-> Sem r a
forall a b. (a -> b) -> a -> b
$ \case
Trace _ -> () -> Sem r ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# INLINE ignoreTrace #-}
traceToOutput
:: Member (Output String) r
=> Sem (Trace ': r) a
-> Sem r a
traceToOutput :: Sem (Trace : r) a -> Sem r a
traceToOutput = (forall x (rInitial :: EffectRow).
Trace (Sem rInitial) x -> Sem r x)
-> Sem (Trace : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall x (rInitial :: EffectRow). e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret ((forall x (rInitial :: EffectRow).
Trace (Sem rInitial) x -> Sem r x)
-> Sem (Trace : r) a -> Sem r a)
-> (forall x (rInitial :: EffectRow).
Trace (Sem rInitial) x -> Sem r x)
-> Sem (Trace : r) a
-> Sem r a
forall a b. (a -> b) -> a -> b
$ \case
Trace m -> String -> Sem r ()
forall o (r :: EffectRow).
MemberWithError (Output o) r =>
o -> Sem r ()
output String
m
{-# INLINE traceToOutput #-}
runTraceList
:: Sem (Trace ': r) a
-> Sem r ([String], a)
runTraceList :: Sem (Trace : r) a -> Sem r ([String], a)
runTraceList = Sem (Output String : r) a -> Sem r ([String], a)
forall o (r :: EffectRow) a. Sem (Output o : r) a -> Sem r ([o], a)
runOutputList (Sem (Output String : r) a -> Sem r ([String], a))
-> (Sem (Trace : r) a -> Sem (Output String : r) a)
-> Sem (Trace : r) a
-> Sem r ([String], a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (rInitial :: EffectRow) x.
Trace (Sem rInitial) x -> Sem (Output String : r) x)
-> Sem (Trace : r) a -> Sem (Output String : r) a
forall (e1 :: (* -> *) -> * -> *) (e2 :: (* -> *) -> * -> *)
(r :: EffectRow) a.
FirstOrder e1 "reinterpret" =>
(forall (rInitial :: EffectRow) x.
e1 (Sem rInitial) x -> Sem (e2 : r) x)
-> Sem (e1 : r) a -> Sem (e2 : r) a
reinterpret (
\case
Trace m -> String -> Sem (Output String : r) ()
forall o (r :: EffectRow).
MemberWithError (Output o) r =>
o -> Sem r ()
output String
m
)
{-# INLINE runTraceList #-}
outputToTrace
:: forall w r a
. Member Trace r
=> (w -> String)
-> Sem (Output w ': r) a
-> Sem r a
outputToTrace :: (w -> String) -> Sem (Output w : r) a -> Sem r a
outputToTrace w -> String
show' = (forall x (rInitial :: EffectRow).
Output w (Sem rInitial) x -> Sem r x)
-> Sem (Output w : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall x (rInitial :: EffectRow). e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret ((forall x (rInitial :: EffectRow).
Output w (Sem rInitial) x -> Sem r x)
-> Sem (Output w : r) a -> Sem r a)
-> (forall x (rInitial :: EffectRow).
Output w (Sem rInitial) x -> Sem r x)
-> Sem (Output w : r) a
-> Sem r a
forall a b. (a -> b) -> a -> b
$ \case
Output m -> String -> Sem r ()
forall (r :: EffectRow).
MemberWithError Trace r =>
String -> Sem r ()
trace (String -> Sem r ()) -> String -> Sem r ()
forall a b. (a -> b) -> a -> b
$ w -> String
show' w
m
{-# INLINE outputToTrace #-}