%
-% (c) The AQUA Project, Glasgow University, 1994-1996
+% (c) The AQUA Project, Glasgow University, 1994-1998
%
\section[TysPrim]{Wired-in knowledge about primitive types}
types and operations.''
\begin{code}
-#include "HsVersions.h"
+module TysPrim(
+ alphaTyVars, betaTyVars, alphaTyVar, betaTyVar, gammaTyVar, deltaTyVar,
+ alphaTy, betaTy, gammaTy, deltaTy,
+ openAlphaTy, openAlphaTyVar, openAlphaTyVars,
+
+ primTyCons,
+
+ charPrimTyCon, charPrimTy,
+ intPrimTyCon, intPrimTy,
+ wordPrimTyCon, wordPrimTy,
+ addrPrimTyCon, addrPrimTy,
+ floatPrimTyCon, floatPrimTy,
+ doublePrimTyCon, doublePrimTy,
+
+ statePrimTyCon, mkStatePrimTy,
+ realWorldTyCon, realWorldTy, realWorldStatePrimTy,
+
+ arrayPrimTyCon, mkArrayPrimTy,
+ byteArrayPrimTyCon, byteArrayPrimTy,
+ mutableArrayPrimTyCon, mkMutableArrayPrimTy,
+ mutableByteArrayPrimTyCon, mkMutableByteArrayPrimTy,
+ mutVarPrimTyCon, mkMutVarPrimTy,
+
+ mVarPrimTyCon, mkMVarPrimTy,
+ stablePtrPrimTyCon, mkStablePtrPrimTy,
+ stableNamePrimTyCon, mkStableNamePrimTy,
+ bcoPrimTyCon, bcoPrimTy,
+ weakPrimTyCon, mkWeakPrimTy,
+ foreignObjPrimTyCon, foreignObjPrimTy,
+ threadIdPrimTyCon, threadIdPrimTy,
+
+ int64PrimTyCon, int64PrimTy,
+ word64PrimTyCon, word64PrimTy,
+
+ primRepTyCon
+ ) where
-module TysPrim where
+#include "HsVersions.h"
-import Ubiq
+import Var ( TyVar, mkSysTyVar )
+import Name ( Name )
+import PrimRep ( PrimRep(..) )
+import TyCon ( TyCon, ArgVrcs, mkPrimTyCon, mkLiftedPrimTyCon )
+import Type ( mkTyConApp, mkTyConTy, mkTyVarTys, mkTyVarTy,
+ unliftedTypeKind, liftedTypeKind, openTypeKind, mkArrowKinds
+ )
+import Unique ( mkAlphaTyVarUnique )
+import PrelNames
+import Outputable
+\end{code}
-import Kind ( mkUnboxedTypeKind, mkBoxedTypeKind )
-import NameTypes ( mkPreludeCoreName, FullName )
-import PrelMods ( pRELUDE_BUILTIN )
-import PrimRep ( PrimRep(..) ) -- getPrimRepInfo uses PrimRep repn
-import TyCon ( mkPrimTyCon, mkDataTyCon, NewOrData(..) )
-import TyVar ( GenTyVar(..), alphaTyVars )
-import Type ( applyTyCon, mkTyVarTys )
-import Usage ( usageOmega )
-import Unique
+%************************************************************************
+%* *
+\subsection{Primitive type constructors}
+%* *
+%************************************************************************
+\begin{code}
+primTyCons :: [TyCon]
+primTyCons
+ = [ addrPrimTyCon
+ , arrayPrimTyCon
+ , byteArrayPrimTyCon
+ , charPrimTyCon
+ , doublePrimTyCon
+ , floatPrimTyCon
+ , intPrimTyCon
+ , int64PrimTyCon
+ , foreignObjPrimTyCon
+ , bcoPrimTyCon
+ , weakPrimTyCon
+ , mutableArrayPrimTyCon
+ , mutableByteArrayPrimTyCon
+ , mVarPrimTyCon
+ , mutVarPrimTyCon
+ , realWorldTyCon
+ , stablePtrPrimTyCon
+ , stableNamePrimTyCon
+ , statePrimTyCon
+ , threadIdPrimTyCon
+ , wordPrimTyCon
+ , word64PrimTyCon
+ ]
\end{code}
+
+%************************************************************************
+%* *
+\subsection{Support code}
+%* *
+%************************************************************************
+
\begin{code}
+alphaTyVars :: [TyVar]
+alphaTyVars = [ mkSysTyVar u liftedTypeKind
+ | u <- map mkAlphaTyVarUnique [2..] ]
+
+betaTyVars = tail alphaTyVars
+
+alphaTyVar, betaTyVar, gammaTyVar :: TyVar
+(alphaTyVar:betaTyVar:gammaTyVar:deltaTyVar:_) = alphaTyVars
+
alphaTys = mkTyVarTys alphaTyVars
(alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
+
+ -- openAlphaTyVar is prepared to be instantiated
+ -- to a lifted or unlifted type variable. It's used for the
+ -- result type for "error", so that we can have (error Int# "Help")
+openAlphaTyVar :: TyVar
+openAlphaTyVar = mkSysTyVar (mkAlphaTyVarUnique 1) openTypeKind
+
+openAlphaTyVars :: [TyVar]
+openAlphaTyVars = [ mkSysTyVar u openTypeKind
+ | u <- map mkAlphaTyVarUnique [2..] ]
+
+openAlphaTy = mkTyVarTy openAlphaTyVar
+
+vrcPos,vrcZero :: (Bool,Bool)
+vrcPos = (True,False)
+vrcZero = (False,False)
+
+vrcsP,vrcsZ,vrcsZP :: ArgVrcs
+vrcsP = [vrcPos]
+vrcsZ = [vrcZero]
+vrcsZP = [vrcZero,vrcPos]
\end{code}
+
%************************************************************************
%* *
\subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
\begin{code}
-- only used herein
-pcPrimTyCon :: Unique{-TyConKey-} -> FAST_STRING -> Int -> ([PrimRep] -> PrimRep) -> TyCon
-pcPrimTyCon key name arity{-UNUSED-} kind_fn{-UNUSED-}
- = mkPrimTyCon key full_name mkUnboxedTypeKind
+pcPrimTyCon :: Name -> ArgVrcs -> PrimRep -> TyCon
+pcPrimTyCon name arg_vrcs rep
+ = mkPrimTyCon name kind arity arg_vrcs rep
+ where
+ arity = length arg_vrcs
+ kind = mkArrowKinds (take arity (repeat liftedTypeKind)) result_kind
+ result_kind = unliftedTypeKind -- all primitive types are unlifted
+
+pcPrimTyCon0 :: Name -> PrimRep -> TyCon
+pcPrimTyCon0 name rep
+ = mkPrimTyCon name result_kind 0 [] rep
where
- full_name = mkPreludeCoreName pRELUDE_BUILTIN name
+ result_kind = unliftedTypeKind -- all primitive types are unlifted
+
+charPrimTy = mkTyConTy charPrimTyCon
+charPrimTyCon = pcPrimTyCon0 charPrimTyConName CharRep
+intPrimTy = mkTyConTy intPrimTyCon
+intPrimTyCon = pcPrimTyCon0 intPrimTyConName IntRep
-charPrimTy = applyTyCon charPrimTyCon []
-charPrimTyCon = pcPrimTyCon charPrimTyConKey SLIT("Char#") 0 (\ [] -> CharRep)
+int64PrimTy = mkTyConTy int64PrimTyCon
+int64PrimTyCon = pcPrimTyCon0 int64PrimTyConName Int64Rep
-intPrimTy = applyTyCon intPrimTyCon []
-intPrimTyCon = pcPrimTyCon intPrimTyConKey SLIT("Int#") 0 (\ [] -> IntRep)
+wordPrimTy = mkTyConTy wordPrimTyCon
+wordPrimTyCon = pcPrimTyCon0 wordPrimTyConName WordRep
-wordPrimTy = applyTyCon wordPrimTyCon []
-wordPrimTyCon = pcPrimTyCon wordPrimTyConKey SLIT("Word#") 0 (\ [] -> WordRep)
+word64PrimTy = mkTyConTy word64PrimTyCon
+word64PrimTyCon = pcPrimTyCon0 word64PrimTyConName Word64Rep
-addrPrimTy = applyTyCon addrPrimTyCon []
-addrPrimTyCon = pcPrimTyCon addrPrimTyConKey SLIT("Addr#") 0 (\ [] -> AddrRep)
+addrPrimTy = mkTyConTy addrPrimTyCon
+addrPrimTyCon = pcPrimTyCon0 addrPrimTyConName AddrRep
-floatPrimTy = applyTyCon floatPrimTyCon []
-floatPrimTyCon = pcPrimTyCon floatPrimTyConKey SLIT("Float#") 0 (\ [] -> FloatRep)
+floatPrimTy = mkTyConTy floatPrimTyCon
+floatPrimTyCon = pcPrimTyCon0 floatPrimTyConName FloatRep
-doublePrimTy = applyTyCon doublePrimTyCon []
-doublePrimTyCon = pcPrimTyCon doublePrimTyConKey SLIT("Double#") 0 (\ [] -> DoubleRep)
+doublePrimTy = mkTyConTy doublePrimTyCon
+doublePrimTyCon = pcPrimTyCon0 doublePrimTyConName DoubleRep
\end{code}
-@PrimitiveKinds@ are used in @PrimitiveOps@, for which we often need
-to reconstruct various type information. (It's slightly more
-convenient/efficient to make type info from kinds, than kinds [etc.]
-from type info.)
+
+%************************************************************************
+%* *
+\subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
+%* *
+%************************************************************************
+
+State# is the primitive, unlifted type of states. It has one type parameter,
+thus
+ State# RealWorld
+or
+ State# s
+
+where s is a type variable. The only purpose of the type parameter is to
+keep different state threads separate. It is represented by nothing at all.
\begin{code}
-getPrimRepInfo ::
- PrimRep -> (String, -- tag string
- Type, TyCon) -- prim type and tycon
+mkStatePrimTy ty = mkTyConApp statePrimTyCon [ty]
+statePrimTyCon = pcPrimTyCon statePrimTyConName vrcsZ VoidRep
+\end{code}
-getPrimRepInfo CharRep = ("Char", charPrimTy, charPrimTyCon)
-getPrimRepInfo IntRep = ("Int", intPrimTy, intPrimTyCon)
-getPrimRepInfo WordRep = ("Word", wordPrimTy, wordPrimTyCon)
-getPrimRepInfo AddrRep = ("Addr", addrPrimTy, addrPrimTyCon)
-getPrimRepInfo FloatRep = ("Float", floatPrimTy, floatPrimTyCon)
-getPrimRepInfo DoubleRep = ("Double", doublePrimTy, doublePrimTyCon)
+RealWorld is deeply magical. It is *primitive*, but it is not
+*unlifted* (hence PrimPtrRep). We never manipulate values of type
+RealWorld; it's only used in the type system, to parameterise State#.
+
+\begin{code}
+realWorldTyCon = mkLiftedPrimTyCon realWorldTyConName liftedTypeKind 0 [] PrimPtrRep
+realWorldTy = mkTyConTy realWorldTyCon
+realWorldStatePrimTy = mkStatePrimTy realWorldTy -- State# RealWorld
\end{code}
+Note: the ``state-pairing'' types are not truly primitive, so they are
+defined in \tr{TysWiredIn.lhs}, not here.
+
+
%************************************************************************
%* *
-\subsection[TysPrim-void]{The @Void#@ type}
+\subsection[TysPrim-arrays]{The primitive array types}
%* *
%************************************************************************
-Very similar to the @State#@ type.
\begin{code}
-voidPrimTy = applyTyCon voidPrimTyCon []
- where
- voidPrimTyCon = pcPrimTyCon voidPrimTyConKey SLIT("Void#") 0
- (\ [] -> VoidRep)
+arrayPrimTyCon = pcPrimTyCon arrayPrimTyConName vrcsP ArrayRep
+mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConName vrcsZP ArrayRep
+mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConName vrcsZ ByteArrayRep
+byteArrayPrimTyCon = pcPrimTyCon0 byteArrayPrimTyConName ByteArrayRep
+
+mkArrayPrimTy elt = mkTyConApp arrayPrimTyCon [elt]
+byteArrayPrimTy = mkTyConTy byteArrayPrimTyCon
+mkMutableArrayPrimTy s elt = mkTyConApp mutableArrayPrimTyCon [s, elt]
+mkMutableByteArrayPrimTy s = mkTyConApp mutableByteArrayPrimTyCon [s]
\end{code}
%************************************************************************
%* *
-\subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
+\subsection[TysPrim-mut-var]{The mutable variable type}
%* *
%************************************************************************
\begin{code}
-mkStatePrimTy ty = applyTyCon statePrimTyCon [ty]
-statePrimTyCon = pcPrimTyCon statePrimTyConKey SLIT("State#") 1
- (\ [s_kind] -> VoidRep)
+mutVarPrimTyCon = pcPrimTyCon mutVarPrimTyConName vrcsZP PrimPtrRep
+
+mkMutVarPrimTy s elt = mkTyConApp mutVarPrimTyCon [s, elt]
\end{code}
-@_RealWorld@ is deeply magical. It {\em is primitive}, but it
-{\em is not unboxed}.
+%************************************************************************
+%* *
+\subsection[TysPrim-synch-var]{The synchronizing variable type}
+%* *
+%************************************************************************
+
\begin{code}
-realWorldTy = applyTyCon realWorldTyCon []
-realWorldTyCon
- = mkDataTyCon realWorldTyConKey mkBoxedTypeKind full_name
- [{-no tyvars-}]
- [{-no context-}]
- [{-no data cons!-}] -- we tell you *nothing* about this guy
- [{-no derivings-}]
- DataType
- where
- full_name = mkPreludeCoreName pRELUDE_BUILTIN SLIT("_RealWorld")
+mVarPrimTyCon = pcPrimTyCon mVarPrimTyConName vrcsZP PrimPtrRep
-realWorldStatePrimTy = mkStatePrimTy realWorldTy
+mkMVarPrimTy s elt = mkTyConApp mVarPrimTyCon [s, elt]
\end{code}
-Note: the ``state-pairing'' types are not truly primitive, so they are
-defined in \tr{TysWiredIn.lhs}, not here.
-
%************************************************************************
%* *
-\subsection[TysPrim-arrays]{The primitive array types}
+\subsection[TysPrim-stable-ptrs]{The stable-pointer type}
%* *
%************************************************************************
\begin{code}
-arrayPrimTyCon = pcPrimTyCon arrayPrimTyConKey SLIT("Array#") 1
- (\ [elt_kind] -> ArrayRep)
+stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConName vrcsP StablePtrRep
-byteArrayPrimTyCon = pcPrimTyCon byteArrayPrimTyConKey SLIT("ByteArray#") 0
- (\ [] -> ByteArrayRep)
+mkStablePtrPrimTy ty = mkTyConApp stablePtrPrimTyCon [ty]
+\end{code}
-mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConKey SLIT("MutableArray#") 2
- (\ [s_kind, elt_kind] -> ArrayRep)
+%************************************************************************
+%* *
+\subsection[TysPrim-stable-names]{The stable-name type}
+%* *
+%************************************************************************
-mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConKey SLIT("MutableByteArray#") 1
- (\ [s_kind] -> ByteArrayRep)
+\begin{code}
+stableNamePrimTyCon = pcPrimTyCon stableNamePrimTyConName vrcsP StableNameRep
-mkArrayPrimTy elt = applyTyCon arrayPrimTyCon [elt]
-byteArrayPrimTy = applyTyCon byteArrayPrimTyCon []
-mkMutableArrayPrimTy s elt = applyTyCon mutableArrayPrimTyCon [s, elt]
-mkMutableByteArrayPrimTy s = applyTyCon mutableByteArrayPrimTyCon [s]
+mkStableNamePrimTy ty = mkTyConApp stableNamePrimTyCon [ty]
\end{code}
%************************************************************************
%* *
-\subsection[TysPrim-synch-var]{The synchronizing variable type}
+\subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
%* *
%************************************************************************
-\begin{code}
-synchVarPrimTyCon = pcPrimTyCon synchVarPrimTyConKey SLIT("SynchVar#") 2
- (\ [s_kind, elt_kind] -> PtrRep)
+A Foreign Object is just a boxed, unlifted, Addr#. They're needed
+because finalisers (weak pointers) can't watch Addr#s, they can only
+watch heap-resident objects.
+
+We can't use a lifted Addr# (such as Addr) because race conditions
+could bite us. For example, if the program deconstructed the Addr
+before passing its contents to a ccall, and a weak pointer was
+watching the Addr, the weak pointer might deduce that the Addr was
+dead before it really was.
-mkSynchVarPrimTy s elt = applyTyCon synchVarPrimTyCon [s, elt]
+\begin{code}
+foreignObjPrimTy = mkTyConTy foreignObjPrimTyCon
+foreignObjPrimTyCon = pcPrimTyCon0 foreignObjPrimTyConName ForeignObjRep
\end{code}
+
+%************************************************************************
+%* *
+\subsection[TysPrim-BCOs]{The ``bytecode object'' type}
+%* *
+%************************************************************************
+\begin{code}
+bcoPrimTy = mkTyConTy bcoPrimTyCon
+bcoPrimTyCon = pcPrimTyCon0 bcoPrimTyConName BCORep
+\end{code}
+
%************************************************************************
%* *
-\subsection[TysPrim-stable-ptrs]{The stable-pointer type}
+\subsection[TysPrim-Weak]{The ``weak pointer'' type}
%* *
%************************************************************************
\begin{code}
-stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConKey SLIT("StablePtr#") 1
- (\ [elt_kind] -> StablePtrRep)
+weakPrimTyCon = pcPrimTyCon weakPrimTyConName vrcsP WeakPtrRep
-mkStablePtrPrimTy ty = applyTyCon stablePtrPrimTyCon [ty]
+mkWeakPrimTy v = mkTyConApp weakPrimTyCon [v]
\end{code}
%************************************************************************
%* *
-\subsection[TysPrim-malloc-ptrs]{The ``malloc''-pointer type}
+\subsection[TysPrim-thread-ids]{The ``thread id'' type}
%* *
%************************************************************************
-``Malloc'' pointers provide a mechanism which will let Haskell's
-garbage collector communicate with a {\em simple\/} garbage collector
-in the IO world (probably \tr{malloc}, hence the name).We want Haskell
-to be able to hold onto references to objects in the IO world and for
-Haskell's garbage collector to tell the IO world when these references
-become garbage. We are not aiming to provide a mechanism that could
-talk to a sophisticated garbage collector such as that provided by a
-LISP system (with a correspondingly complex interface); in particular,
-we shall ignore the danger of circular structures spread across the
-two systems.
+A thread id is represented by a pointer to the TSO itself, to ensure
+that they are always unique and we can always find the TSO for a given
+thread id. However, this has the unfortunate consequence that a
+ThreadId# for a given thread is treated as a root by the garbage
+collector and can keep TSOs around for too long.
+
+Hence the programmer API for thread manipulation uses a weak pointer
+to the thread id internally.
+
+\begin{code}
+threadIdPrimTy = mkTyConTy threadIdPrimTyCon
+threadIdPrimTyCon = pcPrimTyCon0 threadIdPrimTyConName ThreadIdRep
+\end{code}
+
+%************************************************************************
+%* *
+\subsection[TysPrim-PrimRep]{Making types from PrimReps}
+%* *
+%************************************************************************
-There are no primitive operations on @CHeapPtr#@s (although equality
-could possibly be added?)
+Each of the primitive types from this module is equivalent to a
+PrimRep (see PrimRep.lhs). The following function returns the
+primitive TyCon for a given PrimRep.
\begin{code}
-mallocPtrPrimTyCon = pcPrimTyCon mallocPtrPrimTyConKey SLIT("MallocPtr#") 0
- (\ [] -> MallocPtrRep)
+primRepTyCon CharRep = charPrimTyCon
+primRepTyCon Int8Rep = charPrimTyCon
+primRepTyCon IntRep = intPrimTyCon
+primRepTyCon WordRep = wordPrimTyCon
+primRepTyCon Int64Rep = int64PrimTyCon
+primRepTyCon Word64Rep = word64PrimTyCon
+primRepTyCon AddrRep = addrPrimTyCon
+primRepTyCon FloatRep = floatPrimTyCon
+primRepTyCon DoubleRep = doublePrimTyCon
+primRepTyCon StablePtrRep = stablePtrPrimTyCon
+primRepTyCon ForeignObjRep = foreignObjPrimTyCon
+primRepTyCon WeakPtrRep = weakPrimTyCon
+primRepTyCon other = pprPanic "primRepTyCon" (ppr other)
\end{code}