TysWiredIn.lhs 30.9 KB
Newer Older
1
%
2
% (c) The GRASP Project, Glasgow University, 1994-1998
3
4
5
6
%
\section[TysWiredIn]{Wired-in knowledge about {\em non-primitive} types}

\begin{code}
7
-- | This module is about types that can be defined in Haskell, but which
8
--   must be wired into the compiler nonetheless.  C.f module TysPrim
9
module TysWiredIn (
10
        -- * All wired in things
11
        wiredInTyCons,
12

13
        -- * Bool
14
15
16
        boolTy, boolTyCon, boolTyCon_RDR, boolTyConName,
        trueDataCon,  trueDataConId,  true_RDR,
        falseDataCon, falseDataConId, false_RDR,
17
        promotedBoolTyCon, promotedFalseDataCon, promotedTrueDataCon,
18

19
20
21
22
23
        -- * Ordering
        ltDataCon, ltDataConId,
        eqDataCon, eqDataConId,
        gtDataCon, gtDataConId,

24
        -- * Char
25
26
        charTyCon, charDataCon, charTyCon_RDR,
        charTy, stringTy, charTyConName,
sof's avatar
sof committed
27

28
        -- integer-gmp only:
29
30
        integerGmpSDataCon,

31
32
33
34
35
        -- * Double
        doubleTyCon, doubleDataCon, doubleTy, doubleTyConName,

        -- * Float
        floatTyCon, floatDataCon, floatTy, floatTyConName,
sof's avatar
sof committed
36

37
        -- * Int
38
39
        intTyCon, intDataCon, intTyCon_RDR, intDataCon_RDR, intTyConName,
        intTy,
40

41
        -- * Word
42
        wordTyCon, wordDataCon, wordTyConName, wordTy,
Ian Lynagh's avatar
Ian Lynagh committed
43

44
        -- * List
45
46
47
        listTyCon, nilDataCon, nilDataConName, consDataCon, consDataConName,
        listTyCon_RDR, consDataCon_RDR, listTyConName,
        mkListTy, mkPromotedListTy,
48

49
50
51
        -- * Tuples
        mkTupleTy, mkBoxedTupleTy,
        tupleTyCon, tupleCon,
52
        promotedTupleTyCon, promotedTupleDataCon,
53
54
        unitTyCon, unitDataCon, unitDataConId, pairTyCon,
        unboxedUnitTyCon, unboxedUnitDataCon,
55
        unboxedSingletonTyCon, unboxedSingletonDataCon,
56
        unboxedPairTyCon, unboxedPairDataCon,
57

58
        -- * Unit
59
        unitTy,
chak's avatar
chak committed
60

61
        -- * Kinds
62
        typeNatKindCon, typeNatKind, typeSymbolKindCon, typeSymbolKind,
63

64
        -- * Parallel arrays
65
66
67
        mkPArrTy,
        parrTyCon, parrFakeCon, isPArrTyCon, isPArrFakeCon,
        parrTyCon_RDR, parrTyConName,
batterseapower's avatar
batterseapower committed
68
69
70

        -- * Equality predicates
        eqTyCon_RDR, eqTyCon, eqTyConName, eqBoxDataCon,
71
        coercibleTyCon, coercibleDataCon, coercibleClass,
batterseapower's avatar
batterseapower committed
72

73
74
        mkWiredInTyConName -- This is used in TcTypeNats to define the
                           -- built-in functions for evaluation.
75
76
    ) where

77
78
#include "HsVersions.h"

