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