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