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,
17 getUniqueDs, getUniquesDs,
26 #include "HsVersions.h"
28 import HsSyn ( HsMatchContext )
29 import Bag ( emptyBag, snocBag, Bag )
30 import ErrUtils ( WarnMsg )
31 import Id ( mkSysLocal, setIdUnique, Id )
32 import Module ( Module )
33 import Var ( TyVar, setTyVarUnique )
35 import SrcLoc ( noSrcLoc, SrcLoc )
36 import TcHsSyn ( TypecheckedPat )
38 import UniqSupply ( initUs_, splitUniqSupply, uniqFromSupply, uniqsFromSupply,
40 import Unique ( Unique )
42 import CmdLineOpts ( DynFlags )
47 Now the mondo monad magic (yes, @DsM@ is a silly name)---carry around
48 a @UniqueSupply@ and some annotations, which
49 presumably include source-file location information:
54 -> (Name -> Id) -- Lookup well-known Ids
55 -> SrcLoc -- to put in pattern-matching error msgs
56 -> Module -- module: for SCC profiling
58 -> (result, DsWarnings)
60 type DsWarnings = Bag WarnMsg -- The desugarer reports matches which are
61 -- completely shadowed or incomplete patterns
65 {-# INLINE returnDs #-}
67 -- initDs returns the UniqSupply out the end (not just the result)
72 -> Module -- module name: for profiling
76 initDs dflags init_us lookup mod action
77 = action dflags init_us lookup noSrcLoc mod emptyBag
79 thenDs :: DsM a -> (a -> DsM b) -> DsM b
80 andDs :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
82 thenDs m1 m2 dflags us genv loc mod warns
83 = case splitUniqSupply us of { (s1, s2) ->
84 case (m1 dflags s1 genv loc mod warns) of { (result, warns1) ->
85 m2 result dflags s2 genv loc mod warns1}}
87 andDs combiner m1 m2 dflags us genv loc mod warns
88 = case splitUniqSupply us of { (s1, s2) ->
89 case (m1 dflags s1 genv loc mod warns) of { (result1, warns1) ->
90 case (m2 dflags s2 genv loc mod warns1) of { (result2, warns2) ->
91 (combiner result1 result2, warns2) }}}
93 returnDs :: a -> DsM a
94 returnDs result dflags us genv loc mod warns = (result, warns)
96 listDs :: [DsM a] -> DsM [a]
97 listDs [] = returnDs []
100 listDs xs `thenDs` \ rs ->
103 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
105 mapDs f [] = returnDs []
107 = f x `thenDs` \ r ->
108 mapDs f xs `thenDs` \ rs ->
111 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
113 foldlDs k z [] = returnDs z
114 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
117 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
119 mapAndUnzipDs f [] = returnDs ([], [])
120 mapAndUnzipDs f (x:xs)
121 = f x `thenDs` \ (r1, r2) ->
122 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
123 returnDs (r1:rs1, r2:rs2)
125 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
127 zipWithDs f [] ys = returnDs []
128 zipWithDs f (x:xs) (y:ys)
129 = f x y `thenDs` \ r ->
130 zipWithDs f xs ys `thenDs` \ rs ->
134 And all this mysterious stuff is so we can occasionally reach out and
135 grab one or more names. @newLocalDs@ isn't exported---exported
136 functions are defined with it. The difference in name-strings makes
137 it easier to read debugging output.
140 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
141 newSysLocalDs ty dflags us genv loc mod warns
142 = case uniqFromSupply us of { assigned_uniq ->
143 (mkSysLocal SLIT("ds") assigned_uniq ty, warns) }
145 newSysLocalsDs tys = mapDs newSysLocalDs tys
147 newFailLocalDs ty dflags us genv loc mod warns
148 = case uniqFromSupply us of { assigned_uniq ->
149 (mkSysLocal SLIT("fail") assigned_uniq ty, warns) }
150 -- The UserLocal bit just helps make the code a little clearer
152 getUniqueDs :: DsM Unique
153 getUniqueDs dflags us genv loc mod warns
154 = (uniqFromSupply us, warns)
156 getUniquesDs :: DsM [Unique]
157 getUniquesDs dflags us genv loc mod warns
158 = (uniqsFromSupply us, warns)
160 getDOptsDs :: DsM DynFlags
161 getDOptsDs dflags us genv loc mod warns
164 duplicateLocalDs :: Id -> DsM Id
165 duplicateLocalDs old_local dflags us genv loc mod warns
166 = case uniqFromSupply us of { assigned_uniq ->
167 (setIdUnique old_local assigned_uniq, warns) }
169 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
170 cloneTyVarsDs tyvars dflags us genv loc mod warns
171 = (zipWith setTyVarUnique tyvars (uniqsFromSupply us), warns)
175 newTyVarsDs :: [TyVar] -> DsM [TyVar]
176 newTyVarsDs tyvar_tmpls dflags us genv loc mod warns
177 = (zipWith setTyVarUnique tyvar_tmpls (uniqsFromSupply us), warns)
180 We can also reach out and either set/grab location information from
181 the @SrcLoc@ being carried around.
183 uniqSMtoDsM :: UniqSM a -> DsM a
185 uniqSMtoDsM u_action dflags us genv loc mod warns
186 = (initUs_ us u_action, warns)
188 getSrcLocDs :: DsM SrcLoc
189 getSrcLocDs dflags us genv loc mod warns
192 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
193 putSrcLocDs new_loc expr dflags us genv old_loc mod warns
194 = expr dflags us genv new_loc mod warns
196 dsWarn :: WarnMsg -> DsM ()
197 dsWarn warn dflags us genv loc mod warns = ((), warns `snocBag` warn)
202 getModuleDs :: DsM Module
203 getModuleDs dflags us genv loc mod warns = (mod, warns)
207 dsLookupGlobalValue :: Name -> DsM Id
208 dsLookupGlobalValue name dflags us genv loc mod warns
213 %************************************************************************
215 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
217 %************************************************************************
221 = DsMatchContext HsMatchContext [TypecheckedPat] SrcLoc