- , - . . :
{-
import Data.IORef
import Data.Proxy
newtype REPL m f = REPL { run :: forall a. m a -> IO (f a) }
newREPL :: (Monad m) => Proxy m -> (forall a. m a -> f a) -> IO (REPL m f)
newREPL _ runM = do
accum <- newIORef (return ())
return $ REPL (\nextAction -> do
actions <- readIORef accum
writeIORef accum (actions >> nextAction >> return ())
return (runM (actions >> nextAction)))
, , IORef, , -, .
, newREPL, Proxy "", . , m a -> f a m a -> a, , - , , f like:
data StateOutput a = StateOutput a [Int]
deriving (Show)
Identity, .
Proxy , ghci defaulting , repl.
:
>>> repl <- newREPL (Proxy :: Proxy DSL) (\m -> Identity (evalState m []))
>>> run repl $ push 1
Identity ()
>>> run repl $ push 2
Identity ()
>>> run repl $ pop
Identity 2
>>> run repl $ pop
Identity 1
Identity , :
newtype LineOutput a = LineOutput a
instance (Show a) => Show (LineOutput a) where
show (LineOutput x) = show x
, -
type DSL a = State [Int] a
type DSL = State [Int]
, , , Proxy :: DSL. , , .