2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[DsMonad]{@DsMonad@: monadery used in desugaring}
9 initDs, returnDs, thenDs, andDs, mapDs, listDs,
10 mapAndUnzipDs, zipWithDs,
12 newTyVarsDs, cloneTyVarsDs,
13 duplicateLocalDs, newSysLocalDs, newSysLocalsDs,
15 getSrcLocDs, putSrcLocDs,
17 extendEnvDs, lookupEnvDs,
22 DsMatchContext(..), DsMatchKind(..), pprDsWarnings
25 #include "HsVersions.h"
27 import Bag ( emptyBag, snocBag, bagToList, Bag )
28 import BasicTypes ( Module )
29 import ErrUtils ( WarnMsg )
30 import HsSyn ( OutPat )
31 import Id ( mkSysLocal, mkIdWithNewUniq,
32 lookupIdEnv, growIdEnvList, GenId, IdEnv,
35 import PprType ( GenType, GenTyVar )
37 import SrcLoc ( noSrcLoc, SrcLoc )
38 import TcHsSyn ( TypecheckedPat )
40 import TyVar ( cloneTyVar, TyVar )
41 import UniqSupply ( splitUniqSupply, getUnique, getUniques,
43 import Util ( zipWithEqual, panic )
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:
54 -> SrcLoc -- to put in pattern-matching error msgs
55 -> (Module, Group) -- module + group name : for SCC profiling
58 -> (result, DsWarnings)
60 type DsWarnings = Bag WarnMsg -- The desugarer reports matches which are
61 -- completely shadowed or incomplete patterns
63 type Group = FAST_STRING
67 {-# INLINE returnDs #-}
69 -- initDs returns the UniqSupply out the end (not just the result)
73 -> (Module, Group) -- module name: for profiling; (group name: from switches)
77 initDs init_us env module_and_group action
78 = action init_us noSrcLoc module_and_group env 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 us loc mod_and_grp env warns
84 = case splitUniqSupply us of { (s1, s2) ->
85 case (m1 s1 loc mod_and_grp env warns) of { (result, warns1) ->
86 m2 result s2 loc mod_and_grp env warns1}}
88 andDs combiner m1 m2 us loc mod_and_grp env warns
89 = case splitUniqSupply us of { (s1, s2) ->
90 case (m1 s1 loc mod_and_grp env warns) of { (result1, warns1) ->
91 case (m2 s2 loc mod_and_grp env warns1) of { (result2, warns2) ->
92 (combiner result1 result2, warns2) }}}
94 returnDs :: a -> DsM a
95 returnDs result us loc mod_and_grp env 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 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
114 mapAndUnzipDs f [] = returnDs ([], [])
115 mapAndUnzipDs f (x:xs)
116 = f x `thenDs` \ (r1, r2) ->
117 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
118 returnDs (r1:rs1, r2:rs2)
120 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
122 zipWithDs f [] ys = returnDs []
123 zipWithDs f (x:xs) (y:ys)
124 = f x y `thenDs` \ r ->
125 zipWithDs f xs ys `thenDs` \ rs ->
129 And all this mysterious stuff is so we can occasionally reach out and
130 grab one or more names. @newLocalDs@ isn't exported---exported
131 functions are defined with it. The difference in name-strings makes
132 it easier to read debugging output.
135 newLocalDs :: FAST_STRING -> Type -> DsM Id
136 newLocalDs nm ty us loc mod_and_grp env warns
137 = case (getUnique us) of { assigned_uniq ->
138 (mkSysLocal nm assigned_uniq ty loc, warns) }
140 newSysLocalDs = newLocalDs SLIT("ds")
141 newSysLocalsDs tys = mapDs (newLocalDs SLIT("ds")) tys
142 newFailLocalDs = newLocalDs SLIT("fail")
144 duplicateLocalDs :: Id -> DsM Id
145 duplicateLocalDs old_local us loc mod_and_grp env warns
146 = case (getUnique us) of { assigned_uniq ->
147 (mkIdWithNewUniq old_local assigned_uniq, warns) }
149 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
150 cloneTyVarsDs tyvars us loc mod_and_grp env warns
151 = case (getUniques (length tyvars) us) of { uniqs ->
152 (zipWithEqual "cloneTyVarsDs" cloneTyVar tyvars uniqs, warns) }
156 newTyVarsDs :: [TyVar] -> DsM [TyVar]
158 newTyVarsDs tyvar_tmpls us loc mod_and_grp env warns
159 = case (getUniques (length tyvar_tmpls) us) of { uniqs ->
160 (zipWithEqual "newTyVarsDs" cloneTyVar tyvar_tmpls uniqs, warns) }
163 We can also reach out and either set/grab location information from
164 the @SrcLoc@ being carried around.
166 uniqSMtoDsM :: UniqSM a -> DsM a
168 uniqSMtoDsM u_action us loc mod_and_grp env warns
169 = (u_action us, warns)
171 getSrcLocDs :: DsM SrcLoc
172 getSrcLocDs us loc mod_and_grp env warns
175 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
176 putSrcLocDs new_loc expr us old_loc mod_and_grp env warns
177 = expr us new_loc mod_and_grp env warns
179 dsWarn :: WarnMsg -> DsM ()
180 dsWarn warn us loc mod_and_grp env warns = ((), warns `snocBag` warn)
185 getModuleAndGroupDs :: DsM (FAST_STRING, FAST_STRING)
186 getModuleAndGroupDs us loc mod_and_grp env warns
187 = (mod_and_grp, warns)
191 type DsIdEnv = IdEnv Id
193 extendEnvDs :: [(Id, Id)] -> DsM a -> DsM a
195 extendEnvDs pairs then_do us loc mod_and_grp old_env warns
196 = then_do us loc mod_and_grp (growIdEnvList old_env pairs) warns
198 lookupEnvDs :: Id -> DsM Id
199 lookupEnvDs id us loc mod_and_grp env warns
200 = (case (lookupIdEnv env id) of
206 %************************************************************************
208 %* type synonym EquationInfo and access functions for its pieces *
210 %************************************************************************
214 = DsMatchContext DsMatchKind [TypecheckedPat] SrcLoc
228 pprDsWarnings :: DsWarnings -> SDoc
229 pprDsWarnings warns = vcat (bagToList warns)