[project @ 1997-07-26 03:14:06 by sof]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcEnv.lhs
1 \begin{code}
2 #include "HsVersions.h"
3
4 module TcEnv(
5         TcEnv, 
6
7         initEnv, getEnv_LocalIds, getEnv_TyCons, getEnv_Classes,
8         
9         tcExtendTyVarEnv, tcLookupTyVar, 
10
11         tcExtendTyConEnv, tcLookupTyCon, tcLookupTyConByKey, 
12         tcExtendClassEnv, tcLookupClass, tcLookupClassByKey,
13         tcGetTyConsAndClasses,
14
15         tcExtendGlobalValEnv, tcExtendLocalValEnv,
16         tcLookupLocalValue, tcLookupLocalValueOK, tcLookupLocalValueByKey, 
17         tcLookupGlobalValue, tcLookupGlobalValueByKey, tcLookupGlobalValueMaybe,
18         tcAddImportedIdInfo, tcExplicitLookupGlobal,
19         tcLookupGlobalValueByKeyMaybe, 
20
21         newMonoIds, newLocalIds, newLocalId,
22         tcGetGlobalTyVars, tcExtendGlobalTyVars
23   ) where
24
25
26 IMP_Ubiq()
27 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
28 IMPORT_DELOOPER(TcMLoop)  -- for paranoia checking
29 #endif
30
31 import HsTypes  ( HsTyVar(..) )
32 import Id       ( SYN_IE(Id), GenId, idType, mkUserLocal, mkUserId, replaceIdInfo, getIdInfo )
33 import PragmaInfo ( PragmaInfo(..) )
34 import TcKind   ( TcKind, newKindVars, newKindVar, tcDefaultKind, kindToTcKind, Kind )
35 import TcType   ( SYN_IE(TcIdBndr), TcIdOcc(..),
36                   SYN_IE(TcType), TcMaybe, SYN_IE(TcTyVar), SYN_IE(TcTyVarSet),
37                   newTyVarTys, tcInstTyVars, zonkTcTyVars
38                 )
39 import TyVar    ( unionTyVarSets, emptyTyVarSet, tyVarSetToList, SYN_IE(TyVar) )
40 import PprType  ( GenTyVar )
41 import Type     ( tyVarsOfTypes, splitForAllTy )
42 import TyCon    ( TyCon, tyConKind, synTyConArity, SYN_IE(Arity) )
43 import Class    ( SYN_IE(Class), GenClass )
44
45 import TcMonad
46
47 import IdInfo           ( noIdInfo )
48 import Name             ( Name, OccName(..), getSrcLoc, occNameString,
49                           maybeWiredInTyConName, maybeWiredInIdName, isLocallyDefined,
50                           NamedThing(..)
51                         )
52 import Pretty
53 import Unique           ( pprUnique10{-, pprUnique ToDo:rm-}, Unique, Uniquable(..) )
54 import UniqFM        
55 import Util             ( zipEqual, zipWithEqual, zipWith3Equal, zipLazy,
56                           panic, pprPanic, pprTrace
57                         )
58 import Outputable
59 \end{code}
60
61 Data type declarations
62 ~~~~~~~~~~~~~~~~~~~~~
63
64 \begin{code}
65 data TcEnv s = TcEnv
66                   (TyVarEnv s)
67                   (TyConEnv s)
68                   (ClassEnv s)
69                   (ValueEnv Id)                 -- Globals
70                   (ValueEnv (TcIdBndr s))       -- Locals
71                   (MutableVar s (TcTyVarSet s)) -- Free type variables of locals
72                                                 -- ...why mutable? see notes with tcGetGlobalTyVars
73
74 type TyVarEnv s  = UniqFM (TcKind s, TyVar)
75 type TyConEnv s  = UniqFM (TcKind s, Maybe Arity, TyCon)        -- Arity present for Synonyms only
76 type ClassEnv s  = UniqFM (TcKind s, Class)
77 type ValueEnv id = UniqFM id
78
79 initEnv :: MutableVar s (TcTyVarSet s) -> TcEnv s
80 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM emptyUFM emptyUFM mut 
81
82 getEnv_LocalIds (TcEnv _ _ _ _ ls _) = eltsUFM ls
83 getEnv_TyCons   (TcEnv _ ts _ _ _ _) = [tycon | (_, _, tycon) <- eltsUFM ts]
84 getEnv_Classes  (TcEnv _ _ cs _ _ _) = [clas  | (_, clas)     <- eltsUFM cs]
85 \end{code}
86
87 Type variable env
88 ~~~~~~~~~~~~~~~~~
89 \begin{code}
90 tcExtendTyVarEnv :: [Name] -> [(TcKind s, TyVar)] -> TcM s r -> TcM s r
91 tcExtendTyVarEnv names kinds_w_types scope
92   = tcGetEnv                                    `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
93     let
94         tve' = addListToUFM tve (zipEqual "tcTyVarScope" names kinds_w_types)
95     in
96     tcSetEnv (TcEnv tve' tce ce gve lve gtvs) scope
97 \end{code}
98
99 The Kind, TyVar, Class and TyCon envs
100 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101
102 Extending the environments.  Notice the uses of @zipLazy@, which makes sure
103 that the knot-tied TyVars, TyCons and Classes aren't looked at too early.
104
105 \begin{code}
106 tcExtendTyConEnv :: [(Name,Maybe Arity)] -> [TyCon] -> TcM s r -> TcM s r
107
108 tcExtendTyConEnv names_w_arities tycons scope
109   = newKindVars (length names_w_arities)        `thenNF_Tc` \ kinds ->
110     tcGetEnv                                    `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
111     let
112         tce' = addListToUFM tce [ (name, (kind, arity, tycon)) 
113                                 | ((name,arity), (kind,tycon))
114                                   <- zipEqual "tcExtendTyConEnv" names_w_arities (kinds `zipLazy` tycons)
115                                 ]
116     in
117     tcSetEnv (TcEnv tve tce' ce gve lve gtvs) scope     `thenTc` \ result ->
118     mapNF_Tc tcDefaultKind kinds                        `thenNF_Tc_`
119     returnTc result 
120
121
122 tcExtendClassEnv :: [Name] -> [Class] -> TcM s r -> TcM s r
123 tcExtendClassEnv names classes scope
124   = newKindVars (length names)  `thenNF_Tc` \ kinds ->
125     tcGetEnv                    `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
126     let
127         ce' = addListToUFM ce (zipEqual "tcExtendClassEnv" names (kinds `zipLazy` classes))
128     in
129     tcSetEnv (TcEnv tve tce ce' gve lve gtvs) scope     `thenTc` \ result ->
130     mapNF_Tc tcDefaultKind kinds                        `thenNF_Tc_`
131     returnTc result 
132 \end{code}
133
134
135 Looking up in the environments.
136
137 \begin{code}
138 tcLookupTyVar name
139   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
140     returnNF_Tc (lookupWithDefaultUFM tve (pprPanic "tcLookupTyVar:" (ppr PprShowAll name)) name)
141
142
143 tcLookupTyCon name
144   = case maybeWiredInTyConName name of
145         Just tc -> returnTc (kindToTcKind (tyConKind tc), synTyConArity tc, tc)
146         Nothing -> tcGetEnv             `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
147                    case lookupUFM tce name of
148                         Just stuff -> returnTc stuff
149                         Nothing    ->   -- Could be that he's using a class name as a type constructor
150                                       case lookupUFM ce name of
151                                         Just _  -> failTc (classAsTyConErr name)
152                                         Nothing -> pprPanic "tcLookupTyCon:" (ppr PprDebug name)
153
154 tcLookupTyConByKey uniq
155   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
156     let 
157        (kind, arity, tycon) =  lookupWithDefaultUFM_Directly tce 
158                                         (pprPanic "tcLookupTyCon:" (pprUnique10 uniq)) 
159                                         uniq
160     in
161     returnNF_Tc tycon
162
163 tcLookupClass name
164   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
165 --  pprTrace "tcLookupClass:" (hsep [text "Uniq:", pprUnique10 (uniqueOf name), text "; avail:", hsep (map (pprUnique10 . fst) (ufmToList ce))]) $
166 --  pprTrace "tcLookupClass:" (hsep [text "Uniq:", pprUnique (uniqueOf name), text "; avail:", hsep (map (pprUnique . fst) (ufmToList ce))]) $
167     case lookupUFM ce name of
168         Just stuff -> returnTc stuff
169         Nothing    ->   -- Could be that he's using a type constructor as a class
170                         case lookupUFM tce name of
171                           Just _ ->  failTc (tyConAsClassErr name)
172                           Nothing -> pprPanic "tcLookupClass:" (ppr PprShowAll name)
173
174 tcLookupClassByKey uniq
175   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
176     let
177         (kind, clas) = lookupWithDefaultUFM_Directly ce 
178                                 (pprPanic "tcLookupClassByKey:" (pprUnique10 uniq))
179                                 uniq
180     in
181     returnNF_Tc clas
182
183 tcGetTyConsAndClasses :: NF_TcM s ([TyCon], [Class])
184 tcGetTyConsAndClasses
185   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
186     returnNF_Tc ([tc | (_, _, tc) <- eltsUFM tce],
187                  [c  | (_, c)     <- eltsUFM ce])
188 \end{code}
189
190
191
192 Extending and consulting the value environment
193 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194 \begin{code}
195 tcExtendGlobalValEnv ids scope
196   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
197     let
198         gve' = addListToUFM_Directly gve [(uniqueOf id, id) | id <- ids]
199     in
200     tcSetEnv (TcEnv tve tce ce gve' lve gtvs) scope
201
202 tcExtendLocalValEnv names ids scope
203   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
204     tcReadMutVar gtvs   `thenNF_Tc` \ global_tvs ->
205     let
206         lve' = addListToUFM lve (zipEqual "tcExtendLocalValEnv" names ids)
207         extra_global_tyvars = tyVarsOfTypes (map idType ids)
208         new_global_tyvars   = global_tvs `unionTyVarSets` extra_global_tyvars
209     in
210     tcNewMutVar new_global_tyvars       `thenNF_Tc` \ gtvs' ->
211
212     tcSetEnv (TcEnv tve tce ce gve lve' gtvs') scope
213 \end{code}
214
215 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
216 To improve subsequent calls to the same function it writes the zonked set back into
217 the environment.
218
219 \begin{code}
220 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
221 tcGetGlobalTyVars
222   = tcGetEnv                            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
223     tcReadMutVar gtvs                   `thenNF_Tc` \ global_tvs ->
224     zonkTcTyVars global_tvs             `thenNF_Tc` \ global_tvs' ->
225     tcWriteMutVar gtvs global_tvs'      `thenNF_Tc_` 
226     returnNF_Tc global_tvs'
227
228 tcExtendGlobalTyVars extra_global_tvs scope
229   = tcGetEnv                            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
230     tcReadMutVar gtvs                   `thenNF_Tc` \ global_tvs ->
231     let
232         new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tvs
233     in
234     tcNewMutVar new_global_tyvars       `thenNF_Tc` \ gtvs' ->
235     tcSetEnv (TcEnv tve tce ce gve lve gtvs') scope
236 \end{code}
237
238 \begin{code}
239 tcLookupLocalValue :: Name -> NF_TcM s (Maybe (TcIdBndr s))
240 tcLookupLocalValue name
241   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
242     returnNF_Tc (lookupUFM lve name)
243
244 tcLookupLocalValueByKey :: Unique -> NF_TcM s (Maybe (TcIdBndr s))
245 tcLookupLocalValueByKey uniq
246   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
247     returnNF_Tc (lookupUFM_Directly lve uniq)
248
249 tcLookupLocalValueOK :: String -> Name -> NF_TcM s (TcIdBndr s)
250 tcLookupLocalValueOK err name
251   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
252     returnNF_Tc (lookupWithDefaultUFM lve (panic err) name)
253
254
255 tcLookupGlobalValue :: Name -> NF_TcM s Id
256 tcLookupGlobalValue name
257   = case maybeWiredInIdName name of
258         Just id -> returnNF_Tc id
259         Nothing -> tcGetEnv             `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
260                    returnNF_Tc (lookupWithDefaultUFM gve def name)
261   where
262     def = pprPanic "tcLookupGlobalValue:" (ppr PprDebug name)
263
264 tcLookupGlobalValueMaybe :: Name -> NF_TcM s (Maybe Id)
265 tcLookupGlobalValueMaybe name
266   = case maybeWiredInIdName name of
267         Just id -> returnNF_Tc (Just id)
268         Nothing -> tcGetEnv             `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
269                    returnNF_Tc (lookupUFM gve name)
270
271
272 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
273 tcLookupGlobalValueByKey uniq
274   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
275     returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
276   where
277 #ifdef DEBUG
278     def = pprPanic "tcLookupGlobalValueByKey:" (pprUnique10 uniq)
279 #else
280     def = panic "tcLookupGlobalValueByKey"
281 #endif
282
283 tcLookupGlobalValueByKeyMaybe :: Unique -> NF_TcM s (Maybe Id)
284 tcLookupGlobalValueByKeyMaybe uniq
285   = tcGetEnv            `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
286     returnNF_Tc (lookupUFM_Directly gve uniq)
287
288
289 -- Non-monadic version, environment given explicitly
290 tcExplicitLookupGlobal :: TcEnv s -> Name -> Maybe Id
291 tcExplicitLookupGlobal (TcEnv tve tce ce gve lve gtvs) name
292   = case maybeWiredInIdName name of
293         Just id -> Just id
294         Nothing -> lookupUFM gve name
295
296         -- Extract the IdInfo from an IfaceSig imported from an interface file
297 tcAddImportedIdInfo :: TcEnv s -> Id -> Id
298 tcAddImportedIdInfo unf_env id
299   | isLocallyDefined id         -- Don't look up locally defined Ids, because they
300                                 -- have explicit local definitions, so we get a black hole!
301   = id
302   | otherwise
303   = id `replaceIdInfo` new_info
304         -- The Id must be returned without a data dependency on maybe_id
305   where
306     new_info = -- pprTrace "tcAdd" (ppr PprDebug id) $
307                case tcExplicitLookupGlobal unf_env (getName id) of
308                      Nothing          -> noIdInfo
309                      Just imported_id -> getIdInfo imported_id
310                 -- ToDo: could check that types are the same
311 \end{code}
312
313
314 Constructing new Ids
315 ~~~~~~~~~~~~~~~~~~~~
316
317 \begin{code}
318 -- Uses the Name as the Name of the Id
319 newMonoIds :: [Name] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
320
321 newMonoIds names kind m
322   = newTyVarTys no_of_names kind        `thenNF_Tc` \ tys ->
323     let
324         new_ids       = zipWithEqual "newMonoIds" mk_id names tys
325         mk_id name ty = mkUserId name ty NoPragmaInfo
326     in
327     tcExtendLocalValEnv names new_ids (m new_ids)
328   where
329     no_of_names = length names
330
331 newLocalId :: OccName -> TcType s -> NF_TcM s (TcIdBndr s)
332 newLocalId name ty
333   = tcGetSrcLoc         `thenNF_Tc` \ loc ->
334     tcGetUnique         `thenNF_Tc` \ uniq ->
335     returnNF_Tc (mkUserLocal name uniq ty loc)
336
337 newLocalIds :: [OccName] -> [TcType s] -> NF_TcM s [TcIdBndr s]
338 newLocalIds names tys
339   = tcGetSrcLoc                 `thenNF_Tc` \ loc ->
340     tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
341     let
342         new_ids            = zipWith3Equal "newLocalIds" mk_id names uniqs tys
343         mk_id name uniq ty = mkUserLocal name uniq ty loc
344     in
345     returnNF_Tc new_ids
346 \end{code}
347
348 \begin{code}
349 classAsTyConErr name sty
350   = hcat [ptext SLIT("Class used as a type constructor: "), ppr sty name]
351
352 tyConAsClassErr name sty
353   = hcat [ptext SLIT("Type constructor used as a class: "), ppr sty name]
354 \end{code}