96d8c8cb9f68431b32a5b1b07e282bdb21dabc22
[ghc-hetmet.git] / compiler / prelude / PrelInfo.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[PrelInfo]{The @PrelInfo@ interface to the compiler's prelude knowledge}
5
6 \begin{code}
7 module PrelInfo (
8         module MkId,
9
10         ghcPrimExports,
11         wiredInThings, basicKnownKeyNames,
12         primOpId,
13         
14         -- Random other things
15         maybeCharLikeCon, maybeIntLikeCon,
16
17         -- Class categories
18         isNumericClass, isStandardClass
19
20     ) where
21
22 #include "HsVersions.h"
23
24 import PrelNames        ( basicKnownKeyNames, 
25                           hasKey, charDataConKey, intDataConKey,
26                           numericClassKeys, standardClassKeys )
27
28 import PrimOp           ( PrimOp, allThePrimOps, primOpOcc, primOpTag, maxPrimOpTag )
29 import DataCon          ( DataCon )
30 import Id               ( Id, idName )
31 import MkId             -- All of it, for re-export
32 import Name             ( nameOccName )
33 import TysPrim          ( primTyCons )
34 import TysWiredIn       ( wiredInTyCons )
35 import HscTypes         ( TyThing(..), implicitTyThings, GenAvailInfo(..), RdrAvailInfo )
36 import Class            ( Class, classKey )
37 import Type             ( funTyCon )
38 import TyCon            ( tyConName )
39 import Util             ( isIn )
40
41 import Array            ( Array, array, (!) )
42 \end{code}
43
44 %************************************************************************
45 %*                                                                      *
46 \subsection[builtinNameInfo]{Lookup built-in names}
47 %*                                                                      *
48 %************************************************************************
49
50 Notes about wired in things
51 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
52 * Wired-in things are Ids\/TyCons that are completely known to the compiler.
53   They are global values in GHC, (e.g.  listTyCon :: TyCon).
54
55 * A wired in Name contains the thing itself inside the Name: 
56         see Name.wiredInNameTyThing_maybe
57   (E.g. listTyConName contains listTyCon. 
58
59 * The name cache is initialised with (the names of) all wired-in things
60
61 * The type checker sees if the Name is wired in before looking up 
62   the name in the type environment.  So the type envt itself contains
63   no wired in things.
64
65 * MkIface prunes out wired-in things before putting them in an interface file.
66   So interface files never contain wired-in things.
67
68
69 \begin{code}
70 wiredInThings :: [TyThing]
71 -- This list is used only to initialise HscMain.knownKeyNames
72 -- to ensure that when you say "Prelude.map" in your source code, you
73 -- get a Name with the correct known key
74 wiredInThings           
75   = concat
76     [           -- Wired in TyCons and their implicit Ids
77           tycon_things
78         , concatMap implicitTyThings tycon_things
79
80                 -- Wired in Ids
81         , map AnId wiredInIds
82
83                 -- PrimOps
84         , map (AnId . mkPrimOpId) allThePrimOps
85     ]
86   where
87     tycon_things = map ATyCon ([funTyCon] ++ primTyCons ++ wiredInTyCons)
88 \end{code}
89
90 We let a lot of "non-standard" values be visible, so that we can make
91 sense of them in interface pragmas. It's cool, though they all have
92 "non-standard" names, so they won't get past the parser in user code.
93
94 %************************************************************************
95 %*                                                                      *
96                 PrimOpIds
97 %*                                                                      *
98 %************************************************************************
99
100 \begin{code}
101 primOpIds :: Array Int Id       -- Indexed by PrimOp tag
102 primOpIds = array (1,maxPrimOpTag) [ (primOpTag op, mkPrimOpId op) 
103                                    | op <- allThePrimOps]
104
105 primOpId :: PrimOp -> Id
106 primOpId op = primOpIds ! primOpTag op
107 \end{code}
108
109
110 %************************************************************************
111 %*                                                                      *
112 \subsection{Export lists for pseudo-modules (GHC.Prim)}
113 %*                                                                      *
114 %************************************************************************
115
116 GHC.Prim "exports" all the primops and primitive types, some 
117 wired-in Ids.
118
119 \begin{code}
120 ghcPrimExports :: [RdrAvailInfo]
121 ghcPrimExports
122  = map (Avail . nameOccName . idName) ghcPrimIds ++
123    map (Avail . primOpOcc) allThePrimOps ++
124    [ AvailTC occ [occ] |
125      n <- funTyCon : primTyCons, let occ = nameOccName (tyConName n) 
126    ]
127 \end{code}
128
129
130 %************************************************************************
131 %*                                                                      *
132 \subsection{Built-in keys}
133 %*                                                                      *
134 %************************************************************************
135
136 ToDo: make it do the ``like'' part properly (as in 0.26 and before).
137
138 \begin{code}
139 maybeCharLikeCon, maybeIntLikeCon :: DataCon -> Bool
140 maybeCharLikeCon con = con `hasKey` charDataConKey
141 maybeIntLikeCon  con = con `hasKey` intDataConKey
142 \end{code}
143
144
145 %************************************************************************
146 %*                                                                      *
147 \subsection{Class predicates}
148 %*                                                                      *
149 %************************************************************************
150
151 \begin{code}
152 isNumericClass, isStandardClass :: Class -> Bool
153
154 isNumericClass     clas = classKey clas `is_elem` numericClassKeys
155 isStandardClass    clas = classKey clas `is_elem` standardClassKeys
156
157 is_elem :: Eq a => a -> [a] -> Bool
158 is_elem = isIn "is_X_Class"
159 \end{code}