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 Util ( zipWithEqual )
41 import Name ( Name, lookupNameEnv )
42 import HscTypes ( HomeSymbolTable, PersistentCompilerState(..),
43 TyThing(..), TypeEnv, lookupType )
44 import CmdLineOpts ( DynFlags )
49 Now the mondo monad magic (yes, @DsM@ is a silly name)---carry around
50 a @UniqueSupply@ and some annotations, which
51 presumably include source-file location information:
56 -> (Name -> Id) -- Lookup well-known Ids
57 -> SrcLoc -- to put in pattern-matching error msgs
58 -> Module -- module: for SCC profiling
60 -> (result, DsWarnings)
62 type DsWarnings = Bag WarnMsg -- The desugarer reports matches which are
63 -- completely shadowed or incomplete patterns
67 {-# INLINE returnDs #-}
69 -- initDs returns the UniqSupply out the end (not just the result)
73 -> (HomeSymbolTable, PersistentCompilerState, TypeEnv)
74 -> Module -- module name: for profiling
78 initDs dflags init_us (hst,pcs,local_type_env) mod action
79 = action dflags init_us lookup noSrcLoc mod emptyBag
81 -- This lookup is used for well-known Ids,
82 -- such as fold, build, cons etc, so the chances are
83 -- it'll be found in the package symbol table. That's
84 -- why we don't merge all these tables
86 lookup n = case lookupType hst pte n of {
89 case lookupNameEnv local_type_env n of
91 other -> pprPanic "initDS: lookup:" (ppr n)
94 thenDs :: DsM a -> (a -> DsM b) -> DsM b
95 andDs :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
97 thenDs m1 m2 dflags us genv loc mod warns
98 = case splitUniqSupply us of { (s1, s2) ->
99 case (m1 dflags s1 genv loc mod warns) of { (result, warns1) ->
100 m2 result dflags s2 genv loc mod warns1}}
102 andDs combiner m1 m2 dflags us genv loc mod warns
103 = case splitUniqSupply us of { (s1, s2) ->
104 case (m1 dflags s1 genv loc mod warns) of { (result1, warns1) ->
105 case (m2 dflags s2 genv loc mod warns1) of { (result2, warns2) ->
106 (combiner result1 result2, warns2) }}}
108 returnDs :: a -> DsM a
109 returnDs result dflags us genv loc mod warns = (result, warns)
111 listDs :: [DsM a] -> DsM [a]
112 listDs [] = returnDs []
115 listDs xs `thenDs` \ rs ->
118 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
120 mapDs f [] = returnDs []
122 = f x `thenDs` \ r ->
123 mapDs f xs `thenDs` \ rs ->
126 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
128 foldlDs k z [] = returnDs z
129 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
132 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
134 mapAndUnzipDs f [] = returnDs ([], [])
135 mapAndUnzipDs f (x:xs)
136 = f x `thenDs` \ (r1, r2) ->
137 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
138 returnDs (r1:rs1, r2:rs2)
140 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
142 zipWithDs f [] ys = returnDs []
143 zipWithDs f (x:xs) (y:ys)
144 = f x y `thenDs` \ r ->
145 zipWithDs f xs ys `thenDs` \ rs ->
149 And all this mysterious stuff is so we can occasionally reach out and
150 grab one or more names. @newLocalDs@ isn't exported---exported
151 functions are defined with it. The difference in name-strings makes
152 it easier to read debugging output.
155 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
156 newSysLocalDs ty dflags us genv loc mod warns
157 = case uniqFromSupply us of { assigned_uniq ->
158 (mkSysLocal SLIT("ds") assigned_uniq ty, warns) }
160 newSysLocalsDs tys = mapDs newSysLocalDs tys
162 newFailLocalDs ty dflags us genv loc mod warns
163 = case uniqFromSupply us of { assigned_uniq ->
164 (mkSysLocal SLIT("fail") assigned_uniq ty, warns) }
165 -- The UserLocal bit just helps make the code a little clearer
167 getUniqueDs :: DsM Unique
168 getUniqueDs dflags us genv loc mod warns
169 = case (uniqFromSupply us) of { assigned_uniq ->
170 (assigned_uniq, warns) }
172 getDOptsDs :: DsM DynFlags
173 getDOptsDs dflags us genv loc mod warns
176 duplicateLocalDs :: Id -> DsM Id
177 duplicateLocalDs old_local dflags us genv loc mod warns
178 = case uniqFromSupply us of { assigned_uniq ->
179 (setIdUnique old_local assigned_uniq, warns) }
181 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
182 cloneTyVarsDs tyvars dflags us genv loc mod warns
183 = case uniqsFromSupply (length tyvars) us of { uniqs ->
184 (zipWithEqual "cloneTyVarsDs" setTyVarUnique tyvars uniqs, warns) }
188 newTyVarsDs :: [TyVar] -> DsM [TyVar]
190 newTyVarsDs tyvar_tmpls dflags us genv loc mod warns
191 = case uniqsFromSupply (length tyvar_tmpls) us of { uniqs ->
192 (zipWithEqual "newTyVarsDs" setTyVarUnique tyvar_tmpls uniqs, warns) }
195 We can also reach out and either set/grab location information from
196 the @SrcLoc@ being carried around.
198 uniqSMtoDsM :: UniqSM a -> DsM a
200 uniqSMtoDsM u_action dflags us genv loc mod warns
201 = (initUs_ us u_action, warns)
203 getSrcLocDs :: DsM SrcLoc
204 getSrcLocDs dflags us genv loc mod warns
207 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
208 putSrcLocDs new_loc expr dflags us genv old_loc mod warns
209 = expr dflags us genv new_loc mod warns
211 dsWarn :: WarnMsg -> DsM ()
212 dsWarn warn dflags us genv loc mod warns = ((), warns `snocBag` warn)
217 getModuleDs :: DsM Module
218 getModuleDs dflags us genv loc mod warns = (mod, warns)
222 dsLookupGlobalValue :: Name -> DsM Id
223 dsLookupGlobalValue name dflags us genv loc mod warns
228 %************************************************************************
230 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
232 %************************************************************************
236 = DsMatchContext DsMatchKind [TypecheckedPat] SrcLoc