79
import {-# SOURCE #-} MkId( mkDataConWorkId )
80

81
-- friends:
82
import PrelNames
83
84
import TysPrim

85
-- others:
86
87
import Constants        ( mAX_TUPLE_SIZE )
import Module           ( Module )
88
import Type             ( mkTyConApp )
89
import DataCon
90
91
import Var
import TyCon
92
import Class            ( Class, mkClass )
93
import TypeRep
Ian Lynagh's avatar
Ian Lynagh committed
94
import RdrName
95
import Name
96
import BasicTypes       ( TupleSort(..), tupleSortBoxity,
97
                          Arity, RecFlag(..), Boxity(..) )
98
import ForeignCall
99
import Unique           ( incrUnique, mkTupleTyConUnique,
100
                          mkTupleDataConUnique, mkPArrDataConUnique )
Ian Lynagh's avatar
Ian Lynagh committed
101
import Data.Array
102
import FastString
103
import Outputable
104
import Config
105
import Util
106
import BooleanFormula   ( mkAnd )
sof's avatar
sof committed
107

Ian Lynagh's avatar
Ian Lynagh committed
108
alpha_tyvar :: [TyVar]
109
alpha_tyvar = [alphaTyVar]
Ian Lynagh's avatar
Ian Lynagh committed
110
111
112

alpha_ty :: [Type]
alpha_ty = [alphaTy]
113
\end{code}
114
115


116
%************************************************************************
117
%*                                                                      *
118
\subsection{Wired in type constructors}
119
%*                                                                      *
120
%************************************************************************
121

122
123
124
If you change which things are wired in, make sure you change their
names in PrelNames, so they use wTcQual, wDataQual, etc

125
\begin{code}
batterseapower's avatar
batterseapower committed
126
127
128
129
130
131
132
133
134
-- This list is used only to define PrelInfo.wiredInThings. That in turn
-- is used to initialise the name environment carried around by the renamer.
-- This means that if we look up the name of a TyCon (or its implicit binders)
-- that occurs in this list that name will be assigned the wired-in key we
-- define here.
--
-- Because of their infinite nature, this list excludes tuples, Any and implicit
-- parameter TyCons. Instead, we have a hack in lookupOrigNameCache to deal with
-- these names.
135
136
--
-- See also Note [Known-key names]
batterseapower's avatar
batterseapower committed
137
wiredInTyCons :: [TyCon]
138

139
140
141
142
143
144
145
146
147
148
149
wiredInTyCons = [ unitTyCon     -- Not treated like other tuples, because
                                -- it's defined in GHC.Base, and there's only
                                -- one of it.  We put it in wiredInTyCons so
                                -- that it'll pre-populate the name cache, so
                                -- the special case in lookupOrigNameCache
                                -- doesn't need to look out for it
              , boolTyCon
              , charTyCon
              , doubleTyCon
              , floatTyCon
              , intTyCon
150
              , wordTyCon
151
152
              , listTyCon
              , parrTyCon
batterseapower's avatar
batterseapower committed
153
              , eqTyCon
154
              , coercibleTyCon
155
              , typeNatKindCon
156
              , typeSymbolKindCon
157
              ]
158
159
160
           ++ (case cIntegerLibraryType of
               IntegerGMP -> [integerTyCon]
               _ -> [])
161
\end{code}
162

163
\begin{code}
164
mkWiredInTyConName :: BuiltInSyntax -> Module -> FastString -> Unique -> TyCon -> Name
Ian Lynagh's avatar
Ian Lynagh committed
165
mkWiredInTyConName built_in modu fs unique tycon
166
  = mkWiredInName modu (mkTcOccFS fs) unique
167
168
                  (ATyCon tycon)        -- Relevant TyCon
                  built_in
169

170
mkWiredInDataConName :: BuiltInSyntax -> Module -> FastString -> Unique -> DataCon -> Name
Ian Lynagh's avatar
Ian Lynagh committed
171
mkWiredInDataConName built_in modu fs unique datacon
172
  = mkWiredInName modu (mkDataOccFS fs) unique
173
174
                  (ADataCon datacon)    -- Relevant DataCon
                  built_in
175

batterseapower's avatar
batterseapower committed
176
177
eqTyConName, eqBoxDataConName :: Name
eqTyConName      = mkWiredInTyConName   BuiltInSyntax gHC_TYPES (fsLit "~")   eqTyConKey      eqTyCon
178
eqBoxDataConName = mkWiredInDataConName UserSyntax    gHC_TYPES (fsLit "Eq#") eqBoxDataConKey eqBoxDataCon
batterseapower's avatar
batterseapower committed
179

180
181
182
183
coercibleTyConName, coercibleDataConName :: Name
coercibleTyConName   = mkWiredInTyConName   UserSyntax gHC_TYPES (fsLit "Coercible")  coercibleTyConKey   coercibleTyCon
coercibleDataConName = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "MkCoercible") coercibleDataConKey coercibleDataCon

Ian Lynagh's avatar
Ian Lynagh committed
184
charTyConName, charDataConName, intTyConName, intDataConName :: Name
185
charTyConName     = mkWiredInTyConName   UserSyntax gHC_TYPES (fsLit "Char") charTyConKey charTyCon
186
charDataConName   = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "C#") charDataConKey charDataCon
187
188
intTyConName      = mkWiredInTyConName   UserSyntax gHC_TYPES (fsLit "Int") intTyConKey   intTyCon
intDataConName    = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "I#") intDataConKey  intDataCon
Ian Lynagh's avatar
Ian Lynagh committed
189
190

boolTyConName, falseDataConName, trueDataConName :: Name
191
boolTyConName     = mkWiredInTyConName   UserSyntax gHC_TYPES (fsLit "Bool") boolTyConKey boolTyCon
Ian Lynagh's avatar
Ian Lynagh committed
192
falseDataConName  = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "False") falseDataConKey falseDataCon
193
trueDataConName   = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "True")  trueDataConKey  trueDataCon
Ian Lynagh's avatar
Ian Lynagh committed
194
195

listTyConName, nilDataConName, consDataConName :: Name
196
197
198
listTyConName     = mkWiredInTyConName   BuiltInSyntax gHC_TYPES (fsLit "[]") listTyConKey listTyCon
nilDataConName    = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit "[]") nilDataConKey nilDataCon
consDataConName   = mkWiredInDataConName BuiltInSyntax gHC_TYPES (fsLit ":") consDataConKey consDataCon
Ian Lynagh's avatar
Ian Lynagh committed
199

200
201
202
203
204
205
206
wordTyConName, wordDataConName, floatTyConName, floatDataConName, doubleTyConName, doubleDataConName :: Name
wordTyConName      = mkWiredInTyConName   UserSyntax gHC_TYPES (fsLit "Word")   wordTyConKey     wordTyCon
wordDataConName    = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "W#")     wordDataConKey   wordDataCon
floatTyConName     = mkWiredInTyConName   UserSyntax gHC_TYPES (fsLit "Float")  floatTyConKey    floatTyCon
floatDataConName   = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "F#")     floatDataConKey  floatDataCon
doubleTyConName    = mkWiredInTyConName   UserSyntax gHC_TYPES (fsLit "Double") doubleTyConKey   doubleTyCon
doubleDataConName  = mkWiredInDataConName UserSyntax gHC_TYPES (fsLit "D#")     doubleDataConKey doubleDataCon
Ian Lynagh's avatar
Ian Lynagh committed
207

