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