2 % (c) The GRASP Project, Glasgow University, 1994-1998
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.''
57 tupleTyCon, tupleCon, unitTyCon, unitDataCon, pairTyCon, pairDataCon,
61 unboxedTupleTyCon, unboxedTupleCon,
62 unboxedPairTyCon, unboxedPairDataCon,
78 isFFIArgumentTy, -- :: Type -> Bool
79 isFFIResultTy, -- :: Type -> Bool
80 isFFIExternalTy, -- :: Type -> Bool
81 isAddrTy, -- :: Type -> Bool
85 #include "HsVersions.h"
87 import {-# SOURCE #-} MkId( mkDataConId )
94 import Constants ( mAX_TUPLE_SIZE )
95 import Name ( Module, varOcc, mkWiredInTyConName, mkWiredInIdName )
96 import DataCon ( DataCon, mkDataCon )
97 import Var ( TyVar, tyVarKind )
98 import TyCon ( TyCon, mkAlgTyCon, mkSynTyCon, mkTupleTyCon )
99 import BasicTypes ( Arity, NewOrData(..),
100 RecFlag(..), StrictnessMark(..) )
101 import Type ( Type, mkTyConTy, mkTyConApp, mkSigmaTy, mkTyVarTys,
102 mkArrowKinds, boxedTypeKind, unboxedTypeKind,
103 mkFunTy, mkFunTys, isUnLiftedType,
104 splitTyConApp_maybe, splitAlgTyConApp_maybe,
106 import PrimRep ( PrimRep(..) )
108 import CmdLineOpts ( opt_GlasgowExts )
109 import Util ( assoc )
110 import Panic ( panic )
113 alpha_tyvar = [alphaTyVar]
115 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
117 pcRecDataTyCon, pcNonRecDataTyCon, pcNonRecNewTyCon
118 :: Unique{-TyConKey-} -> Module -> FAST_STRING
119 -> [TyVar] -> [DataCon] -> TyCon
121 pcRecDataTyCon = pcTyCon DataType Recursive
122 pcNonRecDataTyCon = pcTyCon DataType NonRecursive
123 pcNonRecNewTyCon = pcTyCon NewType NonRecursive
125 pcTyCon new_or_data is_rec key mod str tyvars cons
128 tycon = mkAlgTyCon name kind
133 Nothing -- Not a dictionary
137 name = mkWiredInTyConName key mod str tycon
138 kind = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind
140 pcSynTyCon key mod str kind arity tyvars expansion
143 tycon = mkSynTyCon name kind arity tyvars expansion
144 name = mkWiredInTyConName key mod str tycon
146 pcDataCon :: Unique{-DataConKey-} -> Module -> FAST_STRING
147 -> [TyVar] -> ThetaType -> [TauType] -> TyCon -> DataCon
148 pcDataCon key mod str tyvars context arg_tys tycon
151 data_con = mkDataCon name
152 [ NotMarkedStrict | a <- arg_tys ]
153 [ {- no labelled fields -} ]
154 tyvars context [] [] arg_tys tycon id
155 name = mkWiredInIdName key mod (varOcc str) id
156 id = mkDataConId data_con
159 %************************************************************************
161 \subsection[TysWiredIn-tuples]{The tuple types}
163 %************************************************************************
166 tupleTyCon :: Arity -> TyCon
167 tupleTyCon i | i > mAX_TUPLE_SIZE = fst (mk_tuple i) -- Build one specially
168 | otherwise = tupleTyConArr!i
170 tupleCon :: Arity -> DataCon
171 tupleCon i | i > mAX_TUPLE_SIZE = snd (mk_tuple i) -- Build one specially
172 | otherwise = tupleConArr!i
174 tupleTyCons :: [TyCon]
175 tupleTyCons = elems tupleTyConArr
177 tupleTyConArr :: Array Int TyCon
178 tupleTyConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map fst tuples)
180 tupleConArr :: Array Int DataCon
181 tupleConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map snd tuples)
183 tuples :: [(TyCon,DataCon)]
184 tuples = [mk_tuple i | i <- [0..mAX_TUPLE_SIZE]]
186 mk_tuple :: Int -> (TyCon,DataCon)
187 mk_tuple arity = (tycon, tuple_con)
189 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con True
190 tc_name = mkWiredInTyConName tc_uniq mod_name name_str tycon
191 tc_kind = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind
193 tuple_con = pcDataCon dc_uniq mod_name name_str tyvars [] tyvar_tys tycon
194 tyvars = take arity alphaTyVars
195 tyvar_tys = mkTyVarTys tyvars
196 (mod_name, name_str) = mkTupNameStr arity
197 tc_uniq = mkTupleTyConUnique arity
198 dc_uniq = mkTupleDataConUnique arity
200 unitTyCon = tupleTyCon 0
201 pairTyCon = tupleTyCon 2
203 unitDataCon = tupleCon 0
204 pairDataCon = tupleCon 2
207 %************************************************************************
209 \subsection[TysWiredIn-ubx-tuples]{Unboxed Tuple Types}
211 %************************************************************************
214 unboxedTupleTyCon :: Arity -> TyCon
215 unboxedTupleTyCon i | i > mAX_TUPLE_SIZE = fst (mk_unboxed_tuple i)
216 | otherwise = unboxedTupleTyConArr!i
218 unboxedTupleCon :: Arity -> DataCon
219 unboxedTupleCon i | i > mAX_TUPLE_SIZE = snd (mk_unboxed_tuple i)
220 | otherwise = unboxedTupleConArr!i
222 unboxedTupleTyConArr :: Array Int TyCon
223 unboxedTupleTyConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map fst ubx_tuples)
225 unboxedTupleConArr :: Array Int DataCon
226 unboxedTupleConArr = array (0,mAX_TUPLE_SIZE) ([0..] `zip` map snd ubx_tuples)
228 ubx_tuples :: [(TyCon,DataCon)]
229 ubx_tuples = [mk_unboxed_tuple i | i <- [0..mAX_TUPLE_SIZE]]
231 mk_unboxed_tuple :: Int -> (TyCon,DataCon)
232 mk_unboxed_tuple arity = (tycon, tuple_con)
234 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con False
235 tc_name = mkWiredInTyConName tc_uniq mod_name name_str tycon
236 tc_kind = mkArrowKinds (map tyVarKind tyvars) unboxedTypeKind
238 tuple_con = pcDataCon dc_uniq mod_name name_str tyvars [] tyvar_tys tycon
239 tyvars = take arity openAlphaTyVars
240 tyvar_tys = mkTyVarTys tyvars
241 (mod_name, name_str) = mkUbxTupNameStr arity
242 tc_uniq = mkUbxTupleTyConUnique arity
243 dc_uniq = mkUbxTupleDataConUnique arity
245 unboxedPairTyCon = unboxedTupleTyCon 2
246 unboxedPairDataCon = unboxedTupleCon 2
249 %************************************************************************
251 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
253 %************************************************************************
256 -- The Void type is represented as a data type with no constructors
257 -- It's a built in type (i.e. there's no way to define it in Haskell;
258 -- the nearest would be
260 -- data Void = -- No constructors!
262 -- ) It's boxed; there is only one value of this
263 -- type, namely "void", whose semantics is just bottom.
265 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
272 charTy = mkTyConTy charTyCon
274 charTyCon = pcNonRecDataTyCon charTyConKey pREL_BASE SLIT("Char") [] [charDataCon]
275 charDataCon = pcDataCon charDataConKey pREL_BASE SLIT("C#") [] [] [charPrimTy] charTyCon
277 stringTy = mkListTy charTy -- convenience only
281 intTy = mkTyConTy intTyCon
283 intTyCon = pcNonRecDataTyCon intTyConKey pREL_BASE SLIT("Int") [] [intDataCon]
284 intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon
286 isIntTy :: Type -> Bool
288 = case (splitAlgTyConApp_maybe ty) of
289 Just (tycon, [], _) -> getUnique tycon == intTyConKey
292 inIntRange :: Integer -> Bool -- Tells if an integer lies in the legal range of Ints
293 inIntRange i = (min_int <= i) && (i <= max_int)
295 max_int, min_int :: Integer
296 max_int = toInteger maxInt
297 min_int = toInteger minInt
299 int8TyCon = pcNonRecDataTyCon int8TyConKey iNT SLIT("Int8") [] [int8DataCon]
301 int8DataCon = pcDataCon int8DataConKey iNT SLIT("I8#") [] [] [intPrimTy] int8TyCon
303 int16TyCon = pcNonRecDataTyCon int16TyConKey iNT SLIT("Int16") [] [int16DataCon]
305 int16DataCon = pcDataCon int16DataConKey iNT SLIT("I16#") [] [] [intPrimTy] int16TyCon
307 int32TyCon = pcNonRecDataTyCon int32TyConKey iNT SLIT("Int32") [] [int32DataCon]
309 int32DataCon = pcDataCon int32DataConKey iNT SLIT("I32#") [] [] [intPrimTy] int32TyCon
311 int64TyCon = pcNonRecDataTyCon int64TyConKey pREL_ADDR SLIT("Int64") [] [int64DataCon]
313 int64DataCon = pcDataCon int64DataConKey pREL_ADDR SLIT("I64#") [] [] [int64PrimTy] int64TyCon
318 wordTy = mkTyConTy wordTyCon
320 wordTyCon = pcNonRecDataTyCon wordTyConKey pREL_ADDR SLIT("Word") [] [wordDataCon]
321 wordDataCon = pcDataCon wordDataConKey pREL_ADDR SLIT("W#") [] [] [wordPrimTy] wordTyCon
323 word8TyCon = pcNonRecDataTyCon word8TyConKey wORD SLIT("Word8") [] [word8DataCon]
325 word8DataCon = pcDataCon word8DataConKey wORD SLIT("W8#") [] [] [wordPrimTy] word8TyCon
327 word16TyCon = pcNonRecDataTyCon word16TyConKey wORD SLIT("Word16") [] [word16DataCon]
329 word16DataCon = pcDataCon word16DataConKey wORD SLIT("W16#") [] [] [wordPrimTy] word16TyCon
331 word32TyCon = pcNonRecDataTyCon word32TyConKey wORD SLIT("Word32") [] [word32DataCon]
333 word32DataCon = pcDataCon word32DataConKey wORD SLIT("W32#") [] [] [wordPrimTy] word32TyCon
335 word64TyCon = pcNonRecDataTyCon word64TyConKey pREL_ADDR SLIT("Word64") [] [word64DataCon]
337 word64DataCon = pcDataCon word64DataConKey pREL_ADDR SLIT("W64#") [] [] [word64PrimTy] word64TyCon
341 addrTy = mkTyConTy addrTyCon
343 addrTyCon = pcNonRecDataTyCon addrTyConKey pREL_ADDR SLIT("Addr") [] [addrDataCon]
344 addrDataCon = pcDataCon addrDataConKey pREL_ADDR SLIT("A#") [] [] [addrPrimTy] addrTyCon
346 isAddrTy :: Type -> Bool
348 = case (splitAlgTyConApp_maybe ty) of
349 Just (tycon, [], _) -> getUnique tycon == addrTyConKey
355 floatTy = mkTyConTy floatTyCon
357 floatTyCon = pcNonRecDataTyCon floatTyConKey pREL_BASE SLIT("Float") [] [floatDataCon]
358 floatDataCon = pcDataCon floatDataConKey pREL_BASE SLIT("F#") [] [] [floatPrimTy] floatTyCon
360 isFloatTy :: Type -> Bool
362 = case (splitAlgTyConApp_maybe ty) of
363 Just (tycon, [], _) -> getUnique tycon == floatTyConKey
369 doubleTy = mkTyConTy doubleTyCon
371 isDoubleTy :: Type -> Bool
373 = case (splitAlgTyConApp_maybe ty) of
374 Just (tycon, [], _) -> getUnique tycon == doubleTyConKey
377 doubleTyCon = pcNonRecDataTyCon doubleTyConKey pREL_BASE SLIT("Double") [] [doubleDataCon]
378 doubleDataCon = pcDataCon doubleDataConKey pREL_BASE SLIT("D#") [] [] [doublePrimTy] doubleTyCon
383 = pcNonRecDataTyCon stablePtrTyConKey pREL_STABLE SLIT("StablePtr")
384 alpha_tyvar [stablePtrDataCon]
387 = pcDataCon stablePtrDataConKey pREL_STABLE SLIT("StablePtr")
388 alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
393 = pcNonRecDataTyCon foreignObjTyConKey pREL_IO_BASE SLIT("ForeignObj")
394 [] [foreignObjDataCon]
397 = pcDataCon foreignObjDataConKey pREL_IO_BASE SLIT("ForeignObj")
398 [] [] [foreignObjPrimTy] foreignObjTyCon
401 %************************************************************************
403 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
405 %************************************************************************
407 @Integer@ and its pals are not really primitive. @Integer@ itself, first:
410 integerTy = mkTyConTy integerTyCon
412 integerTyCon = pcNonRecDataTyCon integerTyConKey pREL_BASE SLIT("Integer") [] [integerDataCon]
414 integerDataCon = pcDataCon integerDataConKey pREL_BASE SLIT("J#")
415 [] [] [intPrimTy, intPrimTy, byteArrayPrimTy] integerTyCon
417 isIntegerTy :: Type -> Bool
419 = case (splitAlgTyConApp_maybe ty) of
420 Just (tycon, [], _) -> getUnique tycon == integerTyConKey
425 %************************************************************************
427 \subsection[TysWiredIn-ext-type]{External types}
429 %************************************************************************
431 The compiler's foreign function interface supports the passing of a
432 restricted set of types as arguments and results (the restricting factor
436 isFFIArgumentTy :: Type -> Bool
438 (opt_GlasgowExts && isUnLiftedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
439 case (splitAlgTyConApp_maybe ty) of
440 Just (tycon, _, _) -> (getUnique tycon) `elem` primArgTyConKeys
443 -- types that can be passed as arguments to "foreign" functions
445 = [ intTyConKey, int8TyConKey, int16TyConKey, int32TyConKey, int64TyConKey
446 , wordTyConKey, word8TyConKey, word16TyConKey, word32TyConKey, word64TyConKey
447 , floatTyConKey, doubleTyConKey
448 , addrTyConKey, charTyConKey, foreignObjTyConKey
450 , byteArrayTyConKey, mutableByteArrayTyConKey
453 -- types that can be passed from the outside world into Haskell.
454 -- excludes (mutable) byteArrays.
455 isFFIExternalTy :: Type -> Bool
457 (opt_GlasgowExts && isUnLiftedType ty) || --leave out for now: maybeToBool (maybeBoxedPrimType ty))) ||
458 case (splitAlgTyConApp_maybe ty) of
459 Just (tycon, _, _) ->
461 u_tycon = getUnique tycon
463 (u_tycon `elem` primArgTyConKeys) &&
464 not (u_tycon `elem` notLegalExternalTyCons)
468 isFFIResultTy :: Type -> Bool
470 not (isUnLiftedType ty) &&
471 case (splitAlgTyConApp_maybe ty) of
472 Just (tycon, _, _) ->
474 u_tycon = getUnique tycon
476 (u_tycon == getUnique unitTyCon) ||
477 ((u_tycon `elem` primArgTyConKeys) &&
478 not (u_tycon `elem` notLegalExternalTyCons))
481 -- it's illegal to return foreign objects and (mutable)
482 -- bytearrays from a _ccall_ / foreign declaration
483 -- (or be passed them as arguments in foreign exported functions).
484 notLegalExternalTyCons =
485 [ foreignObjTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey ]
489 %************************************************************************
491 \subsection[TysWiredIn-Bool]{The @Bool@ type}
493 %************************************************************************
495 An ordinary enumeration type, but deeply wired in. There are no
496 magical operations on @Bool@ (just the regular Prelude code).
498 {\em BEGIN IDLE SPECULATION BY SIMON}
500 This is not the only way to encode @Bool@. A more obvious coding makes
501 @Bool@ just a boxed up version of @Bool#@, like this:
504 data Bool = MkBool Bool#
507 Unfortunately, this doesn't correspond to what the Report says @Bool@
508 looks like! Furthermore, we get slightly less efficient code (I
509 think) with this coding. @gtInt@ would look like this:
512 gtInt :: Int -> Int -> Bool
513 gtInt x y = case x of I# x# ->
515 case (gtIntPrim x# y#) of
519 Notice that the result of the @gtIntPrim@ comparison has to be turned
520 into an integer (here called @b#@), and returned in a @MkBool@ box.
522 The @if@ expression would compile to this:
525 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
528 I think this code is a little less efficient than the previous code,
529 but I'm not certain. At all events, corresponding with the Report is
530 important. The interesting thing is that the language is expressive
531 enough to describe more than one alternative; and that a type doesn't
532 necessarily need to be a straightforwardly boxed version of its
533 primitive counterpart.
535 {\em END IDLE SPECULATION BY SIMON}
538 boolTy = mkTyConTy boolTyCon
540 boolTyCon = pcTyCon EnumType NonRecursive boolTyConKey
541 pREL_BASE SLIT("Bool") [] [falseDataCon, trueDataCon]
543 falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon
544 trueDataCon = pcDataCon trueDataConKey pREL_BASE SLIT("True") [] [] [] boolTyCon
547 %************************************************************************
549 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
551 %************************************************************************
553 Special syntax, deeply wired in, but otherwise an ordinary algebraic
556 data [] a = [] | a : (List a)
558 data (,) a b = (,,) a b
563 mkListTy :: Type -> Type
564 mkListTy ty = mkTyConApp listTyCon [ty]
566 alphaListTy = mkSigmaTy alpha_tyvar [] (mkTyConApp listTyCon alpha_ty)
568 listTyCon = pcRecDataTyCon listTyConKey pREL_BASE SLIT("[]")
569 alpha_tyvar [nilDataCon, consDataCon]
571 nilDataCon = pcDataCon nilDataConKey pREL_BASE SLIT("[]") alpha_tyvar [] [] listTyCon
572 consDataCon = pcDataCon consDataConKey pREL_BASE SLIT(":")
573 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
574 -- Interesting: polymorphic recursion would help here.
575 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
576 -- gets the over-specific type (Type -> Type)
579 %************************************************************************
581 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
583 %************************************************************************
585 The tuple types are definitely magic, because they form an infinite
590 They have a special family of type constructors, of type @TyCon@
591 These contain the tycon arity, but don't require a Unique.
594 They have a special family of constructors, of type
595 @Id@. Again these contain their arity but don't need a Unique.
598 There should be a magic way of generating the info tables and
599 entry code for all tuples.
601 But at the moment we just compile a Haskell source
602 file\srcloc{lib/prelude/...} containing declarations like:
605 data Tuple2 a b = Tup2 a b
606 data Tuple3 a b c = Tup3 a b c
607 data Tuple4 a b c d = Tup4 a b c d
610 The print-names associated with the magic @Id@s for tuple constructors
611 ``just happen'' to be the same as those generated by these
615 The instance environment should have a magic way to know
616 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
617 so on. \ToDo{Not implemented yet.}
620 There should also be a way to generate the appropriate code for each
621 of these instances, but (like the info tables and entry code) it is
622 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
626 mkTupleTy :: Int -> [Type] -> Type
627 mkTupleTy arity tys = mkTyConApp (tupleTyCon arity) tys
629 mkUnboxedTupleTy :: Int -> [Type] -> Type
630 mkUnboxedTupleTy arity tys = mkTyConApp (unboxedTupleTyCon arity) tys
632 unitTy = mkTupleTy 0 []