Skip to content

Instantly share code, notes, and snippets.

@osa1
Last active August 29, 2015 14:02
Show Gist options
  • Save osa1/034493183fcf6b77cd14 to your computer and use it in GitHub Desktop.
Save osa1/034493183fcf6b77cd14 to your computer and use it in GitHub Desktop.
module Main where
import GHC.Stack
newtype M s a = M { unM :: s -> (s, a) }
instance Monad (M s) where
(M m) >>= k = M $ \s -> case m s of
(s', a) -> unM (k a) s'
return a = M $ \s -> (s, a)
errorM :: String -> M s a
errorM s = M $ \_ -> errorWithStackTrace ("--- " ++ s)
runM :: M s a -> s -> a
runM (M m) s = let (_, a) = m s in a
someF :: Int -> IO String
someF n = replicateM n (return '.')
replicateM :: (Monad m) => Int -> m a -> m [a]
replicateM n x = sequence (replicate n x)
bar xs = mapM foo xs
foo :: String -> M s String
foo s = (\x -> (\s -> errorM s) x) s
main = do
print =<< whoCreated 1
print =<< whoCreated (1 :: Int)
print =<< whoCreated '.'
print =<< whoCreated bar
print =<< currentCallStack
print =<< whoCreated =<< someF 10
print =<< whoCreated someF
print (runM (bar ["a"]) "state")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment