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 import IOBase ( IO(..), IOResult(..) )
30 import PrelBase ( Char(..) )
38 %************************************************************************
40 \subsection{Splittable Unique supply: @UniqSupply@}
42 %************************************************************************
44 %************************************************************************
46 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
48 %************************************************************************
50 A value of type @UniqSupply@ is unique, and it can
51 supply {\em one} distinct @Unique@. Also, from the supply, one can
52 also manufacture an arbitrary number of further @UniqueSupplies@,
53 which will be distinct from the first and from all others.
57 = MkSplitUniqSupply Int -- make the Unique with this
59 -- when split => these two supplies
63 mkSplitUniqSupply :: Char -> IO UniqSupply
65 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
66 getUnique :: UniqSupply -> Unique
67 getUniques :: Int -> UniqSupply -> [Unique]
71 mkSplitUniqSupply (C# c#)
73 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
75 -- here comes THE MAGIC:
77 -- This is one of the most hammered bits in the whole compiler
79 = unsafeInterleaveIO (
80 mk_unique >>= \ uniq ->
81 mk_supply# >>= \ s1 ->
82 mk_supply# >>= \ s2 ->
83 return (MkSplitUniqSupply uniq s1 s2)
86 mk_unique = _ccall_ genSymZh >>= \ (W# u#) ->
87 return (I# (w2i (mask# `or#` u#)))
91 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
95 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
97 getUniques (I# i) supply = i `get_from` supply
100 get_from n (MkSplitUniqSupply (I# u) _ s2)
101 = mkUniqueGrimily u : get_from (n -# 1#) s2
104 %************************************************************************
106 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
108 %************************************************************************
111 type UniqSM result = UniqSupply -> result
113 -- the initUs function also returns the final UniqSupply
115 initUs :: UniqSupply -> UniqSM a -> a
117 initUs init_us m = m init_us
119 {-# INLINE thenUs #-}
120 {-# INLINE returnUs #-}
121 {-# INLINE splitUniqSupply #-}
124 @thenUs@ is where we split the @UniqSupply@.
126 fixUs :: (a -> UniqSM a) -> UniqSM a
130 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
133 = case (splitUniqSupply us) of { (s1, s2) ->
134 case (expr s1) of { result ->
139 returnUs :: a -> UniqSM a
140 returnUs result us = result
142 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
144 mapUs f [] = returnUs []
146 = f x `thenUs` \ r ->
147 mapUs f xs `thenUs` \ rs ->
150 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
151 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
153 mapAndUnzipUs f [] = returnUs ([],[])
154 mapAndUnzipUs f (x:xs)
155 = f x `thenUs` \ (r1, r2) ->
156 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
157 returnUs (r1:rs1, r2:rs2)
159 mapAndUnzip3Us f [] = returnUs ([],[],[])
160 mapAndUnzip3Us f (x:xs)
161 = f x `thenUs` \ (r1, r2, r3) ->
162 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
163 returnUs (r1:rs1, r2:rs2, r3:rs3)
165 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
167 = m `thenUs` \ result ->
169 Nothing -> returnUs Nothing
172 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
177 mapAccumLUs f b [] = returnUs (b, [])
178 mapAccumLUs f b (x:xs)
179 = f b x `thenUs` \ (b__2, x__2) ->
180 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
181 returnUs (b__3, x__2:xs__2)