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