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 Kind ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
78 import Unique ( incrUnique, mkTupleTyConUnique,
79 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 :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
118 mkWiredInTyConName built_in mod fs uniq tycon
119 = mkWiredInName mod (mkOccNameFS tcName fs) uniq
120 Nothing -- No parent object
121 (ATyCon tycon) -- Relevant TyCon
124 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name -> Name
125 mkWiredInDataConName built_in mod fs uniq datacon parent
126 = mkWiredInName mod (mkOccNameFS dataName fs) uniq
127 (Just parent) -- Name of parent TyCon
128 (ADataCon datacon) -- Relevant DataCon
131 charTyConName = mkWiredInTyConName UserSyntax pREL_BASE FSLIT("Char") charTyConKey charTyCon
132 charDataConName = mkWiredInDataConName UserSyntax pREL_BASE FSLIT("C#") charDataConKey charDataCon charTyConName
133 intTyConName = mkWiredInTyConName UserSyntax pREL_BASE FSLIT("Int") intTyConKey intTyCon
134 intDataConName = mkWiredInDataConName UserSyntax pREL_BASE FSLIT("I#") intDataConKey intDataCon intTyConName
136 boolTyConName = mkWiredInTyConName UserSyntax pREL_BASE FSLIT("Bool") boolTyConKey boolTyCon
137 falseDataConName = mkWiredInDataConName UserSyntax pREL_BASE FSLIT("False") falseDataConKey falseDataCon boolTyConName
138 trueDataConName = mkWiredInDataConName UserSyntax pREL_BASE FSLIT("True") trueDataConKey trueDataCon boolTyConName
139 listTyConName = mkWiredInTyConName BuiltInSyntax pREL_BASE FSLIT("[]") listTyConKey listTyCon
140 nilDataConName = mkWiredInDataConName BuiltInSyntax pREL_BASE FSLIT("[]") nilDataConKey nilDataCon listTyConName
141 consDataConName = mkWiredInDataConName BuiltInSyntax pREL_BASE FSLIT(":") consDataConKey consDataCon listTyConName
143 floatTyConName = mkWiredInTyConName UserSyntax pREL_FLOAT FSLIT("Float") floatTyConKey floatTyCon
144 floatDataConName = mkWiredInDataConName UserSyntax pREL_FLOAT FSLIT("F#") floatDataConKey floatDataCon floatTyConName
145 doubleTyConName = mkWiredInTyConName UserSyntax pREL_FLOAT FSLIT("Double") doubleTyConKey doubleTyCon
146 doubleDataConName = mkWiredInDataConName UserSyntax pREL_FLOAT FSLIT("D#") doubleDataConKey doubleDataCon doubleTyConName
148 parrTyConName = mkWiredInTyConName BuiltInSyntax pREL_PARR FSLIT("[::]") parrTyConKey parrTyCon
149 parrDataConName = mkWiredInDataConName UserSyntax pREL_PARR FSLIT("PArr") parrDataConKey parrDataCon parrTyConName
151 boolTyCon_RDR = nameRdrName boolTyConName
152 false_RDR = nameRdrName falseDataConName
153 true_RDR = nameRdrName trueDataConName
154 intTyCon_RDR = nameRdrName intTyConName
155 charTyCon_RDR = nameRdrName charTyConName
156 intDataCon_RDR = nameRdrName intDataConName
157 listTyCon_RDR = nameRdrName listTyConName
158 consDataCon_RDR = nameRdrName consDataConName
159 parrTyCon_RDR = nameRdrName parrTyConName
163 %************************************************************************
165 \subsection{mkWiredInTyCon}
167 %************************************************************************
170 pcNonRecDataTyCon = pcTyCon False NonRecursive
171 pcRecDataTyCon = pcTyCon False Recursive
173 pcTyCon is_enum is_rec name tyvars argvrcs cons
176 tycon = mkAlgTyCon name
177 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
180 [] -- No stupid theta
181 (DataTyCon cons is_enum)
182 [] -- No record selectors
184 True -- All the wired-in tycons have generics
186 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
187 pcDataCon = pcDataConWithFixity False
189 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
190 -- The Name should be in the DataName name space; it's the name
191 -- of the DataCon itself.
193 -- The unique is the first of two free uniques;
194 -- the first is used for the datacon itself,
195 -- the second is used for the "worker name"
197 pcDataConWithFixity declared_infix dc_name tyvars arg_tys tycon
200 data_con = mkDataCon dc_name declared_infix True {- Vanilla -}
201 (map (const NotMarkedStrict) arg_tys)
202 [{- No labelled fields -}]
203 tyvars [] [] arg_tys tycon (mkTyVarTys tyvars)
204 (mkDataConIds bogus_wrap_name wrk_name data_con)
207 mod = nameModule dc_name
208 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
209 wrk_key = incrUnique (nameUnique dc_name)
210 wrk_name = mkWiredInName mod wrk_occ wrk_key
211 (Just (tyConName tycon))
212 (AnId (dataConWorkId data_con)) UserSyntax
213 bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
214 -- Wired-in types are too simple to need wrappers
218 %************************************************************************
220 \subsection[TysWiredIn-tuples]{The tuple types}
222 %************************************************************************
225 tupleTyCon :: Boxity -> Arity -> TyCon
226 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i) -- Build one specially
227 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
228 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
230 tupleCon :: Boxity -> Arity -> DataCon
231 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i) -- Build one specially
232 tupleCon Boxed i = snd (boxedTupleArr ! i)
233 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
235 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
236 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
237 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
239 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
240 mk_tuple boxity arity = (tycon, tuple_con)
242 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info
243 mod = mkTupleModule boxity arity
244 tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
245 Nothing (ATyCon tycon) BuiltInSyntax
246 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
247 res_kind | isBoxed boxity = liftedTypeKind
248 | otherwise = ubxTupleKind
250 tyvars | isBoxed boxity = take arity alphaTyVars
251 | otherwise = take arity openAlphaTyVars
253 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
254 tyvar_tys = mkTyVarTys tyvars
255 dc_name = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
256 (Just tc_name) (ADataCon tuple_con) BuiltInSyntax
257 tc_uniq = mkTupleTyConUnique boxity arity
258 dc_uniq = mkTupleDataConUnique boxity arity
259 gen_info = True -- Tuples all have generics..
260 -- hmm: that's a *lot* of code
262 unitTyCon = tupleTyCon Boxed 0
263 unitDataCon = head (tyConDataCons unitTyCon)
264 unitDataConId = dataConWorkId unitDataCon
266 pairTyCon = tupleTyCon Boxed 2
268 unboxedSingletonTyCon = tupleTyCon Unboxed 1
269 unboxedSingletonDataCon = tupleCon Unboxed 1
271 unboxedPairTyCon = tupleTyCon Unboxed 2
272 unboxedPairDataCon = tupleCon Unboxed 2
275 %************************************************************************
277 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
279 %************************************************************************
282 -- The Void type is represented as a data type with no constructors
283 -- It's a built in type (i.e. there's no way to define it in Haskell;
284 -- the nearest would be
286 -- data Void = -- No constructors!
288 -- ) It's lifted; there is only one value of this
289 -- type, namely "void", whose semantics is just bottom.
291 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
298 charTy = mkTyConTy charTyCon
300 charTyCon = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
301 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
303 stringTy = mkListTy charTy -- convenience only
307 intTy = mkTyConTy intTyCon
309 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
310 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
314 floatTy = mkTyConTy floatTyCon
316 floatTyCon = pcNonRecDataTyCon floatTyConName [] [] [floatDataCon]
317 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
321 doubleTy = mkTyConTy doubleTyCon
323 doubleTyCon = pcNonRecDataTyCon doubleTyConName [] [] [doubleDataCon]
324 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
328 %************************************************************************
330 \subsection[TysWiredIn-Bool]{The @Bool@ type}
332 %************************************************************************
334 An ordinary enumeration type, but deeply wired in. There are no
335 magical operations on @Bool@ (just the regular Prelude code).
337 {\em BEGIN IDLE SPECULATION BY SIMON}
339 This is not the only way to encode @Bool@. A more obvious coding makes
340 @Bool@ just a boxed up version of @Bool#@, like this:
343 data Bool = MkBool Bool#
346 Unfortunately, this doesn't correspond to what the Report says @Bool@
347 looks like! Furthermore, we get slightly less efficient code (I
348 think) with this coding. @gtInt@ would look like this:
351 gtInt :: Int -> Int -> Bool
352 gtInt x y = case x of I# x# ->
354 case (gtIntPrim x# y#) of
358 Notice that the result of the @gtIntPrim@ comparison has to be turned
359 into an integer (here called @b#@), and returned in a @MkBool@ box.
361 The @if@ expression would compile to this:
364 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
367 I think this code is a little less efficient than the previous code,
368 but I'm not certain. At all events, corresponding with the Report is
369 important. The interesting thing is that the language is expressive
370 enough to describe more than one alternative; and that a type doesn't
371 necessarily need to be a straightforwardly boxed version of its
372 primitive counterpart.
374 {\em END IDLE SPECULATION BY SIMON}
377 boolTy = mkTyConTy boolTyCon
379 boolTyCon = pcTyCon True NonRecursive boolTyConName
380 [] [] [falseDataCon, trueDataCon]
382 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
383 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
385 falseDataConId = dataConWorkId falseDataCon
386 trueDataConId = dataConWorkId trueDataCon
389 %************************************************************************
391 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
393 %************************************************************************
395 Special syntax, deeply wired in, but otherwise an ordinary algebraic
398 data [] a = [] | a : (List a)
400 data (,) a b = (,,) a b
405 mkListTy :: Type -> Type
406 mkListTy ty = mkTyConApp listTyCon [ty]
408 listTyCon = pcRecDataTyCon listTyConName
409 alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
411 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
412 consDataCon = pcDataConWithFixity True {- Declared infix -}
414 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
415 -- Interesting: polymorphic recursion would help here.
416 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
417 -- gets the over-specific type (Type -> Type)
420 %************************************************************************
422 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
424 %************************************************************************
426 The tuple types are definitely magic, because they form an infinite
431 They have a special family of type constructors, of type @TyCon@
432 These contain the tycon arity, but don't require a Unique.
435 They have a special family of constructors, of type
436 @Id@. Again these contain their arity but don't need a Unique.
439 There should be a magic way of generating the info tables and
440 entry code for all tuples.
442 But at the moment we just compile a Haskell source
443 file\srcloc{lib/prelude/...} containing declarations like:
446 data Tuple2 a b = Tup2 a b
447 data Tuple3 a b c = Tup3 a b c
448 data Tuple4 a b c d = Tup4 a b c d
451 The print-names associated with the magic @Id@s for tuple constructors
452 ``just happen'' to be the same as those generated by these
456 The instance environment should have a magic way to know
457 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
458 so on. \ToDo{Not implemented yet.}
461 There should also be a way to generate the appropriate code for each
462 of these instances, but (like the info tables and entry code) it is
463 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
467 mkTupleTy :: Boxity -> Int -> [Type] -> Type
468 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
470 unitTy = mkTupleTy Boxed 0 []
473 %************************************************************************
475 \subsection[TysWiredIn-PArr]{The @[::]@ type}
477 %************************************************************************
479 Special syntax for parallel arrays needs some wired in definitions.
482 -- construct a type representing the application of the parallel array
485 mkPArrTy :: Type -> Type
486 mkPArrTy ty = mkTyConApp parrTyCon [ty]
488 -- represents the type constructor of parallel arrays
490 -- * this must match the definition in `PrelPArr'
492 -- NB: Although the constructor is given here, it will not be accessible in
493 -- user code as it is not in the environment of any compiled module except
497 parrTyCon = pcNonRecDataTyCon parrTyConName alpha_tyvar [(True, False)] [parrDataCon]
499 parrDataCon :: DataCon
500 parrDataCon = pcDataCon
502 alpha_tyvar -- forall'ed type variables
503 [intPrimTy, -- 1st argument: Int#
504 mkTyConApp -- 2nd argument: Array# a
509 -- check whether a type constructor is the constructor for parallel arrays
511 isPArrTyCon :: TyCon -> Bool
512 isPArrTyCon tc = tyConName tc == parrTyConName
514 -- fake array constructors
516 -- * these constructors are never really used to represent array values;
517 -- however, they are very convenient during desugaring (and, in particular,
518 -- in the pattern matching compiler) to treat array pattern just like
519 -- yet another constructor pattern
521 parrFakeCon :: Arity -> DataCon
522 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
523 parrFakeCon i = parrFakeConArr!i
525 -- pre-defined set of constructors
527 parrFakeConArr :: Array Int DataCon
528 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
529 | i <- [0..mAX_TUPLE_SIZE]]
531 -- build a fake parallel array constructor for the given arity
533 mkPArrFakeCon :: Int -> DataCon
534 mkPArrFakeCon arity = data_con
536 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
537 tyvar = head alphaTyVars
538 tyvarTys = replicate arity $ mkTyVarTy tyvar
539 nameStr = mkFastString ("MkPArr" ++ show arity)
540 name = mkWiredInName pREL_PARR (mkOccNameFS dataName nameStr) uniq
541 Nothing (ADataCon data_con) UserSyntax
542 uniq = mkPArrDataConUnique arity
544 -- checks whether a data constructor is a fake constructor for parallel arrays
546 isPArrFakeCon :: DataCon -> Bool
547 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)