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 MkId ( mkSysLocal )
32 import Id ( mkIdWithNewUniq,
33 lookupIdEnv, growIdEnvList, IdEnv, Id
36 import SrcLoc ( noSrcLoc, SrcLoc )
37 import TcHsSyn ( TypecheckedPat )
39 import TyVar ( cloneTyVar, TyVar )
40 import UniqSupply ( splitUniqSupply, getUnique, getUniques,
42 import Util ( zipWithEqual, panic )
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:
53 -> SrcLoc -- to put in pattern-matching error msgs
54 -> (Module, Group) -- module + group name : for SCC profiling
57 -> (result, DsWarnings)
59 type DsWarnings = Bag WarnMsg -- The desugarer reports matches which are
60 -- completely shadowed or incomplete patterns
62 type Group = FAST_STRING
66 {-# INLINE returnDs #-}
68 -- initDs returns the UniqSupply out the end (not just the result)
72 -> (Module, Group) -- module name: for profiling; (group name: from switches)
76 initDs init_us env module_and_group action
77 = action init_us noSrcLoc module_and_group env emptyBag
79 thenDs :: DsM a -> (a -> DsM b) -> DsM b
80 andDs :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
82 thenDs m1 m2 us loc mod_and_grp env warns
83 = case splitUniqSupply us of { (s1, s2) ->
84 case (m1 s1 loc mod_and_grp env warns) of { (result, warns1) ->
85 m2 result s2 loc mod_and_grp env warns1}}
87 andDs combiner m1 m2 us loc mod_and_grp env warns
88 = case splitUniqSupply us of { (s1, s2) ->
89 case (m1 s1 loc mod_and_grp env warns) of { (result1, warns1) ->
90 case (m2 s2 loc mod_and_grp env warns1) of { (result2, warns2) ->
91 (combiner result1 result2, warns2) }}}
93 returnDs :: a -> DsM a
94 returnDs result us loc mod_and_grp env warns = (result, warns)
96 listDs :: [DsM a] -> DsM [a]
97 listDs [] = returnDs []
100 listDs xs `thenDs` \ rs ->
103 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
105 mapDs f [] = returnDs []
107 = f x `thenDs` \ r ->
108 mapDs f xs `thenDs` \ rs ->
111 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
113 mapAndUnzipDs f [] = returnDs ([], [])
114 mapAndUnzipDs f (x:xs)
115 = f x `thenDs` \ (r1, r2) ->
116 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
117 returnDs (r1:rs1, r2:rs2)
119 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
121 zipWithDs f [] ys = returnDs []
122 zipWithDs f (x:xs) (y:ys)
123 = f x y `thenDs` \ r ->
124 zipWithDs f xs ys `thenDs` \ rs ->
128 And all this mysterious stuff is so we can occasionally reach out and
129 grab one or more names. @newLocalDs@ isn't exported---exported
130 functions are defined with it. The difference in name-strings makes
131 it easier to read debugging output.
134 newLocalDs :: FAST_STRING -> Type -> DsM Id
135 newLocalDs nm ty us loc mod_and_grp env warns
136 = case (getUnique us) of { assigned_uniq ->
137 (mkSysLocal nm assigned_uniq ty loc, warns) }
139 newSysLocalDs = newLocalDs SLIT("ds")
140 newSysLocalsDs tys = mapDs (newLocalDs SLIT("ds")) tys
141 newFailLocalDs = newLocalDs SLIT("fail")
143 duplicateLocalDs :: Id -> DsM Id
144 duplicateLocalDs old_local us loc mod_and_grp env warns
145 = case (getUnique us) of { assigned_uniq ->
146 (mkIdWithNewUniq old_local assigned_uniq, warns) }
148 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
149 cloneTyVarsDs tyvars us loc mod_and_grp env warns
150 = case (getUniques (length tyvars) us) of { uniqs ->
151 (zipWithEqual "cloneTyVarsDs" cloneTyVar tyvars uniqs, warns) }
155 newTyVarsDs :: [TyVar] -> DsM [TyVar]
157 newTyVarsDs tyvar_tmpls us loc mod_and_grp env warns
158 = case (getUniques (length tyvar_tmpls) us) of { uniqs ->
159 (zipWithEqual "newTyVarsDs" cloneTyVar tyvar_tmpls uniqs, warns) }
162 We can also reach out and either set/grab location information from
163 the @SrcLoc@ being carried around.
165 uniqSMtoDsM :: UniqSM a -> DsM a
167 uniqSMtoDsM u_action us loc mod_and_grp env warns
168 = (u_action us, warns)
170 getSrcLocDs :: DsM SrcLoc
171 getSrcLocDs us loc mod_and_grp env warns
174 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
175 putSrcLocDs new_loc expr us old_loc mod_and_grp env warns
176 = expr us new_loc mod_and_grp env warns
178 dsWarn :: WarnMsg -> DsM ()
179 dsWarn warn us loc mod_and_grp env warns = ((), warns `snocBag` warn)
184 getModuleAndGroupDs :: DsM (FAST_STRING, FAST_STRING)
185 getModuleAndGroupDs us loc mod_and_grp env warns
186 = (mod_and_grp, warns)
190 type DsIdEnv = IdEnv Id
192 extendEnvDs :: [(Id, Id)] -> DsM a -> DsM a
194 extendEnvDs pairs then_do us loc mod_and_grp old_env warns
195 = then_do us loc mod_and_grp (growIdEnvList old_env pairs) warns
197 lookupEnvDs :: Id -> DsM Id
198 lookupEnvDs id us loc mod_and_grp env warns
199 = (case (lookupIdEnv env id) of
205 %************************************************************************
207 %* type synonym EquationInfo and access functions for its pieces *
209 %************************************************************************
213 = DsMatchContext DsMatchKind [TypecheckedPat] SrcLoc
227 pprDsWarnings :: DsWarnings -> SDoc
228 pprDsWarnings warns = vcat (bagToList warns)