208
-- Kinds
209
typeNatKindConName, typeSymbolKindConName :: Name
210
typeNatKindConName    = mkWiredInTyConName UserSyntax gHC_TYPELITS (fsLit "Nat")    typeNatKindConNameKey    typeNatKindCon
211
typeSymbolKindConName = mkWiredInTyConName UserSyntax gHC_TYPELITS (fsLit "Symbol") typeSymbolKindConNameKey typeSymbolKindCon
212

213
-- For integer-gmp only:
214
215
216
217
integerRealTyConName :: Name
integerRealTyConName    = case cIntegerLibraryType of
                          IntegerGMP -> mkWiredInTyConName   UserSyntax gHC_INTEGER_TYPE (fsLit "Integer") integerTyConKey integerTyCon
                          _ ->          panic "integerRealTyConName evaluated, but not integer-gmp"
218
219
220
221
integerGmpSDataConName, integerGmpJDataConName :: Name
integerGmpSDataConName = mkWiredInDataConName UserSyntax gHC_INTEGER_TYPE (fsLit "S#") integerGmpSDataConKey integerGmpSDataCon
integerGmpJDataConName = mkWiredInDataConName UserSyntax gHC_INTEGER_TYPE (fsLit "J#") integerGmpJDataConKey integerGmpJDataCon

Ian Lynagh's avatar
Ian Lynagh committed
222
parrTyConName, parrDataConName :: Name
223
224
225
parrTyConName   = mkWiredInTyConName   BuiltInSyntax
                    gHC_PARR' (fsLit "[::]") parrTyConKey parrTyCon
parrDataConName = mkWiredInDataConName UserSyntax
226
                    gHC_PARR' (fsLit "PArr") parrDataConKey parrDataCon
227

Ian Lynagh's avatar
Ian Lynagh committed
228
boolTyCon_RDR, false_RDR, true_RDR, intTyCon_RDR, charTyCon_RDR,
batterseapower's avatar
batterseapower committed
229
    intDataCon_RDR, listTyCon_RDR, consDataCon_RDR, parrTyCon_RDR, eqTyCon_RDR :: RdrName
230
boolTyCon_RDR   = nameRdrName boolTyConName
231
232
233
234
235
236
false_RDR       = nameRdrName falseDataConName
true_RDR        = nameRdrName trueDataConName
intTyCon_RDR    = nameRdrName intTyConName
charTyCon_RDR   = nameRdrName charTyConName
intDataCon_RDR  = nameRdrName intDataConName
listTyCon_RDR   = nameRdrName listTyConName
237
consDataCon_RDR = nameRdrName consDataConName
238
parrTyCon_RDR   = nameRdrName parrTyConName
batterseapower's avatar
batterseapower committed
239
eqTyCon_RDR     = nameRdrName eqTyConName
240
\end{code}
241

242
243
244
245
246
247
248
%************************************************************************
%*                                                                      *
\subsection{mkWiredInTyCon}
%*                                                                      *
%************************************************************************

\begin{code}
249
pcNonRecDataTyCon :: Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon
250
251
-- Not an enumeration, not promotable
pcNonRecDataTyCon = pcTyCon False NonRecursive False
252

253
254
-- This function assumes that the types it creates have all parameters at
-- Representational role!
255
256
pcTyCon :: Bool -> RecFlag -> Bool -> Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon
pcTyCon is_enum is_rec is_prom name cType tyvars cons
257
258
  = tycon
  where
259
    tycon = buildAlgTyCon name
260
                tyvars
261
                (map (const Representational) tyvars)
262
                cType
263
264
                []              -- No stupid theta
                (DataTyCon cons is_enum)
265
                is_rec
266
                is_prom
267
268
                False           -- Not in GADT syntax
                NoParentTyCon
269

270
271
272
273
pcDataCon :: Name -> [TyVar] -> [Type] -> TyCon -> DataCon
pcDataCon = pcDataConWithFixity False

pcDataConWithFixity :: Bool -> Name -> [TyVar] -> [Type] -> TyCon -> DataCon
batterseapower's avatar
batterseapower committed
274
275
pcDataConWithFixity infx n = pcDataConWithFixity' infx n (incrUnique (nameUnique n))
-- The Name's unique is the first of two free uniques;
276
277
-- the first is used for the datacon itself,
-- the second is used for the "worker name"
batterseapower's avatar
batterseapower committed
278
279
280
281
282
283
284
--
-- To support this the mkPreludeDataConUnique function "allocates"
-- one DataCon unique per pair of Ints.

pcDataConWithFixity' :: Bool -> Name -> Unique -> [TyVar] -> [Type] -> TyCon -> DataCon
-- The Name should be in the DataName name space; it's the name
-- of the DataCon itself.
285

batterseapower's avatar
batterseapower committed
286
pcDataConWithFixity' declared_infix dc_name wrk_key tyvars arg_tys tycon
287
288
  = data_con
  where
289
    data_con = mkDataCon dc_name declared_infix
