[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / interpreter / test / typechecker / t012.hs
1 --!!! runST (the classic rank 2 type example)
2
3 newtype ST s a = MkST (s -> (a,s))
4
5 unST :: ST s a -> (s -> (a,s))
6 unST (MkST f) = f
7
8 runST :: (forall s. ST s a) -> a
9 runST m = case unST m () of { (a,_)  -> 
10           a
11           }
12
13 returnST :: a -> ST s a
14 returnST a = MkST (\s -> (a,s))
15
16 thenST :: ST s a -> (a -> ST s b) -> ST s b
17 thenST m k = MkST (\ s0 -> case unST m s0 of { (a,s1) -> unST (k a) s1 })
18
19 instance Monad (ST s) where
20     return = returnST
21     (>>=)  = thenST
22