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, Bag )
29 import ErrUtils ( WarnMsg )
30 import Id ( mkSysLocal, setIdUnique, Id )
31 import Module ( Module )
32 import Var ( TyVar, setTyVarUnique )
34 import SrcLoc ( noSrcLoc, SrcLoc )
35 import TcHsSyn ( TypecheckedPat )
36 import TcEnv ( ValueEnv )
38 import UniqSupply ( initUs_, splitUniqSupply, uniqFromSupply, uniqsFromSupply,
40 import Unique ( Unique )
41 import UniqFM ( lookupWithDefaultUFM_Directly )
42 import Util ( zipWithEqual )
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 -> 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)
70 -> Module -- module name: for profiling
74 initDs init_us genv mod action
75 = action init_us genv noSrcLoc mod emptyBag
77 thenDs :: DsM a -> (a -> DsM b) -> DsM b
78 andDs :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
80 thenDs m1 m2 us genv loc mod warns
81 = case splitUniqSupply us of { (s1, s2) ->
82 case (m1 s1 genv loc mod warns) of { (result, warns1) ->
83 m2 result s2 genv loc mod warns1}}
85 andDs combiner m1 m2 us genv loc mod warns
86 = case splitUniqSupply us of { (s1, s2) ->
87 case (m1 s1 genv loc mod warns) of { (result1, warns1) ->
88 case (m2 s2 genv loc mod warns1) of { (result2, warns2) ->
89 (combiner result1 result2, warns2) }}}
91 returnDs :: a -> DsM a
92 returnDs result us genv loc mod warns = (result, warns)
94 listDs :: [DsM a] -> DsM [a]
95 listDs [] = returnDs []
98 listDs xs `thenDs` \ rs ->
101 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
103 mapDs f [] = returnDs []
105 = f x `thenDs` \ r ->
106 mapDs f xs `thenDs` \ rs ->
109 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
111 foldlDs k z [] = returnDs z
112 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
115 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
117 mapAndUnzipDs f [] = returnDs ([], [])
118 mapAndUnzipDs f (x:xs)
119 = f x `thenDs` \ (r1, r2) ->
120 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
121 returnDs (r1:rs1, r2:rs2)
123 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
125 zipWithDs f [] ys = returnDs []
126 zipWithDs f (x:xs) (y:ys)
127 = f x y `thenDs` \ r ->
128 zipWithDs f xs ys `thenDs` \ rs ->
132 And all this mysterious stuff is so we can occasionally reach out and
133 grab one or more names. @newLocalDs@ isn't exported---exported
134 functions are defined with it. The difference in name-strings makes
135 it easier to read debugging output.
138 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
139 newSysLocalDs ty us genv loc mod warns
140 = case uniqFromSupply us of { assigned_uniq ->
141 (mkSysLocal SLIT("ds") assigned_uniq ty, warns) }
143 newSysLocalsDs tys = mapDs newSysLocalDs tys
145 newFailLocalDs ty us genv loc mod warns
146 = case uniqFromSupply us of { assigned_uniq ->
147 (mkSysLocal SLIT("fail") assigned_uniq ty, warns) }
148 -- The UserLocal bit just helps make the code a little clearer
150 getUniqueDs :: DsM Unique
151 getUniqueDs us genv loc mod warns
152 = case (uniqFromSupply us) of { assigned_uniq ->
153 (assigned_uniq, warns) }
155 duplicateLocalDs :: Id -> DsM Id
156 duplicateLocalDs old_local us genv loc mod warns
157 = case uniqFromSupply us of { assigned_uniq ->
158 (setIdUnique old_local assigned_uniq, warns) }
160 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
161 cloneTyVarsDs tyvars us genv loc mod warns
162 = case uniqsFromSupply (length tyvars) us of { uniqs ->
163 (zipWithEqual "cloneTyVarsDs" setTyVarUnique tyvars uniqs, warns) }
167 newTyVarsDs :: [TyVar] -> DsM [TyVar]
169 newTyVarsDs tyvar_tmpls us genv loc mod warns
170 = case uniqsFromSupply (length tyvar_tmpls) us of { uniqs ->
171 (zipWithEqual "newTyVarsDs" setTyVarUnique tyvar_tmpls uniqs, warns) }
174 We can also reach out and either set/grab location information from
175 the @SrcLoc@ being carried around.
177 uniqSMtoDsM :: UniqSM a -> DsM a
179 uniqSMtoDsM u_action us genv loc mod warns
180 = (initUs_ us u_action, warns)
182 getSrcLocDs :: DsM SrcLoc
183 getSrcLocDs us genv loc mod warns
186 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
187 putSrcLocDs new_loc expr us genv old_loc mod warns
188 = expr us genv new_loc mod warns
190 dsWarn :: WarnMsg -> DsM ()
191 dsWarn warn us genv loc mod warns = ((), warns `snocBag` warn)
196 getModuleDs :: DsM Module
197 getModuleDs us genv loc mod warns = (mod, warns)
201 dsLookupGlobalValue :: Unique -> DsM Id
202 dsLookupGlobalValue key us genv loc mod warns
203 = (lookupWithDefaultUFM_Directly genv def key, warns)
205 def = pprPanic "dsLookupGlobalValue:" (ppr key)
209 %************************************************************************
211 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
213 %************************************************************************
217 = DsMatchContext DsMatchKind [TypecheckedPat] SrcLoc