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.''
14 wiredInTyCons, genericTyCons,
34 falseDataCon, falseDataConId,
56 unitTyCon, unitDataCon, unitDataConId, pairTyCon,
57 unboxedSingletonTyCon, unboxedSingletonDataCon,
58 unboxedPairTyCon, unboxedPairDataCon,
61 genUnitTyCon, genUnitDataCon,
62 plusTyCon, inrDataCon, inlDataCon,
63 crossTyCon, crossDataCon,
67 trueDataCon, trueDataConId,
76 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon
79 #include "HsVersions.h"
81 import {-# SOURCE #-} MkId( mkDataConWorkId )
82 import {-# SOURCE #-} Generics( mkTyConGenInfo )
89 import Constants ( mAX_TUPLE_SIZE )
90 import Module ( mkBasePkgModule )
91 import Name ( Name, nameUnique, nameOccName,
92 nameModule, mkWiredInName )
93 import OccName ( mkOccFS, tcName, dataName, mkDataConWorkerOcc, mkGenOcc1, mkGenOcc2 )
94 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
95 import Var ( TyVar, tyVarKind )
96 import TyCon ( TyCon, AlgTyConFlavour(..), DataConDetails(..), tyConDataCons,
97 mkTupleTyCon, mkAlgTyCon, tyConName
100 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed, StrictnessMark(..) )
102 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
103 mkArrowKinds, liftedTypeKind, unliftedTypeKind,
105 import Unique ( incrUnique, mkTupleTyConUnique,
106 mkTupleDataConUnique, mkPArrDataConUnique )
111 alpha_tyvar = [alphaTyVar]
113 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
117 %************************************************************************
119 \subsection{Wired in type constructors}
121 %************************************************************************
123 If you change which things are wired in, make sure you change their
124 names in PrelNames, so they use wTcQual, wDataQual, etc
127 wiredInTyCons :: [TyCon]
128 wiredInTyCons = data_tycons ++ tuple_tycons ++ unboxed_tuple_tycons
130 data_tycons = genericTyCons ++
145 genericTyCons :: [TyCon]
146 genericTyCons = [ plusTyCon, crossTyCon, genUnitTyCon ]
148 tuple_tycons = unitTyCon : [tupleTyCon Boxed i | i <- [2..mAX_TUPLE_SIZE] ]
149 unboxed_tuple_tycons = [tupleTyCon Unboxed i | i <- [1..mAX_TUPLE_SIZE] ]
153 %************************************************************************
155 \subsection{mkWiredInTyCon}
157 %************************************************************************
160 pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
161 pcRecDataTyCon = pcTyCon DataTyCon Recursive
163 pcTyCon new_or_data is_rec name tyvars argvrcs cons
166 tycon = mkAlgTyCon name kind
171 [] -- No record selectors
176 mod = nameModule name
177 kind = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
178 gen_info = mk_tc_gen_info mod (nameUnique name) name tycon
180 -- We generate names for the generic to/from Ids by incrementing
181 -- the TyCon unique. So each Prelude tycon needs 3 slots, one
182 -- for itself and two more for the generic Ids.
183 mk_tc_gen_info mod tc_uniq tc_name tycon
184 = mkTyConGenInfo tycon [name1, name2]
186 tc_occ_name = nameOccName tc_name
187 occ_name1 = mkGenOcc1 tc_occ_name
188 occ_name2 = mkGenOcc2 tc_occ_name
189 fn1_key = incrUnique tc_uniq
190 fn2_key = incrUnique fn1_key
191 name1 = mkWiredInName mod occ_name1 fn1_key
192 name2 = mkWiredInName mod occ_name2 fn2_key
194 pcDataCon :: Name -> [TyVar] -> ThetaType -> [Type] -> TyCon -> DataCon
195 -- The Name should be in the DataName name space; it's the name
196 -- of the DataCon itself.
198 -- The unique is the first of two free uniques;
199 -- the first is used for the datacon itself,
200 -- the second is used for the "worker name"
202 pcDataCon dc_name tyvars context arg_tys tycon
205 data_con = mkDataCon dc_name
206 [{- No strictness -}]
207 [{- No labelled fields -}]
208 tyvars context [] [] arg_tys tycon work_id
209 Nothing {- No wrapper for wired-in things
210 (they are too simple to need one) -}
212 mod = nameModule dc_name
213 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
214 wrk_key = incrUnique (nameUnique dc_name)
215 wrk_name = mkWiredInName mod wrk_occ wrk_key
216 work_id = mkDataConWorkId wrk_name data_con
220 %************************************************************************
222 \subsection[TysWiredIn-tuples]{The tuple types}
224 %************************************************************************
227 tupleTyCon :: Boxity -> Arity -> TyCon
228 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
229 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
230 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
232 tupleCon :: Boxity -> Arity -> DataCon
233 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
234 tupleCon Boxed i = snd (boxedTupleArr ! i)
235 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
237 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
238 boxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Boxed i) | i <- [0..mAX_TUPLE_SIZE]]
239 unboxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Unboxed i) | i <- [0..mAX_TUPLE_SIZE]]
241 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
242 mk_tuple boxity arity = (tycon, tuple_con)
244 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
245 tc_name = mkWiredInName mod (mkOccFS tcName name_str) tc_uniq
246 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
247 res_kind | isBoxed boxity = liftedTypeKind
248 | otherwise = unliftedTypeKind
250 tyvars | isBoxed boxity = take arity alphaTyVars
251 | otherwise = take arity openAlphaTyVars
253 tuple_con = pcDataCon name tyvars [] tyvar_tys tycon
254 tyvar_tys = mkTyVarTys tyvars
255 (mod_name, name_str) = mkTupNameStr boxity arity
256 name = mkWiredInName mod (mkOccFS dataName name_str) dc_uniq
257 tc_uniq = mkTupleTyConUnique boxity arity
258 dc_uniq = mkTupleDataConUnique boxity arity
259 mod = mkBasePkgModule mod_name
260 gen_info = mk_tc_gen_info mod tc_uniq tc_name tycon
262 unitTyCon = tupleTyCon Boxed 0
263 unitDataCon = head (tyConDataCons unitTyCon)
264 unitDataConId = dataConWorkId unitDataCon
266 pairTyCon = tupleTyCon Boxed 2
268 unboxedSingletonTyCon = tupleTyCon Unboxed 1
269 unboxedSingletonDataCon = tupleCon Unboxed 1
271 unboxedPairTyCon = tupleTyCon Unboxed 2
272 unboxedPairDataCon = tupleCon Unboxed 2
275 %************************************************************************
277 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
279 %************************************************************************
282 -- The Void type is represented as a data type with no constructors
283 -- It's a built in type (i.e. there's no way to define it in Haskell;
284 -- the nearest would be
286 -- data Void = -- No constructors!
288 -- ) It's lifted; there is only one value of this
289 -- type, namely "void", whose semantics is just bottom.
291 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
298 charTy = mkTyConTy charTyCon
300 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
301 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
303 stringTy = mkListTy charTy -- convenience only
307 intTy = mkTyConTy intTyCon
309 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
310 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
314 wordTy = mkTyConTy wordTyCon
316 wordTyCon = pcNonRecDataTyCon wordTyConName [] [] [wordDataCon]
317 wordDataCon = pcDataCon wordDataConName [] [] [wordPrimTy] wordTyCon
321 addrTy = mkTyConTy addrTyCon
323 addrTyCon = pcNonRecDataTyCon addrTyConName [] [] [addrDataCon]
324 addrDataCon = pcDataCon addrDataConName [] [] [addrPrimTy] addrTyCon
328 ptrTy = mkTyConTy ptrTyCon
330 ptrTyCon = pcNonRecDataTyCon ptrTyConName alpha_tyvar [(True,False)] [ptrDataCon]
331 ptrDataCon = pcDataCon ptrDataConName alpha_tyvar [] [addrPrimTy] ptrTyCon
335 funPtrTy = mkTyConTy funPtrTyCon
337 funPtrTyCon = pcNonRecDataTyCon funPtrTyConName alpha_tyvar [(True,False)] [funPtrDataCon]
338 funPtrDataCon = pcDataCon funPtrDataConName alpha_tyvar [] [addrPrimTy] funPtrTyCon
342 floatTy = mkTyConTy floatTyCon
344 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
345 floatDataCon = pcDataCon floatDataConName [] [] [floatPrimTy] floatTyCon
349 doubleTy = mkTyConTy doubleTyCon
351 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
352 doubleDataCon = pcDataCon doubleDataConName [] [] [doublePrimTy] doubleTyCon
357 = pcNonRecDataTyCon stablePtrTyConName
358 alpha_tyvar [(True,False)] [stablePtrDataCon]
361 = pcDataCon stablePtrDataConName
362 alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
365 %************************************************************************
367 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
369 %************************************************************************
371 @Integer@ and its pals are not really primitive. @Integer@ itself, first:
374 integerTy = mkTyConTy integerTyCon
376 integerTyCon = pcNonRecDataTyCon integerTyConName
377 [] [] [smallIntegerDataCon, largeIntegerDataCon]
379 smallIntegerDataCon = pcDataCon smallIntegerDataConName
380 [] [] [intPrimTy] integerTyCon
381 largeIntegerDataCon = pcDataCon largeIntegerDataConName
382 [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
386 %************************************************************************
388 \subsection[TysWiredIn-Bool]{The @Bool@ type}
390 %************************************************************************
392 An ordinary enumeration type, but deeply wired in. There are no
393 magical operations on @Bool@ (just the regular Prelude code).
395 {\em BEGIN IDLE SPECULATION BY SIMON}
397 This is not the only way to encode @Bool@. A more obvious coding makes
398 @Bool@ just a boxed up version of @Bool#@, like this:
401 data Bool = MkBool Bool#
404 Unfortunately, this doesn't correspond to what the Report says @Bool@
405 looks like! Furthermore, we get slightly less efficient code (I
406 think) with this coding. @gtInt@ would look like this:
409 gtInt :: Int -> Int -> Bool
410 gtInt x y = case x of I# x# ->
412 case (gtIntPrim x# y#) of
416 Notice that the result of the @gtIntPrim@ comparison has to be turned
417 into an integer (here called @b#@), and returned in a @MkBool@ box.
419 The @if@ expression would compile to this:
422 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
425 I think this code is a little less efficient than the previous code,
426 but I'm not certain. At all events, corresponding with the Report is
427 important. The interesting thing is that the language is expressive
428 enough to describe more than one alternative; and that a type doesn't
429 necessarily need to be a straightforwardly boxed version of its
430 primitive counterpart.
432 {\em END IDLE SPECULATION BY SIMON}
435 boolTy = mkTyConTy boolTyCon
437 boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConName
438 [] [] [falseDataCon, trueDataCon]
440 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
441 trueDataCon = pcDataCon trueDataConName [] [] [] boolTyCon
443 falseDataConId = dataConWorkId falseDataCon
444 trueDataConId = dataConWorkId trueDataCon
447 %************************************************************************
449 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
451 %************************************************************************
453 Special syntax, deeply wired in, but otherwise an ordinary algebraic
456 data [] a = [] | a : (List a)
458 data (,) a b = (,,) a b
463 mkListTy :: Type -> Type
464 mkListTy ty = mkTyConApp listTyCon [ty]
466 listTyCon = pcRecDataTyCon listTyConName
467 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
469 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
470 consDataCon = pcDataCon consDataConName
471 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
472 -- Interesting: polymorphic recursion would help here.
473 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
474 -- gets the over-specific type (Type -> Type)
477 %************************************************************************
479 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
481 %************************************************************************
483 The tuple types are definitely magic, because they form an infinite
488 They have a special family of type constructors, of type @TyCon@
489 These contain the tycon arity, but don't require a Unique.
492 They have a special family of constructors, of type
493 @Id@. Again these contain their arity but don't need a Unique.
496 There should be a magic way of generating the info tables and
497 entry code for all tuples.
499 But at the moment we just compile a Haskell source
500 file\srcloc{lib/prelude/...} containing declarations like:
503 data Tuple2 a b = Tup2 a b
504 data Tuple3 a b c = Tup3 a b c
505 data Tuple4 a b c d = Tup4 a b c d
508 The print-names associated with the magic @Id@s for tuple constructors
509 ``just happen'' to be the same as those generated by these
513 The instance environment should have a magic way to know
514 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
515 so on. \ToDo{Not implemented yet.}
518 There should also be a way to generate the appropriate code for each
519 of these instances, but (like the info tables and entry code) it is
520 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
524 mkTupleTy :: Boxity -> Int -> [Type] -> Type
525 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
527 unitTy = mkTupleTy Boxed 0 []
530 %************************************************************************
532 \subsection[TysWiredIn-PArr]{The @[::]@ type}
534 %************************************************************************
536 Special syntax for parallel arrays needs some wired in definitions.
539 -- construct a type representing the application of the parallel array
542 mkPArrTy :: Type -> Type
543 mkPArrTy ty = mkTyConApp parrTyCon [ty]
545 -- represents the type constructor of parallel arrays
547 -- * this must match the definition in `PrelPArr'
549 -- NB: Although the constructor is given here, it will not be accessible in
550 -- user code as it is not in the environment of any compiled module except
562 (DataCons [parrDataCon]) -- The constructor defined in `PrelPArr'
563 [] -- No record selectors
568 mod = nameModule parrTyConName
569 kind = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
570 genInfo = mk_tc_gen_info mod (nameUnique parrTyConName) parrTyConName tycon
572 parrDataCon :: DataCon
573 parrDataCon = pcDataCon
575 alpha_tyvar -- forall'ed type variables
577 [intPrimTy, -- 1st argument: Int#
578 mkTyConApp -- 2nd argument: Array# a
583 -- 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 = pcDataCon name [tyvar] [] tyvarTys parrTyCon
610 tyvar = head alphaTyVars
611 tyvarTys = replicate arity $ mkTyVarTy tyvar
612 nameStr = mkFastString ("MkPArr" ++ show arity)
613 name = mkWiredInName mod (mkOccFS dataName nameStr) uniq
614 uniq = mkPArrDataConUnique arity
615 mod = mkBasePkgModule pREL_PARR_Name
617 -- checks whether a data constructor is a fake constructor for parallel arrays
619 isPArrFakeCon :: DataCon -> Bool
620 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)
623 %************************************************************************
625 \subsection{Wired In Type Constructors for Representation Types}
627 %************************************************************************
629 The following code defines the wired in datatypes cross, plus, unit
630 and c_of needed for the generic methods.
632 Ok, so the basic story is that for each type constructor I need to
633 create 2 things - a TyCon and a DataCon and then we are basically
634 ok. There are going to be no arguments passed to these functions
635 because -well- there is nothing to pass to these functions.
639 crossTyCon = pcNonRecDataTyCon crossTyConName alpha_beta_tyvars [] [crossDataCon]
641 crossDataCon :: DataCon
642 crossDataCon = pcDataCon crossDataConName alpha_beta_tyvars [] [alphaTy, betaTy] crossTyCon
645 plusTyCon = pcNonRecDataTyCon plusTyConName alpha_beta_tyvars [] [inlDataCon, inrDataCon]
647 inlDataCon, inrDataCon :: DataCon
648 inlDataCon = pcDataCon inlDataConName alpha_beta_tyvars [] [alphaTy] plusTyCon
649 inrDataCon = pcDataCon inrDataConName alpha_beta_tyvars [] [betaTy] plusTyCon
651 genUnitTyCon :: TyCon -- The "1" type constructor for generics
652 genUnitTyCon = pcNonRecDataTyCon genUnitTyConName [] [] [genUnitDataCon]
654 genUnitDataCon :: DataCon
655 genUnitDataCon = pcDataCon genUnitDataConName [] [] [] genUnitTyCon