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