2 % (c) The GRASP Project, Glasgow University, 1994-1998
4 \section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types}
7 -- | This module is about types that can be defined in Haskell, but which
8 -- must be wired into the compiler nonetheless. C.f module TysPrim
10 -- * All wired in things
14 boolTy, boolTyCon, boolTyCon_RDR, boolTyConName,
15 trueDataCon, trueDataConId, true_RDR,
16 falseDataCon, falseDataConId, false_RDR,
19 charTyCon, charDataCon, charTyCon_RDR,
20 charTy, stringTy, charTyConName,
23 doubleTyCon, doubleDataCon, doubleTy, doubleTyConName,
26 floatTyCon, floatDataCon, floatTy, floatTyConName,
29 intTyCon, intDataCon, intTyCon_RDR, intDataCon_RDR, intTyConName,
33 wordTyCon, wordDataCon, wordTyConName, wordTy,
36 listTyCon, nilDataCon, consDataCon,
37 listTyCon_RDR, consDataCon_RDR, listTyConName,
41 mkTupleTy, mkBoxedTupleTy,
43 unitTyCon, unitDataCon, unitDataConId, pairTyCon,
44 unboxedSingletonTyCon, unboxedSingletonDataCon,
45 unboxedPairTyCon, unboxedPairDataCon,
50 -- * Heterogeneous Metaprogramming
52 hetMetCodeTypeTyConName,
53 hetMetCodeTypeTyCon, isHetMetCodeTypeTyCon,
54 hetMetCodeTypeTyCon_RDR,
58 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
59 parrTyCon_RDR, parrTyConName
62 #include "HsVersions.h"
64 import {-# SOURCE #-} MkId( mkDataConIds )
71 import Constants ( mAX_TUPLE_SIZE )
72 import Module ( Module )
75 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
77 import TyCon ( TyCon, AlgTyConRhs(DataTyCon), tyConDataCons,
78 mkTupleTyCon, mkAlgTyCon, tyConName,
79 TyConParent(NoParentTyCon) )
81 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed, HsBang(..) )
83 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
85 import Coercion ( unsafeCoercionTyCon, symCoercionTyCon,
86 transCoercionTyCon, leftCoercionTyCon,
87 rightCoercionTyCon, instCoercionTyCon )
88 import TypeRep ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
89 import Unique ( incrUnique, mkTupleTyConUnique,
90 mkTupleDataConUnique, mkPArrDataConUnique )
95 alpha_tyvar :: [TyVar]
96 alpha_tyvar = [alphaTyVar]
103 %************************************************************************
105 \subsection{Wired in type constructors}
107 %************************************************************************
109 If you change which things are wired in, make sure you change their
110 names in PrelNames, so they use wTcQual, wDataQual, etc
113 wiredInTyCons :: [TyCon] -- Excludes tuples
114 -- This list is used only to define PrelInfo.wiredInThings
116 -- It does not need to include kind constructors, because
117 -- all that wiredInThings does is to initialise the Name table,
118 -- and kind constructors don't appear in source code.
120 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
121 -- it's defined in GHC.Base, and there's only
122 -- one of it. We put it in wiredInTyCons so
123 -- that it'll pre-populate the name cache, so
124 -- the special case in lookupOrigNameCache
125 -- doesn't need to look out for it
133 , hetMetCodeTypeTyCon
134 , unsafeCoercionTyCon
144 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
145 mkWiredInTyConName built_in modu fs unique tycon
146 = mkWiredInName modu (mkTcOccFS fs) unique
147 (ATyCon tycon) -- Relevant TyCon
150 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
151 mkWiredInDataConName built_in modu fs unique datacon
152 = mkWiredInName modu (mkDataOccFS fs) unique
153 (ADataCon datacon) -- Relevant DataCon
156 charTyConName, charDataConName, intTyConName, intDataConName :: Name
157 charTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Char") charTyConKey charTyCon
158 charDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "C#") charDataConKey charDataCon
159 intTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Int") intTyConKey intTyCon
160 intDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "I#") intDataConKey intDataCon
162 boolTyConName, falseDataConName, trueDataConName :: Name
163 boolTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Bool") boolTyConKey boolTyCon
164 falseDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "False") falseDataConKey falseDataCon
165 trueDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "True") trueDataConKey trueDataCon
167 listTyConName, nilDataConName, consDataConName :: Name
168 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "[]") listTyConKey listTyCon
169 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit "[]") nilDataConKey nilDataCon
170 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit ":") consDataConKey consDataCon
172 floatTyConName, floatDataConName, doubleTyConName, doubleDataConName :: Name
173 floatTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Float") floatTyConKey floatTyCon
174 floatDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "F#") floatDataConKey floatDataCon
175 doubleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Double") doubleTyConKey doubleTyCon
176 doubleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "D#") doubleDataConKey doubleDataCon
178 parrTyConName, parrDataConName :: Name
179 parrTyConName = mkWiredInTyConName BuiltInSyntax
180 gHC_PARR' (fsLit "[::]") parrTyConKey parrTyCon
181 parrDataConName = mkWiredInDataConName UserSyntax
182 gHC_PARR' (fsLit "PArr") parrDataConKey parrDataCon
184 hetMetCodeTypeTyConName :: Name
185 hetMetCodeTypeTyConName = mkWiredInTyConName BuiltInSyntax gHC_HETMET_CODETYPES (fsLit "<[]>@") hetMetCodeTypeTyConKey hetMetCodeTypeTyCon
186 hetMetCodeTypeDataConName :: Name
187 hetMetCodeTypeDataConName =
188 mkWiredInDataConName BuiltInSyntax gHC_HETMET_CODETYPES (fsLit "<[]>") hetMetCodeTypeDataConKey hetMetCodeTypeDataCon
190 boolTyCon_RDR, false_RDR, true_RDR, intTyCon_RDR, charTyCon_RDR,
191 intDataCon_RDR, listTyCon_RDR, consDataCon_RDR, parrTyCon_RDR, hetMetCodeTypeTyCon_RDR :: RdrName
192 boolTyCon_RDR = nameRdrName boolTyConName
193 false_RDR = nameRdrName falseDataConName
194 true_RDR = nameRdrName trueDataConName
195 intTyCon_RDR = nameRdrName intTyConName
196 charTyCon_RDR = nameRdrName charTyConName
197 intDataCon_RDR = nameRdrName intDataConName
198 listTyCon_RDR = nameRdrName listTyConName
199 consDataCon_RDR = nameRdrName consDataConName
200 parrTyCon_RDR = nameRdrName parrTyConName
201 hetMetCodeTypeTyCon_RDR = nameRdrName hetMetCodeTypeTyConName
205 %************************************************************************
207 \subsection{mkWiredInTyCon}
209 %************************************************************************
212 pcNonRecDataTyCon :: Name -> [TyVar] -> [DataCon] -> TyCon
213 pcNonRecDataTyCon = pcTyCon False NonRecursive
214 pcRecDataTyCon :: Name -> [TyVar] -> [DataCon] -> TyCon
215 pcRecDataTyCon = pcTyCon False Recursive
217 pcTyCon :: Bool -> RecFlag -> Name -> [TyVar] -> [DataCon] -> TyCon
218 pcTyCon is_enum is_rec name tyvars cons
221 tycon = mkAlgTyCon name
222 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
224 [] -- No stupid theta
225 (DataTyCon cons is_enum)
228 True -- All the wired-in tycons have generics
229 False -- Not in GADT syntax
231 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
232 pcDataCon = pcDataConWithFixity False
234 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
235 -- The Name should be in the DataName name space; it's the name
236 -- of the DataCon itself.
238 -- The unique is the first of two free uniques;
239 -- the first is used for the datacon itself,
240 -- the second is used for the "worker name"
242 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
245 data_con = mkDataCon dc_name declared_infix
246 (map (const HsNoBang) arg_tys)
247 [] -- No labelled fields
249 [] -- No existential type variables
250 [] -- No equality spec
252 arg_tys (mkTyConApp tycon (mkTyVarTys tyvars))
254 [] -- No stupid theta
255 (mkDataConIds bogus_wrap_name wrk_name data_con)
258 modu = ASSERT( isExternalName dc_name )
260 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
261 wrk_key = incrUnique (nameUnique dc_name)
262 wrk_name = mkWiredInName modu wrk_occ wrk_key
263 (AnId (dataConWorkId data_con)) UserSyntax
264 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
265 -- Wired-in types are too simple to need wrappers
269 %************************************************************************
271 \subsection[TysWiredIn-tuples]{The tuple types}
273 %************************************************************************
276 tupleTyCon :: Boxity -> Arity -> TyCon
277 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
278 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
279 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
281 tupleCon :: Boxity -> Arity -> DataCon
282 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
283 tupleCon Boxed i = snd (boxedTupleArr ! i)
284 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
286 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
287 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
288 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
290 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
291 mk_tuple boxity arity = (tycon, tuple_con)
293 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
294 modu = mkTupleModule boxity arity
295 tc_name = mkWiredInName modu (mkTupleOcc tcName boxity arity) tc_uniq
296 (ATyCon tycon) BuiltInSyntax
297 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
298 res_kind | isBoxed boxity = liftedTypeKind
299 | otherwise = ubxTupleKind
301 tyvars | isBoxed boxity = take arity alphaTyVars
302 | otherwise = take arity openAlphaTyVars
304 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
305 tyvar_tys = mkTyVarTys tyvars
306 dc_name = mkWiredInName modu (mkTupleOcc dataName boxity arity) dc_uniq
307 (ADataCon tuple_con) BuiltInSyntax
308 tc_uniq = mkTupleTyConUnique boxity arity
309 dc_uniq = mkTupleDataConUnique boxity arity
310 gen_info = True -- Tuples all have generics..
311 -- hmm: that's a *lot* of code
314 unitTyCon = tupleTyCon Boxed 0
315 unitDataCon :: DataCon
316 unitDataCon = head (tyConDataCons unitTyCon)
318 unitDataConId = dataConWorkId unitDataCon
321 pairTyCon = tupleTyCon Boxed 2
323 unboxedSingletonTyCon :: TyCon
324 unboxedSingletonTyCon = tupleTyCon Unboxed 1
325 unboxedSingletonDataCon :: DataCon
326 unboxedSingletonDataCon = tupleCon Unboxed 1
328 unboxedPairTyCon :: TyCon
329 unboxedPairTyCon = tupleTyCon Unboxed 2
330 unboxedPairDataCon :: DataCon
331 unboxedPairDataCon = tupleCon Unboxed 2
335 %************************************************************************
337 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
339 %************************************************************************
343 charTy = mkTyConTy charTyCon
346 charTyCon = pcNonRecDataTyCon charTyConName [] [charDataCon]
347 charDataCon :: DataCon
348 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
351 stringTy = mkListTy charTy -- convenience only
356 intTy = mkTyConTy intTyCon
359 intTyCon = pcNonRecDataTyCon intTyConName [] [intDataCon]
360 intDataCon :: DataCon
361 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
366 wordTy = mkTyConTy wordTyCon
369 wordTyCon = pcNonRecDataTyCon wordTyConName [] [wordDataCon]
370 wordDataCon :: DataCon
371 wordDataCon = pcDataCon wordDataConName [] [wordPrimTy] wordTyCon
376 floatTy = mkTyConTy floatTyCon
379 floatTyCon = pcNonRecDataTyCon floatTyConName [] [floatDataCon]
380 floatDataCon :: DataCon
381 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
386 doubleTy = mkTyConTy doubleTyCon
389 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [doubleDataCon]
391 doubleDataCon :: DataCon
392 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
396 %************************************************************************
398 \subsection[TysWiredIn-Bool]{The @Bool@ type}
400 %************************************************************************
402 An ordinary enumeration type, but deeply wired in. There are no
403 magical operations on @Bool@ (just the regular Prelude code).
405 {\em BEGIN IDLE SPECULATION BY SIMON}
407 This is not the only way to encode @Bool@. A more obvious coding makes
408 @Bool@ just a boxed up version of @Bool#@, like this:
411 data Bool = MkBool Bool#
414 Unfortunately, this doesn't correspond to what the Report says @Bool@
415 looks like! Furthermore, we get slightly less efficient code (I
416 think) with this coding. @gtInt@ would look like this:
419 gtInt :: Int -> Int -> Bool
420 gtInt x y = case x of I# x# ->
422 case (gtIntPrim x# y#) of
426 Notice that the result of the @gtIntPrim@ comparison has to be turned
427 into an integer (here called @b#@), and returned in a @MkBool@ box.
429 The @if@ expression would compile to this:
432 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
435 I think this code is a little less efficient than the previous code,
436 but I'm not certain. At all events, corresponding with the Report is
437 important. The interesting thing is that the language is expressive
438 enough to describe more than one alternative; and that a type doesn't
439 necessarily need to be a straightforwardly boxed version of its
440 primitive counterpart.
442 {\em END IDLE SPECULATION BY SIMON}
446 boolTy = mkTyConTy boolTyCon
449 boolTyCon = pcTyCon True NonRecursive boolTyConName
450 [] [falseDataCon, trueDataCon]
452 falseDataCon, trueDataCon :: DataCon
453 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
454 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
456 falseDataConId, trueDataConId :: Id
457 falseDataConId = dataConWorkId falseDataCon
458 trueDataConId = dataConWorkId trueDataCon
461 %************************************************************************
463 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
465 %************************************************************************
467 Special syntax, deeply wired in, but otherwise an ordinary algebraic
470 data [] a = [] | a : (List a)
472 data (,) a b = (,,) a b
477 mkListTy :: Type -> Type
478 mkListTy ty = mkTyConApp listTyCon [ty]
481 listTyCon = pcRecDataTyCon listTyConName alpha_tyvar [nilDataCon, consDataCon]
483 nilDataCon :: DataCon
484 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
486 consDataCon :: DataCon
487 consDataCon = pcDataConWithFixity True {- Declared infix -}
489 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
490 -- Interesting: polymorphic recursion would help here.
491 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
492 -- gets the over-specific type (Type -> Type)
495 %************************************************************************
497 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
499 %************************************************************************
501 The tuple types are definitely magic, because they form an infinite
506 They have a special family of type constructors, of type @TyCon@
507 These contain the tycon arity, but don't require a Unique.
510 They have a special family of constructors, of type
511 @Id@. Again these contain their arity but don't need a Unique.
514 There should be a magic way of generating the info tables and
515 entry code for all tuples.
517 But at the moment we just compile a Haskell source
518 file\srcloc{lib/prelude/...} containing declarations like:
521 data Tuple2 a b = Tup2 a b
522 data Tuple3 a b c = Tup3 a b c
523 data Tuple4 a b c d = Tup4 a b c d
526 The print-names associated with the magic @Id@s for tuple constructors
527 ``just happen'' to be the same as those generated by these
531 The instance environment should have a magic way to know
532 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
533 so on. \ToDo{Not implemented yet.}
536 There should also be a way to generate the appropriate code for each
537 of these instances, but (like the info tables and entry code) it is
538 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
542 mkTupleTy :: Boxity -> [Type] -> Type
543 -- Special case for *boxed* 1-tuples, which are represented by the type itself
544 mkTupleTy boxity [ty] | Boxed <- boxity = ty
545 mkTupleTy boxity tys = mkTyConApp (tupleTyCon boxity (length tys)) tys
547 -- | Build the type of a small tuple that holds the specified type of thing
548 mkBoxedTupleTy :: [Type] -> Type
549 mkBoxedTupleTy tys = mkTupleTy Boxed tys
552 unitTy = mkTupleTy Boxed []
555 %************************************************************************
557 \subsection[TysWiredIn-PArr]{The @[::]@ type}
559 %************************************************************************
561 Special syntax for parallel arrays needs some wired in definitions.
564 -- | Construct a type representing the application of the parallel array constructor
565 mkPArrTy :: Type -> Type
566 mkPArrTy ty = mkTyConApp parrTyCon [ty]
568 -- | Represents the type constructor of parallel arrays
570 -- * This must match the definition in @PrelPArr@
572 -- NB: Although the constructor is given here, it will not be accessible in
573 -- user code as it is not in the environment of any compiled module except
577 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [parrDataCon]
579 parrDataCon :: DataCon
580 parrDataCon = pcDataCon
582 alpha_tyvar -- forall'ed type variables
583 [intPrimTy, -- 1st argument: Int#
584 mkTyConApp -- 2nd argument: Array# a
589 -- | Check whether a type constructor is the constructor for parallel arrays
590 isPArrTyCon :: TyCon -> Bool
591 isPArrTyCon tc = tyConName tc == parrTyConName
593 -- | Fake array constructors
595 -- * These constructors are never really used to represent array values;
596 -- however, they are very convenient during desugaring (and, in particular,
597 -- in the pattern matching compiler) to treat array pattern just like
598 -- yet another constructor pattern
600 parrFakeCon :: Arity -> DataCon
601 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
602 parrFakeCon i = parrFakeConArr!i
604 -- pre-defined set of constructors
606 parrFakeConArr :: Array Int DataCon
607 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
608 | i <- [0..mAX_TUPLE_SIZE]]
610 -- build a fake parallel array constructor for the given arity
612 mkPArrFakeCon :: Int -> DataCon
613 mkPArrFakeCon arity = data_con
615 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
616 tyvar = head alphaTyVars
617 tyvarTys = replicate arity $ mkTyVarTy tyvar
618 nameStr = mkFastString ("MkPArr" ++ show arity)
619 name = mkWiredInName gHC_PARR' (mkDataOccFS nameStr) unique
620 (ADataCon data_con) UserSyntax
621 unique = mkPArrDataConUnique arity
623 -- | Checks whether a data constructor is a fake constructor for parallel arrays
624 isPArrFakeCon :: DataCon -> Bool
625 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)
629 Heterogeneous Metaprogramming
632 -- | Construct a type representing the application of the box type
633 mkHetMetCodeTypeTy :: TyVar -> Type -> Type
634 mkHetMetCodeTypeTy ecn ty = mkTyConApp hetMetCodeTypeTyCon [(mkTyVarTy ecn), ty]
636 -- | Represents the type constructor of box types
637 hetMetCodeTypeTyCon :: TyCon
638 hetMetCodeTypeTyCon = pcNonRecDataTyCon hetMetCodeTypeTyConName [alphaTyVar, betaTyVar] [hetMetCodeTypeDataCon]
640 -- | Check whether a type constructor is the constructor for box types
641 isHetMetCodeTypeTyCon :: TyCon -> Bool
642 isHetMetCodeTypeTyCon tc = tyConName tc == hetMetCodeTypeTyConName
644 hetMetCodeTypeDataCon :: DataCon
645 hetMetCodeTypeDataCon = pcDataCon
646 hetMetCodeTypeDataConName
647 [betaTyVar] -- forall'ed type variables