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