Skip to content

Instantly share code, notes, and snippets.

@blitzcode
Created January 17, 2016 23:17
Show Gist options
  • Save blitzcode/47d727d00085f1177fdc to your computer and use it in GitHub Desktop.
Save blitzcode/47d727d00085f1177fdc to your computer and use it in GitHub Desktop.
monad-control type inferencence
{-# LANGUAGE ScopedTypeVariables, FlexibleContexts #-}
module Main where
import Control.Monad.Trans.Control
import Control.Exception
import Control.Monad.Trans.State
import Control.Monad.IO.Class
withSomething :: forall a m. (MonadBaseControl IO m) => (Int -> m a) -> m (Maybe a)
withSomething f =
control $ \run -> liftIO $ catch
(run $ Just <$> f 10)
(\(_ :: SomeException) -> run $ return (Nothing :: Maybe a))
main :: IO ()
main = do
_ <- flip runStateT (0 :: Int) $ do
withSomething (\_ -> modify' (+ 10))
return ()
{-
main :: IO ()
main = do
_ <- flip runStateT (0 :: Int) $ do
control $ \run -> liftIO $
catch
(putStrLn "Hello World" >> run (modify' (+ 10) >> return (Just (10 :: Int))))
(\(_ :: SomeException) -> run (modify' (+ 10) >> return Nothing))
return ()
-}
name: mc-test
version: 0.1.0.0
synopsis: Simple project template from stack
cabal-version: >=1.10
executable mc-test
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
ghc-options: -Wall
build-depends: base >= 4.7 && < 5
, monad-control
, mtl
, transformers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment