2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[UniqSupply]{The @UniqueSupply@ data type and a (monadic) supply thereof}
9 UniqSupply, -- Abstractly
11 getUnique, getUniques, -- basic ops
13 UniqSM, -- type: unique supply monad
14 initUs, thenUs, returnUs, fixUs,
15 mapUs, mapAndUnzipUs, mapAndUnzip3Us,
16 thenMaybeUs, mapAccumLUs,
22 #include "HsVersions.h"
29 #if __GLASGOW_HASKELL__ < 301
30 import IOBase ( IO(..), IOResult(..) )
40 %************************************************************************
42 \subsection{Splittable Unique supply: @UniqSupply@}
44 %************************************************************************
46 %************************************************************************
48 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
50 %************************************************************************
52 A value of type @UniqSupply@ is unique, and it can
53 supply {\em one} distinct @Unique@. Also, from the supply, one can
54 also manufacture an arbitrary number of further @UniqueSupplies@,
55 which will be distinct from the first and from all others.
59 = MkSplitUniqSupply Int -- make the Unique with this
61 -- when split => these two supplies
65 mkSplitUniqSupply :: Char -> IO UniqSupply
67 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
68 getUnique :: UniqSupply -> Unique
69 getUniques :: Int -> UniqSupply -> [Unique]
73 mkSplitUniqSupply (C# c#)
75 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
77 -- here comes THE MAGIC:
79 -- This is one of the most hammered bits in the whole compiler
81 = unsafeInterleaveIO (
82 mk_unique >>= \ uniq ->
83 mk_supply# >>= \ s1 ->
84 mk_supply# >>= \ s2 ->
85 return (MkSplitUniqSupply uniq s1 s2)
88 mk_unique = _ccall_ genSymZh >>= \ (W# u#) ->
89 return (I# (w2i (mask# `or#` u#)))
93 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
97 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
99 getUniques (I# i) supply = i `get_from` supply
102 get_from n (MkSplitUniqSupply (I# u) _ s2)
103 = mkUniqueGrimily u : get_from (n -# 1#) s2
106 %************************************************************************
108 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
110 %************************************************************************
113 type UniqSM result = UniqSupply -> result
115 -- the initUs function also returns the final UniqSupply
117 initUs :: UniqSupply -> UniqSM a -> a
119 initUs init_us m = m init_us
121 {-# INLINE thenUs #-}
122 {-# INLINE returnUs #-}
123 {-# INLINE splitUniqSupply #-}
126 @thenUs@ is where we split the @UniqSupply@.
128 fixUs :: (a -> UniqSM a) -> UniqSM a
132 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
135 = case (splitUniqSupply us) of { (s1, s2) ->
136 case (expr s1) of { result ->
141 returnUs :: a -> UniqSM a
142 returnUs result us = result
144 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
146 mapUs f [] = returnUs []
148 = f x `thenUs` \ r ->
149 mapUs f xs `thenUs` \ rs ->
152 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
153 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
155 mapAndUnzipUs f [] = returnUs ([],[])
156 mapAndUnzipUs f (x:xs)
157 = f x `thenUs` \ (r1, r2) ->
158 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
159 returnUs (r1:rs1, r2:rs2)
161 mapAndUnzip3Us f [] = returnUs ([],[],[])
162 mapAndUnzip3Us f (x:xs)
163 = f x `thenUs` \ (r1, r2, r3) ->
164 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
165 returnUs (r1:rs1, r2:rs2, r3:rs3)
167 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
169 = m `thenUs` \ result ->
171 Nothing -> returnUs Nothing
174 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
179 mapAccumLUs f b [] = returnUs (b, [])
180 mapAccumLUs f b (x:xs)
181 = f b x `thenUs` \ (b__2, x__2) ->
182 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
183 returnUs (b__3, x__2:xs__2)