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