2 % (c) The GRASP Project, Glasgow University, 1994-1998
4 \section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types}
6 This module is about types that can be defined in Haskell, but which
7 must be wired into the compiler nonetheless.
9 This module tracks the ``state interface'' document, ``GHC prelude:
10 types and operations.''
16 boolTy, boolTyCon, boolTyCon_RDR, boolTyConName,
17 trueDataCon, trueDataConId, true_RDR,
18 falseDataCon, falseDataConId, false_RDR,
20 charTyCon, charDataCon, charTyCon_RDR,
21 charTy, stringTy, charTyConName,
24 doubleTyCon, doubleDataCon, doubleTy, doubleTyConName,
26 floatTyCon, floatDataCon, floatTy, floatTyConName,
28 intTyCon, intDataCon, intTyCon_RDR, intDataCon_RDR, intTyConName,
31 listTyCon, nilDataCon, consDataCon,
32 listTyCon_RDR, consDataCon_RDR, listTyConName,
38 unitTyCon, unitDataCon, unitDataConId, pairTyCon,
39 unboxedSingletonTyCon, unboxedSingletonDataCon,
40 unboxedPairTyCon, unboxedPairDataCon,
42 boxedTupleArr, unboxedTupleArr,
48 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
49 parrTyCon_RDR, parrTyConName
52 #include "HsVersions.h"
54 import {-# SOURCE #-} MkId( mkDataConIds )
61 import Constants ( mAX_TUPLE_SIZE )
62 import Module ( Module )
63 import RdrName ( nameRdrName )
64 import Name ( Name, BuiltInSyntax(..), nameUnique, nameOccName,
65 nameModule, mkWiredInName )
66 import OccName ( mkOccNameFS, tcName, dataName, mkTupleOcc,
68 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
69 import Var ( TyVar, tyVarKind )
70 import TyCon ( TyCon, AlgTyConRhs(DataTyCon), tyConDataCons,
71 mkTupleTyCon, mkAlgTyCon, tyConName,
72 AlgTyConParent(NoParentTyCon) )
74 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed,
77 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
79 import Coercion ( unsafeCoercionTyCon, symCoercionTyCon,
80 transCoercionTyCon, leftCoercionTyCon,
81 rightCoercionTyCon, instCoercionTyCon )
82 import TypeRep ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
83 import Unique ( incrUnique, mkTupleTyConUnique,
84 mkTupleDataConUnique, mkPArrDataConUnique )
89 alpha_tyvar = [alphaTyVar]
94 %************************************************************************
96 \subsection{Wired in type constructors}
98 %************************************************************************
100 If you change which things are wired in, make sure you change their
101 names in PrelNames, so they use wTcQual, wDataQual, etc
104 wiredInTyCons :: [TyCon] -- Excludes tuples
105 -- This list is used only to define PrelInfo.wiredInThings
107 -- It does not need to include kind constructors, because
108 -- all that wiredInThings does is to initialise the Name table,
109 -- and kind constructors don't appear in source code.
111 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
112 -- it's defined in GHC.Base, and there's only
113 -- one of it. We put it in wiredInTyCons so
114 -- that it'll pre-populate the name cache, so
115 -- the special case in lookupOrigNameCache
116 -- doesn't need to look out for it
124 , unsafeCoercionTyCon
134 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
135 mkWiredInTyConName built_in mod fs uniq tycon
136 = mkWiredInName mod (mkOccNameFS tcName fs) uniq
137 (ATyCon tycon) -- Relevant TyCon
140 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
141 mkWiredInDataConName built_in mod fs uniq datacon
142 = mkWiredInName mod (mkOccNameFS dataName fs) uniq
143 (ADataCon datacon) -- Relevant DataCon
146 charTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Char") charTyConKey charTyCon
147 charDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("C#") charDataConKey charDataCon
148 intTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Int") intTyConKey intTyCon
149 intDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("I#") intDataConKey intDataCon
151 boolTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Bool") boolTyConKey boolTyCon
152 falseDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("False") falseDataConKey falseDataCon
153 trueDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("True") trueDataConKey trueDataCon
154 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_BASE FSLIT("[]") listTyConKey listTyCon
155 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE FSLIT("[]") nilDataConKey nilDataCon
156 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE FSLIT(":") consDataConKey consDataCon
158 floatTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT FSLIT("Float") floatTyConKey floatTyCon
159 floatDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT FSLIT("F#") floatDataConKey floatDataCon
160 doubleTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT FSLIT("Double") doubleTyConKey doubleTyCon
161 doubleDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT FSLIT("D#") doubleDataConKey doubleDataCon
163 parrTyConName = mkWiredInTyConName BuiltInSyntax gHC_PARR FSLIT("[::]") parrTyConKey parrTyCon
164 parrDataConName = mkWiredInDataConName UserSyntax gHC_PARR FSLIT("PArr") parrDataConKey parrDataCon
166 boolTyCon_RDR = nameRdrName boolTyConName
167 false_RDR = nameRdrName falseDataConName
168 true_RDR = nameRdrName trueDataConName
169 intTyCon_RDR = nameRdrName intTyConName
170 charTyCon_RDR = nameRdrName charTyConName
171 intDataCon_RDR = nameRdrName intDataConName
172 listTyCon_RDR = nameRdrName listTyConName
173 consDataCon_RDR = nameRdrName consDataConName
174 parrTyCon_RDR = nameRdrName parrTyConName
176 tySuperKindTyCon_RDR = nameRdrName tySuperKindTyConName
177 coSuperKindTyCon_RDR = nameRdrName coSuperKindTyConName
178 liftedTypeKindTyCon_RDR = nameRdrName liftedTypeKindTyConName
179 openTypeKindTyCon_RDR = nameRdrName openTypeKindTyConName
180 unliftedTypeKindTyCon_RDR = nameRdrName unliftedTypeKindTyConName
181 ubxTupleKindTyCon_RDR = nameRdrName ubxTupleKindTyConName
182 argTypeKindTyCon_RDR = nameRdrName argTypeKindTyConName
183 funKindTyCon_RDR = nameRdrName funKindTyConName
188 %************************************************************************
190 \subsection{mkWiredInTyCon}
192 %************************************************************************
195 pcNonRecDataTyCon = pcTyCon False NonRecursive
196 pcRecDataTyCon = pcTyCon False Recursive
198 pcTyCon is_enum is_rec name tyvars cons
201 tycon = mkAlgTyCon name
202 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
204 [] -- No stupid theta
205 (DataTyCon cons is_enum)
206 [] -- No record selectors
209 True -- All the wired-in tycons have generics
210 False -- Not in GADT syntax
212 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
213 pcDataCon = pcDataConWithFixity False
215 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
216 -- The Name should be in the DataName name space; it's the name
217 -- of the DataCon itself.
219 -- The unique is the first of two free uniques;
220 -- the first is used for the datacon itself,
221 -- the second is used for the "worker name"
223 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
226 data_con = mkDataCon dc_name declared_infix
227 (map (const NotMarkedStrict) arg_tys)
228 [] -- No labelled fields
230 [] -- No existential type variables
231 [] -- No equality spec
234 [] -- No stupid theta
235 (mkDataConIds bogus_wrap_name wrk_name data_con)
238 mod = nameModule dc_name
239 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
240 wrk_key = incrUnique (nameUnique dc_name)
241 wrk_name = mkWiredInName mod wrk_occ wrk_key
242 (AnId (dataConWorkId data_con)) UserSyntax
243 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
244 -- Wired-in types are too simple to need wrappers
248 %************************************************************************
250 \subsection[TysWiredIn-tuples]{The tuple types}
252 %************************************************************************
255 tupleTyCon :: Boxity -> Arity -> TyCon
256 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
257 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
258 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
260 tupleCon :: Boxity -> Arity -> DataCon
261 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
262 tupleCon Boxed i = snd (boxedTupleArr ! i)
263 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
265 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
266 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
267 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
269 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
270 mk_tuple boxity arity = (tycon, tuple_con)
272 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
273 mod = mkTupleModule boxity arity
274 tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
275 (ATyCon tycon) BuiltInSyntax
276 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
277 res_kind | isBoxed boxity = liftedTypeKind
278 | otherwise = ubxTupleKind
280 tyvars | isBoxed boxity = take arity alphaTyVars
281 | otherwise = take arity openAlphaTyVars
283 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
284 tyvar_tys = mkTyVarTys tyvars
285 dc_name = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
286 (ADataCon tuple_con) BuiltInSyntax
287 tc_uniq = mkTupleTyConUnique boxity arity
288 dc_uniq = mkTupleDataConUnique boxity arity
289 gen_info = True -- Tuples all have generics..
290 -- hmm: that's a *lot* of code
292 unitTyCon = tupleTyCon Boxed 0
293 unitDataCon = head (tyConDataCons unitTyCon)
294 unitDataConId = dataConWorkId unitDataCon
296 pairTyCon = tupleTyCon Boxed 2
298 unboxedSingletonTyCon = tupleTyCon Unboxed 1
299 unboxedSingletonDataCon = tupleCon Unboxed 1
301 unboxedPairTyCon = tupleTyCon Unboxed 2
302 unboxedPairDataCon = tupleCon Unboxed 2
305 %************************************************************************
307 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
309 %************************************************************************
312 charTy = mkTyConTy charTyCon
314 charTyCon = pcNonRecDataTyCon charTyConName [] [charDataCon]
315 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
317 stringTy = mkListTy charTy -- convenience only
321 intTy = mkTyConTy intTyCon
323 intTyCon = pcNonRecDataTyCon intTyConName [] [intDataCon]
324 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
328 floatTy = mkTyConTy floatTyCon
330 floatTyCon = pcNonRecDataTyCon floatTyConName [] [floatDataCon]
331 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
335 doubleTy = mkTyConTy doubleTyCon
337 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [doubleDataCon]
338 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
342 %************************************************************************
344 \subsection[TysWiredIn-Bool]{The @Bool@ type}
346 %************************************************************************
348 An ordinary enumeration type, but deeply wired in. There are no
349 magical operations on @Bool@ (just the regular Prelude code).
351 {\em BEGIN IDLE SPECULATION BY SIMON}
353 This is not the only way to encode @Bool@. A more obvious coding makes
354 @Bool@ just a boxed up version of @Bool#@, like this:
357 data Bool = MkBool Bool#
360 Unfortunately, this doesn't correspond to what the Report says @Bool@
361 looks like! Furthermore, we get slightly less efficient code (I
362 think) with this coding. @gtInt@ would look like this:
365 gtInt :: Int -> Int -> Bool
366 gtInt x y = case x of I# x# ->
368 case (gtIntPrim x# y#) of
372 Notice that the result of the @gtIntPrim@ comparison has to be turned
373 into an integer (here called @b#@), and returned in a @MkBool@ box.
375 The @if@ expression would compile to this:
378 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
381 I think this code is a little less efficient than the previous code,
382 but I'm not certain. At all events, corresponding with the Report is
383 important. The interesting thing is that the language is expressive
384 enough to describe more than one alternative; and that a type doesn't
385 necessarily need to be a straightforwardly boxed version of its
386 primitive counterpart.
388 {\em END IDLE SPECULATION BY SIMON}
391 boolTy = mkTyConTy boolTyCon
393 boolTyCon = pcTyCon True NonRecursive boolTyConName
394 [] [falseDataCon, trueDataCon]
396 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
397 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
399 falseDataConId = dataConWorkId falseDataCon
400 trueDataConId = dataConWorkId trueDataCon
403 %************************************************************************
405 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
407 %************************************************************************
409 Special syntax, deeply wired in, but otherwise an ordinary algebraic
412 data [] a = [] | a : (List a)
414 data (,) a b = (,,) a b
419 mkListTy :: Type -> Type
420 mkListTy ty = mkTyConApp listTyCon [ty]
422 listTyCon = pcRecDataTyCon listTyConName alpha_tyvar [nilDataCon, consDataCon]
424 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
425 consDataCon = pcDataConWithFixity True {- Declared infix -}
427 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
428 -- Interesting: polymorphic recursion would help here.
429 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
430 -- gets the over-specific type (Type -> Type)
433 %************************************************************************
435 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
437 %************************************************************************
439 The tuple types are definitely magic, because they form an infinite
444 They have a special family of type constructors, of type @TyCon@
445 These contain the tycon arity, but don't require a Unique.
448 They have a special family of constructors, of type
449 @Id@. Again these contain their arity but don't need a Unique.
452 There should be a magic way of generating the info tables and
453 entry code for all tuples.
455 But at the moment we just compile a Haskell source
456 file\srcloc{lib/prelude/...} containing declarations like:
459 data Tuple2 a b = Tup2 a b
460 data Tuple3 a b c = Tup3 a b c
461 data Tuple4 a b c d = Tup4 a b c d
464 The print-names associated with the magic @Id@s for tuple constructors
465 ``just happen'' to be the same as those generated by these
469 The instance environment should have a magic way to know
470 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
471 so on. \ToDo{Not implemented yet.}
474 There should also be a way to generate the appropriate code for each
475 of these instances, but (like the info tables and entry code) it is
476 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
480 mkTupleTy :: Boxity -> Int -> [Type] -> Type
481 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
483 unitTy = mkTupleTy Boxed 0 []
486 %************************************************************************
488 \subsection[TysWiredIn-PArr]{The @[::]@ type}
490 %************************************************************************
492 Special syntax for parallel arrays needs some wired in definitions.
495 -- construct a type representing the application of the parallel array
498 mkPArrTy :: Type -> Type
499 mkPArrTy ty = mkTyConApp parrTyCon [ty]
501 -- represents the type constructor of parallel arrays
503 -- * this must match the definition in `PrelPArr'
505 -- NB: Although the constructor is given here, it will not be accessible in
506 -- user code as it is not in the environment of any compiled module except
510 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [parrDataCon]
512 parrDataCon :: DataCon
513 parrDataCon = pcDataCon
515 alpha_tyvar -- forall'ed type variables
516 [intPrimTy, -- 1st argument: Int#
517 mkTyConApp -- 2nd argument: Array# a
522 -- check whether a type constructor is the constructor for parallel arrays
524 isPArrTyCon :: TyCon -> Bool
525 isPArrTyCon tc = tyConName tc == parrTyConName
527 -- fake array constructors
529 -- * these constructors are never really used to represent array values;
530 -- however, they are very convenient during desugaring (and, in particular,
531 -- in the pattern matching compiler) to treat array pattern just like
532 -- yet another constructor pattern
534 parrFakeCon :: Arity -> DataCon
535 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
536 parrFakeCon i = parrFakeConArr!i
538 -- pre-defined set of constructors
540 parrFakeConArr :: Array Int DataCon
541 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
542 | i <- [0..mAX_TUPLE_SIZE]]
544 -- build a fake parallel array constructor for the given arity
546 mkPArrFakeCon :: Int -> DataCon
547 mkPArrFakeCon arity = data_con
549 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
550 tyvar = head alphaTyVars
551 tyvarTys = replicate arity $ mkTyVarTy tyvar
552 nameStr = mkFastString ("MkPArr" ++ show arity)
553 name = mkWiredInName gHC_PARR (mkOccNameFS dataName nameStr) uniq
554 (ADataCon data_con) UserSyntax
555 uniq = mkPArrDataConUnique arity
557 -- checks whether a data constructor is a fake constructor for parallel arrays
559 isPArrFakeCon :: DataCon -> Bool
560 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)