-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4230 from effectfully/effectfully/builtins/simpli…
…fy-emitter [Builtins] Simplify 'Emitter'
- Loading branch information
Showing
8 changed files
with
36 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 12 additions & 54 deletions
66
plutus-core/plutus-core/src/PlutusCore/Constant/Dynamic/Emit.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,27 @@ | ||
{-# LANGUAGE RankNTypes #-} | ||
|
||
module PlutusCore.Constant.Dynamic.Emit | ||
( MonadEmitter (..) | ||
, Emitter (..) | ||
, NoEmitterT (..) | ||
, WithEmitterT (..) | ||
( Emitter (..) | ||
, emitM | ||
) where | ||
|
||
import Control.Monad.Except | ||
import Control.Monad.Reader | ||
import Control.Monad.State | ||
import Control.Monad.Trans.Identity | ||
import Data.Text (Text) | ||
|
||
-- | A class for emitting 'Text's in a monadic context (basically, for logging). | ||
class Monad m => MonadEmitter m where | ||
emit :: Text -> m () | ||
|
||
-- | A concrete type implementing 'MonadEmitter'. Useful in signatures of built-in functions that | ||
-- do logging. We don't use any concrete first-order encoding and instead pack a @MonadEmitter m@ | ||
-- constraint internally, so that built-in functions that do logging can work in any monad | ||
-- implementing 'MonadEmitter' (for example, @CkM@ or @CekM@). | ||
-- | A monad for logging that does not hardcode any concrete first-order encoding and instead packs | ||
-- a @Monad m@ constraint and a @Text -> m ()@ argument internally, so that built-in functions that | ||
-- do logging can work in any monad (for example, @CkM@ or @CekM@), for which there exists a | ||
-- logging function. | ||
newtype Emitter a = Emitter | ||
{ unEmitter :: forall m. MonadEmitter m => m a | ||
{ unEmitter :: forall m. Monad m => (Text -> m ()) -> m a | ||
} deriving (Functor) | ||
|
||
-- newtype-deriving doesn't work with 'Emitter'. | ||
instance Applicative Emitter where | ||
pure x = Emitter $ pure x | ||
Emitter f <*> Emitter a = Emitter $ f <*> a | ||
pure x = Emitter $ \_ -> pure x | ||
Emitter f <*> Emitter a = Emitter $ \emit -> f emit <*> a emit | ||
|
||
instance Monad Emitter where | ||
Emitter a >>= f = Emitter $ a >>= unEmitter . f | ||
|
||
instance MonadEmitter Emitter where | ||
emit str = Emitter $ emit str | ||
|
||
-- | A newtype wrapper for providing a 'MonadEmitter' instance by directly providing the function. | ||
newtype WithEmitterT m a = WithEmitterT | ||
{ unWithEmitterT :: (Text -> m ()) -> m a | ||
} deriving | ||
( Functor, Applicative, Monad | ||
, MonadError e, MonadState s | ||
) | ||
via | ||
ReaderT (Text -> m ()) m | ||
|
||
instance Monad m => MonadEmitter (WithEmitterT m) where | ||
emit s = WithEmitterT $ \e -> e s | ||
|
||
instance MonadTrans WithEmitterT where | ||
lift a = WithEmitterT $ const a | ||
|
||
-- | A newtype wrapper for via-deriving a vacuous 'MonadEmitter' instance for a monad. | ||
newtype NoEmitterT m a = NoEmitterT | ||
{ unNoEmitterT :: m a | ||
} deriving | ||
( Functor, Applicative, Monad | ||
, MonadReader r, MonadError e, MonadState s | ||
) | ||
via | ||
IdentityT m | ||
|
||
instance Monad m => MonadEmitter (NoEmitterT m) where | ||
emit _ = pure () | ||
Emitter a >>= f = Emitter $ \emit -> a emit >>= \x -> unEmitter (f x) emit | ||
|
||
instance (MonadEmitter m) => MonadEmitter (ExceptT e m) where | ||
emit = lift . emit | ||
emitM :: Text -> Emitter () | ||
emitM text = Emitter ($ text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters