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,
52 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
53 parrTyCon_RDR, parrTyConName
56 #include "HsVersions.h"
58 import {-# SOURCE #-} MkId( mkDataConIds )
65 import Constants ( mAX_TUPLE_SIZE )
66 import Module ( Module )
69 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
71 import TyCon ( TyCon, AlgTyConRhs(DataTyCon), tyConDataCons,
72 mkTupleTyCon, mkAlgTyCon, tyConName,
73 TyConParent(NoParentTyCon) )
75 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed,
78 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
80 import Coercion ( unsafeCoercionTyCon, symCoercionTyCon,
81 transCoercionTyCon, leftCoercionTyCon,
82 rightCoercionTyCon, instCoercionTyCon )
83 import TypeRep ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
84 import Unique ( incrUnique, mkTupleTyConUnique,
85 mkTupleDataConUnique, mkPArrDataConUnique )
90 alpha_tyvar :: [TyVar]
91 alpha_tyvar = [alphaTyVar]
98 %************************************************************************
100 \subsection{Wired in type constructors}
102 %************************************************************************
104 If you change which things are wired in, make sure you change their
105 names in PrelNames, so they use wTcQual, wDataQual, etc
108 wiredInTyCons :: [TyCon] -- Excludes tuples
109 -- This list is used only to define PrelInfo.wiredInThings
111 -- It does not need to include kind constructors, because
112 -- all that wiredInThings does is to initialise the Name table,
113 -- and kind constructors don't appear in source code.
115 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
116 -- it's defined in GHC.Base, and there's only
117 -- one of it. We put it in wiredInTyCons so
118 -- that it'll pre-populate the name cache, so
119 -- the special case in lookupOrigNameCache
120 -- doesn't need to look out for it
128 , unsafeCoercionTyCon
138 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
139 mkWiredInTyConName built_in modu fs unique tycon
140 = mkWiredInName modu (mkTcOccFS fs) unique
141 (ATyCon tycon) -- Relevant TyCon
144 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
145 mkWiredInDataConName built_in modu fs unique datacon
146 = mkWiredInName modu (mkDataOccFS fs) unique
147 (ADataCon datacon) -- Relevant DataCon
150 charTyConName, charDataConName, intTyConName, intDataConName :: Name
151 charTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Char") charTyConKey charTyCon
152 charDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "C#") charDataConKey charDataCon
153 intTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Int") intTyConKey intTyCon
154 intDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "I#") intDataConKey intDataCon
156 boolTyConName, falseDataConName, trueDataConName :: Name
157 boolTyConName = mkWiredInTyConName UserSyntax gHC_BOOL (fsLit "Bool") boolTyConKey boolTyCon
158 falseDataConName = mkWiredInDataConName UserSyntax gHC_BOOL (fsLit "False") falseDataConKey falseDataCon
159 trueDataConName = mkWiredInDataConName UserSyntax gHC_BOOL (fsLit "True") trueDataConKey trueDataCon
161 listTyConName, nilDataConName, consDataConName :: Name
162 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "[]") listTyConKey listTyCon
163 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit "[]") nilDataConKey nilDataCon
164 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit ":") consDataConKey consDataCon
166 floatTyConName, floatDataConName, doubleTyConName, doubleDataConName :: Name
167 floatTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Float") floatTyConKey floatTyCon
168 floatDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "F#") floatDataConKey floatDataCon
169 doubleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Double") doubleTyConKey doubleTyCon
170 doubleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "D#") doubleDataConKey doubleDataCon
172 parrTyConName, parrDataConName :: Name
173 parrTyConName = mkWiredInTyConName BuiltInSyntax gHC_PARR (fsLit "[::]") parrTyConKey parrTyCon
174 parrDataConName = mkWiredInDataConName UserSyntax gHC_PARR (fsLit "PArr") parrDataConKey parrDataCon
176 boolTyCon_RDR, false_RDR, true_RDR, intTyCon_RDR, charTyCon_RDR,
177 intDataCon_RDR, listTyCon_RDR, consDataCon_RDR, parrTyCon_RDR:: RdrName
178 boolTyCon_RDR = nameRdrName boolTyConName
179 false_RDR = nameRdrName falseDataConName
180 true_RDR = nameRdrName trueDataConName
181 intTyCon_RDR = nameRdrName intTyConName
182 charTyCon_RDR = nameRdrName charTyConName
183 intDataCon_RDR = nameRdrName intDataConName
184 listTyCon_RDR = nameRdrName listTyConName
185 consDataCon_RDR = nameRdrName consDataConName
186 parrTyCon_RDR = nameRdrName parrTyConName
188 tySuperKindTyCon_RDR = nameRdrName tySuperKindTyConName
189 coSuperKindTyCon_RDR = nameRdrName coSuperKindTyConName
190 liftedTypeKindTyCon_RDR = nameRdrName liftedTypeKindTyConName
191 openTypeKindTyCon_RDR = nameRdrName openTypeKindTyConName
192 unliftedTypeKindTyCon_RDR = nameRdrName unliftedTypeKindTyConName
193 ubxTupleKindTyCon_RDR = nameRdrName ubxTupleKindTyConName
194 argTypeKindTyCon_RDR = nameRdrName argTypeKindTyConName
195 funKindTyCon_RDR = nameRdrName funKindTyConName
200 %************************************************************************
202 \subsection{mkWiredInTyCon}
204 %************************************************************************
207 pcNonRecDataTyCon :: Name -> [TyVar] -> [DataCon] -> TyCon
208 pcNonRecDataTyCon = pcTyCon False NonRecursive
209 pcRecDataTyCon :: Name -> [TyVar] -> [DataCon] -> TyCon
210 pcRecDataTyCon = pcTyCon False Recursive
212 pcTyCon :: Bool -> RecFlag -> Name -> [TyVar] -> [DataCon] -> TyCon
213 pcTyCon is_enum is_rec name tyvars cons
216 tycon = mkAlgTyCon name
217 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
219 [] -- No stupid theta
220 (DataTyCon cons is_enum)
223 True -- All the wired-in tycons have generics
224 False -- Not in GADT syntax
226 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
227 pcDataCon = pcDataConWithFixity False
229 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
230 -- The Name should be in the DataName name space; it's the name
231 -- of the DataCon itself.
233 -- The unique is the first of two free uniques;
234 -- the first is used for the datacon itself,
235 -- the second is used for the "worker name"
237 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
240 data_con = mkDataCon dc_name declared_infix
241 (map (const NotMarkedStrict) arg_tys)
242 [] -- No labelled fields
244 [] -- No existential type variables
245 [] -- No equality spec
247 arg_tys (mkTyConApp tycon (mkTyVarTys tyvars))
249 [] -- No stupid theta
250 (mkDataConIds bogus_wrap_name wrk_name data_con)
253 modu = ASSERT( isExternalName dc_name )
255 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
256 wrk_key = incrUnique (nameUnique dc_name)
257 wrk_name = mkWiredInName modu wrk_occ wrk_key
258 (AnId (dataConWorkId data_con)) UserSyntax
259 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
260 -- Wired-in types are too simple to need wrappers
264 %************************************************************************
266 \subsection[TysWiredIn-tuples]{The tuple types}
268 %************************************************************************
271 tupleTyCon :: Boxity -> Arity -> TyCon
272 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
273 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
274 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
276 tupleCon :: Boxity -> Arity -> DataCon
277 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
278 tupleCon Boxed i = snd (boxedTupleArr ! i)
279 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
281 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
282 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
283 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
285 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
286 mk_tuple boxity arity = (tycon, tuple_con)
288 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
289 modu = mkTupleModule boxity arity
290 tc_name = mkWiredInName modu (mkTupleOcc tcName boxity arity) tc_uniq
291 (ATyCon tycon) BuiltInSyntax
292 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
293 res_kind | isBoxed boxity = liftedTypeKind
294 | otherwise = ubxTupleKind
296 tyvars | isBoxed boxity = take arity alphaTyVars
297 | otherwise = take arity openAlphaTyVars
299 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
300 tyvar_tys = mkTyVarTys tyvars
301 dc_name = mkWiredInName modu (mkTupleOcc dataName boxity arity) dc_uniq
302 (ADataCon tuple_con) BuiltInSyntax
303 tc_uniq = mkTupleTyConUnique boxity arity
304 dc_uniq = mkTupleDataConUnique boxity arity
305 gen_info = True -- Tuples all have generics..
306 -- hmm: that's a *lot* of code
309 unitTyCon = tupleTyCon Boxed 0
310 unitDataCon :: DataCon
311 unitDataCon = head (tyConDataCons unitTyCon)
313 unitDataConId = dataConWorkId unitDataCon
316 pairTyCon = tupleTyCon Boxed 2
318 unboxedSingletonTyCon :: TyCon
319 unboxedSingletonTyCon = tupleTyCon Unboxed 1
320 unboxedSingletonDataCon :: DataCon
321 unboxedSingletonDataCon = tupleCon Unboxed 1
323 unboxedPairTyCon :: TyCon
324 unboxedPairTyCon = tupleTyCon Unboxed 2
325 unboxedPairDataCon :: DataCon
326 unboxedPairDataCon = tupleCon Unboxed 2
330 %************************************************************************
332 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
334 %************************************************************************
338 charTy = mkTyConTy charTyCon
341 charTyCon = pcNonRecDataTyCon charTyConName [] [charDataCon]
342 charDataCon :: DataCon
343 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
346 stringTy = mkListTy charTy -- convenience only
351 intTy = mkTyConTy intTyCon
354 intTyCon = pcNonRecDataTyCon intTyConName [] [intDataCon]
355 intDataCon :: DataCon
356 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
361 wordTy = mkTyConTy wordTyCon
364 wordTyCon = pcNonRecDataTyCon wordTyConName [] [wordDataCon]
365 wordDataCon :: DataCon
366 wordDataCon = pcDataCon wordDataConName [] [wordPrimTy] wordTyCon
371 floatTy = mkTyConTy floatTyCon
374 floatTyCon = pcNonRecDataTyCon floatTyConName [] [floatDataCon]
375 floatDataCon :: DataCon
376 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
381 doubleTy = mkTyConTy doubleTyCon
384 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [doubleDataCon]
386 doubleDataCon :: DataCon
387 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
391 %************************************************************************
393 \subsection[TysWiredIn-Bool]{The @Bool@ type}
395 %************************************************************************
397 An ordinary enumeration type, but deeply wired in. There are no
398 magical operations on @Bool@ (just the regular Prelude code).
400 {\em BEGIN IDLE SPECULATION BY SIMON}
402 This is not the only way to encode @Bool@. A more obvious coding makes
403 @Bool@ just a boxed up version of @Bool#@, like this:
406 data Bool = MkBool Bool#
409 Unfortunately, this doesn't correspond to what the Report says @Bool@
410 looks like! Furthermore, we get slightly less efficient code (I
411 think) with this coding. @gtInt@ would look like this:
414 gtInt :: Int -> Int -> Bool
415 gtInt x y = case x of I# x# ->
417 case (gtIntPrim x# y#) of
421 Notice that the result of the @gtIntPrim@ comparison has to be turned
422 into an integer (here called @b#@), and returned in a @MkBool@ box.
424 The @if@ expression would compile to this:
427 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
430 I think this code is a little less efficient than the previous code,
431 but I'm not certain. At all events, corresponding with the Report is
432 important. The interesting thing is that the language is expressive
433 enough to describe more than one alternative; and that a type doesn't
434 necessarily need to be a straightforwardly boxed version of its
435 primitive counterpart.
437 {\em END IDLE SPECULATION BY SIMON}
441 boolTy = mkTyConTy boolTyCon
444 boolTyCon = pcTyCon True NonRecursive boolTyConName
445 [] [falseDataCon, trueDataCon]
447 falseDataCon, trueDataCon :: DataCon
448 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
449 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
451 falseDataConId, trueDataConId :: Id
452 falseDataConId = dataConWorkId falseDataCon
453 trueDataConId = dataConWorkId trueDataCon
456 %************************************************************************
458 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
460 %************************************************************************
462 Special syntax, deeply wired in, but otherwise an ordinary algebraic
465 data [] a = [] | a : (List a)
467 data (,) a b = (,,) a b
472 mkListTy :: Type -> Type
473 mkListTy ty = mkTyConApp listTyCon [ty]
476 listTyCon = pcRecDataTyCon listTyConName alpha_tyvar [nilDataCon, consDataCon]
478 nilDataCon :: DataCon
479 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
481 consDataCon :: DataCon
482 consDataCon = pcDataConWithFixity True {- Declared infix -}
484 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
485 -- Interesting: polymorphic recursion would help here.
486 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
487 -- gets the over-specific type (Type -> Type)
490 %************************************************************************
492 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
494 %************************************************************************
496 The tuple types are definitely magic, because they form an infinite
501 They have a special family of type constructors, of type @TyCon@
502 These contain the tycon arity, but don't require a Unique.
505 They have a special family of constructors, of type
506 @Id@. Again these contain their arity but don't need a Unique.
509 There should be a magic way of generating the info tables and
510 entry code for all tuples.
512 But at the moment we just compile a Haskell source
513 file\srcloc{lib/prelude/...} containing declarations like:
516 data Tuple2 a b = Tup2 a b
517 data Tuple3 a b c = Tup3 a b c
518 data Tuple4 a b c d = Tup4 a b c d
521 The print-names associated with the magic @Id@s for tuple constructors
522 ``just happen'' to be the same as those generated by these
526 The instance environment should have a magic way to know
527 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
528 so on. \ToDo{Not implemented yet.}
531 There should also be a way to generate the appropriate code for each
532 of these instances, but (like the info tables and entry code) it is
533 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
537 mkTupleTy :: Boxity -> [Type] -> Type
538 -- Special case for *boxed* 1-tuples, which are represented by the type itself
539 mkTupleTy boxity [ty] | Boxed <- boxity = ty
540 mkTupleTy boxity tys = mkTyConApp (tupleTyCon boxity (length tys)) tys
542 -- | Build the type of a small tuple that holds the specified type of thing
543 mkBoxedTupleTy :: [Type] -> Type
544 mkBoxedTupleTy tys = mkTupleTy Boxed tys
547 unitTy = mkTupleTy Boxed []
550 %************************************************************************
552 \subsection[TysWiredIn-PArr]{The @[::]@ type}
554 %************************************************************************
556 Special syntax for parallel arrays needs some wired in definitions.
559 -- | Construct a type representing the application of the parallel array constructor
560 mkPArrTy :: Type -> Type
561 mkPArrTy ty = mkTyConApp parrTyCon [ty]
563 -- | Represents the type constructor of parallel arrays
565 -- * This must match the definition in @PrelPArr@
567 -- NB: Although the constructor is given here, it will not be accessible in
568 -- user code as it is not in the environment of any compiled module except
572 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [parrDataCon]
574 parrDataCon :: DataCon
575 parrDataCon = pcDataCon
577 alpha_tyvar -- forall'ed type variables
578 [intPrimTy, -- 1st argument: Int#
579 mkTyConApp -- 2nd argument: Array# a
584 -- | Check whether a type constructor is the constructor for parallel arrays
585 isPArrTyCon :: TyCon -> Bool
586 isPArrTyCon tc = tyConName tc == parrTyConName
588 -- | Fake array constructors
590 -- * These constructors are never really used to represent array values;
591 -- however, they are very convenient during desugaring (and, in particular,
592 -- in the pattern matching compiler) to treat array pattern just like
593 -- yet another constructor pattern
595 parrFakeCon :: Arity -> DataCon
596 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
597 parrFakeCon i = parrFakeConArr!i
599 -- pre-defined set of constructors
601 parrFakeConArr :: Array Int DataCon
602 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
603 | i <- [0..mAX_TUPLE_SIZE]]
605 -- build a fake parallel array constructor for the given arity
607 mkPArrFakeCon :: Int -> DataCon
608 mkPArrFakeCon arity = data_con
610 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
611 tyvar = head alphaTyVars
612 tyvarTys = replicate arity $ mkTyVarTy tyvar
613 nameStr = mkFastString ("MkPArr" ++ show arity)
614 name = mkWiredInName gHC_PARR (mkDataOccFS nameStr) unique
615 (ADataCon data_con) UserSyntax
616 unique = mkPArrDataConUnique arity
618 -- | Checks whether a data constructor is a fake constructor for parallel arrays
619 isPArrFakeCon :: DataCon -> Bool
620 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)