File: Test_ReaderContT.hs

package info (click to toggle)
haskell-monad-par 0.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208 kB
  • sloc: haskell: 1,583; makefile: 19
file content (35 lines) | stat: -rw-r--r-- 983 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{-# LANGUAGE CPP #-}


import Control.Monad.Cont as C
import qualified Control.Monad.Reader as R
import Data.IORef
import Text.Printf

type M a = R.ReaderT Int ContIO a
type ContIO = C.ContT () IO

test ref = do 
  x <- R.ask 
  liftIO$ printf "Observed value %d before callCC\n" x 
  callCC$ \cont -> do
     y <- R.ask 
     liftIO$ writeIORef ref cont
     liftIO$ printf "Observed value %d inside callCC\n" y
  z <- R.ask 
  liftIO$ printf "Observed value %d in invoked continuation\n" z
  return ()

main = do ref <- newIORef (error "unused")
          let test' = do test ref
	      m1 = do R.runReaderT test' (100::Int)

          C.runContT m1 (\ () -> return ())
          k <- readIORef ref
          let m2 = do w <- R.ask 
		      liftIO$ putStrLn ("  In new runReader instance: observed " ++ show w)
		      k ()
		      liftIO$ putStrLn " !! Should not reach here..."

          C.runContT (R.runReaderT m2 200) (\ () -> return ())
          putStrLn "Done with main."