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