2 % (c) The GRASP Project, Glasgow University, 1994-1995
4 \section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types}
6 This module is about types that can be defined in Haskell, but which
7 must be wired into the compiler nonetheless.
9 This module tracks the ``state interface'' document, ``GHC prelude:
10 types and operations.''
32 getStatePairingConInfo,
61 tupleTyCon, tupleCon, unitTyCon, unitDataCon, pairTyCon, pairDataCon,
77 stateAndAddrPrimTyCon,
78 stateAndArrayPrimTyCon,
79 stateAndByteArrayPrimTyCon,
80 stateAndCharPrimTyCon,
81 stateAndDoublePrimTyCon,
82 stateAndFloatPrimTyCon,
84 stateAndInt64PrimTyCon,
85 stateAndForeignObjPrimTyCon,
86 stateAndMutableArrayPrimTyCon,
87 stateAndMutableByteArrayPrimTyCon,
89 stateAndPtrPrimDataCon,
90 stateAndStablePtrPrimTyCon,
91 stateAndSynchVarPrimTyCon,
92 stateAndWordPrimTyCon,
93 stateAndWord64PrimTyCon,
113 isFFIArgumentTy, -- :: Type -> Bool
114 isFFIResultTy, -- :: Type -> Bool
115 isFFIExternalTy, -- :: Type -> Bool
116 isAddrTy, -- :: Type -> Bool
120 #include "HsVersions.h"
122 import {-# SOURCE #-} MkId ( mkDataCon, mkTupleCon )
123 import {-# SOURCE #-} Id ( Id, StrictnessMark(..) )
130 import Kind ( mkBoxedTypeKind, mkArrowKind )
131 import Name ( mkWiredInTyConName, mkWiredInIdName )
132 import TyCon ( mkDataTyCon, mkTupleTyCon, mkSynTyCon,
135 import BasicTypes ( Module, NewOrData(..), RecFlag(..) )
136 import Type ( Type, mkTyConTy, mkTyConApp, mkSigmaTy, mkTyVarTys,
137 mkFunTy, mkFunTys, splitTyConApp_maybe, splitAlgTyConApp_maybe,
138 GenType(..), ThetaType, TauType, isUnpointedType )
139 import TyVar ( GenTyVar, TyVar, tyVarKind, alphaTyVars, alphaTyVar, betaTyVar )
140 import Lex ( mkTupNameStr )
142 import CmdLineOpts ( opt_GlasgowExts )
143 import Util ( assoc, panic )
146 alpha_tyvar = [alphaTyVar]
148 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
150 pcRecDataTyCon, pcNonRecDataTyCon, pcNonRecNewTyCon
151 :: Unique{-TyConKey-} -> Module -> FAST_STRING
152 -> [TyVar] -> [Id] -> TyCon
154 pcRecDataTyCon = pc_tycon DataType Recursive
155 pcNonRecDataTyCon = pc_tycon DataType NonRecursive
156 pcNonRecNewTyCon = pc_tycon NewType NonRecursive
158 pc_tycon new_or_data is_rec key mod str tyvars cons
161 tycon = mkDataTyCon name tycon_kind
166 Nothing -- Not a dictionary
170 name = mkWiredInTyConName key mod str tycon
171 tycon_kind = foldr (mkArrowKind . tyVarKind) mkBoxedTypeKind tyvars
173 pcSynTyCon key mod str kind arity tyvars expansion
176 tycon = mkSynTyCon name kind arity tyvars expansion
177 name = mkWiredInTyConName key mod str tycon
179 pcDataCon :: Unique{-DataConKey-} -> Module -> FAST_STRING
180 -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> Id
181 pcDataCon key mod str tyvars context arg_tys tycon
184 data_con = mkDataCon name
185 [ NotMarkedStrict | a <- arg_tys ]
186 [ {- no labelled fields -} ]
187 tyvars context [] [] arg_tys tycon
188 name = mkWiredInIdName key mod str data_con
191 %************************************************************************
193 \subsection[TysWiredIn-tuples]{The tuple types}
195 %************************************************************************
198 tupleTyCon :: Arity -> TyCon
202 tycon = mkTupleTyCon uniq name arity
203 uniq = mkTupleTyConUnique arity
204 name = mkWiredInTyConName uniq mod_name (mkTupNameStr arity) tycon
205 mod_name | arity == 0 = pREL_BASE
206 | otherwise = pREL_TUP
208 tupleCon :: Arity -> Id
212 tuple_con = mkTupleCon arity name ty
213 uniq = mkTupleDataConUnique arity
214 name = mkWiredInIdName uniq mod_name (mkTupNameStr arity) tuple_con
215 mod_name | arity == 0 = pREL_BASE
216 | otherwise = pREL_TUP
217 ty = mkSigmaTy tyvars [] (mkFunTys tyvar_tys (mkTyConApp tycon tyvar_tys))
218 tyvars = take arity alphaTyVars
219 tyvar_tys = mkTyVarTys tyvars
220 tycon = tupleTyCon arity
222 unitTyCon = tupleTyCon 0
223 pairTyCon = tupleTyCon 2
225 unitDataCon = tupleCon 0
226 pairDataCon = tupleCon 2
230 %************************************************************************
232 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
234 %************************************************************************
237 charTy = mkTyConTy charTyCon
239 charTyCon = pcNonRecDataTyCon charTyConKey pREL_BASE SLIT("Char") [] [charDataCon]
240 charDataCon = pcDataCon charDataConKey pREL_BASE SLIT("C#") [] [] [charPrimTy] charTyCon
242 stringTy = mkListTy charTy -- convenience only
246 intTy = mkTyConTy intTyCon
248 intTyCon = pcNonRecDataTyCon intTyConKey pREL_BASE SLIT("Int") [] [intDataCon]
249 intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon
251 isIntTy :: GenType flexi -> Bool
253 = case (splitAlgTyConApp_maybe ty) of
254 Just (tycon, [], _) -> uniqueOf tycon == intTyConKey
257 inIntRange :: Integer -> Bool -- Tells if an integer lies in the legal range of Ints
258 inIntRange i = (min_int <= i) && (i <= max_int)
260 max_int, min_int :: Integer
261 max_int = toInteger maxInt
262 min_int = toInteger minInt
264 int8TyCon = pcNonRecDataTyCon int8TyConKey iNT SLIT("Int8") [] [int8DataCon]
266 int8DataCon = pcDataCon int8DataConKey iNT SLIT("I8#") [] [] [intPrimTy] int8TyCon
268 int16TyCon = pcNonRecDataTyCon int16TyConKey iNT SLIT("Int16") [] [int16DataCon]
270 int16DataCon = pcDataCon int16DataConKey iNT SLIT("I16#") [] [] [intPrimTy] int16TyCon
272 int32TyCon = pcNonRecDataTyCon int32TyConKey iNT SLIT("Int32") [] [int32DataCon]
274 int32DataCon = pcDataCon int32DataConKey iNT SLIT("I32#") [] [] [intPrimTy] int32TyCon
276 int64Ty = mkTyConTy int64TyCon
278 int64TyCon = pcNonRecDataTyCon int64TyConKey iNT SLIT("Int64") [] [int64DataCon]
279 int64DataCon = pcDataCon int64DataConKey iNT SLIT("I64#") [] [] [int64PrimTy] int64TyCon
284 wordTy = mkTyConTy wordTyCon
286 wordTyCon = pcNonRecDataTyCon wordTyConKey pREL_FOREIGN SLIT("Word") [] [wordDataCon]
287 wordDataCon = pcDataCon wordDataConKey pREL_FOREIGN SLIT("W#") [] [] [wordPrimTy] wordTyCon
289 word8TyCon = pcNonRecDataTyCon word8TyConKey wORD SLIT("Word8") [] [word8DataCon]
291 word8DataCon = pcDataCon word8DataConKey wORD SLIT("W8#") [] [] [wordPrimTy] word8TyCon
293 word16TyCon = pcNonRecDataTyCon word16TyConKey wORD SLIT("Word16") [] [word16DataCon]
295 word16DataCon = pcDataCon word16DataConKey wORD SLIT("W16#") [] [] [wordPrimTy] word16TyCon
297 word32TyCon = pcNonRecDataTyCon word32TyConKey wORD SLIT("Word32") [] [word32DataCon]
299 word32DataCon = pcDataCon word32DataConKey wORD SLIT("W32#") [] [] [wordPrimTy] word32TyCon
301 word64Ty = mkTyConTy word64TyCon
303 word64TyCon = pcNonRecDataTyCon word64TyConKey wORD SLIT("Word64") [] [word64DataCon]
304 word64DataCon = pcDataCon word64DataConKey wORD SLIT("W64#") [] [] [word64PrimTy] word64TyCon
308 addrTy = mkTyConTy addrTyCon
310 addrTyCon = pcNonRecDataTyCon addrTyConKey pREL_ADDR SLIT("Addr") [] [addrDataCon]
311 addrDataCon = pcDataCon addrDataConKey pREL_ADDR SLIT("A#") [] [] [addrPrimTy] addrTyCon
313 isAddrTy :: GenType flexi -> Bool
315 = case (splitAlgTyConApp_maybe ty) of
316 Just (tycon, [], _) -> uniqueOf tycon == addrTyConKey
322 floatTy = mkTyConTy floatTyCon
324 floatTyCon = pcNonRecDataTyCon floatTyConKey pREL_BASE SLIT("Float") [] [floatDataCon]
325 floatDataCon = pcDataCon floatDataConKey pREL_BASE SLIT("F#") [] [] [floatPrimTy] floatTyCon
327 isFloatTy :: GenType flexi -> Bool
329 = case (splitAlgTyConApp_maybe ty) of
330 Just (tycon, [], _) -> uniqueOf tycon == floatTyConKey
336 doubleTy = mkTyConTy doubleTyCon
338 isDoubleTy :: GenType flexi -> Bool
340 = case (splitAlgTyConApp_maybe ty) of
341 Just (tycon, [], _) -> uniqueOf tycon == doubleTyConKey
344 doubleTyCon = pcNonRecDataTyCon doubleTyConKey pREL_BASE SLIT("Double") [] [doubleDataCon]
345 doubleDataCon = pcDataCon doubleDataConKey pREL_BASE SLIT("D#") [] [] [doublePrimTy] doubleTyCon
349 mkStateTy ty = mkTyConApp stateTyCon [ty]
350 realWorldStateTy = mkStateTy realWorldTy -- a common use
352 stateTyCon = pcNonRecDataTyCon stateTyConKey pREL_ST SLIT("State") alpha_tyvar [stateDataCon]
354 = pcDataCon stateDataConKey pREL_ST SLIT("S#")
355 alpha_tyvar [] [mkStatePrimTy alphaTy] stateTyCon
360 = pcNonRecDataTyCon stablePtrTyConKey pREL_FOREIGN SLIT("StablePtr")
361 alpha_tyvar [stablePtrDataCon]
364 = pcDataCon stablePtrDataConKey pREL_FOREIGN SLIT("StablePtr")
365 alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
370 = pcNonRecDataTyCon foreignObjTyConKey pREL_IO_BASE SLIT("ForeignObj")
371 [] [foreignObjDataCon]
374 = pcDataCon foreignObjDataConKey pREL_IO_BASE SLIT("ForeignObj")
375 [] [] [foreignObjPrimTy] foreignObjTyCon
378 %************************************************************************
380 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
382 %************************************************************************
384 @Integer@ and its pals are not really primitive. @Integer@ itself, first:
386 integerTy :: GenType t
387 integerTy = mkTyConTy integerTyCon
389 integerTyCon = pcNonRecDataTyCon integerTyConKey pREL_BASE SLIT("Integer") [] [integerDataCon]
391 integerDataCon = pcDataCon integerDataConKey pREL_BASE SLIT("J#")
392 [] [] [intPrimTy, intPrimTy, byteArrayPrimTy] integerTyCon
394 isIntegerTy :: GenType flexi -> Bool
396 = case (splitAlgTyConApp_maybe ty) of
397 Just (tycon, [], _) -> uniqueOf tycon == integerTyConKey
401 And the other pairing types:
403 return2GMPsTyCon = pcNonRecDataTyCon return2GMPsTyConKey
404 pREL_NUM SLIT("Return2GMPs") [] [return2GMPsDataCon]
407 = pcDataCon return2GMPsDataConKey pREL_NUM SLIT("Return2GMPs") [] []
408 [intPrimTy, intPrimTy, byteArrayPrimTy,
409 intPrimTy, intPrimTy, byteArrayPrimTy] return2GMPsTyCon
411 returnIntAndGMPTyCon = pcNonRecDataTyCon returnIntAndGMPTyConKey
412 pREL_NUM SLIT("ReturnIntAndGMP") [] [returnIntAndGMPDataCon]
414 returnIntAndGMPDataCon
415 = pcDataCon returnIntAndGMPDataConKey pREL_NUM SLIT("ReturnIntAndGMP") [] []
416 [intPrimTy, intPrimTy, intPrimTy, byteArrayPrimTy] returnIntAndGMPTyCon
419 %************************************************************************
421 \subsection[TysWiredIn-state-pairing]{``State-pairing'' types}
423 %************************************************************************
425 These boring types pair a \tr{State#} with another primitive type.
426 They are not really primitive, so they are given here, not in
429 We fish one of these \tr{StateAnd<blah>#} things with
430 @getStatePairingConInfo@ (given a little way down).
434 = pcNonRecDataTyCon stateAndPtrPrimTyConKey pREL_ST SLIT("StateAndPtr#")
435 alpha_beta_tyvars [stateAndPtrPrimDataCon]
436 stateAndPtrPrimDataCon
437 = pcDataCon stateAndPtrPrimDataConKey pREL_ST SLIT("StateAndPtr#")
438 alpha_beta_tyvars [] [mkStatePrimTy alphaTy, betaTy]
441 stateAndCharPrimTyCon
442 = pcNonRecDataTyCon stateAndCharPrimTyConKey pREL_ST SLIT("StateAndChar#")
443 alpha_tyvar [stateAndCharPrimDataCon]
444 stateAndCharPrimDataCon
445 = pcDataCon stateAndCharPrimDataConKey pREL_ST SLIT("StateAndChar#")
446 alpha_tyvar [] [mkStatePrimTy alphaTy, charPrimTy]
447 stateAndCharPrimTyCon
450 = pcNonRecDataTyCon stateAndIntPrimTyConKey pREL_ST SLIT("StateAndInt#")
451 alpha_tyvar [stateAndIntPrimDataCon]
452 stateAndIntPrimDataCon
453 = pcDataCon stateAndIntPrimDataConKey pREL_ST SLIT("StateAndInt#")
454 alpha_tyvar [] [mkStatePrimTy alphaTy, intPrimTy]
457 stateAndInt64PrimTyCon
458 = pcNonRecDataTyCon stateAndInt64PrimTyConKey pREL_ST SLIT("StateAndInt64#")
459 alpha_tyvar [stateAndInt64PrimDataCon]
460 stateAndInt64PrimDataCon
461 = pcDataCon stateAndInt64PrimDataConKey pREL_ST SLIT("StateAndInt64#")
462 alpha_tyvar [] [mkStatePrimTy alphaTy, int64PrimTy]
463 stateAndInt64PrimTyCon
465 stateAndWordPrimTyCon
466 = pcNonRecDataTyCon stateAndWordPrimTyConKey pREL_ST SLIT("StateAndWord#")
467 alpha_tyvar [stateAndWordPrimDataCon]
468 stateAndWordPrimDataCon
469 = pcDataCon stateAndWordPrimDataConKey pREL_ST SLIT("StateAndWord#")
470 alpha_tyvar [] [mkStatePrimTy alphaTy, wordPrimTy]
471 stateAndWordPrimTyCon
473 stateAndWord64PrimTyCon
474 = pcNonRecDataTyCon stateAndWord64PrimTyConKey pREL_ST SLIT("StateAndWord64#")
475 alpha_tyvar [stateAndWord64PrimDataCon]
476 stateAndWord64PrimDataCon
477 = pcDataCon stateAndWord64PrimDataConKey pREL_ST SLIT("StateAndWord64#")
478 alpha_tyvar [] [mkStatePrimTy alphaTy, word64PrimTy]
479 stateAndWord64PrimTyCon
481 stateAndAddrPrimTyCon
482 = pcNonRecDataTyCon stateAndAddrPrimTyConKey pREL_ST SLIT("StateAndAddr#")
483 alpha_tyvar [stateAndAddrPrimDataCon]
484 stateAndAddrPrimDataCon
485 = pcDataCon stateAndAddrPrimDataConKey pREL_ST SLIT("StateAndAddr#")
486 alpha_tyvar [] [mkStatePrimTy alphaTy, addrPrimTy]
487 stateAndAddrPrimTyCon
489 stateAndStablePtrPrimTyCon
490 = pcNonRecDataTyCon stateAndStablePtrPrimTyConKey pREL_FOREIGN SLIT("StateAndStablePtr#")
491 alpha_beta_tyvars [stateAndStablePtrPrimDataCon]
492 stateAndStablePtrPrimDataCon
493 = pcDataCon stateAndStablePtrPrimDataConKey pREL_FOREIGN SLIT("StateAndStablePtr#")
495 [mkStatePrimTy alphaTy, mkTyConApp stablePtrPrimTyCon [betaTy]]
496 stateAndStablePtrPrimTyCon
498 stateAndForeignObjPrimTyCon
499 = pcNonRecDataTyCon stateAndForeignObjPrimTyConKey pREL_IO_BASE SLIT("StateAndForeignObj#")
500 alpha_tyvar [stateAndForeignObjPrimDataCon]
501 stateAndForeignObjPrimDataCon
502 = pcDataCon stateAndForeignObjPrimDataConKey pREL_IO_BASE SLIT("StateAndForeignObj#")
504 [mkStatePrimTy alphaTy, mkTyConTy foreignObjPrimTyCon]
505 stateAndForeignObjPrimTyCon
507 stateAndFloatPrimTyCon
508 = pcNonRecDataTyCon stateAndFloatPrimTyConKey pREL_ST SLIT("StateAndFloat#")
509 alpha_tyvar [stateAndFloatPrimDataCon]
510 stateAndFloatPrimDataCon
511 = pcDataCon stateAndFloatPrimDataConKey pREL_ST SLIT("StateAndFloat#")
512 alpha_tyvar [] [mkStatePrimTy alphaTy, floatPrimTy]
513 stateAndFloatPrimTyCon
515 stateAndDoublePrimTyCon
516 = pcNonRecDataTyCon stateAndDoublePrimTyConKey pREL_ST SLIT("StateAndDouble#")
517 alpha_tyvar [stateAndDoublePrimDataCon]
518 stateAndDoublePrimDataCon
519 = pcDataCon stateAndDoublePrimDataConKey pREL_ST SLIT("StateAndDouble#")
520 alpha_tyvar [] [mkStatePrimTy alphaTy, doublePrimTy]
521 stateAndDoublePrimTyCon
525 stateAndArrayPrimTyCon
526 = pcNonRecDataTyCon stateAndArrayPrimTyConKey pREL_ARR SLIT("StateAndArray#")
527 alpha_beta_tyvars [stateAndArrayPrimDataCon]
528 stateAndArrayPrimDataCon
529 = pcDataCon stateAndArrayPrimDataConKey pREL_ARR SLIT("StateAndArray#")
530 alpha_beta_tyvars [] [mkStatePrimTy alphaTy, mkArrayPrimTy betaTy]
531 stateAndArrayPrimTyCon
533 stateAndMutableArrayPrimTyCon
534 = pcNonRecDataTyCon stateAndMutableArrayPrimTyConKey pREL_ARR SLIT("StateAndMutableArray#")
535 alpha_beta_tyvars [stateAndMutableArrayPrimDataCon]
536 stateAndMutableArrayPrimDataCon
537 = pcDataCon stateAndMutableArrayPrimDataConKey pREL_ARR SLIT("StateAndMutableArray#")
538 alpha_beta_tyvars [] [mkStatePrimTy alphaTy, mkMutableArrayPrimTy alphaTy betaTy]
539 stateAndMutableArrayPrimTyCon
541 stateAndByteArrayPrimTyCon
542 = pcNonRecDataTyCon stateAndByteArrayPrimTyConKey pREL_ARR SLIT("StateAndByteArray#")
543 alpha_tyvar [stateAndByteArrayPrimDataCon]
544 stateAndByteArrayPrimDataCon
545 = pcDataCon stateAndByteArrayPrimDataConKey pREL_ARR SLIT("StateAndByteArray#")
546 alpha_tyvar [] [mkStatePrimTy alphaTy, byteArrayPrimTy]
547 stateAndByteArrayPrimTyCon
549 stateAndMutableByteArrayPrimTyCon
550 = pcNonRecDataTyCon stateAndMutableByteArrayPrimTyConKey pREL_ARR SLIT("StateAndMutableByteArray#")
551 alpha_tyvar [stateAndMutableByteArrayPrimDataCon]
552 stateAndMutableByteArrayPrimDataCon
553 = pcDataCon stateAndMutableByteArrayPrimDataConKey pREL_ARR SLIT("StateAndMutableByteArray#")
554 alpha_tyvar [] [mkStatePrimTy alphaTy, mkTyConApp mutableByteArrayPrimTyCon alpha_ty]
555 stateAndMutableByteArrayPrimTyCon
557 stateAndSynchVarPrimTyCon
558 = pcNonRecDataTyCon stateAndSynchVarPrimTyConKey pREL_CONC SLIT("StateAndSynchVar#")
559 alpha_beta_tyvars [stateAndSynchVarPrimDataCon]
560 stateAndSynchVarPrimDataCon
561 = pcDataCon stateAndSynchVarPrimDataConKey pREL_CONC SLIT("StateAndSynchVar#")
562 alpha_beta_tyvars [] [mkStatePrimTy alphaTy, mkSynchVarPrimTy alphaTy betaTy]
563 stateAndSynchVarPrimTyCon
566 The ccall-desugaring mechanism uses this function to figure out how to
567 rebox the result. It's really a HACK, especially the part about
568 how many types to drop from \tr{tys_applied}.
571 getStatePairingConInfo
572 :: Type -- primitive type
573 -> (Id, -- state pair constructor for prim type
574 Type) -- type of state pair
576 getStatePairingConInfo prim_ty
577 = case (splitTyConApp_maybe prim_ty) of
578 Nothing -> panic "getStatePairingConInfo:1"
579 Just (prim_tycon, tys_applied) ->
581 (pair_con, pair_tycon, num_tys) = assoc "getStatePairingConInfo" tbl prim_tycon
582 pair_ty = mkTyConApp pair_tycon (realWorldTy : drop num_tys tys_applied)
587 (charPrimTyCon, (stateAndCharPrimDataCon, stateAndCharPrimTyCon, 0)),
588 (intPrimTyCon, (stateAndIntPrimDataCon, stateAndIntPrimTyCon, 0)),
589 (wordPrimTyCon, (stateAndWordPrimDataCon, stateAndWordPrimTyCon, 0)),
590 (int64PrimTyCon, (stateAndInt64PrimDataCon, stateAndInt64PrimTyCon, 0)),
591 (word64PrimTyCon, (stateAndWord64PrimDataCon, stateAndWord64PrimTyCon, 0)),
592 (addrPrimTyCon, (stateAndAddrPrimDataCon, stateAndAddrPrimTyCon, 0)),
593 (stablePtrPrimTyCon, (stateAndStablePtrPrimDataCon, stateAndStablePtrPrimTyCon, 0)),
594 (foreignObjPrimTyCon, (stateAndForeignObjPrimDataCon, stateAndForeignObjPrimTyCon, 0)),
595 (floatPrimTyCon, (stateAndFloatPrimDataCon, stateAndFloatPrimTyCon, 0)),
596 (doublePrimTyCon, (stateAndDoublePrimDataCon, stateAndDoublePrimTyCon, 0)),
597 (arrayPrimTyCon, (stateAndArrayPrimDataCon, stateAndArrayPrimTyCon, 0)),
598 (mutableArrayPrimTyCon, (stateAndMutableArrayPrimDataCon, stateAndMutableArrayPrimTyCon, 1)),
599 (byteArrayPrimTyCon, (stateAndByteArrayPrimDataCon, stateAndByteArrayPrimTyCon, 0)),
600 (mutableByteArrayPrimTyCon, (stateAndMutableByteArrayPrimDataCon, stateAndMutableByteArrayPrimTyCon, 1)),
601 (synchVarPrimTyCon, (stateAndSynchVarPrimDataCon, stateAndSynchVarPrimTyCon, 1))
602 -- (PtrPrimTyCon, (stateAndPtrPrimDataCon, stateAndPtrPrimTyCon, 0)),
607 %************************************************************************
609 \subsection[TysWiredIn-ext-type]{External types}
611 %************************************************************************
613 The compiler's foreign function interface supports the passing of a
614 restricted set of types as arguments and results (the restricting factor
618 isFFIArgumentTy :: Type -> Bool
620 (opt_GlasgowExts && isUnpointedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
621 case (splitAlgTyConApp_maybe ty) of
622 Just (tycon, _, _) -> (uniqueOf tycon) `elem` primArgTyConKeys
625 -- types that can be passed as arguments to "foreign" functions
627 = [ intTyConKey, int8TyConKey, int16TyConKey, int32TyConKey, int64TyConKey
628 , wordTyConKey, word8TyConKey, word16TyConKey, word32TyConKey, word64TyConKey
629 , floatTyConKey, doubleTyConKey
630 , addrTyConKey, charTyConKey, foreignObjTyConKey
631 , stablePtrTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey
634 -- types that can be passed from the outside world into Haskell.
635 -- excludes (mutable) byteArrays.
636 isFFIExternalTy :: Type -> Bool
638 (opt_GlasgowExts && isUnpointedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
639 case (splitAlgTyConApp_maybe ty) of
640 Just (tycon, _, _) ->
642 u_tycon = uniqueOf tycon
644 (u_tycon `elem` primArgTyConKeys) &&
645 not (u_tycon `elem` notLegalExternalTyCons)
649 isFFIResultTy :: Type -> Bool
651 not (isUnpointedType ty) &&
652 case (splitAlgTyConApp_maybe ty) of
653 Just (tycon, _, _) ->
655 u_tycon = uniqueOf tycon
657 (u_tycon == uniqueOf unitTyCon) ||
658 ((u_tycon `elem` primArgTyConKeys) &&
659 not (u_tycon `elem` notLegalExternalTyCons))
662 -- it's illegal to return foreign objects and (mutable)
663 -- bytearrays from a _ccall_ / foreign declaration
664 -- (or be passed them as arguments in foreign exported functions).
665 notLegalExternalTyCons =
666 [ foreignObjTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey ]
671 %************************************************************************
673 \subsection[TysWiredIn-ST]{The basic @_ST@ state-transformer type}
675 %************************************************************************
677 The only reason this is wired in is because we have to represent the
681 mkStateTransformerTy s a = mkTyConApp stTyCon [s, a]
683 stTyCon = pcNonRecNewTyCon stTyConKey pREL_ST SLIT("ST") alpha_beta_tyvars [stDataCon]
685 stDataCon = pcDataCon stDataConKey pREL_ST SLIT("ST")
686 alpha_beta_tyvars [] [ty] stTyCon
688 ty = mkFunTy (mkStatePrimTy alphaTy) (mkSTretTy alphaTy betaTy)
690 mkSTretTy alpha beta = mkTyConApp stRetTyCon [alpha,beta]
693 = pcNonRecDataTyCon stRetTyConKey pREL_ST SLIT("STret")
694 alpha_beta_tyvars [stRetDataCon]
696 = pcDataCon stRetDataConKey pREL_ST SLIT("STret")
697 alpha_beta_tyvars [] [mkStatePrimTy alphaTy, betaTy]
701 %************************************************************************
703 \subsection[TysWiredIn-Bool]{The @Bool@ type}
705 %************************************************************************
707 An ordinary enumeration type, but deeply wired in. There are no
708 magical operations on @Bool@ (just the regular Prelude code).
710 {\em BEGIN IDLE SPECULATION BY SIMON}
712 This is not the only way to encode @Bool@. A more obvious coding makes
713 @Bool@ just a boxed up version of @Bool#@, like this:
716 data Bool = MkBool Bool#
719 Unfortunately, this doesn't correspond to what the Report says @Bool@
720 looks like! Furthermore, we get slightly less efficient code (I
721 think) with this coding. @gtInt@ would look like this:
724 gtInt :: Int -> Int -> Bool
725 gtInt x y = case x of I# x# ->
727 case (gtIntPrim x# y#) of
731 Notice that the result of the @gtIntPrim@ comparison has to be turned
732 into an integer (here called @b#@), and returned in a @MkBool@ box.
734 The @if@ expression would compile to this:
737 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
740 I think this code is a little less efficient than the previous code,
741 but I'm not certain. At all events, corresponding with the Report is
742 important. The interesting thing is that the language is expressive
743 enough to describe more than one alternative; and that a type doesn't
744 necessarily need to be a straightforwardly boxed version of its
745 primitive counterpart.
747 {\em END IDLE SPECULATION BY SIMON}
750 boolTy = mkTyConTy boolTyCon
752 boolTyCon = pcNonRecDataTyCon boolTyConKey pREL_BASE SLIT("Bool") [] [falseDataCon, trueDataCon]
754 falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon
755 trueDataCon = pcDataCon trueDataConKey pREL_BASE SLIT("True") [] [] [] boolTyCon
758 %************************************************************************
760 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
762 %************************************************************************
764 Special syntax, deeply wired in, but otherwise an ordinary algebraic
767 data [] a = [] | a : (List a)
769 data (,) a b = (,,) a b
774 mkListTy :: GenType t -> GenType t
775 mkListTy ty = mkTyConApp listTyCon [ty]
777 alphaListTy = mkSigmaTy alpha_tyvar [] (mkTyConApp listTyCon alpha_ty)
779 listTyCon = pcRecDataTyCon listTyConKey pREL_BASE SLIT("[]")
780 alpha_tyvar [nilDataCon, consDataCon]
782 nilDataCon = pcDataCon nilDataConKey pREL_BASE SLIT("[]") alpha_tyvar [] [] listTyCon
783 consDataCon = pcDataCon consDataConKey pREL_BASE SLIT(":")
784 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
785 -- Interesting: polymorphic recursion would help here.
786 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
787 -- gets the over-specific type (Type -> Type)
790 %************************************************************************
792 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
794 %************************************************************************
796 The tuple types are definitely magic, because they form an infinite
801 They have a special family of type constructors, of type @TyCon@
802 These contain the tycon arity, but don't require a Unique.
805 They have a special family of constructors, of type
806 @Id@. Again these contain their arity but don't need a Unique.
809 There should be a magic way of generating the info tables and
810 entry code for all tuples.
812 But at the moment we just compile a Haskell source
813 file\srcloc{lib/prelude/...} containing declarations like:
816 data Tuple2 a b = Tup2 a b
817 data Tuple3 a b c = Tup3 a b c
818 data Tuple4 a b c d = Tup4 a b c d
821 The print-names associated with the magic @Id@s for tuple constructors
822 ``just happen'' to be the same as those generated by these
826 The instance environment should have a magic way to know
827 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
828 so on. \ToDo{Not implemented yet.}
831 There should also be a way to generate the appropriate code for each
832 of these instances, but (like the info tables and entry code) it is
833 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
837 mkTupleTy :: Int -> [GenType t] -> GenType t
839 mkTupleTy arity tys = mkTyConApp (tupleTyCon arity) tys
841 unitTy = mkTupleTy 0 []
844 %************************************************************************
846 \subsection[TysWiredIn-_Lift]{@_Lift@ type: to support array indexing}
848 %************************************************************************
850 Again, deeply turgid: \tr{data _Lift a = _Lift a}.
853 mkLiftTy ty = mkTyConApp liftTyCon [ty]
857 = mkSigmaTy tvs theta (mkTyConApp liftTyCon [tau])
859 (tvs, theta, tau) = splitSigmaTy ty
862 = case (splitAlgTyConApp_maybeExpandingDicts tau) of
863 Just (tycon, tys, _) -> tycon == liftTyCon
866 (tvs, theta, tau) = splitSigmaTy ty
870 alphaLiftTy = mkSigmaTy alpha_tyvar [] (mkTyConApp liftTyCon alpha_ty)
873 = pcNonRecDataTyCon liftTyConKey pREL_BASE SLIT("Lift") alpha_tyvar [liftDataCon]
876 = pcDataCon liftDataConKey pREL_BASE SLIT("Lift")
877 alpha_tyvar [] alpha_ty liftTyCon
879 bottom = panic "liftDataCon:State# _RealWorld"