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, 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 (DataTyCon (Just []) cons is_enum)
180 [] -- No record selectors
182 True -- All the wired-in tycons have generics
184 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
185 pcDataCon = pcDataConWithFixity False
187 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
188 -- The Name should be in the DataName name space; it's the name
189 -- of the DataCon itself.
191 -- The unique is the first of two free uniques;
192 -- the first is used for the datacon itself,
193 -- the second is used for the "worker name"
195 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
198 data_con = mkDataCon dc_name declared_infix True {- Vanilla -}
199 (map (const NotMarkedStrict) arg_tys)
200 [{- No labelled fields -}]
201 tyvars [] [] arg_tys tycon (mkTyVarTys tyvars)
202 (mkDataConIds bogus_wrap_name wrk_name data_con)
205 mod = nameModule dc_name
206 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
207 wrk_key = incrUnique (nameUnique dc_name)
208 wrk_name = mkWiredInName mod wrk_occ wrk_key
209 (Just (tyConName tycon))
210 (AnId (dataConWorkId data_con)) UserSyntax
211 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
212 -- Wired-in types are too simple to need wrappers
216 %************************************************************************
218 \subsection[TysWiredIn-tuples]{The tuple types}
220 %************************************************************************
223 tupleTyCon :: Boxity -> Arity -> TyCon
224 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
225 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
226 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
228 tupleCon :: Boxity -> Arity -> DataCon
229 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
230 tupleCon Boxed i = snd (boxedTupleArr ! i)
231 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
233 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
234 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
235 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
237 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
238 mk_tuple boxity arity = (tycon, tuple_con)
240 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
241 mod = mkTupleModule boxity arity
242 tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
243 Nothing (ATyCon tycon) BuiltInSyntax
244 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
245 res_kind | isBoxed boxity = liftedTypeKind
246 | otherwise = ubxTupleKind
248 tyvars | isBoxed boxity = take arity alphaTyVars
249 | otherwise = take arity openAlphaTyVars
251 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
252 tyvar_tys = mkTyVarTys tyvars
253 dc_name = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
254 (Just tc_name) (ADataCon tuple_con) BuiltInSyntax
255 tc_uniq = mkTupleTyConUnique boxity arity
256 dc_uniq = mkTupleDataConUnique boxity arity
257 gen_info = True -- Tuples all have generics..
258 -- hmm: that's a *lot* of code
260 unitTyCon = tupleTyCon Boxed 0
261 unitDataCon = head (tyConDataCons unitTyCon)
262 unitDataConId = dataConWorkId unitDataCon
264 pairTyCon = tupleTyCon Boxed 2
266 unboxedSingletonTyCon = tupleTyCon Unboxed 1
267 unboxedSingletonDataCon = tupleCon Unboxed 1
269 unboxedPairTyCon = tupleTyCon Unboxed 2
270 unboxedPairDataCon = tupleCon Unboxed 2
273 %************************************************************************
275 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
277 %************************************************************************
280 -- The Void type is represented as a data type with no constructors
281 -- It's a built in type (i.e. there's no way to define it in Haskell;
282 -- the nearest would be
284 -- data Void = -- No constructors!
286 -- ) It's lifted; there is only one value of this
287 -- type, namely "void", whose semantics is just bottom.
289 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
296 charTy = mkTyConTy charTyCon
298 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
299 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
301 stringTy = mkListTy charTy -- convenience only
305 intTy = mkTyConTy intTyCon
307 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
308 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
312 floatTy = mkTyConTy floatTyCon
314 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
315 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
319 doubleTy = mkTyConTy doubleTyCon
321 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
322 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
326 %************************************************************************
328 \subsection[TysWiredIn-Bool]{The @Bool@ type}
330 %************************************************************************
332 An ordinary enumeration type, but deeply wired in. There are no
333 magical operations on @Bool@ (just the regular Prelude code).
335 {\em BEGIN IDLE SPECULATION BY SIMON}
337 This is not the only way to encode @Bool@. A more obvious coding makes
338 @Bool@ just a boxed up version of @Bool#@, like this:
341 data Bool = MkBool Bool#
344 Unfortunately, this doesn't correspond to what the Report says @Bool@
345 looks like! Furthermore, we get slightly less efficient code (I
346 think) with this coding. @gtInt@ would look like this:
349 gtInt :: Int -> Int -> Bool
350 gtInt x y = case x of I# x# ->
352 case (gtIntPrim x# y#) of
356 Notice that the result of the @gtIntPrim@ comparison has to be turned
357 into an integer (here called @b#@), and returned in a @MkBool@ box.
359 The @if@ expression would compile to this:
362 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
365 I think this code is a little less efficient than the previous code,
366 but I'm not certain. At all events, corresponding with the Report is
367 important. The interesting thing is that the language is expressive
368 enough to describe more than one alternative; and that a type doesn't
369 necessarily need to be a straightforwardly boxed version of its
370 primitive counterpart.
372 {\em END IDLE SPECULATION BY SIMON}
375 boolTy = mkTyConTy boolTyCon
377 boolTyCon = pcTyCon True NonRecursive boolTyConName
378 [] [] [falseDataCon, trueDataCon]
380 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
381 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
383 falseDataConId = dataConWorkId falseDataCon
384 trueDataConId = dataConWorkId trueDataCon
387 %************************************************************************
389 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
391 %************************************************************************
393 Special syntax, deeply wired in, but otherwise an ordinary algebraic
396 data [] a = [] | a : (List a)
398 data (,) a b = (,,) a b
403 mkListTy :: Type -> Type
404 mkListTy ty = mkTyConApp listTyCon [ty]
406 listTyCon = pcRecDataTyCon listTyConName
407 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
409 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
410 consDataCon = pcDataConWithFixity True {- Declared infix -}
412 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
413 -- Interesting: polymorphic recursion would help here.
414 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
415 -- gets the over-specific type (Type -> Type)
418 %************************************************************************
420 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
422 %************************************************************************
424 The tuple types are definitely magic, because they form an infinite
429 They have a special family of type constructors, of type @TyCon@
430 These contain the tycon arity, but don't require a Unique.
433 They have a special family of constructors, of type
434 @Id@. Again these contain their arity but don't need a Unique.
437 There should be a magic way of generating the info tables and
438 entry code for all tuples.
440 But at the moment we just compile a Haskell source
441 file\srcloc{lib/prelude/...} containing declarations like:
444 data Tuple2 a b = Tup2 a b
445 data Tuple3 a b c = Tup3 a b c
446 data Tuple4 a b c d = Tup4 a b c d
449 The print-names associated with the magic @Id@s for tuple constructors
450 ``just happen'' to be the same as those generated by these
454 The instance environment should have a magic way to know
455 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
456 so on. \ToDo{Not implemented yet.}
459 There should also be a way to generate the appropriate code for each
460 of these instances, but (like the info tables and entry code) it is
461 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
465 mkTupleTy :: Boxity -> Int -> [Type] -> Type
466 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
468 unitTy = mkTupleTy Boxed 0 []
471 %************************************************************************
473 \subsection[TysWiredIn-PArr]{The @[::]@ type}
475 %************************************************************************
477 Special syntax for parallel arrays needs some wired in definitions.
480 -- construct a type representing the application of the parallel array
483 mkPArrTy :: Type -> Type
484 mkPArrTy ty = mkTyConApp parrTyCon [ty]
486 -- represents the type constructor of parallel arrays
488 -- * this must match the definition in `PrelPArr'
490 -- NB: Although the constructor is given here, it will not be accessible in
491 -- user code as it is not in the environment of any compiled module except
495 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [(True, False)] [parrDataCon]
497 parrDataCon :: DataCon
498 parrDataCon = pcDataCon
500 alpha_tyvar -- forall'ed type variables
501 [intPrimTy, -- 1st argument: Int#
502 mkTyConApp -- 2nd argument: Array# a
507 -- check whether a type constructor is the constructor for parallel arrays
509 isPArrTyCon :: TyCon -> Bool
510 isPArrTyCon tc = tyConName tc == parrTyConName
512 -- fake array constructors
514 -- * these constructors are never really used to represent array values;
515 -- however, they are very convenient during desugaring (and, in particular,
516 -- in the pattern matching compiler) to treat array pattern just like
517 -- yet another constructor pattern
519 parrFakeCon :: Arity -> DataCon
520 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
521 parrFakeCon i = parrFakeConArr!i
523 -- pre-defined set of constructors
525 parrFakeConArr :: Array Int DataCon
526 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
527 | i <- [0..mAX_TUPLE_SIZE]]
529 -- build a fake parallel array constructor for the given arity
531 mkPArrFakeCon :: Int -> DataCon
532 mkPArrFakeCon arity = data_con
534 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
535 tyvar = head alphaTyVars
536 tyvarTys = replicate arity $ mkTyVarTy tyvar
537 nameStr = mkFastString ("MkPArr" ++ show arity)
538 name = mkWiredInName pREL_PARR (mkOccFS dataName nameStr) uniq
539 Nothing (ADataCon data_con) UserSyntax
540 uniq = mkPArrDataConUnique arity
542 -- checks whether a data constructor is a fake constructor for parallel arrays
544 isPArrFakeCon :: DataCon -> Bool
545 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)