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