[project @ 2003-12-10 10:12:13 by wolfgang]
[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, 
15
16         boolTy, boolTyCon, boolTyCon_RDR, boolTyConName,
17         trueDataCon,  trueDataConId,  true_RDR,
18         falseDataCon, falseDataConId, false_RDR,
19
20         charTyCon, charDataCon, charTyCon_RDR,
21         charTy, stringTy, charTyConName,
22
23         
24         doubleTyCon, doubleDataCon, doubleTy,
25         
26         floatTyCon, floatDataCon, floatTy,
27
28         intTyCon, intDataCon, intTyCon_RDR, intDataCon_RDR, intTyConName,
29         intTy,
30
31         listTyCon, nilDataCon, consDataCon,
32         listTyCon_RDR, consDataCon_RDR, listTyConName,
33         mkListTy,
34
35         -- tuples
36         mkTupleTy,
37         tupleTyCon, tupleCon, 
38         unitTyCon, unitDataCon, unitDataConId, pairTyCon, 
39         unboxedSingletonTyCon, unboxedSingletonDataCon,
40         unboxedPairTyCon, unboxedPairDataCon,
41
42         unitTy,
43         voidTy,
44
45         -- parallel arrays
46         mkPArrTy,
47         parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
48         parrTyCon_RDR, parrTyConName
49     ) where
50
51 #include "HsVersions.h"
52
53 import {-# SOURCE #-} MkId( mkDataConIds )
54
55 -- friends:
56 import PrelNames
57 import TysPrim
58
59 -- others:
60 import Constants        ( mAX_TUPLE_SIZE )
61 import Module           ( Module )
62 import RdrName          ( nameRdrName )
63 import Name             ( Name, nameUnique, nameOccName, 
64                           nameModule, mkWiredInName )
65 import OccName          ( mkOccFS, tcName, dataName, mkTupleOcc, mkDataConWorkerOcc )
66 import DataCon          ( DataCon, mkDataCon, dataConWorkId, dataConSourceArity )
67 import Var              ( TyVar, tyVarKind )
68 import TyCon            ( TyCon, AlgTyConFlavour(..), DataConDetails(..), tyConDataCons,
69                           mkTupleTyCon, mkAlgTyCon, tyConName
70                         )
71
72 import BasicTypes       ( Arity, RecFlag(..), Boxity(..), isBoxed, StrictnessMark(..) )
73
74 import Type             ( Type, mkTyConTy, mkTyConApp, mkTyVarTy, mkTyVarTys, 
75                           mkArrowKinds, liftedTypeKind, unliftedTypeKind,
76                           ThetaType, TyThing(..) )
77 import Unique           ( incrUnique, mkTupleTyConUnique,
78                           mkTupleDataConUnique, mkPArrDataConUnique )
79 import PrelNames
80 import Array
81 import FastString
82 import Outputable
83
84 alpha_tyvar = [alphaTyVar]
85 alpha_ty    = [alphaTy]
86 \end{code}
87
88
89 %************************************************************************
90 %*                                                                      *
91 \subsection{Wired in type constructors}
92 %*                                                                      *
93 %************************************************************************
94
95 If you change which things are wired in, make sure you change their
96 names in PrelNames, so they use wTcQual, wDataQual, etc
97
98 \begin{code}
99 wiredInTyCons :: [TyCon]        -- Excludes tuples
100 wiredInTyCons = [ unitTyCon     -- Not treated like other tuples, because
101                                 -- it's defined in GHC.Base, and there's only
102                                 -- one of it.  We put it in wiredInTyCons so
103                                 -- that it'll pre-populate the name cache, so
104                                 -- the special case in lookupOrigNameCache 
105                                 -- doesn't need to look out for it
106               , boolTyCon
107               , charTyCon
108               , doubleTyCon
109               , floatTyCon
110               , intTyCon
111               , listTyCon
112               , parrTyCon
113               ]
114 \end{code}
115
116 \begin{code}
117 mkWiredInTyConName :: Module -> FastString -> Unique -> TyCon -> Name
118 mkWiredInTyConName mod fs uniq tycon
119   = mkWiredInName mod (mkOccFS tcName fs) uniq
120                   Nothing               -- No parent object
121                   (ATyCon tycon)        -- Relevant TyCon
122
123 mkWiredInDataConName :: Module -> FastString -> Unique -> DataCon -> Name -> Name
124 mkWiredInDataConName mod fs uniq datacon parent
125   = mkWiredInName mod (mkOccFS dataName fs) uniq
126                   (Just parent)         -- Name of parent TyCon
127                   (ADataCon datacon)    -- Relevant DataCon
128
129 charTyConName     = mkWiredInTyConName   pREL_BASE FSLIT("Char") charTyConKey charTyCon
130 charDataConName   = mkWiredInDataConName pREL_BASE FSLIT("C#") charDataConKey charDataCon charTyConName
131 intTyConName      = mkWiredInTyConName   pREL_BASE FSLIT("Int") intTyConKey   intTyCon
132 intDataConName    = mkWiredInDataConName pREL_BASE FSLIT("I#") intDataConKey  intDataCon intTyConName
133                                                   
134 boolTyConName     = mkWiredInTyConName   pREL_BASE FSLIT("Bool") boolTyConKey boolTyCon
135 falseDataConName  = mkWiredInDataConName pREL_BASE FSLIT("False") falseDataConKey falseDataCon boolTyConName
136 trueDataConName   = mkWiredInDataConName pREL_BASE FSLIT("True")  trueDataConKey  trueDataCon  boolTyConName
137 listTyConName     = mkWiredInTyConName   pREL_BASE FSLIT("[]") listTyConKey listTyCon
138 nilDataConName    = mkWiredInDataConName pREL_BASE FSLIT("[]") nilDataConKey nilDataCon  listTyConName
139 consDataConName   = mkWiredInDataConName pREL_BASE FSLIT(":") consDataConKey consDataCon listTyConName
140
141 floatTyConName     = mkWiredInTyConName   pREL_FLOAT FSLIT("Float") floatTyConKey floatTyCon
142 floatDataConName   = mkWiredInDataConName pREL_FLOAT FSLIT("F#") floatDataConKey floatDataCon floatTyConName
143 doubleTyConName    = mkWiredInTyConName   pREL_FLOAT FSLIT("Double") doubleTyConKey doubleTyCon
144 doubleDataConName  = mkWiredInDataConName pREL_FLOAT FSLIT("D#") doubleDataConKey doubleDataCon doubleTyConName
145
146 parrTyConName     = mkWiredInTyConName   pREL_PARR FSLIT("[::]") parrTyConKey parrTyCon 
147 parrDataConName   = mkWiredInDataConName pREL_PARR FSLIT("PArr") parrDataConKey parrDataCon parrTyConName
148
149 boolTyCon_RDR   = nameRdrName boolTyConName
150 false_RDR       = nameRdrName falseDataConName
151 true_RDR        = nameRdrName trueDataConName
152 intTyCon_RDR    = nameRdrName intTyConName
153 charTyCon_RDR   = nameRdrName charTyConName
154 intDataCon_RDR  = nameRdrName intDataConName
155 listTyCon_RDR   = nameRdrName listTyConName
156 consDataCon_RDR = nameRdrName consDataConName
157 parrTyCon_RDR   = nameRdrName parrTyConName
158 \end{code}
159
160
161 %************************************************************************
162 %*                                                                      *
163 \subsection{mkWiredInTyCon}
164 %*                                                                      *
165 %************************************************************************
166
167 \begin{code}
168 pcNonRecDataTyCon = pcTyCon False NonRecursive
169 pcRecDataTyCon    = pcTyCon False Recursive
170
171 pcTyCon is_enum is_rec name tyvars argvrcs cons
172   = tycon
173   where
174     tycon = mkAlgTyCon name
175                 (mkArrowKinds (map tyVarKind tyvars) liftedTypeKind)
176                 tyvars
177                 []              -- No context
178                 argvrcs
179                 (DataCons cons)
180                 []              -- No record selectors
181                 (DataTyCon is_enum)
182                 is_rec
183                 True            -- All the wired-in tycons have generics
184
185 pcDataCon :: Name -> [TyVar] -> ThetaType -> [Type] -> TyCon -> DataCon
186 -- The Name should be in the DataName name space; it's the name
187 -- of the DataCon itself.
188 --
189 -- The unique is the first of two free uniques;
190 -- the first is used for the datacon itself,
191 -- the second is used for the "worker name"
192
193 pcDataCon dc_name tyvars context arg_tys tycon
194   = data_con
195   where
196     data_con = mkDataCon dc_name        
197                 (map (const NotMarkedStrict) arg_tys)
198                 [{- No labelled fields -}]
199                 tyvars context [] [] arg_tys tycon 
200                 (mkDataConIds bogus_wrap_name wrk_name data_con)
201
202     mod      = nameModule dc_name
203     wrk_occ  = mkDataConWorkerOcc (nameOccName dc_name)
204     wrk_key  = incrUnique (nameUnique dc_name)
205     wrk_name = mkWiredInName mod wrk_occ wrk_key
206                              (Just (tyConName tycon))
207                              (AnId (dataConWorkId data_con))
208     bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
209         -- Wired-in types are too simple to need wrappers
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   = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
232 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [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         mod     = mkTupleModule boxity arity
239         tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
240                                 Nothing (ATyCon tycon)
241         tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
242         res_kind | isBoxed boxity = liftedTypeKind
243                  | otherwise      = unliftedTypeKind
244
245         tyvars   | isBoxed boxity = take arity alphaTyVars
246                  | otherwise      = take arity openAlphaTyVars
247
248         tuple_con = pcDataCon dc_name tyvars [] tyvar_tys tycon
249         tyvar_tys = mkTyVarTys tyvars
250         dc_name   = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
251                                   (Just tc_name) (ADataCon tuple_con)
252         tc_uniq   = mkTupleTyConUnique   boxity arity
253         dc_uniq   = mkTupleDataConUnique boxity arity
254         gen_info  = True                -- Tuples all have generics..
255                                         -- hmm: that's a *lot* of code
256
257 unitTyCon     = tupleTyCon Boxed 0
258 unitDataCon   = head (tyConDataCons unitTyCon)
259 unitDataConId = dataConWorkId unitDataCon
260
261 pairTyCon = tupleTyCon Boxed 2
262
263 unboxedSingletonTyCon   = tupleTyCon Unboxed 1
264 unboxedSingletonDataCon = tupleCon   Unboxed 1
265
266 unboxedPairTyCon   = tupleTyCon Unboxed 2
267 unboxedPairDataCon = tupleCon   Unboxed 2
268 \end{code}
269
270 %************************************************************************
271 %*                                                                      *
272 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
273 %*                                                                      *
274 %************************************************************************
275
276 \begin{code}
277 -- The Void type is represented as a data type with no constructors
278 -- It's a built in type (i.e. there's no way to define it in Haskell;
279 --      the nearest would be
280 --
281 --              data Void =             -- No constructors!
282 --
283 -- ) It's lifted; there is only one value of this
284 -- type, namely "void", whose semantics is just bottom.
285 --
286 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
287 -- voidTy using ().
288 voidTy = unitTy
289 \end{code}
290
291
292 \begin{code}
293 charTy = mkTyConTy charTyCon
294
295 charTyCon   = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
296 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
297
298 stringTy = mkListTy charTy -- convenience only
299 \end{code}
300
301 \begin{code}
302 intTy = mkTyConTy intTyCon 
303
304 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
305 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
306 \end{code}
307
308 \begin{code}
309 floatTy = mkTyConTy floatTyCon
310
311 floatTyCon   = pcNonRecDataTyCon floatTyConName   [] [] [floatDataCon]
312 floatDataCon = pcDataCon         floatDataConName [] [] [floatPrimTy] floatTyCon
313 \end{code}
314
315 \begin{code}
316 doubleTy = mkTyConTy doubleTyCon
317
318 doubleTyCon   = pcNonRecDataTyCon doubleTyConName   [] [] [doubleDataCon]
319 doubleDataCon = pcDataCon         doubleDataConName [] [] [doublePrimTy] doubleTyCon
320 \end{code}
321
322
323 %************************************************************************
324 %*                                                                      *
325 \subsection[TysWiredIn-Bool]{The @Bool@ type}
326 %*                                                                      *
327 %************************************************************************
328
329 An ordinary enumeration type, but deeply wired in.  There are no
330 magical operations on @Bool@ (just the regular Prelude code).
331
332 {\em BEGIN IDLE SPECULATION BY SIMON}
333
334 This is not the only way to encode @Bool@.  A more obvious coding makes
335 @Bool@ just a boxed up version of @Bool#@, like this:
336 \begin{verbatim}
337 type Bool# = Int#
338 data Bool = MkBool Bool#
339 \end{verbatim}
340
341 Unfortunately, this doesn't correspond to what the Report says @Bool@
342 looks like!  Furthermore, we get slightly less efficient code (I
343 think) with this coding. @gtInt@ would look like this:
344
345 \begin{verbatim}
346 gtInt :: Int -> Int -> Bool
347 gtInt x y = case x of I# x# ->
348             case y of I# y# ->
349             case (gtIntPrim x# y#) of
350                 b# -> MkBool b#
351 \end{verbatim}
352
353 Notice that the result of the @gtIntPrim@ comparison has to be turned
354 into an integer (here called @b#@), and returned in a @MkBool@ box.
355
356 The @if@ expression would compile to this:
357 \begin{verbatim}
358 case (gtInt x y) of
359   MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
360 \end{verbatim}
361
362 I think this code is a little less efficient than the previous code,
363 but I'm not certain.  At all events, corresponding with the Report is
364 important.  The interesting thing is that the language is expressive
365 enough to describe more than one alternative; and that a type doesn't
366 necessarily need to be a straightforwardly boxed version of its
367 primitive counterpart.
368
369 {\em END IDLE SPECULATION BY SIMON}
370
371 \begin{code}
372 boolTy = mkTyConTy boolTyCon
373
374 boolTyCon = pcTyCon True NonRecursive boolTyConName
375                     [] [] [falseDataCon, trueDataCon]
376
377 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
378 trueDataCon  = pcDataCon trueDataConName  [] [] [] boolTyCon
379
380 falseDataConId = dataConWorkId falseDataCon
381 trueDataConId  = dataConWorkId trueDataCon
382 \end{code}
383
384 %************************************************************************
385 %*                                                                      *
386 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
387 %*                                                                      *
388 %************************************************************************
389
390 Special syntax, deeply wired in, but otherwise an ordinary algebraic
391 data types:
392 \begin{verbatim}
393 data [] a = [] | a : (List a)
394 data () = ()
395 data (,) a b = (,,) a b
396 ...
397 \end{verbatim}
398
399 \begin{code}
400 mkListTy :: Type -> Type
401 mkListTy ty = mkTyConApp listTyCon [ty]
402
403 listTyCon = pcRecDataTyCon listTyConName
404                         alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
405
406 nilDataCon  = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
407 consDataCon = pcDataCon consDataConName
408                alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
409 -- Interesting: polymorphic recursion would help here.
410 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
411 -- gets the over-specific type (Type -> Type)
412 \end{code}
413
414 %************************************************************************
415 %*                                                                      *
416 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
417 %*                                                                      *
418 %************************************************************************
419
420 The tuple types are definitely magic, because they form an infinite
421 family.
422
423 \begin{itemize}
424 \item
425 They have a special family of type constructors, of type @TyCon@
426 These contain the tycon arity, but don't require a Unique.
427
428 \item
429 They have a special family of constructors, of type
430 @Id@. Again these contain their arity but don't need a Unique.
431
432 \item
433 There should be a magic way of generating the info tables and
434 entry code for all tuples.
435
436 But at the moment we just compile a Haskell source
437 file\srcloc{lib/prelude/...} containing declarations like:
438 \begin{verbatim}
439 data Tuple0             = Tup0
440 data Tuple2  a b        = Tup2  a b
441 data Tuple3  a b c      = Tup3  a b c
442 data Tuple4  a b c d    = Tup4  a b c d
443 ...
444 \end{verbatim}
445 The print-names associated with the magic @Id@s for tuple constructors
446 ``just happen'' to be the same as those generated by these
447 declarations.
448
449 \item
450 The instance environment should have a magic way to know
451 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
452 so on. \ToDo{Not implemented yet.}
453
454 \item
455 There should also be a way to generate the appropriate code for each
456 of these instances, but (like the info tables and entry code) it is
457 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
458 \end{itemize}
459
460 \begin{code}
461 mkTupleTy :: Boxity -> Int -> [Type] -> Type
462 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
463
464 unitTy    = mkTupleTy Boxed 0 []
465 \end{code}
466
467 %************************************************************************
468 %*                                                                      *
469 \subsection[TysWiredIn-PArr]{The @[::]@ type}
470 %*                                                                      *
471 %************************************************************************
472
473 Special syntax for parallel arrays needs some wired in definitions.
474
475 \begin{code}
476 -- construct a type representing the application of the parallel array
477 -- constructor 
478 --
479 mkPArrTy    :: Type -> Type
480 mkPArrTy ty  = mkTyConApp parrTyCon [ty]
481
482 -- represents the type constructor of parallel arrays
483 --
484 -- * this must match the definition in `PrelPArr'
485 --
486 -- NB: Although the constructor is given here, it will not be accessible in
487 --     user code as it is not in the environment of any compiled module except
488 --     `PrelPArr'.
489 --
490 parrTyCon :: TyCon
491 parrTyCon  = pcNonRecDataTyCon parrTyConName alpha_tyvar [(True, False)] [parrDataCon]
492
493 parrDataCon :: DataCon
494 parrDataCon  = pcDataCon 
495                  parrDataConName 
496                  alpha_tyvar            -- forall'ed type variables
497                  []                     -- context
498                  [intPrimTy,            -- 1st argument: Int#
499                   mkTyConApp            -- 2nd argument: Array# a
500                     arrayPrimTyCon 
501                     alpha_ty] 
502                  parrTyCon
503
504 -- check whether a type constructor is the constructor for parallel arrays
505 --
506 isPArrTyCon    :: TyCon -> Bool
507 isPArrTyCon tc  = tyConName tc == parrTyConName
508
509 -- fake array constructors
510 --
511 -- * these constructors are never really used to represent array values;
512 --   however, they are very convenient during desugaring (and, in particular,
513 --   in the pattern matching compiler) to treat array pattern just like
514 --   yet another constructor pattern
515 --
516 parrFakeCon                        :: Arity -> DataCon
517 parrFakeCon i | i > mAX_TUPLE_SIZE  = mkPArrFakeCon  i  -- build one specially
518 parrFakeCon i                       = parrFakeConArr!i
519
520 -- pre-defined set of constructors
521 --
522 parrFakeConArr :: Array Int DataCon
523 parrFakeConArr  = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)   
524                                             | i <- [0..mAX_TUPLE_SIZE]]
525
526 -- build a fake parallel array constructor for the given arity
527 --
528 mkPArrFakeCon       :: Int -> DataCon
529 mkPArrFakeCon arity  = data_con
530   where
531         data_con  = pcDataCon name [tyvar] [] tyvarTys parrTyCon
532         tyvar     = head alphaTyVars
533         tyvarTys  = replicate arity $ mkTyVarTy tyvar
534         nameStr   = mkFastString ("MkPArr" ++ show arity)
535         name      = mkWiredInName pREL_PARR (mkOccFS dataName nameStr) uniq
536                                   Nothing (ADataCon data_con)
537         uniq      = mkPArrDataConUnique arity
538
539 -- checks whether a data constructor is a fake constructor for parallel arrays
540 --
541 isPArrFakeCon      :: DataCon -> Bool
542 isPArrFakeCon dcon  = dcon == parrFakeCon (dataConSourceArity dcon)
543 \end{code}
544