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 ( 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, TyThing(..) )
75 import Kind ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
76 import Unique ( incrUnique, mkTupleTyConUnique,
77 mkTupleDataConUnique, mkPArrDataConUnique )
83 alpha_tyvar = [alphaTyVar]
88 %************************************************************************
90 \subsection{Wired in type constructors}
92 %************************************************************************
94 If you change which things are wired in, make sure you change their
95 names in PrelNames, so they use wTcQual, wDataQual, etc
98 wiredInTyCons :: [TyCon] -- Excludes tuples
99 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
100 -- it's defined in GHC.Base, and there's only
101 -- one of it. We put it in wiredInTyCons so
102 -- that it'll pre-populate the name cache, so
103 -- the special case in lookupOrigNameCache
104 -- doesn't need to look out for it
116 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
117 mkWiredInTyConName built_in mod fs uniq tycon
118 = mkWiredInName mod (mkOccFS tcName fs) uniq
119 Nothing -- No parent object
120 (ATyCon tycon) -- Relevant TyCon
123 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name -> Name
124 mkWiredInDataConName built_in mod fs uniq datacon parent
125 = mkWiredInName mod (mkOccFS dataName fs) uniq
126 (Just parent) -- Name of parent TyCon
127 (ADataCon datacon) -- Relevant DataCon
130 charTyConName = mkWiredInTyConName UserSyntax pREL_BASE FSLIT("Char") charTyConKey charTyCon
131 charDataConName = mkWiredInDataConName UserSyntax pREL_BASE FSLIT("C#") charDataConKey charDataCon charTyConName
132 intTyConName = mkWiredInTyConName UserSyntax pREL_BASE FSLIT("Int") intTyConKey intTyCon
133 intDataConName = mkWiredInDataConName UserSyntax pREL_BASE FSLIT("I#") intDataConKey intDataCon intTyConName
135 boolTyConName = mkWiredInTyConName UserSyntax pREL_BASE FSLIT("Bool") boolTyConKey boolTyCon
136 falseDataConName = mkWiredInDataConName UserSyntax pREL_BASE FSLIT("False") falseDataConKey falseDataCon boolTyConName
137 trueDataConName = mkWiredInDataConName UserSyntax pREL_BASE FSLIT("True") trueDataConKey trueDataCon boolTyConName
138 listTyConName = mkWiredInTyConName BuiltInSyntax pREL_BASE FSLIT("[]") listTyConKey listTyCon
139 nilDataConName = mkWiredInDataConName BuiltInSyntax pREL_BASE FSLIT("[]") nilDataConKey nilDataCon listTyConName
140 consDataConName = mkWiredInDataConName BuiltInSyntax pREL_BASE FSLIT(":") consDataConKey consDataCon listTyConName
142 floatTyConName = mkWiredInTyConName UserSyntax pREL_FLOAT FSLIT("Float") floatTyConKey floatTyCon
143 floatDataConName = mkWiredInDataConName UserSyntax pREL_FLOAT FSLIT("F#") floatDataConKey floatDataCon floatTyConName
144 doubleTyConName = mkWiredInTyConName UserSyntax pREL_FLOAT FSLIT("Double") doubleTyConKey doubleTyCon
145 doubleDataConName = mkWiredInDataConName UserSyntax pREL_FLOAT FSLIT("D#") doubleDataConKey doubleDataCon doubleTyConName
147 parrTyConName = mkWiredInTyConName BuiltInSyntax pREL_PARR FSLIT("[::]") parrTyConKey parrTyCon
148 parrDataConName = mkWiredInDataConName UserSyntax pREL_PARR FSLIT("PArr") parrDataConKey parrDataCon parrTyConName
150 boolTyCon_RDR = nameRdrName boolTyConName
151 false_RDR = nameRdrName falseDataConName
152 true_RDR = nameRdrName trueDataConName
153 intTyCon_RDR = nameRdrName intTyConName
154 charTyCon_RDR = nameRdrName charTyConName
155 intDataCon_RDR = nameRdrName intDataConName
156 listTyCon_RDR = nameRdrName listTyConName
157 consDataCon_RDR = nameRdrName consDataConName
158 parrTyCon_RDR = nameRdrName parrTyConName
162 %************************************************************************
164 \subsection{mkWiredInTyCon}
166 %************************************************************************
169 pcNonRecDataTyCon = pcTyCon False NonRecursive
170 pcRecDataTyCon = pcTyCon False Recursive
172 pcTyCon is_enum is_rec name tyvars argvrcs cons
175 tycon = mkAlgTyCon name
176 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
179 [] -- No stupid theta
180 (DataTyCon cons is_enum)
181 [] -- No record selectors
183 True -- All the wired-in tycons have generics
185 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
186 pcDataCon = pcDataConWithFixity False
188 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
189 -- The Name should be in the DataName name space; it's the name
190 -- of the DataCon itself.
192 -- The unique is the first of two free uniques;
193 -- the first is used for the datacon itself,
194 -- the second is used for the "worker name"
196 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
199 data_con = mkDataCon dc_name declared_infix True {- Vanilla -}
200 (map (const NotMarkedStrict) arg_tys)
201 [{- No labelled fields -}]
202 tyvars [] [] arg_tys tycon (mkTyVarTys tyvars)
203 (mkDataConIds bogus_wrap_name wrk_name data_con)
206 mod = nameModule dc_name
207 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
208 wrk_key = incrUnique (nameUnique dc_name)
209 wrk_name = mkWiredInName mod wrk_occ wrk_key
210 (Just (tyConName tycon))
211 (AnId (dataConWorkId data_con)) UserSyntax
212 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
213 -- Wired-in types are too simple to need wrappers
217 %************************************************************************
219 \subsection[TysWiredIn-tuples]{The tuple types}
221 %************************************************************************
224 tupleTyCon :: Boxity -> Arity -> TyCon
225 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
226 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
227 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
229 tupleCon :: Boxity -> Arity -> DataCon
230 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
231 tupleCon Boxed i = snd (boxedTupleArr ! i)
232 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
234 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
235 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
236 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
238 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
239 mk_tuple boxity arity = (tycon, tuple_con)
241 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
242 mod = mkTupleModule boxity arity
243 tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
244 Nothing (ATyCon tycon) BuiltInSyntax
245 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
246 res_kind | isBoxed boxity = liftedTypeKind
247 | otherwise = ubxTupleKind
249 tyvars | isBoxed boxity = take arity alphaTyVars
250 | otherwise = take arity openAlphaTyVars
252 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
253 tyvar_tys = mkTyVarTys tyvars
254 dc_name = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
255 (Just tc_name) (ADataCon tuple_con) BuiltInSyntax
256 tc_uniq = mkTupleTyConUnique boxity arity
257 dc_uniq = mkTupleDataConUnique boxity arity
258 gen_info = True -- Tuples all have generics..
259 -- hmm: that's a *lot* of code
261 unitTyCon = tupleTyCon Boxed 0
262 unitDataCon = head (tyConDataCons unitTyCon)
263 unitDataConId = dataConWorkId unitDataCon
265 pairTyCon = tupleTyCon Boxed 2
267 unboxedSingletonTyCon = tupleTyCon Unboxed 1
268 unboxedSingletonDataCon = tupleCon Unboxed 1
270 unboxedPairTyCon = tupleTyCon Unboxed 2
271 unboxedPairDataCon = tupleCon Unboxed 2
274 %************************************************************************
276 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
278 %************************************************************************
281 -- The Void type is represented as a data type with no constructors
282 -- It's a built in type (i.e. there's no way to define it in Haskell;
283 -- the nearest would be
285 -- data Void = -- No constructors!
287 -- ) It's lifted; there is only one value of this
288 -- type, namely "void", whose semantics is just bottom.
290 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
297 charTy = mkTyConTy charTyCon
299 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
300 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
302 stringTy = mkListTy charTy -- convenience only
306 intTy = mkTyConTy intTyCon
308 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
309 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
313 floatTy = mkTyConTy floatTyCon
315 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
316 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
320 doubleTy = mkTyConTy doubleTyCon
322 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
323 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
327 %************************************************************************
329 \subsection[TysWiredIn-Bool]{The @Bool@ type}
331 %************************************************************************
333 An ordinary enumeration type, but deeply wired in. There are no
334 magical operations on @Bool@ (just the regular Prelude code).
336 {\em BEGIN IDLE SPECULATION BY SIMON}
338 This is not the only way to encode @Bool@. A more obvious coding makes
339 @Bool@ just a boxed up version of @Bool#@, like this:
342 data Bool = MkBool Bool#
345 Unfortunately, this doesn't correspond to what the Report says @Bool@
346 looks like! Furthermore, we get slightly less efficient code (I
347 think) with this coding. @gtInt@ would look like this:
350 gtInt :: Int -> Int -> Bool
351 gtInt x y = case x of I# x# ->
353 case (gtIntPrim x# y#) of
357 Notice that the result of the @gtIntPrim@ comparison has to be turned
358 into an integer (here called @b#@), and returned in a @MkBool@ box.
360 The @if@ expression would compile to this:
363 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
366 I think this code is a little less efficient than the previous code,
367 but I'm not certain. At all events, corresponding with the Report is
368 important. The interesting thing is that the language is expressive
369 enough to describe more than one alternative; and that a type doesn't
370 necessarily need to be a straightforwardly boxed version of its
371 primitive counterpart.
373 {\em END IDLE SPECULATION BY SIMON}
376 boolTy = mkTyConTy boolTyCon
378 boolTyCon = pcTyCon True NonRecursive boolTyConName
379 [] [] [falseDataCon, trueDataCon]
381 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
382 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
384 falseDataConId = dataConWorkId falseDataCon
385 trueDataConId = dataConWorkId trueDataCon
388 %************************************************************************
390 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
392 %************************************************************************
394 Special syntax, deeply wired in, but otherwise an ordinary algebraic
397 data [] a = [] | a : (List a)
399 data (,) a b = (,,) a b
404 mkListTy :: Type -> Type
405 mkListTy ty = mkTyConApp listTyCon [ty]
407 listTyCon = pcRecDataTyCon listTyConName
408 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
410 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
411 consDataCon = pcDataConWithFixity True {- Declared infix -}
413 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
414 -- Interesting: polymorphic recursion would help here.
415 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
416 -- gets the over-specific type (Type -> Type)
419 %************************************************************************
421 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
423 %************************************************************************
425 The tuple types are definitely magic, because they form an infinite
430 They have a special family of type constructors, of type @TyCon@
431 These contain the tycon arity, but don't require a Unique.
434 They have a special family of constructors, of type
435 @Id@. Again these contain their arity but don't need a Unique.
438 There should be a magic way of generating the info tables and
439 entry code for all tuples.
441 But at the moment we just compile a Haskell source
442 file\srcloc{lib/prelude/...} containing declarations like:
445 data Tuple2 a b = Tup2 a b
446 data Tuple3 a b c = Tup3 a b c
447 data Tuple4 a b c d = Tup4 a b c d
450 The print-names associated with the magic @Id@s for tuple constructors
451 ``just happen'' to be the same as those generated by these
455 The instance environment should have a magic way to know
456 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
457 so on. \ToDo{Not implemented yet.}
460 There should also be a way to generate the appropriate code for each
461 of these instances, but (like the info tables and entry code) it is
462 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
466 mkTupleTy :: Boxity -> Int -> [Type] -> Type
467 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
469 unitTy = mkTupleTy Boxed 0 []
472 %************************************************************************
474 \subsection[TysWiredIn-PArr]{The @[::]@ type}
476 %************************************************************************
478 Special syntax for parallel arrays needs some wired in definitions.
481 -- construct a type representing the application of the parallel array
484 mkPArrTy :: Type -> Type
485 mkPArrTy ty = mkTyConApp parrTyCon [ty]
487 -- represents the type constructor of parallel arrays
489 -- * this must match the definition in `PrelPArr'
491 -- NB: Although the constructor is given here, it will not be accessible in
492 -- user code as it is not in the environment of any compiled module except
496 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [(True, False)] [parrDataCon]
498 parrDataCon :: DataCon
499 parrDataCon = pcDataCon
501 alpha_tyvar -- forall'ed type variables
502 [intPrimTy, -- 1st argument: Int#
503 mkTyConApp -- 2nd argument: Array# a
508 -- check whether a type constructor is the constructor for parallel arrays
510 isPArrTyCon :: TyCon -> Bool
511 isPArrTyCon tc = tyConName tc == parrTyConName
513 -- fake array constructors
515 -- * these constructors are never really used to represent array values;
516 -- however, they are very convenient during desugaring (and, in particular,
517 -- in the pattern matching compiler) to treat array pattern just like
518 -- yet another constructor pattern
520 parrFakeCon :: Arity -> DataCon
521 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
522 parrFakeCon i = parrFakeConArr!i
524 -- pre-defined set of constructors
526 parrFakeConArr :: Array Int DataCon
527 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
528 | i <- [0..mAX_TUPLE_SIZE]]
530 -- build a fake parallel array constructor for the given arity
532 mkPArrFakeCon :: Int -> DataCon
533 mkPArrFakeCon arity = data_con
535 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
536 tyvar = head alphaTyVars
537 tyvarTys = replicate arity $ mkTyVarTy tyvar
538 nameStr = mkFastString ("MkPArr" ++ show arity)
539 name = mkWiredInName pREL_PARR (mkOccFS dataName nameStr) uniq
540 Nothing (ADataCon data_con) UserSyntax
541 uniq = mkPArrDataConUnique arity
543 -- checks whether a data constructor is a fake constructor for parallel arrays
545 isPArrFakeCon :: DataCon -> Bool
546 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)