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 TcHsSyn ( TypecheckedPat, TypecheckedMatchContext )
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 )
37 import UniqSupply ( initUs_, splitUniqSupply, uniqFromSupply, uniqsFromSupply,
39 import Unique ( Unique )
41 import CmdLineOpts ( DynFlags )
46 Now the mondo monad magic (yes, @DsM@ is a silly name)---carry around
47 a @UniqueSupply@ and some annotations, which
48 presumably include source-file location information:
53 -> (Name -> Id) -- Lookup well-known Ids
54 -> SrcLoc -- to put in pattern-matching error msgs
55 -> Module -- module: for SCC profiling
57 -> (result, DsWarnings)
59 type DsWarnings = Bag WarnMsg -- The desugarer reports matches which are
60 -- completely shadowed or incomplete patterns
64 {-# INLINE returnDs #-}
66 -- initDs returns the UniqSupply out the end (not just the result)
71 -> Module -- module name: for profiling
75 initDs dflags init_us lookup mod action
76 = action dflags init_us lookup noSrcLoc mod emptyBag
78 thenDs :: DsM a -> (a -> DsM b) -> DsM b
79 andDs :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
81 thenDs m1 m2 dflags us genv loc mod warns
82 = case splitUniqSupply us of { (s1, s2) ->
83 case (m1 dflags s1 genv loc mod warns) of { (result, warns1) ->
84 m2 result dflags s2 genv loc mod warns1}}
86 andDs combiner m1 m2 dflags us genv loc mod warns
87 = case splitUniqSupply us of { (s1, s2) ->
88 case (m1 dflags s1 genv loc mod warns) of { (result1, warns1) ->
89 case (m2 dflags s2 genv loc mod warns1) of { (result2, warns2) ->
90 (combiner result1 result2, warns2) }}}
92 returnDs :: a -> DsM a
93 returnDs result dflags us genv loc mod warns = (result, warns)
95 listDs :: [DsM a] -> DsM [a]
96 listDs [] = returnDs []
99 listDs xs `thenDs` \ rs ->
102 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
104 mapDs f [] = returnDs []
106 = f x `thenDs` \ r ->
107 mapDs f xs `thenDs` \ rs ->
110 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
112 foldlDs k z [] = returnDs z
113 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
116 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
118 mapAndUnzipDs f [] = returnDs ([], [])
119 mapAndUnzipDs f (x:xs)
120 = f x `thenDs` \ (r1, r2) ->
121 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
122 returnDs (r1:rs1, r2:rs2)
124 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
126 zipWithDs f [] ys = returnDs []
127 zipWithDs f (x:xs) (y:ys)
128 = f x y `thenDs` \ r ->
129 zipWithDs f xs ys `thenDs` \ rs ->
133 And all this mysterious stuff is so we can occasionally reach out and
134 grab one or more names. @newLocalDs@ isn't exported---exported
135 functions are defined with it. The difference in name-strings makes
136 it easier to read debugging output.
139 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
140 newSysLocalDs ty dflags us genv loc mod warns
141 = case uniqFromSupply us of { assigned_uniq ->
142 (mkSysLocal SLIT("ds") assigned_uniq ty, warns) }
144 newSysLocalsDs tys = mapDs newSysLocalDs tys
146 newFailLocalDs ty dflags us genv loc mod warns
147 = case uniqFromSupply us of { assigned_uniq ->
148 (mkSysLocal SLIT("fail") assigned_uniq ty, warns) }
149 -- The UserLocal bit just helps make the code a little clearer
151 getUniqueDs :: DsM Unique
152 getUniqueDs dflags us genv loc mod warns
153 = (uniqFromSupply us, warns)
155 getUniquesDs :: DsM [Unique]
156 getUniquesDs dflags us genv loc mod warns
157 = (uniqsFromSupply us, warns)
159 getDOptsDs :: DsM DynFlags
160 getDOptsDs dflags us genv loc mod warns
163 duplicateLocalDs :: Id -> DsM Id
164 duplicateLocalDs old_local dflags us genv loc mod warns
165 = case uniqFromSupply us of { assigned_uniq ->
166 (setIdUnique old_local assigned_uniq, warns) }
168 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
169 cloneTyVarsDs tyvars dflags us genv loc mod warns
170 = (zipWith setTyVarUnique tyvars (uniqsFromSupply us), warns)
174 newTyVarsDs :: [TyVar] -> DsM [TyVar]
175 newTyVarsDs tyvar_tmpls dflags us genv loc mod warns
176 = (zipWith setTyVarUnique tyvar_tmpls (uniqsFromSupply us), 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 dflags us genv loc mod warns
185 = (initUs_ us u_action, warns)
187 getSrcLocDs :: DsM SrcLoc
188 getSrcLocDs dflags us genv loc mod warns
191 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
192 putSrcLocDs new_loc expr dflags us genv old_loc mod warns
193 = expr dflags us genv new_loc mod warns
195 dsWarn :: WarnMsg -> DsM ()
196 dsWarn warn dflags us genv loc mod warns = ((), warns `snocBag` warn)
201 getModuleDs :: DsM Module
202 getModuleDs dflags us genv loc mod warns = (mod, warns)
206 dsLookupGlobalValue :: Name -> DsM Id
207 dsLookupGlobalValue name dflags us genv loc mod warns
212 %************************************************************************
214 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
216 %************************************************************************
220 = DsMatchContext TypecheckedMatchContext [TypecheckedPat] SrcLoc