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