[project @ 1996-12-19 09:10:02 by simonpj]
[ghc-hetmet.git] / ghc / compiler / prelude / PrelInfo.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[PrelInfo]{The @PrelInfo@ interface to the compiler's prelude knowledge}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module PrelInfo (
10         -- finite maps for built-in things (for the renamer and typechecker):
11         builtinNames, builtinKeys, derivingOccurrences,
12         SYN_IE(BuiltinNames),
13
14         maybeCharLikeTyCon, maybeIntLikeTyCon,
15
16         eq_RDR, ne_RDR, le_RDR, lt_RDR, ge_RDR, gt_RDR, max_RDR, min_RDR, compare_RDR, 
17         minBound_RDR, maxBound_RDR, enumFrom_RDR, enumFromTo_RDR, enumFromThen_RDR, 
18         enumFromThenTo_RDR, fromEnum_RDR,
19         range_RDR, index_RDR, inRange_RDR, readsPrec_RDR, readList_RDR, 
20         showsPrec_RDR, showList_RDR, plus_RDR, times_RDR, ltTag_RDR, eqTag_RDR, gtTag_RDR, 
21         eqH_Char_RDR, ltH_Char_RDR, eqH_Word_RDR, ltH_Word_RDR, eqH_Addr_RDR, ltH_Addr_RDR, 
22         eqH_Float_RDR, ltH_Float_RDR, eqH_Double_RDR, ltH_Double_RDR, eqH_Int_RDR, ltH_Int_RDR, 
23         geH_RDR, leH_RDR, minusH_RDR, false_RDR, true_RDR, and_RDR, not_RDR, append_RDR, 
24         map_RDR, compose_RDR, mkInt_RDR, error_RDR, showString_RDR, showParen_RDR, readParen_RDR, 
25         lex_RDR, showSpace_RDR, showList___RDR, readList___RDR, negate_RDR,
26
27         numClass_RDR, fractionalClass_RDR, eqClass_RDR, ccallableClass_RDR, creturnableClass_RDR,
28         monadZeroClass_RDR, enumClass_RDR, evalClass_RDR, ordClass_RDR,
29
30         needsDataDeclCtxtClassKeys, cCallishClassKeys, isNoDictClass,
31         isNumericClass, isStandardClass, isCcallishClass
32     ) where
33
34 IMP_Ubiq()
35 IMPORT_DELOOPER(PrelLoop) ( primOpName )
36 -- IMPORT_DELOOPER(IdLoop)        ( SpecEnv )
37
38 -- friends:
39 import PrelMods         -- Prelude module names
40 import PrelVals         -- VALUES
41 import PrimOp           ( PrimOp(..), allThePrimOps )
42 import PrimRep          ( PrimRep(..) )
43 import TysPrim          -- TYPES
44 import TysWiredIn
45
46 -- others:
47 import SpecEnv          ( SpecEnv )
48 import RdrHsSyn         ( RdrName(..), varQual, tcQual, qual )
49 import Id               ( GenId, SYN_IE(Id) )
50 import Name             ( Name, OccName(..), DefnInfo(..), Provenance(..),
51                           getName, mkGlobalName, modAndOcc )
52 import Class            ( Class(..), GenClass, classKey )
53 import TyCon            ( tyConDataCons, mkFunTyCon, TyCon )
54 import Type
55 import Bag
56 import Unique           -- *Key stuff
57 import UniqFM           ( UniqFM, listToUFM ) 
58 import Util             ( isIn )
59 \end{code}
60
61 %************************************************************************
62 %*                                                                      *
63 \subsection[builtinNameInfo]{Lookup built-in names}
64 %*                                                                      *
65 %************************************************************************
66
67 We have two ``builtin name funs,'' one to look up @TyCons@ and
68 @Classes@, the other to look up values.
69
70 \begin{code}
71 type BuiltinNames = Bag Name
72
73 builtinNames :: BuiltinNames
74 builtinNames
75   =     -- Wired in TyCons
76     unionManyBags (map getTyConNames wired_in_tycons)   `unionBags`
77
78         -- Wired in Ids
79     listToBag (map getName wired_in_ids)                `unionBags`
80
81         -- PrimOps
82     listToBag (map (getName.primOpName) allThePrimOps)  `unionBags`
83
84         -- Other names with magic keys
85     listToBag builtinKeys
86 \end{code}
87
88
89 \begin{code}
90 getTyConNames :: TyCon -> Bag Name
91 getTyConNames tycon
92     =  getName tycon `consBag` listToBag (map getName (tyConDataCons tycon))
93         -- Synonyms return empty list of constructors
94 \end{code}
95
96
97 We let a lot of "non-standard" values be visible, so that we can make
98 sense of them in interface pragmas. It's cool, though they all have
99 "non-standard" names, so they won't get past the parser in user code.
100
101 %************************************************************************
102 %*                                                                      *
103 \subsection{Wired in TyCons}
104 %*                                                                      *
105 %************************************************************************
106
107
108 \begin{code}
109 wired_in_tycons = [mkFunTyCon] ++
110                   prim_tycons ++
111                   tuple_tycons ++
112                   data_tycons
113
114 prim_tycons
115   = [ addrPrimTyCon
116     , arrayPrimTyCon
117     , byteArrayPrimTyCon
118     , charPrimTyCon
119     , doublePrimTyCon
120     , floatPrimTyCon
121     , intPrimTyCon
122     , foreignObjPrimTyCon
123     , mutableArrayPrimTyCon
124     , mutableByteArrayPrimTyCon
125     , synchVarPrimTyCon
126     , realWorldTyCon
127     , stablePtrPrimTyCon
128     , statePrimTyCon
129     , wordPrimTyCon
130     ]
131
132 tuple_tycons = unitTyCon : [tupleTyCon i | i <- [2..37] ]
133
134
135 data_tycons
136   = [ listTyCon
137     , addrTyCon
138     , boolTyCon
139     , charTyCon
140     , doubleTyCon
141     , floatTyCon
142     , foreignObjTyCon
143     , intTyCon
144     , integerTyCon
145     , liftTyCon
146     , primIoTyCon
147     , return2GMPsTyCon
148     , returnIntAndGMPTyCon
149     , stTyCon
150     , stablePtrTyCon
151     , stateAndAddrPrimTyCon
152     , stateAndArrayPrimTyCon
153     , stateAndByteArrayPrimTyCon
154     , stateAndCharPrimTyCon
155     , stateAndDoublePrimTyCon
156     , stateAndFloatPrimTyCon
157     , stateAndForeignObjPrimTyCon
158     , stateAndIntPrimTyCon
159     , stateAndMutableArrayPrimTyCon
160     , stateAndMutableByteArrayPrimTyCon
161     , stateAndPtrPrimTyCon
162     , stateAndStablePtrPrimTyCon
163     , stateAndSynchVarPrimTyCon
164     , stateAndWordPrimTyCon
165     , stateTyCon
166     , voidTyCon
167     , wordTyCon
168     ]
169
170 min_nonprim_tycon_list  -- used w/ HideMostBuiltinNames
171   = [ boolTyCon
172     , charTyCon
173     , intTyCon
174     , floatTyCon
175     , doubleTyCon
176     , integerTyCon
177     , liftTyCon
178     , return2GMPsTyCon  -- ADR asked for these last two (WDP 94/11)
179     , returnIntAndGMPTyCon
180     ]
181 \end{code}
182
183 %************************************************************************
184 %*                                                                      *
185 \subsection{Wired in Ids}
186 %*                                                                      *
187 %************************************************************************
188
189 The WiredIn Ids ...
190 ToDo: Some of these should be moved to id_keys_infos!
191
192 \begin{code}
193 wired_in_ids
194   = [ aBSENT_ERROR_ID
195     , augmentId
196     , buildId
197     , eRROR_ID
198     , foldlId
199     , foldrId
200     , iRREFUT_PAT_ERROR_ID
201     , integerMinusOneId
202     , integerPlusOneId
203     , integerPlusTwoId
204     , integerZeroId
205     , nON_EXHAUSTIVE_GUARDS_ERROR_ID
206     , nO_DEFAULT_METHOD_ERROR_ID
207     , nO_EXPLICIT_METHOD_ERROR_ID
208     , pAR_ERROR_ID
209     , pAT_ERROR_ID
210     , packStringForCId
211     , rEC_CON_ERROR_ID
212     , rEC_UPD_ERROR_ID
213     , realWorldPrimId
214     , runSTId
215     , tRACE_ID
216     , unpackCString2Id
217     , unpackCStringAppendId
218     , unpackCStringFoldrId
219     , unpackCStringId
220     , voidId
221
222 --  , copyableId
223 --  , forkId
224 --  , noFollowId
225 --    , parAtAbsId
226 --    , parAtForNowId
227 --    , parAtId
228 --    , parAtRelId
229 --    , parGlobalId
230 --    , parId
231 --    , parLocalId
232 --    , seqId
233     ]
234 \end{code}
235
236
237 %************************************************************************
238 %*                                                                      *
239 \subsection{Built-in keys}
240 %*                                                                      *
241 %************************************************************************
242
243 Ids, Synonyms, Classes and ClassOps with builtin keys. 
244
245 \begin{code}
246 getKeyOrig :: (Module, OccName, Unique) -> Name
247 getKeyOrig (mod, occ, uniq) = mkGlobalName uniq mod occ VanillaDefn Implicit
248
249 builtinKeys :: [Name]
250 builtinKeys
251   = map getKeyOrig
252     [
253         -- Type constructors (synonyms especially)
254       (iO_BASE,         TCOcc SLIT("IO"),       iOTyConKey)
255     , (pREL_BASE,       TCOcc SLIT("Ordering"), orderingTyConKey)
256     , (pREL_NUM,        TCOcc SLIT("Rational"), rationalTyConKey)
257     , (pREL_NUM,        TCOcc SLIT("Ratio"),    ratioTyConKey)
258
259
260         --  Classes.  *Must* include:
261         --      classes that are grabbed by key (e.g., eqClassKey)
262         --      classes in "Class.standardClassKeys" (quite a few)
263     , (pREL_BASE, TCOcc SLIT("Eq"),             eqClassKey)             -- mentioned, derivable
264     , (pREL_BASE, TCOcc SLIT("Eval"),           evalClassKey)           -- mentioned
265     , (pREL_BASE, TCOcc SLIT("Ord"),            ordClassKey)            -- derivable
266     , (pREL_BASE, TCOcc SLIT("Bounded"),        boundedClassKey)        -- derivable
267     , (pREL_BASE, TCOcc SLIT("Num"),            numClassKey)            -- mentioned, numeric
268     , (pREL_BASE, TCOcc SLIT("Enum"),           enumClassKey)           -- derivable
269     , (pREL_BASE, TCOcc SLIT("Monad"),          monadClassKey)
270     , (pREL_BASE, TCOcc SLIT("MonadZero"),              monadZeroClassKey)
271     , (pREL_BASE, TCOcc SLIT("MonadPlus"),              monadPlusClassKey)
272     , (pREL_BASE, TCOcc SLIT("Functor"),                functorClassKey)
273     , (pREL_BASE, TCOcc SLIT("Show"),           showClassKey)           -- derivable
274     , (pREL_NUM, TCOcc SLIT("Real"),            realClassKey)           -- numeric
275     , (pREL_NUM, TCOcc SLIT("Integral"),        integralClassKey)       -- numeric
276     , (pREL_NUM, TCOcc SLIT("Fractional"),      fractionalClassKey)     -- numeric
277     , (pREL_NUM, TCOcc SLIT("Floating"),        floatingClassKey)       -- numeric
278     , (pREL_NUM, TCOcc SLIT("RealFrac"),        realFracClassKey)       -- numeric
279     , (pREL_NUM, TCOcc SLIT("RealFloat"),       realFloatClassKey)      -- numeric
280     , (pREL_READ, TCOcc SLIT("Read"),           readClassKey)           -- derivable
281     , (iX,      TCOcc SLIT("Ix"),               ixClassKey)             -- derivable (but it isn't Prelude.Ix; hmmm)
282     , (fOREIGN, TCOcc SLIT("CCallable"),        cCallableClassKey)      -- mentioned, ccallish
283     , (fOREIGN,   TCOcc SLIT("CReturnable"),    cReturnableClassKey)    -- mentioned, ccallish
284
285
286         -- ClassOps 
287     , (pREL_BASE, VarOcc SLIT("fromInt"),       fromIntClassOpKey)
288     , (pREL_BASE, VarOcc SLIT("fromInteger"),   fromIntegerClassOpKey)
289     , (pREL_BASE, VarOcc SLIT("enumFrom"),      enumFromClassOpKey)
290     , (pREL_BASE, VarOcc SLIT("enumFromThen"),  enumFromThenClassOpKey)
291     , (pREL_BASE, VarOcc SLIT("enumFromTo"),    enumFromToClassOpKey)
292     , (pREL_BASE, VarOcc SLIT("enumFromThenTo"), enumFromThenToClassOpKey)
293     , (pREL_BASE, VarOcc SLIT("fromEnum"),      fromEnumClassOpKey)
294     , (pREL_BASE, VarOcc SLIT("=="),            eqClassOpKey)
295     , (pREL_BASE, VarOcc SLIT(">>="),           thenMClassOpKey)
296     , (pREL_BASE, VarOcc SLIT("zero"),          zeroClassOpKey)
297     , (pREL_NUM, VarOcc SLIT("fromRational"),   fromRationalClassOpKey)
298     ]
299 \end{code}
300
301 ToDo: make it do the ``like'' part properly (as in 0.26 and before).
302
303 \begin{code}
304 maybeCharLikeTyCon tc = if (uniqueOf tc == charDataConKey) then Just charDataCon else Nothing
305 maybeIntLikeTyCon  tc = if (uniqueOf tc == intDataConKey)  then Just intDataCon  else Nothing
306 \end{code}
307
308 %************************************************************************
309 %*                                                                      *
310 \subsection{Commonly-used RdrNames}
311 %*                                                                      *
312 %************************************************************************
313
314 These RdrNames are not really "built in", but some parts of the compiler
315 (notably the deriving mechanism) need to mention their names, and it's convenient
316 to write them all down in one place.
317
318 \begin{code}
319 prelude_primop op = qual (modAndOcc (primOpName op))
320
321 eqClass_RDR             = tcQual (pREL_BASE, SLIT("Eq"))
322 ordClass_RDR            = tcQual (pREL_BASE, SLIT("Ord"))
323 evalClass_RDR           = tcQual (pREL_BASE, SLIT("Eval"))
324 monadZeroClass_RDR      = tcQual (pREL_BASE, SLIT("MonadZero"))
325 enumClass_RDR           = tcQual (pREL_BASE, SLIT("Enum"))
326 numClass_RDR            = tcQual (pREL_BASE, SLIT("Num"))
327 fractionalClass_RDR     = tcQual (pREL_NUM,  SLIT("Fractional"))
328 ccallableClass_RDR      = tcQual (fOREIGN,   SLIT("CCallable"))
329 creturnableClass_RDR    = tcQual (fOREIGN,   SLIT("CReturnable"))
330
331 negate_RDR         = varQual (pREL_BASE, SLIT("negate"))
332 eq_RDR             = varQual (pREL_BASE, SLIT("=="))
333 ne_RDR             = varQual (pREL_BASE, SLIT("/="))
334 le_RDR             = varQual (pREL_BASE, SLIT("<="))
335 lt_RDR             = varQual (pREL_BASE, SLIT("<"))
336 ge_RDR             = varQual (pREL_BASE, SLIT(">="))
337 gt_RDR             = varQual (pREL_BASE, SLIT(">"))
338 ltTag_RDR          = varQual (pREL_BASE,  SLIT("LT"))
339 eqTag_RDR          = varQual (pREL_BASE,  SLIT("EQ"))
340 gtTag_RDR          = varQual (pREL_BASE,  SLIT("GT"))
341 max_RDR            = varQual (pREL_BASE, SLIT("max"))
342 min_RDR            = varQual (pREL_BASE, SLIT("min"))
343 compare_RDR        = varQual (pREL_BASE, SLIT("compare"))
344 minBound_RDR       = varQual (pREL_BASE, SLIT("minBound"))
345 maxBound_RDR       = varQual (pREL_BASE, SLIT("maxBound"))
346 false_RDR          = varQual (pREL_BASE,  SLIT("False"))
347 true_RDR           = varQual (pREL_BASE,  SLIT("True"))
348 and_RDR            = varQual (pREL_BASE,  SLIT("&&"))
349 not_RDR            = varQual (pREL_BASE,  SLIT("not"))
350 compose_RDR        = varQual (pREL_BASE, SLIT("."))
351 append_RDR         = varQual (pREL_BASE, SLIT("++"))
352 map_RDR            = varQual (pREL_BASE, SLIT("map"))
353
354 showList___RDR     = varQual (pREL_BASE,  SLIT("showList__"))
355 showsPrec_RDR      = varQual (pREL_BASE, SLIT("showsPrec"))
356 showList_RDR       = varQual (pREL_BASE, SLIT("showList"))
357 showSpace_RDR      = varQual (pREL_BASE,  SLIT("showSpace"))
358 showString_RDR     = varQual (pREL_BASE, SLIT("showString"))
359 showParen_RDR      = varQual (pREL_BASE, SLIT("showParen"))
360
361 range_RDR          = varQual (iX,   SLIT("range"))
362 index_RDR          = varQual (iX,   SLIT("index"))
363 inRange_RDR        = varQual (iX,   SLIT("inRange"))
364
365 readsPrec_RDR      = varQual (pREL_READ, SLIT("readsPrec"))
366 readList_RDR       = varQual (pREL_READ, SLIT("readList"))
367 readParen_RDR      = varQual (pREL_READ, SLIT("readParen"))
368 lex_RDR            = varQual (pREL_READ,  SLIT("lex"))
369 readList___RDR     = varQual (pREL_READ,  SLIT("readList__"))
370
371 fromEnum_RDR       = varQual (pREL_BASE, SLIT("fromEnum"))
372 enumFrom_RDR       = varQual (pREL_BASE, SLIT("enumFrom"))
373 enumFromTo_RDR     = varQual (pREL_BASE, SLIT("enumFromTo"))
374 enumFromThen_RDR   = varQual (pREL_BASE, SLIT("enumFromThen"))
375 enumFromThenTo_RDR = varQual (pREL_BASE, SLIT("enumFromThenTo"))
376 plus_RDR           = varQual (pREL_BASE, SLIT("+"))
377 times_RDR          = varQual (pREL_BASE, SLIT("*"))
378 mkInt_RDR          = varQual (pREL_BASE, SLIT("I#"))
379
380 error_RDR          = varQual (iO_BASE, SLIT("error"))
381
382 eqH_Char_RDR    = prelude_primop CharEqOp
383 ltH_Char_RDR    = prelude_primop CharLtOp
384 eqH_Word_RDR    = prelude_primop WordEqOp
385 ltH_Word_RDR    = prelude_primop WordLtOp
386 eqH_Addr_RDR    = prelude_primop AddrEqOp
387 ltH_Addr_RDR    = prelude_primop AddrLtOp
388 eqH_Float_RDR   = prelude_primop FloatEqOp
389 ltH_Float_RDR   = prelude_primop FloatLtOp
390 eqH_Double_RDR  = prelude_primop DoubleEqOp
391 ltH_Double_RDR  = prelude_primop DoubleLtOp
392 eqH_Int_RDR     = prelude_primop IntEqOp
393 ltH_Int_RDR     = prelude_primop IntLtOp
394 geH_RDR         = prelude_primop IntGeOp
395 leH_RDR         = prelude_primop IntLeOp
396 minusH_RDR      = prelude_primop IntSubOp
397
398 intType_RDR = qual (modAndOcc intTyCon)
399 \end{code}
400
401 %************************************************************************
402 %*                                                                      *
403 \subsection[Class-std-groups]{Standard groups of Prelude classes}
404 %*                                                                      *
405 %************************************************************************
406
407 @derivableClassKeys@ is also used in checking \tr{deriving} constructs
408 (@TcDeriv@).
409
410 @derivingOccurrences@ maps a class name to a list of the (qualified) occurrences
411 that will be mentioned by  the derived code for the class when it is later generated.
412 We don't need to put in things that are WiredIn (because they are already mapped to their
413 correct name by the @NameSupply@.  The class itself, and all its class ops, is
414 already flagged as an occurrence so we don't need to mention that either.
415
416 @derivingOccurrences@ has an item for every derivable class, even if that item is empty,
417 because we treat lookup failure as indicating that the class is illegal in a deriving clause.
418
419 \begin{code}
420 derivingOccurrences :: UniqFM [RdrName]
421 derivingOccurrences = listToUFM deriving_occ_info
422
423 derivableClassKeys  = map fst deriving_occ_info
424
425 deriving_occ_info
426   = [ (eqClassKey,      [intType_RDR, and_RDR, not_RDR])
427     , (ordClassKey,     [intType_RDR, compose_RDR])
428     , (enumClassKey,    [intType_RDR, map_RDR])
429     , (evalClassKey,    [intType_RDR])
430     , (boundedClassKey, [intType_RDR])
431     , (showClassKey,    [intType_RDR, numClass_RDR, ordClass_RDR, compose_RDR, showString_RDR, 
432                          showParen_RDR, showSpace_RDR, showList___RDR])
433     , (readClassKey,    [intType_RDR, numClass_RDR, ordClass_RDR, append_RDR, 
434                          lex_RDR, readParen_RDR, readList___RDR])
435     , (ixClassKey,      [intType_RDR, numClass_RDR, and_RDR, map_RDR])
436     ]
437         -- intType: Practically any deriving needs Int, either for index calculations, 
438         --              or for taggery.
439         -- ordClass: really it's the methods that are actually used.
440         -- numClass: for Int literals
441 \end{code}
442
443
444 NOTE: @Eq@ and @Text@ do need to appear in @standardClasses@
445 even though every numeric class has these two as a superclass,
446 because the list of ambiguous dictionaries hasn't been simplified.
447
448 \begin{code}
449 isCcallishClass, isNoDictClass, isNumericClass, isStandardClass :: Class -> Bool
450
451 isNumericClass   clas = classKey clas `is_elem` numericClassKeys
452 isStandardClass  clas = classKey clas `is_elem` standardClassKeys
453 isCcallishClass  clas = classKey clas `is_elem` cCallishClassKeys
454 isNoDictClass    clas = classKey clas `is_elem` noDictClassKeys
455 is_elem = isIn "is_X_Class"
456
457 numericClassKeys
458   = [ numClassKey
459     , realClassKey
460     , integralClassKey
461     , fractionalClassKey
462     , floatingClassKey
463     , realFracClassKey
464     , realFloatClassKey
465     ]
466
467 needsDataDeclCtxtClassKeys -- see comments in TcDeriv
468   = [ readClassKey
469     ]
470
471 cCallishClassKeys = [ cCallableClassKey, cReturnableClassKey ]
472
473 standardClassKeys
474   = derivableClassKeys ++ numericClassKeys ++ cCallishClassKeys
475     --
476     -- We have to have "CCallable" and "CReturnable" in the standard
477     -- classes, so that if you go...
478     --
479     --      _ccall_ foo ... 93{-numeric literal-} ...
480     --
481     -- ... it can do The Right Thing on the 93.
482
483 noDictClassKeys         -- These classes are used only for type annotations;
484                         -- they are not implemented by dictionaries, ever.
485   = cCallishClassKeys
486         -- I used to think that class Eval belonged in here, but
487         -- we really want functions with type (Eval a => ...) and that
488         -- means that we really want to pass a placeholder for an Eval
489         -- dictionary.  The unit tuple is what we'll get if we leave things
490         -- alone, and that'll do for now.  Could arrange to drop that parameter
491         -- in the end.
492 \end{code}