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 -- The above warning supression flag is a temporary kludge.
15 -- While working on this module you are encouraged to remove it and fix
16 -- any warnings in the module. See
17 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
23 boolTy, boolTyCon, boolTyCon_RDR, boolTyConName,
24 trueDataCon, trueDataConId, true_RDR,
25 falseDataCon, falseDataConId, false_RDR,
27 charTyCon, charDataCon, charTyCon_RDR,
28 charTy, stringTy, charTyConName,
31 doubleTyCon, doubleDataCon, doubleTy, doubleTyConName,
33 floatTyCon, floatDataCon, floatTy, floatTyConName,
35 intTyCon, intDataCon, intTyCon_RDR, intDataCon_RDR, intTyConName,
38 listTyCon, nilDataCon, consDataCon,
39 listTyCon_RDR, consDataCon_RDR, listTyConName,
45 unitTyCon, unitDataCon, unitDataConId, pairTyCon,
46 unboxedSingletonTyCon, unboxedSingletonDataCon,
47 unboxedPairTyCon, unboxedPairDataCon,
49 boxedTupleArr, unboxedTupleArr,
55 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
56 parrTyCon_RDR, parrTyConName
59 import {-# SOURCE #-} MkId( mkDataConIds )
66 import Constants ( mAX_TUPLE_SIZE )
67 import Module ( Module )
68 import RdrName ( nameRdrName )
69 import Name ( Name, BuiltInSyntax(..), nameUnique, nameOccName,
70 nameModule, mkWiredInName )
71 import OccName ( mkOccNameFS, tcName, dataName, mkTupleOcc,
73 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
74 import Var ( TyVar, tyVarKind )
75 import TyCon ( TyCon, AlgTyConRhs(DataTyCon), tyConDataCons,
76 mkTupleTyCon, mkAlgTyCon, tyConName,
77 TyConParent(NoParentTyCon) )
79 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed,
82 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
84 import Coercion ( unsafeCoercionTyCon, symCoercionTyCon,
85 transCoercionTyCon, leftCoercionTyCon,
86 rightCoercionTyCon, instCoercionTyCon )
87 import TypeRep ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
88 import Unique ( incrUnique, mkTupleTyConUnique,
89 mkTupleDataConUnique, mkPArrDataConUnique )
94 alpha_tyvar = [alphaTyVar]
99 %************************************************************************
101 \subsection{Wired in type constructors}
103 %************************************************************************
105 If you change which things are wired in, make sure you change their
106 names in PrelNames, so they use wTcQual, wDataQual, etc
109 wiredInTyCons :: [TyCon] -- Excludes tuples
110 -- This list is used only to define PrelInfo.wiredInThings
112 -- It does not need to include kind constructors, because
113 -- all that wiredInThings does is to initialise the Name table,
114 -- and kind constructors don't appear in source code.
116 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
117 -- it's defined in GHC.Base, and there's only
118 -- one of it. We put it in wiredInTyCons so
119 -- that it'll pre-populate the name cache, so
120 -- the special case in lookupOrigNameCache
121 -- doesn't need to look out for it
129 , unsafeCoercionTyCon
139 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
140 mkWiredInTyConName built_in mod fs uniq tycon
141 = mkWiredInName mod (mkOccNameFS tcName fs) uniq
142 (ATyCon tycon) -- Relevant TyCon
145 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
146 mkWiredInDataConName built_in mod fs uniq datacon
147 = mkWiredInName mod (mkOccNameFS dataName fs) uniq
148 (ADataCon datacon) -- Relevant DataCon
151 charTyConName = mkWiredInTyConName UserSyntax gHC_BASE (fsLit "Char") charTyConKey charTyCon
152 charDataConName = mkWiredInDataConName UserSyntax gHC_BASE (fsLit "C#") charDataConKey charDataCon
153 intTyConName = mkWiredInTyConName UserSyntax gHC_BASE (fsLit "Int") intTyConKey intTyCon
154 intDataConName = mkWiredInDataConName UserSyntax gHC_BASE (fsLit "I#") intDataConKey intDataCon
156 boolTyConName = mkWiredInTyConName UserSyntax gHC_BOOL (fsLit "Bool") boolTyConKey boolTyCon
157 falseDataConName = mkWiredInDataConName UserSyntax gHC_BOOL (fsLit "False") falseDataConKey falseDataCon
158 trueDataConName = mkWiredInDataConName UserSyntax gHC_BOOL (fsLit "True") trueDataConKey trueDataCon
159 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_BASE (fsLit "[]") listTyConKey listTyCon
160 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE (fsLit "[]") nilDataConKey nilDataCon
161 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE (fsLit ":") consDataConKey consDataCon
163 floatTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT (fsLit "Float") floatTyConKey floatTyCon
164 floatDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT (fsLit "F#") floatDataConKey floatDataCon
165 doubleTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT (fsLit "Double") doubleTyConKey doubleTyCon
166 doubleDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT (fsLit "D#") doubleDataConKey doubleDataCon
168 parrTyConName = mkWiredInTyConName BuiltInSyntax gHC_PARR (fsLit "[::]") parrTyConKey parrTyCon
169 parrDataConName = mkWiredInDataConName UserSyntax gHC_PARR (fsLit "PArr") parrDataConKey parrDataCon
171 boolTyCon_RDR = nameRdrName boolTyConName
172 false_RDR = nameRdrName falseDataConName
173 true_RDR = nameRdrName trueDataConName
174 intTyCon_RDR = nameRdrName intTyConName
175 charTyCon_RDR = nameRdrName charTyConName
176 intDataCon_RDR = nameRdrName intDataConName
177 listTyCon_RDR = nameRdrName listTyConName
178 consDataCon_RDR = nameRdrName consDataConName
179 parrTyCon_RDR = nameRdrName parrTyConName
181 tySuperKindTyCon_RDR = nameRdrName tySuperKindTyConName
182 coSuperKindTyCon_RDR = nameRdrName coSuperKindTyConName
183 liftedTypeKindTyCon_RDR = nameRdrName liftedTypeKindTyConName
184 openTypeKindTyCon_RDR = nameRdrName openTypeKindTyConName
185 unliftedTypeKindTyCon_RDR = nameRdrName unliftedTypeKindTyConName
186 ubxTupleKindTyCon_RDR = nameRdrName ubxTupleKindTyConName
187 argTypeKindTyCon_RDR = nameRdrName argTypeKindTyConName
188 funKindTyCon_RDR = nameRdrName funKindTyConName
193 %************************************************************************
195 \subsection{mkWiredInTyCon}
197 %************************************************************************
200 pcNonRecDataTyCon = pcTyCon False NonRecursive
201 pcRecDataTyCon = pcTyCon False Recursive
203 pcTyCon is_enum is_rec name tyvars cons
206 tycon = mkAlgTyCon name
207 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
209 [] -- No stupid theta
210 (DataTyCon cons is_enum)
211 [] -- No record selectors
214 True -- All the wired-in tycons have generics
215 False -- Not in GADT syntax
217 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
218 pcDataCon = pcDataConWithFixity False
220 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
221 -- The Name should be in the DataName name space; it's the name
222 -- of the DataCon itself.
224 -- The unique is the first of two free uniques;
225 -- the first is used for the datacon itself,
226 -- the second is used for the "worker name"
228 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
231 data_con = mkDataCon dc_name declared_infix
232 (map (const NotMarkedStrict) arg_tys)
233 [] -- No labelled fields
235 [] -- No existential type variables
236 [] -- No equality spec
239 [] -- No stupid theta
240 (mkDataConIds bogus_wrap_name wrk_name data_con)
243 mod = nameModule dc_name
244 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
245 wrk_key = incrUnique (nameUnique dc_name)
246 wrk_name = mkWiredInName mod wrk_occ wrk_key
247 (AnId (dataConWorkId data_con)) UserSyntax
248 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
249 -- Wired-in types are too simple to need wrappers
253 %************************************************************************
255 \subsection[TysWiredIn-tuples]{The tuple types}
257 %************************************************************************
260 tupleTyCon :: Boxity -> Arity -> TyCon
261 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
262 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
263 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
265 tupleCon :: Boxity -> Arity -> DataCon
266 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
267 tupleCon Boxed i = snd (boxedTupleArr ! i)
268 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
270 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
271 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
272 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
274 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
275 mk_tuple boxity arity = (tycon, tuple_con)
277 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
278 mod = mkTupleModule boxity arity
279 tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
280 (ATyCon tycon) BuiltInSyntax
281 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
282 res_kind | isBoxed boxity = liftedTypeKind
283 | otherwise = ubxTupleKind
285 tyvars | isBoxed boxity = take arity alphaTyVars
286 | otherwise = take arity openAlphaTyVars
288 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
289 tyvar_tys = mkTyVarTys tyvars
290 dc_name = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
291 (ADataCon tuple_con) BuiltInSyntax
292 tc_uniq = mkTupleTyConUnique boxity arity
293 dc_uniq = mkTupleDataConUnique boxity arity
294 gen_info = True -- Tuples all have generics..
295 -- hmm: that's a *lot* of code
297 unitTyCon = tupleTyCon Boxed 0
298 unitDataCon = head (tyConDataCons unitTyCon)
299 unitDataConId = dataConWorkId unitDataCon
301 pairTyCon = tupleTyCon Boxed 2
303 unboxedSingletonTyCon = tupleTyCon Unboxed 1
304 unboxedSingletonDataCon = tupleCon Unboxed 1
306 unboxedPairTyCon = tupleTyCon Unboxed 2
307 unboxedPairDataCon = tupleCon Unboxed 2
310 %************************************************************************
312 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
314 %************************************************************************
317 charTy = mkTyConTy charTyCon
319 charTyCon = pcNonRecDataTyCon charTyConName [] [charDataCon]
320 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
322 stringTy = mkListTy charTy -- convenience only
326 intTy = mkTyConTy intTyCon
328 intTyCon = pcNonRecDataTyCon intTyConName [] [intDataCon]
329 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
333 floatTy = mkTyConTy floatTyCon
335 floatTyCon = pcNonRecDataTyCon floatTyConName [] [floatDataCon]
336 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
340 doubleTy = mkTyConTy doubleTyCon
342 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [doubleDataCon]
343 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
347 %************************************************************************
349 \subsection[TysWiredIn-Bool]{The @Bool@ type}
351 %************************************************************************
353 An ordinary enumeration type, but deeply wired in. There are no
354 magical operations on @Bool@ (just the regular Prelude code).
356 {\em BEGIN IDLE SPECULATION BY SIMON}
358 This is not the only way to encode @Bool@. A more obvious coding makes
359 @Bool@ just a boxed up version of @Bool#@, like this:
362 data Bool = MkBool Bool#
365 Unfortunately, this doesn't correspond to what the Report says @Bool@
366 looks like! Furthermore, we get slightly less efficient code (I
367 think) with this coding. @gtInt@ would look like this:
370 gtInt :: Int -> Int -> Bool
371 gtInt x y = case x of I# x# ->
373 case (gtIntPrim x# y#) of
377 Notice that the result of the @gtIntPrim@ comparison has to be turned
378 into an integer (here called @b#@), and returned in a @MkBool@ box.
380 The @if@ expression would compile to this:
383 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
386 I think this code is a little less efficient than the previous code,
387 but I'm not certain. At all events, corresponding with the Report is
388 important. The interesting thing is that the language is expressive
389 enough to describe more than one alternative; and that a type doesn't
390 necessarily need to be a straightforwardly boxed version of its
391 primitive counterpart.
393 {\em END IDLE SPECULATION BY SIMON}
396 boolTy = mkTyConTy boolTyCon
398 boolTyCon = pcTyCon True NonRecursive boolTyConName
399 [] [falseDataCon, trueDataCon]
401 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
402 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
404 falseDataConId = dataConWorkId falseDataCon
405 trueDataConId = dataConWorkId trueDataCon
408 %************************************************************************
410 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
412 %************************************************************************
414 Special syntax, deeply wired in, but otherwise an ordinary algebraic
417 data [] a = [] | a : (List a)
419 data (,) a b = (,,) a b
424 mkListTy :: Type -> Type
425 mkListTy ty = mkTyConApp listTyCon [ty]
427 listTyCon = pcRecDataTyCon listTyConName alpha_tyvar [nilDataCon, consDataCon]
429 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
430 consDataCon = pcDataConWithFixity True {- Declared infix -}
432 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
433 -- Interesting: polymorphic recursion would help here.
434 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
435 -- gets the over-specific type (Type -> Type)
438 %************************************************************************
440 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
442 %************************************************************************
444 The tuple types are definitely magic, because they form an infinite
449 They have a special family of type constructors, of type @TyCon@
450 These contain the tycon arity, but don't require a Unique.
453 They have a special family of constructors, of type
454 @Id@. Again these contain their arity but don't need a Unique.
457 There should be a magic way of generating the info tables and
458 entry code for all tuples.
460 But at the moment we just compile a Haskell source
461 file\srcloc{lib/prelude/...} containing declarations like:
464 data Tuple2 a b = Tup2 a b
465 data Tuple3 a b c = Tup3 a b c
466 data Tuple4 a b c d = Tup4 a b c d
469 The print-names associated with the magic @Id@s for tuple constructors
470 ``just happen'' to be the same as those generated by these
474 The instance environment should have a magic way to know
475 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
476 so on. \ToDo{Not implemented yet.}
479 There should also be a way to generate the appropriate code for each
480 of these instances, but (like the info tables and entry code) it is
481 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
485 mkTupleTy :: Boxity -> Int -> [Type] -> Type
486 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
488 unitTy = mkTupleTy Boxed 0 []
491 %************************************************************************
493 \subsection[TysWiredIn-PArr]{The @[::]@ type}
495 %************************************************************************
497 Special syntax for parallel arrays needs some wired in definitions.
500 -- construct a type representing the application of the parallel array
503 mkPArrTy :: Type -> Type
504 mkPArrTy ty = mkTyConApp parrTyCon [ty]
506 -- represents the type constructor of parallel arrays
508 -- * this must match the definition in `PrelPArr'
510 -- NB: Although the constructor is given here, it will not be accessible in
511 -- user code as it is not in the environment of any compiled module except
515 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [parrDataCon]
517 parrDataCon :: DataCon
518 parrDataCon = pcDataCon
520 alpha_tyvar -- forall'ed type variables
521 [intPrimTy, -- 1st argument: Int#
522 mkTyConApp -- 2nd argument: Array# a
527 -- check whether a type constructor is the constructor for parallel arrays
529 isPArrTyCon :: TyCon -> Bool
530 isPArrTyCon tc = tyConName tc == parrTyConName
532 -- fake array constructors
534 -- * these constructors are never really used to represent array values;
535 -- however, they are very convenient during desugaring (and, in particular,
536 -- in the pattern matching compiler) to treat array pattern just like
537 -- yet another constructor pattern
539 parrFakeCon :: Arity -> DataCon
540 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
541 parrFakeCon i = parrFakeConArr!i
543 -- pre-defined set of constructors
545 parrFakeConArr :: Array Int DataCon
546 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
547 | i <- [0..mAX_TUPLE_SIZE]]
549 -- build a fake parallel array constructor for the given arity
551 mkPArrFakeCon :: Int -> DataCon
552 mkPArrFakeCon arity = data_con
554 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
555 tyvar = head alphaTyVars
556 tyvarTys = replicate arity $ mkTyVarTy tyvar
557 nameStr = mkFastString ("MkPArr" ++ show arity)
558 name = mkWiredInName gHC_PARR (mkOccNameFS dataName nameStr) uniq
559 (ADataCon data_con) UserSyntax
560 uniq = mkPArrDataConUnique arity
562 -- checks whether a data constructor is a fake constructor for parallel arrays
564 isPArrFakeCon :: DataCon -> Bool
565 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)