3d234338500b4314aa8f3609436bcff8ae93a466
[ghc-hetmet.git] / ghc / compiler / prelude / TysWiredIn.lhs
1 %
2 % (c) The GRASP Project, Glasgow University, 1994-1998
3 %
4 \section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types}
5
6 This module is about types that can be defined in Haskell, but which
7 must be wired into the compiler nonetheless.
8
9 This module tracks the ``state interface'' document, ``GHC prelude:
10 types and operations.''
11
12 \begin{code}
13 module TysWiredIn (
14         addrDataCon,
15         addrTy,
16         addrTyCon,
17         boolTy,
18         boolTyCon,
19         charDataCon,
20         charTy,
21         charTyCon,
22         consDataCon,
23         doubleDataCon,
24         doubleTy,
25         isDoubleTy,
26         doubleTyCon,
27         falseDataCon,
28         floatDataCon,
29         floatTy,
30         isFloatTy,
31         floatTyCon,
32
33         voidTyCon, voidTy, 
34
35         intDataCon,
36         intTy,
37         intTyCon,
38         isIntTy,
39         inIntRange,
40
41         int8TyCon,
42         int16TyCon,
43         int32TyCon,
44
45         int64TyCon,
46         int64DataCon,
47 --      int64Ty,
48
49         integerTy,
50         integerTyCon,
51         integerDataCon,
52         isIntegerTy,
53
54         listTyCon,
55
56         mkListTy,
57         nilDataCon,
58
59         -- tuples
60         mkTupleTy,
61         tupleTyCon, tupleCon, unitTyCon, unitDataCon, pairTyCon, pairDataCon,
62
63         -- unboxed tuples
64         mkUnboxedTupleTy,
65         unboxedTupleTyCon, unboxedTupleCon, 
66         unboxedPairTyCon, unboxedPairDataCon,
67
68         stateDataCon,
69         stateTyCon,
70         realWorldStateTy,
71
72         stablePtrTyCon,
73         stringTy,
74         trueDataCon,
75         unitTy,
76         wordDataCon,
77         wordTy,
78         wordTyCon,
79
80         word8TyCon,
81         word16TyCon,
82         word32TyCon,
83
84         word64DataCon,
85 --      word64Ty,
86         word64TyCon,
87         
88         isFFIArgumentTy,  -- :: Type -> Bool
89         isFFIResultTy,    -- :: Type -> Bool
90         isFFIExternalTy,  -- :: Type -> Bool
91         isAddrTy,         -- :: Type -> Bool
92
93     ) where
94
95 #include "HsVersions.h"
96
97 import {-# SOURCE #-} MkId( mkDataConId )
98
99 -- friends:
100 import PrelMods
101 import TysPrim
102
103 -- others:
104 import Constants        ( mAX_TUPLE_SIZE )
105 import Name             ( Module, varOcc, mkWiredInTyConName, mkWiredInIdName )
106 import DataCon          ( DataCon, mkDataCon )
107 import Var              ( TyVar, tyVarKind )
108 import TyCon            ( TyCon, mkAlgTyCon, mkSynTyCon, mkTupleTyCon )
109 import BasicTypes       ( Arity, NewOrData(..), 
110                           RecFlag(..), StrictnessMark(..) )
111 import Type             ( Type, mkTyConTy, mkTyConApp, mkSigmaTy, mkTyVarTys, 
112                           mkArrowKinds, boxedTypeKind, unboxedTypeKind,
113                           mkFunTy, mkFunTys, isUnLiftedType,
114                           splitTyConApp_maybe, splitAlgTyConApp_maybe,
115                           ThetaType, TauType )
116 import PrimRep          ( PrimRep(..) )
117 import Unique
118 import CmdLineOpts      ( opt_GlasgowExts )
119 import Util             ( assoc )
120 import Panic            ( panic )
121 import Array
122
123 alpha_tyvar       = [alphaTyVar]
124 alpha_ty          = [alphaTy]
125 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
126
127 pcRecDataTyCon, pcNonRecDataTyCon, pcNonRecNewTyCon
128         :: Unique{-TyConKey-} -> Module -> FAST_STRING
129         -> [TyVar] -> [DataCon] -> TyCon
130
131 pcRecDataTyCon    = pcTyCon DataType Recursive
132 pcNonRecDataTyCon = pcTyCon DataType NonRecursive
133 pcNonRecNewTyCon  = pcTyCon NewType  NonRecursive
134
135 pcTyCon new_or_data is_rec key mod str tyvars cons
136   = tycon
137   where
138     tycon = mkAlgTyCon name kind 
139                 tyvars 
140                 []              -- No context
141                 cons
142                 []              -- No derivings
143                 Nothing         -- Not a dictionary
144                 new_or_data
145                 is_rec
146
147     name = mkWiredInTyConName key mod str tycon
148     kind = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind
149
150 pcSynTyCon key mod str kind arity tyvars expansion
151   = tycon
152   where
153     tycon = mkSynTyCon name kind arity tyvars expansion
154     name  = mkWiredInTyConName key mod str tycon
155
156 pcDataCon :: Unique{-DataConKey-} -> Module -> FAST_STRING
157           -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> DataCon
158 pcDataCon key mod str tyvars context arg_tys tycon
159   = data_con
160   where
161     data_con = mkDataCon name 
162                 [ NotMarkedStrict | a <- arg_tys ]
163                 [ {- no labelled fields -} ]
164                 tyvars context [] [] arg_tys tycon id
165     name = mkWiredInIdName key mod (varOcc str) id
166     id   = mkDataConId data_con
167 \end{code}
168
169 %************************************************************************
170 %*                                                                      *
171 \subsection[TysWiredIn-tuples]{The tuple types}
172 %*                                                                      *
173 %************************************************************************
174
175 \begin{code}
176 tupleTyCon :: Arity -> TyCon
177 tupleTyCon i | i > mAX_TUPLE_SIZE = fst (mk_tuple i)    -- Build one specially
178              | otherwise          = tupleTyConArr!i
179
180 tupleCon :: Arity -> DataCon
181 tupleCon i | i > mAX_TUPLE_SIZE = snd (mk_tuple i)      -- Build one specially
182            | otherwise          = tupleConArr!i
183
184 tupleTyCons :: [TyCon]
185 tupleTyCons = elems tupleTyConArr
186
187 tupleTyConArr :: Array Int TyCon
188 tupleTyConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map fst tuples)
189
190 tupleConArr :: Array Int DataCon
191 tupleConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map snd tuples)
192
193 tuples :: [(TyCon,DataCon)]
194 tuples = [mk_tuple i | i <- [0..mAX_TUPLE_SIZE]]
195
196 mk_tuple :: Int -> (TyCon,DataCon)
197 mk_tuple arity = (tycon, tuple_con)
198   where
199         tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con True
200         tc_name = mkWiredInTyConName tc_uniq mod_name name_str tycon
201         tc_kind = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind
202
203         tuple_con = pcDataCon dc_uniq mod_name name_str tyvars [] tyvar_tys tycon
204         tyvars    = take arity alphaTyVars
205         tyvar_tys = mkTyVarTys tyvars
206         (mod_name, name_str) = mkTupNameStr arity
207         tc_uniq   = mkTupleTyConUnique   arity
208         dc_uniq   = mkTupleDataConUnique arity
209
210 unitTyCon = tupleTyCon 0
211 pairTyCon = tupleTyCon 2
212
213 unitDataCon = tupleCon 0
214 pairDataCon = tupleCon 2
215 \end{code}
216
217 %************************************************************************
218 %*                                                                      *
219 \subsection[TysWiredIn-ubx-tuples]{Unboxed Tuple Types}
220 %*                                                                      *
221 %************************************************************************
222
223 \begin{code}
224 unboxedTupleTyCon :: Arity -> TyCon
225 unboxedTupleTyCon i | i > mAX_TUPLE_SIZE = fst (mk_unboxed_tuple i)
226                     | otherwise          = unboxedTupleTyConArr!i
227
228 unboxedTupleCon :: Arity -> DataCon
229 unboxedTupleCon i | i > mAX_TUPLE_SIZE = snd (mk_unboxed_tuple i)
230                   | otherwise          = unboxedTupleConArr!i
231
232 unboxedTupleTyConArr :: Array Int TyCon
233 unboxedTupleTyConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map fst ubx_tuples)
234
235 unboxedTupleConArr :: Array Int DataCon
236 unboxedTupleConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map snd ubx_tuples)
237
238 ubx_tuples :: [(TyCon,DataCon)]
239 ubx_tuples = [mk_unboxed_tuple i | i <- [0..mAX_TUPLE_SIZE]]
240
241 mk_unboxed_tuple :: Int -> (TyCon,DataCon)
242 mk_unboxed_tuple arity = (tycon, tuple_con)
243   where
244         tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con False
245         tc_name = mkWiredInTyConName tc_uniq mod_name name_str tycon
246         tc_kind = mkArrowKinds (map tyVarKind tyvars) unboxedTypeKind
247
248         tuple_con = pcDataCon dc_uniq mod_name name_str tyvars [] tyvar_tys tycon
249         tyvars    = take arity openAlphaTyVars
250         tyvar_tys = mkTyVarTys tyvars
251         (mod_name, name_str) = mkUbxTupNameStr arity
252         tc_uniq   = mkUbxTupleTyConUnique   arity
253         dc_uniq   = mkUbxTupleDataConUnique arity
254
255 unboxedPairTyCon   = unboxedTupleTyCon 2
256 unboxedPairDataCon = unboxedTupleCon 2
257 \end{code}
258
259 %************************************************************************
260 %*                                                                      *
261 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
262 %*                                                                      *
263 %************************************************************************
264
265 \begin{code}
266 -- The Void type is represented as a data type with no constructors
267 -- It's a built in type (i.e. there's no way to define it in Haskell;
268 --      the nearest would be
269 --
270 --              data Void =             -- No constructors!
271 --
272 -- ) It's boxed; there is only one value of this
273 -- type, namely "void", whose semantics is just bottom.
274
275 voidTy    = mkTyConTy voidTyCon
276 voidTyCon = pcNonRecDataTyCon voidTyConKey pREL_GHC SLIT("Void") [] [{-No data cons-}]
277
278 \end{code}
279
280 \begin{code}
281 charTy = mkTyConTy charTyCon
282
283 charTyCon = pcNonRecDataTyCon charTyConKey  pREL_BASE  SLIT("Char") [] [charDataCon]
284 charDataCon = pcDataCon charDataConKey pREL_BASE SLIT("C#") [] [] [charPrimTy] charTyCon
285
286 stringTy = mkListTy charTy -- convenience only
287 \end{code}
288
289 \begin{code}
290 intTy = mkTyConTy intTyCon 
291
292 intTyCon = pcNonRecDataTyCon intTyConKey pREL_BASE SLIT("Int") [] [intDataCon]
293 intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon
294
295 isIntTy :: Type -> Bool
296 isIntTy ty
297   = case (splitAlgTyConApp_maybe ty) of
298         Just (tycon, [], _) -> getUnique tycon == intTyConKey
299         _                   -> False
300
301 inIntRange :: Integer -> Bool   -- Tells if an integer lies in the legal range of Ints
302 inIntRange i = (min_int <= i) && (i <= max_int)
303
304 max_int, min_int :: Integer
305 max_int = toInteger maxInt  
306 min_int = toInteger minInt
307
308 int8TyCon = pcNonRecDataTyCon int8TyConKey iNT SLIT("Int8") [] [int8DataCon]
309   where
310    int8DataCon = pcDataCon int8DataConKey iNT SLIT("I8#") [] [] [intPrimTy] int8TyCon
311
312 int16TyCon = pcNonRecDataTyCon int16TyConKey iNT SLIT("Int16") [] [int16DataCon]
313   where
314    int16DataCon = pcDataCon int16DataConKey iNT SLIT("I16#") [] [] [intPrimTy] int16TyCon
315
316 int32TyCon = pcNonRecDataTyCon int32TyConKey iNT SLIT("Int32") [] [int32DataCon]
317   where
318    int32DataCon = pcDataCon int32DataConKey iNT SLIT("I32#") [] [] [intPrimTy] int32TyCon
319
320 int64Ty = mkTyConTy int64TyCon 
321
322 int64TyCon = pcNonRecDataTyCon int64TyConKey pREL_ADDR SLIT("Int64") [] [int64DataCon]
323 int64DataCon = pcDataCon int64DataConKey pREL_ADDR SLIT("I64#") [] [] [int64PrimTy] int64TyCon
324 \end{code}
325
326 \begin{code}
327
328 wordTy = mkTyConTy wordTyCon
329
330 wordTyCon = pcNonRecDataTyCon wordTyConKey   pREL_ADDR SLIT("Word") [] [wordDataCon]
331 wordDataCon = pcDataCon wordDataConKey pREL_ADDR SLIT("W#") [] [] [wordPrimTy] wordTyCon
332
333 word8TyCon = pcNonRecDataTyCon word8TyConKey   wORD SLIT("Word8") [] [word8DataCon]
334   where
335    word8DataCon = pcDataCon word8DataConKey wORD SLIT("W8#") [] [] [wordPrimTy] word8TyCon
336
337 word16TyCon = pcNonRecDataTyCon word16TyConKey   wORD SLIT("Word16") [] [word16DataCon]
338   where
339    word16DataCon = pcDataCon word16DataConKey wORD SLIT("W16#") [] [] [wordPrimTy] word16TyCon
340
341 word32TyCon = pcNonRecDataTyCon word32TyConKey   wORD SLIT("Word32") [] [word32DataCon]
342   where
343    word32DataCon = pcDataCon word32DataConKey wORD SLIT("W32#") [] [] [wordPrimTy] word32TyCon
344
345 word64Ty = mkTyConTy word64TyCon
346
347 word64TyCon = pcNonRecDataTyCon word64TyConKey   pREL_ADDR SLIT("Word64") [] [word64DataCon]
348 word64DataCon = pcDataCon word64DataConKey pREL_ADDR SLIT("W64#") [] [] [word64PrimTy] word64TyCon
349 \end{code}
350
351 \begin{code}
352 addrTy = mkTyConTy addrTyCon
353
354 addrTyCon = pcNonRecDataTyCon addrTyConKey   pREL_ADDR SLIT("Addr") [] [addrDataCon]
355 addrDataCon = pcDataCon addrDataConKey pREL_ADDR SLIT("A#") [] [] [addrPrimTy] addrTyCon
356
357 isAddrTy :: Type -> Bool
358 isAddrTy ty
359   = case (splitAlgTyConApp_maybe ty) of
360         Just (tycon, [], _) -> getUnique tycon == addrTyConKey
361         _                   -> False
362
363 \end{code}
364
365 \begin{code}
366 floatTy = mkTyConTy floatTyCon
367
368 floatTyCon = pcNonRecDataTyCon floatTyConKey pREL_BASE SLIT("Float") [] [floatDataCon]
369 floatDataCon = pcDataCon floatDataConKey pREL_BASE SLIT("F#") [] [] [floatPrimTy] floatTyCon
370
371 isFloatTy :: Type -> Bool
372 isFloatTy ty
373   = case (splitAlgTyConApp_maybe ty) of
374         Just (tycon, [], _) -> getUnique tycon == floatTyConKey
375         _                   -> False
376
377 \end{code}
378
379 \begin{code}
380 doubleTy = mkTyConTy doubleTyCon
381
382 isDoubleTy :: Type -> Bool
383 isDoubleTy ty
384   = case (splitAlgTyConApp_maybe ty) of
385         Just (tycon, [], _) -> getUnique tycon == doubleTyConKey
386         _                   -> False
387
388 doubleTyCon = pcNonRecDataTyCon doubleTyConKey pREL_BASE SLIT("Double") [] [doubleDataCon]
389 doubleDataCon = pcDataCon doubleDataConKey pREL_BASE SLIT("D#") [] [] [doublePrimTy] doubleTyCon
390 \end{code}
391
392 \begin{code}
393 mkStateTy ty     = mkTyConApp stateTyCon [ty]
394 realWorldStateTy = mkStateTy realWorldTy -- a common use
395
396 stateTyCon = pcNonRecDataTyCon stateTyConKey pREL_ST SLIT("State") alpha_tyvar [stateDataCon]
397 stateDataCon
398   = pcDataCon stateDataConKey pREL_ST SLIT("S#")
399         alpha_tyvar [] [mkStatePrimTy alphaTy] stateTyCon
400 \end{code}
401
402 \begin{code}
403 stablePtrTyCon
404   = pcNonRecDataTyCon stablePtrTyConKey pREL_FOREIGN SLIT("StablePtr")
405         alpha_tyvar [stablePtrDataCon]
406   where
407     stablePtrDataCon
408       = pcDataCon stablePtrDataConKey pREL_FOREIGN SLIT("StablePtr")
409             alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
410 \end{code}
411
412 \begin{code}
413 foreignObjTyCon
414   = pcNonRecDataTyCon foreignObjTyConKey pREL_IO_BASE SLIT("ForeignObj")
415         [] [foreignObjDataCon]
416   where
417     foreignObjDataCon
418       = pcDataCon foreignObjDataConKey pREL_IO_BASE SLIT("ForeignObj")
419             [] [] [foreignObjPrimTy] foreignObjTyCon
420 \end{code}
421
422 %************************************************************************
423 %*                                                                      *
424 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
425 %*                                                                      *
426 %************************************************************************
427
428 @Integer@ and its pals are not really primitive.  @Integer@ itself, first:
429 \begin{code}
430 integerTy :: Type
431 integerTy = mkTyConTy integerTyCon
432
433 integerTyCon = pcNonRecDataTyCon integerTyConKey pREL_BASE SLIT("Integer") [] [integerDataCon]
434
435 integerDataCon = pcDataCon integerDataConKey pREL_BASE SLIT("J#")
436                 [] [] [intPrimTy, intPrimTy, byteArrayPrimTy] integerTyCon
437
438 isIntegerTy :: Type -> Bool
439 isIntegerTy ty
440   = case (splitAlgTyConApp_maybe ty) of
441         Just (tycon, [], _) -> getUnique tycon == integerTyConKey
442         _                   -> False
443 \end{code}
444
445
446 %************************************************************************
447 %*                                                                      *
448 \subsection[TysWiredIn-ext-type]{External types}
449 %*                                                                      *
450 %************************************************************************
451
452 The compiler's foreign function interface supports the passing of a
453 restricted set of types as arguments and results (the restricting factor
454 being the )
455
456 \begin{code}
457 isFFIArgumentTy :: Type -> Bool
458 isFFIArgumentTy ty =
459   (opt_GlasgowExts && isUnLiftedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
460   case (splitAlgTyConApp_maybe ty) of
461     Just (tycon, _, _) -> (getUnique tycon) `elem` primArgTyConKeys
462     _                  -> False
463
464 -- types that can be passed as arguments to "foreign" functions
465 primArgTyConKeys 
466   = [ intTyConKey, int8TyConKey, int16TyConKey, int32TyConKey, int64TyConKey
467     , wordTyConKey, word8TyConKey, word16TyConKey, word32TyConKey, word64TyConKey
468     , floatTyConKey, doubleTyConKey
469     , addrTyConKey, charTyConKey, foreignObjTyConKey
470     , stablePtrTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey
471     ]
472
473 -- types that can be passed from the outside world into Haskell.
474 -- excludes (mutable) byteArrays.
475 isFFIExternalTy :: Type -> Bool
476 isFFIExternalTy ty = 
477   (opt_GlasgowExts && isUnLiftedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
478   case (splitAlgTyConApp_maybe ty) of
479     Just (tycon, _, _) -> 
480        let 
481         u_tycon = getUnique tycon
482        in  
483        (u_tycon `elem` primArgTyConKeys) &&
484        not (u_tycon `elem` notLegalExternalTyCons)
485     _                  -> False
486
487
488 isFFIResultTy :: Type -> Bool
489 isFFIResultTy ty =
490    not (isUnLiftedType ty) &&
491    case (splitAlgTyConApp_maybe ty) of
492     Just (tycon, _, _) -> 
493         let
494          u_tycon = getUnique tycon
495         in
496         (u_tycon == getUnique unitTyCon) ||
497         ((u_tycon `elem` primArgTyConKeys) && 
498          not (u_tycon `elem` notLegalExternalTyCons))
499     _                  -> False
500
501 -- it's illegal to return foreign objects and (mutable)
502 -- bytearrays from a _ccall_ / foreign declaration
503 -- (or be passed them as arguments in foreign exported functions).
504 notLegalExternalTyCons =
505   [ foreignObjTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey ]
506     
507 \end{code}
508
509 %************************************************************************
510 %*                                                                      *
511 \subsection[TysWiredIn-Bool]{The @Bool@ type}
512 %*                                                                      *
513 %************************************************************************
514
515 An ordinary enumeration type, but deeply wired in.  There are no
516 magical operations on @Bool@ (just the regular Prelude code).
517
518 {\em BEGIN IDLE SPECULATION BY SIMON}
519
520 This is not the only way to encode @Bool@.  A more obvious coding makes
521 @Bool@ just a boxed up version of @Bool#@, like this:
522 \begin{verbatim}
523 type Bool# = Int#
524 data Bool = MkBool Bool#
525 \end{verbatim}
526
527 Unfortunately, this doesn't correspond to what the Report says @Bool@
528 looks like!  Furthermore, we get slightly less efficient code (I
529 think) with this coding. @gtInt@ would look like this:
530
531 \begin{verbatim}
532 gtInt :: Int -> Int -> Bool
533 gtInt x y = case x of I# x# ->
534             case y of I# y# ->
535             case (gtIntPrim x# y#) of
536                 b# -> MkBool b#
537 \end{verbatim}
538
539 Notice that the result of the @gtIntPrim@ comparison has to be turned
540 into an integer (here called @b#@), and returned in a @MkBool@ box.
541
542 The @if@ expression would compile to this:
543 \begin{verbatim}
544 case (gtInt x y) of
545   MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
546 \end{verbatim}
547
548 I think this code is a little less efficient than the previous code,
549 but I'm not certain.  At all events, corresponding with the Report is
550 important.  The interesting thing is that the language is expressive
551 enough to describe more than one alternative; and that a type doesn't
552 necessarily need to be a straightforwardly boxed version of its
553 primitive counterpart.
554
555 {\em END IDLE SPECULATION BY SIMON}
556
557 \begin{code}
558 boolTy = mkTyConTy boolTyCon
559
560 boolTyCon = pcTyCon EnumType NonRecursive boolTyConKey 
561                     pREL_BASE SLIT("Bool") [] [falseDataCon, trueDataCon]
562
563 falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon
564 trueDataCon  = pcDataCon trueDataConKey  pREL_BASE SLIT("True")  [] [] [] boolTyCon
565 \end{code}
566
567 %************************************************************************
568 %*                                                                      *
569 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
570 %*                                                                      *
571 %************************************************************************
572
573 Special syntax, deeply wired in, but otherwise an ordinary algebraic
574 data types:
575 \begin{verbatim}
576 data [] a = [] | a : (List a)
577 data () = ()
578 data (,) a b = (,,) a b
579 ...
580 \end{verbatim}
581
582 \begin{code}
583 mkListTy :: Type -> Type
584 mkListTy ty = mkTyConApp listTyCon [ty]
585
586 alphaListTy = mkSigmaTy alpha_tyvar [] (mkTyConApp listTyCon alpha_ty)
587
588 listTyCon = pcRecDataTyCon listTyConKey pREL_BASE SLIT("[]") 
589                         alpha_tyvar [nilDataCon, consDataCon]
590
591 nilDataCon  = pcDataCon nilDataConKey  pREL_BASE SLIT("[]") alpha_tyvar [] [] listTyCon
592 consDataCon = pcDataCon consDataConKey pREL_BASE SLIT(":")
593                 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
594 -- Interesting: polymorphic recursion would help here.
595 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
596 -- gets the over-specific type (Type -> Type)
597 \end{code}
598
599 %************************************************************************
600 %*                                                                      *
601 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
602 %*                                                                      *
603 %************************************************************************
604
605 The tuple types are definitely magic, because they form an infinite
606 family.
607
608 \begin{itemize}
609 \item
610 They have a special family of type constructors, of type @TyCon@
611 These contain the tycon arity, but don't require a Unique.
612
613 \item
614 They have a special family of constructors, of type
615 @Id@. Again these contain their arity but don't need a Unique.
616
617 \item
618 There should be a magic way of generating the info tables and
619 entry code for all tuples.
620
621 But at the moment we just compile a Haskell source
622 file\srcloc{lib/prelude/...} containing declarations like:
623 \begin{verbatim}
624 data Tuple0             = Tup0
625 data Tuple2  a b        = Tup2  a b
626 data Tuple3  a b c      = Tup3  a b c
627 data Tuple4  a b c d    = Tup4  a b c d
628 ...
629 \end{verbatim}
630 The print-names associated with the magic @Id@s for tuple constructors
631 ``just happen'' to be the same as those generated by these
632 declarations.
633
634 \item
635 The instance environment should have a magic way to know
636 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
637 so on. \ToDo{Not implemented yet.}
638
639 \item
640 There should also be a way to generate the appropriate code for each
641 of these instances, but (like the info tables and entry code) it is
642 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
643 \end{itemize}
644
645 \begin{code}
646 mkTupleTy :: Int -> [Type] -> Type
647 mkTupleTy arity tys = mkTyConApp (tupleTyCon arity) tys
648
649 mkUnboxedTupleTy :: Int -> [Type] -> Type
650 mkUnboxedTupleTy arity tys = mkTyConApp (unboxedTupleTyCon arity) tys
651
652 unitTy    = mkTupleTy 0 []
653 \end{code}