{-# LANGUAGE Trustworthy #-}
module Cleff.Trace
(
Trace (..)
, trace
, runTraceHandle
, runTraceStdout
, runTraceStderr
, ignoreTrace
, traceToOutput
) where
import Cleff
import Cleff.Output
import System.IO (Handle, hPutStrLn, stderr, stdout)
data Trace :: Effect where
Trace :: String -> Trace m ()
makeEffect_ ''Trace
trace :: Trace :> es => String -> Eff es ()
runTraceHandle :: IOE :> es => Handle -> Eff (Trace ': es) a -> Eff es a
runTraceHandle :: Handle -> Eff (Trace : es) a -> Eff es a
runTraceHandle Handle
h = HandlerIO Trace es -> Eff (Trace : es) ~> Eff es
forall (es :: [(Type -> Type) -> Type -> Type])
(e :: (Type -> Type) -> Type -> Type).
(IOE :> es) =>
HandlerIO e es -> Eff (e : es) ~> Eff es
interpretIO \case
Trace s -> Handle -> String -> IO ()
hPutStrLn Handle
h String
s
{-# INLINE runTraceHandle #-}
runTraceStdout :: IOE :> es => Eff (Trace ': es) ~> Eff es
runTraceStdout :: Eff (Trace : es) ~> Eff es
runTraceStdout = Handle -> Eff (Trace : es) a -> Eff es a
forall (es :: [(Type -> Type) -> Type -> Type]) a.
(IOE :> es) =>
Handle -> Eff (Trace : es) a -> Eff es a
runTraceHandle Handle
stdout
{-# INLINE runTraceStdout #-}
runTraceStderr :: IOE :> es => Eff (Trace ': es) ~> Eff es
runTraceStderr :: Eff (Trace : es) ~> Eff es
runTraceStderr = Handle -> Eff (Trace : es) a -> Eff es a
forall (es :: [(Type -> Type) -> Type -> Type]) a.
(IOE :> es) =>
Handle -> Eff (Trace : es) a -> Eff es a
runTraceHandle Handle
stderr
{-# INLINE runTraceStderr #-}
ignoreTrace :: Eff (Trace ': es) ~> Eff es
ignoreTrace :: Eff (Trace : es) a -> Eff es a
ignoreTrace = Handler Trace es -> Eff (Trace : es) ~> Eff es
forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]).
Handler e es -> Eff (e : es) ~> Eff es
interpret \case
Trace _ -> () -> Eff es ()
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure ()
{-# INLINE ignoreTrace #-}
traceToOutput :: Eff (Trace ': es) ~> Eff (Output String ': es)
traceToOutput :: Eff (Trace : es) a -> Eff (Output String : es) a
traceToOutput = Handler Trace (Output String : es)
-> Eff (Trace : es) ~> Eff (Output String : es)
forall (e' :: (Type -> Type) -> Type -> Type)
(e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]).
Handler e (e' : es) -> Eff (e : es) ~> Eff (e' : es)
reinterpret \case
Trace s -> String -> Eff (Output String : es) ()
forall o (es :: [(Type -> Type) -> Type -> Type]).
(Output o :> es) =>
o -> Eff es ()
output String
s
{-# INLINE traceToOutput #-}