Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/Python/Inline/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module Python.Inline.Monad
( -- * Type classes
Namespace(..)
, MonadPy(..)
, evalM
, execM
, evalPyFunctionM
) where

import Python.Internal.Eval
Expand All @@ -13,6 +16,8 @@ import Python.Internal.Types
-- wrapper around 'Py'.
--
-- For @Py@ global variables are 'Main' and local are 'Temp'
--
-- @since 0.3
class Monad m => MonadPy m where
-- | Lift @Py@ computation into given monad.
liftPy :: Py a -> m a
Expand All @@ -31,3 +36,41 @@ instance MonadPy Py where
liftPy = id
withGlobals f = f Main
withLocals f = f Temp


-- | Analog of 'eval' which uses globals and locals carried by 'MonadPy'
--
-- @since 0.3
evalM
:: (MonadPy m)
=> PyQuote -- ^ Source code
-> m PyObject
evalM q =
withGlobals $ \globals ->
withLocals $ \locals ->
liftPy $ eval globals locals q

-- | Analog of 'exec' which uses globals and locals carried by
-- 'MonadPy'
--
-- @since 0.3
execM
:: (MonadPy m)
=> PyQuote -- ^ Source code
-> m ()
execM q =
withGlobals $ \globals ->
withLocals $ \locals ->
liftPy $ exec globals locals q

-- | Analog of 'evalPyFunction' which uses globals and locals carried by
-- 'MonadPy'.
--
-- @since 0.3
evalPyFunctionM
:: (MonadPy m)
=> PyQuote -- ^ Source code
-> m PyObject
evalPyFunctionM q =
withGlobals $ \globals ->
liftPy $ evalPyFunction globals q
Loading