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