[project @ 2000-07-11 16:24:57 by simonmar]
[ghc-hetmet.git] / ghc / compiler / prelude / TysWiredIn.lhs
1 %
2 % (c) The GRASP Project, Glasgow University, 1994-1998
3 %
4 \section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types}
5
6 This module is about types that can be defined in Haskell, but which
7 must be wired into the compiler nonetheless.
8
9 This module tracks the ``state interface'' document, ``GHC prelude:
10 types and operations.''
11
12 \begin{code}
13 module TysWiredIn (
14         addrDataCon,
15         addrTy,
16         addrTyCon,
17         boolTy,
18         boolTyCon,
19         charDataCon,
20         charTy,
21         charTyCon,
22         consDataCon,
23         doubleDataCon,
24         doubleTy,
25         isDoubleTy,
26         doubleTyCon,
27         falseDataCon, falseDataConId,
28         floatDataCon,
29         floatTy,
30         isFloatTy,
31         floatTyCon,
32
33         intDataCon,
34         intTy,
35         intTyCon,
36         isIntTy,
37
38         integerTy,
39         integerTyCon,
40         smallIntegerDataCon,
41         largeIntegerDataCon,
42         isIntegerTy,
43
44         listTyCon,
45
46         mkListTy,
47         nilDataCon,
48
49         -- tuples
50         mkTupleTy,
51         tupleTyCon, tupleCon, 
52         unitTyCon, unitDataConId, pairTyCon, 
53         unboxedSingletonTyCon, unboxedSingletonDataCon,
54         unboxedPairTyCon, unboxedPairDataCon,
55
56         stablePtrTyCon,
57         stringTy,
58         trueDataCon, trueDataConId,
59         unitTy,
60         voidTy,
61         wordDataCon,
62         wordTy,
63         wordTyCon,
64
65         isFFIArgumentTy,  -- :: Bool -> Type -> Bool
66         isFFIResultTy,    -- :: Type -> Bool
67         isFFIExternalTy,  -- :: Type -> Bool
68         isAddrTy,         -- :: Type -> Bool
69         isForeignObjTy    -- :: Type -> Bool
70
71     ) where
72
73 #include "HsVersions.h"
74
75 import {-# SOURCE #-} MkId( mkDataConId, mkDataConWrapId )
76
77 -- friends:
78 import PrelNames
79 import TysPrim
80
81 -- others:
82 import Constants        ( mAX_TUPLE_SIZE )
83 import Module           ( Module, mkPrelModule )
84 import Name             ( mkWiredInTyConName, mkWiredInIdName, mkSrcOccFS, mkWorkerOcc, dataName )
85 import DataCon          ( DataCon, StrictnessMark(..),  mkDataCon, dataConId )
86 import Var              ( TyVar, tyVarKind )
87 import TyCon            ( TyCon, AlgTyConFlavour(..), ArgVrcs, tyConDataCons,
88                           mkAlgTyCon, mkSynTyCon, mkTupleTyCon, isUnLiftedTyCon
89                         )
90 import BasicTypes       ( Arity, RecFlag(..), Boxity(..), isBoxed )
91 import Type             ( Type, mkTyConTy, mkTyConApp, mkSigmaTy, mkTyVarTys, 
92                           mkArrowKinds, boxedTypeKind, unboxedTypeKind,
93                           mkFunTy, mkFunTys,
94                           splitTyConApp_maybe, repType,
95                           TauType, ClassContext )
96 import Unique
97 import CmdLineOpts      ( opt_GlasgowExts )
98 import Array
99
100 alpha_tyvar       = [alphaTyVar]
101 alpha_ty          = [alphaTy]
102 alpha_beta_tyvars = [alphaTyVar, betaTyVar]
103
104 pcRecDataTyCon, pcNonRecDataTyCon
105         :: Unique{-TyConKey-} -> Module -> FAST_STRING
106         -> [TyVar] -> ArgVrcs -> [DataCon] -> TyCon
107
108 pcRecDataTyCon    = pcTyCon DataTyCon Recursive
109 pcNonRecDataTyCon = pcTyCon DataTyCon NonRecursive
110
111 pcTyCon new_or_data is_rec key mod str tyvars argvrcs cons
112   = tycon
113   where
114     tycon = mkAlgTyCon name kind 
115                 tyvars 
116                 []              -- No context
117                 argvrcs
118                 cons
119                 (length cons)
120                 []              -- No derivings
121                 new_or_data
122                 is_rec
123
124     name = mkWiredInTyConName key mod str tycon
125     kind = mkArrowKinds (map tyVarKind tyvars) boxedTypeKind
126
127 pcSynTyCon key mod str kind arity tyvars expansion argvrcs  -- this fun never used!
128   = tycon
129   where
130     tycon = mkSynTyCon name kind arity tyvars expansion argvrcs
131     name  = mkWiredInTyConName key mod str tycon
132
133 pcDataCon :: Unique{-DataConKey-} -> Module -> FAST_STRING
134           -> [TyVar] -> ClassContext -> [TauType] -> TyCon -> DataCon
135 -- The unique is the first of two free uniques;
136 -- the first is used for the datacon itself and the worker; 
137 -- the second is used for the wrapper.
138 pcDataCon wrap_key mod str tyvars context arg_tys tycon
139   = data_con
140   where
141     data_con = mkDataCon wrap_name 
142                 [ NotMarkedStrict | a <- arg_tys ]
143                 [ {- no labelled fields -} ]
144                 tyvars context [] [] arg_tys tycon work_id wrap_id
145
146     work_occ  = mkWorkerOcc wrap_occ
147     work_key  = incrUnique wrap_key
148     work_name = mkWiredInIdName work_key mod work_occ work_id
149     work_id   = mkDataConId work_name data_con
150     
151     wrap_occ  = mkSrcOccFS dataName str
152     wrap_name = mkWiredInIdName wrap_key mod wrap_occ wrap_id
153     wrap_id   = mkDataConWrapId data_con
154 \end{code}
155
156
157 %************************************************************************
158 %*                                                                      *
159 \subsection[TysWiredIn-tuples]{The tuple types}
160 %*                                                                      *
161 %************************************************************************
162
163 \begin{code}
164 tupleTyCon :: Boxity -> Arity -> TyCon
165 tupleTyCon boxity i | i > mAX_TUPLE_SIZE = fst (mk_tuple boxity i)      -- Build one specially
166 tupleTyCon Boxed   i = fst (boxedTupleArr   ! i)
167 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
168
169 tupleCon :: Boxity -> Arity -> DataCon
170 tupleCon boxity i | i > mAX_TUPLE_SIZE = snd (mk_tuple boxity i)        -- Build one specially
171 tupleCon Boxed   i = snd (boxedTupleArr   ! i)
172 tupleCon Unboxed i = snd (unboxedTupleArr ! i)
173
174 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
175 boxedTupleArr   = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Boxed i)   | i <- [0..mAX_TUPLE_SIZE]]
176 unboxedTupleArr = array (0,mAX_TUPLE_SIZE) [(i,mk_tuple Unboxed i) | i <- [0..mAX_TUPLE_SIZE]]
177
178 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
179 mk_tuple boxity arity = (tycon, tuple_con)
180   where
181         tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con boxity
182         tc_name = mkWiredInTyConName tc_uniq mod name_str tycon
183         tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
184         res_kind | isBoxed boxity = boxedTypeKind
185                  | otherwise      = unboxedTypeKind
186
187         tyvars   | isBoxed boxity = take arity alphaTyVars
188                  | otherwise      = take arity openAlphaTyVars
189
190         tuple_con = pcDataCon dc_uniq mod name_str tyvars [] tyvar_tys tycon
191         tyvar_tys = mkTyVarTys tyvars
192         (mod_name, name_str) = mkTupNameStr boxity arity
193         tc_uniq   = mkTupleTyConUnique   boxity arity
194         dc_uniq   = mkTupleDataConUnique boxity arity
195         mod       = mkPrelModule mod_name
196
197 unitTyCon     = tupleTyCon Boxed 0
198 unitDataConId = dataConId (head (tyConDataCons unitTyCon))
199
200 pairTyCon = tupleTyCon Boxed 2
201
202 unboxedSingletonTyCon   = tupleTyCon Unboxed 1
203 unboxedSingletonDataCon = tupleCon   Unboxed 1
204
205 unboxedPairTyCon   = tupleTyCon Unboxed 2
206 unboxedPairDataCon = tupleCon   Unboxed 2
207 \end{code}
208
209 %************************************************************************
210 %*                                                                      *
211 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
212 %*                                                                      *
213 %************************************************************************
214
215 \begin{code}
216 -- The Void type is represented as a data type with no constructors
217 -- It's a built in type (i.e. there's no way to define it in Haskell;
218 --      the nearest would be
219 --
220 --              data Void =             -- No constructors!
221 --
222 -- ) It's boxed; there is only one value of this
223 -- type, namely "void", whose semantics is just bottom.
224 --
225 -- Haskell 98 drops the definition of a Void type, so we just 'simulate'
226 -- voidTy using ().
227 voidTy = unitTy
228 \end{code}
229
230
231 \begin{code}
232 charTy = mkTyConTy charTyCon
233
234 charTyCon = pcNonRecDataTyCon charTyConKey  pREL_BASE  SLIT("Char") [] [] [charDataCon]
235 charDataCon = pcDataCon charDataConKey pREL_BASE SLIT("C#") [] [] [charPrimTy] charTyCon
236
237 stringTy = mkListTy charTy -- convenience only
238 \end{code}
239
240 \begin{code}
241 intTy = mkTyConTy intTyCon 
242
243 intTyCon = pcNonRecDataTyCon intTyConKey pREL_BASE SLIT("Int") [] [] [intDataCon]
244 intDataCon = pcDataCon intDataConKey pREL_BASE SLIT("I#") [] [] [intPrimTy] intTyCon
245
246 isIntTy :: Type -> Bool
247 isIntTy = isTyCon intTyConKey
248 \end{code}
249
250 \begin{code}
251
252 wordTy = mkTyConTy wordTyCon
253
254 wordTyCon = pcNonRecDataTyCon wordTyConKey   pREL_ADDR SLIT("Word") [] [] [wordDataCon]
255 wordDataCon = pcDataCon wordDataConKey pREL_ADDR SLIT("W#") [] [] [wordPrimTy] wordTyCon
256 \end{code}
257
258 \begin{code}
259 addrTy = mkTyConTy addrTyCon
260
261 addrTyCon = pcNonRecDataTyCon addrTyConKey   pREL_ADDR SLIT("Addr") [] [] [addrDataCon]
262 addrDataCon = pcDataCon addrDataConKey pREL_ADDR SLIT("A#") [] [] [addrPrimTy] addrTyCon
263
264 isAddrTy :: Type -> Bool
265 isAddrTy = isTyCon addrTyConKey
266 \end{code}
267
268 \begin{code}
269 floatTy = mkTyConTy floatTyCon
270
271 floatTyCon = pcNonRecDataTyCon floatTyConKey pREL_FLOAT SLIT("Float") [] [] [floatDataCon]
272 floatDataCon = pcDataCon floatDataConKey pREL_FLOAT SLIT("F#") [] [] [floatPrimTy] floatTyCon
273
274 isFloatTy :: Type -> Bool
275 isFloatTy = isTyCon floatTyConKey
276 \end{code}
277
278 \begin{code}
279 doubleTy = mkTyConTy doubleTyCon
280
281 isDoubleTy :: Type -> Bool
282 isDoubleTy = isTyCon doubleTyConKey
283
284 doubleTyCon = pcNonRecDataTyCon doubleTyConKey pREL_FLOAT SLIT("Double") [] [] [doubleDataCon]
285 doubleDataCon = pcDataCon doubleDataConKey pREL_FLOAT SLIT("D#") [] [] [doublePrimTy] doubleTyCon
286 \end{code}
287
288 \begin{code}
289 stablePtrTyCon
290   = pcNonRecDataTyCon stablePtrTyConKey pREL_STABLE SLIT("StablePtr")
291         alpha_tyvar [(True,False)] [stablePtrDataCon]
292   where
293     stablePtrDataCon
294       = pcDataCon stablePtrDataConKey pREL_STABLE SLIT("StablePtr")
295             alpha_tyvar [] [mkStablePtrPrimTy alphaTy] stablePtrTyCon
296 \end{code}
297
298 \begin{code}
299 foreignObjTyCon
300   = pcNonRecDataTyCon foreignObjTyConKey pREL_IO_BASE SLIT("ForeignObj")
301         [] [] [foreignObjDataCon]
302   where
303     foreignObjDataCon
304       = pcDataCon foreignObjDataConKey pREL_IO_BASE SLIT("ForeignObj")
305             [] [] [foreignObjPrimTy] foreignObjTyCon
306
307 isForeignObjTy :: Type -> Bool
308 isForeignObjTy = isTyCon foreignObjTyConKey
309 \end{code}
310
311 %************************************************************************
312 %*                                                                      *
313 \subsection[TysWiredIn-Integer]{@Integer@ and its related ``pairing'' types}
314 %*                                                                      *
315 %************************************************************************
316
317 @Integer@ and its pals are not really primitive.  @Integer@ itself, first:
318 \begin{code}
319 integerTy :: Type
320 integerTy = mkTyConTy integerTyCon
321
322 integerTyCon = pcNonRecDataTyCon integerTyConKey pREL_NUM SLIT("Integer")
323                    [] [] [smallIntegerDataCon, largeIntegerDataCon]
324
325 smallIntegerDataCon = pcDataCon smallIntegerDataConKey pREL_NUM SLIT("S#")
326                 [] [] [intPrimTy] integerTyCon
327 largeIntegerDataCon = pcDataCon largeIntegerDataConKey pREL_NUM SLIT("J#")
328                 [] [] [intPrimTy, byteArrayPrimTy] integerTyCon
329
330
331 isIntegerTy :: Type -> Bool
332 isIntegerTy = isTyCon integerTyConKey
333 \end{code}
334
335
336 %************************************************************************
337 %*                                                                      *
338 \subsection[TysWiredIn-ext-type]{External types}
339 %*                                                                      *
340 %************************************************************************
341
342 The compiler's foreign function interface supports the passing of a
343 restricted set of types as arguments and results (the restricting factor
344 being the )
345
346 \begin{code}
347 isFFIArgumentTy :: Bool -> Type -> Bool
348 -- Checks for valid argument type for a 'foreign import'
349 isFFIArgumentTy is_safe ty = checkRepTyCon (legalOutgoingTyCon is_safe) ty
350
351 isFFIExternalTy :: Type -> Bool
352 -- Types that are allowed as arguments of a 'foreign export'
353 isFFIExternalTy ty = checkRepTyCon legalIncomingTyCon ty
354
355 isFFIResultTy :: Type -> Bool
356 -- Types that are allowed as a result of a 'foreign import' or of a 'foreign export'
357 -- Maybe we should distinguish between import and export, but 
358 -- here we just choose the more restrictive 'incoming' predicate
359 -- But we allow () as well
360 isFFIResultTy ty = checkRepTyCon (\tc -> tc == unitTyCon || legalIncomingTyCon tc) ty
361
362 checkRepTyCon :: (TyCon -> Bool) -> Type -> Bool
363         -- look through newtypes
364 checkRepTyCon check_tc ty = checkTyCon check_tc (repType ty)
365
366 checkTyCon :: (TyCon -> Bool) -> Type -> Bool
367 checkTyCon check_tc ty = case splitTyConApp_maybe ty of
368                                 Just (tycon, _) -> check_tc tycon
369                                 Nothing         -> False
370
371 isTyCon :: Unique -> Type -> Bool
372 isTyCon uniq ty = checkTyCon (\tc -> uniq == getUnique tc) ty
373 \end{code}
374
375 ----------------------------------------------
376 These chaps do the work; they are not exported
377 ----------------------------------------------
378
379 \begin{code}
380 legalIncomingTyCon :: TyCon -> Bool
381 -- It's illegal to return foreign objects and (mutable)
382 -- bytearrays from a _ccall_ / foreign declaration
383 -- (or be passed them as arguments in foreign exported functions).
384 legalIncomingTyCon tc
385   | getUnique tc `elem` [ foreignObjTyConKey, byteArrayTyConKey, mutableByteArrayTyConKey ] 
386   = False
387   | otherwise
388   = marshalableTyCon tc
389
390 legalOutgoingTyCon :: Bool -> TyCon -> Bool
391 -- Checks validity of types going from Haskell -> external world
392 -- The boolean is true for a 'safe' call (when we don't want to
393 -- pass Haskell pointers to the world)
394 legalOutgoingTyCon be_safe tc
395   | be_safe && getUnique tc `elem` [byteArrayTyConKey, mutableByteArrayTyConKey]
396   = False
397   | otherwise
398   = marshalableTyCon tc
399
400 marshalableTyCon tc
401   =  (opt_GlasgowExts && isUnLiftedTyCon tc)
402   || getUnique tc `elem` [ intTyConKey, int8TyConKey, int16TyConKey, int32TyConKey, int64TyConKey
403                          , wordTyConKey, word8TyConKey, word16TyConKey, word32TyConKey, word64TyConKey
404                          , floatTyConKey, doubleTyConKey
405                          , addrTyConKey, charTyConKey, foreignObjTyConKey
406                          , stablePtrTyConKey
407                          , byteArrayTyConKey, mutableByteArrayTyConKey
408                          , boolTyConKey
409                          ]
410 \end{code}
411
412
413 %************************************************************************
414 %*                                                                      *
415 \subsection[TysWiredIn-Bool]{The @Bool@ type}
416 %*                                                                      *
417 %************************************************************************
418
419 An ordinary enumeration type, but deeply wired in.  There are no
420 magical operations on @Bool@ (just the regular Prelude code).
421
422 {\em BEGIN IDLE SPECULATION BY SIMON}
423
424 This is not the only way to encode @Bool@.  A more obvious coding makes
425 @Bool@ just a boxed up version of @Bool#@, like this:
426 \begin{verbatim}
427 type Bool# = Int#
428 data Bool = MkBool Bool#
429 \end{verbatim}
430
431 Unfortunately, this doesn't correspond to what the Report says @Bool@
432 looks like!  Furthermore, we get slightly less efficient code (I
433 think) with this coding. @gtInt@ would look like this:
434
435 \begin{verbatim}
436 gtInt :: Int -> Int -> Bool
437 gtInt x y = case x of I# x# ->
438             case y of I# y# ->
439             case (gtIntPrim x# y#) of
440                 b# -> MkBool b#
441 \end{verbatim}
442
443 Notice that the result of the @gtIntPrim@ comparison has to be turned
444 into an integer (here called @b#@), and returned in a @MkBool@ box.
445
446 The @if@ expression would compile to this:
447 \begin{verbatim}
448 case (gtInt x y) of
449   MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
450 \end{verbatim}
451
452 I think this code is a little less efficient than the previous code,
453 but I'm not certain.  At all events, corresponding with the Report is
454 important.  The interesting thing is that the language is expressive
455 enough to describe more than one alternative; and that a type doesn't
456 necessarily need to be a straightforwardly boxed version of its
457 primitive counterpart.
458
459 {\em END IDLE SPECULATION BY SIMON}
460
461 \begin{code}
462 boolTy = mkTyConTy boolTyCon
463
464 boolTyCon = pcTyCon EnumTyCon NonRecursive boolTyConKey 
465                     pREL_BASE SLIT("Bool") [] [] [falseDataCon, trueDataCon]
466
467 falseDataCon = pcDataCon falseDataConKey pREL_BASE SLIT("False") [] [] [] boolTyCon
468 trueDataCon  = pcDataCon trueDataConKey  pREL_BASE SLIT("True")  [] [] [] boolTyCon
469
470 falseDataConId = dataConId falseDataCon
471 trueDataConId  = dataConId trueDataCon
472 \end{code}
473
474 %************************************************************************
475 %*                                                                      *
476 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
477 %*                                                                      *
478 %************************************************************************
479
480 Special syntax, deeply wired in, but otherwise an ordinary algebraic
481 data types:
482 \begin{verbatim}
483 data [] a = [] | a : (List a)
484 data () = ()
485 data (,) a b = (,,) a b
486 ...
487 \end{verbatim}
488
489 \begin{code}
490 mkListTy :: Type -> Type
491 mkListTy ty = mkTyConApp listTyCon [ty]
492
493 alphaListTy = mkSigmaTy alpha_tyvar [] (mkTyConApp listTyCon alpha_ty)
494
495 listTyCon = pcRecDataTyCon listTyConKey pREL_BASE SLIT("[]") 
496                         alpha_tyvar [(True,False)] [nilDataCon, consDataCon]
497
498 nilDataCon  = pcDataCon nilDataConKey  pREL_BASE SLIT("[]") alpha_tyvar [] [] listTyCon
499 consDataCon = pcDataCon consDataConKey pREL_BASE SLIT(":")
500                 alpha_tyvar [] [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
501 -- Interesting: polymorphic recursion would help here.
502 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
503 -- gets the over-specific type (Type -> Type)
504 \end{code}
505
506 %************************************************************************
507 %*                                                                      *
508 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
509 %*                                                                      *
510 %************************************************************************
511
512 The tuple types are definitely magic, because they form an infinite
513 family.
514
515 \begin{itemize}
516 \item
517 They have a special family of type constructors, of type @TyCon@
518 These contain the tycon arity, but don't require a Unique.
519
520 \item
521 They have a special family of constructors, of type
522 @Id@. Again these contain their arity but don't need a Unique.
523
524 \item
525 There should be a magic way of generating the info tables and
526 entry code for all tuples.
527
528 But at the moment we just compile a Haskell source
529 file\srcloc{lib/prelude/...} containing declarations like:
530 \begin{verbatim}
531 data Tuple0             = Tup0
532 data Tuple2  a b        = Tup2  a b
533 data Tuple3  a b c      = Tup3  a b c
534 data Tuple4  a b c d    = Tup4  a b c d
535 ...
536 \end{verbatim}
537 The print-names associated with the magic @Id@s for tuple constructors
538 ``just happen'' to be the same as those generated by these
539 declarations.
540
541 \item
542 The instance environment should have a magic way to know
543 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
544 so on. \ToDo{Not implemented yet.}
545
546 \item
547 There should also be a way to generate the appropriate code for each
548 of these instances, but (like the info tables and entry code) it is
549 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
550 \end{itemize}
551
552 \begin{code}
553 mkTupleTy :: Boxity -> Int -> [Type] -> Type
554 mkTupleTy boxity arity tys = mkTyConApp (tupleTyCon boxity arity) tys
555
556 unitTy    = mkTupleTy Boxed 0 []
557 \end{code}