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,
46 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
47 parrTyCon_RDR, parrTyConName
50 #include "HsVersions.h"
52 import {-# SOURCE #-} MkId( mkDataConIds )
59 import Constants ( mAX_TUPLE_SIZE )
60 import Module ( Module )
61 import RdrName ( nameRdrName )
62 import Name ( Name, BuiltInSyntax(..), nameUnique, nameOccName,
63 nameModule, mkWiredInName )
64 import OccName ( mkOccNameFS, tcName, dataName, mkTupleOcc,
66 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
67 import Var ( TyVar, tyVarKind )
68 import TyCon ( TyCon, AlgTyConRhs(DataTyCon), tyConDataCons,
69 mkTupleTyCon, mkAlgTyCon, tyConName,
70 AlgTyConParent(NoParentTyCon) )
72 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed,
75 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
77 import Coercion ( unsafeCoercionTyCon, symCoercionTyCon,
78 transCoercionTyCon, leftCoercionTyCon,
79 rightCoercionTyCon, instCoercionTyCon )
80 import TypeRep ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
81 import Unique ( incrUnique, mkTupleTyConUnique,
82 mkTupleDataConUnique, mkPArrDataConUnique )
87 alpha_tyvar = [alphaTyVar]
92 %************************************************************************
94 \subsection{Wired in type constructors}
96 %************************************************************************
98 If you change which things are wired in, make sure you change their
99 names in PrelNames, so they use wTcQual, wDataQual, etc
102 wiredInTyCons :: [TyCon] -- Excludes tuples
103 -- This list is used only to define PrelInfo.wiredInThings
105 -- It does not need to include kind constructors, because
106 -- all that wiredInThings does is to initialise the Name table,
107 -- and kind constructors don't appear in source code.
109 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
110 -- it's defined in GHC.Base, and there's only
111 -- one of it. We put it in wiredInTyCons so
112 -- that it'll pre-populate the name cache, so
113 -- the special case in lookupOrigNameCache
114 -- doesn't need to look out for it
122 , unsafeCoercionTyCon
132 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
133 mkWiredInTyConName built_in mod fs uniq tycon
134 = mkWiredInName mod (mkOccNameFS tcName fs) uniq
135 (ATyCon tycon) -- Relevant TyCon
138 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
139 mkWiredInDataConName built_in mod fs uniq datacon
140 = mkWiredInName mod (mkOccNameFS dataName fs) uniq
141 (ADataCon datacon) -- Relevant DataCon
144 charTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Char") charTyConKey charTyCon
145 charDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("C#") charDataConKey charDataCon
146 intTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Int") intTyConKey intTyCon
147 intDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("I#") intDataConKey intDataCon
149 boolTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Bool") boolTyConKey boolTyCon
150 falseDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("False") falseDataConKey falseDataCon
151 trueDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("True") trueDataConKey trueDataCon
152 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_BASE FSLIT("[]") listTyConKey listTyCon
153 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE FSLIT("[]") nilDataConKey nilDataCon
154 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE FSLIT(":") consDataConKey consDataCon
156 floatTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT FSLIT("Float") floatTyConKey floatTyCon
157 floatDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT FSLIT("F#") floatDataConKey floatDataCon
158 doubleTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT FSLIT("Double") doubleTyConKey doubleTyCon
159 doubleDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT FSLIT("D#") doubleDataConKey doubleDataCon
161 parrTyConName = mkWiredInTyConName BuiltInSyntax gHC_PARR FSLIT("[::]") parrTyConKey parrTyCon
162 parrDataConName = mkWiredInDataConName UserSyntax gHC_PARR FSLIT("PArr") parrDataConKey parrDataCon
164 boolTyCon_RDR = nameRdrName boolTyConName
165 false_RDR = nameRdrName falseDataConName
166 true_RDR = nameRdrName trueDataConName
167 intTyCon_RDR = nameRdrName intTyConName
168 charTyCon_RDR = nameRdrName charTyConName
169 intDataCon_RDR = nameRdrName intDataConName
170 listTyCon_RDR = nameRdrName listTyConName
171 consDataCon_RDR = nameRdrName consDataConName
172 parrTyCon_RDR = nameRdrName parrTyConName
174 tySuperKindTyCon_RDR = nameRdrName tySuperKindTyConName
175 coSuperKindTyCon_RDR = nameRdrName coSuperKindTyConName
176 liftedTypeKindTyCon_RDR = nameRdrName liftedTypeKindTyConName
177 openTypeKindTyCon_RDR = nameRdrName openTypeKindTyConName
178 unliftedTypeKindTyCon_RDR = nameRdrName unliftedTypeKindTyConName
179 ubxTupleKindTyCon_RDR = nameRdrName ubxTupleKindTyConName
180 argTypeKindTyCon_RDR = nameRdrName argTypeKindTyConName
181 funKindTyCon_RDR = nameRdrName funKindTyConName
186 %************************************************************************
188 \subsection{mkWiredInTyCon}
190 %************************************************************************
193 pcNonRecDataTyCon = pcTyCon False NonRecursive
194 pcRecDataTyCon = pcTyCon False Recursive
196 pcTyCon is_enum is_rec name tyvars cons
199 tycon = mkAlgTyCon name
200 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
202 [] -- No stupid theta
203 (DataTyCon cons is_enum)
204 [] -- No record selectors
207 True -- All the wired-in tycons have generics
208 False -- Not in GADT syntax
210 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
211 pcDataCon = pcDataConWithFixity False
213 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
214 -- The Name should be in the DataName name space; it's the name
215 -- of the DataCon itself.
217 -- The unique is the first of two free uniques;
218 -- the first is used for the datacon itself,
219 -- the second is used for the "worker name"
221 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
224 data_con = mkDataCon dc_name declared_infix
225 (map (const NotMarkedStrict) arg_tys)
226 [] -- No labelled fields
228 [] -- No existential type variables
229 [] -- No equality spec
232 [] -- No stupid theta
233 (mkDataConIds bogus_wrap_name wrk_name data_con)
236 mod = nameModule dc_name
237 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
238 wrk_key = incrUnique (nameUnique dc_name)
239 wrk_name = mkWiredInName mod wrk_occ wrk_key
240 (AnId (dataConWorkId data_con)) UserSyntax
241 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
242 -- Wired-in types are too simple to need wrappers
246 %************************************************************************
248 \subsection[TysWiredIn-tuples]{The tuple types}
250 %************************************************************************
253 tupleTyCon :: Boxity -> Arity -> TyCon
254 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
255 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
256 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
258 tupleCon :: Boxity -> Arity -> DataCon
259 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
260 tupleCon Boxed i = snd (boxedTupleArr ! i)
261 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
263 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
264 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
265 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
267 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
268 mk_tuple boxity arity = (tycon, tuple_con)
270 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
271 mod = mkTupleModule boxity arity
272 tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
273 (ATyCon tycon) BuiltInSyntax
274 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
275 res_kind | isBoxed boxity = liftedTypeKind
276 | otherwise = ubxTupleKind
278 tyvars | isBoxed boxity = take arity alphaTyVars
279 | otherwise = take arity openAlphaTyVars
281 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
282 tyvar_tys = mkTyVarTys tyvars
283 dc_name = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
284 (ADataCon tuple_con) BuiltInSyntax
285 tc_uniq = mkTupleTyConUnique boxity arity
286 dc_uniq = mkTupleDataConUnique boxity arity
287 gen_info = True -- Tuples all have generics..
288 -- hmm: that's a *lot* of code
290 unitTyCon = tupleTyCon Boxed 0
291 unitDataCon = head (tyConDataCons unitTyCon)
292 unitDataConId = dataConWorkId unitDataCon
294 pairTyCon = tupleTyCon Boxed 2
296 unboxedSingletonTyCon = tupleTyCon Unboxed 1
297 unboxedSingletonDataCon = tupleCon Unboxed 1
299 unboxedPairTyCon = tupleTyCon Unboxed 2
300 unboxedPairDataCon = tupleCon Unboxed 2
303 %************************************************************************
305 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
307 %************************************************************************
310 charTy = mkTyConTy charTyCon
312 charTyCon = pcNonRecDataTyCon charTyConName [] [charDataCon]
313 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
315 stringTy = mkListTy charTy -- convenience only
319 intTy = mkTyConTy intTyCon
321 intTyCon = pcNonRecDataTyCon intTyConName [] [intDataCon]
322 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
326 floatTy = mkTyConTy floatTyCon
328 floatTyCon = pcNonRecDataTyCon floatTyConName [] [floatDataCon]
329 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
333 doubleTy = mkTyConTy doubleTyCon
335 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [doubleDataCon]
336 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
340 %************************************************************************
342 \subsection[TysWiredIn-Bool]{The @Bool@ type}
344 %************************************************************************
346 An ordinary enumeration type, but deeply wired in. There are no
347 magical operations on @Bool@ (just the regular Prelude code).
349 {\em BEGIN IDLE SPECULATION BY SIMON}
351 This is not the only way to encode @Bool@. A more obvious coding makes
352 @Bool@ just a boxed up version of @Bool#@, like this:
355 data Bool = MkBool Bool#
358 Unfortunately, this doesn't correspond to what the Report says @Bool@
359 looks like! Furthermore, we get slightly less efficient code (I
360 think) with this coding. @gtInt@ would look like this:
363 gtInt :: Int -> Int -> Bool
364 gtInt x y = case x of I# x# ->
366 case (gtIntPrim x# y#) of
370 Notice that the result of the @gtIntPrim@ comparison has to be turned
371 into an integer (here called @b#@), and returned in a @MkBool@ box.
373 The @if@ expression would compile to this:
376 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
379 I think this code is a little less efficient than the previous code,
380 but I'm not certain. At all events, corresponding with the Report is
381 important. The interesting thing is that the language is expressive
382 enough to describe more than one alternative; and that a type doesn't
383 necessarily need to be a straightforwardly boxed version of its
384 primitive counterpart.
386 {\em END IDLE SPECULATION BY SIMON}
389 boolTy = mkTyConTy boolTyCon
391 boolTyCon = pcTyCon True NonRecursive boolTyConName
392 [] [falseDataCon, trueDataCon]
394 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
395 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
397 falseDataConId = dataConWorkId falseDataCon
398 trueDataConId = dataConWorkId trueDataCon
401 %************************************************************************
403 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
405 %************************************************************************
407 Special syntax, deeply wired in, but otherwise an ordinary algebraic
410 data [] a = [] | a : (List a)
412 data (,) a b = (,,) a b
417 mkListTy :: Type -> Type
418 mkListTy ty = mkTyConApp listTyCon [ty]
420 listTyCon = pcRecDataTyCon listTyConName alpha_tyvar [nilDataCon, consDataCon]
422 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
423 consDataCon = pcDataConWithFixity True {- Declared infix -}
425 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
426 -- Interesting: polymorphic recursion would help here.
427 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
428 -- gets the over-specific type (Type -> Type)
431 %************************************************************************
433 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
435 %************************************************************************
437 The tuple types are definitely magic, because they form an infinite
442 They have a special family of type constructors, of type @TyCon@
443 These contain the tycon arity, but don't require a Unique.
446 They have a special family of constructors, of type
447 @Id@. Again these contain their arity but don't need a Unique.
450 There should be a magic way of generating the info tables and
451 entry code for all tuples.
453 But at the moment we just compile a Haskell source
454 file\srcloc{lib/prelude/...} containing declarations like:
457 data Tuple2 a b = Tup2 a b
458 data Tuple3 a b c = Tup3 a b c
459 data Tuple4 a b c d = Tup4 a b c d
462 The print-names associated with the magic @Id@s for tuple constructors
463 ``just happen'' to be the same as those generated by these
467 The instance environment should have a magic way to know
468 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
469 so on. \ToDo{Not implemented yet.}
472 There should also be a way to generate the appropriate code for each
473 of these instances, but (like the info tables and entry code) it is
474 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
478 mkTupleTy :: Boxity -> Int -> [Type] -> Type
479 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
481 unitTy = mkTupleTy Boxed 0 []
484 %************************************************************************
486 \subsection[TysWiredIn-PArr]{The @[::]@ type}
488 %************************************************************************
490 Special syntax for parallel arrays needs some wired in definitions.
493 -- construct a type representing the application of the parallel array
496 mkPArrTy :: Type -> Type
497 mkPArrTy ty = mkTyConApp parrTyCon [ty]
499 -- represents the type constructor of parallel arrays
501 -- * this must match the definition in `PrelPArr'
503 -- NB: Although the constructor is given here, it will not be accessible in
504 -- user code as it is not in the environment of any compiled module except
508 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [parrDataCon]
510 parrDataCon :: DataCon
511 parrDataCon = pcDataCon
513 alpha_tyvar -- forall'ed type variables
514 [intPrimTy, -- 1st argument: Int#
515 mkTyConApp -- 2nd argument: Array# a
520 -- check whether a type constructor is the constructor for parallel arrays
522 isPArrTyCon :: TyCon -> Bool
523 isPArrTyCon tc = tyConName tc == parrTyConName
525 -- fake array constructors
527 -- * these constructors are never really used to represent array values;
528 -- however, they are very convenient during desugaring (and, in particular,
529 -- in the pattern matching compiler) to treat array pattern just like
530 -- yet another constructor pattern
532 parrFakeCon :: Arity -> DataCon
533 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
534 parrFakeCon i = parrFakeConArr!i
536 -- pre-defined set of constructors
538 parrFakeConArr :: Array Int DataCon
539 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
540 | i <- [0..mAX_TUPLE_SIZE]]
542 -- build a fake parallel array constructor for the given arity
544 mkPArrFakeCon :: Int -> DataCon
545 mkPArrFakeCon arity = data_con
547 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
548 tyvar = head alphaTyVars
549 tyvarTys = replicate arity $ mkTyVarTy tyvar
550 nameStr = mkFastString ("MkPArr" ++ show arity)
551 name = mkWiredInName gHC_PARR (mkOccNameFS dataName nameStr) uniq
552 (ADataCon data_con) UserSyntax
553 uniq = mkPArrDataConUnique arity
555 -- checks whether a data constructor is a fake constructor for parallel arrays
557 isPArrFakeCon :: DataCon -> Bool
558 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)