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