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,
47 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
48 parrTyCon_RDR, parrTyConName
51 #include "HsVersions.h"
53 import {-# SOURCE #-} MkId( mkDataConIds )
60 import Constants ( mAX_TUPLE_SIZE )
61 import Module ( Module )
62 import RdrName ( nameRdrName )
63 import Name ( Name, BuiltInSyntax(..), nameUnique, nameOccName,
64 nameModule, mkWiredInName )
65 import OccName ( mkOccNameFS, tcName, dataName, mkTupleOcc,
67 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
68 import Var ( TyVar, tyVarKind )
69 import TyCon ( TyCon, AlgTyConRhs(DataTyCon), tyConDataCons,
70 mkTupleTyCon, mkAlgTyCon, tyConName,
71 AlgTyConParent(NoParentTyCon) )
73 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed,
76 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
78 import Coercion ( unsafeCoercionTyCon, symCoercionTyCon,
79 transCoercionTyCon, leftCoercionTyCon,
80 rightCoercionTyCon, instCoercionTyCon )
81 import TypeRep ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
82 import Unique ( incrUnique, mkTupleTyConUnique,
83 mkTupleDataConUnique, mkPArrDataConUnique )
88 alpha_tyvar = [alphaTyVar]
93 %************************************************************************
95 \subsection{Wired in type constructors}
97 %************************************************************************
99 If you change which things are wired in, make sure you change their
100 names in PrelNames, so they use wTcQual, wDataQual, etc
103 wiredInTyCons :: [TyCon] -- Excludes tuples
104 -- This list is used only to define PrelInfo.wiredInThings
106 -- It does not need to include kind constructors, because
107 -- all that wiredInThings does is to initialise the Name table,
108 -- and kind constructors don't appear in source code.
110 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
111 -- it's defined in GHC.Base, and there's only
112 -- one of it. We put it in wiredInTyCons so
113 -- that it'll pre-populate the name cache, so
114 -- the special case in lookupOrigNameCache
115 -- doesn't need to look out for it
123 , unsafeCoercionTyCon
133 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
134 mkWiredInTyConName built_in mod fs uniq tycon
135 = mkWiredInName mod (mkOccNameFS tcName fs) uniq
136 (ATyCon tycon) -- Relevant TyCon
139 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
140 mkWiredInDataConName built_in mod fs uniq datacon
141 = mkWiredInName mod (mkOccNameFS dataName fs) uniq
142 (ADataCon datacon) -- Relevant DataCon
145 charTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Char") charTyConKey charTyCon
146 charDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("C#") charDataConKey charDataCon
147 intTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Int") intTyConKey intTyCon
148 intDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("I#") intDataConKey intDataCon
150 boolTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Bool") boolTyConKey boolTyCon
151 falseDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("False") falseDataConKey falseDataCon
152 trueDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("True") trueDataConKey trueDataCon
153 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_BASE FSLIT("[]") listTyConKey listTyCon
154 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE FSLIT("[]") nilDataConKey nilDataCon
155 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE FSLIT(":") consDataConKey consDataCon
157 floatTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT FSLIT("Float") floatTyConKey floatTyCon
158 floatDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT FSLIT("F#") floatDataConKey floatDataCon
159 doubleTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT FSLIT("Double") doubleTyConKey doubleTyCon
160 doubleDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT FSLIT("D#") doubleDataConKey doubleDataCon
162 parrTyConName = mkWiredInTyConName BuiltInSyntax gHC_PARR FSLIT("[::]") parrTyConKey parrTyCon
163 parrDataConName = mkWiredInDataConName UserSyntax gHC_PARR FSLIT("PArr") parrDataConKey parrDataCon
165 boolTyCon_RDR = nameRdrName boolTyConName
166 false_RDR = nameRdrName falseDataConName
167 true_RDR = nameRdrName trueDataConName
168 intTyCon_RDR = nameRdrName intTyConName
169 charTyCon_RDR = nameRdrName charTyConName
170 intDataCon_RDR = nameRdrName intDataConName
171 listTyCon_RDR = nameRdrName listTyConName
172 consDataCon_RDR = nameRdrName consDataConName
173 parrTyCon_RDR = nameRdrName parrTyConName
175 tySuperKindTyCon_RDR = nameRdrName tySuperKindTyConName
176 coSuperKindTyCon_RDR = nameRdrName coSuperKindTyConName
177 liftedTypeKindTyCon_RDR = nameRdrName liftedTypeKindTyConName
178 openTypeKindTyCon_RDR = nameRdrName openTypeKindTyConName
179 unliftedTypeKindTyCon_RDR = nameRdrName unliftedTypeKindTyConName
180 ubxTupleKindTyCon_RDR = nameRdrName ubxTupleKindTyConName
181 argTypeKindTyCon_RDR = nameRdrName argTypeKindTyConName
182 funKindTyCon_RDR = nameRdrName funKindTyConName
187 %************************************************************************
189 \subsection{mkWiredInTyCon}
191 %************************************************************************
194 pcNonRecDataTyCon = pcTyCon False NonRecursive
195 pcRecDataTyCon = pcTyCon False Recursive
197 pcTyCon is_enum is_rec name tyvars cons
200 tycon = mkAlgTyCon name
201 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
203 [] -- No stupid theta
204 (DataTyCon cons is_enum)
205 [] -- No record selectors
208 True -- All the wired-in tycons have generics
209 False -- Not in GADT syntax
211 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
212 pcDataCon = pcDataConWithFixity False
214 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
215 -- The Name should be in the DataName name space; it's the name
216 -- of the DataCon itself.
218 -- The unique is the first of two free uniques;
219 -- the first is used for the datacon itself,
220 -- the second is used for the "worker name"
222 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
225 data_con = mkDataCon dc_name declared_infix
226 (map (const NotMarkedStrict) arg_tys)
227 [] -- No labelled fields
229 [] -- No existential type variables
230 [] -- No equality spec
233 [] -- No stupid theta
234 (mkDataConIds bogus_wrap_name wrk_name data_con)
237 mod = nameModule dc_name
238 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
239 wrk_key = incrUnique (nameUnique dc_name)
240 wrk_name = mkWiredInName mod wrk_occ wrk_key
241 (AnId (dataConWorkId data_con)) UserSyntax
242 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
243 -- Wired-in types are too simple to need wrappers
247 %************************************************************************
249 \subsection[TysWiredIn-tuples]{The tuple types}
251 %************************************************************************
254 tupleTyCon :: Boxity -> Arity -> TyCon
255 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
256 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
257 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
259 tupleCon :: Boxity -> Arity -> DataCon
260 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
261 tupleCon Boxed i = snd (boxedTupleArr ! i)
262 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
264 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
265 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
266 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
268 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
269 mk_tuple boxity arity = (tycon, tuple_con)
271 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
272 mod = mkTupleModule boxity arity
273 tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
274 (ATyCon tycon) BuiltInSyntax
275 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
276 res_kind | isBoxed boxity = liftedTypeKind
277 | otherwise = ubxTupleKind
279 tyvars | isBoxed boxity = take arity alphaTyVars
280 | otherwise = take arity openAlphaTyVars
282 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
283 tyvar_tys = mkTyVarTys tyvars
284 dc_name = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
285 (ADataCon tuple_con) BuiltInSyntax
286 tc_uniq = mkTupleTyConUnique boxity arity
287 dc_uniq = mkTupleDataConUnique boxity arity
288 gen_info = True -- Tuples all have generics..
289 -- hmm: that's a *lot* of code
291 unitTyCon = tupleTyCon Boxed 0
292 unitDataCon = head (tyConDataCons unitTyCon)
293 unitDataConId = dataConWorkId unitDataCon
295 pairTyCon = tupleTyCon Boxed 2
297 unboxedSingletonTyCon = tupleTyCon Unboxed 1
298 unboxedSingletonDataCon = tupleCon Unboxed 1
300 unboxedPairTyCon = tupleTyCon Unboxed 2
301 unboxedPairDataCon = tupleCon Unboxed 2
304 %************************************************************************
306 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
308 %************************************************************************
311 -- The Void type is represented as a data type with no constructors
312 -- It's a built in type (i.e. there's no way to define it in Haskell;
313 -- the nearest would be
315 -- data Void = -- No constructors!
317 -- ) It's lifted; there is only one value of this
318 -- type, namely "void", whose semantics is just bottom.
320 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
327 charTy = mkTyConTy charTyCon
329 charTyCon = pcNonRecDataTyCon charTyConName [] [charDataCon]
330 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
332 stringTy = mkListTy charTy -- convenience only
336 intTy = mkTyConTy intTyCon
338 intTyCon = pcNonRecDataTyCon intTyConName [] [intDataCon]
339 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
343 floatTy = mkTyConTy floatTyCon
345 floatTyCon = pcNonRecDataTyCon floatTyConName [] [floatDataCon]
346 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
350 doubleTy = mkTyConTy doubleTyCon
352 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [doubleDataCon]
353 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
357 %************************************************************************
359 \subsection[TysWiredIn-Bool]{The @Bool@ type}
361 %************************************************************************
363 An ordinary enumeration type, but deeply wired in. There are no
364 magical operations on @Bool@ (just the regular Prelude code).
366 {\em BEGIN IDLE SPECULATION BY SIMON}
368 This is not the only way to encode @Bool@. A more obvious coding makes
369 @Bool@ just a boxed up version of @Bool#@, like this:
372 data Bool = MkBool Bool#
375 Unfortunately, this doesn't correspond to what the Report says @Bool@
376 looks like! Furthermore, we get slightly less efficient code (I
377 think) with this coding. @gtInt@ would look like this:
380 gtInt :: Int -> Int -> Bool
381 gtInt x y = case x of I# x# ->
383 case (gtIntPrim x# y#) of
387 Notice that the result of the @gtIntPrim@ comparison has to be turned
388 into an integer (here called @b#@), and returned in a @MkBool@ box.
390 The @if@ expression would compile to this:
393 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
396 I think this code is a little less efficient than the previous code,
397 but I'm not certain. At all events, corresponding with the Report is
398 important. The interesting thing is that the language is expressive
399 enough to describe more than one alternative; and that a type doesn't
400 necessarily need to be a straightforwardly boxed version of its
401 primitive counterpart.
403 {\em END IDLE SPECULATION BY SIMON}
406 boolTy = mkTyConTy boolTyCon
408 boolTyCon = pcTyCon True NonRecursive boolTyConName
409 [] [falseDataCon, trueDataCon]
411 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
412 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
414 falseDataConId = dataConWorkId falseDataCon
415 trueDataConId = dataConWorkId trueDataCon
418 %************************************************************************
420 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
422 %************************************************************************
424 Special syntax, deeply wired in, but otherwise an ordinary algebraic
427 data [] a = [] | a : (List a)
429 data (,) a b = (,,) a b
434 mkListTy :: Type -> Type
435 mkListTy ty = mkTyConApp listTyCon [ty]
437 listTyCon = pcRecDataTyCon listTyConName alpha_tyvar [nilDataCon, consDataCon]
439 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
440 consDataCon = pcDataConWithFixity True {- Declared infix -}
442 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
443 -- Interesting: polymorphic recursion would help here.
444 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
445 -- gets the over-specific type (Type -> Type)
448 %************************************************************************
450 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
452 %************************************************************************
454 The tuple types are definitely magic, because they form an infinite
459 They have a special family of type constructors, of type @TyCon@
460 These contain the tycon arity, but don't require a Unique.
463 They have a special family of constructors, of type
464 @Id@. Again these contain their arity but don't need a Unique.
467 There should be a magic way of generating the info tables and
468 entry code for all tuples.
470 But at the moment we just compile a Haskell source
471 file\srcloc{lib/prelude/...} containing declarations like:
474 data Tuple2 a b = Tup2 a b
475 data Tuple3 a b c = Tup3 a b c
476 data Tuple4 a b c d = Tup4 a b c d
479 The print-names associated with the magic @Id@s for tuple constructors
480 ``just happen'' to be the same as those generated by these
484 The instance environment should have a magic way to know
485 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
486 so on. \ToDo{Not implemented yet.}
489 There should also be a way to generate the appropriate code for each
490 of these instances, but (like the info tables and entry code) it is
491 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
495 mkTupleTy :: Boxity -> Int -> [Type] -> Type
496 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
498 unitTy = mkTupleTy Boxed 0 []
501 %************************************************************************
503 \subsection[TysWiredIn-PArr]{The @[::]@ type}
505 %************************************************************************
507 Special syntax for parallel arrays needs some wired in definitions.
510 -- construct a type representing the application of the parallel array
513 mkPArrTy :: Type -> Type
514 mkPArrTy ty = mkTyConApp parrTyCon [ty]
516 -- represents the type constructor of parallel arrays
518 -- * this must match the definition in `PrelPArr'
520 -- NB: Although the constructor is given here, it will not be accessible in
521 -- user code as it is not in the environment of any compiled module except
525 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [parrDataCon]
527 parrDataCon :: DataCon
528 parrDataCon = pcDataCon
530 alpha_tyvar -- forall'ed type variables
531 [intPrimTy, -- 1st argument: Int#
532 mkTyConApp -- 2nd argument: Array# a
537 -- check whether a type constructor is the constructor for parallel arrays
539 isPArrTyCon :: TyCon -> Bool
540 isPArrTyCon tc = tyConName tc == parrTyConName
542 -- fake array constructors
544 -- * these constructors are never really used to represent array values;
545 -- however, they are very convenient during desugaring (and, in particular,
546 -- in the pattern matching compiler) to treat array pattern just like
547 -- yet another constructor pattern
549 parrFakeCon :: Arity -> DataCon
550 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
551 parrFakeCon i = parrFakeConArr!i
553 -- pre-defined set of constructors
555 parrFakeConArr :: Array Int DataCon
556 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
557 | i <- [0..mAX_TUPLE_SIZE]]
559 -- build a fake parallel array constructor for the given arity
561 mkPArrFakeCon :: Int -> DataCon
562 mkPArrFakeCon arity = data_con
564 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
565 tyvar = head alphaTyVars
566 tyvarTys = replicate arity $ mkTyVarTy tyvar
567 nameStr = mkFastString ("MkPArr" ++ show arity)
568 name = mkWiredInName gHC_PARR (mkOccNameFS dataName nameStr) uniq
569 (ADataCon data_con) UserSyntax
570 uniq = mkPArrDataConUnique arity
572 -- checks whether a data constructor is a fake constructor for parallel arrays
574 isPArrFakeCon :: DataCon -> Bool
575 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)