[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / compiler / 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 IndTree(IndTree(..), itgen, itiap, itrap, itrapstate) where
5
6 --partain: import Auxiliary
7 import PreludeGlaST
8
9 type IndTree s t = _MutableArray s (Int,Int) t
10
11 itgen :: Constructed a => (Int,Int) -> a -> IndTree s a
12 itgen n x = 
13         _runST (
14         newArray ((1,1),n) x)
15
16 itiap :: Constructed a => (Int,Int) -> (a->a) -> IndTree s a -> IndTree s a
17 itiap i f arr =
18         _runST (
19         readArray arr i `thenStrictlyST` \val ->
20         writeArray arr i (f val) `seqStrictlyST`
21         returnStrictlyST arr)
22
23 itrap :: Constructed a => ((Int,Int),(Int,Int)) -> (a->a) -> IndTree s a -> IndTree s a
24 itrap ((i,k),(j,l)) f arr = _runST(itrap' i k)
25         where
26         itrap' i k = if k > l then returnStrictlyST arr
27                      else (itrapsnd i k `seqStrictlyST`
28                         itrap' i (k+1))
29         itrapsnd i k = if i > j then returnStrictlyST arr
30                      else (readArray arr (i,k) `thenStrictlyST` \val ->
31                         writeArray arr (i,k) (f val) `seqStrictlyST`
32                         itrapsnd (i+1) k)
33
34 itrapstate :: Constructed b => ((Int,Int),(Int,Int)) -> (a->b->(a,b)) -> ((Int,Int)->c->a) ->
35                 (a->c) -> c -> IndTree s b -> (c, IndTree s b)
36 itrapstate ((i,k),(j,l)) f c d s arr = _runST(itrapstate' i k s)
37         where
38         itrapstate' i k s = if k > l then returnStrictlyST (s,arr)
39                             else (itrapstatesnd i k s `thenStrictlyST` \(s,arr) ->
40                                 itrapstate' i (k+1) s)
41         itrapstatesnd i k s = if i > j then returnStrictlyST (s,arr)
42                             else (readArray arr (i,k) `thenStrictlyST` \val ->
43                                let (newstate, newval) = f (c (i,k) s) val
44                                in writeArray arr (i,k) newval `seqStrictlyST`
45                                itrapstatesnd (i+1) k (d newstate))
46
47 -- stuff from Auxiliary: copied here (partain)
48
49 sap :: (a->b) -> (c,a) -> (c,b)
50 sap f (x,y) = (x, f y)
51
52 fap :: (a->b) -> (a,c) -> (b,c)
53 fap f (x,y) = (f x, y)
54
55 nonempty :: [a] -> Bool
56 nonempty []    = False
57 nonempty (_:_) = True
58
59 -- const :: a -> b -> a
60 -- const k x = k
61
62 -- id :: a -> a
63 -- id x = x
64
65 compose :: [a->a] -> a -> a
66 compose = foldr (.) id
67
68 data Maybe t = Just t | Nothing
69
70 class Constructed a where
71    normal :: a -> Bool
72
73 instance Constructed Bool where
74    normal True = True
75    normal False = True
76
77 instance Constructed Int where
78    normal 0 = True
79    normal n = True
80
81 instance (Constructed a, Constructed b) => Constructed (a,b) where
82    normal (x,y) = normal x && normal y
83
84 -- pair :: (Constructed a, Constructed b) => a -> b -> (a,b)
85 -- pair x y | normal x && normal y = (x,y)
86
87 instance Constructed (Maybe a) where
88    normal Nothing = True
89    normal (Just _) = True
90
91 just :: Constructed a => a -> Maybe a
92 just x | normal x = Just x