[project @ 2004-03-17 13:59:06 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, 
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, AlgTyConRhs(DataTyCon), 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                           ThetaType, TyThing(..) )
76 import Kind             ( mkArrowKinds, liftedTypeKind, ubxTupleKind )
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                 (DataTyCon cons is_enum)
180                 []              -- No record selectors
181                 is_rec
182                 True            -- All the wired-in tycons have generics
183
184 pcDataCon :: Name -> [TyVar] -> ThetaType -> [Type] -> TyCon -> DataCon
185 -- The Name should be in the DataName name space; it's the name
186 -- of the DataCon itself.
187 --
188 -- The unique is the first of two free uniques;
189 -- the first is used for the datacon itself,
190 -- the second is used for the "worker name"
191
192 pcDataCon dc_name tyvars context arg_tys tycon
193   = data_con
194   where
195     data_con = mkDataCon dc_name        
196                 (map (const NotMarkedStrict) arg_tys)
197                 [{- No labelled fields -}]
198                 tyvars context [] [] arg_tys tycon 
199                 (mkDataConIds bogus_wrap_name wrk_name data_con)
200
201     mod      = nameModule dc_name
202     wrk_occ  = mkDataConWorkerOcc (nameOccName dc_name)
203     wrk_key  = incrUnique (nameUnique dc_name)
204     wrk_name = mkWiredInName mod wrk_occ wrk_key
205                              (Just (tyConName tycon))
206                              (AnId (dataConWorkId data_con))
207     bogus_wrap_name = pprPanic "Wired-in data wrapper id" (ppr dc_name)
208         -- Wired-in types are too simple to need wrappers
209 \end{code}
210
211
212 %************************************************************************
213 %*                                                                      *
214 \subsection[TysWiredIn-tuples]{The tuple types}
215 %*                                                                      *
216 %************************************************************************
217
218 \begin{code}
219 tupleTyCon :: Boxity -> Arity -> TyCon
220 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i)      -- Build one specially
221 tupleTyCon Boxed   i = fst (boxedTupleArr   ! i)
222 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
223
224 tupleCon :: Boxity -> Arity -> DataCon
225 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i)        -- Build one specially
226 tupleCon Boxed   i = snd (boxedTupleArr   ! i)
227 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
228
229 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
230 boxedTupleArr   = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
231 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
232
233 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
234 mk_tuple boxity arity = (tycon, tuple_con)
235   where
236         tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity gen_info 
237         mod     = mkTupleModule boxity arity
238         tc_name = mkWiredInName mod (mkTupleOcc tcName boxity arity) tc_uniq
239                                 Nothing (ATyCon tycon)
240         tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
241         res_kind | isBoxed boxity = liftedTypeKind
242                  | otherwise      = ubxTupleKind
243
244         tyvars   | isBoxed boxity = take arity alphaTyVars
245                  | otherwise      = take arity openAlphaTyVars
246
247         tuple_con = pcDataCon dc_name tyvars [] tyvar_tys tycon
248         tyvar_tys = mkTyVarTys tyvars
249         dc_name   = mkWiredInName mod (mkTupleOcc dataName boxity arity) dc_uniq
250                                   (Just tc_name) (ADataCon tuple_con)
251         tc_uniq   = mkTupleTyConUnique   boxity arity
252         dc_uniq   = mkTupleDataConUnique boxity arity
253         gen_info  = True                -- Tuples all have generics..
254                                         -- hmm: that's a *lot* of code
255
256 unitTyCon     = tupleTyCon Boxed 0
257 unitDataCon   = head (tyConDataCons unitTyCon)
258 unitDataConId = dataConWorkId unitDataCon
259
260 pairTyCon = tupleTyCon Boxed 2
261
262 unboxedSingletonTyCon   = tupleTyCon Unboxed 1
263 unboxedSingletonDataCon = tupleCon   Unboxed 1
264
265 unboxedPairTyCon   = tupleTyCon Unboxed 2
266 unboxedPairDataCon = tupleCon   Unboxed 2
267 \end{code}
268
269 %************************************************************************
270 %*                                                                      *
271 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
272 %*                                                                      *
273 %************************************************************************
274
275 \begin{code}
276 -- The Void type is represented as a data type with no constructors
277 -- It's a built in type (i.e. there's no way to define it in Haskell;
278 --      the nearest would be
279 --
280 --              data Void =             -- No constructors!
281 --
282 -- ) It's lifted; there is only one value of this
283 -- type, namely "void", whose semantics is just bottom.
284 --
285 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
286 -- voidTy using ().
287 voidTy = unitTy
288 \end{code}
289
290
291 \begin{code}
292 charTy = mkTyConTy charTyCon
293
294 charTyCon   = pcNonRecDataTyCon charTyConName [] [] [charDataCon]
295 charDataCon = pcDataCon charDataConName [] [] [charPrimTy] charTyCon
296
297 stringTy = mkListTy charTy -- convenience only
298 \end{code}
299
300 \begin{code}
301 intTy = mkTyConTy intTyCon 
302
303 intTyCon = pcNonRecDataTyCon intTyConName [] [] [intDataCon]
304 intDataCon = pcDataCon intDataConName [] [] [intPrimTy] intTyCon
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-Bool]{The @Bool@ type}
325 %*                                                                      *
326 %************************************************************************
327
328 An ordinary enumeration type, but deeply wired in.  There are no
329 magical operations on @Bool@ (just the regular Prelude code).
330
331 {\em BEGIN IDLE SPECULATION BY SIMON}
332
333 This is not the only way to encode @Bool@.  A more obvious coding makes
334 @Bool@ just a boxed up version of @Bool#@, like this:
335 \begin{verbatim}
336 type Bool# = Int#
337 data Bool = MkBool Bool#
338 \end{verbatim}
339
340 Unfortunately, this doesn't correspond to what the Report says @Bool@
341 looks like!  Furthermore, we get slightly less efficient code (I
342 think) with this coding. @gtInt@ would look like this:
343
344 \begin{verbatim}
345 gtInt :: Int -> Int -> Bool
346 gtInt x y = case x of I# x# ->
347             case y of I# y# ->
348             case (gtIntPrim x# y#) of
349                 b# -> MkBool b#
350 \end{verbatim}
351
352 Notice that the result of the @gtIntPrim@ comparison has to be turned
353 into an integer (here called @b#@), and returned in a @MkBool@ box.
354
355 The @if@ expression would compile to this:
356 \begin{verbatim}
357 case (gtInt x y) of
358   MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
359 \end{verbatim}
360
361 I think this code is a little less efficient than the previous code,
362 but I'm not certain.  At all events, corresponding with the Report is
363 important.  The interesting thing is that the language is expressive
364 enough to describe more than one alternative; and that a type doesn't
365 necessarily need to be a straightforwardly boxed version of its
366 primitive counterpart.
367
368 {\em END IDLE SPECULATION BY SIMON}
369
370 \begin{code}
371 boolTy = mkTyConTy boolTyCon
372
373 boolTyCon = pcTyCon True NonRecursive boolTyConName
374                     [] [] [falseDataCon, trueDataCon]
375
376 falseDataCon = pcDataCon falseDataConName [] [] [] boolTyCon
377 trueDataCon  = pcDataCon trueDataConName  [] [] [] boolTyCon
378
379 falseDataConId = dataConWorkId falseDataCon
380 trueDataConId  = dataConWorkId trueDataCon
381 \end{code}
382
383 %************************************************************************
384 %*                                                                      *
385 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
386 %*                                                                      *
387 %************************************************************************
388
389 Special syntax, deeply wired in, but otherwise an ordinary algebraic
390 data types:
391 \begin{verbatim}
392 data [] a = [] | a : (List a)
393 data () = ()
394 data (,) a b = (,,) a b
395 ...
396 \end{verbatim}
397
398 \begin{code}
399 mkListTy :: Type -> Type
400 mkListTy ty = mkTyConApp listTyCon [ty]
401
402 listTyCon = pcRecDataTyCon listTyConName
403                         alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
404
405 nilDataCon  = pcDataCon nilDataConName alpha_tyvar [] [] listTyCon
406 consDataCon = pcDataCon consDataConName
407                alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
408 -- Interesting: polymorphic recursion would help here.
409 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
410 -- gets the over-specific type (Type -> Type)
411 \end{code}
412
413 %************************************************************************
414 %*                                                                      *
415 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
416 %*                                                                      *
417 %************************************************************************
418
419 The tuple types are definitely magic, because they form an infinite
420 family.
421
422 \begin{itemize}
423 \item
424 They have a special family of type constructors, of type @TyCon@
425 These contain the tycon arity, but don't require a Unique.
426
427 \item
428 They have a special family of constructors, of type
429 @Id@. Again these contain their arity but don't need a Unique.
430
431 \item
432 There should be a magic way of generating the info tables and
433 entry code for all tuples.
434
435 But at the moment we just compile a Haskell source
436 file\srcloc{lib/prelude/...} containing declarations like:
437 \begin{verbatim}
438 data Tuple0             = Tup0
439 data Tuple2  a b        = Tup2  a b
440 data Tuple3  a b c      = Tup3  a b c
441 data Tuple4  a b c d    = Tup4  a b c d
442 ...
443 \end{verbatim}
444 The print-names associated with the magic @Id@s for tuple constructors
445 ``just happen'' to be the same as those generated by these
446 declarations.
447
448 \item
449 The instance environment should have a magic way to know
450 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
451 so on. \ToDo{Not implemented yet.}
452
453 \item
454 There should also be a way to generate the appropriate code for each
455 of these instances, but (like the info tables and entry code) it is
456 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
457 \end{itemize}
458
459 \begin{code}
460 mkTupleTy :: Boxity -> Int -> [Type] -> Type
461 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
462
463 unitTy    = mkTupleTy Boxed 0 []
464 \end{code}
465
466 %************************************************************************
467 %*                                                                      *
468 \subsection[TysWiredIn-PArr]{The @[::]@ type}
469 %*                                                                      *
470 %************************************************************************
471
472 Special syntax for parallel arrays needs some wired in definitions.
473
474 \begin{code}
475 -- construct a type representing the application of the parallel array
476 -- constructor 
477 --
478 mkPArrTy    :: Type -> Type
479 mkPArrTy ty  = mkTyConApp parrTyCon [ty]
480
481 -- represents the type constructor of parallel arrays
482 --
483 -- * this must match the definition in `PrelPArr'
484 --
485 -- NB: Although the constructor is given here, it will not be accessible in
486 --     user code as it is not in the environment of any compiled module except
487 --     `PrelPArr'.
488 --
489 parrTyCon :: TyCon
490 parrTyCon  = pcNonRecDataTyCon parrTyConName alpha_tyvar [(True, False)] [parrDataCon]
491
492 parrDataCon :: DataCon
493 parrDataCon  = pcDataCon 
494                  parrDataConName 
495                  alpha_tyvar            -- forall'ed type variables
496                  []                     -- context
497                  [intPrimTy,            -- 1st argument: Int#
498                   mkTyConApp            -- 2nd argument: Array# a
499                     arrayPrimTyCon 
500                     alpha_ty] 
501                  parrTyCon
502
503 -- check whether a type constructor is the constructor for parallel arrays
504 --
505 isPArrTyCon    :: TyCon -> Bool
506 isPArrTyCon tc  = tyConName tc == parrTyConName
507
508 -- fake array constructors
509 --
510 -- * these constructors are never really used to represent array values;
511 --   however, they are very convenient during desugaring (and, in particular,
512 --   in the pattern matching compiler) to treat array pattern just like
513 --   yet another constructor pattern
514 --
515 parrFakeCon                        :: Arity -> DataCon
516 parrFakeCon i | i > mAX_TUPLE_SIZE  = mkPArrFakeCon  i  -- build one specially
517 parrFakeCon i                       = parrFakeConArr!i
518
519 -- pre-defined set of constructors
520 --
521 parrFakeConArr :: Array Int DataCon
522 parrFakeConArr  = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)   
523                                             | i <- [0..mAX_TUPLE_SIZE]]
524
525 -- build a fake parallel array constructor for the given arity
526 --
527 mkPArrFakeCon       :: Int -> DataCon
528 mkPArrFakeCon arity  = data_con
529   where
530         data_con  = pcDataCon name [tyvar] [] tyvarTys parrTyCon
531         tyvar     = head alphaTyVars
532         tyvarTys  = replicate arity $ mkTyVarTy tyvar
533         nameStr   = mkFastString ("MkPArr" ++ show arity)
534         name      = mkWiredInName pREL_PARR (mkOccFS dataName nameStr) uniq
535                                   Nothing (ADataCon data_con)
536         uniq      = mkPArrDataConUnique arity
537
538 -- checks whether a data constructor is a fake constructor for parallel arrays
539 --
540 isPArrFakeCon      :: DataCon -> Bool
541 isPArrFakeCon dcon  = dcon == parrFakeCon (dataConSourceArity dcon)
542 \end{code}
543