| 1 | {-# OPTIONS -fglasgow-exts #-} |
|---|
| 2 | module Bug where |
|---|
| 3 | |
|---|
| 4 | import Control.Monad.ST |
|---|
| 5 | import Data.STRef |
|---|
| 6 | |
|---|
| 7 | --------------------------------------------------------------------------- |
|---|
| 8 | |
|---|
| 9 | newtype M s a = |
|---|
| 10 | MkM (STRef s Int -> ST s a) |
|---|
| 11 | |
|---|
| 12 | runM :: (forall s . M s a) -> a |
|---|
| 13 | runM 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 | |
|---|
| 26 | instance 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 | |
|---|
| 37 | inc :: M s Int |
|---|
| 38 | inc = MkM (\ref -> |
|---|
| 39 | do n <- readSTRef ref |
|---|
| 40 | writeSTRef ref (n+1) |
|---|
| 41 | return n |
|---|
| 42 | ) |
|---|
| 43 | |
|---|
| 44 | --------------------------------------------------------------------------- |
|---|