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,
25 falseDataCon, falseDataConId,
47 unitTyCon, unitDataCon, unitDataConId, pairTyCon,
48 unboxedSingletonTyCon, unboxedSingletonDataCon,
49 unboxedPairTyCon, unboxedPairDataCon,
52 genUnitTyCon, genUnitDataCon,
53 plusTyCon, inrDataCon, inlDataCon,
54 crossTyCon, crossDataCon,
57 trueDataCon, trueDataConId,
66 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon
69 #include "HsVersions.h"
71 import {-# SOURCE #-} MkId( mkDataConWorkId )
72 import {-# SOURCE #-} Generics( mkTyConGenInfo )
79 import Constants ( mAX_TUPLE_SIZE )
80 import Module ( mkBasePkgModule )
81 import Name ( Name, nameUnique, nameOccName,
82 nameModule, mkWiredInName )
83 import OccName ( mkOccFS, tcName, dataName, mkDataConWorkerOcc, mkGenOcc1, mkGenOcc2 )
84 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
85 import Var ( TyVar, tyVarKind )
86 import TyCon ( TyCon, AlgTyConFlavour(..), DataConDetails(..), tyConDataCons,
87 mkTupleTyCon, mkAlgTyCon, tyConName
90 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed )
92 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
93 mkArrowKinds, liftedTypeKind, unliftedTypeKind,
95 import Unique ( incrUnique, mkTupleTyConUnique,
96 mkTupleDataConUnique, mkPArrDataConUnique )
101 alpha_tyvar = [alphaTyVar]
103 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
107 %************************************************************************
109 \subsection{Wired in type constructors}
111 %************************************************************************
113 If you change which things are wired in, make sure you change their
114 names in PrelNames, so they use wTcQual, wDataQual, etc
117 wiredInTyCons :: [TyCon]
118 wiredInTyCons = data_tycons ++ tuple_tycons ++ unboxed_tuple_tycons
120 data_tycons = genericTyCons ++
132 genericTyCons :: [TyCon]
133 genericTyCons = [ plusTyCon, crossTyCon, genUnitTyCon ]
135 tuple_tycons = unitTyCon : [tupleTyCon Boxed i | i <- [2..mAX_TUPLE_SIZE] ]
136 unboxed_tuple_tycons = [tupleTyCon Unboxed i | i <- [1..mAX_TUPLE_SIZE] ]
140 %************************************************************************
142 \subsection{mkWiredInTyCon}
144 %************************************************************************
147 pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
148 pcRecDataTyCon = pcTyCon DataTyCon Recursive
150 pcTyCon new_or_data is_rec name tyvars argvrcs cons
153 tycon = mkAlgTyCon name kind
158 [] -- No record selectors
163 mod = nameModule name
164 kind = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
165 gen_info = mk_tc_gen_info mod (nameUnique name) name tycon
167 -- We generate names for the generic to/from Ids by incrementing
168 -- the TyCon unique. So each Prelude tycon needs 3 slots, one
169 -- for itself and two more for the generic Ids.
170 mk_tc_gen_info mod tc_uniq tc_name tycon
171 = mkTyConGenInfo tycon [name1, name2]
173 tc_occ_name = nameOccName tc_name
174 occ_name1 = mkGenOcc1 tc_occ_name
175 occ_name2 = mkGenOcc2 tc_occ_name
176 fn1_key = incrUnique tc_uniq
177 fn2_key = incrUnique fn1_key
178 name1 = mkWiredInName mod occ_name1 fn1_key
179 name2 = mkWiredInName mod occ_name2 fn2_key
181 pcDataCon :: Name -> [TyVar] -> ThetaType -> [Type] -> TyCon -> DataCon
182 -- The Name should be in the DataName name space; it's the name
183 -- of the DataCon itself.
185 -- The unique is the first of two free uniques;
186 -- the first is used for the datacon itself,
187 -- the second is used for the "worker name"
189 pcDataCon dc_name tyvars context arg_tys tycon
192 data_con = mkDataCon dc_name
193 [{- No strictness -}]
194 [{- No labelled fields -}]
195 tyvars context [] [] arg_tys tycon work_id
196 Nothing {- No wrapper for wired-in things
197 (they are too simple to need one) -}
199 mod = nameModule dc_name
200 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
201 wrk_key = incrUnique (nameUnique dc_name)
202 wrk_name = mkWiredInName mod wrk_occ wrk_key
203 work_id = mkDataConWorkId wrk_name data_con
207 %************************************************************************
209 \subsection[TysWiredIn-tuples]{The tuple types}
211 %************************************************************************
214 tupleTyCon :: Boxity -> Arity -> TyCon
215 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
216 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
217 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
219 tupleCon :: Boxity -> Arity -> DataCon
220 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
221 tupleCon Boxed i = snd (boxedTupleArr ! i)
222 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
224 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
225 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
226 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
228 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
229 mk_tuple boxity arity = (tycon, tuple_con)
231 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
232 tc_name = mkWiredInName mod (mkOccFS tcName name_str) tc_uniq
233 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
234 res_kind | isBoxed boxity = liftedTypeKind
235 | otherwise = unliftedTypeKind
237 tyvars | isBoxed boxity = take arity alphaTyVars
238 | otherwise = take arity openAlphaTyVars
240 tuple_con = pcDataCon name tyvars [] tyvar_tys tycon
241 tyvar_tys = mkTyVarTys tyvars
242 (mod_name, name_str) = mkTupNameStr boxity arity
243 name = mkWiredInName mod (mkOccFS dataName name_str) dc_uniq
244 tc_uniq = mkTupleTyConUnique boxity arity
245 dc_uniq = mkTupleDataConUnique boxity arity
246 mod = mkBasePkgModule mod_name
247 gen_info = mk_tc_gen_info mod tc_uniq tc_name tycon
249 unitTyCon = tupleTyCon Boxed 0
250 unitDataCon = head (tyConDataCons unitTyCon)
251 unitDataConId = dataConWorkId unitDataCon
253 pairTyCon = tupleTyCon Boxed 2
255 unboxedSingletonTyCon = tupleTyCon Unboxed 1
256 unboxedSingletonDataCon = tupleCon Unboxed 1
258 unboxedPairTyCon = tupleTyCon Unboxed 2
259 unboxedPairDataCon = tupleCon Unboxed 2
262 %************************************************************************
264 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
266 %************************************************************************
269 -- The Void type is represented as a data type with no constructors
270 -- It's a built in type (i.e. there's no way to define it in Haskell;
271 -- the nearest would be
273 -- data Void = -- No constructors!
275 -- ) It's lifted; there is only one value of this
276 -- type, namely "void", whose semantics is just bottom.
278 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
285 charTy = mkTyConTy charTyCon
287 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
288 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
290 stringTy = mkListTy charTy -- convenience only
294 intTy = mkTyConTy intTyCon
296 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
297 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
301 wordTy = mkTyConTy wordTyCon
303 wordTyCon = pcNonRecDataTyCon wordTyConName [] [] [wordDataCon]
304 wordDataCon = pcDataCon wordDataConName [] [] [wordPrimTy] wordTyCon
308 floatTy = mkTyConTy floatTyCon
310 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
311 floatDataCon = pcDataCon floatDataConName [] [] [floatPrimTy] floatTyCon
315 doubleTy = mkTyConTy doubleTyCon
317 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
318 doubleDataCon = pcDataCon doubleDataConName [] [] [doublePrimTy] doubleTyCon
322 %************************************************************************
324 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
326 %************************************************************************
328 @Integer@ and its pals are not really primitive. @Integer@ itself, first:
331 integerTy = mkTyConTy integerTyCon
333 integerTyCon = pcNonRecDataTyCon integerTyConName
334 [] [] [smallIntegerDataCon, largeIntegerDataCon]
336 smallIntegerDataCon = pcDataCon smallIntegerDataConName
337 [] [] [intPrimTy] integerTyCon
338 largeIntegerDataCon = pcDataCon largeIntegerDataConName
339 [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
343 %************************************************************************
345 \subsection[TysWiredIn-Bool]{The @Bool@ type}
347 %************************************************************************
349 An ordinary enumeration type, but deeply wired in. There are no
350 magical operations on @Bool@ (just the regular Prelude code).
352 {\em BEGIN IDLE SPECULATION BY SIMON}
354 This is not the only way to encode @Bool@. A more obvious coding makes
355 @Bool@ just a boxed up version of @Bool#@, like this:
358 data Bool = MkBool Bool#
361 Unfortunately, this doesn't correspond to what the Report says @Bool@
362 looks like! Furthermore, we get slightly less efficient code (I
363 think) with this coding. @gtInt@ would look like this:
366 gtInt :: Int -> Int -> Bool
367 gtInt x y = case x of I# x# ->
369 case (gtIntPrim x# y#) of
373 Notice that the result of the @gtIntPrim@ comparison has to be turned
374 into an integer (here called @b#@), and returned in a @MkBool@ box.
376 The @if@ expression would compile to this:
379 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
382 I think this code is a little less efficient than the previous code,
383 but I'm not certain. At all events, corresponding with the Report is
384 important. The interesting thing is that the language is expressive
385 enough to describe more than one alternative; and that a type doesn't
386 necessarily need to be a straightforwardly boxed version of its
387 primitive counterpart.
389 {\em END IDLE SPECULATION BY SIMON}
392 boolTy = mkTyConTy boolTyCon
394 boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConName
395 [] [] [falseDataCon, trueDataCon]
397 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
398 trueDataCon = pcDataCon trueDataConName [] [] [] boolTyCon
400 falseDataConId = dataConWorkId falseDataCon
401 trueDataConId = dataConWorkId trueDataCon
404 %************************************************************************
406 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
408 %************************************************************************
410 Special syntax, deeply wired in, but otherwise an ordinary algebraic
413 data [] a = [] | a : (List a)
415 data (,) a b = (,,) a b
420 mkListTy :: Type -> Type
421 mkListTy ty = mkTyConApp listTyCon [ty]
423 listTyCon = pcRecDataTyCon listTyConName
424 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
426 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
427 consDataCon = pcDataCon consDataConName
428 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
429 -- Interesting: polymorphic recursion would help here.
430 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
431 -- gets the over-specific type (Type -> Type)
434 %************************************************************************
436 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
438 %************************************************************************
440 The tuple types are definitely magic, because they form an infinite
445 They have a special family of type constructors, of type @TyCon@
446 These contain the tycon arity, but don't require a Unique.
449 They have a special family of constructors, of type
450 @Id@. Again these contain their arity but don't need a Unique.
453 There should be a magic way of generating the info tables and
454 entry code for all tuples.
456 But at the moment we just compile a Haskell source
457 file\srcloc{lib/prelude/...} containing declarations like:
460 data Tuple2 a b = Tup2 a b
461 data Tuple3 a b c = Tup3 a b c
462 data Tuple4 a b c d = Tup4 a b c d
465 The print-names associated with the magic @Id@s for tuple constructors
466 ``just happen'' to be the same as those generated by these
470 The instance environment should have a magic way to know
471 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
472 so on. \ToDo{Not implemented yet.}
475 There should also be a way to generate the appropriate code for each
476 of these instances, but (like the info tables and entry code) it is
477 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
481 mkTupleTy :: Boxity -> Int -> [Type] -> Type
482 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
484 unitTy = mkTupleTy Boxed 0 []
487 %************************************************************************
489 \subsection[TysWiredIn-PArr]{The @[::]@ type}
491 %************************************************************************
493 Special syntax for parallel arrays needs some wired in definitions.
496 -- construct a type representing the application of the parallel array
499 mkPArrTy :: Type -> Type
500 mkPArrTy ty = mkTyConApp parrTyCon [ty]
502 -- represents the type constructor of parallel arrays
504 -- * this must match the definition in `PrelPArr'
506 -- NB: Although the constructor is given here, it will not be accessible in
507 -- user code as it is not in the environment of any compiled module except
519 (DataCons [parrDataCon]) -- The constructor defined in `PrelPArr'
520 [] -- No record selectors
525 mod = nameModule parrTyConName
526 kind = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
527 genInfo = mk_tc_gen_info mod (nameUnique parrTyConName) parrTyConName tycon
529 parrDataCon :: DataCon
530 parrDataCon = pcDataCon
532 alpha_tyvar -- forall'ed type variables
534 [intPrimTy, -- 1st argument: Int#
535 mkTyConApp -- 2nd argument: Array# a
540 -- check whether a type constructor is the constructor for parallel arrays
542 isPArrTyCon :: TyCon -> Bool
543 isPArrTyCon tc = tyConName tc == parrTyConName
545 -- fake array constructors
547 -- * these constructors are never really used to represent array values;
548 -- however, they are very convenient during desugaring (and, in particular,
549 -- in the pattern matching compiler) to treat array pattern just like
550 -- yet another constructor pattern
552 parrFakeCon :: Arity -> DataCon
553 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
554 parrFakeCon i = parrFakeConArr!i
556 -- pre-defined set of constructors
558 parrFakeConArr :: Array Int DataCon
559 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
560 | i <- [0..mAX_TUPLE_SIZE]]
562 -- build a fake parallel array constructor for the given arity
564 mkPArrFakeCon :: Int -> DataCon
565 mkPArrFakeCon arity = pcDataCon name [tyvar] [] tyvarTys parrTyCon
567 tyvar = head alphaTyVars
568 tyvarTys = replicate arity $ mkTyVarTy tyvar
569 nameStr = mkFastString ("MkPArr" ++ show arity)
570 name = mkWiredInName mod (mkOccFS dataName nameStr) uniq
571 uniq = mkPArrDataConUnique arity
572 mod = mkBasePkgModule pREL_PARR_Name
574 -- checks whether a data constructor is a fake constructor for parallel arrays
576 isPArrFakeCon :: DataCon -> Bool
577 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)
580 %************************************************************************
582 \subsection{Wired In Type Constructors for Representation Types}
584 %************************************************************************
586 The following code defines the wired in datatypes cross, plus, unit
587 and c_of needed for the generic methods.
589 Ok, so the basic story is that for each type constructor I need to
590 create 2 things - a TyCon and a DataCon and then we are basically
591 ok. There are going to be no arguments passed to these functions
592 because -well- there is nothing to pass to these functions.
596 crossTyCon = pcNonRecDataTyCon crossTyConName alpha_beta_tyvars [] [crossDataCon]
598 crossDataCon :: DataCon
599 crossDataCon = pcDataCon crossDataConName alpha_beta_tyvars [] [alphaTy, betaTy] crossTyCon
602 plusTyCon = pcNonRecDataTyCon plusTyConName alpha_beta_tyvars [] [inlDataCon, inrDataCon]
604 inlDataCon, inrDataCon :: DataCon
605 inlDataCon = pcDataCon inlDataConName alpha_beta_tyvars [] [alphaTy] plusTyCon
606 inrDataCon = pcDataCon inrDataConName alpha_beta_tyvars [] [betaTy] plusTyCon
608 genUnitTyCon :: TyCon -- The "1" type constructor for generics
609 genUnitTyCon = pcNonRecDataTyCon genUnitTyConName [] [] [genUnitDataCon]
611 genUnitDataCon :: DataCon
612 genUnitDataCon = pcDataCon genUnitDataConName [] [] [] genUnitTyCon