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 )
37 import UniqSupply ( initUs_, splitUniqSupply, uniqFromSupply, uniqsFromSupply,
39 import Unique ( Unique )
40 import UniqFM ( lookupWithDefaultUFM_Directly )
41 import Util ( zipWithEqual )
42 import Name ( Name, lookupNameEnv )
43 import HscTypes ( HomeSymbolTable, PersistentCompilerState(..),
44 TyThing(..), TypeEnv, lookupTypeEnv )
45 import CmdLineOpts ( DynFlags )
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 -> (Name -> Id) -- Lookup well-known Ids
58 -> SrcLoc -- to put in pattern-matching error msgs
59 -> Module -- module: for SCC profiling
61 -> (result, DsWarnings)
63 type DsWarnings = Bag WarnMsg -- The desugarer reports matches which are
64 -- completely shadowed or incomplete patterns
68 {-# INLINE returnDs #-}
70 -- initDs returns the UniqSupply out the end (not just the result)
74 -> (HomeSymbolTable, PersistentCompilerState, TypeEnv)
75 -> Module -- module name: for profiling
79 initDs dflags init_us (hst,pcs,local_type_env) mod action
80 = action dflags init_us lookup noSrcLoc mod emptyBag
82 -- This lookup is used for well-known Ids,
83 -- such as fold, build, cons etc, so the chances are
84 -- it'll be found in the package symbol table. That's
85 -- why we don't merge all these tables
87 lookup n = case lookupTypeEnv pst n of {
90 case lookupTypeEnv hst n of {
93 case lookupNameEnv local_type_env n of
95 other -> pprPanic "initDS: lookup:" (ppr n)
98 thenDs :: DsM a -> (a -> DsM b) -> DsM b
99 andDs :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
101 thenDs m1 m2 dflags us genv loc mod warns
102 = case splitUniqSupply us of { (s1, s2) ->
103 case (m1 dflags s1 genv loc mod warns) of { (result, warns1) ->
104 m2 result dflags s2 genv loc mod warns1}}
106 andDs combiner m1 m2 dflags us genv loc mod warns
107 = case splitUniqSupply us of { (s1, s2) ->
108 case (m1 dflags s1 genv loc mod warns) of { (result1, warns1) ->
109 case (m2 dflags s2 genv loc mod warns1) of { (result2, warns2) ->
110 (combiner result1 result2, warns2) }}}
112 returnDs :: a -> DsM a
113 returnDs result dflags us genv loc mod warns = (result, warns)
115 listDs :: [DsM a] -> DsM [a]
116 listDs [] = returnDs []
119 listDs xs `thenDs` \ rs ->
122 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
124 mapDs f [] = returnDs []
126 = f x `thenDs` \ r ->
127 mapDs f xs `thenDs` \ rs ->
130 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
132 foldlDs k z [] = returnDs z
133 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
136 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
138 mapAndUnzipDs f [] = returnDs ([], [])
139 mapAndUnzipDs f (x:xs)
140 = f x `thenDs` \ (r1, r2) ->
141 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
142 returnDs (r1:rs1, r2:rs2)
144 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
146 zipWithDs f [] ys = returnDs []
147 zipWithDs f (x:xs) (y:ys)
148 = f x y `thenDs` \ r ->
149 zipWithDs f xs ys `thenDs` \ rs ->
153 And all this mysterious stuff is so we can occasionally reach out and
154 grab one or more names. @newLocalDs@ isn't exported---exported
155 functions are defined with it. The difference in name-strings makes
156 it easier to read debugging output.
159 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
160 newSysLocalDs ty dflags us genv loc mod warns
161 = case uniqFromSupply us of { assigned_uniq ->
162 (mkSysLocal SLIT("ds") assigned_uniq ty, warns) }
164 newSysLocalsDs tys = mapDs newSysLocalDs tys
166 newFailLocalDs ty dflags us genv loc mod warns
167 = case uniqFromSupply us of { assigned_uniq ->
168 (mkSysLocal SLIT("fail") assigned_uniq ty, warns) }
169 -- The UserLocal bit just helps make the code a little clearer
171 getUniqueDs :: DsM Unique
172 getUniqueDs dflags us genv loc mod warns
173 = case (uniqFromSupply us) of { assigned_uniq ->
174 (assigned_uniq, warns) }
176 getDOptsDs :: DsM DynFlags
177 getDOptsDs dflags us genv loc mod warns
180 duplicateLocalDs :: Id -> DsM Id
181 duplicateLocalDs old_local dflags us genv loc mod warns
182 = case uniqFromSupply us of { assigned_uniq ->
183 (setIdUnique old_local assigned_uniq, warns) }
185 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
186 cloneTyVarsDs tyvars dflags us genv loc mod warns
187 = case uniqsFromSupply (length tyvars) us of { uniqs ->
188 (zipWithEqual "cloneTyVarsDs" setTyVarUnique tyvars uniqs, warns) }
192 newTyVarsDs :: [TyVar] -> DsM [TyVar]
194 newTyVarsDs tyvar_tmpls dflags us genv loc mod warns
195 = case uniqsFromSupply (length tyvar_tmpls) us of { uniqs ->
196 (zipWithEqual "newTyVarsDs" setTyVarUnique tyvar_tmpls uniqs, warns) }
199 We can also reach out and either set/grab location information from
200 the @SrcLoc@ being carried around.
202 uniqSMtoDsM :: UniqSM a -> DsM a
204 uniqSMtoDsM u_action dflags us genv loc mod warns
205 = (initUs_ us u_action, warns)
207 getSrcLocDs :: DsM SrcLoc
208 getSrcLocDs dflags us genv loc mod warns
211 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
212 putSrcLocDs new_loc expr dflags us genv old_loc mod warns
213 = expr dflags us genv new_loc mod warns
215 dsWarn :: WarnMsg -> DsM ()
216 dsWarn warn dflags us genv loc mod warns = ((), warns `snocBag` warn)
221 getModuleDs :: DsM Module
222 getModuleDs dflags us genv loc mod warns = (mod, warns)
226 dsLookupGlobalValue :: Name -> DsM Id
227 dsLookupGlobalValue name dflags us genv loc mod warns
232 %************************************************************************
234 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
236 %************************************************************************
240 = DsMatchContext DsMatchKind [TypecheckedPat] SrcLoc