[project @ 1996-04-07 15:41:24 by partain]
[ghc-hetmet.git] / ghc / compiler / prelude / TysWiredIn.lhs
1 %
2 % (c) The GRASP Project, Glasgow University, 1994-1995
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 #include "HsVersions.h"
14
15 module TysWiredIn (
16         addrDataCon,
17         addrTy,
18         addrTyCon,
19         boolTy,
20         boolTyCon,
21         charDataCon,
22         charTy,
23         charTyCon,
24         consDataCon,
25         doubleDataCon,
26         doubleTy,
27         doubleTyCon,
28         eqDataCon,
29         falseDataCon,
30         floatDataCon,
31         floatTy,
32         floatTyCon,
33         getStatePairingConInfo,
34         gtDataCon,
35         intDataCon,
36         intTy,
37         intTyCon,
38         integerTy,
39         integerTyCon,
40         integerDataCon,
41         liftDataCon,
42         liftTyCon,
43         listTyCon,
44         ltDataCon,
45         mallocPtrTyCon,
46         mkLiftTy,
47         mkListTy,
48         mkPrimIoTy,
49         mkStateTransformerTy,
50         mkTupleTy,
51         nilDataCon,
52         orderingTy,
53         orderingTyCon,
54         primIoTyCon,
55         ratioDataCon,
56         ratioTyCon,
57         rationalTy,
58         rationalTyCon,
59         realWorldStateTy,
60         return2GMPsTyCon,
61         returnIntAndGMPTyCon,
62         stTyCon,
63         stablePtrTyCon,
64         stateAndAddrPrimTyCon,
65         stateAndArrayPrimTyCon,
66         stateAndByteArrayPrimTyCon,
67         stateAndCharPrimTyCon,
68         stateAndDoublePrimTyCon,
69         stateAndFloatPrimTyCon,
70         stateAndIntPrimTyCon,
71         stateAndMallocPtrPrimTyCon,
72         stateAndMutableArrayPrimTyCon,
73         stateAndMutableByteArrayPrimTyCon,
74         stateAndPtrPrimTyCon,
75         stateAndStablePtrPrimTyCon,
76         stateAndSynchVarPrimTyCon,
77         stateAndWordPrimTyCon,
78         stateDataCon,
79         stateTyCon,
80         stringTy,
81         stringTyCon,
82         trueDataCon,
83         unitTy,
84         wordDataCon,
85         wordTy,
86         wordTyCon
87
88     ) where
89
90 import Ubiq
91 import TyLoop           ( mkDataCon, StrictnessMark(..) )
92
93 -- friends:
94 import PrelMods
95 import TysPrim
96
97 -- others:
98 import SpecEnv          ( SpecEnv(..) )
99 import Kind             ( mkBoxedTypeKind, mkArrowKind )
100 import Name             ( mkBuiltinName )
101 import SrcLoc           ( mkBuiltinSrcLoc )
102 import TyCon            ( mkDataTyCon, mkTupleTyCon, mkSynTyCon,
103                           NewOrData(..), TyCon
104                         )
105 import Type             ( mkTyConTy, applyTyCon, mkSynTy, mkSigmaTy,
106                           mkFunTys, maybeAppDataTyCon,
107                           GenType(..), ThetaType(..), TauType(..) )
108 import TyVar            ( getTyVarKind, alphaTyVar, betaTyVar )
109 import Unique
110 import Util             ( assoc, panic )
111
112 nullSpecEnv =  error "TysWiredIn:nullSpecEnv =  "
113 addOneToSpecEnv =  error "TysWiredIn:addOneToSpecEnv =  "
114 pc_gen_specs = error "TysWiredIn:pc_gen_specs  "
115 mkSpecInfo = error "TysWiredIn:SpecInfo"
116
117 pcDataTyCon :: Unique{-TyConKey-} -> Module -> FAST_STRING
118             -> [TyVar] -> [Id] -> TyCon
119 pcDataTyCon key mod str tyvars cons
120   = mkDataTyCon (mkBuiltinName key mod str) tycon_kind 
121                 tyvars [{-no context-}] cons [{-no derivings-}]
122                 DataType
123   where
124     tycon_kind = foldr (mkArrowKind . getTyVarKind) mkBoxedTypeKind tyvars
125
126 pcDataCon :: Unique{-DataConKey-} -> Module -> FAST_STRING
127           -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> SpecEnv -> Id
128 pcDataCon key mod str tyvars context arg_tys tycon specenv
129   = mkDataCon (mkBuiltinName key mod str)
130         [ NotMarkedStrict | a <- arg_tys ]
131         [ {- no labelled fields -} ]
132         tyvars context arg_tys tycon
133         -- specenv
134
135 pcGenerateDataSpecs :: Type -> SpecEnv
136 pcGenerateDataSpecs ty
137   = pc_gen_specs False err err err ty
138   where
139     err = panic "PrelUtils:GenerateDataSpecs"
140 \end{code}
141
142 %************************************************************************
143 %*                                                                      *
144 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
145 %*                                                                      *
146 %************************************************************************
147
148 \begin{code}
149 charTy = mkTyConTy charTyCon
150
151 charTyCon = pcDataTyCon charTyConKey pRELUDE_BUILTIN SLIT("Char") [] [charDataCon]
152 charDataCon = pcDataCon charDataConKey pRELUDE_BUILTIN SLIT("C#") [] [] [charPrimTy] charTyCon nullSpecEnv
153 \end{code}
154
155 \begin{code}
156 intTy = mkTyConTy intTyCon 
157
158 intTyCon = pcDataTyCon intTyConKey pRELUDE_BUILTIN SLIT("Int") [] [intDataCon]
159 intDataCon = pcDataCon intDataConKey pRELUDE_BUILTIN SLIT("I#") [] [] [intPrimTy] intTyCon nullSpecEnv
160 \end{code}
161
162 \begin{code}
163 wordTy = mkTyConTy wordTyCon
164
165 wordTyCon = pcDataTyCon wordTyConKey pRELUDE_BUILTIN SLIT("_Word") [] [wordDataCon]
166 wordDataCon = pcDataCon wordDataConKey pRELUDE_BUILTIN SLIT("W#") [] [] [wordPrimTy] wordTyCon nullSpecEnv
167 \end{code}
168
169 \begin{code}
170 addrTy = mkTyConTy addrTyCon
171
172 addrTyCon = pcDataTyCon addrTyConKey pRELUDE_BUILTIN SLIT("_Addr") [] [addrDataCon]
173 addrDataCon = pcDataCon addrDataConKey pRELUDE_BUILTIN SLIT("A#") [] [] [addrPrimTy] addrTyCon nullSpecEnv
174 \end{code}
175
176 \begin{code}
177 floatTy = mkTyConTy floatTyCon
178
179 floatTyCon = pcDataTyCon floatTyConKey pRELUDE_BUILTIN SLIT("Float") [] [floatDataCon]
180 floatDataCon = pcDataCon floatDataConKey pRELUDE_BUILTIN SLIT("F#") [] [] [floatPrimTy] floatTyCon nullSpecEnv
181 \end{code}
182
183 \begin{code}
184 doubleTy = mkTyConTy doubleTyCon
185
186 doubleTyCon = pcDataTyCon doubleTyConKey pRELUDE_BUILTIN SLIT("Double") [] [doubleDataCon]
187 doubleDataCon = pcDataCon doubleDataConKey pRELUDE_BUILTIN SLIT("D#") [] [] [doublePrimTy] doubleTyCon nullSpecEnv
188 \end{code}
189
190 \begin{code}
191 mkStateTy ty     = applyTyCon stateTyCon [ty]
192 realWorldStateTy = mkStateTy realWorldTy -- a common use
193
194 stateTyCon = pcDataTyCon stateTyConKey pRELUDE_BUILTIN SLIT("_State") [alphaTyVar] [stateDataCon]
195 stateDataCon
196   = pcDataCon stateDataConKey pRELUDE_BUILTIN SLIT("S#")
197         [alphaTyVar] [] [mkStatePrimTy alphaTy] stateTyCon nullSpecEnv
198 \end{code}
199
200 \begin{code}
201 stablePtrTyCon
202   = pcDataTyCon stablePtrTyConKey gLASGOW_MISC SLIT("_StablePtr")
203         [alphaTyVar] [stablePtrDataCon]
204   where
205     stablePtrDataCon
206       = pcDataCon stablePtrDataConKey gLASGOW_MISC SLIT("_StablePtr")
207             [alphaTyVar] [] [applyTyCon stablePtrPrimTyCon [alphaTy]] stablePtrTyCon nullSpecEnv
208 \end{code}
209
210 \begin{code}
211 mallocPtrTyCon
212   = pcDataTyCon mallocPtrTyConKey gLASGOW_MISC SLIT("_MallocPtr")
213         [] [mallocPtrDataCon]
214   where
215     mallocPtrDataCon
216       = pcDataCon mallocPtrDataConKey gLASGOW_MISC SLIT("_MallocPtr")
217             [] [] [applyTyCon mallocPtrPrimTyCon []] mallocPtrTyCon nullSpecEnv
218 \end{code}
219
220 %************************************************************************
221 %*                                                                      *
222 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
223 %*                                                                      *
224 %************************************************************************
225
226 @Integer@ and its pals are not really primitive.  @Integer@ itself, first:
227 \begin{code}
228 integerTy :: GenType t u
229 integerTy    = mkTyConTy integerTyCon
230
231 integerTyCon = pcDataTyCon integerTyConKey pRELUDE_BUILTIN SLIT("Integer") [] [integerDataCon]
232
233 integerDataCon = pcDataCon integerDataConKey pRELUDE_BUILTIN SLIT("J#")
234                 [] [] [intPrimTy, intPrimTy, byteArrayPrimTy] integerTyCon nullSpecEnv
235 \end{code}
236
237 And the other pairing types:
238 \begin{code}
239 return2GMPsTyCon = pcDataTyCon return2GMPsTyConKey
240         pRELUDE_BUILTIN SLIT("_Return2GMPs") [] [return2GMPsDataCon]
241
242 return2GMPsDataCon
243   = pcDataCon return2GMPsDataConKey pRELUDE_BUILTIN SLIT("_Return2GMPs") [] []
244         [intPrimTy, intPrimTy, byteArrayPrimTy,
245          intPrimTy, intPrimTy, byteArrayPrimTy] return2GMPsTyCon nullSpecEnv
246
247 returnIntAndGMPTyCon = pcDataTyCon returnIntAndGMPTyConKey
248         pRELUDE_BUILTIN SLIT("_ReturnIntAndGMP") [] [returnIntAndGMPDataCon]
249
250 returnIntAndGMPDataCon
251   = pcDataCon returnIntAndGMPDataConKey pRELUDE_BUILTIN SLIT("_ReturnIntAndGMP") [] []
252         [intPrimTy, intPrimTy, intPrimTy, byteArrayPrimTy] returnIntAndGMPTyCon nullSpecEnv
253 \end{code}
254
255 %************************************************************************
256 %*                                                                      *
257 \subsection[TysWiredIn-state-pairing]{``State-pairing'' types}
258 %*                                                                      *
259 %************************************************************************
260
261 These boring types pair a \tr{State#} with another primitive type.
262 They are not really primitive, so they are given here, not in
263 \tr{TysPrim.lhs}.
264
265 We fish one of these \tr{StateAnd<blah>#} things with
266 @getStatePairingConInfo@ (given a little way down).
267
268 \begin{code}
269 stateAndPtrPrimTyCon
270   = pcDataTyCon stateAndPtrPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndPtr#")
271                 [alphaTyVar, betaTyVar] [stateAndPtrPrimDataCon]
272 stateAndPtrPrimDataCon
273   = pcDataCon stateAndPtrPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndPtr#")
274                 [alphaTyVar, betaTyVar] [] [mkStatePrimTy alphaTy, betaTy]
275                 stateAndPtrPrimTyCon nullSpecEnv
276
277 stateAndCharPrimTyCon
278   = pcDataTyCon stateAndCharPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndChar#")
279                 [alphaTyVar] [stateAndCharPrimDataCon]
280 stateAndCharPrimDataCon
281   = pcDataCon stateAndCharPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndChar#")
282                 [alphaTyVar] [] [mkStatePrimTy alphaTy, charPrimTy]
283                 stateAndCharPrimTyCon nullSpecEnv
284
285 stateAndIntPrimTyCon
286   = pcDataTyCon stateAndIntPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndInt#")
287                 [alphaTyVar] [stateAndIntPrimDataCon]
288 stateAndIntPrimDataCon
289   = pcDataCon stateAndIntPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndInt#")
290                 [alphaTyVar] [] [mkStatePrimTy alphaTy, intPrimTy]
291                 stateAndIntPrimTyCon nullSpecEnv
292
293 stateAndWordPrimTyCon
294   = pcDataTyCon stateAndWordPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndWord#")
295                 [alphaTyVar] [stateAndWordPrimDataCon]
296 stateAndWordPrimDataCon
297   = pcDataCon stateAndWordPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndWord#")
298                 [alphaTyVar] [] [mkStatePrimTy alphaTy, wordPrimTy]
299                 stateAndWordPrimTyCon nullSpecEnv
300
301 stateAndAddrPrimTyCon
302   = pcDataTyCon stateAndAddrPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndAddr#")
303                 [alphaTyVar] [stateAndAddrPrimDataCon]
304 stateAndAddrPrimDataCon
305   = pcDataCon stateAndAddrPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndAddr#")
306                 [alphaTyVar] [] [mkStatePrimTy alphaTy, addrPrimTy]
307                 stateAndAddrPrimTyCon nullSpecEnv
308
309 stateAndStablePtrPrimTyCon
310   = pcDataTyCon stateAndStablePtrPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndStablePtr#")
311                 [alphaTyVar, betaTyVar] [stateAndStablePtrPrimDataCon]
312 stateAndStablePtrPrimDataCon
313   = pcDataCon stateAndStablePtrPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndStablePtr#")
314                 [alphaTyVar, betaTyVar] []
315                 [mkStatePrimTy alphaTy, applyTyCon stablePtrPrimTyCon [betaTy]]
316                 stateAndStablePtrPrimTyCon nullSpecEnv
317
318 stateAndMallocPtrPrimTyCon
319   = pcDataTyCon stateAndMallocPtrPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndMallocPtr#")
320                 [alphaTyVar] [stateAndMallocPtrPrimDataCon]
321 stateAndMallocPtrPrimDataCon
322   = pcDataCon stateAndMallocPtrPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndMallocPtr#")
323                 [alphaTyVar] []
324                 [mkStatePrimTy alphaTy, applyTyCon mallocPtrPrimTyCon []]
325                 stateAndMallocPtrPrimTyCon nullSpecEnv
326
327 stateAndFloatPrimTyCon
328   = pcDataTyCon stateAndFloatPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndFloat#")
329                 [alphaTyVar] [stateAndFloatPrimDataCon]
330 stateAndFloatPrimDataCon
331   = pcDataCon stateAndFloatPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndFloat#")
332                 [alphaTyVar] [] [mkStatePrimTy alphaTy, floatPrimTy]
333                 stateAndFloatPrimTyCon nullSpecEnv
334
335 stateAndDoublePrimTyCon
336   = pcDataTyCon stateAndDoublePrimTyConKey pRELUDE_BUILTIN SLIT("StateAndDouble#")
337                 [alphaTyVar] [stateAndDoublePrimDataCon]
338 stateAndDoublePrimDataCon
339   = pcDataCon stateAndDoublePrimDataConKey pRELUDE_BUILTIN SLIT("StateAndDouble#")
340                 [alphaTyVar] [] [mkStatePrimTy alphaTy, doublePrimTy]
341                 stateAndDoublePrimTyCon nullSpecEnv
342 \end{code}
343
344 \begin{code}
345 stateAndArrayPrimTyCon
346   = pcDataTyCon stateAndArrayPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndArray#")
347                 [alphaTyVar, betaTyVar] [stateAndArrayPrimDataCon]
348 stateAndArrayPrimDataCon
349   = pcDataCon stateAndArrayPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndArray#")
350                 [alphaTyVar, betaTyVar] [] [mkStatePrimTy alphaTy, mkArrayPrimTy betaTy]
351                 stateAndArrayPrimTyCon nullSpecEnv
352
353 stateAndMutableArrayPrimTyCon
354   = pcDataTyCon stateAndMutableArrayPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndMutableArray#")
355                 [alphaTyVar, betaTyVar] [stateAndMutableArrayPrimDataCon]
356 stateAndMutableArrayPrimDataCon
357   = pcDataCon stateAndMutableArrayPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndMutableArray#")
358                 [alphaTyVar, betaTyVar] [] [mkStatePrimTy alphaTy, mkMutableArrayPrimTy alphaTy betaTy]
359                 stateAndMutableArrayPrimTyCon nullSpecEnv
360
361 stateAndByteArrayPrimTyCon
362   = pcDataTyCon stateAndByteArrayPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndByteArray#")
363                 [alphaTyVar] [stateAndByteArrayPrimDataCon]
364 stateAndByteArrayPrimDataCon
365   = pcDataCon stateAndByteArrayPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndByteArray#")
366                 [alphaTyVar] [] [mkStatePrimTy alphaTy, byteArrayPrimTy]
367                 stateAndByteArrayPrimTyCon nullSpecEnv
368
369 stateAndMutableByteArrayPrimTyCon
370   = pcDataTyCon stateAndMutableByteArrayPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndMutableByteArray#")
371                 [alphaTyVar] [stateAndMutableByteArrayPrimDataCon]
372 stateAndMutableByteArrayPrimDataCon
373   = pcDataCon stateAndMutableByteArrayPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndMutableByteArray#")
374                 [alphaTyVar] [] [mkStatePrimTy alphaTy, applyTyCon mutableByteArrayPrimTyCon [alphaTy]]
375                 stateAndMutableByteArrayPrimTyCon nullSpecEnv
376
377 stateAndSynchVarPrimTyCon
378   = pcDataTyCon stateAndSynchVarPrimTyConKey pRELUDE_BUILTIN SLIT("StateAndSynchVar#")
379                 [alphaTyVar, betaTyVar] [stateAndSynchVarPrimDataCon]
380 stateAndSynchVarPrimDataCon
381   = pcDataCon stateAndSynchVarPrimDataConKey pRELUDE_BUILTIN SLIT("StateAndSynchVar#")
382                 [alphaTyVar, betaTyVar] [] [mkStatePrimTy alphaTy, mkSynchVarPrimTy alphaTy betaTy]
383                 stateAndSynchVarPrimTyCon nullSpecEnv
384 \end{code}
385
386 The ccall-desugaring mechanism uses this function to figure out how to
387 rebox the result.  It's really a HACK, especially the part about
388 how many types to drop from \tr{tys_applied}.
389
390 \begin{code}
391 getStatePairingConInfo
392         :: Type -- primitive type
393         -> (Id,         -- state pair constructor for prim type
394             Type)       -- type of state pair
395
396 getStatePairingConInfo prim_ty
397   = case (maybeAppDataTyCon prim_ty) of
398       Nothing -> panic "getStatePairingConInfo:1"
399       Just (prim_tycon, tys_applied, _) ->
400         let
401             (pair_con, pair_tycon, num_tys) = assoc "getStatePairingConInfo" tbl prim_tycon
402             pair_ty = applyTyCon pair_tycon (realWorldTy : drop num_tys tys_applied)
403         in
404         (pair_con, pair_ty)
405   where
406     tbl = [
407         (charPrimTyCon, (stateAndCharPrimDataCon, stateAndCharPrimTyCon, 0)),
408         (intPrimTyCon, (stateAndIntPrimDataCon, stateAndIntPrimTyCon, 0)),
409         (wordPrimTyCon, (stateAndWordPrimDataCon, stateAndWordPrimTyCon, 0)),
410         (addrPrimTyCon, (stateAndAddrPrimDataCon, stateAndAddrPrimTyCon, 0)),
411         (stablePtrPrimTyCon, (stateAndStablePtrPrimDataCon, stateAndStablePtrPrimTyCon, 0)),
412         (mallocPtrPrimTyCon, (stateAndMallocPtrPrimDataCon, stateAndMallocPtrPrimTyCon, 0)),
413         (floatPrimTyCon, (stateAndFloatPrimDataCon, stateAndFloatPrimTyCon, 0)),
414         (doublePrimTyCon, (stateAndDoublePrimDataCon, stateAndDoublePrimTyCon, 0)),
415         (arrayPrimTyCon, (stateAndArrayPrimDataCon, stateAndArrayPrimTyCon, 0)),
416         (mutableArrayPrimTyCon, (stateAndMutableArrayPrimDataCon, stateAndMutableArrayPrimTyCon, 1)),
417         (byteArrayPrimTyCon, (stateAndByteArrayPrimDataCon, stateAndByteArrayPrimTyCon, 0)),
418         (mutableByteArrayPrimTyCon, (stateAndMutableByteArrayPrimDataCon, stateAndMutableByteArrayPrimTyCon, 1)),
419         (synchVarPrimTyCon, (stateAndSynchVarPrimDataCon, stateAndSynchVarPrimTyCon, 1))
420         -- (PtrPrimTyCon, (stateAndPtrPrimDataCon, stateAndPtrPrimTyCon, 0)),
421         ]
422 \end{code}
423
424 %************************************************************************
425 %*                                                                      *
426 \subsection[TysWiredIn-ST]{The basic @_ST@ state-transformer type}
427 %*                                                                      *
428 %************************************************************************
429
430 This is really just an ordinary synonym, except it is ABSTRACT.
431
432 \begin{code}
433 mkStateTransformerTy s a = mkSynTy stTyCon [s, a]
434
435 stTyCon
436   = mkSynTyCon
437      (mkBuiltinName stTyConKey gLASGOW_ST SLIT("_ST"))
438      (panic "TysWiredIn.stTyCon:Kind")
439      2 [alphaTyVar, betaTyVar]
440      (mkFunTys [mkStateTy alphaTy] (mkTupleTy 2 [betaTy, mkStateTy alphaTy]))
441 \end{code}
442
443 %************************************************************************
444 %*                                                                      *
445 \subsection[TysWiredIn-IO]{The @PrimIO@ and @IO@ monadic-I/O types}
446 %*                                                                      *
447 %************************************************************************
448
449 @PrimIO@ and @IO@ really are just plain synonyms.
450
451 \begin{code}
452 mkPrimIoTy a = mkSynTy primIoTyCon [a]
453
454 primIoTyCon
455   = mkSynTyCon
456      (mkBuiltinName primIoTyConKey pRELUDE_PRIMIO SLIT("PrimIO"))
457      (panic "TysWiredIn.primIoTyCon:Kind")
458      1 [alphaTyVar] (mkStateTransformerTy realWorldTy alphaTy)
459 \end{code}
460
461 %************************************************************************
462 %*                                                                      *
463 \subsection[TysWiredIn-Bool]{The @Bool@ type}
464 %*                                                                      *
465 %************************************************************************
466
467 An ordinary enumeration type, but deeply wired in.  There are no
468 magical operations on @Bool@ (just the regular Prelude code).
469
470 {\em BEGIN IDLE SPECULATION BY SIMON}
471
472 This is not the only way to encode @Bool@.  A more obvious coding makes
473 @Bool@ just a boxed up version of @Bool#@, like this:
474 \begin{verbatim}
475 type Bool# = Int#
476 data Bool = MkBool Bool#
477 \end{verbatim}
478
479 Unfortunately, this doesn't correspond to what the Report says @Bool@
480 looks like!  Furthermore, we get slightly less efficient code (I
481 think) with this coding. @gtInt@ would look like this:
482
483 \begin{verbatim}
484 gtInt :: Int -> Int -> Bool
485 gtInt x y = case x of I# x# ->
486             case y of I# y# ->
487             case (gtIntPrim x# y#) of
488                 b# -> MkBool b#
489 \end{verbatim}
490
491 Notice that the result of the @gtIntPrim@ comparison has to be turned
492 into an integer (here called @b#@), and returned in a @MkBool@ box.
493
494 The @if@ expression would compile to this:
495 \begin{verbatim}
496 case (gtInt x y) of
497   MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
498 \end{verbatim}
499
500 I think this code is a little less efficient than the previous code,
501 but I'm not certain.  At all events, corresponding with the Report is
502 important.  The interesting thing is that the language is expressive
503 enough to describe more than one alternative; and that a type doesn't
504 necessarily need to be a straightforwardly boxed version of its
505 primitive counterpart.
506
507 {\em END IDLE SPECULATION BY SIMON}
508
509 \begin{code}
510 boolTy = mkTyConTy boolTyCon
511
512 boolTyCon = pcDataTyCon boolTyConKey pRELUDE_CORE SLIT("Bool") [] [falseDataCon, trueDataCon]
513
514 falseDataCon = pcDataCon falseDataConKey pRELUDE_CORE SLIT("False") [] [] [] boolTyCon nullSpecEnv
515 trueDataCon  = pcDataCon trueDataConKey  pRELUDE_CORE SLIT("True")  [] [] [] boolTyCon nullSpecEnv
516 \end{code}
517
518 %************************************************************************
519 %*                                                                      *
520 \subsection[TysWiredIn-Ordering]{The @Ordering@ type}
521 %*                                                                      *
522 %************************************************************************
523
524 \begin{code}
525 ---------------------------------------------
526 -- data Ordering = LT | EQ | GT deriving ()
527 ---------------------------------------------
528
529 orderingTy = mkTyConTy orderingTyCon
530
531 orderingTyCon = pcDataTyCon orderingTyConKey pRELUDE_BUILTIN SLIT("Ordering") []
532                             [ltDataCon, eqDataCon, gtDataCon]
533
534 ltDataCon  = pcDataCon ltDataConKey pRELUDE_BUILTIN SLIT("LT") [] [] [] orderingTyCon nullSpecEnv
535 eqDataCon  = pcDataCon eqDataConKey pRELUDE_BUILTIN SLIT("EQ") [] [] [] orderingTyCon nullSpecEnv
536 gtDataCon  = pcDataCon gtDataConKey pRELUDE_BUILTIN SLIT("GT") [] [] [] orderingTyCon nullSpecEnv
537 \end{code}
538
539 %************************************************************************
540 %*                                                                      *
541 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
542 %*                                                                      *
543 %************************************************************************
544
545 Special syntax, deeply wired in, but otherwise an ordinary algebraic
546 data type:
547 \begin{verbatim}
548 data List a = Nil | a : (List a)
549 ToDo: data [] a = [] | a : (List a)
550 ToDo: data () = ()
551       data (,,) a b c = (,,) a b c
552 \end{verbatim}
553
554 \begin{code}
555 mkListTy :: GenType t u -> GenType t u
556 mkListTy ty = applyTyCon listTyCon [ty]
557
558 alphaListTy = mkSigmaTy [alphaTyVar] [] (applyTyCon listTyCon [alphaTy])
559
560 listTyCon = pcDataTyCon listTyConKey pRELUDE_BUILTIN SLIT("[]") 
561                         [alphaTyVar] [nilDataCon, consDataCon]
562
563 nilDataCon  = pcDataCon nilDataConKey  pRELUDE_BUILTIN SLIT("[]") [alphaTyVar] [] [] listTyCon
564                 (pcGenerateDataSpecs alphaListTy)
565 consDataCon = pcDataCon consDataConKey pRELUDE_BUILTIN SLIT(":")
566                 [alphaTyVar] [] [alphaTy, applyTyCon listTyCon [alphaTy]] listTyCon
567                 (pcGenerateDataSpecs alphaListTy)
568 -- Interesting: polymorphic recursion would help here.
569 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
570 -- gets the over-specific type (Type -> Type)
571 \end{code}
572
573 %************************************************************************
574 %*                                                                      *
575 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
576 %*                                                                      *
577 %************************************************************************
578
579 The tuple types are definitely magic, because they form an infinite
580 family.
581
582 \begin{itemize}
583 \item
584 They have a special family of type constructors, of type @TyCon@
585 These contain the tycon arity, but don't require a Unique.
586
587 \item
588 They have a special family of constructors, of type
589 @Id@. Again these contain their arity but don't need a Unique.
590
591 \item
592 There should be a magic way of generating the info tables and
593 entry code for all tuples.
594
595 But at the moment we just compile a Haskell source
596 file\srcloc{lib/prelude/...} containing declarations like:
597 \begin{verbatim}
598 data Tuple0             = Tup0
599 data Tuple2  a b        = Tup2  a b
600 data Tuple3  a b c      = Tup3  a b c
601 data Tuple4  a b c d    = Tup4  a b c d
602 ...
603 \end{verbatim}
604 The print-names associated with the magic @Id@s for tuple constructors
605 ``just happen'' to be the same as those generated by these
606 declarations.
607
608 \item
609 The instance environment should have a magic way to know
610 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
611 so on. \ToDo{Not implemented yet.}
612
613 \item
614 There should also be a way to generate the appropriate code for each
615 of these instances, but (like the info tables and entry code) it is
616 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
617 \end{itemize}
618
619 \begin{code}
620 mkTupleTy :: Int -> [GenType t u] -> GenType t u
621
622 mkTupleTy arity tys = applyTyCon (mkTupleTyCon arity) tys
623
624 unitTy    = mkTupleTy 0 []
625 \end{code}
626
627 %************************************************************************
628 %*                                                                      *
629 \subsection[TysWiredIn-Ratios]{@Ratio@ and @Rational@}
630 %*                                                                      *
631 %************************************************************************
632
633 ToDo: make this (mostly) go away.
634
635 \begin{code}
636 rationalTy :: GenType t u
637
638 mkRatioTy ty = applyTyCon ratioTyCon [ty]
639 rationalTy   = mkRatioTy integerTy
640
641 ratioTyCon = pcDataTyCon ratioTyConKey pRELUDE_RATIO SLIT("Ratio") [alphaTyVar] [ratioDataCon]
642
643 ratioDataCon = pcDataCon ratioDataConKey pRELUDE_RATIO SLIT(":%")
644                 [alphaTyVar] [{-(integralClass,alphaTy)-}] [alphaTy, alphaTy] ratioTyCon nullSpecEnv
645         -- context omitted to match lib/prelude/ defn of "data Ratio ..."
646
647 rationalTyCon
648   = mkSynTyCon
649       (mkBuiltinName rationalTyConKey pRELUDE_RATIO SLIT("Rational"))
650       mkBoxedTypeKind
651       0 [] rationalTy -- == mkRatioTy integerTy
652 \end{code}
653
654 %************************************************************************
655 %*                                                                      *
656 \subsection[TysWiredIn-_Lift]{@_Lift@ type: to support array indexing}
657 %*                                                                      *
658 %************************************************************************
659
660 Again, deeply turgid: \tr{data _Lift a = _Lift a}.
661
662 \begin{code}
663 mkLiftTy ty = applyTyCon liftTyCon [ty]
664
665 {-
666 mkLiftTy ty
667   = mkSigmaTy tvs theta (applyTyCon liftTyCon [tau])
668   where
669     (tvs, theta, tau) = splitSigmaTy ty
670
671 isLiftTy ty
672   = case maybeAppDataTyCon tau of
673       Just (tycon, tys, _) -> tycon == liftTyCon
674       Nothing -> False
675   where
676     (tvs, theta, tau) = splitSigmaTy ty
677 -}
678
679
680 alphaLiftTy = mkSigmaTy [alphaTyVar] [] (applyTyCon liftTyCon [alphaTy])
681
682 liftTyCon
683   = pcDataTyCon liftTyConKey pRELUDE_BUILTIN SLIT("_Lift") [alphaTyVar] [liftDataCon]
684
685 liftDataCon
686   = pcDataCon liftDataConKey pRELUDE_BUILTIN SLIT("_Lift")
687                 [alphaTyVar] [] [alphaTy] liftTyCon
688                 ((pcGenerateDataSpecs alphaLiftTy) `addOneToSpecEnv`
689                  (mkSpecInfo [Just realWorldStatePrimTy] 0 bottom))
690   where
691     bottom = panic "liftDataCon:State# _RealWorld"
692 \end{code}
693
694
695 %************************************************************************
696 %*                                                                      *
697 \subsection[TysWiredIn-for-convenience]{Types wired in for convenience (e.g., @String@)}
698 %*                                                                      *
699 %************************************************************************
700
701 \begin{code}
702 stringTy = mkListTy charTy
703
704 stringTyCon
705  = mkSynTyCon
706      (mkBuiltinName stringTyConKey pRELUDE_CORE SLIT("String"))
707      mkBoxedTypeKind
708      0 [] stringTy
709 \end{code}