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 NameTypes ( mkPreludeCoreName, FullName )
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] -> PrimRep) -> TyCon
42 pcPrimTyCon key name arity{-UNUSED-} kind_fn{-UNUSED-}
43 = mkPrimTyCon key full_name mkUnboxedTypeKind
45 full_name = mkPreludeCoreName pRELUDE_BUILTIN name
48 charPrimTy = applyTyCon charPrimTyCon []
49 charPrimTyCon = pcPrimTyCon charPrimTyConKey SLIT("Char#") 0 (\ [] -> CharRep)
51 intPrimTy = applyTyCon intPrimTyCon []
52 intPrimTyCon = pcPrimTyCon intPrimTyConKey SLIT("Int#") 0 (\ [] -> IntRep)
54 wordPrimTy = applyTyCon wordPrimTyCon []
55 wordPrimTyCon = pcPrimTyCon wordPrimTyConKey SLIT("Word#") 0 (\ [] -> WordRep)
57 addrPrimTy = applyTyCon addrPrimTyCon []
58 addrPrimTyCon = pcPrimTyCon addrPrimTyConKey SLIT("Addr#") 0 (\ [] -> AddrRep)
60 floatPrimTy = applyTyCon floatPrimTyCon []
61 floatPrimTyCon = pcPrimTyCon floatPrimTyConKey SLIT("Float#") 0 (\ [] -> FloatRep)
63 doublePrimTy = applyTyCon doublePrimTyCon []
64 doublePrimTyCon = pcPrimTyCon doublePrimTyConKey SLIT("Double#") 0 (\ [] -> DoubleRep)
67 @PrimitiveKinds@ are used in @PrimitiveOps@, for which we often need
68 to reconstruct various type information. (It's slightly more
69 convenient/efficient to make type info from kinds, than kinds [etc.]
74 PrimRep -> (String, -- tag string
75 Type, TyCon) -- prim type and tycon
77 getPrimRepInfo CharRep = ("Char", charPrimTy, charPrimTyCon)
78 getPrimRepInfo IntRep = ("Int", intPrimTy, intPrimTyCon)
79 getPrimRepInfo WordRep = ("Word", wordPrimTy, wordPrimTyCon)
80 getPrimRepInfo AddrRep = ("Addr", addrPrimTy, addrPrimTyCon)
81 getPrimRepInfo FloatRep = ("Float", floatPrimTy, floatPrimTyCon)
82 getPrimRepInfo DoubleRep = ("Double", doublePrimTy, doublePrimTyCon)
85 %************************************************************************
87 \subsection[TysPrim-void]{The @Void#@ type}
89 %************************************************************************
91 Very similar to the @State#@ type.
93 voidPrimTy = applyTyCon voidPrimTyCon []
95 voidPrimTyCon = pcPrimTyCon voidPrimTyConKey SLIT("Void#") 0
99 %************************************************************************
101 \subsection[TysPrim-state]{The @State#@ type (and @_RealWorld@ types)}
103 %************************************************************************
106 mkStatePrimTy ty = applyTyCon statePrimTyCon [ty]
107 statePrimTyCon = pcPrimTyCon statePrimTyConKey SLIT("State#") 1
108 (\ [s_kind] -> VoidRep)
111 @_RealWorld@ is deeply magical. It {\em is primitive}, but it
112 {\em is not unboxed}.
114 realWorldTy = applyTyCon realWorldTyCon []
116 = mkDataTyCon realWorldTyConKey mkBoxedTypeKind full_name
119 [{-no data cons!-}] -- we tell you *nothing* about this guy
123 full_name = mkPreludeCoreName pRELUDE_BUILTIN 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.
131 %************************************************************************
133 \subsection[TysPrim-arrays]{The primitive array types}
135 %************************************************************************
138 arrayPrimTyCon = pcPrimTyCon arrayPrimTyConKey SLIT("Array#") 1
139 (\ [elt_kind] -> ArrayRep)
141 byteArrayPrimTyCon = pcPrimTyCon byteArrayPrimTyConKey SLIT("ByteArray#") 0
142 (\ [] -> ByteArrayRep)
144 mutableArrayPrimTyCon = pcPrimTyCon mutableArrayPrimTyConKey SLIT("MutableArray#") 2
145 (\ [s_kind, elt_kind] -> ArrayRep)
147 mutableByteArrayPrimTyCon = pcPrimTyCon mutableByteArrayPrimTyConKey SLIT("MutableByteArray#") 1
148 (\ [s_kind] -> ByteArrayRep)
150 mkArrayPrimTy elt = applyTyCon arrayPrimTyCon [elt]
151 byteArrayPrimTy = applyTyCon byteArrayPrimTyCon []
152 mkMutableArrayPrimTy s elt = applyTyCon mutableArrayPrimTyCon [s, elt]
153 mkMutableByteArrayPrimTy s = applyTyCon mutableByteArrayPrimTyCon [s]
156 %************************************************************************
158 \subsection[TysPrim-synch-var]{The synchronizing variable type}
160 %************************************************************************
163 synchVarPrimTyCon = pcPrimTyCon synchVarPrimTyConKey SLIT("SynchVar#") 2
164 (\ [s_kind, elt_kind] -> PtrRep)
166 mkSynchVarPrimTy s elt = applyTyCon synchVarPrimTyCon [s, elt]
169 %************************************************************************
171 \subsection[TysPrim-stable-ptrs]{The stable-pointer type}
173 %************************************************************************
176 stablePtrPrimTyCon = pcPrimTyCon stablePtrPrimTyConKey SLIT("StablePtr#") 1
177 (\ [elt_kind] -> StablePtrRep)
179 mkStablePtrPrimTy ty = applyTyCon stablePtrPrimTyCon [ty]
182 %************************************************************************
184 \subsection[TysPrim-malloc-ptrs]{The ``malloc''-pointer type}
186 %************************************************************************
188 ``Malloc'' pointers provide a mechanism which will let Haskell's
189 garbage collector communicate with a {\em simple\/} garbage collector
190 in the IO world (probably \tr{malloc}, hence the name).We want Haskell
191 to be able to hold onto references to objects in the IO world and for
192 Haskell's garbage collector to tell the IO world when these references
193 become garbage. We are not aiming to provide a mechanism that could
194 talk to a sophisticated garbage collector such as that provided by a
195 LISP system (with a correspondingly complex interface); in particular,
196 we shall ignore the danger of circular structures spread across the
199 There are no primitive operations on @CHeapPtr#@s (although equality
200 could possibly be added?)
203 mallocPtrPrimTyCon = pcPrimTyCon mallocPtrPrimTyConKey SLIT("MallocPtr#") 0
204 (\ [] -> MallocPtrRep)