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 )
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 Nothing -- No parent object
136 (ATyCon tycon) -- Relevant TyCon
139 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name -> Name
140 mkWiredInDataConName built_in mod fs uniq datacon parent
141 = mkWiredInName mod (mkOccNameFS dataName fs) uniq
142 (Just parent) -- Name of parent TyCon
143 (ADataCon datacon) -- Relevant DataCon
146 charTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Char") charTyConKey charTyCon
147 charDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("C#") charDataConKey charDataCon charTyConName
148 intTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Int") intTyConKey intTyCon
149 intDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("I#") intDataConKey intDataCon intTyConName
151 boolTyConName = mkWiredInTyConName UserSyntax gHC_BASE FSLIT("Bool") boolTyConKey boolTyCon
152 falseDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("False") falseDataConKey falseDataCon boolTyConName
153 trueDataConName = mkWiredInDataConName UserSyntax gHC_BASE FSLIT("True") trueDataConKey trueDataCon boolTyConName
154 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_BASE FSLIT("[]") listTyConKey listTyCon
155 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE FSLIT("[]") nilDataConKey nilDataCon listTyConName
156 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_BASE FSLIT(":") consDataConKey consDataCon listTyConName
158 floatTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT FSLIT("Float") floatTyConKey floatTyCon
159 floatDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT FSLIT("F#") floatDataConKey floatDataCon floatTyConName
160 doubleTyConName = mkWiredInTyConName UserSyntax gHC_FLOAT FSLIT("Double") doubleTyConKey doubleTyCon
161 doubleDataConName = mkWiredInDataConName UserSyntax gHC_FLOAT FSLIT("D#") doubleDataConKey doubleDataCon doubleTyConName
163 parrTyConName = mkWiredInTyConName BuiltInSyntax gHC_PARR FSLIT("[::]") parrTyConKey parrTyCon
164 parrDataConName = mkWiredInDataConName UserSyntax gHC_PARR FSLIT("PArr") parrDataConKey parrDataCon parrTyConName
166 boolTyCon_RDR = nameRdrName boolTyConName
167 false_RDR = nameRdrName falseDataConName
168 true_RDR = nameRdrName trueDataConName
169 intTyCon_RDR = nameRdrName intTyConName
170 charTyCon_RDR = nameRdrName charTyConName
171 intDataCon_RDR = nameRdrName intDataConName
172 listTyCon_RDR = nameRdrName listTyConName
173 consDataCon_RDR = nameRdrName consDataConName
174 parrTyCon_RDR = nameRdrName parrTyConName
176 tySuperKindTyCon_RDR = nameRdrName tySuperKindTyConName
177 coSuperKindTyCon_RDR = nameRdrName coSuperKindTyConName
178 liftedTypeKindTyCon_RDR = nameRdrName liftedTypeKindTyConName
179 openTypeKindTyCon_RDR = nameRdrName openTypeKindTyConName
180 unliftedTypeKindTyCon_RDR = nameRdrName unliftedTypeKindTyConName
181 ubxTupleKindTyCon_RDR = nameRdrName ubxTupleKindTyConName
182 argTypeKindTyCon_RDR = nameRdrName argTypeKindTyConName
183 funKindTyCon_RDR = nameRdrName funKindTyConName
188 %************************************************************************
190 \subsection{mkWiredInTyCon}
192 %************************************************************************
195 pcNonRecDataTyCon = pcTyCon False NonRecursive
196 pcRecDataTyCon = pcTyCon False Recursive
198 pcTyCon is_enum is_rec name tyvars argvrcs cons
201 tycon = mkAlgTyCon name
202 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
205 [] -- No stupid theta
206 (DataTyCon cons is_enum)
207 [] -- No record selectors
209 True -- All the wired-in tycons have generics
210 False -- Not in GADT syntax
212 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
213 pcDataCon = pcDataConWithFixity False
215 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
216 -- The Name should be in the DataName name space; it's the name
217 -- of the DataCon itself.
219 -- The unique is the first of two free uniques;
220 -- the first is used for the datacon itself,
221 -- the second is used for the "worker name"
223 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
226 data_con = mkDataCon dc_name declared_infix
227 (map (const NotMarkedStrict) arg_tys)
228 [] -- No labelled fields
230 [] -- No existential type variables
231 [] -- No equality spec
234 [] -- No stupid theta
235 (mkDataConIds bogus_wrap_name wrk_name data_con)
238 mod = nameModule dc_name
239 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
240 wrk_key = incrUnique (nameUnique dc_name)
241 wrk_name = mkWiredInName mod wrk_occ wrk_key
242 (Just (tyConName tycon))
243 (AnId (dataConWorkId data_con)) UserSyntax
244 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
245 -- Wired-in types are too simple to need wrappers
249 %************************************************************************
251 \subsection[TysWiredIn-tuples]{The tuple types}
253 %************************************************************************
256 tupleTyCon :: Boxity -> Arity -> TyCon
257 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
258 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
259 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
261 tupleCon :: Boxity -> Arity -> DataCon
262 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
263 tupleCon Boxed i = snd (boxedTupleArr ! i)
264 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
266 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
267 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
268 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
270 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
271 mk_tuple boxity arity = (tycon, tuple_con)
273 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
274 mod = mkTupleModule boxity arity
275 tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
276 Nothing (ATyCon tycon) BuiltInSyntax
277 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
278 res_kind | isBoxed boxity = liftedTypeKind
279 | otherwise = ubxTupleKind
281 tyvars | isBoxed boxity = take arity alphaTyVars
282 | otherwise = take arity openAlphaTyVars
284 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
285 tyvar_tys = mkTyVarTys tyvars
286 dc_name = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
287 (Just tc_name) (ADataCon tuple_con) BuiltInSyntax
288 tc_uniq = mkTupleTyConUnique boxity arity
289 dc_uniq = mkTupleDataConUnique boxity arity
290 gen_info = True -- Tuples all have generics..
291 -- hmm: that's a *lot* of code
293 unitTyCon = tupleTyCon Boxed 0
294 unitDataCon = head (tyConDataCons unitTyCon)
295 unitDataConId = dataConWorkId unitDataCon
297 pairTyCon = tupleTyCon Boxed 2
299 unboxedSingletonTyCon = tupleTyCon Unboxed 1
300 unboxedSingletonDataCon = tupleCon Unboxed 1
302 unboxedPairTyCon = tupleTyCon Unboxed 2
303 unboxedPairDataCon = tupleCon Unboxed 2
306 %************************************************************************
308 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
310 %************************************************************************
313 -- The Void type is represented as a data type with no constructors
314 -- It's a built in type (i.e. there's no way to define it in Haskell;
315 -- the nearest would be
317 -- data Void = -- No constructors!
319 -- ) It's lifted; there is only one value of this
320 -- type, namely "void", whose semantics is just bottom.
322 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
329 charTy = mkTyConTy charTyCon
331 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
332 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
334 stringTy = mkListTy charTy -- convenience only
338 intTy = mkTyConTy intTyCon
340 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
341 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
345 floatTy = mkTyConTy floatTyCon
347 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
348 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
352 doubleTy = mkTyConTy doubleTyCon
354 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
355 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
359 %************************************************************************
361 \subsection[TysWiredIn-Bool]{The @Bool@ type}
363 %************************************************************************
365 An ordinary enumeration type, but deeply wired in. There are no
366 magical operations on @Bool@ (just the regular Prelude code).
368 {\em BEGIN IDLE SPECULATION BY SIMON}
370 This is not the only way to encode @Bool@. A more obvious coding makes
371 @Bool@ just a boxed up version of @Bool#@, like this:
374 data Bool = MkBool Bool#
377 Unfortunately, this doesn't correspond to what the Report says @Bool@
378 looks like! Furthermore, we get slightly less efficient code (I
379 think) with this coding. @gtInt@ would look like this:
382 gtInt :: Int -> Int -> Bool
383 gtInt x y = case x of I# x# ->
385 case (gtIntPrim x# y#) of
389 Notice that the result of the @gtIntPrim@ comparison has to be turned
390 into an integer (here called @b#@), and returned in a @MkBool@ box.
392 The @if@ expression would compile to this:
395 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
398 I think this code is a little less efficient than the previous code,
399 but I'm not certain. At all events, corresponding with the Report is
400 important. The interesting thing is that the language is expressive
401 enough to describe more than one alternative; and that a type doesn't
402 necessarily need to be a straightforwardly boxed version of its
403 primitive counterpart.
405 {\em END IDLE SPECULATION BY SIMON}
408 boolTy = mkTyConTy boolTyCon
410 boolTyCon = pcTyCon True NonRecursive boolTyConName
411 [] [] [falseDataCon, trueDataCon]
413 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
414 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
416 falseDataConId = dataConWorkId falseDataCon
417 trueDataConId = dataConWorkId trueDataCon
420 %************************************************************************
422 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
424 %************************************************************************
426 Special syntax, deeply wired in, but otherwise an ordinary algebraic
429 data [] a = [] | a : (List a)
431 data (,) a b = (,,) a b
436 mkListTy :: Type -> Type
437 mkListTy ty = mkTyConApp listTyCon [ty]
439 listTyCon = pcRecDataTyCon listTyConName
440 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
442 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
443 consDataCon = pcDataConWithFixity True {- Declared infix -}
445 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
446 -- Interesting: polymorphic recursion would help here.
447 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
448 -- gets the over-specific type (Type -> Type)
451 %************************************************************************
453 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
455 %************************************************************************
457 The tuple types are definitely magic, because they form an infinite
462 They have a special family of type constructors, of type @TyCon@
463 These contain the tycon arity, but don't require a Unique.
466 They have a special family of constructors, of type
467 @Id@. Again these contain their arity but don't need a Unique.
470 There should be a magic way of generating the info tables and
471 entry code for all tuples.
473 But at the moment we just compile a Haskell source
474 file\srcloc{lib/prelude/...} containing declarations like:
477 data Tuple2 a b = Tup2 a b
478 data Tuple3 a b c = Tup3 a b c
479 data Tuple4 a b c d = Tup4 a b c d
482 The print-names associated with the magic @Id@s for tuple constructors
483 ``just happen'' to be the same as those generated by these
487 The instance environment should have a magic way to know
488 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
489 so on. \ToDo{Not implemented yet.}
492 There should also be a way to generate the appropriate code for each
493 of these instances, but (like the info tables and entry code) it is
494 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
498 mkTupleTy :: Boxity -> Int -> [Type] -> Type
499 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
501 unitTy = mkTupleTy Boxed 0 []
504 %************************************************************************
506 \subsection[TysWiredIn-PArr]{The @[::]@ type}
508 %************************************************************************
510 Special syntax for parallel arrays needs some wired in definitions.
513 -- construct a type representing the application of the parallel array
516 mkPArrTy :: Type -> Type
517 mkPArrTy ty = mkTyConApp parrTyCon [ty]
519 -- represents the type constructor of parallel arrays
521 -- * this must match the definition in `PrelPArr'
523 -- NB: Although the constructor is given here, it will not be accessible in
524 -- user code as it is not in the environment of any compiled module except
528 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [(True, False)] [parrDataCon]
530 parrDataCon :: DataCon
531 parrDataCon = pcDataCon
533 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 = data_con
567 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
568 tyvar = head alphaTyVars
569 tyvarTys = replicate arity $ mkTyVarTy tyvar
570 nameStr = mkFastString ("MkPArr" ++ show arity)
571 name = mkWiredInName gHC_PARR (mkOccNameFS dataName nameStr) uniq
572 Nothing (ADataCon data_con) UserSyntax
573 uniq = mkPArrDataConUnique arity
575 -- checks whether a data constructor is a fake constructor for parallel arrays
577 isPArrFakeCon :: DataCon -> Bool
578 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)