290
                (map (const HsNoBang) arg_tys)
291
                []      -- No labelled fields
292
                tyvars
293
294
295
296
297
298
                []      -- No existential type variables
                []      -- No equality spec
                []      -- No theta
                arg_tys (mkTyConApp tycon (mkTyVarTys tyvars))
                tycon
                []      -- No stupid theta
299
                (mkDataConWorkId wrk_name data_con)
300
                NoDataConRep    -- Wired-in types are too simple to need wrappers
301

302
303
    modu     = ASSERT( isExternalName dc_name )
               nameModule dc_name
304
    wrk_occ  = mkDataConWorkerOcc (nameOccName dc_name)
Ian Lynagh's avatar
Ian Lynagh committed
305
    wrk_name = mkWiredInName modu wrk_occ wrk_key
306
                             (AnId (dataConWorkId data_con)) UserSyntax
307
308
\end{code}

309

310
%************************************************************************
311
%*                                                                      *
312
      Kinds
313
%*                                                                      *
314
315
316
%************************************************************************

\begin{code}
317
typeNatKindCon, typeSymbolKindCon :: TyCon
318
319
-- data Nat
-- data Symbol
320
321
typeNatKindCon    = pcTyCon False NonRecursive True typeNatKindConName    Nothing [] []
typeSymbolKindCon = pcTyCon False NonRecursive True typeSymbolKindConName Nothing [] []
322

323
typeNatKind, typeSymbolKind :: Kind
324
typeNatKind    = TyConApp (promoteTyCon typeNatKindCon)    []
325
typeSymbolKind = TyConApp (promoteTyCon typeSymbolKindCon) []
326
327
328
\end{code}


329
%************************************************************************
330
%*                                                                      *
331
\subsection[TysWiredIn-tuples]{The tuple types}
332
%*                                                                      *
333
334
%************************************************************************

335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
Note [How tuples work]
~~~~~~~~~~~~~~~~~~~~~~
* There are three families of tuple TyCons and corresponding
  DataCons, (boxed, unboxed, and constraint tuples), expressed by the
  type BasicTypes.TupleSort.

* DataCons (and workers etc) for BoxedTuple and ConstraintTuple have
    - distinct Uniques
    - the same OccName
  Using the same OccName means (hack!) that a single copy of the
  runtime library code (info tables etc) works for both.

* When looking up an OccName in the original-name cache
  (IfaceEnv.lookupOrigNameCache), we spot the tuple OccName to make sure
  we get the right wired-in name.  This guy can't tell the difference
  betweeen BoxedTuple and ConstraintTuple (same OccName!), so tuples
351
  are not serialised into interface files using OccNames at all.
352

353
\begin{code}
batterseapower's avatar
batterseapower committed
354
tupleTyCon :: TupleSort -> Arity -> TyCon
355
tupleTyCon sort i | i > mAX_TUPLE_SIZE = fst (mk_tuple sort i)  -- Build one specially
batterseapower's avatar
batterseapower committed
356
357
tupleTyCon BoxedTuple   i = fst (boxedTupleArr   ! i)
tupleTyCon UnboxedTuple i = fst (unboxedTupleArr ! i)
358
tupleTyCon ConstraintTuple    i = fst (factTupleArr    ! i)
batterseapower's avatar
batterseapower committed
359

360
promotedTupleTyCon :: TupleSort -> Arity -> TyCon
361
promotedTupleTyCon sort i = promoteTyCon (tupleTyCon sort i)
362

363
promotedTupleDataCon :: TupleSort -> Arity -> TyCon
364
promotedTupleDataCon sort i = promoteDataCon (tupleCon sort i)
365

batterseapower's avatar
batterseapower committed
366
tupleCon :: TupleSort -> Arity -> DataCon
367
tupleCon sort i | i > mAX_TUPLE_SIZE = snd (mk_tuple sort i)    -- Build one specially
batterseapower's avatar
batterseapower committed
368
369
tupleCon BoxedTuple   i = snd (boxedTupleArr   ! i)
tupleCon UnboxedTuple i = snd (unboxedTupleArr ! i)
370
tupleCon ConstraintTuple    i = snd (factTupleArr    ! i)
batterseapower's avatar
batterseapower committed
371
372
373
374

boxedTupleArr, unboxedTupleArr, factTupleArr :: Array Int (TyCon,DataCon)
boxedTupleArr   = listArray (0,mAX_TUPLE_SIZE) [mk_tuple BoxedTuple i | i <- [0..mAX_TUPLE_SIZE]]
unboxedTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple UnboxedTuple i | i <- [0..mAX_TUPLE_SIZE]]
375
factTupleArr = listArray (0,mAX_TUPLE_SIZE) [mk_tuple ConstraintTuple i | i <- [0..mAX_TUPLE_SIZE]]
batterseapower's avatar
batterseapower committed
376
377
378

mk_tuple :: TupleSort -> Int -> (TyCon,DataCon)
mk_tuple sort arity = (tycon, tuple_con)
379
  where
380
        tycon   = mkTupleTyCon tc_name tc_kind arity tyvars tuple_con sort prom_tc
