{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Carrier.Trace.Printing
(
runTrace
, TraceC(..)
, module Control.Effect.Trace
) where
import Control.Algebra
import Control.Applicative (Alternative)
import Control.Effect.Trace
import Control.Monad (MonadPlus)
import Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import System.IO
runTrace :: TraceC m a -> m a
runTrace (TraceC m) = m
{-# INLINE runTrace #-}
newtype TraceC m a = TraceC (m a)
deriving (Alternative, Applicative, Functor, Monad, Fail.MonadFail, MonadFix, MonadIO, MonadPlus)
instance MonadTrans TraceC where
lift = TraceC
{-# INLINE lift #-}
instance (MonadIO m, Algebra sig m) => Algebra (Trace :+: sig) (TraceC m) where
alg hdl sig ctx = case sig of
L (Trace s) -> ctx <$ liftIO (hPutStrLn stderr s)
R other -> TraceC (alg (runTrace . hdl) other ctx)
{-# INLINE alg #-}