[project @ 2001-08-04 06:19:54 by ken]
[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         wiredInTyCons, genericTyCons,
15
16         addrDataCon,
17         addrTy,
18         addrTyCon,
19         ptrDataCon,
20         ptrTy,
21         ptrTyCon,
22         funPtrDataCon,
23         funPtrTy,
24         funPtrTyCon,
25         boolTy,
26         boolTyCon,
27         charDataCon,
28         charTy,
29         charTyCon,
30         consDataCon,
31         doubleDataCon,
32         doubleTy,
33         doubleTyCon,
34         falseDataCon, falseDataConId,
35         floatDataCon,
36         floatTy,
37         floatTyCon,
38
39         intDataCon,
40         intTy,
41         intTyCon,
42
43         integerTy,
44         integerTyCon,
45         smallIntegerDataCon,
46         largeIntegerDataCon,
47
48         listTyCon,
49
50         mkListTy,
51         nilDataCon,
52
53         -- tuples
54         mkTupleTy,
55         tupleTyCon, tupleCon, 
56         unitTyCon, unitDataConId, pairTyCon, 
57         unboxedSingletonTyCon, unboxedSingletonDataCon,
58         unboxedPairTyCon, unboxedPairDataCon,
59
60         -- Generics
61         genUnitTyCon, genUnitDataCon, 
62         plusTyCon, inrDataCon, inlDataCon,
63         crossTyCon, crossDataCon,
64
65         stablePtrTyCon,
66         stringTy,
67         trueDataCon, trueDataConId,
68         unitTy,
69         voidTy,
70         wordDataCon,
71         wordTy,
72         wordTyCon,
73
74         isFFIArgumentTy,     -- :: DynFlags -> Safety -> Type -> Bool
75         isFFIImportResultTy, -- :: DynFlags -> Type -> Bool
76         isFFIExportResultTy, -- :: Type -> Bool
77         isFFIExternalTy,     -- :: Type -> Bool
78         isFFIDynArgumentTy,  -- :: Type -> Bool
79         isFFIDynResultTy,    -- :: Type -> Bool
80         isFFILabelTy,        -- :: Type -> Bool
81     ) where
82
83 #include "HsVersions.h"
84
85 import {-# SOURCE #-} MkId( mkDataConId, mkDataConWrapId )
86 import {-# SOURCE #-} Generics( mkTyConGenInfo )
87
88 -- friends:
89 import PrelNames
90 import TysPrim
91
92 -- others:
93 import ForeignCall      ( Safety, playSafe )
94 import Constants        ( mAX_TUPLE_SIZE )
95 import Module           ( mkPrelModule )
96 import Name             ( Name, nameRdrName, nameUnique, nameOccName, 
97                           nameModule, mkWiredInName )
98 import OccName          ( mkOccFS, tcName, dataName, mkWorkerOcc, mkGenOcc1, mkGenOcc2 )
99 import RdrName          ( rdrNameOcc )
100 import DataCon          ( DataCon, mkDataCon, dataConId )
101 import Var              ( TyVar, tyVarKind )
102 import TyCon            ( TyCon, AlgTyConFlavour(..), tyConDataCons,
103                           mkTupleTyCon, isUnLiftedTyCon, mkAlgTyCon
104                         )
105
106 import BasicTypes       ( Arity, RecFlag(..), Boxity(..), isBoxed, StrictnessMark(..) )
107
108 import Type             ( Type, mkTyConTy, mkTyConApp, mkTyVarTys, 
109                           mkArrowKinds, liftedTypeKind, unliftedTypeKind,
110                           splitTyConApp_maybe,
111                           TauType, ThetaType )
112 import Unique           ( incrUnique, mkTupleTyConUnique, mkTupleDataConUnique )
113 import PrelNames
114 import CmdLineOpts
115 import Array
116
117 alpha_tyvar       = [alphaTyVar]
118 alpha_ty          = [alphaTy]
119 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
120 \end{code}
121
122
123 %************************************************************************
124 %*                                                                      *
125 \subsection{Wired in type constructors}
126 %*                                                                      *
127 %************************************************************************
128
129 \begin{code}
130 wiredInTyCons :: [TyCon]
131 wiredInTyCons = data_tycons ++ tuple_tycons ++ unboxed_tuple_tycons
132
133 data_tycons = genericTyCons ++
134               [ addrTyCon
135               , ptrTyCon
136               , funPtrTyCon
137               , boolTyCon
138               , charTyCon
139               , doubleTyCon
140               , floatTyCon
141               , intTyCon
142               , integerTyCon
143               , listTyCon
144               , wordTyCon
145               ]
146
147 genericTyCons :: [TyCon]
148 genericTyCons = [ plusTyCon, crossTyCon, genUnitTyCon ]
149
150
151 tuple_tycons = unitTyCon : [tupleTyCon Boxed   i | i <- [2..mAX_TUPLE_SIZE] ]
152 unboxed_tuple_tycons     = [tupleTyCon Unboxed i | i <- [1..mAX_TUPLE_SIZE] ]
153 \end{code}
154
155
156 %************************************************************************
157 %*                                                                      *
158 \subsection{mkWiredInTyCon}
159 %*                                                                      *
160 %************************************************************************
161
162 \begin{code}
163 pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
164 pcRecDataTyCon = pcTyCon DataTyCon Recursive
165
166 pcTyCon new_or_data is_rec name tyvars argvrcs cons
167   = tycon
168   where
169     tycon = mkAlgTyCon name kind
170                 tyvars
171                 []              -- No context
172                 argvrcs
173                 cons
174                 (length cons) 
175                 []              -- No record selectors
176                 new_or_data
177                 is_rec
178                 gen_info
179
180     mod      = nameModule name
181     kind     = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
182     gen_info = mk_tc_gen_info mod (nameUnique name) name tycon
183
184 -- We generate names for the generic to/from Ids by incrementing
185 -- the TyCon unique.  So each Prelude tycon needs 3 slots, one
186 -- for itself and two more for the generic Ids.
187 mk_tc_gen_info mod tc_uniq tc_name tycon
188   = mkTyConGenInfo tycon [name1, name2]
189   where
190         tc_occ_name = nameOccName tc_name
191         occ_name1   = mkGenOcc1 tc_occ_name
192         occ_name2   = mkGenOcc2 tc_occ_name
193         fn1_key     = incrUnique tc_uniq
194         fn2_key     = incrUnique fn1_key
195         name1       = mkWiredInName  mod occ_name1 fn1_key
196         name2       = mkWiredInName  mod occ_name2 fn2_key
197
198 pcDataCon :: Name -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> DataCon
199 -- The unique is the first of two free uniques;
200 -- the first is used for the datacon itself and the worker;
201 -- the second is used for the wrapper.
202
203 pcDataCon name tyvars context arg_tys tycon
204   = data_con
205   where
206     data_con = mkDataCon name
207                 [ NotMarkedStrict | a <- arg_tys ]
208                 [ {- no labelled fields -} ]
209                 tyvars context [] [] arg_tys tycon work_id wrap_id
210
211     wrap_rdr  = nameRdrName name
212     wrap_occ  = rdrNameOcc wrap_rdr
213
214     mod       = nameModule name
215     wrap_id   = mkDataConWrapId data_con
216
217     work_occ  = mkWorkerOcc wrap_occ
218     work_key  = incrUnique (nameUnique name)
219     work_name = mkWiredInName mod work_occ work_key
220     work_id   = mkDataConId work_name data_con
221 \end{code}
222
223
224 %************************************************************************
225 %*                                                                      *
226 \subsection[TysWiredIn-tuples]{The tuple types}
227 %*                                                                      *
228 %************************************************************************
229
230 \begin{code}
231 tupleTyCon :: Boxity -> Arity -> TyCon
232 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i)      -- Build one specially
233 tupleTyCon Boxed   i = fst (boxedTupleArr   ! i)
234 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
235
236 tupleCon :: Boxity -> Arity -> DataCon
237 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i)        -- Build one specially
238 tupleCon Boxed   i = snd (boxedTupleArr   ! i)
239 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
240
241 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
242 boxedTupleArr   = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Boxed i)   | i <- [0..mAX_TUPLE_SIZE]]
243 unboxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Unboxed i) | i <- [0..mAX_TUPLE_SIZE]]
244
245 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
246 mk_tuple boxity arity = (tycon, tuple_con)
247   where
248         tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info 
249         tc_name = mkWiredInName mod (mkOccFS tcName name_str) tc_uniq
250         tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
251         res_kind | isBoxed boxity = liftedTypeKind
252                  | otherwise      = unliftedTypeKind
253
254         tyvars   | isBoxed boxity = take arity alphaTyVars
255                  | otherwise      = take arity openAlphaTyVars
256
257         tuple_con = pcDataCon name tyvars [] tyvar_tys tycon
258         tyvar_tys = mkTyVarTys tyvars
259         (mod_name, name_str) = mkTupNameStr boxity arity
260         name      = mkWiredInName mod (mkOccFS dataName name_str) dc_uniq
261         tc_uniq   = mkTupleTyConUnique   boxity arity
262         dc_uniq   = mkTupleDataConUnique boxity arity
263         mod       = mkPrelModule mod_name
264         gen_info  = mk_tc_gen_info mod tc_uniq tc_name tycon
265
266 unitTyCon     = tupleTyCon Boxed 0
267 unitDataConId = dataConId (head (tyConDataCons unitTyCon))
268
269 pairTyCon = tupleTyCon Boxed 2
270
271 unboxedSingletonTyCon   = tupleTyCon Unboxed 1
272 unboxedSingletonDataCon = tupleCon   Unboxed 1
273
274 unboxedPairTyCon   = tupleTyCon Unboxed 2
275 unboxedPairDataCon = tupleCon   Unboxed 2
276 \end{code}
277
278 %************************************************************************
279 %*                                                                      *
280 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
281 %*                                                                      *
282 %************************************************************************
283
284 \begin{code}
285 -- The Void type is represented as a data type with no constructors
286 -- It's a built in type (i.e. there's no way to define it in Haskell;
287 --      the nearest would be
288 --
289 --              data Void =             -- No constructors!
290 --
291 -- ) It's lifted; there is only one value of this
292 -- type, namely "void", whose semantics is just bottom.
293 --
294 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
295 -- voidTy using ().
296 voidTy = unitTy
297 \end{code}
298
299
300 \begin{code}
301 charTy = mkTyConTy charTyCon
302
303 charTyCon   = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
304 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
305
306 stringTy = mkListTy charTy -- convenience only
307 \end{code}
308
309 \begin{code}
310 intTy = mkTyConTy intTyCon 
311
312 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
313 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
314 \end{code}
315
316 \begin{code}
317 wordTy = mkTyConTy wordTyCon
318
319 wordTyCon = pcNonRecDataTyCon wordTyConName [] [] [wordDataCon]
320 wordDataCon = pcDataCon wordDataConName [] [] [wordPrimTy] wordTyCon
321 \end{code}
322
323 \begin{code}
324 addrTy = mkTyConTy addrTyCon
325
326 addrTyCon = pcNonRecDataTyCon addrTyConName [] [] [addrDataCon]
327 addrDataCon = pcDataCon addrDataConName [] [] [addrPrimTy] addrTyCon
328 \end{code}
329
330 \begin{code}
331 ptrTy = mkTyConTy ptrTyCon
332
333 ptrTyCon = pcNonRecDataTyCon ptrTyConName alpha_tyvar [(True,False)] [ptrDataCon]
334 ptrDataCon = pcDataCon ptrDataConName alpha_tyvar [] [addrPrimTy] ptrTyCon
335 \end{code}
336
337 \begin{code}
338 funPtrTy = mkTyConTy funPtrTyCon
339
340 funPtrTyCon = pcNonRecDataTyCon funPtrTyConName alpha_tyvar [(True,False)] [funPtrDataCon]
341 funPtrDataCon = pcDataCon funPtrDataConName alpha_tyvar [] [addrPrimTy] funPtrTyCon
342 \end{code}
343
344 \begin{code}
345 floatTy = mkTyConTy floatTyCon
346
347 floatTyCon   = pcNonRecDataTyCon floatTyConName   [] [] [floatDataCon]
348 floatDataCon = pcDataCon         floatDataConName [] [] [floatPrimTy] floatTyCon
349 \end{code}
350
351 \begin{code}
352 doubleTy = mkTyConTy doubleTyCon
353
354 doubleTyCon   = pcNonRecDataTyCon doubleTyConName     [] [] [doubleDataCon]
355 doubleDataCon = pcDataCon         doubleDataConName [] [] [doublePrimTy] doubleTyCon
356 \end{code}
357
358 \begin{code}
359 stablePtrTyCon
360   = pcNonRecDataTyCon stablePtrTyConName
361         alpha_tyvar [(True,False)] [stablePtrDataCon]
362   where
363     stablePtrDataCon
364       = pcDataCon stablePtrDataConName
365             alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
366 \end{code}
367
368 \begin{code}
369 foreignObjTyCon
370   = pcNonRecDataTyCon foreignObjTyConName
371         [] [] [foreignObjDataCon]
372   where
373     foreignObjDataCon
374       = pcDataCon foreignObjDataConName
375             [] [] [foreignObjPrimTy] foreignObjTyCon
376 \end{code}
377
378 \begin{code}
379 foreignPtrTyCon
380   = pcNonRecDataTyCon foreignPtrTyConName
381         alpha_tyvar  [(True,False)] [foreignPtrDataCon]
382   where
383     foreignPtrDataCon
384       = pcDataCon foreignPtrDataConName
385             alpha_tyvar [] [foreignObjPrimTy] foreignPtrTyCon
386 \end{code}
387
388 %************************************************************************
389 %*                                                                      *
390 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
391 %*                                                                      *
392 %************************************************************************
393
394 @Integer@ and its pals are not really primitive.  @Integer@ itself, first:
395 \begin{code}
396 integerTy :: Type
397 integerTy = mkTyConTy integerTyCon
398
399 integerTyCon = pcNonRecDataTyCon integerTyConName
400                    [] [] [smallIntegerDataCon, largeIntegerDataCon]
401
402 smallIntegerDataCon = pcDataCon smallIntegerDataConName
403                 [] [] [intPrimTy] integerTyCon
404 largeIntegerDataCon = pcDataCon largeIntegerDataConName
405                 [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
406 \end{code}
407
408
409 %************************************************************************
410 %*                                                                      *
411 \subsection[TysWiredIn-ext-type]{External types}
412 %*                                                                      *
413 %************************************************************************
414
415 The compiler's foreign function interface supports the passing of a
416 restricted set of types as arguments and results (the restricting factor
417 being the )
418
419 \begin{code}
420 isFFIArgumentTy :: DynFlags -> Safety -> Type -> Bool
421 -- Checks for valid argument type for a 'foreign import'
422 isFFIArgumentTy dflags safety ty 
423    = checkRepTyCon (legalOutgoingTyCon dflags safety) ty
424
425 isFFIExternalTy :: Type -> Bool
426 -- Types that are allowed as arguments of a 'foreign export'
427 isFFIExternalTy ty = checkRepTyCon legalFEArgTyCon ty
428
429 isFFIImportResultTy :: DynFlags -> Type -> Bool
430 isFFIImportResultTy dflags ty 
431   = checkRepTyCon (legalFIResultTyCon dflags) ty
432
433 isFFIExportResultTy :: Type -> Bool
434 isFFIExportResultTy ty = checkRepTyCon legalFEResultTyCon ty
435
436 isFFIDynArgumentTy :: Type -> Bool
437 -- The argument type of a foreign import dynamic must be Ptr, FunPtr, Addr,
438 -- or a newtype of either.
439 isFFIDynArgumentTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == funPtrTyCon || tc == addrTyCon)
440
441 isFFIDynResultTy :: Type -> Bool
442 -- The result type of a foreign export dynamic must be Ptr, FunPtr, Addr,
443 -- or a newtype of either.
444 isFFIDynResultTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == funPtrTyCon || tc == addrTyCon)
445
446 isFFILabelTy :: Type -> Bool
447 -- The type of a foreign label must be Ptr, FunPtr, Addr,
448 -- or a newtype of either.
449 isFFILabelTy = checkRepTyCon (\tc -> tc == ptrTyCon || tc == funPtrTyCon || tc == addrTyCon)
450
451 checkRepTyCon :: (TyCon -> Bool) -> Type -> Bool
452         -- Look through newtypes
453 checkRepTyCon check_tc ty = case splitTyConApp_maybe ty of
454                                 Just (tycon, _) -> check_tc tycon
455                                 Nothing         -> False
456 \end{code}
457
458 ----------------------------------------------
459 These chaps do the work; they are not exported
460 ----------------------------------------------
461
462 \begin{code}
463 legalFEArgTyCon :: TyCon -> Bool
464 -- It's illegal to return foreign objects and (mutable)
465 -- bytearrays from a _ccall_ / foreign declaration
466 -- (or be passed them as arguments in foreign exported functions).
467 legalFEArgTyCon tc
468   | getUnique tc `elem` [ foreignObjTyConKey, foreignPtrTyConKey,
469                           byteArrayTyConKey, mutableByteArrayTyConKey ] 
470   = False
471   -- It's also illegal to make foreign exports that take unboxed
472   -- arguments.  The RTS API currently can't invoke such things.  --SDM 7/2000
473   | otherwise
474   = boxedMarshalableTyCon tc
475
476 legalFIResultTyCon :: DynFlags -> TyCon -> Bool
477 legalFIResultTyCon dflags tc
478   | getUnique tc `elem`
479         [ foreignObjTyConKey, foreignPtrTyConKey,
480           byteArrayTyConKey, mutableByteArrayTyConKey ]  = False
481   | tc == unitTyCon = True
482   | otherwise       = marshalableTyCon dflags tc
483
484 legalFEResultTyCon :: TyCon -> Bool
485 legalFEResultTyCon tc
486   | getUnique tc `elem` 
487         [ foreignObjTyConKey, foreignPtrTyConKey,
488           byteArrayTyConKey, mutableByteArrayTyConKey ]  = False
489   | tc == unitTyCon = True
490   | otherwise       = boxedMarshalableTyCon tc
491
492 legalOutgoingTyCon :: DynFlags -> Safety -> TyCon -> Bool
493 -- Checks validity of types going from Haskell -> external world
494 legalOutgoingTyCon dflags safety tc
495   | playSafe safety && getUnique tc `elem` [byteArrayTyConKey, mutableByteArrayTyConKey]
496   = False
497   | otherwise
498   = marshalableTyCon dflags tc
499
500 marshalableTyCon dflags tc
501   =  (dopt Opt_GlasgowExts dflags && isUnLiftedTyCon tc)
502   || boxedMarshalableTyCon tc
503
504 boxedMarshalableTyCon tc
505    = getUnique tc `elem` [ intTyConKey, int8TyConKey, int16TyConKey
506                          , int32TyConKey, int64TyConKey
507                          , wordTyConKey, word8TyConKey, word16TyConKey
508                          , word32TyConKey, word64TyConKey
509                          , floatTyConKey, doubleTyConKey
510                          , addrTyConKey, ptrTyConKey, funPtrTyConKey
511                          , charTyConKey, foreignObjTyConKey
512                          , foreignPtrTyConKey
513                          , stablePtrTyConKey
514                          , byteArrayTyConKey, mutableByteArrayTyConKey
515                          , boolTyConKey
516                          ]
517 \end{code}
518
519
520 %************************************************************************
521 %*                                                                      *
522 \subsection[TysWiredIn-Bool]{The @Bool@ type}
523 %*                                                                      *
524 %************************************************************************
525
526 An ordinary enumeration type, but deeply wired in.  There are no
527 magical operations on @Bool@ (just the regular Prelude code).
528
529 {\em BEGIN IDLE SPECULATION BY SIMON}
530
531 This is not the only way to encode @Bool@.  A more obvious coding makes
532 @Bool@ just a boxed up version of @Bool#@, like this:
533 \begin{verbatim}
534 type Bool# = Int#
535 data Bool = MkBool Bool#
536 \end{verbatim}
537
538 Unfortunately, this doesn't correspond to what the Report says @Bool@
539 looks like!  Furthermore, we get slightly less efficient code (I
540 think) with this coding. @gtInt@ would look like this:
541
542 \begin{verbatim}
543 gtInt :: Int -> Int -> Bool
544 gtInt x y = case x of I# x# ->
545             case y of I# y# ->
546             case (gtIntPrim x# y#) of
547                 b# -> MkBool b#
548 \end{verbatim}
549
550 Notice that the result of the @gtIntPrim@ comparison has to be turned
551 into an integer (here called @b#@), and returned in a @MkBool@ box.
552
553 The @if@ expression would compile to this:
554 \begin{verbatim}
555 case (gtInt x y) of
556   MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
557 \end{verbatim}
558
559 I think this code is a little less efficient than the previous code,
560 but I'm not certain.  At all events, corresponding with the Report is
561 important.  The interesting thing is that the language is expressive
562 enough to describe more than one alternative; and that a type doesn't
563 necessarily need to be a straightforwardly boxed version of its
564 primitive counterpart.
565
566 {\em END IDLE SPECULATION BY SIMON}
567
568 \begin{code}
569 boolTy = mkTyConTy boolTyCon
570
571 boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConName
572                     [] [] [falseDataCon, trueDataCon]
573
574 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
575 trueDataCon  = pcDataCon trueDataConName  [] [] [] boolTyCon
576
577 falseDataConId = dataConId falseDataCon
578 trueDataConId  = dataConId trueDataCon
579 \end{code}
580
581 %************************************************************************
582 %*                                                                      *
583 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
584 %*                                                                      *
585 %************************************************************************
586
587 Special syntax, deeply wired in, but otherwise an ordinary algebraic
588 data types:
589 \begin{verbatim}
590 data [] a = [] | a : (List a)
591 data () = ()
592 data (,) a b = (,,) a b
593 ...
594 \end{verbatim}
595
596 \begin{code}
597 mkListTy :: Type -> Type
598 mkListTy ty = mkTyConApp listTyCon [ty]
599
600 listTyCon = pcRecDataTyCon listTyConName
601                         alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
602
603 nilDataCon  = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
604 consDataCon = pcDataCon consDataConName
605                alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
606 -- Interesting: polymorphic recursion would help here.
607 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
608 -- gets the over-specific type (Type -> Type)
609 \end{code}
610
611 %************************************************************************
612 %*                                                                      *
613 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
614 %*                                                                      *
615 %************************************************************************
616
617 The tuple types are definitely magic, because they form an infinite
618 family.
619
620 \begin{itemize}
621 \item
622 They have a special family of type constructors, of type @TyCon@
623 These contain the tycon arity, but don't require a Unique.
624
625 \item
626 They have a special family of constructors, of type
627 @Id@. Again these contain their arity but don't need a Unique.
628
629 \item
630 There should be a magic way of generating the info tables and
631 entry code for all tuples.
632
633 But at the moment we just compile a Haskell source
634 file\srcloc{lib/prelude/...} containing declarations like:
635 \begin{verbatim}
636 data Tuple0             = Tup0
637 data Tuple2  a b        = Tup2  a b
638 data Tuple3  a b c      = Tup3  a b c
639 data Tuple4  a b c d    = Tup4  a b c d
640 ...
641 \end{verbatim}
642 The print-names associated with the magic @Id@s for tuple constructors
643 ``just happen'' to be the same as those generated by these
644 declarations.
645
646 \item
647 The instance environment should have a magic way to know
648 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
649 so on. \ToDo{Not implemented yet.}
650
651 \item
652 There should also be a way to generate the appropriate code for each
653 of these instances, but (like the info tables and entry code) it is
654 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
655 \end{itemize}
656
657 \begin{code}
658 mkTupleTy :: Boxity -> Int -> [Type] -> Type
659 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
660
661 unitTy    = mkTupleTy Boxed 0 []
662 \end{code}
663
664 %************************************************************************
665 %*                                                                      *
666 \subsection{Wired In Type Constructors for Representation Types}
667 %*                                                                      *
668 %************************************************************************
669
670 The following code defines the wired in datatypes cross, plus, unit
671 and c_of needed for the generic methods.
672
673 Ok, so the basic story is that for each type constructor I need to
674 create 2 things - a TyCon and a DataCon and then we are basically
675 ok. There are going to be no arguments passed to these functions
676 because -well- there is nothing to pass to these functions.
677
678 \begin{code}
679 crossTyCon :: TyCon
680 crossTyCon = pcNonRecDataTyCon crossTyConName alpha_beta_tyvars [] [crossDataCon]
681
682 crossDataCon :: DataCon
683 crossDataCon = pcDataCon crossDataConName alpha_beta_tyvars [] [alphaTy, betaTy] crossTyCon
684
685 plusTyCon :: TyCon
686 plusTyCon = pcNonRecDataTyCon plusTyConName alpha_beta_tyvars [] [inlDataCon, inrDataCon]
687
688 inlDataCon, inrDataCon :: DataCon
689 inlDataCon = pcDataCon inlDataConName alpha_beta_tyvars [] [alphaTy] plusTyCon
690 inrDataCon = pcDataCon inrDataConName alpha_beta_tyvars [] [betaTy]  plusTyCon
691
692 genUnitTyCon :: TyCon   -- The "1" type constructor for generics
693 genUnitTyCon = pcNonRecDataTyCon genUnitTyConName [] [] [genUnitDataCon]
694
695 genUnitDataCon :: DataCon
696 genUnitDataCon = pcDataCon genUnitDataConName [] [] [] genUnitTyCon
697 \end{code}