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