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,
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 )
41 import Util ( zipWithEqual )
43 import CmdLineOpts ( DynFlags )
48 Now the mondo monad magic (yes, @DsM@ is a silly name)---carry around
49 a @UniqueSupply@ and some annotations, which
50 presumably include source-file location information:
55 -> (Name -> Id) -- Lookup well-known Ids
56 -> SrcLoc -- to put in pattern-matching error msgs
57 -> Module -- module: for SCC profiling
59 -> (result, DsWarnings)
61 type DsWarnings = Bag WarnMsg -- The desugarer reports matches which are
62 -- completely shadowed or incomplete patterns
66 {-# INLINE returnDs #-}
68 -- initDs returns the UniqSupply out the end (not just the result)
73 -> Module -- module name: for profiling
77 initDs dflags init_us lookup mod action
78 = action dflags init_us lookup noSrcLoc mod emptyBag
80 thenDs :: DsM a -> (a -> DsM b) -> DsM b
81 andDs :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
83 thenDs m1 m2 dflags us genv loc mod warns
84 = case splitUniqSupply us of { (s1, s2) ->
85 case (m1 dflags s1 genv loc mod warns) of { (result, warns1) ->
86 m2 result dflags s2 genv loc mod warns1}}
88 andDs combiner m1 m2 dflags us genv loc mod warns
89 = case splitUniqSupply us of { (s1, s2) ->
90 case (m1 dflags s1 genv loc mod warns) of { (result1, warns1) ->
91 case (m2 dflags s2 genv loc mod warns1) of { (result2, warns2) ->
92 (combiner result1 result2, warns2) }}}
94 returnDs :: a -> DsM a
95 returnDs result dflags us genv loc mod warns = (result, warns)
97 listDs :: [DsM a] -> DsM [a]
98 listDs [] = returnDs []
101 listDs xs `thenDs` \ rs ->
104 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
106 mapDs f [] = returnDs []
108 = f x `thenDs` \ r ->
109 mapDs f xs `thenDs` \ rs ->
112 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
114 foldlDs k z [] = returnDs z
115 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
118 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
120 mapAndUnzipDs f [] = returnDs ([], [])
121 mapAndUnzipDs f (x:xs)
122 = f x `thenDs` \ (r1, r2) ->
123 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
124 returnDs (r1:rs1, r2:rs2)
126 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
128 zipWithDs f [] ys = returnDs []
129 zipWithDs f (x:xs) (y:ys)
130 = f x y `thenDs` \ r ->
131 zipWithDs f xs ys `thenDs` \ rs ->
135 And all this mysterious stuff is so we can occasionally reach out and
136 grab one or more names. @newLocalDs@ isn't exported---exported
137 functions are defined with it. The difference in name-strings makes
138 it easier to read debugging output.
141 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
142 newSysLocalDs ty dflags us genv loc mod warns
143 = case uniqFromSupply us of { assigned_uniq ->
144 (mkSysLocal SLIT("ds") assigned_uniq ty, warns) }
146 newSysLocalsDs tys = mapDs newSysLocalDs tys
148 newFailLocalDs ty dflags us genv loc mod warns
149 = case uniqFromSupply us of { assigned_uniq ->
150 (mkSysLocal SLIT("fail") assigned_uniq ty, warns) }
151 -- The UserLocal bit just helps make the code a little clearer
153 getUniqueDs :: DsM Unique
154 getUniqueDs dflags us genv loc mod warns
155 = case (uniqFromSupply us) of { assigned_uniq ->
156 (assigned_uniq, warns) }
158 getDOptsDs :: DsM DynFlags
159 getDOptsDs dflags us genv loc mod warns
162 duplicateLocalDs :: Id -> DsM Id
163 duplicateLocalDs old_local dflags us genv loc mod warns
164 = case uniqFromSupply us of { assigned_uniq ->
165 (setIdUnique old_local assigned_uniq, warns) }
167 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
168 cloneTyVarsDs tyvars dflags us genv loc mod warns
169 = case uniqsFromSupply (length tyvars) us of { uniqs ->
170 (zipWithEqual "cloneTyVarsDs" setTyVarUnique tyvars uniqs, warns) }
174 newTyVarsDs :: [TyVar] -> DsM [TyVar]
176 newTyVarsDs tyvar_tmpls dflags us genv loc mod warns
177 = case uniqsFromSupply (length tyvar_tmpls) us of { uniqs ->
178 (zipWithEqual "newTyVarsDs" setTyVarUnique tyvar_tmpls uniqs, warns) }
181 We can also reach out and either set/grab location information from
182 the @SrcLoc@ being carried around.
184 uniqSMtoDsM :: UniqSM a -> DsM a
186 uniqSMtoDsM u_action dflags us genv loc mod warns
187 = (initUs_ us u_action, warns)
189 getSrcLocDs :: DsM SrcLoc
190 getSrcLocDs dflags us genv loc mod warns
193 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
194 putSrcLocDs new_loc expr dflags us genv old_loc mod warns
195 = expr dflags us genv new_loc mod warns
197 dsWarn :: WarnMsg -> DsM ()
198 dsWarn warn dflags us genv loc mod warns = ((), warns `snocBag` warn)
203 getModuleDs :: DsM Module
204 getModuleDs dflags us genv loc mod warns = (mod, warns)
208 dsLookupGlobalValue :: Name -> DsM Id
209 dsLookupGlobalValue name dflags us genv loc mod warns
214 %************************************************************************
216 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
218 %************************************************************************
222 = DsMatchContext HsMatchContext [TypecheckedPat] SrcLoc