API Annotations tweaks.
[ghc.git] / compiler / prelude / TysWiredIn.hs
1 {-
2 (c) The GRASP Project, Glasgow University, 1994-1998
3
4 \section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types}
5 -}
6
7 {-# LANGUAGE CPP #-}
8
9 -- | This module is about types that can be defined in Haskell, but which
10 -- must be wired into the compiler nonetheless. C.f module TysPrim
11 module TysWiredIn (
12 -- * All wired in things
13 wiredInTyCons, isBuiltInOcc_maybe,
14
15 -- * Bool
16 boolTy, boolTyCon, boolTyCon_RDR, boolTyConName,
17 trueDataCon, trueDataConId, true_RDR,
18 falseDataCon, falseDataConId, false_RDR,
19 promotedBoolTyCon, promotedFalseDataCon, promotedTrueDataCon,
20
21 -- * Ordering
22 ltDataCon, ltDataConId,
23 eqDataCon, eqDataConId,
24 gtDataCon, gtDataConId,
25 promotedOrderingTyCon,
26 promotedLTDataCon, promotedEQDataCon, promotedGTDataCon,
27
28 -- * Char
29 charTyCon, charDataCon, charTyCon_RDR,
30 charTy, stringTy, charTyConName,
31
32 -- * Double
33 doubleTyCon, doubleDataCon, doubleTy, doubleTyConName,
34
35 -- * Float
36 floatTyCon, floatDataCon, floatTy, floatTyConName,
37
38 -- * Int
39 intTyCon, intDataCon, intTyCon_RDR, intDataCon_RDR, intTyConName,
40 intTy,
41
42 -- * Word
43 wordTyCon, wordDataCon, wordTyConName, wordTy,
44
45 -- * List
46 listTyCon, nilDataCon, nilDataConName, consDataCon, consDataConName,
47 listTyCon_RDR, consDataCon_RDR, listTyConName,
48 mkListTy, mkPromotedListTy,
49
50 -- * Tuples
51 mkTupleTy, mkBoxedTupleTy,
52 tupleTyCon, tupleCon,
53 promotedTupleTyCon, promotedTupleDataCon,
54 unitTyCon, unitDataCon, unitDataConId, pairTyCon,
55 unboxedUnitTyCon, unboxedUnitDataCon,
56 unboxedSingletonTyCon, unboxedSingletonDataCon,
57 unboxedPairTyCon, unboxedPairDataCon,
58
59 -- * Unit
60 unitTy,
61
62 -- * Kinds
63 typeNatKindCon, typeNatKind, typeSymbolKindCon, typeSymbolKind,
64
65 -- * Parallel arrays
66 mkPArrTy,
67 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
68 parrTyCon_RDR, parrTyConName,
69
70 -- * Equality predicates
71 eqTyCon_RDR, eqTyCon, eqTyConName, eqBoxDataCon,
72 coercibleTyCon, coercibleDataCon, coercibleClass,
73
74 mkWiredInTyConName -- This is used in TcTypeNats to define the
75 -- built-in functions for evaluation.
76 ) where
77
78 #include "HsVersions.h"
79
80 import {-# SOURCE #-} MkId( mkDataConWorkId )
81
82 -- friends:
83 import PrelNames
84 import TysPrim
85
86 -- others:
87 import Constants ( mAX_TUPLE_SIZE )
88 import Module ( Module )
89 import Type ( mkTyConApp )
90 import DataCon
91 import ConLike
92 import Var
93 import TyCon
94 import Class ( Class, mkClass )
95 import TypeRep
96 import RdrName
97 import Name
98 import BasicTypes ( TupleSort(..), tupleSortBoxity,
99 Arity, RecFlag(..), Boxity(..) )
100 import ForeignCall
101 import Unique ( incrUnique, mkTupleTyConUnique,
102 mkTupleDataConUnique, mkPArrDataConUnique )
103 import Data.Array
104 import FastString
105 import Outputable
106 import Util
107 import BooleanFormula ( mkAnd )
108
109 alpha_tyvar :: [TyVar]
110 alpha_tyvar = [alphaTyVar]
111
112 alpha_ty :: [Type]
113 alpha_ty = [alphaTy]
114
115 {-
116 ************************************************************************
117 * *
118 \subsection{Wired in type constructors}
119 * *
120 ************************************************************************
121
122 If you change which things are wired in, make sure you change their
123 names in PrelNames, so they use wTcQual, wDataQual, etc
124 -}
125
126 -- This list is used only to define PrelInfo.wiredInThings. That in turn
127 -- is used to initialise the name environment carried around by the renamer.
128 -- This means that if we look up the name of a TyCon (or its implicit binders)
129 -- that occurs in this list that name will be assigned the wired-in key we
130 -- define here.
131 --
132 -- Because of their infinite nature, this list excludes tuples, Any and implicit
133 -- parameter TyCons. Instead, we have a hack in lookupOrigNameCache to deal with
134 -- these names.
135 --
136 -- See also Note [Known-key names]
137 wiredInTyCons :: [TyCon]
138
139 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
140 -- it's defined in GHC.Base, and there's only
141 -- one of it. We put it in wiredInTyCons so
142 -- that it'll pre-populate the name cache, so
143 -- the special case in lookupOrigNameCache
144 -- doesn't need to look out for it
145 , boolTyCon
146 , charTyCon
147 , doubleTyCon
148 , floatTyCon
149 , intTyCon
150 , wordTyCon
151 , listTyCon
152 , parrTyCon
153 , eqTyCon
154 , coercibleTyCon
155 , typeNatKindCon
156 , typeSymbolKindCon
157 ]
158
159 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
160 mkWiredInTyConName built_in modu fs unique tycon
161 = mkWiredInName modu (mkTcOccFS fs) unique
162 (ATyCon tycon) -- Relevant TyCon
163 built_in
164
165 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
166 mkWiredInDataConName built_in modu fs unique datacon
167 = mkWiredInName modu (mkDataOccFS fs) unique
168 (AConLike (RealDataCon datacon)) -- Relevant DataCon
169 built_in
170
171 -- See Note [Kind-changing of (~) and Coercible]
172 eqTyConName, eqBoxDataConName :: Name
173 eqTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "~") eqTyConKey eqTyCon
174 eqBoxDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "Eq#") eqBoxDataConKey eqBoxDataCon
175
176 -- See Note [Kind-changing of (~) and Coercible]
177 coercibleTyConName, coercibleDataConName :: Name
178 coercibleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Coercible") coercibleTyConKey coercibleTyCon
179 coercibleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "MkCoercible") coercibleDataConKey coercibleDataCon
180
181 charTyConName, charDataConName, intTyConName, intDataConName :: Name
182 charTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Char") charTyConKey charTyCon
183 charDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "C#") charDataConKey charDataCon
184 intTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Int") intTyConKey intTyCon
185 intDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "I#") intDataConKey intDataCon
186
187 boolTyConName, falseDataConName, trueDataConName :: Name
188 boolTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Bool") boolTyConKey boolTyCon
189 falseDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "False") falseDataConKey falseDataCon
190 trueDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "True") trueDataConKey trueDataCon
191
192 listTyConName, nilDataConName, consDataConName :: Name
193 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "[]") listTyConKey listTyCon
194 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit "[]") nilDataConKey nilDataCon
195 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit ":") consDataConKey consDataCon
196
197 wordTyConName, wordDataConName, floatTyConName, floatDataConName, doubleTyConName, doubleDataConName :: Name
198 wordTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Word") wordTyConKey wordTyCon
199 wordDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "W#") wordDataConKey wordDataCon
200 floatTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Float") floatTyConKey floatTyCon
201 floatDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "F#") floatDataConKey floatDataCon
202 doubleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Double") doubleTyConKey doubleTyCon
203 doubleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "D#") doubleDataConKey doubleDataCon
204
205 -- Kinds
206 typeNatKindConName, typeSymbolKindConName :: Name
207 typeNatKindConName = mkWiredInTyConName UserSyntax gHC_TYPELITS (fsLit "Nat") typeNatKindConNameKey typeNatKindCon
208 typeSymbolKindConName = mkWiredInTyConName UserSyntax gHC_TYPELITS (fsLit "Symbol") typeSymbolKindConNameKey typeSymbolKindCon
209
210 parrTyConName, parrDataConName :: Name
211 parrTyConName = mkWiredInTyConName BuiltInSyntax
212 gHC_PARR' (fsLit "[::]") parrTyConKey parrTyCon
213 parrDataConName = mkWiredInDataConName UserSyntax
214 gHC_PARR' (fsLit "PArr") parrDataConKey parrDataCon
215
216 boolTyCon_RDR, false_RDR, true_RDR, intTyCon_RDR, charTyCon_RDR,
217 intDataCon_RDR, listTyCon_RDR, consDataCon_RDR, parrTyCon_RDR, eqTyCon_RDR :: RdrName
218 boolTyCon_RDR = nameRdrName boolTyConName
219 false_RDR = nameRdrName falseDataConName
220 true_RDR = nameRdrName trueDataConName
221 intTyCon_RDR = nameRdrName intTyConName
222 charTyCon_RDR = nameRdrName charTyConName
223 intDataCon_RDR = nameRdrName intDataConName
224 listTyCon_RDR = nameRdrName listTyConName
225 consDataCon_RDR = nameRdrName consDataConName
226 parrTyCon_RDR = nameRdrName parrTyConName
227 eqTyCon_RDR = nameRdrName eqTyConName
228
229 {-
230 ************************************************************************
231 * *
232 \subsection{mkWiredInTyCon}
233 * *
234 ************************************************************************
235 -}
236
237 pcNonRecDataTyCon :: Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon
238 -- Not an enumeration, not promotable
239 pcNonRecDataTyCon = pcTyCon False NonRecursive False
240
241 -- This function assumes that the types it creates have all parameters at
242 -- Representational role!
243 pcTyCon :: Bool -> RecFlag -> Bool -> Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon
244 pcTyCon is_enum is_rec is_prom name cType tyvars cons
245 = buildAlgTyCon name
246 tyvars
247 (map (const Representational) tyvars)
248 cType
249 [] -- No stupid theta
250 (DataTyCon cons is_enum)
251 is_rec
252 is_prom
253 False -- Not in GADT syntax
254 NoParentTyCon
255
256 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
257 pcDataCon = pcDataConWithFixity False
258
259 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
260 pcDataConWithFixity infx n = pcDataConWithFixity' infx n (incrUnique (nameUnique n))
261 -- The Name's unique is the first of two free uniques;
262 -- the first is used for the datacon itself,
263 -- the second is used for the "worker name"
264 --
265 -- To support this the mkPreludeDataConUnique function "allocates"
266 -- one DataCon unique per pair of Ints.
267
268 pcDataConWithFixity' :: Bool -> Name -> Unique -> [TyVar] -> [Type] -> TyCon -> DataCon
269 -- The Name should be in the DataName name space; it's the name
270 -- of the DataCon itself.
271
272 pcDataConWithFixity' declared_infix dc_name wrk_key tyvars arg_tys tycon
273 = data_con
274 where
275 data_con = mkDataCon dc_name declared_infix
276 (map (const HsNoBang) arg_tys)
277 [] -- No labelled fields
278 tyvars
279 [] -- No existential type variables
280 [] -- No equality spec
281 [] -- No theta
282 arg_tys (mkTyConApp tycon (mkTyVarTys tyvars))
283 tycon
284 [] -- No stupid theta
285 (mkDataConWorkId wrk_name data_con)
286 NoDataConRep -- Wired-in types are too simple to need wrappers
287
288 modu = ASSERT( isExternalName dc_name )
289 nameModule dc_name
290 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
291 wrk_name = mkWiredInName modu wrk_occ wrk_key
292 (AnId (dataConWorkId data_con)) UserSyntax
293
294 {-
295 ************************************************************************
296 * *
297 Kinds
298 * *
299 ************************************************************************
300 -}
301
302 typeNatKindCon, typeSymbolKindCon :: TyCon
303 -- data Nat
304 -- data Symbol
305 typeNatKindCon = pcTyCon False NonRecursive True typeNatKindConName Nothing [] []
306 typeSymbolKindCon = pcTyCon False NonRecursive True typeSymbolKindConName Nothing [] []
307
308 typeNatKind, typeSymbolKind :: Kind
309 typeNatKind = TyConApp (promoteTyCon typeNatKindCon) []
310 typeSymbolKind = TyConApp (promoteTyCon typeSymbolKindCon) []
311
312 {-
313 ************************************************************************
314 * *
315 Stuff for dealing with tuples
316 * *
317 ************************************************************************
318
319 Note [How tuples work] See also Note [Known-key names] in PrelNames
320 ~~~~~~~~~~~~~~~~~~~~~~
321 * There are three families of tuple TyCons and corresponding
322 DataCons, (boxed, unboxed, and constraint tuples), expressed by the
323 type BasicTypes.TupleSort.
324
325 * DataCons (and workers etc) for BoxedTuple and ConstraintTuple have
326 - distinct Uniques
327 - the same OccName
328 Using the same OccName means (hack!) that a single copy of the
329 runtime library code (info tables etc) works for both.
330
331 * When looking up an OccName in the original-name cache
332 (IfaceEnv.lookupOrigNameCache), we spot the tuple OccName to make sure
333 we get the right wired-in name. This guy can't tell the difference
334 betweeen BoxedTuple and ConstraintTuple (same OccName!), so tuples
335 are not serialised into interface files using OccNames at all.
336 -}
337
338 isBuiltInOcc_maybe :: OccName -> Maybe Name
339 -- Built in syntax isn't "in scope" so these OccNames
340 -- map to wired-in Names with BuiltInSyntax
341 isBuiltInOcc_maybe occ
342 = case occNameString occ of
343 "[]" -> choose_ns listTyCon nilDataCon
344 ":" -> Just consDataConName
345 "[::]" -> Just parrTyConName
346 "(##)" -> choose_ns unboxedUnitTyCon unboxedUnitDataCon
347 "()" -> choose_ns unitTyCon unitDataCon
348 '(':'#':',':rest -> parse_tuple UnboxedTuple 2 rest
349 '(':',':rest -> parse_tuple BoxedTuple 2 rest
350 _other -> Nothing
351 where
352 ns = occNameSpace occ
353
354 parse_tuple sort n rest
355 | (',' : rest2) <- rest = parse_tuple sort (n+1) rest2
356 | tail_matches sort rest = choose_ns (tupleTyCon sort n)
357 (tupleCon sort n)
358 | otherwise = Nothing
359
360 tail_matches BoxedTuple ")" = True
361 tail_matches UnboxedTuple "#)" = True
362 tail_matches _ _ = False
363
364 choose_ns tc dc
365 | isTcClsNameSpace ns = Just (getName tc)
366 | isDataConNameSpace ns = Just (getName dc)
367 | otherwise = Just (getName (dataConWorkId dc))
368
369 mkTupleOcc :: NameSpace -> TupleSort -> Arity -> OccName
370 mkTupleOcc ns sort ar = mkOccName ns str
371 where
372 -- No need to cache these, the caching is done in mk_tuple
373 str = case sort of
374 UnboxedTuple -> '(' : '#' : commas ++ "#)"
375 BoxedTuple -> '(' : commas ++ ")"
376 ConstraintTuple -> '(' : commas ++ ")"
377
378 commas = take (ar-1) (repeat ',')
379
380 -- Cute hack: we reuse the standard tuple OccNames (and hence code)
381 -- for fact tuples, but give them different Uniques so they are not equal.
382 --
383 -- You might think that this will go wrong because isBuiltInOcc_maybe won't
384 -- be able to tell the difference between boxed tuples and constraint tuples. BUT:
385 -- 1. Constraint tuples never occur directly in user code, so it doesn't matter
386 -- that we can't detect them in Orig OccNames originating from the user
387 -- programs (or those built by setRdrNameSpace used on an Exact tuple Name)
388 -- 2. Interface files have a special representation for tuple *occurrences*
389 -- in IfaceTyCons, their workers (in IfaceSyn) and their DataCons (in case
390 -- alternatives). Thus we don't rely on the OccName to figure out what kind
391 -- of tuple an occurrence was trying to use in these situations.
392 -- 3. We *don't* represent tuple data type declarations specially, so those
393 -- are still turned into wired-in names via isBuiltInOcc_maybe. But that's OK
394 -- because we don't actually need to declare constraint tuples thanks to this hack.
395 --
396 -- So basically any OccName like (,,) flowing to isBuiltInOcc_maybe will always
397 -- refer to the standard boxed tuple. Cool :-)
398
399
400 tupleTyCon :: TupleSort -> Arity -> TyCon
401 tupleTyCon sort i | i > mAX_TUPLE_SIZE = fst (mk_tuple sort i) -- Build one specially
402 tupleTyCon BoxedTuple i = fst (boxedTupleArr ! i)
403 tupleTyCon UnboxedTuple i = fst (unboxedTupleArr ! i)
404 tupleTyCon ConstraintTuple i = fst (factTupleArr ! i)
405
406 promotedTupleTyCon :: TupleSort -> Arity -> TyCon
407 promotedTupleTyCon sort i = promoteTyCon (tupleTyCon sort i)
408
409 promotedTupleDataCon :: TupleSort -> Arity -> TyCon
410 promotedTupleDataCon sort i = promoteDataCon (tupleCon sort i)
411
412 tupleCon :: TupleSort -> Arity -> DataCon
413 tupleCon sort i | i > mAX_TUPLE_SIZE = snd (mk_tuple sort i) -- Build one specially
414 tupleCon BoxedTuple i = snd (boxedTupleArr ! i)
415 tupleCon UnboxedTuple i = snd (unboxedTupleArr ! i)
416 tupleCon ConstraintTuple i = snd (factTupleArr ! i)
417
418 boxedTupleArr, unboxedTupleArr, factTupleArr :: Array Int (TyCon,DataCon)
419 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple BoxedTuple i | i <- [0..mAX_TUPLE_SIZE]]
420 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple UnboxedTuple i | i <- [0..mAX_TUPLE_SIZE]]
421 factTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple ConstraintTuple i | i <- [0..mAX_TUPLE_SIZE]]
422
423 mk_tuple :: TupleSort -> Int -> (TyCon,DataCon)
424 mk_tuple sort arity = (tycon, tuple_con)
425 where
426 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con sort prom_tc
427 prom_tc = case sort of
428 BoxedTuple -> Just (mkPromotedTyCon tycon (promoteKind tc_kind))
429 UnboxedTuple -> Nothing
430 ConstraintTuple -> Nothing
431
432 modu = mkTupleModule sort
433 tc_name = mkWiredInName modu (mkTupleOcc tcName sort arity) tc_uniq
434 (ATyCon tycon) BuiltInSyntax
435 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
436 res_kind = case sort of
437 BoxedTuple -> liftedTypeKind
438 UnboxedTuple -> unliftedTypeKind
439 ConstraintTuple -> constraintKind
440
441 tyvars = take arity $ case sort of
442 BoxedTuple -> alphaTyVars
443 UnboxedTuple -> openAlphaTyVars
444 ConstraintTuple -> tyVarList constraintKind
445
446 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
447 tyvar_tys = mkTyVarTys tyvars
448 dc_name = mkWiredInName modu (mkTupleOcc dataName sort arity) dc_uniq
449 (AConLike (RealDataCon tuple_con)) BuiltInSyntax
450 tc_uniq = mkTupleTyConUnique sort arity
451 dc_uniq = mkTupleDataConUnique sort arity
452
453 unitTyCon :: TyCon
454 unitTyCon = tupleTyCon BoxedTuple 0
455 unitDataCon :: DataCon
456 unitDataCon = head (tyConDataCons unitTyCon)
457 unitDataConId :: Id
458 unitDataConId = dataConWorkId unitDataCon
459
460 pairTyCon :: TyCon
461 pairTyCon = tupleTyCon BoxedTuple 2
462
463 unboxedUnitTyCon :: TyCon
464 unboxedUnitTyCon = tupleTyCon UnboxedTuple 0
465 unboxedUnitDataCon :: DataCon
466 unboxedUnitDataCon = tupleCon UnboxedTuple 0
467
468 unboxedSingletonTyCon :: TyCon
469 unboxedSingletonTyCon = tupleTyCon UnboxedTuple 1
470 unboxedSingletonDataCon :: DataCon
471 unboxedSingletonDataCon = tupleCon UnboxedTuple 1
472
473 unboxedPairTyCon :: TyCon
474 unboxedPairTyCon = tupleTyCon UnboxedTuple 2
475 unboxedPairDataCon :: DataCon
476 unboxedPairDataCon = tupleCon UnboxedTuple 2
477
478 {-
479 ************************************************************************
480 * *
481 \subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
482 * *
483 ************************************************************************
484 -}
485
486 eqTyCon :: TyCon
487 eqTyCon = mkAlgTyCon eqTyConName
488 (ForAllTy kv $ mkArrowKinds [k, k] constraintKind)
489 [kv, a, b]
490 [Nominal, Nominal, Nominal]
491 Nothing
492 [] -- No stupid theta
493 (DataTyCon [eqBoxDataCon] False)
494 NoParentTyCon
495 NonRecursive
496 False
497 Nothing -- No parent for constraint-kinded types
498 where
499 kv = kKiVar
500 k = mkTyVarTy kv
501 a:b:_ = tyVarList k
502
503 eqBoxDataCon :: DataCon
504 eqBoxDataCon = pcDataCon eqBoxDataConName args [TyConApp eqPrimTyCon (map mkTyVarTy args)] eqTyCon
505 where
506 kv = kKiVar
507 k = mkTyVarTy kv
508 a:b:_ = tyVarList k
509 args = [kv, a, b]
510
511
512 coercibleTyCon :: TyCon
513 coercibleTyCon = mkClassTyCon
514 coercibleTyConName kind tvs [Nominal, Representational, Representational]
515 rhs coercibleClass NonRecursive
516 where kind = (ForAllTy kv $ mkArrowKinds [k, k] constraintKind)
517 kv = kKiVar
518 k = mkTyVarTy kv
519 a:b:_ = tyVarList k
520 tvs = [kv, a, b]
521 rhs = DataTyCon [coercibleDataCon] False
522
523 coercibleDataCon :: DataCon
524 coercibleDataCon = pcDataCon coercibleDataConName args [TyConApp eqReprPrimTyCon (map mkTyVarTy args)] coercibleTyCon
525 where
526 kv = kKiVar
527 k = mkTyVarTy kv
528 a:b:_ = tyVarList k
529 args = [kv, a, b]
530
531 coercibleClass :: Class
532 coercibleClass = mkClass (tyConTyVars coercibleTyCon) [] [] [] [] [] (mkAnd []) coercibleTyCon
533
534 charTy :: Type
535 charTy = mkTyConTy charTyCon
536
537 charTyCon :: TyCon
538 charTyCon = pcNonRecDataTyCon charTyConName
539 (Just (CType "" Nothing (fsLit "HsChar")))
540 [] [charDataCon]
541 charDataCon :: DataCon
542 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
543
544 stringTy :: Type
545 stringTy = mkListTy charTy -- convenience only
546
547 intTy :: Type
548 intTy = mkTyConTy intTyCon
549
550 intTyCon :: TyCon
551 intTyCon = pcNonRecDataTyCon intTyConName
552 (Just (CType "" Nothing (fsLit "HsInt"))) []
553 [intDataCon]
554 intDataCon :: DataCon
555 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
556
557 wordTy :: Type
558 wordTy = mkTyConTy wordTyCon
559
560 wordTyCon :: TyCon
561 wordTyCon = pcNonRecDataTyCon wordTyConName
562 (Just (CType "" Nothing (fsLit "HsWord"))) []
563 [wordDataCon]
564 wordDataCon :: DataCon
565 wordDataCon = pcDataCon wordDataConName [] [wordPrimTy] wordTyCon
566
567 floatTy :: Type
568 floatTy = mkTyConTy floatTyCon
569
570 floatTyCon :: TyCon
571 floatTyCon = pcNonRecDataTyCon floatTyConName
572 (Just (CType "" Nothing (fsLit "HsFloat"))) []
573 [floatDataCon]
574 floatDataCon :: DataCon
575 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
576
577 doubleTy :: Type
578 doubleTy = mkTyConTy doubleTyCon
579
580 doubleTyCon :: TyCon
581 doubleTyCon = pcNonRecDataTyCon doubleTyConName
582 (Just (CType "" Nothing (fsLit "HsDouble"))) []
583 [doubleDataCon]
584
585 doubleDataCon :: DataCon
586 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
587
588 {-
589 ************************************************************************
590 * *
591 \subsection[TysWiredIn-Bool]{The @Bool@ type}
592 * *
593 ************************************************************************
594
595 An ordinary enumeration type, but deeply wired in. There are no
596 magical operations on @Bool@ (just the regular Prelude code).
597
598 {\em BEGIN IDLE SPECULATION BY SIMON}
599
600 This is not the only way to encode @Bool@. A more obvious coding makes
601 @Bool@ just a boxed up version of @Bool#@, like this:
602 \begin{verbatim}
603 type Bool# = Int#
604 data Bool = MkBool Bool#
605 \end{verbatim}
606
607 Unfortunately, this doesn't correspond to what the Report says @Bool@
608 looks like! Furthermore, we get slightly less efficient code (I
609 think) with this coding. @gtInt@ would look like this:
610
611 \begin{verbatim}
612 gtInt :: Int -> Int -> Bool
613 gtInt x y = case x of I# x# ->
614 case y of I# y# ->
615 case (gtIntPrim x# y#) of
616 b# -> MkBool b#
617 \end{verbatim}
618
619 Notice that the result of the @gtIntPrim@ comparison has to be turned
620 into an integer (here called @b#@), and returned in a @MkBool@ box.
621
622 The @if@ expression would compile to this:
623 \begin{verbatim}
624 case (gtInt x y) of
625 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
626 \end{verbatim}
627
628 I think this code is a little less efficient than the previous code,
629 but I'm not certain. At all events, corresponding with the Report is
630 important. The interesting thing is that the language is expressive
631 enough to describe more than one alternative; and that a type doesn't
632 necessarily need to be a straightforwardly boxed version of its
633 primitive counterpart.
634
635 {\em END IDLE SPECULATION BY SIMON}
636 -}
637
638 boolTy :: Type
639 boolTy = mkTyConTy boolTyCon
640
641 boolTyCon :: TyCon
642 boolTyCon = pcTyCon True NonRecursive True boolTyConName
643 (Just (CType "" Nothing (fsLit "HsBool")))
644 [] [falseDataCon, trueDataCon]
645
646 falseDataCon, trueDataCon :: DataCon
647 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
648 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
649
650 falseDataConId, trueDataConId :: Id
651 falseDataConId = dataConWorkId falseDataCon
652 trueDataConId = dataConWorkId trueDataCon
653
654 orderingTyCon :: TyCon
655 orderingTyCon = pcTyCon True NonRecursive True orderingTyConName Nothing
656 [] [ltDataCon, eqDataCon, gtDataCon]
657
658 ltDataCon, eqDataCon, gtDataCon :: DataCon
659 ltDataCon = pcDataCon ltDataConName [] [] orderingTyCon
660 eqDataCon = pcDataCon eqDataConName [] [] orderingTyCon
661 gtDataCon = pcDataCon gtDataConName [] [] orderingTyCon
662
663 ltDataConId, eqDataConId, gtDataConId :: Id
664 ltDataConId = dataConWorkId ltDataCon
665 eqDataConId = dataConWorkId eqDataCon
666 gtDataConId = dataConWorkId gtDataCon
667
668 {-
669 ************************************************************************
670 * *
671 \subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
672 * *
673 ************************************************************************
674
675 Special syntax, deeply wired in, but otherwise an ordinary algebraic
676 data types:
677 \begin{verbatim}
678 data [] a = [] | a : (List a)
679 data () = ()
680 data (,) a b = (,,) a b
681 ...
682 \end{verbatim}
683 -}
684
685 mkListTy :: Type -> Type
686 mkListTy ty = mkTyConApp listTyCon [ty]
687
688 listTyCon :: TyCon
689 listTyCon = pcTyCon False Recursive True
690 listTyConName Nothing alpha_tyvar [nilDataCon, consDataCon]
691
692 mkPromotedListTy :: Type -> Type
693 mkPromotedListTy ty = mkTyConApp promotedListTyCon [ty]
694
695 promotedListTyCon :: TyCon
696 promotedListTyCon = promoteTyCon listTyCon
697
698 nilDataCon :: DataCon
699 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
700
701 consDataCon :: DataCon
702 consDataCon = pcDataConWithFixity True {- Declared infix -}
703 consDataConName
704 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
705 -- Interesting: polymorphic recursion would help here.
706 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
707 -- gets the over-specific type (Type -> Type)
708
709 {-
710 ************************************************************************
711 * *
712 \subsection[TysWiredIn-Tuples]{The @Tuple@ types}
713 * *
714 ************************************************************************
715
716 The tuple types are definitely magic, because they form an infinite
717 family.
718
719 \begin{itemize}
720 \item
721 They have a special family of type constructors, of type @TyCon@
722 These contain the tycon arity, but don't require a Unique.
723
724 \item
725 They have a special family of constructors, of type
726 @Id@. Again these contain their arity but don't need a Unique.
727
728 \item
729 There should be a magic way of generating the info tables and
730 entry code for all tuples.
731
732 But at the moment we just compile a Haskell source
733 file\srcloc{lib/prelude/...} containing declarations like:
734 \begin{verbatim}
735 data Tuple0 = Tup0
736 data Tuple2 a b = Tup2 a b
737 data Tuple3 a b c = Tup3 a b c
738 data Tuple4 a b c d = Tup4 a b c d
739 ...
740 \end{verbatim}
741 The print-names associated with the magic @Id@s for tuple constructors
742 ``just happen'' to be the same as those generated by these
743 declarations.
744
745 \item
746 The instance environment should have a magic way to know
747 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
748 so on. \ToDo{Not implemented yet.}
749
750 \item
751 There should also be a way to generate the appropriate code for each
752 of these instances, but (like the info tables and entry code) it is
753 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
754 \end{itemize}
755 -}
756
757 mkTupleTy :: TupleSort -> [Type] -> Type
758 -- Special case for *boxed* 1-tuples, which are represented by the type itself
759 mkTupleTy sort [ty] | Boxed <- tupleSortBoxity sort = ty
760 mkTupleTy sort tys = mkTyConApp (tupleTyCon sort (length tys)) tys
761
762 -- | Build the type of a small tuple that holds the specified type of thing
763 mkBoxedTupleTy :: [Type] -> Type
764 mkBoxedTupleTy tys = mkTupleTy BoxedTuple tys
765
766 unitTy :: Type
767 unitTy = mkTupleTy BoxedTuple []
768
769 {-
770 ************************************************************************
771 * *
772 \subsection[TysWiredIn-PArr]{The @[::]@ type}
773 * *
774 ************************************************************************
775
776 Special syntax for parallel arrays needs some wired in definitions.
777 -}
778
779 -- | Construct a type representing the application of the parallel array constructor
780 mkPArrTy :: Type -> Type
781 mkPArrTy ty = mkTyConApp parrTyCon [ty]
782
783 -- | Represents the type constructor of parallel arrays
784 --
785 -- * This must match the definition in @PrelPArr@
786 --
787 -- NB: Although the constructor is given here, it will not be accessible in
788 -- user code as it is not in the environment of any compiled module except
789 -- @PrelPArr@.
790 --
791 parrTyCon :: TyCon
792 parrTyCon = pcNonRecDataTyCon parrTyConName Nothing alpha_tyvar [parrDataCon]
793
794 parrDataCon :: DataCon
795 parrDataCon = pcDataCon
796 parrDataConName
797 alpha_tyvar -- forall'ed type variables
798 [intTy, -- 1st argument: Int
799 mkTyConApp -- 2nd argument: Array# a
800 arrayPrimTyCon
801 alpha_ty]
802 parrTyCon
803
804 -- | Check whether a type constructor is the constructor for parallel arrays
805 isPArrTyCon :: TyCon -> Bool
806 isPArrTyCon tc = tyConName tc == parrTyConName
807
808 -- | Fake array constructors
809 --
810 -- * These constructors are never really used to represent array values;
811 -- however, they are very convenient during desugaring (and, in particular,
812 -- in the pattern matching compiler) to treat array pattern just like
813 -- yet another constructor pattern
814 --
815 parrFakeCon :: Arity -> DataCon
816 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
817 parrFakeCon i = parrFakeConArr!i
818
819 -- pre-defined set of constructors
820 --
821 parrFakeConArr :: Array Int DataCon
822 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
823 | i <- [0..mAX_TUPLE_SIZE]]
824
825 -- build a fake parallel array constructor for the given arity
826 --
827 mkPArrFakeCon :: Int -> DataCon
828 mkPArrFakeCon arity = data_con
829 where
830 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
831 tyvar = head alphaTyVars
832 tyvarTys = replicate arity $ mkTyVarTy tyvar
833 nameStr = mkFastString ("MkPArr" ++ show arity)
834 name = mkWiredInName gHC_PARR' (mkDataOccFS nameStr) unique
835 (AConLike (RealDataCon data_con)) UserSyntax
836 unique = mkPArrDataConUnique arity
837
838 -- | Checks whether a data constructor is a fake constructor for parallel arrays
839 isPArrFakeCon :: DataCon -> Bool
840 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)
841
842 -- Promoted Booleans
843
844 promotedBoolTyCon, promotedFalseDataCon, promotedTrueDataCon :: TyCon
845 promotedBoolTyCon = promoteTyCon boolTyCon
846 promotedTrueDataCon = promoteDataCon trueDataCon
847 promotedFalseDataCon = promoteDataCon falseDataCon
848
849 -- Promoted Ordering
850
851 promotedOrderingTyCon
852 , promotedLTDataCon
853 , promotedEQDataCon
854 , promotedGTDataCon
855 :: TyCon
856 promotedOrderingTyCon = promoteTyCon orderingTyCon
857 promotedLTDataCon = promoteDataCon ltDataCon
858 promotedEQDataCon = promoteDataCon eqDataCon
859 promotedGTDataCon = promoteDataCon gtDataCon