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 ( mkPrimitiveName )
18 import PrelMods ( gHC_BUILTINS )
19 import PrimRep ( PrimRep(..) ) -- getPrimRepInfo uses PrimRep repn
20 import TyCon ( mkPrimTyCon, mkDataTyCon, NewOrData(..) )
21 import Type ( mkTyConTy )
22 import TyVar ( GenTyVar(..), alphaTyVars )
23 import Type ( applyTyCon, mkTyVarTys )
24 import Usage ( usageOmega )
30 alphaTys = mkTyVarTys alphaTyVars
31 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
34 %************************************************************************
36 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
38 %************************************************************************
42 pcPrimTyCon :: Unique{-TyConKey-} -> FAST_STRING -> Int -> PrimRep -> TyCon
44 pcPrimTyCon key str arity primrep
45 = mkPrimTyCon name (mk_kind arity) primrep
47 name = mkPrimitiveName key (OrigName gHC_BUILTINS str)
49 mk_kind 0 = mkUnboxedTypeKind
50 mk_kind n = mkTypeKind `mkArrowKind` mk_kind (n-1)
53 charPrimTy = applyTyCon charPrimTyCon []
54 charPrimTyCon = pcPrimTyCon charPrimTyConKey SLIT("Char#") 0 CharRep
56 intPrimTy = applyTyCon intPrimTyCon []
57 intPrimTyCon = pcPrimTyCon intPrimTyConKey SLIT("Int#") 0 IntRep
59 wordPrimTy = applyTyCon wordPrimTyCon []
60 wordPrimTyCon = pcPrimTyCon wordPrimTyConKey SLIT("Word#") 0 WordRep
62 addrPrimTy = applyTyCon addrPrimTyCon []
63 addrPrimTyCon = pcPrimTyCon addrPrimTyConKey SLIT("Addr#") 0 AddrRep
65 floatPrimTy = applyTyCon floatPrimTyCon []
66 floatPrimTyCon = pcPrimTyCon floatPrimTyConKey SLIT("Float#") 0 FloatRep
68 doublePrimTy = applyTyCon doublePrimTyCon []
69 doublePrimTyCon = pcPrimTyCon doublePrimTyConKey SLIT("Double#") 0 DoubleRep
72 @PrimitiveKinds@ are used in @PrimitiveOps@, for which we often need
73 to reconstruct various type information. (It's slightly more
74 convenient/efficient to make type info from kinds, than kinds [etc.]
79 PrimRep -> (String, -- tag string
80 Type, TyCon) -- prim type and tycon
82 getPrimRepInfo CharRep = ("Char", charPrimTy, charPrimTyCon)
83 getPrimRepInfo IntRep = ("Int", intPrimTy, intPrimTyCon)
84 getPrimRepInfo WordRep = ("Word", wordPrimTy, wordPrimTyCon)
85 getPrimRepInfo AddrRep = ("Addr", addrPrimTy, addrPrimTyCon)
86 getPrimRepInfo FloatRep = ("Float", floatPrimTy, floatPrimTyCon)
87 getPrimRepInfo DoubleRep = ("Double", doublePrimTy, doublePrimTyCon)
90 %************************************************************************
92 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
94 %************************************************************************
96 State# is the primitive, unboxed type of states. It has one type parameter,
102 where s is a type variable. The only purpose of the type parameter is to
103 keep different state threads separate. It is represented by nothing at all.
106 mkStatePrimTy ty = applyTyCon statePrimTyCon [ty]
107 statePrimTyCon = pcPrimTyCon statePrimTyConKey SLIT("State#") 1 VoidRep
110 @_RealWorld@ is deeply magical. It {\em is primitive}, but it
111 {\em is not unboxed}.
112 We never manipulate values of type RealWorld; it's only used in the type
113 system, to parameterise State#.
116 realWorldTy = applyTyCon realWorldTyCon []
118 = mkDataTyCon name mkBoxedTypeKind
121 [{-no data cons!-}] -- we tell you *nothing* about this guy
125 name = mkPrimitiveName realWorldTyConKey (OrigName gHC_BUILTINS SLIT("RealWorld"))
127 realWorldStatePrimTy = mkStatePrimTy realWorldTy
130 Note: the ``state-pairing'' types are not truly primitive, so they are
131 defined in \tr{TysWiredIn.lhs}, not here.
134 -- The Void type is represented as a data type with no constructors
135 -- It's a built in type (i.e. there's no way to define it in Haskell;
136 -- the nearest would be
138 -- data Void = -- No constructors!
140 -- ) It's boxed; there is only one value of this
141 -- type, namely "void", whose semantics is just bottom.
142 voidTy = mkTyConTy voidTyCon
145 = mkDataTyCon name mkBoxedTypeKind
152 name = mkPrimitiveName voidTyConKey (OrigName gHC_BUILTINS SLIT("Void"))
155 %************************************************************************
157 \subsection[TysPrim-arrays]{The primitive array types}
159 %************************************************************************
162 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConKey SLIT("Array#") 1 ArrayRep
164 byteArrayPrimTyCon = pcPrimTyCon byteArrayPrimTyConKey SLIT("ByteArray#") 0 ByteArrayRep
166 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConKey SLIT("MutableArray#") 2 ArrayRep
168 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConKey SLIT("MutableByteArray#") 1 ByteArrayRep
170 mkArrayPrimTy elt = applyTyCon arrayPrimTyCon [elt]
171 byteArrayPrimTy = applyTyCon byteArrayPrimTyCon []
172 mkMutableArrayPrimTy s elt = applyTyCon mutableArrayPrimTyCon [s, elt]
173 mkMutableByteArrayPrimTy s = applyTyCon mutableByteArrayPrimTyCon [s]
176 %************************************************************************
178 \subsection[TysPrim-synch-var]{The synchronizing variable type}
180 %************************************************************************
183 synchVarPrimTyCon = pcPrimTyCon synchVarPrimTyConKey SLIT("SynchVar#") 2 PtrRep
185 mkSynchVarPrimTy s elt = applyTyCon synchVarPrimTyCon [s, elt]
188 %************************************************************************
190 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
192 %************************************************************************
195 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConKey SLIT("StablePtr#") 1 StablePtrRep
197 mkStablePtrPrimTy ty = applyTyCon stablePtrPrimTyCon [ty]
200 %************************************************************************
202 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
204 %************************************************************************
206 Foreign objects (formerly ``Malloc'' pointers) provide a mechanism which
207 will let Haskell's garbage collector communicate with a {\em simple\/}
208 garbage collector in the IO world. We want Haskell to be able to hold
209 onto references to objects in the IO world and for Haskell's garbage
210 collector to tell the IO world when these references become garbage.
211 We are not aiming to provide a mechanism that could
212 talk to a sophisticated garbage collector such as that provided by a
213 LISP system (with a correspondingly complex interface); in particular,
214 we shall ignore the danger of circular structures spread across the
217 There are no primitive operations on @ForeignObj#@s (although equality
218 could possibly be added?)
221 foreignObjPrimTy = applyTyCon foreignObjPrimTyCon []
222 foreignObjPrimTyCon = pcPrimTyCon foreignObjPrimTyConKey SLIT("ForeignObj#") 0 ForeignObjRep