{-# LANGUAGE TemplateHaskell #-}
module Polysemy.Trace
(
Trace (..)
, trace
, runTraceIO
, runIgnoringTrace
, runTraceAsOutput
, runOutputAsTrace
) where
import Polysemy
import Polysemy.Output
data Trace m a where
Trace :: String -> Trace m ()
makeSem ''Trace
runTraceIO :: Member (Lift IO) r => Sem (Trace ': r) a -> Sem r a
runTraceIO = interpret $ \case
Trace m -> sendM $ putStrLn m
{-# INLINE runTraceIO #-}
runIgnoringTrace :: Member (Lift IO) r => Sem (Trace ': r) a -> Sem r a
runIgnoringTrace = interpret $ \case
Trace _ -> pure ()
{-# INLINE runIgnoringTrace #-}
runTraceAsOutput
:: Member (Output String) r
=> Sem (Trace ': r) a
-> Sem r a
runTraceAsOutput = interpret $ \case
Trace m -> output m
{-# INLINE runTraceAsOutput #-}
runOutputAsTrace
:: ( Show w
, Member Trace r
)
=> Sem (Output w ': r) a
-> Sem r a
runOutputAsTrace = interpret $ \case
Output m -> trace $ show m
{-# INLINE runOutputAsTrace #-}