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 )
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
42 -> Int -> ([PrimRep] -> PrimRep) -> TyCon
43 pcPrimTyCon key str arity{-UNUSED-} kind_fn{-UNUSED-}
44 = mkPrimTyCon name mkUnboxedTypeKind
46 name = mkBuiltinName key pRELUDE_BUILTIN str
49 charPrimTy = applyTyCon charPrimTyCon []
50 charPrimTyCon = pcPrimTyCon charPrimTyConKey SLIT("Char#") 0 (\ [] -> CharRep)
52 intPrimTy = applyTyCon intPrimTyCon []
53 intPrimTyCon = pcPrimTyCon intPrimTyConKey SLIT("Int#") 0 (\ [] -> IntRep)
55 wordPrimTy = applyTyCon wordPrimTyCon []
56 wordPrimTyCon = pcPrimTyCon wordPrimTyConKey SLIT("Word#") 0 (\ [] -> WordRep)
58 addrPrimTy = applyTyCon addrPrimTyCon []
59 addrPrimTyCon = pcPrimTyCon addrPrimTyConKey SLIT("Addr#") 0 (\ [] -> AddrRep)
61 floatPrimTy = applyTyCon floatPrimTyCon []
62 floatPrimTyCon = pcPrimTyCon floatPrimTyConKey SLIT("Float#") 0 (\ [] -> FloatRep)
64 doublePrimTy = applyTyCon doublePrimTyCon []
65 doublePrimTyCon = pcPrimTyCon doublePrimTyConKey SLIT("Double#") 0 (\ [] -> DoubleRep)
68 @PrimitiveKinds@ are used in @PrimitiveOps@, for which we often need
69 to reconstruct various type information. (It's slightly more
70 convenient/efficient to make type info from kinds, than kinds [etc.]
75 PrimRep -> (String, -- tag string
76 Type, TyCon) -- prim type and tycon
78 getPrimRepInfo CharRep = ("Char", charPrimTy, charPrimTyCon)
79 getPrimRepInfo IntRep = ("Int", intPrimTy, intPrimTyCon)
80 getPrimRepInfo WordRep = ("Word", wordPrimTy, wordPrimTyCon)
81 getPrimRepInfo AddrRep = ("Addr", addrPrimTy, addrPrimTyCon)
82 getPrimRepInfo FloatRep = ("Float", floatPrimTy, floatPrimTyCon)
83 getPrimRepInfo DoubleRep = ("Double", doublePrimTy, doublePrimTyCon)
86 %************************************************************************
88 \subsection[TysPrim-void]{The @Void#@ type}
90 %************************************************************************
92 Very similar to the @State#@ type.
94 voidPrimTy = applyTyCon voidPrimTyCon []
96 voidPrimTyCon = pcPrimTyCon voidPrimTyConKey SLIT("Void#") 0
100 %************************************************************************
102 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
104 %************************************************************************
107 mkStatePrimTy ty = applyTyCon statePrimTyCon [ty]
108 statePrimTyCon = pcPrimTyCon statePrimTyConKey SLIT("State#") 1
109 (\ [s_kind] -> VoidRep)
112 @_RealWorld@ is deeply magical. It {\em is primitive}, but it
113 {\em is not unboxed}.
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
140 (\ [elt_kind] -> ArrayRep)
142 byteArrayPrimTyCon = pcPrimTyCon byteArrayPrimTyConKey SLIT("ByteArray#") 0
143 (\ [] -> ByteArrayRep)
145 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConKey SLIT("MutableArray#") 2
146 (\ [s_kind, elt_kind] -> ArrayRep)
148 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConKey SLIT("MutableByteArray#") 1
149 (\ [s_kind] -> ByteArrayRep)
151 mkArrayPrimTy elt = applyTyCon arrayPrimTyCon [elt]
152 byteArrayPrimTy = applyTyCon byteArrayPrimTyCon []
153 mkMutableArrayPrimTy s elt = applyTyCon mutableArrayPrimTyCon [s, elt]
154 mkMutableByteArrayPrimTy s = applyTyCon mutableByteArrayPrimTyCon [s]
157 %************************************************************************
159 \subsection[TysPrim-synch-var]{The synchronizing variable type}
161 %************************************************************************
164 synchVarPrimTyCon = pcPrimTyCon synchVarPrimTyConKey SLIT("SynchVar#") 2
165 (\ [s_kind, elt_kind] -> PtrRep)
167 mkSynchVarPrimTy s elt = applyTyCon synchVarPrimTyCon [s, elt]
170 %************************************************************************
172 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
174 %************************************************************************
177 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConKey SLIT("StablePtr#") 1
178 (\ [elt_kind] -> StablePtrRep)
180 mkStablePtrPrimTy ty = applyTyCon stablePtrPrimTyCon [ty]
183 %************************************************************************
185 \subsection[TysPrim-malloc-ptrs]{The ``malloc''-pointer type}
187 %************************************************************************
189 ``Malloc'' pointers provide a mechanism which will let Haskell's
190 garbage collector communicate with a {\em simple\/} garbage collector
191 in the IO world (probably \tr{malloc}, hence the name).We want Haskell
192 to be able to hold onto references to objects in the IO world and for
193 Haskell's garbage collector to tell the IO world when these references
194 become garbage. We are not aiming to provide a mechanism that could
195 talk to a sophisticated garbage collector such as that provided by a
196 LISP system (with a correspondingly complex interface); in particular,
197 we shall ignore the danger of circular structures spread across the
200 There are no primitive operations on @CHeapPtr#@s (although equality
201 could possibly be added?)
204 mallocPtrPrimTyCon = pcPrimTyCon mallocPtrPrimTyConKey SLIT("MallocPtr#") 0
205 (\ [] -> MallocPtrRep)