[project @ 1998-12-02 13:17:09 by simonm]
[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             ( mkWiredInTyConName, mkWiredInIdName, mkTupNameStr,
106                           mkUbxTupNameStr )
107 import DataCon          ( DataCon, mkDataCon )
108 import Var              ( TyVar, tyVarKind )
109 import TyCon            ( TyCon, mkAlgTyCon, mkSynTyCon, mkTupleTyCon )
110 import BasicTypes       ( Module, Arity, NewOrData(..), 
111                           RecFlag(..), StrictnessMark(..) )
112 import Type             ( Type, mkTyConTy, mkTyConApp, mkSigmaTy, mkTyVarTys, 
113                           mkArrowKinds, boxedTypeKind, unboxedTypeKind,
114                           mkFunTy, mkFunTys, isUnLiftedType,
115                           splitTyConApp_maybe, splitAlgTyConApp_maybe,
116                           GenType(..), ThetaType, TauType )
117 import PrimRep          ( PrimRep(..) )
118 import Unique
119 import CmdLineOpts      ( opt_GlasgowExts )
120 import Util             ( assoc, 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 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 voidTy    = mkTyConTy voidTyCon
275 voidTyCon = pcNonRecDataTyCon voidTyConKey pREL_GHC SLIT("Void") [] [{-No data cons-}]
276 \end{code}
277
278 \begin{code}
279 charTy = mkTyConTy charTyCon
280
281 charTyCon = pcNonRecDataTyCon charTyConKey  pREL_BASE  SLIT("Char") [] [charDataCon]
282 charDataCon = pcDataCon charDataConKey pREL_BASE SLIT("C#") [] [] [charPrimTy] charTyCon
283
284 stringTy = mkListTy charTy -- convenience only
285 \end{code}
286
287 \begin{code}
288 intTy = mkTyConTy intTyCon 
289
290 intTyCon = pcNonRecDataTyCon intTyConKey pREL_BASE SLIT("Int") [] [intDataCon]
291 intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon
292
293 isIntTy :: GenType flexi -> Bool
294 isIntTy ty
295   = case (splitAlgTyConApp_maybe ty) of
296         Just (tycon, [], _) -> getUnique tycon == intTyConKey
297         _                   -> False
298
299 inIntRange :: Integer -> Bool   -- Tells if an integer lies in the legal range of Ints
300 inIntRange i = (min_int <= i) && (i <= max_int)
301
302 max_int, min_int :: Integer
303 max_int = toInteger maxInt  
304 min_int = toInteger minInt
305
306 int8TyCon = pcNonRecDataTyCon int8TyConKey iNT SLIT("Int8") [] [int8DataCon]
307   where
308    int8DataCon = pcDataCon int8DataConKey iNT SLIT("I8#") [] [] [intPrimTy] int8TyCon
309
310 int16TyCon = pcNonRecDataTyCon int16TyConKey iNT SLIT("Int16") [] [int16DataCon]
311   where
312    int16DataCon = pcDataCon int16DataConKey iNT SLIT("I16#") [] [] [intPrimTy] int16TyCon
313
314 int32TyCon = pcNonRecDataTyCon int32TyConKey iNT SLIT("Int32") [] [int32DataCon]
315   where
316    int32DataCon = pcDataCon int32DataConKey iNT SLIT("I32#") [] [] [intPrimTy] int32TyCon
317
318 int64Ty = mkTyConTy int64TyCon 
319
320 int64TyCon = pcNonRecDataTyCon int64TyConKey pREL_ADDR SLIT("Int64") [] [int64DataCon]
321 int64DataCon = pcDataCon int64DataConKey pREL_ADDR SLIT("I64#") [] [] [int64PrimTy] int64TyCon
322 \end{code}
323
324 \begin{code}
325
326 wordTy = mkTyConTy wordTyCon
327
328 wordTyCon = pcNonRecDataTyCon wordTyConKey   pREL_ADDR SLIT("Word") [] [wordDataCon]
329 wordDataCon = pcDataCon wordDataConKey pREL_ADDR SLIT("W#") [] [] [wordPrimTy] wordTyCon
330
331 word8TyCon = pcNonRecDataTyCon word8TyConKey   wORD SLIT("Word8") [] [word8DataCon]
332   where
333    word8DataCon = pcDataCon word8DataConKey wORD SLIT("W8#") [] [] [wordPrimTy] word8TyCon
334
335 word16TyCon = pcNonRecDataTyCon word16TyConKey   wORD SLIT("Word16") [] [word16DataCon]
336   where
337    word16DataCon = pcDataCon word16DataConKey wORD SLIT("W16#") [] [] [wordPrimTy] word16TyCon
338
339 word32TyCon = pcNonRecDataTyCon word32TyConKey   wORD SLIT("Word32") [] [word32DataCon]
340   where
341    word32DataCon = pcDataCon word32DataConKey wORD SLIT("W32#") [] [] [wordPrimTy] word32TyCon
342
343 word64Ty = mkTyConTy word64TyCon
344
345 word64TyCon = pcNonRecDataTyCon word64TyConKey   pREL_ADDR SLIT("Word64") [] [word64DataCon]
346 word64DataCon = pcDataCon word64DataConKey pREL_ADDR SLIT("W64#") [] [] [word64PrimTy] word64TyCon
347 \end{code}
348
349 \begin{code}
350 addrTy = mkTyConTy addrTyCon
351
352 addrTyCon = pcNonRecDataTyCon addrTyConKey   pREL_ADDR SLIT("Addr") [] [addrDataCon]
353 addrDataCon = pcDataCon addrDataConKey pREL_ADDR SLIT("A#") [] [] [addrPrimTy] addrTyCon
354
355 isAddrTy :: GenType flexi -> Bool
356 isAddrTy ty
357   = case (splitAlgTyConApp_maybe ty) of
358         Just (tycon, [], _) -> getUnique tycon == addrTyConKey
359         _                   -> False
360
361 \end{code}
362
363 \begin{code}
364 floatTy = mkTyConTy floatTyCon
365
366 floatTyCon = pcNonRecDataTyCon floatTyConKey pREL_BASE SLIT("Float") [] [floatDataCon]
367 floatDataCon = pcDataCon floatDataConKey pREL_BASE SLIT("F#") [] [] [floatPrimTy] floatTyCon
368
369 isFloatTy :: GenType flexi -> Bool
370 isFloatTy ty
371   = case (splitAlgTyConApp_maybe ty) of
372         Just (tycon, [], _) -> getUnique tycon == floatTyConKey
373         _                   -> False
374
375 \end{code}
376
377 \begin{code}
378 doubleTy = mkTyConTy doubleTyCon
379
380 isDoubleTy :: GenType flexi -> Bool
381 isDoubleTy ty
382   = case (splitAlgTyConApp_maybe ty) of
383         Just (tycon, [], _) -> getUnique tycon == doubleTyConKey
384         _                   -> False
385
386 doubleTyCon = pcNonRecDataTyCon doubleTyConKey pREL_BASE SLIT("Double") [] [doubleDataCon]
387 doubleDataCon = pcDataCon doubleDataConKey pREL_BASE SLIT("D#") [] [] [doublePrimTy] doubleTyCon
388 \end{code}
389
390 \begin{code}
391 mkStateTy ty     = mkTyConApp stateTyCon [ty]
392 realWorldStateTy = mkStateTy realWorldTy -- a common use
393
394 stateTyCon = pcNonRecDataTyCon stateTyConKey pREL_ST SLIT("State") alpha_tyvar [stateDataCon]
395 stateDataCon
396   = pcDataCon stateDataConKey pREL_ST SLIT("S#")
397         alpha_tyvar [] [mkStatePrimTy alphaTy] stateTyCon
398 \end{code}
399
400 \begin{code}
401 stablePtrTyCon
402   = pcNonRecDataTyCon stablePtrTyConKey pREL_FOREIGN SLIT("StablePtr")
403         alpha_tyvar [stablePtrDataCon]
404   where
405     stablePtrDataCon
406       = pcDataCon stablePtrDataConKey pREL_FOREIGN SLIT("StablePtr")
407             alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
408 \end{code}
409
410 \begin{code}
411 foreignObjTyCon
412   = pcNonRecDataTyCon foreignObjTyConKey pREL_IO_BASE SLIT("ForeignObj")
413         [] [foreignObjDataCon]
414   where
415     foreignObjDataCon
416       = pcDataCon foreignObjDataConKey pREL_IO_BASE SLIT("ForeignObj")
417             [] [] [foreignObjPrimTy] foreignObjTyCon
418 \end{code}
419
420 %************************************************************************
421 %*                                                                      *
422 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
423 %*                                                                      *
424 %************************************************************************
425
426 @Integer@ and its pals are not really primitive.  @Integer@ itself, first:
427 \begin{code}
428 integerTy :: GenType t
429 integerTy    = mkTyConTy integerTyCon
430
431 integerTyCon = pcNonRecDataTyCon integerTyConKey pREL_BASE SLIT("Integer") [] [integerDataCon]
432
433 integerDataCon = pcDataCon integerDataConKey pREL_BASE SLIT("J#")
434                 [] [] [intPrimTy, intPrimTy, byteArrayPrimTy] integerTyCon
435
436 isIntegerTy :: GenType flexi -> Bool
437 isIntegerTy ty
438   = case (splitAlgTyConApp_maybe ty) of
439         Just (tycon, [], _) -> getUnique tycon == integerTyConKey
440         _                   -> False
441 \end{code}
442
443
444 %************************************************************************
445 %*                                                                      *
446 \subsection[TysWiredIn-ext-type]{External types}
447 %*                                                                      *
448 %************************************************************************
449
450 The compiler's foreign function interface supports the passing of a
451 restricted set of types as arguments and results (the restricting factor
452 being the )
453
454 \begin{code}
455 isFFIArgumentTy :: Type -> Bool
456 isFFIArgumentTy ty =
457   (opt_GlasgowExts && isUnLiftedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
458   case (splitAlgTyConApp_maybe ty) of
459     Just (tycon, _, _) -> (getUnique tycon) `elem` primArgTyConKeys
460     _                  -> False
461
462 -- types that can be passed as arguments to "foreign" functions
463 primArgTyConKeys 
464   = [ intTyConKey, int8TyConKey, int16TyConKey, int32TyConKey, int64TyConKey
465     , wordTyConKey, word8TyConKey, word16TyConKey, word32TyConKey, word64TyConKey
466     , floatTyConKey, doubleTyConKey
467     , addrTyConKey, charTyConKey, foreignObjTyConKey
468     , stablePtrTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey
469     ]
470
471 -- types that can be passed from the outside world into Haskell.
472 -- excludes (mutable) byteArrays.
473 isFFIExternalTy :: Type -> Bool
474 isFFIExternalTy ty = 
475   (opt_GlasgowExts && isUnLiftedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
476   case (splitAlgTyConApp_maybe ty) of
477     Just (tycon, _, _) -> 
478        let 
479         u_tycon = getUnique tycon
480        in  
481        (u_tycon `elem` primArgTyConKeys) &&
482        not (u_tycon `elem` notLegalExternalTyCons)
483     _                  -> False
484
485
486 isFFIResultTy :: Type -> Bool
487 isFFIResultTy ty =
488    not (isUnLiftedType ty) &&
489    case (splitAlgTyConApp_maybe ty) of
490     Just (tycon, _, _) -> 
491         let
492          u_tycon = getUnique tycon
493         in
494         (u_tycon == getUnique unitTyCon) ||
495         ((u_tycon `elem` primArgTyConKeys) && 
496          not (u_tycon `elem` notLegalExternalTyCons))
497     _                  -> False
498
499 -- it's illegal to return foreign objects and (mutable)
500 -- bytearrays from a _ccall_ / foreign declaration
501 -- (or be passed them as arguments in foreign exported functions).
502 notLegalExternalTyCons =
503   [ foreignObjTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey ]
504     
505 \end{code}
506
507 %************************************************************************
508 %*                                                                      *
509 \subsection[TysWiredIn-Bool]{The @Bool@ type}
510 %*                                                                      *
511 %************************************************************************
512
513 An ordinary enumeration type, but deeply wired in.  There are no
514 magical operations on @Bool@ (just the regular Prelude code).
515
516 {\em BEGIN IDLE SPECULATION BY SIMON}
517
518 This is not the only way to encode @Bool@.  A more obvious coding makes
519 @Bool@ just a boxed up version of @Bool#@, like this:
520 \begin{verbatim}
521 type Bool# = Int#
522 data Bool = MkBool Bool#
523 \end{verbatim}
524
525 Unfortunately, this doesn't correspond to what the Report says @Bool@
526 looks like!  Furthermore, we get slightly less efficient code (I
527 think) with this coding. @gtInt@ would look like this:
528
529 \begin{verbatim}
530 gtInt :: Int -> Int -> Bool
531 gtInt x y = case x of I# x# ->
532             case y of I# y# ->
533             case (gtIntPrim x# y#) of
534                 b# -> MkBool b#
535 \end{verbatim}
536
537 Notice that the result of the @gtIntPrim@ comparison has to be turned
538 into an integer (here called @b#@), and returned in a @MkBool@ box.
539
540 The @if@ expression would compile to this:
541 \begin{verbatim}
542 case (gtInt x y) of
543   MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
544 \end{verbatim}
545
546 I think this code is a little less efficient than the previous code,
547 but I'm not certain.  At all events, corresponding with the Report is
548 important.  The interesting thing is that the language is expressive
549 enough to describe more than one alternative; and that a type doesn't
550 necessarily need to be a straightforwardly boxed version of its
551 primitive counterpart.
552
553 {\em END IDLE SPECULATION BY SIMON}
554
555 \begin{code}
556 boolTy = mkTyConTy boolTyCon
557
558 boolTyCon = pcTyCon EnumType NonRecursive boolTyConKey 
559                     pREL_BASE SLIT("Bool") [] [falseDataCon, trueDataCon]
560
561 falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon
562 trueDataCon  = pcDataCon trueDataConKey  pREL_BASE SLIT("True")  [] [] [] boolTyCon
563 \end{code}
564
565 %************************************************************************
566 %*                                                                      *
567 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
568 %*                                                                      *
569 %************************************************************************
570
571 Special syntax, deeply wired in, but otherwise an ordinary algebraic
572 data types:
573 \begin{verbatim}
574 data [] a = [] | a : (List a)
575 data () = ()
576 data (,) a b = (,,) a b
577 ...
578 \end{verbatim}
579
580 \begin{code}
581 mkListTy :: GenType t -> GenType t
582 mkListTy ty = mkTyConApp listTyCon [ty]
583
584 alphaListTy = mkSigmaTy alpha_tyvar [] (mkTyConApp listTyCon alpha_ty)
585
586 listTyCon = pcRecDataTyCon listTyConKey pREL_BASE SLIT("[]") 
587                         alpha_tyvar [nilDataCon, consDataCon]
588
589 nilDataCon  = pcDataCon nilDataConKey  pREL_BASE SLIT("[]") alpha_tyvar [] [] listTyCon
590 consDataCon = pcDataCon consDataConKey pREL_BASE SLIT(":")
591                 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
592 -- Interesting: polymorphic recursion would help here.
593 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
594 -- gets the over-specific type (Type -> Type)
595 \end{code}
596
597 %************************************************************************
598 %*                                                                      *
599 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
600 %*                                                                      *
601 %************************************************************************
602
603 The tuple types are definitely magic, because they form an infinite
604 family.
605
606 \begin{itemize}
607 \item
608 They have a special family of type constructors, of type @TyCon@
609 These contain the tycon arity, but don't require a Unique.
610
611 \item
612 They have a special family of constructors, of type
613 @Id@. Again these contain their arity but don't need a Unique.
614
615 \item
616 There should be a magic way of generating the info tables and
617 entry code for all tuples.
618
619 But at the moment we just compile a Haskell source
620 file\srcloc{lib/prelude/...} containing declarations like:
621 \begin{verbatim}
622 data Tuple0             = Tup0
623 data Tuple2  a b        = Tup2  a b
624 data Tuple3  a b c      = Tup3  a b c
625 data Tuple4  a b c d    = Tup4  a b c d
626 ...
627 \end{verbatim}
628 The print-names associated with the magic @Id@s for tuple constructors
629 ``just happen'' to be the same as those generated by these
630 declarations.
631
632 \item
633 The instance environment should have a magic way to know
634 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
635 so on. \ToDo{Not implemented yet.}
636
637 \item
638 There should also be a way to generate the appropriate code for each
639 of these instances, but (like the info tables and entry code) it is
640 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
641 \end{itemize}
642
643 \begin{code}
644 mkTupleTy :: Int -> [GenType t] -> GenType t
645 mkTupleTy arity tys = mkTyConApp (tupleTyCon arity) tys
646
647 mkUnboxedTupleTy :: Int -> [GenType t] -> GenType t
648 mkUnboxedTupleTy arity tys = mkTyConApp (unboxedTupleTyCon arity) tys
649
650 unitTy    = mkTupleTy 0 []
651 \end{code}