381
        prom_tc = case sort of
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
          BoxedTuple      -> Just (mkPromotedTyCon tycon (promoteKind tc_kind))
          UnboxedTuple    -> Nothing
          ConstraintTuple -> Nothing

        modu    = mkTupleModule sort arity
        tc_name = mkWiredInName modu (mkTupleOcc tcName sort arity) tc_uniq
                                (ATyCon tycon) BuiltInSyntax
        tc_kind = mkArrowKinds (map tyVarKind tyvars) res_kind
        res_kind = case sort of
          BoxedTuple      -> liftedTypeKind
          UnboxedTuple    -> unliftedTypeKind
          ConstraintTuple -> constraintKind

        tyvars = take arity $ case sort of
          BoxedTuple      -> alphaTyVars
          UnboxedTuple    -> openAlphaTyVars
          ConstraintTuple -> tyVarList constraintKind

        tuple_con = pcDataCon dc_name tyvars tyvar_tys tycon
        tyvar_tys = mkTyVarTys tyvars
        dc_name   = mkWiredInName modu (mkTupleOcc dataName sort arity) dc_uniq
                                  (ADataCon tuple_con) BuiltInSyntax
        tc_uniq   = mkTupleTyConUnique   sort arity
        dc_uniq   = mkTupleDataConUnique sort arity
406

Ian Lynagh's avatar
Ian Lynagh committed
407
unitTyCon :: TyCon
batterseapower's avatar
batterseapower committed
408
unitTyCon     = tupleTyCon BoxedTuple 0
Ian Lynagh's avatar
Ian Lynagh committed
409
unitDataCon :: DataCon
410
unitDataCon   = head (tyConDataCons unitTyCon)
Ian Lynagh's avatar
Ian Lynagh committed
411
unitDataConId :: Id
412
unitDataConId = dataConWorkId unitDataCon
413

Ian Lynagh's avatar
Ian Lynagh committed
414
pairTyCon :: TyCon
batterseapower's avatar
batterseapower committed
415
pairTyCon = tupleTyCon BoxedTuple 2
416

417
418
419
420
421
unboxedUnitTyCon :: TyCon
unboxedUnitTyCon   = tupleTyCon UnboxedTuple 0
unboxedUnitDataCon :: DataCon
unboxedUnitDataCon = tupleCon   UnboxedTuple 0

Ian Lynagh's avatar
Ian Lynagh committed
422
unboxedSingletonTyCon :: TyCon
batterseapower's avatar
batterseapower committed
423
unboxedSingletonTyCon   = tupleTyCon UnboxedTuple 1
Ian Lynagh's avatar
Ian Lynagh committed
424
unboxedSingletonDataCon :: DataCon
batterseapower's avatar
batterseapower committed
425
unboxedSingletonDataCon = tupleCon   UnboxedTuple 1
426

Ian Lynagh's avatar
Ian Lynagh committed
427
unboxedPairTyCon :: TyCon
batterseapower's avatar
batterseapower committed
428
unboxedPairTyCon   = tupleTyCon UnboxedTuple 2
Ian Lynagh's avatar
Ian Lynagh committed
429
unboxedPairDataCon :: DataCon
batterseapower's avatar
batterseapower committed
430
unboxedPairDataCon = tupleCon   UnboxedTuple 2
431
\end{code}
432

433

434
%************************************************************************
435
%*                                                                      *
436
\subsection[TysWiredIn-boxed-prim]{The ``boxed primitive'' types (@Char@, @Int@, etc)}
437
%*                                                                      *
438
439
%************************************************************************

batterseapower's avatar
batterseapower committed
440
441
442
\begin{code}
eqTyCon :: TyCon
eqTyCon = mkAlgTyCon eqTyConName
dreixel's avatar
dreixel committed
443
444
            (ForAllTy kv $ mkArrowKinds [k, k] constraintKind)
            [kv, a, b]
445
            [Nominal, Nominal, Nominal]
446
            Nothing
batterseapower's avatar
batterseapower committed
447
448
449
450
451
            []      -- No stupid theta
            (DataTyCon [eqBoxDataCon] False)
            NoParentTyCon
            NonRecursive
            False
452
            Nothing   -- No parent for constraint-kinded types
dreixel's avatar
dreixel committed
453
454
455
456
457
  where
    kv = kKiVar
    k = mkTyVarTy kv
    a:b:_ = tyVarList k

batterseapower's avatar
batterseapower committed
458
eqBoxDataCon :: DataCon
dreixel's avatar
dreixel committed
459
460
461
462
463
464
eqBoxDataCon = pcDataCon eqBoxDataConName args [TyConApp eqPrimTyCon (map mkTyVarTy args)] eqTyCon
  where
    kv = kKiVar
    k = mkTyVarTy kv
    a:b:_ = tyVarList k
    args = [kv, a, b]
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482


coercibleTyCon :: TyCon
coercibleTyCon = mkClassTyCon
    coercibleTyConName kind tvs [Representational, Representational]
    rhs coercibleClass NonRecursive
  where kind = mkArrowKinds [liftedTypeKind, liftedTypeKind] constraintKind
        a:b:_ = tyVarList liftedTypeKind
        tvs = [a, b]
        rhs = DataTyCon [coercibleDataCon] False

coercibleDataCon :: DataCon
coercibleDataCon = pcDataCon coercibleDataConName args [TyConApp eqReprPrimTyCon (liftedTypeKind : map mkTyVarTy args)] coercibleTyCon
  where
    a:b:_ = tyVarList liftedTypeKind
    args = [a, b]

