2 % (c) The AQUA Project, Glasgow University, 1994-1996
4 \section[TysPrim]{Wired-in knowledge about primitive types}
6 This module tracks the ``state interface'' document, ``GHC prelude:
7 types and operations.''
10 #include "HsVersions.h"
16 import Kind ( mkUnboxedTypeKind, mkBoxedTypeKind, mkTypeKind, mkArrowKind )
17 import Name ( mkBuiltinName )
18 import PrelMods ( pRELUDE_BUILTIN )
19 import PrimRep ( PrimRep(..) ) -- getPrimRepInfo uses PrimRep repn
20 import TyCon ( mkPrimTyCon, mkDataTyCon, NewOrData(..) )
21 import TyVar ( GenTyVar(..), alphaTyVars )
22 import Type ( applyTyCon, mkTyVarTys )
23 import Usage ( usageOmega )
29 alphaTys = mkTyVarTys alphaTyVars
30 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
33 %************************************************************************
35 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
37 %************************************************************************
41 pcPrimTyCon :: Unique{-TyConKey-} -> FAST_STRING -> Int -> PrimRep -> TyCon
43 pcPrimTyCon key str arity primrep
44 = mkPrimTyCon name (mk_kind arity) primrep
46 name = mkBuiltinName key pRELUDE_BUILTIN str
48 mk_kind 0 = mkUnboxedTypeKind
49 mk_kind n = mkTypeKind `mkArrowKind` mk_kind (n-1)
52 charPrimTy = applyTyCon charPrimTyCon []
53 charPrimTyCon = pcPrimTyCon charPrimTyConKey SLIT("Char#") 0 CharRep
55 intPrimTy = applyTyCon intPrimTyCon []
56 intPrimTyCon = pcPrimTyCon intPrimTyConKey SLIT("Int#") 0 IntRep
58 wordPrimTy = applyTyCon wordPrimTyCon []
59 wordPrimTyCon = pcPrimTyCon wordPrimTyConKey SLIT("Word#") 0 WordRep
61 addrPrimTy = applyTyCon addrPrimTyCon []
62 addrPrimTyCon = pcPrimTyCon addrPrimTyConKey SLIT("Addr#") 0 AddrRep
64 floatPrimTy = applyTyCon floatPrimTyCon []
65 floatPrimTyCon = pcPrimTyCon floatPrimTyConKey SLIT("Float#") 0 FloatRep
67 doublePrimTy = applyTyCon doublePrimTyCon []
68 doublePrimTyCon = pcPrimTyCon doublePrimTyConKey SLIT("Double#") 0 DoubleRep
71 @PrimitiveKinds@ are used in @PrimitiveOps@, for which we often need
72 to reconstruct various type information. (It's slightly more
73 convenient/efficient to make type info from kinds, than kinds [etc.]
78 PrimRep -> (String, -- tag string
79 Type, TyCon) -- prim type and tycon
81 getPrimRepInfo CharRep = ("Char", charPrimTy, charPrimTyCon)
82 getPrimRepInfo IntRep = ("Int", intPrimTy, intPrimTyCon)
83 getPrimRepInfo WordRep = ("Word", wordPrimTy, wordPrimTyCon)
84 getPrimRepInfo AddrRep = ("Addr", addrPrimTy, addrPrimTyCon)
85 getPrimRepInfo FloatRep = ("Float", floatPrimTy, floatPrimTyCon)
86 getPrimRepInfo DoubleRep = ("Double", doublePrimTy, doublePrimTyCon)
89 %************************************************************************
91 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
93 %************************************************************************
95 State# is the primitive, unboxed type of states. It has one type parameter,
101 where s is a type variable. The only purpose of the type parameter is to
102 keep different state threads separate. It is represented by nothing at all.
105 mkStatePrimTy ty = applyTyCon statePrimTyCon [ty]
106 statePrimTyCon = pcPrimTyCon statePrimTyConKey SLIT("State#") 1 VoidRep
109 @_RealWorld@ is deeply magical. It {\em is primitive}, but it
110 {\em is not unboxed}.
111 We never manipulate values of type RealWorld; it's only used in the type
112 system, to parameterise State#.
115 realWorldTy = applyTyCon realWorldTyCon []
117 = mkDataTyCon name mkBoxedTypeKind
120 [{-no data cons!-}] -- we tell you *nothing* about this guy
124 name = mkBuiltinName realWorldTyConKey pRELUDE_BUILTIN SLIT("_RealWorld")
126 realWorldStatePrimTy = mkStatePrimTy realWorldTy
129 Note: the ``state-pairing'' types are not truly primitive, so they are
130 defined in \tr{TysWiredIn.lhs}, not here.
132 %************************************************************************
134 \subsection[TysPrim-arrays]{The primitive array types}
136 %************************************************************************
139 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConKey SLIT("Array#") 1 ArrayRep
141 byteArrayPrimTyCon = pcPrimTyCon byteArrayPrimTyConKey SLIT("ByteArray#") 0 ByteArrayRep
143 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConKey SLIT("MutableArray#") 2 ArrayRep
145 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConKey SLIT("MutableByteArray#") 1 ByteArrayRep
147 mkArrayPrimTy elt = applyTyCon arrayPrimTyCon [elt]
148 byteArrayPrimTy = applyTyCon byteArrayPrimTyCon []
149 mkMutableArrayPrimTy s elt = applyTyCon mutableArrayPrimTyCon [s, elt]
150 mkMutableByteArrayPrimTy s = applyTyCon mutableByteArrayPrimTyCon [s]
153 %************************************************************************
155 \subsection[TysPrim-synch-var]{The synchronizing variable type}
157 %************************************************************************
160 synchVarPrimTyCon = pcPrimTyCon synchVarPrimTyConKey SLIT("SynchVar#") 2 PtrRep
162 mkSynchVarPrimTy s elt = applyTyCon synchVarPrimTyCon [s, elt]
165 %************************************************************************
167 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
169 %************************************************************************
172 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConKey SLIT("StablePtr#") 1 StablePtrRep
174 mkStablePtrPrimTy ty = applyTyCon stablePtrPrimTyCon [ty]
177 %************************************************************************
179 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
181 %************************************************************************
183 Foreign objects (formerly ``Malloc'' pointers) provide a mechanism which
184 will let Haskell's garbage collector communicate with a {\em simple\/}
185 garbage collector in the IO world. We want Haskell to be able to hold
186 onto references to objects in the IO world and for Haskell's garbage
187 collector to tell the IO world when these references become garbage.
188 We are not aiming to provide a mechanism that could
189 talk to a sophisticated garbage collector such as that provided by a
190 LISP system (with a correspondingly complex interface); in particular,
191 we shall ignore the danger of circular structures spread across the
194 There are no primitive operations on @ForeignObj#@s (although equality
195 could possibly be added?)
198 foreignObjPrimTy = applyTyCon foreignObjPrimTyCon []
199 foreignObjPrimTyCon = pcPrimTyCon foreignObjPrimTyConKey SLIT("ForeignObj#") 0 ForeignObjRep