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