coercibleClass :: Class
483
coercibleClass = mkClass (tyConTyVars coercibleTyCon) [] [] [] [] [] (mkAnd []) coercibleTyCon
484

batterseapower's avatar
batterseapower committed
485
486
\end{code}

487
\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
488
charTy :: Type
489
charTy = mkTyConTy charTyCon
490

Ian Lynagh's avatar
Ian Lynagh committed
491
charTyCon :: TyCon
492
493
charTyCon   = pcNonRecDataTyCon charTyConName
                                (Just (CType Nothing (fsLit "HsChar")))
494
                                [] [charDataCon]
Ian Lynagh's avatar
Ian Lynagh committed
495
charDataCon :: DataCon
496
charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon
497

Ian Lynagh's avatar
Ian Lynagh committed
498
stringTy :: Type
499
stringTy = mkListTy charTy -- convenience only
500
501
\end{code}

502
503
504
505
\begin{code}
integerTyCon :: TyCon
integerTyCon = case cIntegerLibraryType of
               IntegerGMP ->
506
                   pcNonRecDataTyCon integerRealTyConName Nothing []
507
                                     [integerGmpSDataCon, integerGmpJDataCon]
508
509
               _ ->
                   panic "Evaluated integerTyCon, but not using IntegerGMP"
510
511
512
513
514
515
516
517
518
519
520
521
522
523

integerGmpSDataCon :: DataCon
integerGmpSDataCon = pcDataCon integerGmpSDataConName []
                               [intPrimTy]
                               integerTyCon

-- integerGmpJDataCon isn't exported, but we need to define it to fill
-- out integerTyCon
integerGmpJDataCon :: DataCon
integerGmpJDataCon = pcDataCon integerGmpJDataConName []
                               [intPrimTy, byteArrayPrimTy]
                               integerTyCon
\end{code}

524
\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
525
intTy :: Type
526
intTy = mkTyConTy intTyCon
527

Ian Lynagh's avatar
Ian Lynagh committed
528
intTyCon :: TyCon
529
intTyCon = pcNonRecDataTyCon intTyConName (Just (CType Nothing (fsLit "HsInt"))) [] [intDataCon]
Ian Lynagh's avatar
Ian Lynagh committed
530
intDataCon :: DataCon
531
intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon
532
533
\end{code}

Ian Lynagh's avatar
Ian Lynagh committed
534
535
\begin{code}
wordTy :: Type
536
wordTy = mkTyConTy wordTyCon
Ian Lynagh's avatar
Ian Lynagh committed
537
538

wordTyCon :: TyCon
539
wordTyCon = pcNonRecDataTyCon wordTyConName (Just (CType Nothing (fsLit "HsWord"))) [] [wordDataCon]
Ian Lynagh's avatar
Ian Lynagh committed
540
541
542
543
wordDataCon :: DataCon
wordDataCon = pcDataCon wordDataConName [] [wordPrimTy] wordTyCon
\end{code}

544
\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
545
floatTy :: Type
546
floatTy = mkTyConTy floatTyCon
547

Ian Lynagh's avatar
Ian Lynagh committed
548
floatTyCon :: TyCon
549
floatTyCon   = pcNonRecDataTyCon floatTyConName   (Just (CType Nothing (fsLit "HsFloat"))) [] [floatDataCon]
Ian Lynagh's avatar
Ian Lynagh committed
550
floatDataCon :: DataCon
551
floatDataCon = pcDataCon         floatDataConName [] [floatPrimTy] floatTyCon
552
553
554
\end{code}

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
555
doubleTy :: Type
556
doubleTy = mkTyConTy doubleTyCon
557

Ian Lynagh's avatar
Ian Lynagh committed
558
doubleTyCon :: TyCon
559
doubleTyCon = pcNonRecDataTyCon doubleTyConName (Just (CType Nothing (fsLit "HsDouble"))) [] [doubleDataCon]
Ian Lynagh's avatar
Ian Lynagh committed
560
561
562

doubleDataCon :: DataCon
doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon
563
564
565
566
\end{code}


%************************************************************************
567
%*                                                                      *
568
\subsection[TysWiredIn-Bool]{The @Bool@ type}
569
%*                                                                      *
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
%************************************************************************

An ordinary enumeration type, but deeply wired in.  There are no
magical operations on @Bool@ (just the regular Prelude code).

{\em BEGIN IDLE SPECULATION BY SIMON}

This is not the only way to encode @Bool@.  A more obvious coding makes
@Bool@ just a boxed up version of @Bool#@, like this:
\begin{verbatim}
type Bool# = Int#
data Bool = MkBool Bool#
\end{verbatim}

Unfortunately, this doesn't correspond to what the Report says @Bool@
looks like!  Furthermore, we get slightly less efficient code (I
think) with this coding. @gtInt@ would look like this:

