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, AlgTyConRhs(DataTyCon), tyConDataCons,
69 mkTupleTyCon, mkAlgTyCon, tyConName
72 import BasicTypes ( Arity, RecFlag(..), Boxity(..), isBoxed, StrictnessMark(..) )
74 import Type ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys,
75 ThetaType, TyThing(..) )
76 import Kind ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
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)
179 (DataTyCon cons is_enum)
180 [] -- No record selectors
182 True -- All the wired-in tycons have generics
184 pcDataCon :: Name -> [TyVar] -> ThetaType -> [Type] -> TyCon -> DataCon
185 -- The Name should be in the DataName name space; it's the name
186 -- of the DataCon itself.
188 -- The unique is the first of two free uniques;
189 -- the first is used for the datacon itself,
190 -- the second is used for the "worker name"
192 pcDataCon dc_name tyvars context arg_tys tycon
195 data_con = mkDataCon dc_name
196 (map (const NotMarkedStrict) arg_tys)
197 [{- No labelled fields -}]
198 tyvars context [] [] arg_tys tycon
199 (mkDataConIds bogus_wrap_name wrk_name data_con)
201 mod = nameModule dc_name
202 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
203 wrk_key = incrUnique (nameUnique dc_name)
204 wrk_name = mkWiredInName mod wrk_occ wrk_key
205 (Just (tyConName tycon))
206 (AnId (dataConWorkId data_con))
207 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
208 -- Wired-in types are too simple to need wrappers
212 %************************************************************************
214 \subsection[TysWiredIn-tuples]{The tuple types}
216 %************************************************************************
219 tupleTyCon :: Boxity -> Arity -> TyCon
220 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
221 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
222 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
224 tupleCon :: Boxity -> Arity -> DataCon
225 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
226 tupleCon Boxed i = snd (boxedTupleArr ! i)
227 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
229 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
230 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
231 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
233 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
234 mk_tuple boxity arity = (tycon, tuple_con)
236 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
237 mod = mkTupleModule boxity arity
238 tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
239 Nothing (ATyCon tycon)
240 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
241 res_kind | isBoxed boxity = liftedTypeKind
242 | otherwise = ubxTupleKind
244 tyvars | isBoxed boxity = take arity alphaTyVars
245 | otherwise = take arity openAlphaTyVars
247 tuple_con = pcDataCon dc_name tyvars [] tyvar_tys tycon
248 tyvar_tys = mkTyVarTys tyvars
249 dc_name = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
250 (Just tc_name) (ADataCon tuple_con)
251 tc_uniq = mkTupleTyConUnique boxity arity
252 dc_uniq = mkTupleDataConUnique boxity arity
253 gen_info = True -- Tuples all have generics..
254 -- hmm: that's a *lot* of code
256 unitTyCon = tupleTyCon Boxed 0
257 unitDataCon = head (tyConDataCons unitTyCon)
258 unitDataConId = dataConWorkId unitDataCon
260 pairTyCon = tupleTyCon Boxed 2
262 unboxedSingletonTyCon = tupleTyCon Unboxed 1
263 unboxedSingletonDataCon = tupleCon Unboxed 1
265 unboxedPairTyCon = tupleTyCon Unboxed 2
266 unboxedPairDataCon = tupleCon Unboxed 2
269 %************************************************************************
271 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
273 %************************************************************************
276 -- The Void type is represented as a data type with no constructors
277 -- It's a built in type (i.e. there's no way to define it in Haskell;
278 -- the nearest would be
280 -- data Void = -- No constructors!
282 -- ) It's lifted; there is only one value of this
283 -- type, namely "void", whose semantics is just bottom.
285 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
292 charTy = mkTyConTy charTyCon
294 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
295 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
297 stringTy = mkListTy charTy -- convenience only
301 intTy = mkTyConTy intTyCon
303 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
304 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
308 floatTy = mkTyConTy floatTyCon
310 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
311 floatDataCon = pcDataCon floatDataConName [] [] [floatPrimTy] floatTyCon
315 doubleTy = mkTyConTy doubleTyCon
317 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
318 doubleDataCon = pcDataCon doubleDataConName [] [] [doublePrimTy] doubleTyCon
322 %************************************************************************
324 \subsection[TysWiredIn-Bool]{The @Bool@ type}
326 %************************************************************************
328 An ordinary enumeration type, but deeply wired in. There are no
329 magical operations on @Bool@ (just the regular Prelude code).
331 {\em BEGIN IDLE SPECULATION BY SIMON}
333 This is not the only way to encode @Bool@. A more obvious coding makes
334 @Bool@ just a boxed up version of @Bool#@, like this:
337 data Bool = MkBool Bool#
340 Unfortunately, this doesn't correspond to what the Report says @Bool@
341 looks like! Furthermore, we get slightly less efficient code (I
342 think) with this coding. @gtInt@ would look like this:
345 gtInt :: Int -> Int -> Bool
346 gtInt x y = case x of I# x# ->
348 case (gtIntPrim x# y#) of
352 Notice that the result of the @gtIntPrim@ comparison has to be turned
353 into an integer (here called @b#@), and returned in a @MkBool@ box.
355 The @if@ expression would compile to this:
358 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
361 I think this code is a little less efficient than the previous code,
362 but I'm not certain. At all events, corresponding with the Report is
363 important. The interesting thing is that the language is expressive
364 enough to describe more than one alternative; and that a type doesn't
365 necessarily need to be a straightforwardly boxed version of its
366 primitive counterpart.
368 {\em END IDLE SPECULATION BY SIMON}
371 boolTy = mkTyConTy boolTyCon
373 boolTyCon = pcTyCon True NonRecursive boolTyConName
374 [] [] [falseDataCon, trueDataCon]
376 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
377 trueDataCon = pcDataCon trueDataConName [] [] [] boolTyCon
379 falseDataConId = dataConWorkId falseDataCon
380 trueDataConId = dataConWorkId trueDataCon
383 %************************************************************************
385 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
387 %************************************************************************
389 Special syntax, deeply wired in, but otherwise an ordinary algebraic
392 data [] a = [] | a : (List a)
394 data (,) a b = (,,) a b
399 mkListTy :: Type -> Type
400 mkListTy ty = mkTyConApp listTyCon [ty]
402 listTyCon = pcRecDataTyCon listTyConName
403 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
405 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
406 consDataCon = pcDataCon consDataConName
407 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
408 -- Interesting: polymorphic recursion would help here.
409 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
410 -- gets the over-specific type (Type -> Type)
413 %************************************************************************
415 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
417 %************************************************************************
419 The tuple types are definitely magic, because they form an infinite
424 They have a special family of type constructors, of type @TyCon@
425 These contain the tycon arity, but don't require a Unique.
428 They have a special family of constructors, of type
429 @Id@. Again these contain their arity but don't need a Unique.
432 There should be a magic way of generating the info tables and
433 entry code for all tuples.
435 But at the moment we just compile a Haskell source
436 file\srcloc{lib/prelude/...} containing declarations like:
439 data Tuple2 a b = Tup2 a b
440 data Tuple3 a b c = Tup3 a b c
441 data Tuple4 a b c d = Tup4 a b c d
444 The print-names associated with the magic @Id@s for tuple constructors
445 ``just happen'' to be the same as those generated by these
449 The instance environment should have a magic way to know
450 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
451 so on. \ToDo{Not implemented yet.}
454 There should also be a way to generate the appropriate code for each
455 of these instances, but (like the info tables and entry code) it is
456 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
460 mkTupleTy :: Boxity -> Int -> [Type] -> Type
461 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
463 unitTy = mkTupleTy Boxed 0 []
466 %************************************************************************
468 \subsection[TysWiredIn-PArr]{The @[::]@ type}
470 %************************************************************************
472 Special syntax for parallel arrays needs some wired in definitions.
475 -- construct a type representing the application of the parallel array
478 mkPArrTy :: Type -> Type
479 mkPArrTy ty = mkTyConApp parrTyCon [ty]
481 -- represents the type constructor of parallel arrays
483 -- * this must match the definition in `PrelPArr'
485 -- NB: Although the constructor is given here, it will not be accessible in
486 -- user code as it is not in the environment of any compiled module except
490 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [(True, False)] [parrDataCon]
492 parrDataCon :: DataCon
493 parrDataCon = pcDataCon
495 alpha_tyvar -- forall'ed type variables
497 [intPrimTy, -- 1st argument: Int#
498 mkTyConApp -- 2nd argument: Array# a
503 -- check whether a type constructor is the constructor for parallel arrays
505 isPArrTyCon :: TyCon -> Bool
506 isPArrTyCon tc = tyConName tc == parrTyConName
508 -- fake array constructors
510 -- * these constructors are never really used to represent array values;
511 -- however, they are very convenient during desugaring (and, in particular,
512 -- in the pattern matching compiler) to treat array pattern just like
513 -- yet another constructor pattern
515 parrFakeCon :: Arity -> DataCon
516 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
517 parrFakeCon i = parrFakeConArr!i
519 -- pre-defined set of constructors
521 parrFakeConArr :: Array Int DataCon
522 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
523 | i <- [0..mAX_TUPLE_SIZE]]
525 -- build a fake parallel array constructor for the given arity
527 mkPArrFakeCon :: Int -> DataCon
528 mkPArrFakeCon arity = data_con
530 data_con = pcDataCon name [tyvar] [] tyvarTys parrTyCon
531 tyvar = head alphaTyVars
532 tyvarTys = replicate arity $ mkTyVarTy tyvar
533 nameStr = mkFastString ("MkPArr" ++ show arity)
534 name = mkWiredInName pREL_PARR (mkOccFS dataName nameStr) uniq
535 Nothing (ADataCon data_con)
536 uniq = mkPArrDataConUnique arity
538 -- checks whether a data constructor is a fake constructor for parallel arrays
540 isPArrFakeCon :: DataCon -> Bool
541 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)