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 ( applyTyCon, mkTyVarTys, mkTyConTy )
22 import TyVar ( GenTyVar(..), alphaTyVars )
23 import Usage ( usageOmega )
28 alphaTys = mkTyVarTys alphaTyVars
29 (alphaTy:betaTy:gammaTy:deltaTy:_) = alphaTys
32 %************************************************************************
34 \subsection[TysPrim-basic]{Basic primitive types (@Char#@, @Int#@, etc.)}
36 %************************************************************************
40 pcPrimTyCon :: Unique{-TyConKey-} -> FAST_STRING -> Int -> PrimRep -> TyCon
42 pcPrimTyCon key str arity primrep
43 = mkPrimTyCon name (mk_kind arity) primrep
45 name = mkPrimitiveName key (OrigName gHC_BUILTINS str)
47 mk_kind 0 = mkUnboxedTypeKind
48 mk_kind n = mkTypeKind `mkArrowKind` mk_kind (n-1)
51 charPrimTy = applyTyCon charPrimTyCon []
52 charPrimTyCon = pcPrimTyCon charPrimTyConKey SLIT("Char#") 0 CharRep
54 intPrimTy = applyTyCon intPrimTyCon []
55 intPrimTyCon = pcPrimTyCon intPrimTyConKey SLIT("Int#") 0 IntRep
57 wordPrimTy = applyTyCon wordPrimTyCon []
58 wordPrimTyCon = pcPrimTyCon wordPrimTyConKey SLIT("Word#") 0 WordRep
60 addrPrimTy = applyTyCon addrPrimTyCon []
61 addrPrimTyCon = pcPrimTyCon addrPrimTyConKey SLIT("Addr#") 0 AddrRep
63 floatPrimTy = applyTyCon floatPrimTyCon []
64 floatPrimTyCon = pcPrimTyCon floatPrimTyConKey SLIT("Float#") 0 FloatRep
66 doublePrimTy = applyTyCon doublePrimTyCon []
67 doublePrimTyCon = pcPrimTyCon doublePrimTyConKey SLIT("Double#") 0 DoubleRep
70 @PrimitiveKinds@ are used in @PrimitiveOps@, for which we often need
71 to reconstruct various type information. (It's slightly more
72 convenient/efficient to make type info from kinds, than kinds [etc.]
77 PrimRep -> (String, -- tag string
78 Type, TyCon) -- prim type and tycon
80 getPrimRepInfo CharRep = ("Char", charPrimTy, charPrimTyCon)
81 getPrimRepInfo IntRep = ("Int", intPrimTy, intPrimTyCon)
82 getPrimRepInfo WordRep = ("Word", wordPrimTy, wordPrimTyCon)
83 getPrimRepInfo AddrRep = ("Addr", addrPrimTy, addrPrimTyCon)
84 getPrimRepInfo FloatRep = ("Float", floatPrimTy, floatPrimTyCon)
85 getPrimRepInfo DoubleRep = ("Double", doublePrimTy, doublePrimTyCon)
88 %************************************************************************
90 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
92 %************************************************************************
94 State# is the primitive, unboxed type of states. It has one type parameter,
100 where s is a type variable. The only purpose of the type parameter is to
101 keep different state threads separate. It is represented by nothing at all.
104 mkStatePrimTy ty = applyTyCon statePrimTyCon [ty]
105 statePrimTyCon = pcPrimTyCon statePrimTyConKey SLIT("State#") 1 VoidRep
108 @_RealWorld@ is deeply magical. It {\em is primitive}, but it
109 {\em is not unboxed}.
110 We never manipulate values of type RealWorld; it's only used in the type
111 system, to parameterise State#.
114 realWorldTy = applyTyCon realWorldTyCon []
116 = mkDataTyCon name mkBoxedTypeKind
119 [{-no data cons!-}] -- we tell you *nothing* about this guy
123 name = mkPrimitiveName realWorldTyConKey (OrigName gHC_BUILTINS SLIT("RealWorld"))
125 realWorldStatePrimTy = mkStatePrimTy realWorldTy
128 Note: the ``state-pairing'' types are not truly primitive, so they are
129 defined in \tr{TysWiredIn.lhs}, not here.
132 -- The Void type is represented as a data type with no constructors
133 -- It's a built in type (i.e. there's no way to define it in Haskell;
134 -- the nearest would be
136 -- data Void = -- No constructors!
138 -- ) It's boxed; there is only one value of this
139 -- type, namely "void", whose semantics is just bottom.
140 voidTy = mkTyConTy voidTyCon
143 = mkDataTyCon name mkBoxedTypeKind
150 name = mkPrimitiveName voidTyConKey (OrigName gHC_BUILTINS SLIT("Void"))
153 %************************************************************************
155 \subsection[TysPrim-arrays]{The primitive array types}
157 %************************************************************************
160 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConKey SLIT("Array#") 1 ArrayRep
162 byteArrayPrimTyCon = pcPrimTyCon byteArrayPrimTyConKey SLIT("ByteArray#") 0 ByteArrayRep
164 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConKey SLIT("MutableArray#") 2 ArrayRep
166 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConKey SLIT("MutableByteArray#") 1 ByteArrayRep
168 mkArrayPrimTy elt = applyTyCon arrayPrimTyCon [elt]
169 byteArrayPrimTy = applyTyCon byteArrayPrimTyCon []
170 mkMutableArrayPrimTy s elt = applyTyCon mutableArrayPrimTyCon [s, elt]
171 mkMutableByteArrayPrimTy s = applyTyCon mutableByteArrayPrimTyCon [s]
174 %************************************************************************
176 \subsection[TysPrim-synch-var]{The synchronizing variable type}
178 %************************************************************************
181 synchVarPrimTyCon = pcPrimTyCon synchVarPrimTyConKey SLIT("SynchVar#") 2 PtrRep
183 mkSynchVarPrimTy s elt = applyTyCon synchVarPrimTyCon [s, elt]
186 %************************************************************************
188 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
190 %************************************************************************
193 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConKey SLIT("StablePtr#") 1 StablePtrRep
195 mkStablePtrPrimTy ty = applyTyCon stablePtrPrimTyCon [ty]
198 %************************************************************************
200 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
202 %************************************************************************
204 Foreign objects (formerly ``Malloc'' pointers) provide a mechanism which
205 will let Haskell's garbage collector communicate with a {\em simple\/}
206 garbage collector in the IO world. We want Haskell to be able to hold
207 onto references to objects in the IO world and for Haskell's garbage
208 collector to tell the IO world when these references become garbage.
209 We are not aiming to provide a mechanism that could
210 talk to a sophisticated garbage collector such as that provided by a
211 LISP system (with a correspondingly complex interface); in particular,
212 we shall ignore the danger of circular structures spread across the
215 There are no primitive operations on @ForeignObj#@s (although equality
216 could possibly be added?)
219 foreignObjPrimTy = applyTyCon foreignObjPrimTyCon []
220 foreignObjPrimTyCon = pcPrimTyCon foreignObjPrimTyConKey SLIT("ForeignObj#") 0 ForeignObjRep