\begin{verbatim}
gtInt :: Int -> Int -> Bool
gtInt x y = case x of I# x# ->
591
592
593
            case y of I# y# ->
            case (gtIntPrim x# y#) of
                b# -> MkBool b#
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
\end{verbatim}

Notice that the result of the @gtIntPrim@ comparison has to be turned
into an integer (here called @b#@), and returned in a @MkBool@ box.

The @if@ expression would compile to this:
\begin{verbatim}
case (gtInt x y) of
  MkBool b# -> case b# of { 1# -> e1; 0# -> e2 }
\end{verbatim}

I think this code is a little less efficient than the previous code,
but I'm not certain.  At all events, corresponding with the Report is
important.  The interesting thing is that the language is expressive
enough to describe more than one alternative; and that a type doesn't
necessarily need to be a straightforwardly boxed version of its
primitive counterpart.

{\em END IDLE SPECULATION BY SIMON}

\begin{code}
Ian Lynagh's avatar
Ian Lynagh committed
615
boolTy :: Type
616
boolTy = mkTyConTy boolTyCon
617

Ian Lynagh's avatar
Ian Lynagh committed
618
boolTyCon :: TyCon
619
boolTyCon = pcTyCon True NonRecursive True boolTyConName
620
                    (Just (CType Nothing (fsLit "HsBool")))
621
                    [] [falseDataCon, trueDataCon]
622

Ian Lynagh's avatar
Ian Lynagh committed
623
falseDataCon, trueDataCon :: DataCon
624
625
falseDataCon = pcDataCon falseDataConName [] [] boolTyCon
trueDataCon  = pcDataCon trueDataConName  [] [] boolTyCon
626

Ian Lynagh's avatar
Ian Lynagh committed
627
falseDataConId, trueDataConId :: Id
628
629
falseDataConId = dataConWorkId falseDataCon
trueDataConId  = dataConWorkId trueDataCon
630
631

orderingTyCon :: TyCon
632
orderingTyCon = pcTyCon True NonRecursive True orderingTyConName Nothing
633
634
635
636
637
638
639
640
641
642
643
                        [] [ltDataCon, eqDataCon, gtDataCon]

ltDataCon, eqDataCon, gtDataCon :: DataCon
ltDataCon = pcDataCon ltDataConName  [] [] orderingTyCon
eqDataCon = pcDataCon eqDataConName  [] [] orderingTyCon
gtDataCon = pcDataCon gtDataConName  [] [] orderingTyCon

ltDataConId, eqDataConId, gtDataConId :: Id
ltDataConId = dataConWorkId ltDataCon
eqDataConId = dataConWorkId eqDataCon
gtDataConId = dataConWorkId gtDataCon
644
645
646
\end{code}

%************************************************************************
647
%*                                                                      *
648
\subsection[TysWiredIn-List]{The @List@ type (incl ``build'' magic)}
649
%*                                                                      *
650
651
652
%************************************************************************

Special syntax, deeply wired in, but otherwise an ordinary algebraic
653
data types:
654
\begin{verbatim}
655
656
657
658
data [] a = [] | a : (List a)
data () = ()
data (,) a b = (,,) a b
...
659
660
661
\end{verbatim}

\begin{code}
662
mkListTy :: Type -> Type
663
mkListTy ty = mkTyConApp listTyCon [ty]
664

Ian Lynagh's avatar
Ian Lynagh committed
665
listTyCon :: TyCon
666
667
listTyCon = pcTyCon False Recursive True
                    listTyConName Nothing alpha_tyvar [nilDataCon, consDataCon]
668

669
670
671
672
mkPromotedListTy :: Type -> Type
mkPromotedListTy ty = mkTyConApp promotedListTyCon [ty]

promotedListTyCon :: TyCon
673
promotedListTyCon = promoteTyCon listTyCon
674

Ian Lynagh's avatar
Ian Lynagh committed
675
nilDataCon :: DataCon
676
nilDataCon  = pcDataCon nilDataConName alpha_tyvar [] listTyCon
677

Ian Lynagh's avatar
Ian Lynagh committed
678
consDataCon :: DataCon
679
consDataCon = pcDataConWithFixity True {- Declared infix -}
680
681
               consDataConName
               alpha_tyvar [alphaTy, mkTyConApp listTyCon alpha_ty] listTyCon
682
683
684
-- Interesting: polymorphic recursion would help here.
-- We can't use (mkListTy alphaTy) in the defn of consDataCon, else mkListTy
-- gets the over-specific type (Type -> Type)
685
686
687
\end{code}

%************************************************************************
688
%*                                                                      *
689
\subsection[TysWiredIn-Tuples]{The @Tuple@ types}
690
%*                                                                      *
691
692
693
694
695
696
697
%************************************************************************

The tuple types are definitely magic, because they form an infinite
family.

\begin{itemize}
\item
698
They have a special family of type constructors, of type @TyCon@
699
700
701
702
These contain the tycon arity, but don't require a Unique.

\item
They have a special family of constructors, of type
703
@Id@. Again these contain their arity but don't need a Unique.
704
705
706
707
708
709
710
711

\item
There should be a magic way of generating the info tables and
entry code for all tuples.

But at the moment we just compile a Haskell source
file\srcloc{lib/prelude/...} containing declarations like:
\begin{verbatim}
712
713
714
715
data Tuple0             = Tup0
data Tuple2  a b        = Tup2  a b
data Tuple3  a b c      = Tup3  a b c
data Tuple4  a b c d    = Tup4  a b c d
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
...
\end{verbatim}
The print-names associated with the magic @Id@s for tuple constructors
``just happen'' to be the same as those generated by these
declarations.

\item
The instance environment should have a magic way to know
that each tuple type is an instances of classes @Eq@, @Ix@, @Ord@ and
so on. \ToDo{Not implemented yet.}

\item
There should also be a way to generate the appropriate code for each
of these instances, but (like the info tables and entry code) it is
done by enumeration\srcloc{lib/prelude/InTup?.hs}.
\end{itemize}

\begin{code}
batterseapower's avatar
batterseapower committed
734
mkTupleTy :: TupleSort -> [Type] -> Type
735
-- Special case for *boxed* 1-tuples, which are represented by the type itself
batterseapower's avatar
batterseapower committed
736
737
mkTupleTy sort [ty] | Boxed <- tupleSortBoxity sort = ty
mkTupleTy sort tys = mkTyConApp (tupleTyCon sort (length tys)) tys
738

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
739
740
-- | Build the type of a small tuple that holds the specified type of thing
mkBoxedTupleTy :: [Type] -> Type
batterseapower's avatar
batterseapower committed
741
mkBoxedTupleTy tys = mkTupleTy BoxedTuple tys
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
742

Ian Lynagh's avatar
Ian Lynagh committed
743
unitTy :: Type
batterseapower's avatar
batterseapower committed
744
unitTy = mkTupleTy BoxedTuple []
745
\end{code}
746

chak's avatar
chak committed
747
%************************************************************************
748
%*                                                                      *
chak's avatar
chak committed
749
\subsection[TysWiredIn-PArr]{The @[::]@ type}
750
%*                                                                      *
chak's avatar
chak committed
751
752
753
754
755
%************************************************************************

Special syntax for parallel arrays needs some wired in definitions.

\begin{code}
756
-- | Construct a type representing the application of the parallel array constructor
chak's avatar
chak committed
757
758
759
mkPArrTy    :: Type -> Type
mkPArrTy ty  = mkTyConApp parrTyCon [ty]

760
-- | Represents the type constructor of parallel arrays
chak's avatar
chak committed
761
--
762
--  * This must match the definition in @PrelPArr@
chak's avatar
chak committed
763
764
765
--
-- NB: Although the constructor is given here, it will not be accessible in
--     user code as it is not in the environment of any compiled module except
766
--     @PrelPArr@.
chak's avatar
chak committed
767
768
--
parrTyCon :: TyCon
769
parrTyCon  = pcNonRecDataTyCon parrTyConName Nothing alpha_tyvar [parrDataCon]
chak's avatar
chak committed
770
771

parrDataCon :: DataCon
772
773
parrDataCon  = pcDataCon
                 parrDataConName
774
775
776
                 alpha_tyvar            -- forall'ed type variables
                 [intTy,                -- 1st argument: Int
                  mkTyConApp            -- 2nd argument: Array# a
777
778
                    arrayPrimTyCon
                    alpha_ty]
779
                 parrTyCon
chak's avatar
chak committed
780

781
-- | Check whether a type constructor is the constructor for parallel arrays
chak's avatar
chak committed
782
783
784
isPArrTyCon    :: TyCon -> Bool
isPArrTyCon tc  = tyConName tc == parrTyConName

785
-- | Fake array constructors
chak's avatar
chak committed
786
--
787
-- * These constructors are never really used to represent array values;
chak's avatar
chak committed
788
789
790
791
792
--   however, they are very convenient during desugaring (and, in particular,
--   in the pattern matching compiler) to treat array pattern just like
--   yet another constructor pattern
--
parrFakeCon                        :: Arity -> DataCon
793
parrFakeCon i | i > mAX_TUPLE_SIZE  = mkPArrFakeCon  i  -- build one specially
chak's avatar
chak committed
794
795
796
797
798
parrFakeCon i                       = parrFakeConArr!i

-- pre-defined set of constructors
--
parrFakeConArr :: Array Int DataCon
799
parrFakeConArr  = array (0, mAX_TUPLE_SIZE) [(i, mkPArrFakeCon i)
800
                                            | i <- [0..mAX_TUPLE_SIZE]]
chak's avatar
chak committed
801
802
803
804

-- build a fake parallel array constructor for the given arity
--
mkPArrFakeCon       :: Int -> DataCon
805
mkPArrFakeCon arity  = data_con
chak's avatar
chak committed
806
  where
807
808
809
        data_con  = pcDataCon name [tyvar] tyvarTys parrTyCon
        tyvar     = head alphaTyVars
        tyvarTys  = replicate arity $ mkTyVarTy tyvar
810
        nameStr   = mkFastString ("MkPArr" ++ show arity)
811
812
813
        name      = mkWiredInName gHC_PARR' (mkDataOccFS nameStr) unique
                                  (ADataCon data_con) UserSyntax
        unique      = mkPArrDataConUnique arity
chak's avatar
chak committed
814

815
-- | Checks whether a data constructor is a fake constructor for parallel arrays
chak's avatar
chak committed
816
817
818
isPArrFakeCon      :: DataCon -> Bool
isPArrFakeCon dcon  = dcon == parrFakeCon (dataConSourceArity dcon)
\end{code}
819
820
821
822
823
824
825
826
827
828
829
830

Promoted Booleans

\begin{code}
promotedBoolTyCon, promotedFalseDataCon, promotedTrueDataCon :: TyCon
promotedBoolTyCon     = promoteTyCon boolTyCon
promotedTrueDataCon   = promoteDataCon trueDataCon
promotedFalseDataCon  = promoteDataCon falseDataCon
\end{code}