Ticket #1369: Bug.hs

File Bug.hs, 0.9 KB (added by guest, 6 years ago)
Line 
1{-# OPTIONS -fglasgow-exts #-}
2module Bug where
3
4import Control.Monad.ST
5import Data.STRef
6
7---------------------------------------------------------------------------
8
9newtype M s a =
10  MkM (STRef s Int -> ST s a)
11
12runM :: (forall s . M s a) -> a
13runM mm =
14  runST (
15    do ref <- newSTRef 0
16       m ref
17  )
18 where
19  MkM m = mm
20
21---------------------------------------------------------------------------
22
23-- the instance declaration and function definition of "inc" are just here
24-- for giving context; removing them still makes runM not type check.
25
26instance Monad (M s) where
27  return x =
28    MkM (\_ -> return x)
29
30  MkM m >>= k =
31    MkM (\ref ->
32      do x <- m ref
33         let MkM m' = k x
34         m' ref
35    )
36
37inc :: M s Int
38inc = MkM (\ref ->
39  do n <- readSTRef ref
40     writeSTRef ref (n+1)
41     return n
42  )
43
44---------------------------------------------------------------------------