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 ( mkWiredInTyConName )
18 import PrimRep ( PrimRep(..) ) -- getPrimRepInfo uses PrimRep repn
19 import TyCon --( mkPrimTyCon, mkDataTyCon, NewOrData(..) )
20 import Type --( applyTyCon, mkTyVarTys, mkTyConTy )
21 import TyVar ( GenTyVar(..), alphaTyVars )
22 import Usage ( usageOmega )
23 import PrelMods ( gHC__ )
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
45 name = mkWiredInTyConName key gHC__ str the_tycon
46 the_tycon = mkPrimTyCon name (mk_kind arity) primrep
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 []
115 realWorldTyCon = mk_no_constr_tycon realWorldTyConKey SLIT("RealWorld")
116 realWorldStatePrimTy = mkStatePrimTy realWorldTy
119 Note: the ``state-pairing'' types are not truly primitive, so they are
120 defined in \tr{TysWiredIn.lhs}, not here.
123 -- The Void type is represented as a data type with no constructors
124 -- It's a built in type (i.e. there's no way to define it in Haskell;
125 -- the nearest would be
127 -- data Void = -- No constructors!
129 -- ) It's boxed; there is only one value of this
130 -- type, namely "void", whose semantics is just bottom.
131 voidTy = mkTyConTy voidTyCon
132 voidTyCon = mk_no_constr_tycon voidTyConKey SLIT("Void")
136 mk_no_constr_tycon key str
139 name = mkWiredInTyConName key gHC__ str the_tycon
140 the_tycon = mkDataTyCon name mkBoxedTypeKind
143 [{-no data cons!-}] -- we tell you *nothing* about this guy
148 %************************************************************************
150 \subsection[TysPrim-arrays]{The primitive array types}
152 %************************************************************************
155 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConKey SLIT("Array#") 1 ArrayRep
157 byteArrayPrimTyCon = pcPrimTyCon byteArrayPrimTyConKey SLIT("ByteArray#") 0 ByteArrayRep
159 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConKey SLIT("MutableArray#") 2 ArrayRep
161 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConKey SLIT("MutableByteArray#") 1 ByteArrayRep
163 mkArrayPrimTy elt = applyTyCon arrayPrimTyCon [elt]
164 byteArrayPrimTy = applyTyCon byteArrayPrimTyCon []
165 mkMutableArrayPrimTy s elt = applyTyCon mutableArrayPrimTyCon [s, elt]
166 mkMutableByteArrayPrimTy s = applyTyCon mutableByteArrayPrimTyCon [s]
169 %************************************************************************
171 \subsection[TysPrim-synch-var]{The synchronizing variable type}
173 %************************************************************************
176 synchVarPrimTyCon = pcPrimTyCon synchVarPrimTyConKey SLIT("SynchVar#") 2 PtrRep
178 mkSynchVarPrimTy s elt = applyTyCon synchVarPrimTyCon [s, elt]
181 %************************************************************************
183 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
185 %************************************************************************
188 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConKey SLIT("StablePtr#") 1 StablePtrRep
190 mkStablePtrPrimTy ty = applyTyCon stablePtrPrimTyCon [ty]
193 %************************************************************************
195 \subsection[TysPrim-foreign-objs]{The ``foreign object'' type}
197 %************************************************************************
199 Foreign objects (formerly ``Malloc'' pointers) provide a mechanism which
200 will let Haskell's garbage collector communicate with a {\em simple\/}
201 garbage collector in the IO world. We want Haskell to be able to hold
202 onto references to objects in the IO world and for Haskell's garbage
203 collector to tell the IO world when these references become garbage.
204 We are not aiming to provide a mechanism that could
205 talk to a sophisticated garbage collector such as that provided by a
206 LISP system (with a correspondingly complex interface); in particular,
207 we shall ignore the danger of circular structures spread across the
210 There are no primitive operations on @ForeignObj#@s (although equality
211 could possibly be added?)
214 foreignObjPrimTy = applyTyCon foreignObjPrimTyCon []
215 foreignObjPrimTyCon = pcPrimTyCon foreignObjPrimTyConKey SLIT("ForeignObj#") 0 ForeignObjRep