Created
July 8, 2021 19:03
-
-
Save dagit/b21b1fc9b6c6f51d614d755dde95164e to your computer and use it in GitHub Desktop.
shift + reset example
This file contains hidden or 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
module Main where | |
-- | We're using Cont directly from transformers package | |
import Control.Monad.Trans.Cont | |
import Control.Monad.Trans.Writer | |
import Control.Monad.Trans.Class (lift) | |
-- | We're going to be doing a lot of 'print'ing from inside 'ContT' | |
-- so let's give ourselves a conviennt way to do it. | |
cPrint = lift . putStrLn | |
-- | Let's do something in the IO monad so that we can trace the | |
-- execution with 'print' | |
foo :: ContT Int IO Int | |
foo = do | |
cPrint "From the top" | |
r <- resetT $ do | |
cPrint "Start of reset block" | |
shiftT $ \esc1 -> do | |
cPrint "Start of shift1" | |
cPrint "about to esc1" | |
-- Keep in mind, in ContT, the escape continuation is an action | |
-- in the inner monad, so we need to lift it. | |
lift $ esc1 2 | |
cPrint "After esc1" | |
pure 3 | |
shiftT $ \esc2 -> do | |
cPrint "Start of shift2" | |
cPrint "calling esc1 again" | |
lift $ esc1 4 | |
cPrint "after second esc1" | |
cPrint "about to esc2" | |
lift $ esc2 5 | |
cPrint "after esc2" | |
pure 6 | |
pure r | |
prod :: ContT () (Writer [(Char,Int)]) () | |
prod = resetT $ do | |
a <- shiftT $ \yield -> do | |
lift $ yield 'a' | |
lift $ yield 'b' | |
lift $ yield 'c' | |
pure () | |
b <- shiftT $ \yield -> do | |
lift $ yield 1 | |
lift $ yield 2 | |
lift $ yield 3 | |
pure () | |
lift $ tell $ pure (a,b) | |
pure () | |
main :: IO () | |
main = do | |
let m = evalContT foo | |
r <- m | |
putStrLn $ "Final result of foo: " <> show r | |
putStrLn $ "Final result of prod: " <> show (execWriter (evalContT prod)) |
This file contains hidden or 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
cabal-version: 2.4 | |
name: scratch | |
version: 0.1.0.0 | |
-- A short (one-line) description of the package. | |
-- synopsis: | |
-- A longer description of the package. | |
-- description: | |
-- A URL where users can report bugs. | |
-- bug-reports: | |
-- The license under which the package is released. | |
-- license: | |
author: Jason Dagit | |
maintainer: [email protected] | |
-- A copyright notice. | |
-- copyright: | |
-- category: | |
extra-source-files: CHANGELOG.md | |
executable scratch | |
main-is: Main.hs | |
-- Modules included in this executable, other than Main. | |
-- other-modules: | |
-- LANGUAGE extensions used by modules in this package. | |
-- other-extensions: | |
build-depends: base ^>=4.14.1.0 | |
build-depends: transformers | |
hs-source-dirs: . | |
default-language: Haskell2010 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment