51e7b929e76e0e2b2d79dcdf59dbf5246b0e0b28
[ghc-hetmet.git] / ghc / tests / typecheck / should_fail / tcfail068.hs
1 -- !! Make sure that state threads don't escape
2 -- !! (example from Neil Ashton at York)
3 --
4 module ShouldFail where
5
6 import ST
7
8 type IndTree s t = STArray s (Int,Int) t
9
10 itgen :: Constructed a => (Int,Int) -> a -> IndTree s a
11 itgen n x = 
12         runST (
13         newSTArray ((1,1),n) x)
14
15 itiap :: Constructed a => (Int,Int) -> (a->a) -> IndTree s a -> IndTree s a
16 itiap i f arr =
17         runST (
18         readSTArray arr i >>= \val ->
19         writeSTArray arr i (f val) >>
20         return arr)
21
22 itrap :: Constructed a => ((Int,Int),(Int,Int)) -> (a->a) -> IndTree s a -> IndTree s a
23 itrap ((i,k),(j,l)) f arr = runST(itrap' i k)
24         where
25         itrap' i k = if k > l then return arr
26                      else (itrapsnd i k >>
27                         itrap' i (k+1))
28         itrapsnd i k = if i > j then return arr
29                      else (readSTArray arr (i,k) >>= \val ->
30                         writeSTArray arr (i,k) (f val) >>
31                         itrapsnd (i+1) k)
32
33 itrapstate :: Constructed b => ((Int,Int),(Int,Int)) -> (a->b->(a,b)) -> ((Int,Int)->c->a) ->
34                 (a->c) -> c -> IndTree s b -> (c, IndTree s b)
35 itrapstate ((i,k),(j,l)) f c d s arr = runST(itrapstate' i k s)
36         where
37         itrapstate' i k s = if k > l then return (s,arr)
38                             else (itrapstatesnd i k s >>= \(s,arr) ->
39                                 itrapstate' i (k+1) s)
40         itrapstatesnd i k s = if i > j then return (s,arr)
41                             else (readSTArray arr (i,k) >>= \val ->
42                                let (newstate, newval) = f (c (i,k) s) val
43                                in writeSTArray arr (i,k) newval >>
44                                itrapstatesnd (i+1) k (d newstate))
45
46 -- stuff from Auxiliary: copied here (partain)
47
48 sap :: (a->b) -> (c,a) -> (c,b)
49 sap f (x,y) = (x, f y)
50
51 fap :: (a->b) -> (a,c) -> (b,c)
52 fap f (x,y) = (f x, y)
53
54 nonempty :: [a] -> Bool
55 nonempty []    = False
56 nonempty (_:_) = True
57
58 -- const :: a -> b -> a
59 -- const k x = k
60
61 -- id :: a -> a
62 -- id x = x
63
64 compose :: [a->a] -> a -> a
65 compose = foldr (.) id
66
67 class Constructed a where
68    normal :: a -> Bool
69
70 instance Constructed Bool where
71    normal True = True
72    normal False = True
73
74 instance Constructed Int where
75    normal 0 = True
76    normal n = True
77
78 instance (Constructed a, Constructed b) => Constructed (a,b) where
79    normal (x,y) = normal x && normal y
80
81 -- pair :: (Constructed a, Constructed b) => a -> b -> (a,b)
82 -- pair x y | normal x && normal y = (x,y)
83
84 instance Constructed (Maybe a) where
85    normal Nothing = True
86    normal (Just _) = True
87
88 just :: Constructed a => a -> Maybe a
89 just x | normal x = Just x