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