2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[DsMonad]{@DsMonad@: monadery used in desugaring}
9 initDs, returnDs, thenDs, andDs, mapDs, listDs,
10 mapAndUnzipDs, zipWithDs, foldlDs,
12 newTyVarsDs, cloneTyVarsDs,
13 duplicateLocalDs, newSysLocalDs, newSysLocalsDs,
15 getSrcLocDs, putSrcLocDs,
23 DsMatchContext(..), DsMatchKind(..)
26 #include "HsVersions.h"
28 import Bag ( emptyBag, snocBag, bagToList, Bag )
29 import ErrUtils ( WarnMsg, pprBagOfErrors )
30 import HsSyn ( OutPat )
31 import Id ( mkSysLocal, setIdUnique, Id )
32 import Module ( Module )
33 import Name ( Name, maybeWiredInIdName )
34 import Var ( TyVar, setTyVarUnique )
37 import SrcLoc ( noSrcLoc, SrcLoc )
38 import TcHsSyn ( TypecheckedPat )
39 import TcEnv ( ValueEnv )
41 import UniqSupply ( initUs_, splitUniqSupply, uniqFromSupply, uniqsFromSupply,
43 import Unique ( Unique )
44 import UniqFM ( lookupWithDefaultUFM )
45 import Util ( zipWithEqual )
50 Now the mondo monad magic (yes, @DsM@ is a silly name)---carry around
51 a @UniqueSupply@ and some annotations, which
52 presumably include source-file location information:
57 -> SrcLoc -- to put in pattern-matching error msgs
58 -> (Module, Group) -- module + group name : for SCC profiling
60 -> (result, DsWarnings)
62 type DsWarnings = Bag WarnMsg -- The desugarer reports matches which are
63 -- completely shadowed or incomplete patterns
65 type Group = FAST_STRING
69 {-# INLINE returnDs #-}
71 -- initDs returns the UniqSupply out the end (not just the result)
75 -> (Module, Group) -- module name: for profiling; (group name: from switches)
79 initDs init_us genv module_and_group action
80 = action init_us genv noSrcLoc module_and_group emptyBag
82 thenDs :: DsM a -> (a -> DsM b) -> DsM b
83 andDs :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
85 thenDs m1 m2 us genv loc mod_and_grp warns
86 = case splitUniqSupply us of { (s1, s2) ->
87 case (m1 s1 genv loc mod_and_grp warns) of { (result, warns1) ->
88 m2 result s2 genv loc mod_and_grp warns1}}
90 andDs combiner m1 m2 us genv loc mod_and_grp warns
91 = case splitUniqSupply us of { (s1, s2) ->
92 case (m1 s1 genv loc mod_and_grp warns) of { (result1, warns1) ->
93 case (m2 s2 genv loc mod_and_grp warns1) of { (result2, warns2) ->
94 (combiner result1 result2, warns2) }}}
96 returnDs :: a -> DsM a
97 returnDs result us genv loc mod_and_grp warns = (result, warns)
99 listDs :: [DsM a] -> DsM [a]
100 listDs [] = returnDs []
103 listDs xs `thenDs` \ rs ->
106 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
108 mapDs f [] = returnDs []
110 = f x `thenDs` \ r ->
111 mapDs f xs `thenDs` \ rs ->
114 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
116 foldlDs k z [] = returnDs z
117 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
120 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
122 mapAndUnzipDs f [] = returnDs ([], [])
123 mapAndUnzipDs f (x:xs)
124 = f x `thenDs` \ (r1, r2) ->
125 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
126 returnDs (r1:rs1, r2:rs2)
128 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
130 zipWithDs f [] ys = returnDs []
131 zipWithDs f (x:xs) (y:ys)
132 = f x y `thenDs` \ r ->
133 zipWithDs f xs ys `thenDs` \ rs ->
137 And all this mysterious stuff is so we can occasionally reach out and
138 grab one or more names. @newLocalDs@ isn't exported---exported
139 functions are defined with it. The difference in name-strings makes
140 it easier to read debugging output.
143 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
144 newSysLocalDs ty us genv loc mod_and_grp warns
145 = case uniqFromSupply us of { assigned_uniq ->
146 (mkSysLocal SLIT("ds") assigned_uniq ty, warns) }
148 newSysLocalsDs tys = mapDs newSysLocalDs tys
150 newFailLocalDs ty us genv loc mod_and_grp warns
151 = case uniqFromSupply us of { assigned_uniq ->
152 (mkSysLocal SLIT("fail") assigned_uniq ty, warns) }
153 -- The UserLocal bit just helps make the code a little clearer
155 getUniqueDs :: DsM Unique
156 getUniqueDs us genv loc mod_and_grp warns
157 = case (uniqFromSupply us) of { assigned_uniq ->
158 (assigned_uniq, warns) }
160 duplicateLocalDs :: Id -> DsM Id
161 duplicateLocalDs old_local us genv loc mod_and_grp warns
162 = case uniqFromSupply us of { assigned_uniq ->
163 (setIdUnique old_local assigned_uniq, warns) }
165 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
166 cloneTyVarsDs tyvars us genv loc mod_and_grp warns
167 = case uniqsFromSupply (length tyvars) us of { uniqs ->
168 (zipWithEqual "cloneTyVarsDs" setTyVarUnique tyvars uniqs, warns) }
172 newTyVarsDs :: [TyVar] -> DsM [TyVar]
174 newTyVarsDs tyvar_tmpls us genv loc mod_and_grp warns
175 = case uniqsFromSupply (length tyvar_tmpls) us of { uniqs ->
176 (zipWithEqual "newTyVarsDs" setTyVarUnique tyvar_tmpls uniqs, warns) }
179 We can also reach out and either set/grab location information from
180 the @SrcLoc@ being carried around.
182 uniqSMtoDsM :: UniqSM a -> DsM a
184 uniqSMtoDsM u_action us genv loc mod_and_grp warns
185 = (initUs_ us u_action, warns)
187 getSrcLocDs :: DsM SrcLoc
188 getSrcLocDs us genv loc mod_and_grp warns
191 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
192 putSrcLocDs new_loc expr us genv old_loc mod_and_grp warns
193 = expr us genv new_loc mod_and_grp warns
195 dsWarn :: WarnMsg -> DsM ()
196 dsWarn warn us genv loc mod_and_grp warns = ((), warns `snocBag` warn)
201 getModuleAndGroupDs :: DsM (Module, Group)
202 getModuleAndGroupDs us genv loc mod_and_grp warns
203 = (mod_and_grp, warns)
207 dsLookupGlobalValue :: Name -> DsM Id
208 dsLookupGlobalValue name us genv loc mod_and_grp warns
209 = case maybeWiredInIdName name of
210 Just id -> (id, warns)
211 Nothing -> (lookupWithDefaultUFM genv def name, warns)
213 def = pprPanic "tcLookupGlobalValue:" (ppr name)
217 %************************************************************************
219 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
221 %************************************************************************
225 = DsMatchContext DsMatchKind [TypecheckedPat] SrcLoc