-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8c5183
commit 9f5d839
Showing
7 changed files
with
56 additions
and
56 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
29 changes: 11 additions & 18 deletions
29
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,27 +1,20 @@ | ||
{-# LANGUAGE RankNTypes #-} | ||
|
||
module PlutusCore.Constant.Dynamic.Emit | ||
( Emitter (..) | ||
, emitM | ||
, runEmitter | ||
, emit | ||
) where | ||
|
||
import Control.Monad.Writer.Strict | ||
import Data.DList as DList | ||
import Data.Text (Text) | ||
import Data.Tuple | ||
|
||
-- | 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. 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 $ \emit -> f emit <*> a emit | ||
{ unEmitter :: Writer (DList Text) a | ||
} deriving newtype (Functor, Applicative, Monad) | ||
|
||
instance Monad Emitter where | ||
Emitter a >>= f = Emitter $ \emit -> a emit >>= \x -> unEmitter (f x) emit | ||
runEmitter :: Emitter a -> (DList Text, a) | ||
runEmitter = swap . runWriter . unEmitter | ||
|
||
emitM :: Text -> Emitter () | ||
emitM text = Emitter ($ text) | ||
emit :: Text -> Emitter () | ||
emit text = Emitter . tell $ pure 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