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,
26 floatTyCon, floatDataCon, floatTy,
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, nameUnique, nameOccName,
64 nameModule, mkWiredInName )
65 import OccName ( mkOccFS, tcName, dataName, mkTupleOcc, mkDataConWorkerOcc )
66 import DataCon ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
67 import Var ( TyVar, tyVarKind )
68 import TyCon ( TyCon, AlgTyConFlavour(..), DataConDetails(..), tyConDataCons,
69 mkTupleTyCon, mkAlgTyCon, tyConName
72 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed, StrictnessMark(..) )
74 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
75 mkArrowKinds, liftedTypeKind, unliftedTypeKind,
76 ThetaType, TyThing(..) )
77 import Unique ( incrUnique, mkTupleTyConUnique,
78 mkTupleDataConUnique, mkPArrDataConUnique )
84 alpha_tyvar = [alphaTyVar]
89 %************************************************************************
91 \subsection{Wired in type constructors}
93 %************************************************************************
95 If you change which things are wired in, make sure you change their
96 names in PrelNames, so they use wTcQual, wDataQual, etc
99 wiredInTyCons :: [TyCon] -- Excludes tuples
100 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
101 -- it's defined in GHC.Base, and there's only
102 -- one of it. We put it in wiredInTyCons so
103 -- that it'll pre-populate the name cache, so
104 -- the special case in lookupOrigNameCache
105 -- doesn't need to look out for it
117 mkWiredInTyConName :: Module -> FastString -> Unique -> TyCon -> Name
118 mkWiredInTyConName mod fs uniq tycon
119 = mkWiredInName mod (mkOccFS tcName fs) uniq
120 Nothing -- No parent object
121 (ATyCon tycon) -- Relevant TyCon
123 mkWiredInDataConName :: Module -> FastString -> Unique -> DataCon -> Name -> Name
124 mkWiredInDataConName mod fs uniq datacon parent
125 = mkWiredInName mod (mkOccFS dataName fs) uniq
126 (Just parent) -- Name of parent TyCon
127 (ADataCon datacon) -- Relevant DataCon
129 charTyConName = mkWiredInTyConName pREL_BASE FSLIT("Char") charTyConKey charTyCon
130 charDataConName = mkWiredInDataConName pREL_BASE FSLIT("C#") charDataConKey charDataCon charTyConName
131 intTyConName = mkWiredInTyConName pREL_BASE FSLIT("Int") intTyConKey intTyCon
132 intDataConName = mkWiredInDataConName pREL_BASE FSLIT("I#") intDataConKey intDataCon intTyConName
134 boolTyConName = mkWiredInTyConName pREL_BASE FSLIT("Bool") boolTyConKey boolTyCon
135 falseDataConName = mkWiredInDataConName pREL_BASE FSLIT("False") falseDataConKey falseDataCon boolTyConName
136 trueDataConName = mkWiredInDataConName pREL_BASE FSLIT("True") trueDataConKey trueDataCon boolTyConName
137 listTyConName = mkWiredInTyConName pREL_BASE FSLIT("[]") listTyConKey listTyCon
138 nilDataConName = mkWiredInDataConName pREL_BASE FSLIT("[]") nilDataConKey nilDataCon listTyConName
139 consDataConName = mkWiredInDataConName pREL_BASE FSLIT(":") consDataConKey consDataCon listTyConName
141 floatTyConName = mkWiredInTyConName pREL_FLOAT FSLIT("Float") floatTyConKey floatTyCon
142 floatDataConName = mkWiredInDataConName pREL_FLOAT FSLIT("F#") floatDataConKey floatDataCon floatTyConName
143 doubleTyConName = mkWiredInTyConName pREL_FLOAT FSLIT("Double") doubleTyConKey doubleTyCon
144 doubleDataConName = mkWiredInDataConName pREL_FLOAT FSLIT("D#") doubleDataConKey doubleDataCon doubleTyConName
146 parrTyConName = mkWiredInTyConName pREL_PARR FSLIT("[::]") parrTyConKey parrTyCon
147 parrDataConName = mkWiredInDataConName pREL_PARR FSLIT("PArr") parrDataConKey parrDataCon parrTyConName
149 boolTyCon_RDR = nameRdrName boolTyConName
150 false_RDR = nameRdrName falseDataConName
151 true_RDR = nameRdrName trueDataConName
152 intTyCon_RDR = nameRdrName intTyConName
153 charTyCon_RDR = nameRdrName charTyConName
154 intDataCon_RDR = nameRdrName intDataConName
155 listTyCon_RDR = nameRdrName listTyConName
156 consDataCon_RDR = nameRdrName consDataConName
157 parrTyCon_RDR = nameRdrName parrTyConName
161 %************************************************************************
163 \subsection{mkWiredInTyCon}
165 %************************************************************************
168 pcNonRecDataTyCon = pcTyCon False NonRecursive
169 pcRecDataTyCon = pcTyCon False Recursive
171 pcTyCon is_enum is_rec name tyvars argvrcs cons
174 tycon = mkAlgTyCon name
175 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
180 [] -- No record selectors
183 True -- All the wired-in tycons have generics
185 pcDataCon :: Name -> [TyVar] -> ThetaType -> [Type] -> TyCon -> DataCon
186 -- The Name should be in the DataName name space; it's the name
187 -- of the DataCon itself.
189 -- The unique is the first of two free uniques;
190 -- the first is used for the datacon itself,
191 -- the second is used for the "worker name"
193 pcDataCon dc_name tyvars context arg_tys tycon
196 data_con = mkDataCon dc_name
197 (map (const NotMarkedStrict) arg_tys)
198 [{- No labelled fields -}]
199 tyvars context [] [] arg_tys tycon
200 (mkDataConIds bogus_wrap_name wrk_name data_con)
202 mod = nameModule dc_name
203 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
204 wrk_key = incrUnique (nameUnique dc_name)
205 wrk_name = mkWiredInName mod wrk_occ wrk_key
206 (Just (tyConName tycon))
207 (AnId (dataConWorkId data_con))
208 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
209 -- Wired-in types are too simple to need wrappers
213 %************************************************************************
215 \subsection[TysWiredIn-tuples]{The tuple types}
217 %************************************************************************
220 tupleTyCon :: Boxity -> Arity -> TyCon
221 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
222 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
223 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
225 tupleCon :: Boxity -> Arity -> DataCon
226 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
227 tupleCon Boxed i = snd (boxedTupleArr ! i)
228 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
230 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
231 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
232 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
234 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
235 mk_tuple boxity arity = (tycon, tuple_con)
237 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
238 mod = mkTupleModule boxity arity
239 tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
240 Nothing (ATyCon tycon)
241 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
242 res_kind | isBoxed boxity = liftedTypeKind
243 | otherwise = unliftedTypeKind
245 tyvars | isBoxed boxity = take arity alphaTyVars
246 | otherwise = take arity openAlphaTyVars
248 tuple_con = pcDataCon dc_name tyvars [] tyvar_tys tycon
249 tyvar_tys = mkTyVarTys tyvars
250 dc_name = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
251 (Just tc_name) (ADataCon tuple_con)
252 tc_uniq = mkTupleTyConUnique boxity arity
253 dc_uniq = mkTupleDataConUnique boxity arity
254 gen_info = True -- Tuples all have generics..
255 -- hmm: that's a *lot* of code
257 unitTyCon = tupleTyCon Boxed 0
258 unitDataCon = head (tyConDataCons unitTyCon)
259 unitDataConId = dataConWorkId unitDataCon
261 pairTyCon = tupleTyCon Boxed 2
263 unboxedSingletonTyCon = tupleTyCon Unboxed 1
264 unboxedSingletonDataCon = tupleCon Unboxed 1
266 unboxedPairTyCon = tupleTyCon Unboxed 2
267 unboxedPairDataCon = tupleCon Unboxed 2
270 %************************************************************************
272 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
274 %************************************************************************
277 -- The Void type is represented as a data type with no constructors
278 -- It's a built in type (i.e. there's no way to define it in Haskell;
279 -- the nearest would be
281 -- data Void = -- No constructors!
283 -- ) It's lifted; there is only one value of this
284 -- type, namely "void", whose semantics is just bottom.
286 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
293 charTy = mkTyConTy charTyCon
295 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
296 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
298 stringTy = mkListTy charTy -- convenience only
302 intTy = mkTyConTy intTyCon
304 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
305 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
309 floatTy = mkTyConTy floatTyCon
311 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
312 floatDataCon = pcDataCon floatDataConName [] [] [floatPrimTy] floatTyCon
316 doubleTy = mkTyConTy doubleTyCon
318 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
319 doubleDataCon = pcDataCon doubleDataConName [] [] [doublePrimTy] doubleTyCon
323 %************************************************************************
325 \subsection[TysWiredIn-Bool]{The @Bool@ type}
327 %************************************************************************
329 An ordinary enumeration type, but deeply wired in. There are no
330 magical operations on @Bool@ (just the regular Prelude code).
332 {\em BEGIN IDLE SPECULATION BY SIMON}
334 This is not the only way to encode @Bool@. A more obvious coding makes
335 @Bool@ just a boxed up version of @Bool#@, like this:
338 data Bool = MkBool Bool#
341 Unfortunately, this doesn't correspond to what the Report says @Bool@
342 looks like! Furthermore, we get slightly less efficient code (I
343 think) with this coding. @gtInt@ would look like this:
346 gtInt :: Int -> Int -> Bool
347 gtInt x y = case x of I# x# ->
349 case (gtIntPrim x# y#) of
353 Notice that the result of the @gtIntPrim@ comparison has to be turned
354 into an integer (here called @b#@), and returned in a @MkBool@ box.
356 The @if@ expression would compile to this:
359 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
362 I think this code is a little less efficient than the previous code,
363 but I'm not certain. At all events, corresponding with the Report is
364 important. The interesting thing is that the language is expressive
365 enough to describe more than one alternative; and that a type doesn't
366 necessarily need to be a straightforwardly boxed version of its
367 primitive counterpart.
369 {\em END IDLE SPECULATION BY SIMON}
372 boolTy = mkTyConTy boolTyCon
374 boolTyCon = pcTyCon True NonRecursive boolTyConName
375 [] [] [falseDataCon, trueDataCon]
377 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
378 trueDataCon = pcDataCon trueDataConName [] [] [] boolTyCon
380 falseDataConId = dataConWorkId falseDataCon
381 trueDataConId = dataConWorkId trueDataCon
384 %************************************************************************
386 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
388 %************************************************************************
390 Special syntax, deeply wired in, but otherwise an ordinary algebraic
393 data [] a = [] | a : (List a)
395 data (,) a b = (,,) a b
400 mkListTy :: Type -> Type
401 mkListTy ty = mkTyConApp listTyCon [ty]
403 listTyCon = pcRecDataTyCon listTyConName
404 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
406 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
407 consDataCon = pcDataCon consDataConName
408 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
409 -- Interesting: polymorphic recursion would help here.
410 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
411 -- gets the over-specific type (Type -> Type)
414 %************************************************************************
416 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
418 %************************************************************************
420 The tuple types are definitely magic, because they form an infinite
425 They have a special family of type constructors, of type @TyCon@
426 These contain the tycon arity, but don't require a Unique.
429 They have a special family of constructors, of type
430 @Id@. Again these contain their arity but don't need a Unique.
433 There should be a magic way of generating the info tables and
434 entry code for all tuples.
436 But at the moment we just compile a Haskell source
437 file\srcloc{lib/prelude/...} containing declarations like:
440 data Tuple2 a b = Tup2 a b
441 data Tuple3 a b c = Tup3 a b c
442 data Tuple4 a b c d = Tup4 a b c d
445 The print-names associated with the magic @Id@s for tuple constructors
446 ``just happen'' to be the same as those generated by these
450 The instance environment should have a magic way to know
451 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
452 so on. \ToDo{Not implemented yet.}
455 There should also be a way to generate the appropriate code for each
456 of these instances, but (like the info tables and entry code) it is
457 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
461 mkTupleTy :: Boxity -> Int -> [Type] -> Type
462 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
464 unitTy = mkTupleTy Boxed 0 []
467 %************************************************************************
469 \subsection[TysWiredIn-PArr]{The @[::]@ type}
471 %************************************************************************
473 Special syntax for parallel arrays needs some wired in definitions.
476 -- construct a type representing the application of the parallel array
479 mkPArrTy :: Type -> Type
480 mkPArrTy ty = mkTyConApp parrTyCon [ty]
482 -- represents the type constructor of parallel arrays
484 -- * this must match the definition in `PrelPArr'
486 -- NB: Although the constructor is given here, it will not be accessible in
487 -- user code as it is not in the environment of any compiled module except
491 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [(True, False)] [parrDataCon]
493 parrDataCon :: DataCon
494 parrDataCon = pcDataCon
496 alpha_tyvar -- forall'ed type variables
498 [intPrimTy, -- 1st argument: Int#
499 mkTyConApp -- 2nd argument: Array# a
504 -- check whether a type constructor is the constructor for parallel arrays
506 isPArrTyCon :: TyCon -> Bool
507 isPArrTyCon tc = tyConName tc == parrTyConName
509 -- fake array constructors
511 -- * these constructors are never really used to represent array values;
512 -- however, they are very convenient during desugaring (and, in particular,
513 -- in the pattern matching compiler) to treat array pattern just like
514 -- yet another constructor pattern
516 parrFakeCon :: Arity -> DataCon
517 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
518 parrFakeCon i = parrFakeConArr!i
520 -- pre-defined set of constructors
522 parrFakeConArr :: Array Int DataCon
523 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
524 | i <- [0..mAX_TUPLE_SIZE]]
526 -- build a fake parallel array constructor for the given arity
528 mkPArrFakeCon :: Int -> DataCon
529 mkPArrFakeCon arity = data_con
531 data_con = pcDataCon name [tyvar] [] tyvarTys parrTyCon
532 tyvar = head alphaTyVars
533 tyvarTys = replicate arity $ mkTyVarTy tyvar
534 nameStr = mkFastString ("MkPArr" ++ show arity)
535 name = mkWiredInName pREL_PARR (mkOccFS dataName nameStr) uniq
536 Nothing (ADataCon data_con)
537 uniq = mkPArrDataConUnique arity
539 -- checks whether a data constructor is a fake constructor for parallel arrays
541 isPArrFakeCon :: DataCon -> Bool
542 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)