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