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