[project @ 2003-06-16 15:32:16 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         boolTy,
17         boolTyCon,
18         charDataCon,
19         charTy,
20         charTyCon,
21         consDataCon,
22         doubleDataCon,
23         doubleTy,
24         doubleTyCon,
25         falseDataCon, falseDataConId,
26         floatDataCon,
27         floatTy,
28         floatTyCon,
29
30         intDataCon,
31         intTy,
32         intTyCon,
33
34         integerTy,
35         integerTyCon,
36         smallIntegerDataCon,
37         largeIntegerDataCon,
38
39         listTyCon,
40
41         mkListTy,
42         nilDataCon,
43
44         -- tuples
45         mkTupleTy,
46         tupleTyCon, tupleCon, 
47         unitTyCon, unitDataCon, unitDataConId, pairTyCon, 
48         unboxedSingletonTyCon, unboxedSingletonDataCon,
49         unboxedPairTyCon, unboxedPairDataCon,
50
51         -- Generics
52         genUnitTyCon, genUnitDataCon, 
53         plusTyCon, inrDataCon, inlDataCon,
54         crossTyCon, crossDataCon,
55
56         stringTy,
57         trueDataCon, trueDataConId,
58         unitTy,
59         voidTy,
60         wordDataCon,
61         wordTy,
62         wordTyCon,
63
64         -- parallel arrays
65         mkPArrTy,
66         parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon
67     ) where
68
69 #include "HsVersions.h"
70
71 import {-# SOURCE #-} MkId( mkDataConWorkId )
72 import {-# SOURCE #-} Generics( mkTyConGenInfo )
73
74 -- friends:
75 import PrelNames
76 import TysPrim
77
78 -- others:
79 import Constants        ( mAX_TUPLE_SIZE )
80 import Module           ( mkBasePkgModule )
81 import Name             ( Name, nameUnique, nameOccName, 
82                           nameModule, mkWiredInName )
83 import OccName          ( mkOccFS, tcName, dataName, mkDataConWorkerOcc, mkGenOcc1, mkGenOcc2 )
84 import DataCon          ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
85 import Var              ( TyVar, tyVarKind )
86 import TyCon            ( TyCon, AlgTyConFlavour(..), DataConDetails(..), tyConDataCons,
87                           mkTupleTyCon, mkAlgTyCon, tyConName
88                         )
89
90 import BasicTypes       ( Arity, RecFlag(..), Boxity(..), isBoxed )
91
92 import Type             ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys, 
93                           mkArrowKinds, liftedTypeKind, unliftedTypeKind,
94                           ThetaType )
95 import Unique           ( incrUnique, mkTupleTyConUnique,
96                           mkTupleDataConUnique, mkPArrDataConUnique )
97 import PrelNames
98 import Array
99 import FastString
100
101 alpha_tyvar       = [alphaTyVar]
102 alpha_ty          = [alphaTy]
103 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
104 \end{code}
105
106
107 %************************************************************************
108 %*                                                                      *
109 \subsection{Wired in type constructors}
110 %*                                                                      *
111 %************************************************************************
112
113 If you change which things are wired in, make sure you change their
114 names in PrelNames, so they use wTcQual, wDataQual, etc
115
116 \begin{code}
117 wiredInTyCons :: [TyCon]
118 wiredInTyCons = data_tycons ++ tuple_tycons ++ unboxed_tuple_tycons
119
120 data_tycons = genericTyCons ++
121               [ boolTyCon
122               , charTyCon
123               , doubleTyCon
124               , floatTyCon
125               , intTyCon
126               , integerTyCon
127               , listTyCon
128               , parrTyCon
129               , wordTyCon
130               ]
131
132 genericTyCons :: [TyCon]
133 genericTyCons = [ plusTyCon, crossTyCon, genUnitTyCon ]
134
135 tuple_tycons = unitTyCon : [tupleTyCon Boxed   i | i <- [2..mAX_TUPLE_SIZE] ]
136 unboxed_tuple_tycons     = [tupleTyCon Unboxed i | i <- [1..mAX_TUPLE_SIZE] ]
137 \end{code}
138
139
140 %************************************************************************
141 %*                                                                      *
142 \subsection{mkWiredInTyCon}
143 %*                                                                      *
144 %************************************************************************
145
146 \begin{code}
147 pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
148 pcRecDataTyCon = pcTyCon DataTyCon Recursive
149
150 pcTyCon new_or_data is_rec name tyvars argvrcs cons
151   = tycon
152   where
153     tycon = mkAlgTyCon name kind
154                 tyvars
155                 []              -- No context
156                 argvrcs
157                 (DataCons cons)
158                 []              -- No record selectors
159                 new_or_data
160                 is_rec
161                 gen_info
162
163     mod      = nameModule name
164     kind     = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
165     gen_info = mk_tc_gen_info mod (nameUnique name) name tycon
166
167 -- We generate names for the generic to/from Ids by incrementing
168 -- the TyCon unique.  So each Prelude tycon needs 3 slots, one
169 -- for itself and two more for the generic Ids.
170 mk_tc_gen_info mod tc_uniq tc_name tycon
171   = mkTyConGenInfo tycon [name1, name2]
172   where
173         tc_occ_name = nameOccName tc_name
174         occ_name1   = mkGenOcc1 tc_occ_name
175         occ_name2   = mkGenOcc2 tc_occ_name
176         fn1_key     = incrUnique tc_uniq
177         fn2_key     = incrUnique fn1_key
178         name1       = mkWiredInName  mod occ_name1 fn1_key
179         name2       = mkWiredInName  mod occ_name2 fn2_key
180
181 pcDataCon :: Name -> [TyVar] -> ThetaType -> [Type] -> TyCon -> DataCon
182 -- The Name should be in the DataName name space; it's the name
183 -- of the DataCon itself.
184 --
185 -- The unique is the first of two free uniques;
186 -- the first is used for the datacon itself,
187 -- the second is used for the "worker name"
188
189 pcDataCon dc_name tyvars context arg_tys tycon
190   = data_con
191   where
192     data_con = mkDataCon dc_name        
193                 [{- No strictness -}]
194                 [{- No labelled fields -}]
195                 tyvars context [] [] arg_tys tycon work_id 
196                 Nothing {- No wrapper for wired-in things
197                            (they are too simple to need one) -}
198
199     mod      = nameModule dc_name
200     wrk_occ  = mkDataConWorkerOcc (nameOccName dc_name)
201     wrk_key  = incrUnique (nameUnique dc_name)
202     wrk_name = mkWiredInName mod wrk_occ wrk_key
203     work_id  = mkDataConWorkId wrk_name data_con
204 \end{code}
205
206
207 %************************************************************************
208 %*                                                                      *
209 \subsection[TysWiredIn-tuples]{The tuple types}
210 %*                                                                      *
211 %************************************************************************
212
213 \begin{code}
214 tupleTyCon :: Boxity -> Arity -> TyCon
215 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i)      -- Build one specially
216 tupleTyCon Boxed   i = fst (boxedTupleArr   ! i)
217 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
218
219 tupleCon :: Boxity -> Arity -> DataCon
220 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i)        -- Build one specially
221 tupleCon Boxed   i = snd (boxedTupleArr   ! i)
222 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
223
224 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
225 boxedTupleArr   = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
226 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
227
228 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
229 mk_tuple boxity arity = (tycon, tuple_con)
230   where
231         tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info 
232         tc_name = mkWiredInName mod (mkOccFS tcName name_str) tc_uniq
233         tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
234         res_kind | isBoxed boxity = liftedTypeKind
235                  | otherwise      = unliftedTypeKind
236
237         tyvars   | isBoxed boxity = take arity alphaTyVars
238                  | otherwise      = take arity openAlphaTyVars
239
240         tuple_con = pcDataCon name tyvars [] tyvar_tys tycon
241         tyvar_tys = mkTyVarTys tyvars
242         (mod_name, name_str) = mkTupNameStr boxity arity
243         name      = mkWiredInName mod (mkOccFS dataName name_str) dc_uniq
244         tc_uniq   = mkTupleTyConUnique   boxity arity
245         dc_uniq   = mkTupleDataConUnique boxity arity
246         mod       = mkBasePkgModule mod_name
247         gen_info  = mk_tc_gen_info mod tc_uniq tc_name tycon
248
249 unitTyCon     = tupleTyCon Boxed 0
250 unitDataCon   = head (tyConDataCons unitTyCon)
251 unitDataConId = dataConWorkId unitDataCon
252
253 pairTyCon = tupleTyCon Boxed 2
254
255 unboxedSingletonTyCon   = tupleTyCon Unboxed 1
256 unboxedSingletonDataCon = tupleCon   Unboxed 1
257
258 unboxedPairTyCon   = tupleTyCon Unboxed 2
259 unboxedPairDataCon = tupleCon   Unboxed 2
260 \end{code}
261
262 %************************************************************************
263 %*                                                                      *
264 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
265 %*                                                                      *
266 %************************************************************************
267
268 \begin{code}
269 -- The Void type is represented as a data type with no constructors
270 -- It's a built in type (i.e. there's no way to define it in Haskell;
271 --      the nearest would be
272 --
273 --              data Void =             -- No constructors!
274 --
275 -- ) It's lifted; there is only one value of this
276 -- type, namely "void", whose semantics is just bottom.
277 --
278 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
279 -- voidTy using ().
280 voidTy = unitTy
281 \end{code}
282
283
284 \begin{code}
285 charTy = mkTyConTy charTyCon
286
287 charTyCon   = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
288 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
289
290 stringTy = mkListTy charTy -- convenience only
291 \end{code}
292
293 \begin{code}
294 intTy = mkTyConTy intTyCon 
295
296 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
297 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
298 \end{code}
299
300 \begin{code}
301 wordTy = mkTyConTy wordTyCon
302
303 wordTyCon = pcNonRecDataTyCon wordTyConName [] [] [wordDataCon]
304 wordDataCon = pcDataCon wordDataConName [] [] [wordPrimTy] wordTyCon
305 \end{code}
306
307 \begin{code}
308 floatTy = mkTyConTy floatTyCon
309
310 floatTyCon   = pcNonRecDataTyCon floatTyConName   [] [] [floatDataCon]
311 floatDataCon = pcDataCon         floatDataConName [] [] [floatPrimTy] floatTyCon
312 \end{code}
313
314 \begin{code}
315 doubleTy = mkTyConTy doubleTyCon
316
317 doubleTyCon   = pcNonRecDataTyCon doubleTyConName   [] [] [doubleDataCon]
318 doubleDataCon = pcDataCon         doubleDataConName [] [] [doublePrimTy] doubleTyCon
319 \end{code}
320
321
322 %************************************************************************
323 %*                                                                      *
324 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
325 %*                                                                      *
326 %************************************************************************
327
328 @Integer@ and its pals are not really primitive.  @Integer@ itself, first:
329 \begin{code}
330 integerTy :: Type
331 integerTy = mkTyConTy integerTyCon
332
333 integerTyCon = pcNonRecDataTyCon integerTyConName
334                    [] [] [smallIntegerDataCon, largeIntegerDataCon]
335
336 smallIntegerDataCon = pcDataCon smallIntegerDataConName
337                 [] [] [intPrimTy] integerTyCon
338 largeIntegerDataCon = pcDataCon largeIntegerDataConName
339                 [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
340 \end{code}
341
342
343 %************************************************************************
344 %*                                                                      *
345 \subsection[TysWiredIn-Bool]{The @Bool@ type}
346 %*                                                                      *
347 %************************************************************************
348
349 An ordinary enumeration type, but deeply wired in.  There are no
350 magical operations on @Bool@ (just the regular Prelude code).
351
352 {\em BEGIN IDLE SPECULATION BY SIMON}
353
354 This is not the only way to encode @Bool@.  A more obvious coding makes
355 @Bool@ just a boxed up version of @Bool#@, like this:
356 \begin{verbatim}
357 type Bool# = Int#
358 data Bool = MkBool Bool#
359 \end{verbatim}
360
361 Unfortunately, this doesn't correspond to what the Report says @Bool@
362 looks like!  Furthermore, we get slightly less efficient code (I
363 think) with this coding. @gtInt@ would look like this:
364
365 \begin{verbatim}
366 gtInt :: Int -> Int -> Bool
367 gtInt x y = case x of I# x# ->
368             case y of I# y# ->
369             case (gtIntPrim x# y#) of
370                 b# -> MkBool b#
371 \end{verbatim}
372
373 Notice that the result of the @gtIntPrim@ comparison has to be turned
374 into an integer (here called @b#@), and returned in a @MkBool@ box.
375
376 The @if@ expression would compile to this:
377 \begin{verbatim}
378 case (gtInt x y) of
379   MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
380 \end{verbatim}
381
382 I think this code is a little less efficient than the previous code,
383 but I'm not certain.  At all events, corresponding with the Report is
384 important.  The interesting thing is that the language is expressive
385 enough to describe more than one alternative; and that a type doesn't
386 necessarily need to be a straightforwardly boxed version of its
387 primitive counterpart.
388
389 {\em END IDLE SPECULATION BY SIMON}
390
391 \begin{code}
392 boolTy = mkTyConTy boolTyCon
393
394 boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConName
395                     [] [] [falseDataCon, trueDataCon]
396
397 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
398 trueDataCon  = pcDataCon trueDataConName  [] [] [] boolTyCon
399
400 falseDataConId = dataConWorkId falseDataCon
401 trueDataConId  = dataConWorkId trueDataCon
402 \end{code}
403
404 %************************************************************************
405 %*                                                                      *
406 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
407 %*                                                                      *
408 %************************************************************************
409
410 Special syntax, deeply wired in, but otherwise an ordinary algebraic
411 data types:
412 \begin{verbatim}
413 data [] a = [] | a : (List a)
414 data () = ()
415 data (,) a b = (,,) a b
416 ...
417 \end{verbatim}
418
419 \begin{code}
420 mkListTy :: Type -> Type
421 mkListTy ty = mkTyConApp listTyCon [ty]
422
423 listTyCon = pcRecDataTyCon listTyConName
424                         alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
425
426 nilDataCon  = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
427 consDataCon = pcDataCon consDataConName
428                alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
429 -- Interesting: polymorphic recursion would help here.
430 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
431 -- gets the over-specific type (Type -> Type)
432 \end{code}
433
434 %************************************************************************
435 %*                                                                      *
436 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
437 %*                                                                      *
438 %************************************************************************
439
440 The tuple types are definitely magic, because they form an infinite
441 family.
442
443 \begin{itemize}
444 \item
445 They have a special family of type constructors, of type @TyCon@
446 These contain the tycon arity, but don't require a Unique.
447
448 \item
449 They have a special family of constructors, of type
450 @Id@. Again these contain their arity but don't need a Unique.
451
452 \item
453 There should be a magic way of generating the info tables and
454 entry code for all tuples.
455
456 But at the moment we just compile a Haskell source
457 file\srcloc{lib/prelude/...} containing declarations like:
458 \begin{verbatim}
459 data Tuple0             = Tup0
460 data Tuple2  a b        = Tup2  a b
461 data Tuple3  a b c      = Tup3  a b c
462 data Tuple4  a b c d    = Tup4  a b c d
463 ...
464 \end{verbatim}
465 The print-names associated with the magic @Id@s for tuple constructors
466 ``just happen'' to be the same as those generated by these
467 declarations.
468
469 \item
470 The instance environment should have a magic way to know
471 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
472 so on. \ToDo{Not implemented yet.}
473
474 \item
475 There should also be a way to generate the appropriate code for each
476 of these instances, but (like the info tables and entry code) it is
477 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
478 \end{itemize}
479
480 \begin{code}
481 mkTupleTy :: Boxity -> Int -> [Type] -> Type
482 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
483
484 unitTy    = mkTupleTy Boxed 0 []
485 \end{code}
486
487 %************************************************************************
488 %*                                                                      *
489 \subsection[TysWiredIn-PArr]{The @[::]@ type}
490 %*                                                                      *
491 %************************************************************************
492
493 Special syntax for parallel arrays needs some wired in definitions.
494
495 \begin{code}
496 -- construct a type representing the application of the parallel array
497 -- constructor 
498 --
499 mkPArrTy    :: Type -> Type
500 mkPArrTy ty  = mkTyConApp parrTyCon [ty]
501
502 -- represents the type constructor of parallel arrays
503 --
504 -- * this must match the definition in `PrelPArr'
505 --
506 -- NB: Although the constructor is given here, it will not be accessible in
507 --     user code as it is not in the environment of any compiled module except
508 --     `PrelPArr'.
509 --
510 parrTyCon :: TyCon
511 parrTyCon  = tycon
512   where
513     tycon   = mkAlgTyCon 
514                 parrTyConName 
515                 kind
516                 tyvars
517                 []                       -- No context
518                 [(True, False)]
519                 (DataCons [parrDataCon]) -- The constructor defined in `PrelPArr'
520                 []                       -- No record selectors
521                 DataTyCon
522                 NonRecursive
523                 genInfo
524     tyvars  = alpha_tyvar
525     mod     = nameModule parrTyConName
526     kind    = mkArrowKinds (map tyVarKind tyvars) liftedTypeKind
527     genInfo = mk_tc_gen_info mod (nameUnique parrTyConName) parrTyConName tycon
528
529 parrDataCon :: DataCon
530 parrDataCon  = pcDataCon 
531                  parrDataConName 
532                  alpha_tyvar            -- forall'ed type variables
533                  []                     -- context
534                  [intPrimTy,            -- 1st argument: Int#
535                   mkTyConApp            -- 2nd argument: Array# a
536                     arrayPrimTyCon 
537                     alpha_ty] 
538                  parrTyCon
539
540 -- check whether a type constructor is the constructor for parallel arrays
541 --
542 isPArrTyCon    :: TyCon -> Bool
543 isPArrTyCon tc  = tyConName tc == parrTyConName
544
545 -- fake array constructors
546 --
547 -- * these constructors are never really used to represent array values;
548 --   however, they are very convenient during desugaring (and, in particular,
549 --   in the pattern matching compiler) to treat array pattern just like
550 --   yet another constructor pattern
551 --
552 parrFakeCon                        :: Arity -> DataCon
553 parrFakeCon i | i > mAX_TUPLE_SIZE  = mkPArrFakeCon  i  -- build one specially
554 parrFakeCon i                       = parrFakeConArr!i
555
556 -- pre-defined set of constructors
557 --
558 parrFakeConArr :: Array Int DataCon
559 parrFakeConArr  = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)   
560                                             | i <- [0..mAX_TUPLE_SIZE]]
561
562 -- build a fake parallel array constructor for the given arity
563 --
564 mkPArrFakeCon       :: Int -> DataCon
565 mkPArrFakeCon arity  = pcDataCon name [tyvar] [] tyvarTys parrTyCon
566   where
567         tyvar     = head alphaTyVars
568         tyvarTys  = replicate arity $ mkTyVarTy tyvar
569         nameStr   = mkFastString ("MkPArr" ++ show arity)
570         name      = mkWiredInName mod (mkOccFS dataName nameStr) uniq
571         uniq      = mkPArrDataConUnique arity
572         mod       = mkBasePkgModule pREL_PARR_Name
573
574 -- checks whether a data constructor is a fake constructor for parallel arrays
575 --
576 isPArrFakeCon      :: DataCon -> Bool
577 isPArrFakeCon dcon  = dcon == parrFakeCon (dataConSourceArity dcon)
578 \end{code}
579
580 %************************************************************************
581 %*                                                                      *
582 \subsection{Wired In Type Constructors for Representation Types}
583 %*                                                                      *
584 %************************************************************************
585
586 The following code defines the wired in datatypes cross, plus, unit
587 and c_of needed for the generic methods.
588
589 Ok, so the basic story is that for each type constructor I need to
590 create 2 things - a TyCon and a DataCon and then we are basically
591 ok. There are going to be no arguments passed to these functions
592 because -well- there is nothing to pass to these functions.
593
594 \begin{code}
595 crossTyCon :: TyCon
596 crossTyCon = pcNonRecDataTyCon crossTyConName alpha_beta_tyvars [] [crossDataCon]
597
598 crossDataCon :: DataCon
599 crossDataCon = pcDataCon crossDataConName alpha_beta_tyvars [] [alphaTy, betaTy] crossTyCon
600
601 plusTyCon :: TyCon
602 plusTyCon = pcNonRecDataTyCon plusTyConName alpha_beta_tyvars [] [inlDataCon, inrDataCon]
603
604 inlDataCon, inrDataCon :: DataCon
605 inlDataCon = pcDataCon inlDataConName alpha_beta_tyvars [] [alphaTy] plusTyCon
606 inrDataCon = pcDataCon inrDataConName alpha_beta_tyvars [] [betaTy]  plusTyCon
607
608 genUnitTyCon :: TyCon   -- The "1" type constructor for generics
609 genUnitTyCon = pcNonRecDataTyCon genUnitTyConName [] [] [genUnitDataCon]
610
611 genUnitDataCon :: DataCon
612 genUnitDataCon = pcDataCon genUnitDataConName [] [] [] genUnitTyCon
613 \end{code}