Add typed holes support in Template Haskell.
[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 -- * Word8
46 word8TyCon, word8DataCon, word8TyConName, word8Ty,
47
48 -- * List
49 listTyCon, listTyCon_RDR, listTyConName, listTyConKey,
50 nilDataCon, nilDataConName, nilDataConKey,
51 consDataCon_RDR, consDataCon, consDataConName,
52
53 mkListTy, mkPromotedListTy,
54
55 -- * Maybe
56 maybeTyCon, maybeTyConName,
57 nothingDataCon, nothingDataConName, justDataCon, justDataConName,
58
59 -- * Tuples
60 mkTupleTy, mkBoxedTupleTy,
61 tupleTyCon, tupleDataCon, tupleTyConName,
62 promotedTupleTyCon, promotedTupleDataCon,
63 unitTyCon, unitDataCon, unitDataConId, unitTy, unitTyConKey,
64 pairTyCon,
65 unboxedUnitTyCon, unboxedUnitDataCon,
66 unboxedSingletonTyCon, unboxedSingletonDataCon,
67 unboxedPairTyCon, unboxedPairDataCon,
68 cTupleTyConName, cTupleTyConNames, isCTupleTyConName,
69
70 -- * Kinds
71 typeNatKindCon, typeNatKind, typeSymbolKindCon, typeSymbolKind,
72
73 -- * Parallel arrays
74 mkPArrTy,
75 parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
76 parrTyCon_RDR, parrTyConName,
77
78 -- * Equality predicates
79 eqTyCon_RDR, eqTyCon, eqTyConName, eqBoxDataCon,
80 coercibleTyCon, coercibleDataCon, coercibleClass,
81
82 -- * Implicit Parameters
83 ipTyCon, ipDataCon, ipClass,
84
85 callStackTyCon,
86
87 mkWiredInTyConName -- This is used in TcTypeNats to define the
88 -- built-in functions for evaluation.
89 ) where
90
91 #include "HsVersions.h"
92
93 import {-# SOURCE #-} MkId( mkDataConWorkId )
94
95 -- friends:
96 import PrelNames
97 import TysPrim
98
99 -- others:
100 import CoAxiom
101 import Coercion
102 import Constants ( mAX_TUPLE_SIZE, mAX_CTUPLE_SIZE )
103 import Module ( Module )
104 import Type ( mkTyConApp )
105 import DataCon
106 import ConLike
107 import Var
108 import TyCon
109 import Class ( Class, mkClass )
110 import TypeRep
111 import RdrName
112 import Name
113 import NameSet ( NameSet, mkNameSet, elemNameSet )
114 import BasicTypes ( Arity, RecFlag(..), Boxity(..),
115 TupleSort(..) )
116 import ForeignCall
117 import SrcLoc ( noSrcSpan )
118 import Unique
119 import Data.Array
120 import FastString
121 import Outputable
122 import Util
123 import BooleanFormula ( mkAnd )
124
125 alpha_tyvar :: [TyVar]
126 alpha_tyvar = [alphaTyVar]
127
128 alpha_ty :: [Type]
129 alpha_ty = [alphaTy]
130
131 {-
132 ************************************************************************
133 * *
134 \subsection{Wired in type constructors}
135 * *
136 ************************************************************************
137
138 If you change which things are wired in, make sure you change their
139 names in PrelNames, so they use wTcQual, wDataQual, etc
140 -}
141
142 -- This list is used only to define PrelInfo.wiredInThings. That in turn
143 -- is used to initialise the name environment carried around by the renamer.
144 -- This means that if we look up the name of a TyCon (or its implicit binders)
145 -- that occurs in this list that name will be assigned the wired-in key we
146 -- define here.
147 --
148 -- Because of their infinite nature, this list excludes tuples, Any and implicit
149 -- parameter TyCons. Instead, we have a hack in lookupOrigNameCache to deal with
150 -- these names.
151 --
152 -- See also Note [Known-key names]
153 wiredInTyCons :: [TyCon]
154
155 wiredInTyCons = [ unitTyCon -- Not treated like other tuples, because
156 -- it's defined in GHC.Base, and there's only
157 -- one of it. We put it in wiredInTyCons so
158 -- that it'll pre-populate the name cache, so
159 -- the special case in lookupOrigNameCache
160 -- doesn't need to look out for it
161 , boolTyCon
162 , charTyCon
163 , doubleTyCon
164 , floatTyCon
165 , intTyCon
166 , wordTyCon
167 , word8TyCon
168 , listTyCon
169 , maybeTyCon
170 , parrTyCon
171 , eqTyCon
172 , coercibleTyCon
173 , typeNatKindCon
174 , typeSymbolKindCon
175 , ipTyCon
176 ]
177
178 mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
179 mkWiredInTyConName built_in modu fs unique tycon
180 = mkWiredInName modu (mkTcOccFS fs) unique
181 (ATyCon tycon) -- Relevant TyCon
182 built_in
183
184 mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
185 mkWiredInDataConName built_in modu fs unique datacon
186 = mkWiredInName modu (mkDataOccFS fs) unique
187 (AConLike (RealDataCon datacon)) -- Relevant DataCon
188 built_in
189
190 mkWiredInCoAxiomName :: BuiltInSyntax -> Module -> FastString -> Unique
191 -> CoAxiom Branched -> Name
192 mkWiredInCoAxiomName built_in modu fs unique ax
193 = mkWiredInName modu (mkTcOccFS fs) unique
194 (ACoAxiom ax) -- Relevant CoAxiom
195 built_in
196
197 -- See Note [Kind-changing of (~) and Coercible]
198 eqTyConName, eqBoxDataConName :: Name
199 eqTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "~") eqTyConKey eqTyCon
200 eqBoxDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "Eq#") eqBoxDataConKey eqBoxDataCon
201
202 -- See Note [Kind-changing of (~) and Coercible]
203 coercibleTyConName, coercibleDataConName :: Name
204 coercibleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Coercible") coercibleTyConKey coercibleTyCon
205 coercibleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "MkCoercible") coercibleDataConKey coercibleDataCon
206
207 charTyConName, charDataConName, intTyConName, intDataConName :: Name
208 charTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Char") charTyConKey charTyCon
209 charDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "C#") charDataConKey charDataCon
210 intTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Int") intTyConKey intTyCon
211 intDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "I#") intDataConKey intDataCon
212
213 boolTyConName, falseDataConName, trueDataConName :: Name
214 boolTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Bool") boolTyConKey boolTyCon
215 falseDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "False") falseDataConKey falseDataCon
216 trueDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "True") trueDataConKey trueDataCon
217
218 listTyConName, nilDataConName, consDataConName :: Name
219 listTyConName = mkWiredInTyConName BuiltInSyntax gHC_TYPES (fsLit "[]") listTyConKey listTyCon
220 nilDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit "[]") nilDataConKey nilDataCon
221 consDataConName = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit ":") consDataConKey consDataCon
222
223 maybeTyConName, nothingDataConName, justDataConName :: Name
224 maybeTyConName = mkWiredInTyConName UserSyntax gHC_BASE (fsLit "Maybe")
225 maybeTyConKey maybeTyCon
226 nothingDataConName = mkWiredInDataConName UserSyntax gHC_BASE (fsLit "Nothing")
227 nothingDataConKey nothingDataCon
228 justDataConName = mkWiredInDataConName UserSyntax gHC_BASE (fsLit "Just")
229 justDataConKey justDataCon
230
231 wordTyConName, wordDataConName, word8TyConName, word8DataConName :: Name
232 wordTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Word") wordTyConKey wordTyCon
233 wordDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "W#") wordDataConKey wordDataCon
234 word8TyConName = mkWiredInTyConName UserSyntax gHC_WORD (fsLit "Word8") word8TyConKey word8TyCon
235 word8DataConName = mkWiredInDataConName UserSyntax gHC_WORD (fsLit "W8#") word8DataConKey word8DataCon
236
237 floatTyConName, floatDataConName, doubleTyConName, doubleDataConName :: Name
238 floatTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Float") floatTyConKey floatTyCon
239 floatDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "F#") floatDataConKey floatDataCon
240 doubleTyConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Double") doubleTyConKey doubleTyCon
241 doubleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "D#") doubleDataConKey doubleDataCon
242
243 -- Kinds
244 typeNatKindConName, typeSymbolKindConName :: Name
245 typeNatKindConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Nat") typeNatKindConNameKey typeNatKindCon
246 typeSymbolKindConName = mkWiredInTyConName UserSyntax gHC_TYPES (fsLit "Symbol") typeSymbolKindConNameKey typeSymbolKindCon
247
248 parrTyConName, parrDataConName :: Name
249 parrTyConName = mkWiredInTyConName BuiltInSyntax
250 gHC_PARR' (fsLit "[::]") parrTyConKey parrTyCon
251 parrDataConName = mkWiredInDataConName UserSyntax
252 gHC_PARR' (fsLit "PArr") parrDataConKey parrDataCon
253
254 boolTyCon_RDR, false_RDR, true_RDR, intTyCon_RDR, charTyCon_RDR,
255 intDataCon_RDR, listTyCon_RDR, consDataCon_RDR, parrTyCon_RDR, eqTyCon_RDR :: RdrName
256 boolTyCon_RDR = nameRdrName boolTyConName
257 false_RDR = nameRdrName falseDataConName
258 true_RDR = nameRdrName trueDataConName
259 intTyCon_RDR = nameRdrName intTyConName
260 charTyCon_RDR = nameRdrName charTyConName
261 intDataCon_RDR = nameRdrName intDataConName
262 listTyCon_RDR = nameRdrName listTyConName
263 consDataCon_RDR = nameRdrName consDataConName
264 parrTyCon_RDR = nameRdrName parrTyConName
265 eqTyCon_RDR = nameRdrName eqTyConName
266
267 {-
268 ************************************************************************
269 * *
270 \subsection{mkWiredInTyCon}
271 * *
272 ************************************************************************
273 -}
274
275 pcNonRecDataTyCon :: Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon
276 -- Not an enumeration, not promotable
277 pcNonRecDataTyCon = pcTyCon False NonRecursive False
278
279 -- This function assumes that the types it creates have all parameters at
280 -- Representational role!
281 pcTyCon :: Bool -> RecFlag -> Bool -> Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon
282 pcTyCon is_enum is_rec is_prom name cType tyvars cons
283 = buildAlgTyCon name
284 tyvars
285 (map (const Representational) tyvars)
286 cType
287 [] -- No stupid theta
288 (DataTyCon cons is_enum)
289 is_rec
290 is_prom
291 False -- Not in GADT syntax
292 NoParentTyCon
293
294 pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
295 pcDataCon = pcDataConWithFixity False
296
297 pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
298 pcDataConWithFixity infx n = pcDataConWithFixity' infx n (incrUnique (nameUnique n))
299 -- The Name's unique is the first of two free uniques;
300 -- the first is used for the datacon itself,
301 -- the second is used for the "worker name"
302 --
303 -- To support this the mkPreludeDataConUnique function "allocates"
304 -- one DataCon unique per pair of Ints.
305
306 pcDataConWithFixity' :: Bool -> Name -> Unique -> [TyVar] -> [Type] -> TyCon -> DataCon
307 -- The Name should be in the DataName name space; it's the name
308 -- of the DataCon itself.
309
310 pcDataConWithFixity' declared_infix dc_name wrk_key tyvars arg_tys tycon
311 = data_con
312 where
313 data_con = mkDataCon dc_name declared_infix
314 (map (const no_bang) arg_tys)
315 [] -- No labelled fields
316 tyvars
317 [] -- No existential type variables
318 [] -- No equality spec
319 [] -- No theta
320 arg_tys (mkTyConApp tycon (mkTyVarTys tyvars))
321 tycon
322 [] -- No stupid theta
323 (mkDataConWorkId wrk_name data_con)
324 NoDataConRep -- Wired-in types are too simple to need wrappers
325
326 no_bang = HsSrcBang Nothing NoSrcUnpack NoSrcStrict
327
328 modu = ASSERT( isExternalName dc_name )
329 nameModule dc_name
330 wrk_occ = mkDataConWorkerOcc (nameOccName dc_name)
331 wrk_name = mkWiredInName modu wrk_occ wrk_key
332 (AnId (dataConWorkId data_con)) UserSyntax
333
334 {-
335 ************************************************************************
336 * *
337 Kinds
338 * *
339 ************************************************************************
340 -}
341
342 typeNatKindCon, typeSymbolKindCon :: TyCon
343 -- data Nat
344 -- data Symbol
345 typeNatKindCon = pcTyCon False NonRecursive True typeNatKindConName Nothing [] []
346 typeSymbolKindCon = pcTyCon False NonRecursive True typeSymbolKindConName Nothing [] []
347
348 typeNatKind, typeSymbolKind :: Kind
349 typeNatKind = TyConApp (promoteTyCon typeNatKindCon) []
350 typeSymbolKind = TyConApp (promoteTyCon typeSymbolKindCon) []
351
352 {-
353 ************************************************************************
354 * *
355 Stuff for dealing with tuples
356 * *
357 ************************************************************************
358
359 Note [How tuples work] See also Note [Known-key names] in PrelNames
360 ~~~~~~~~~~~~~~~~~~~~~~
361 * There are three families of tuple TyCons and corresponding
362 DataCons, expressed by the type BasicTypes.TupleSort:
363 data TupleSort = BoxedTuple | UnboxedTuple | ConstraintTuple
364
365 * All three families are AlgTyCons, whose AlgTyConRhs is TupleTyCon
366
367 * BoxedTuples
368 - A wired-in type
369 - Data type declarations in GHC.Tuple
370 - The data constructors really have an info table
371
372 * UnboxedTuples
373 - A wired-in type
374 - Have a pretend DataCon, defined in GHC.Prim,
375 but no actual declaration and no info table
376
377 * ConstraintTuples
378 - Are known-key rather than wired-in. Reason: it's awkward to
379 have all the superclass selectors wired-in.
380 - Declared as classes in GHC.Classes, e.g.
381 class (c1,c2) => (c1,c2)
382 - Given constraints: the superclasses automatically become available
383 - Wanted constraints: there is a built-in instance
384 instance (c1,c2) => (c1,c2)
385 - Currently just go up to 16; beyond that
386 you have to use manual nesting
387 - Their OccNames look like (%,,,%), so they can easily be
388 distinguished from term tuples. But (following Haskell) we
389 pretty-print saturated constraint tuples with round parens; see
390 BasicTypes.tupleParens.
391
392 * In quite a lot of places things are restrcted just to
393 BoxedTuple/UnboxedTuple, and then we used BasicTypes.Boxity to distinguish
394 E.g. tupleTyCon has a Boxity argument
395
396 * When looking up an OccName in the original-name cache
397 (IfaceEnv.lookupOrigNameCache), we spot the tuple OccName to make sure
398 we get the right wired-in name. This guy can't tell the difference
399 betweeen BoxedTuple and ConstraintTuple (same OccName!), so tuples
400 are not serialised into interface files using OccNames at all.
401 -}
402
403 isBuiltInOcc_maybe :: OccName -> Maybe Name
404 -- Built in syntax isn't "in scope" so these OccNames
405 -- map to wired-in Names with BuiltInSyntax
406 isBuiltInOcc_maybe occ
407 = case occNameString occ of
408 "[]" -> choose_ns listTyConName nilDataConName
409 ":" -> Just consDataConName
410 "[::]" -> Just parrTyConName
411 "()" -> tup_name Boxed 0
412 "(##)" -> tup_name Unboxed 0
413 '(':',':rest -> parse_tuple Boxed 2 rest
414 '(':'#':',':rest -> parse_tuple Unboxed 2 rest
415 _other -> Nothing
416 where
417 ns = occNameSpace occ
418
419 parse_tuple sort n rest
420 | (',' : rest2) <- rest = parse_tuple sort (n+1) rest2
421 | tail_matches sort rest = tup_name sort n
422 | otherwise = Nothing
423
424 tail_matches Boxed ")" = True
425 tail_matches Unboxed "#)" = True
426 tail_matches _ _ = False
427
428 tup_name boxity arity
429 = choose_ns (getName (tupleTyCon boxity arity))
430 (getName (tupleDataCon boxity arity))
431
432 choose_ns tc dc
433 | isTcClsNameSpace ns = Just tc
434 | isDataConNameSpace ns = Just dc
435 | otherwise = pprPanic "tup_name" (ppr occ)
436
437 mkTupleOcc :: NameSpace -> Boxity -> Arity -> OccName
438 mkTupleOcc ns sort ar = mkOccName ns str
439 where
440 -- No need to cache these, the caching is done in mk_tuple
441 str = case sort of
442 Unboxed -> '(' : '#' : commas ++ "#)"
443 Boxed -> '(' : commas ++ ")"
444
445 commas = take (ar-1) (repeat ',')
446
447 mkCTupleOcc :: NameSpace -> Arity -> OccName
448 mkCTupleOcc ns ar = mkOccName ns str
449 where
450 str = "(%" ++ commas ++ "%)"
451 commas = take (ar-1) (repeat ',')
452
453 cTupleTyConName :: Arity -> Name
454 cTupleTyConName arity
455 = mkExternalName (mkCTupleTyConUnique arity) gHC_CLASSES
456 (mkCTupleOcc tcName arity) noSrcSpan
457 -- The corresponding DataCon does not have a known-key name
458
459 cTupleTyConNames :: [Name]
460 cTupleTyConNames = map cTupleTyConName (0 : [2..mAX_CTUPLE_SIZE])
461
462 cTupleTyConNameSet :: NameSet
463 cTupleTyConNameSet = mkNameSet cTupleTyConNames
464
465 isCTupleTyConName :: Name -> Bool
466 isCTupleTyConName n
467 = ASSERT2( isExternalName n, ppr n )
468 nameModule n == gHC_CLASSES
469 && n `elemNameSet` cTupleTyConNameSet
470
471 tupleTyCon :: Boxity -> Arity -> TyCon
472 tupleTyCon sort i | i > mAX_TUPLE_SIZE = fst (mk_tuple sort i) -- Build one specially
473 tupleTyCon Boxed i = fst (boxedTupleArr ! i)
474 tupleTyCon Unboxed i = fst (unboxedTupleArr ! i)
475
476 tupleTyConName :: TupleSort -> Arity -> Name
477 tupleTyConName ConstraintTuple a = cTupleTyConName a
478 tupleTyConName BoxedTuple a = tyConName (tupleTyCon Boxed a)
479 tupleTyConName UnboxedTuple a = tyConName (tupleTyCon Unboxed a)
480
481 promotedTupleTyCon :: Boxity -> Arity -> TyCon
482 promotedTupleTyCon boxity i = promoteTyCon (tupleTyCon boxity i)
483
484 promotedTupleDataCon :: Boxity -> Arity -> TyCon
485 promotedTupleDataCon boxity i = promoteDataCon (tupleDataCon boxity i)
486
487 tupleDataCon :: Boxity -> Arity -> DataCon
488 tupleDataCon sort i | i > mAX_TUPLE_SIZE = snd (mk_tuple sort i) -- Build one specially
489 tupleDataCon Boxed i = snd (boxedTupleArr ! i)
490 tupleDataCon Unboxed i = snd (unboxedTupleArr ! i)
491
492 boxedTupleArr, unboxedTupleArr :: Array Int (TyCon,DataCon)
493 boxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Boxed i | i <- [0..mAX_TUPLE_SIZE]]
494 unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple Unboxed i | i <- [0..mAX_TUPLE_SIZE]]
495
496 mk_tuple :: Boxity -> Int -> (TyCon,DataCon)
497 mk_tuple boxity arity = (tycon, tuple_con)
498 where
499 tycon = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con
500 tup_sort
501 prom_tc NoParentTyCon
502
503 tup_sort = case boxity of
504 Boxed -> BoxedTuple
505 Unboxed -> UnboxedTuple
506
507 prom_tc = case boxity of
508 Boxed -> Just (mkPromotedTyCon tycon (promoteKind tc_kind))
509 Unboxed -> Nothing
510
511 modu = case boxity of
512 Boxed -> gHC_TUPLE
513 Unboxed -> gHC_PRIM
514
515 tc_name = mkWiredInName modu (mkTupleOcc tcName boxity arity) tc_uniq
516 (ATyCon tycon) BuiltInSyntax
517 tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
518
519 res_kind = case boxity of
520 Boxed -> liftedTypeKind
521 Unboxed -> unliftedTypeKind
522
523 tyvars = take arity $ case boxity of
524 Boxed -> alphaTyVars
525 Unboxed -> openAlphaTyVars
526
527 tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
528 tyvar_tys = mkTyVarTys tyvars
529 dc_name = mkWiredInName modu (mkTupleOcc dataName boxity arity) dc_uniq
530 (AConLike (RealDataCon tuple_con)) BuiltInSyntax
531 tc_uniq = mkTupleTyConUnique boxity arity
532 dc_uniq = mkTupleDataConUnique boxity arity
533
534 unitTyCon :: TyCon
535 unitTyCon = tupleTyCon Boxed 0
536
537 unitTyConKey :: Unique
538 unitTyConKey = getUnique unitTyCon
539
540 unitDataCon :: DataCon
541 unitDataCon = head (tyConDataCons unitTyCon)
542
543 unitDataConId :: Id
544 unitDataConId = dataConWorkId unitDataCon
545
546 pairTyCon :: TyCon
547 pairTyCon = tupleTyCon Boxed 2
548
549 unboxedUnitTyCon :: TyCon
550 unboxedUnitTyCon = tupleTyCon Unboxed 0
551
552 unboxedUnitDataCon :: DataCon
553 unboxedUnitDataCon = tupleDataCon Unboxed 0
554
555 unboxedSingletonTyCon :: TyCon
556 unboxedSingletonTyCon = tupleTyCon Unboxed 1
557
558 unboxedSingletonDataCon :: DataCon
559 unboxedSingletonDataCon = tupleDataCon Unboxed 1
560
561 unboxedPairTyCon :: TyCon
562 unboxedPairTyCon = tupleTyCon Unboxed 2
563
564 unboxedPairDataCon :: DataCon
565 unboxedPairDataCon = tupleDataCon Unboxed 2
566
567 {-
568 ************************************************************************
569 * *
570 The ``boxed primitive'' types (@Char@, @Int@, etc)
571 * *
572 ************************************************************************
573 -}
574
575 charTy :: Type
576 charTy = mkTyConTy charTyCon
577
578 charTyCon :: TyCon
579 charTyCon = pcNonRecDataTyCon charTyConName
580 (Just (CType "" Nothing ("HsChar",fsLit "HsChar")))
581 [] [charDataCon]
582 charDataCon :: DataCon
583 charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
584
585 stringTy :: Type
586 stringTy = mkListTy charTy -- convenience only
587
588 intTy :: Type
589 intTy = mkTyConTy intTyCon
590
591 intTyCon :: TyCon
592 intTyCon = pcNonRecDataTyCon intTyConName
593 (Just (CType "" Nothing ("HsInt",fsLit "HsInt"))) []
594 [intDataCon]
595 intDataCon :: DataCon
596 intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
597
598 wordTy :: Type
599 wordTy = mkTyConTy wordTyCon
600
601 wordTyCon :: TyCon
602 wordTyCon = pcNonRecDataTyCon wordTyConName
603 (Just (CType "" Nothing ("HsWord", fsLit "HsWord"))) []
604 [wordDataCon]
605 wordDataCon :: DataCon
606 wordDataCon = pcDataCon wordDataConName [] [wordPrimTy] wordTyCon
607
608 word8Ty :: Type
609 word8Ty = mkTyConTy word8TyCon
610
611 word8TyCon :: TyCon
612 word8TyCon = pcNonRecDataTyCon word8TyConName
613 (Just (CType "" Nothing ("HsWord8", fsLit "HsWord8"))) []
614 [word8DataCon]
615 word8DataCon :: DataCon
616 word8DataCon = pcDataCon word8DataConName [] [wordPrimTy] word8TyCon
617
618 floatTy :: Type
619 floatTy = mkTyConTy floatTyCon
620
621 floatTyCon :: TyCon
622 floatTyCon = pcNonRecDataTyCon floatTyConName
623 (Just (CType "" Nothing ("HsFloat", fsLit "HsFloat"))) []
624 [floatDataCon]
625 floatDataCon :: DataCon
626 floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon
627
628 doubleTy :: Type
629 doubleTy = mkTyConTy doubleTyCon
630
631 doubleTyCon :: TyCon
632 doubleTyCon = pcNonRecDataTyCon doubleTyConName
633 (Just (CType "" Nothing ("HsDouble",fsLit "HsDouble"))) []
634 [doubleDataCon]
635
636 doubleDataCon :: DataCon
637 doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
638
639 {-
640 ************************************************************************
641 * *
642 The Bool type
643 * *
644 ************************************************************************
645
646 An ordinary enumeration type, but deeply wired in. There are no
647 magical operations on @Bool@ (just the regular Prelude code).
648
649 {\em BEGIN IDLE SPECULATION BY SIMON}
650
651 This is not the only way to encode @Bool@. A more obvious coding makes
652 @Bool@ just a boxed up version of @Bool#@, like this:
653 \begin{verbatim}
654 type Bool# = Int#
655 data Bool = MkBool Bool#
656 \end{verbatim}
657
658 Unfortunately, this doesn't correspond to what the Report says @Bool@
659 looks like! Furthermore, we get slightly less efficient code (I
660 think) with this coding. @gtInt@ would look like this:
661
662 \begin{verbatim}
663 gtInt :: Int -> Int -> Bool
664 gtInt x y = case x of I# x# ->
665 case y of I# y# ->
666 case (gtIntPrim x# y#) of
667 b# -> MkBool b#
668 \end{verbatim}
669
670 Notice that the result of the @gtIntPrim@ comparison has to be turned
671 into an integer (here called @b#@), and returned in a @MkBool@ box.
672
673 The @if@ expression would compile to this:
674 \begin{verbatim}
675 case (gtInt x y) of
676 MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
677 \end{verbatim}
678
679 I think this code is a little less efficient than the previous code,
680 but I'm not certain. At all events, corresponding with the Report is
681 important. The interesting thing is that the language is expressive
682 enough to describe more than one alternative; and that a type doesn't
683 necessarily need to be a straightforwardly boxed version of its
684 primitive counterpart.
685
686 {\em END IDLE SPECULATION BY SIMON}
687 -}
688
689 boolTy :: Type
690 boolTy = mkTyConTy boolTyCon
691
692 boolTyCon :: TyCon
693 boolTyCon = pcTyCon True NonRecursive True boolTyConName
694 (Just (CType "" Nothing ("HsBool", fsLit "HsBool")))
695 [] [falseDataCon, trueDataCon]
696
697 falseDataCon, trueDataCon :: DataCon
698 falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
699 trueDataCon = pcDataCon trueDataConName [] [] boolTyCon
700
701 falseDataConId, trueDataConId :: Id
702 falseDataConId = dataConWorkId falseDataCon
703 trueDataConId = dataConWorkId trueDataCon
704
705 orderingTyCon :: TyCon
706 orderingTyCon = pcTyCon True NonRecursive True orderingTyConName Nothing
707 [] [ltDataCon, eqDataCon, gtDataCon]
708
709 ltDataCon, eqDataCon, gtDataCon :: DataCon
710 ltDataCon = pcDataCon ltDataConName [] [] orderingTyCon
711 eqDataCon = pcDataCon eqDataConName [] [] orderingTyCon
712 gtDataCon = pcDataCon gtDataConName [] [] orderingTyCon
713
714 ltDataConId, eqDataConId, gtDataConId :: Id
715 ltDataConId = dataConWorkId ltDataCon
716 eqDataConId = dataConWorkId eqDataCon
717 gtDataConId = dataConWorkId gtDataCon
718
719 {-
720 ************************************************************************
721 * *
722 The List type
723 Special syntax, deeply wired in,
724 but otherwise an ordinary algebraic data type
725 * *
726 ************************************************************************
727
728 data [] a = [] | a : (List a)
729 -}
730
731 mkListTy :: Type -> Type
732 mkListTy ty = mkTyConApp listTyCon [ty]
733
734 listTyCon :: TyCon
735 listTyCon = pcTyCon False Recursive True
736 listTyConName Nothing alpha_tyvar [nilDataCon, consDataCon]
737
738 mkPromotedListTy :: Type -> Type
739 mkPromotedListTy ty = mkTyConApp promotedListTyCon [ty]
740
741 promotedListTyCon :: TyCon
742 promotedListTyCon = promoteTyCon listTyCon
743
744 nilDataCon :: DataCon
745 nilDataCon = pcDataCon nilDataConName alpha_tyvar [] listTyCon
746
747 consDataCon :: DataCon
748 consDataCon = pcDataConWithFixity True {- Declared infix -}
749 consDataConName
750 alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
751 -- Interesting: polymorphic recursion would help here.
752 -- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
753 -- gets the over-specific type (Type -> Type)
754
755 -- Wired-in type Maybe
756
757 maybeTyCon :: TyCon
758 maybeTyCon = pcTyCon True NonRecursive True maybeTyConName Nothing alpha_tyvar
759 [nothingDataCon, justDataCon]
760
761 nothingDataCon :: DataCon
762 nothingDataCon = pcDataCon nothingDataConName alpha_tyvar [] maybeTyCon
763
764 justDataCon :: DataCon
765 justDataCon = pcDataCon justDataConName alpha_tyvar [alphaTy] maybeTyCon
766
767 {-
768 ** *********************************************************************
769 * *
770 The tuple types
771 * *
772 ************************************************************************
773
774 The tuple types are definitely magic, because they form an infinite
775 family.
776
777 \begin{itemize}
778 \item
779 They have a special family of type constructors, of type @TyCon@
780 These contain the tycon arity, but don't require a Unique.
781
782 \item
783 They have a special family of constructors, of type
784 @Id@. Again these contain their arity but don't need a Unique.
785
786 \item
787 There should be a magic way of generating the info tables and
788 entry code for all tuples.
789
790 But at the moment we just compile a Haskell source
791 file\srcloc{lib/prelude/...} containing declarations like:
792 \begin{verbatim}
793 data Tuple0 = Tup0
794 data Tuple2 a b = Tup2 a b
795 data Tuple3 a b c = Tup3 a b c
796 data Tuple4 a b c d = Tup4 a b c d
797 ...
798 \end{verbatim}
799 The print-names associated with the magic @Id@s for tuple constructors
800 ``just happen'' to be the same as those generated by these
801 declarations.
802
803 \item
804 The instance environment should have a magic way to know
805 that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
806 so on. \ToDo{Not implemented yet.}
807
808 \item
809 There should also be a way to generate the appropriate code for each
810 of these instances, but (like the info tables and entry code) it is
811 done by enumeration\srcloc{lib/prelude/InTup?.hs}.
812 \end{itemize}
813 -}
814
815 mkTupleTy :: Boxity -> [Type] -> Type
816 -- Special case for *boxed* 1-tuples, which are represented by the type itself
817 mkTupleTy Boxed [ty] = ty
818 mkTupleTy boxity tys = mkTyConApp (tupleTyCon boxity (length tys)) tys
819
820 -- | Build the type of a small tuple that holds the specified type of thing
821 mkBoxedTupleTy :: [Type] -> Type
822 mkBoxedTupleTy tys = mkTupleTy Boxed tys
823
824 unitTy :: Type
825 unitTy = mkTupleTy Boxed []
826
827
828 {- *********************************************************************
829 * *
830 The parallel-array type, [::]
831 * *
832 ************************************************************************
833
834 Special syntax for parallel arrays needs some wired in definitions.
835 -}
836
837 -- | Construct a type representing the application of the parallel array constructor
838 mkPArrTy :: Type -> Type
839 mkPArrTy ty = mkTyConApp parrTyCon [ty]
840
841 -- | Represents the type constructor of parallel arrays
842 --
843 -- * This must match the definition in @PrelPArr@
844 --
845 -- NB: Although the constructor is given here, it will not be accessible in
846 -- user code as it is not in the environment of any compiled module except
847 -- @PrelPArr@.
848 --
849 parrTyCon :: TyCon
850 parrTyCon = pcNonRecDataTyCon parrTyConName Nothing alpha_tyvar [parrDataCon]
851
852 parrDataCon :: DataCon
853 parrDataCon = pcDataCon
854 parrDataConName
855 alpha_tyvar -- forall'ed type variables
856 [intTy, -- 1st argument: Int
857 mkTyConApp -- 2nd argument: Array# a
858 arrayPrimTyCon
859 alpha_ty]
860 parrTyCon
861
862 -- | Check whether a type constructor is the constructor for parallel arrays
863 isPArrTyCon :: TyCon -> Bool
864 isPArrTyCon tc = tyConName tc == parrTyConName
865
866 -- | Fake array constructors
867 --
868 -- * These constructors are never really used to represent array values;
869 -- however, they are very convenient during desugaring (and, in particular,
870 -- in the pattern matching compiler) to treat array pattern just like
871 -- yet another constructor pattern
872 --
873 parrFakeCon :: Arity -> DataCon
874 parrFakeCon i | i > mAX_TUPLE_SIZE = mkPArrFakeCon i -- build one specially
875 parrFakeCon i = parrFakeConArr!i
876
877 -- pre-defined set of constructors
878 --
879 parrFakeConArr :: Array Int DataCon
880 parrFakeConArr = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
881 | i <- [0..mAX_TUPLE_SIZE]]
882
883 -- build a fake parallel array constructor for the given arity
884 --
885 mkPArrFakeCon :: Int -> DataCon
886 mkPArrFakeCon arity = data_con
887 where
888 data_con = pcDataCon name [tyvar] tyvarTys parrTyCon
889 tyvar = head alphaTyVars
890 tyvarTys = replicate arity $ mkTyVarTy tyvar
891 nameStr = mkFastString ("MkPArr" ++ show arity)
892 name = mkWiredInName gHC_PARR' (mkDataOccFS nameStr) unique
893 (AConLike (RealDataCon data_con)) UserSyntax
894 unique = mkPArrDataConUnique arity
895
896 -- | Checks whether a data constructor is a fake constructor for parallel arrays
897 isPArrFakeCon :: DataCon -> Bool
898 isPArrFakeCon dcon = dcon == parrFakeCon (dataConSourceArity dcon)
899
900 -- Promoted Booleans
901
902 promotedBoolTyCon, promotedFalseDataCon, promotedTrueDataCon :: TyCon
903 promotedBoolTyCon = promoteTyCon boolTyCon
904 promotedTrueDataCon = promoteDataCon trueDataCon
905 promotedFalseDataCon = promoteDataCon falseDataCon
906
907 -- Promoted Ordering
908
909 promotedOrderingTyCon
910 , promotedLTDataCon
911 , promotedEQDataCon
912 , promotedGTDataCon
913 :: TyCon
914 promotedOrderingTyCon = promoteTyCon orderingTyCon
915 promotedLTDataCon = promoteDataCon ltDataCon
916 promotedEQDataCon = promoteDataCon eqDataCon
917 promotedGTDataCon = promoteDataCon gtDataCon
918
919 {- *********************************************************************
920 * *
921 Type equalities
922 * *
923 ********************************************************************* -}
924
925 eqTyCon :: TyCon
926 eqTyCon = mkAlgTyCon eqTyConName
927 (ForAllTy kv $ mkArrowKinds [k, k] constraintKind)
928 [kv, a, b]
929 [Nominal, Nominal, Nominal]
930 Nothing
931 [] -- No stupid theta
932 (DataTyCon [eqBoxDataCon] False)
933 NoParentTyCon
934 NonRecursive
935 False
936 Nothing -- No parent for constraint-kinded types
937 where
938 kv = kKiVar
939 k = mkTyVarTy kv
940 [a,b] = mkTemplateTyVars [k,k]
941
942 eqBoxDataCon :: DataCon
943 eqBoxDataCon = pcDataCon eqBoxDataConName args [TyConApp eqPrimTyCon (map mkTyVarTy args)] eqTyCon
944 where
945 kv = kKiVar
946 k = mkTyVarTy kv
947 [a,b] = mkTemplateTyVars [k,k]
948 args = [kv, a, b]
949
950
951 coercibleTyCon :: TyCon
952 coercibleTyCon = mkClassTyCon
953 coercibleTyConName kind tvs [Nominal, Representational, Representational]
954 rhs coercibleClass NonRecursive
955 where kind = (ForAllTy kv $ mkArrowKinds [k, k] constraintKind)
956 kv = kKiVar
957 k = mkTyVarTy kv
958 [a,b] = mkTemplateTyVars [k,k]
959 tvs = [kv, a, b]
960 rhs = DataTyCon [coercibleDataCon] False
961
962 coercibleDataCon :: DataCon
963 coercibleDataCon = pcDataCon coercibleDataConName args [TyConApp eqReprPrimTyCon (map mkTyVarTy args)] coercibleTyCon
964 where
965 kv = kKiVar
966 k = mkTyVarTy kv
967 [a,b] = mkTemplateTyVars [k,k]
968 args = [kv, a, b]
969
970 coercibleClass :: Class
971 coercibleClass = mkClass (tyConTyVars coercibleTyCon) [] [] [] [] [] (mkAnd []) coercibleTyCon
972
973 {-
974 Note [The Implicit Parameter class]
975
976 Implicit parameters `?x :: a` are desugared into dictionaries for the
977 class `IP "x" a`, which is defined (in GHC.Classes) as
978
979 class IP (x :: Symbol) a | x -> a
980
981 This class is wired-in so that `error` and `undefined`, which have
982 wired-in types, can use the implicit-call-stack feature to provide
983 a call-stack alongside the error message.
984 -}
985
986 ipDataConName, ipTyConName, ipCoName :: Name
987 ipDataConName = mkWiredInDataConName UserSyntax gHC_CLASSES (fsLit "IP")
988 ipDataConKey ipDataCon
989 ipTyConName = mkWiredInTyConName UserSyntax gHC_CLASSES (fsLit "IP")
990 ipTyConKey ipTyCon
991 ipCoName = mkWiredInCoAxiomName BuiltInSyntax gHC_CLASSES (fsLit "NTCo:IP")
992 ipCoNameKey (toBranchedAxiom ipCoAxiom)
993
994 -- See Note [The Implicit Parameter class]
995 ipTyCon :: TyCon
996 ipTyCon = mkClassTyCon ipTyConName kind [ip,a] [] rhs ipClass NonRecursive
997 where
998 kind = mkArrowKinds [typeSymbolKind, liftedTypeKind] constraintKind
999 [ip,a] = mkTemplateTyVars [typeSymbolKind, liftedTypeKind]
1000 rhs = NewTyCon ipDataCon (mkTyVarTy a) ([], mkTyVarTy a) ipCoAxiom
1001
1002 ipCoAxiom :: CoAxiom Unbranched
1003 ipCoAxiom = mkNewTypeCo ipCoName ipTyCon [ip,a] [Nominal, Nominal] (mkTyVarTy a)
1004 where
1005 [ip,a] = mkTemplateTyVars [typeSymbolKind, liftedTypeKind]
1006
1007 ipDataCon :: DataCon
1008 ipDataCon = pcDataCon ipDataConName [ip,a] ts ipTyCon
1009 where
1010 [ip,a] = mkTemplateTyVars [typeSymbolKind, liftedTypeKind]
1011 ts = [mkTyVarTy a]
1012
1013 ipClass :: Class
1014 ipClass = mkClass (tyConTyVars ipTyCon) [([ip], [a])] [] [] [] [] (mkAnd [])
1015 ipTyCon
1016 where
1017 [ip, a] = tyConTyVars ipTyCon
1018
1019 -- this is a fake version of the CallStack TyCon so we can refer to it
1020 -- in MkCore.errorTy
1021 callStackTyCon :: TyCon
1022 callStackTyCon = pcNonRecDataTyCon callStackTyConName Nothing [] []