9b62c27df3f3baf14b7bb2b9ef676a0b9fddcd1e
[ghc.git] / compiler / basicTypes / DataCon.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1998
4
5 \section[DataCon]{@DataCon@: Data Constructors}
6 -}
7
8 {-# LANGUAGE CPP, DeriveDataTypeable #-}
9
10 module DataCon (
11 -- * Main data types
12 DataCon, DataConRep(..),
13 SrcStrictness(..), SrcUnpackedness(..),
14 HsSrcBang(..), HsImplBang(..),
15 StrictnessMark(..),
16 ConTag,
17
18 -- ** Equality specs
19 EqSpec, mkEqSpec, eqSpecTyVar, eqSpecType,
20 eqSpecPair, eqSpecPreds,
21 substEqSpec, filterEqSpec,
22
23 -- ** Field labels
24 FieldLbl(..), FieldLabel, FieldLabelString,
25
26 -- ** Type construction
27 mkDataCon, buildAlgTyCon, buildSynTyCon, fIRST_TAG,
28
29 -- ** Type deconstruction
30 dataConRepType, dataConSig, dataConInstSig, dataConFullSig,
31 dataConName, dataConIdentity, dataConTag, dataConTagZ,
32 dataConTyCon, dataConOrigTyCon,
33 dataConUserType,
34 dataConUnivTyVars, dataConExTyVars, dataConUnivAndExTyVars,
35 dataConUserTyVars, dataConUserTyVarBinders,
36 dataConEqSpec, dataConTheta,
37 dataConStupidTheta,
38 dataConInstArgTys, dataConOrigArgTys, dataConOrigResTy,
39 dataConInstOrigArgTys, dataConRepArgTys,
40 dataConFieldLabels, dataConFieldType, dataConFieldType_maybe,
41 dataConSrcBangs,
42 dataConSourceArity, dataConRepArity,
43 dataConIsInfix,
44 dataConWorkId, dataConWrapId, dataConWrapId_maybe,
45 dataConImplicitTyThings,
46 dataConRepStrictness, dataConImplBangs, dataConBoxer,
47
48 splitDataProductType_maybe,
49
50 -- ** Predicates on DataCons
51 isNullarySrcDataCon, isNullaryRepDataCon, isTupleDataCon, isUnboxedTupleCon,
52 isUnboxedSumCon,
53 isVanillaDataCon, classDataCon, dataConCannotMatch,
54 dataConUserTyVarsArePermuted,
55 isBanged, isMarkedStrict, eqHsBang, isSrcStrict, isSrcUnpacked,
56 specialPromotedDc,
57
58 -- ** Promotion related functions
59 promoteDataCon
60 ) where
61
62 #include "HsVersions.h"
63
64 import GhcPrelude
65
66 import {-# SOURCE #-} MkId( DataConBoxer )
67 import Type
68 import ForeignCall ( CType )
69 import Coercion
70 import Unify
71 import TyCon
72 import FieldLabel
73 import Class
74 import Name
75 import PrelNames
76 import Var
77 import Outputable
78 import Util
79 import BasicTypes
80 import FastString
81 import Module
82 import Binary
83 import UniqSet
84 import Unique( mkAlphaTyVarUnique )
85
86 import qualified Data.Data as Data
87 import Data.Char
88 import Data.Word
89 import Data.List( find )
90
91 {-
92 Data constructor representation
93 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94 Consider the following Haskell data type declaration
95
96 data T = T !Int ![Int]
97
98 Using the strictness annotations, GHC will represent this as
99
100 data T = T Int# [Int]
101
102 That is, the Int has been unboxed. Furthermore, the Haskell source construction
103
104 T e1 e2
105
106 is translated to
107
108 case e1 of { I# x ->
109 case e2 of { r ->
110 T x r }}
111
112 That is, the first argument is unboxed, and the second is evaluated. Finally,
113 pattern matching is translated too:
114
115 case e of { T a b -> ... }
116
117 becomes
118
119 case e of { T a' b -> let a = I# a' in ... }
120
121 To keep ourselves sane, we name the different versions of the data constructor
122 differently, as follows.
123
124
125 Note [Data Constructor Naming]
126 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127 Each data constructor C has two, and possibly up to four, Names associated with it:
128
129 OccName Name space Name of Notes
130 ---------------------------------------------------------------------------
131 The "data con itself" C DataName DataCon In dom( GlobalRdrEnv )
132 The "worker data con" C VarName Id The worker
133 The "wrapper data con" $WC VarName Id The wrapper
134 The "newtype coercion" :CoT TcClsName TyCon
135
136 EVERY data constructor (incl for newtypes) has the former two (the
137 data con itself, and its worker. But only some data constructors have a
138 wrapper (see Note [The need for a wrapper]).
139
140 Each of these three has a distinct Unique. The "data con itself" name
141 appears in the output of the renamer, and names the Haskell-source
142 data constructor. The type checker translates it into either the wrapper Id
143 (if it exists) or worker Id (otherwise).
144
145 The data con has one or two Ids associated with it:
146
147 The "worker Id", is the actual data constructor.
148 * Every data constructor (newtype or data type) has a worker
149
150 * The worker is very like a primop, in that it has no binding.
151
152 * For a *data* type, the worker *is* the data constructor;
153 it has no unfolding
154
155 * For a *newtype*, the worker has a compulsory unfolding which
156 does a cast, e.g.
157 newtype T = MkT Int
158 The worker for MkT has unfolding
159 \\(x:Int). x `cast` sym CoT
160 Here CoT is the type constructor, witnessing the FC axiom
161 axiom CoT : T = Int
162
163 The "wrapper Id", \$WC, goes as follows
164
165 * Its type is exactly what it looks like in the source program.
166
167 * It is an ordinary function, and it gets a top-level binding
168 like any other function.
169
170 * The wrapper Id isn't generated for a data type if there is
171 nothing for the wrapper to do. That is, if its defn would be
172 \$wC = C
173
174 Note [The need for a wrapper]
175 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
176 Why might the wrapper have anything to do? Two reasons:
177
178 * Unboxing strict fields (with -funbox-strict-fields)
179 data T = MkT !(Int,Int)
180 \$wMkT :: (Int,Int) -> T
181 \$wMkT (x,y) = MkT x y
182 Notice that the worker has two fields where the wapper has
183 just one. That is, the worker has type
184 MkT :: Int -> Int -> T
185
186 * Equality constraints for GADTs
187 data T a where { MkT :: a -> T [a] }
188
189 The worker gets a type with explicit equality
190 constraints, thus:
191 MkT :: forall a b. (a=[b]) => b -> T a
192
193 The wrapper has the programmer-specified type:
194 \$wMkT :: a -> T [a]
195 \$wMkT a x = MkT [a] a [a] x
196 The third argument is a coercion
197 [a] :: [a]~[a]
198
199 INVARIANT: the dictionary constructor for a class
200 never has a wrapper.
201
202
203 A note about the stupid context
204 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205 Data types can have a context:
206
207 data (Eq a, Ord b) => T a b = T1 a b | T2 a
208
209 and that makes the constructors have a context too
210 (notice that T2's context is "thinned"):
211
212 T1 :: (Eq a, Ord b) => a -> b -> T a b
213 T2 :: (Eq a) => a -> T a b
214
215 Furthermore, this context pops up when pattern matching
216 (though GHC hasn't implemented this, but it is in H98, and
217 I've fixed GHC so that it now does):
218
219 f (T2 x) = x
220 gets inferred type
221 f :: Eq a => T a b -> a
222
223 I say the context is "stupid" because the dictionaries passed
224 are immediately discarded -- they do nothing and have no benefit.
225 It's a flaw in the language.
226
227 Up to now [March 2002] I have put this stupid context into the
228 type of the "wrapper" constructors functions, T1 and T2, but
229 that turned out to be jolly inconvenient for generics, and
230 record update, and other functions that build values of type T
231 (because they don't have suitable dictionaries available).
232
233 So now I've taken the stupid context out. I simply deal with
234 it separately in the type checker on occurrences of a
235 constructor, either in an expression or in a pattern.
236
237 [May 2003: actually I think this decision could easily be
238 reversed now, and probably should be. Generics could be
239 disabled for types with a stupid context; record updates now
240 (H98) needs the context too; etc. It's an unforced change, so
241 I'm leaving it for now --- but it does seem odd that the
242 wrapper doesn't include the stupid context.]
243
244 [July 04] With the advent of generalised data types, it's less obvious
245 what the "stupid context" is. Consider
246 C :: forall a. Ord a => a -> a -> T (Foo a)
247 Does the C constructor in Core contain the Ord dictionary? Yes, it must:
248
249 f :: T b -> Ordering
250 f = /\b. \x:T b.
251 case x of
252 C a (d:Ord a) (p:a) (q:a) -> compare d p q
253
254 Note that (Foo a) might not be an instance of Ord.
255
256 ************************************************************************
257 * *
258 \subsection{Data constructors}
259 * *
260 ************************************************************************
261 -}
262
263 -- | A data constructor
264 --
265 -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
266 -- 'ApiAnnotation.AnnClose','ApiAnnotation.AnnComma'
267
268 -- For details on above see note [Api annotations] in ApiAnnotation
269 data DataCon
270 = MkData {
271 dcName :: Name, -- This is the name of the *source data con*
272 -- (see "Note [Data Constructor Naming]" above)
273 dcUnique :: Unique, -- Cached from Name
274 dcTag :: ConTag, -- ^ Tag, used for ordering 'DataCon's
275
276 -- Running example:
277 --
278 -- *** As declared by the user
279 -- data T a b c where
280 -- MkT :: forall c y x b. (x~y,Ord x) => x -> y -> T (x,y) b c
281
282 -- *** As represented internally
283 -- data T a b c where
284 -- MkT :: forall a b c. forall x y. (a~(x,y),x~y,Ord x)
285 -- => x -> y -> T a b c
286 --
287 -- The next six fields express the type of the constructor, in pieces
288 -- e.g.
289 --
290 -- dcUnivTyVars = [a,b,c]
291 -- dcExTyVars = [x,y]
292 -- dcUserTyVarBinders = [c,y,x,b]
293 -- dcEqSpec = [a~(x,y)]
294 -- dcOtherTheta = [x~y, Ord x]
295 -- dcOrigArgTys = [x,y]
296 -- dcRepTyCon = T
297
298 -- In general, the dcUnivTyVars are NOT NECESSARILY THE SAME AS THE TYVARS
299 -- FOR THE PARENT TyCon. (This is a change (Oct05): previously, vanilla
300 -- datacons guaranteed to have the same type variables as their parent TyCon,
301 -- but that seems ugly.) They can be different in the case where a GADT
302 -- constructor uses different names for the universal tyvars than does
303 -- the tycon. For example:
304 --
305 -- data H a where
306 -- MkH :: b -> H b
307 --
308 -- Here, the tyConTyVars of H will be [a], but the dcUnivTyVars of MkH
309 -- will be [b].
310
311 dcVanilla :: Bool, -- True <=> This is a vanilla Haskell 98 data constructor
312 -- Its type is of form
313 -- forall a1..an . t1 -> ... tm -> T a1..an
314 -- No existentials, no coercions, nothing.
315 -- That is: dcExTyVars = dcEqSpec = dcOtherTheta = []
316 -- NB 1: newtypes always have a vanilla data con
317 -- NB 2: a vanilla constructor can still be declared in GADT-style
318 -- syntax, provided its type looks like the above.
319 -- The declaration format is held in the TyCon (algTcGadtSyntax)
320
321 -- Universally-quantified type vars [a,b,c]
322 -- INVARIANT: length matches arity of the dcRepTyCon
323 -- INVARIANT: result type of data con worker is exactly (T a b c)
324 -- COROLLARY: The dcUnivTyVars are always in one-to-one correspondence with
325 -- the tyConTyVars of the parent TyCon
326 dcUnivTyVars :: [TyVar],
327
328 -- Existentially-quantified type vars [x,y]
329 dcExTyVars :: [TyVar],
330
331 -- INVARIANT: the UnivTyVars and ExTyVars all have distinct OccNames
332 -- Reason: less confusing, and easier to generate IfaceSyn
333
334 -- The type vars in the order the user wrote them [c,y,x,b]
335 -- INVARIANT: the set of tyvars in dcUserTyVarBinders is exactly the
336 -- set of dcExTyVars unioned with the set of dcUnivTyVars
337 -- whose tyvars do not appear in dcEqSpec
338 -- See Note [DataCon user type variable binders]
339 dcUserTyVarBinders :: [TyVarBinder],
340
341 dcEqSpec :: [EqSpec], -- Equalities derived from the result type,
342 -- _as written by the programmer_
343
344 -- This field allows us to move conveniently between the two ways
345 -- of representing a GADT constructor's type:
346 -- MkT :: forall a b. (a ~ [b]) => b -> T a
347 -- MkT :: forall b. b -> T [b]
348 -- Each equality is of the form (a ~ ty), where 'a' is one of
349 -- the universally quantified type variables
350
351 -- The next two fields give the type context of the data constructor
352 -- (aside from the GADT constraints,
353 -- which are given by the dcExpSpec)
354 -- In GADT form, this is *exactly* what the programmer writes, even if
355 -- the context constrains only universally quantified variables
356 -- MkT :: forall a b. (a ~ b, Ord b) => a -> T a b
357 dcOtherTheta :: ThetaType, -- The other constraints in the data con's type
358 -- other than those in the dcEqSpec
359
360 dcStupidTheta :: ThetaType, -- The context of the data type declaration
361 -- data Eq a => T a = ...
362 -- or, rather, a "thinned" version thereof
363 -- "Thinned", because the Report says
364 -- to eliminate any constraints that don't mention
365 -- tyvars free in the arg types for this constructor
366 --
367 -- INVARIANT: the free tyvars of dcStupidTheta are a subset of dcUnivTyVars
368 -- Reason: dcStupidTeta is gotten by thinning the stupid theta from the tycon
369 --
370 -- "Stupid", because the dictionaries aren't used for anything.
371 -- Indeed, [as of March 02] they are no longer in the type of
372 -- the wrapper Id, because that makes it harder to use the wrap-id
373 -- to rebuild values after record selection or in generics.
374
375 dcOrigArgTys :: [Type], -- Original argument types
376 -- (before unboxing and flattening of strict fields)
377 dcOrigResTy :: Type, -- Original result type, as seen by the user
378 -- NB: for a data instance, the original user result type may
379 -- differ from the DataCon's representation TyCon. Example
380 -- data instance T [a] where MkT :: a -> T [a]
381 -- The OrigResTy is T [a], but the dcRepTyCon might be :T123
382
383 -- Now the strictness annotations and field labels of the constructor
384 dcSrcBangs :: [HsSrcBang],
385 -- See Note [Bangs on data constructor arguments]
386 --
387 -- The [HsSrcBang] as written by the programmer.
388 --
389 -- Matches 1-1 with dcOrigArgTys
390 -- Hence length = dataConSourceArity dataCon
391
392 dcFields :: [FieldLabel],
393 -- Field labels for this constructor, in the
394 -- same order as the dcOrigArgTys;
395 -- length = 0 (if not a record) or dataConSourceArity.
396
397 -- The curried worker function that corresponds to the constructor:
398 -- It doesn't have an unfolding; the code generator saturates these Ids
399 -- and allocates a real constructor when it finds one.
400 dcWorkId :: Id,
401
402 -- Constructor representation
403 dcRep :: DataConRep,
404
405 -- Cached; see Note [DataCon arities]
406 -- INVARIANT: dcRepArity == length dataConRepArgTys
407 -- INVARIANT: dcSourceArity == length dcOrigArgTys
408 dcRepArity :: Arity,
409 dcSourceArity :: Arity,
410
411 -- Result type of constructor is T t1..tn
412 dcRepTyCon :: TyCon, -- Result tycon, T
413
414 dcRepType :: Type, -- Type of the constructor
415 -- forall a x y. (a~(x,y), x~y, Ord x) =>
416 -- x -> y -> T a
417 -- (this is *not* of the constructor wrapper Id:
418 -- see Note [Data con representation] below)
419 -- Notice that the existential type parameters come *second*.
420 -- Reason: in a case expression we may find:
421 -- case (e :: T t) of
422 -- MkT x y co1 co2 (d:Ord x) (v:r) (w:F s) -> ...
423 -- It's convenient to apply the rep-type of MkT to 't', to get
424 -- forall x y. (t~(x,y), x~y, Ord x) => x -> y -> T t
425 -- and use that to check the pattern. Mind you, this is really only
426 -- used in CoreLint.
427
428
429 dcInfix :: Bool, -- True <=> declared infix
430 -- Used for Template Haskell and 'deriving' only
431 -- The actual fixity is stored elsewhere
432
433 dcPromoted :: TyCon -- The promoted TyCon
434 -- See Note [Promoted data constructors] in TyCon
435 }
436
437
438 {- Note [TyVarBinders in DataCons]
439 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
440 For the TyVarBinders in a DataCon and PatSyn:
441
442 * Each argument flag is Inferred or Specified.
443 None are Required. (A DataCon is a term-level function; see
444 Note [No Required TyBinder in terms] in TyCoRep.)
445
446 Why do we need the TyVarBinders, rather than just the TyVars? So that
447 we can construct the right type for the DataCon with its foralls
448 attributed the correct visibility. That in turn governs whether you
449 can use visible type application at a call of the data constructor.
450
451 See also [DataCon user type variable binders] for an extended discussion on the
452 order in which TyVarBinders appear in a DataCon.
453
454 Note [DataCon arities]
455 ~~~~~~~~~~~~~~~~~~~~~~
456 dcSourceArity does not take constraints into account,
457 but dcRepArity does. For example:
458 MkT :: Ord a => a -> T a
459 dcSourceArity = 1
460 dcRepArity = 2
461
462 Note [DataCon user type variable binders]
463 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
464 In System FC, data constructor type signatures always quantify over all of
465 their universal type variables, followed by their existential type variables.
466 Normally, this isn't a problem, as most datatypes naturally quantify their type
467 variables in this order anyway. For example:
468
469 data T a b = forall c. MkT b c
470
471 Here, we have `MkT :: forall {k} (a :: k) (b :: *) (c :: *). b -> c -> T a b`,
472 where k, a, and b are universal and c is existential. (The inferred variable k
473 isn't available for TypeApplications, hence why it's in braces.) This is a
474 perfectly reasonable order to use, as the syntax of H98-style datatypes
475 (+ ExistentialQuantification) suggests it.
476
477 Things become more complicated when GADT syntax enters the picture. Consider
478 this example:
479
480 data X a where
481 MkX :: forall b a. b -> Proxy a -> X a
482
483 If we adopt the earlier approach of quantifying all the universal variables
484 followed by all the existential ones, GHC would come up with this type
485 signature for MkX:
486
487 MkX :: forall {k} (a :: k) (b :: *). b -> Proxy a -> X a
488
489 But this is not what we want at all! After all, if a user were to use
490 TypeApplications on MkX, they would expect to instantiate `b` before `a`,
491 as that's the order in which they were written in the `forall`. (See #11721.)
492 Instead, we'd like GHC to come up with this type signature:
493
494 MkX :: forall {k} (b :: *) (a :: k). b -> Proxy a -> X a
495
496 In fact, even if we left off the explicit forall:
497
498 data X a where
499 MkX :: b -> Proxy a -> X a
500
501 Then a user should still expect `b` to be quantified before `a`, since
502 according to the rules of TypeApplications, in the absence of `forall` GHC
503 performs a stable topological sort on the type variables in the user-written
504 type signature, which would place `b` before `a`.
505
506 But as noted above, enacting this behavior is not entirely trivial, as System
507 FC demands the variables go in universal-then-existential order under the hood.
508 Our solution is thus to equip DataCon with two different sets of type
509 variables:
510
511 * dcUnivTyVars and dcExTyVars, for the universal and existential type
512 variables, respectively. Their order is irrelevant for the purposes of
513 TypeApplications, and as a consequence, they do not come equipped with
514 visibilities (that is, they are TyVars instead of TyVarBinders).
515 * dcUserTyVarBinders, for the type variables binders in the order in which they
516 originally arose in the user-written type signature. Their order *does*
517 matter for TypeApplications, so they are full TyVarBinders, complete
518 with visibilities.
519
520 This encoding has some redundancy. The set of tyvars in dcUserTyVarBinders
521 consists precisely of:
522
523 * The set of tyvars in dcUnivTyVars whose type variables do not appear in
524 dcEqSpec, unioned with:
525 * The set of tyvars in dcExTyVars
526
527 The word "set" is used above because the order in which the tyvars
528 appear in dcUserTyVarBinders can be completely different from the order in
529 dcUnivTyVars or dcExTyVars. That is, the tyvars in dcUserTyVarBinders are a
530 permutation of (dcExTyVars + a subset of dcUnivTyVars). But aside from the
531 ordering, they in fact share the same type variables (with the same Uniques).
532 We sometimes refer to this as "the dcUserTyVarBinders invariant".
533
534 dcUserTyVarBinders, as the name suggests, is the one that users will see most
535 of the time. It's used when computing the type signature of a data constructor
536 (see dataConUserType), and as a result, it's what matters from a
537 TypeApplications perspective.
538 -}
539
540 -- | Data Constructor Representation
541 data DataConRep
542 = NoDataConRep -- No wrapper
543
544 | DCR { dcr_wrap_id :: Id -- Takes src args, unboxes/flattens,
545 -- and constructs the representation
546
547 , dcr_boxer :: DataConBoxer
548
549 , dcr_arg_tys :: [Type] -- Final, representation argument types,
550 -- after unboxing and flattening,
551 -- and *including* all evidence args
552
553 , dcr_stricts :: [StrictnessMark] -- 1-1 with dcr_arg_tys
554 -- See also Note [Data-con worker strictness] in MkId.hs
555
556 , dcr_bangs :: [HsImplBang] -- The actual decisions made (including failures)
557 -- about the original arguments; 1-1 with orig_arg_tys
558 -- See Note [Bangs on data constructor arguments]
559
560 }
561 -- Algebraic data types always have a worker, and
562 -- may or may not have a wrapper, depending on whether
563 -- the wrapper does anything.
564 --
565 -- Data types have a worker with no unfolding
566 -- Newtypes just have a worker, which has a compulsory unfolding (just a cast)
567
568 -- _Neither_ the worker _nor_ the wrapper take the dcStupidTheta dicts as arguments
569
570 -- The wrapper (if it exists) takes dcOrigArgTys as its arguments
571 -- The worker takes dataConRepArgTys as its arguments
572 -- If the worker is absent, dataConRepArgTys is the same as dcOrigArgTys
573
574 -- The 'NoDataConRep' case is important
575 -- Not only is this efficient,
576 -- but it also ensures that the wrapper is replaced
577 -- by the worker (because it *is* the worker)
578 -- even when there are no args. E.g. in
579 -- f (:) x
580 -- the (:) *is* the worker.
581 -- This is really important in rule matching,
582 -- (We could match on the wrappers,
583 -- but that makes it less likely that rules will match
584 -- when we bring bits of unfoldings together.)
585
586 -------------------------
587
588 -- | Haskell Source Bang
589 --
590 -- Bangs on data constructor arguments as the user wrote them in the
591 -- source code.
592 --
593 -- @(HsSrcBang _ SrcUnpack SrcLazy)@ and
594 -- @(HsSrcBang _ SrcUnpack NoSrcStrict)@ (without StrictData) makes no sense, we
595 -- emit a warning (in checkValidDataCon) and treat it like
596 -- @(HsSrcBang _ NoSrcUnpack SrcLazy)@
597 data HsSrcBang =
598 HsSrcBang SourceText -- Note [Pragma source text] in BasicTypes
599 SrcUnpackedness
600 SrcStrictness
601 deriving Data.Data
602
603 -- | Haskell Implementation Bang
604 --
605 -- Bangs of data constructor arguments as generated by the compiler
606 -- after consulting HsSrcBang, flags, etc.
607 data HsImplBang
608 = HsLazy -- ^ Lazy field, or one with an unlifted type
609 | HsStrict -- ^ Strict but not unpacked field
610 | HsUnpack (Maybe Coercion)
611 -- ^ Strict and unpacked field
612 -- co :: arg-ty ~ product-ty HsBang
613 deriving Data.Data
614
615 -- | Source Strictness
616 --
617 -- What strictness annotation the user wrote
618 data SrcStrictness = SrcLazy -- ^ Lazy, ie '~'
619 | SrcStrict -- ^ Strict, ie '!'
620 | NoSrcStrict -- ^ no strictness annotation
621 deriving (Eq, Data.Data)
622
623 -- | Source Unpackedness
624 --
625 -- What unpackedness the user requested
626 data SrcUnpackedness = SrcUnpack -- ^ {-# UNPACK #-} specified
627 | SrcNoUnpack -- ^ {-# NOUNPACK #-} specified
628 | NoSrcUnpack -- ^ no unpack pragma
629 deriving (Eq, Data.Data)
630
631
632
633 -------------------------
634 -- StrictnessMark is internal only, used to indicate strictness
635 -- of the DataCon *worker* fields
636 data StrictnessMark = MarkedStrict | NotMarkedStrict
637
638 -- | An 'EqSpec' is a tyvar/type pair representing an equality made in
639 -- rejigging a GADT constructor
640 data EqSpec = EqSpec TyVar
641 Type
642
643 -- | Make an 'EqSpec'
644 mkEqSpec :: TyVar -> Type -> EqSpec
645 mkEqSpec tv ty = EqSpec tv ty
646
647 eqSpecTyVar :: EqSpec -> TyVar
648 eqSpecTyVar (EqSpec tv _) = tv
649
650 eqSpecType :: EqSpec -> Type
651 eqSpecType (EqSpec _ ty) = ty
652
653 eqSpecPair :: EqSpec -> (TyVar, Type)
654 eqSpecPair (EqSpec tv ty) = (tv, ty)
655
656 eqSpecPreds :: [EqSpec] -> ThetaType
657 eqSpecPreds spec = [ mkPrimEqPred (mkTyVarTy tv) ty
658 | EqSpec tv ty <- spec ]
659
660 -- | Substitute in an 'EqSpec'. Precondition: if the LHS of the EqSpec
661 -- is mapped in the substitution, it is mapped to a type variable, not
662 -- a full type.
663 substEqSpec :: TCvSubst -> EqSpec -> EqSpec
664 substEqSpec subst (EqSpec tv ty)
665 = EqSpec tv' (substTy subst ty)
666 where
667 tv' = getTyVar "substEqSpec" (substTyVar subst tv)
668
669 -- | Filter out any 'TyVar's mentioned in an 'EqSpec'.
670 filterEqSpec :: [EqSpec] -> [TyVar] -> [TyVar]
671 filterEqSpec eq_spec
672 = filter not_in_eq_spec
673 where
674 not_in_eq_spec var = all (not . (== var) . eqSpecTyVar) eq_spec
675
676 instance Outputable EqSpec where
677 ppr (EqSpec tv ty) = ppr (tv, ty)
678
679 {- Note [Bangs on data constructor arguments]
680 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
681 Consider
682 data T = MkT !Int {-# UNPACK #-} !Int Bool
683
684 When compiling the module, GHC will decide how to represent
685 MkT, depending on the optimisation level, and settings of
686 flags like -funbox-small-strict-fields.
687
688 Terminology:
689 * HsSrcBang: What the user wrote
690 Constructors: HsSrcBang
691
692 * HsImplBang: What GHC decided
693 Constructors: HsLazy, HsStrict, HsUnpack
694
695 * If T was defined in this module, MkT's dcSrcBangs field
696 records the [HsSrcBang] of what the user wrote; in the example
697 [ HsSrcBang _ NoSrcUnpack SrcStrict
698 , HsSrcBang _ SrcUnpack SrcStrict
699 , HsSrcBang _ NoSrcUnpack NoSrcStrictness]
700
701 * However, if T was defined in an imported module, the importing module
702 must follow the decisions made in the original module, regardless of
703 the flag settings in the importing module.
704 Also see Note [Bangs on imported data constructors] in MkId
705
706 * The dcr_bangs field of the dcRep field records the [HsImplBang]
707 If T was defined in this module, Without -O the dcr_bangs might be
708 [HsStrict, HsStrict, HsLazy]
709 With -O it might be
710 [HsStrict, HsUnpack _, HsLazy]
711 With -funbox-small-strict-fields it might be
712 [HsUnpack, HsUnpack _, HsLazy]
713 With -XStrictData it might be
714 [HsStrict, HsUnpack _, HsStrict]
715
716 Note [Data con representation]
717 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
718 The dcRepType field contains the type of the representation of a constructor
719 This may differ from the type of the constructor *Id* (built
720 by MkId.mkDataConId) for two reasons:
721 a) the constructor Id may be overloaded, but the dictionary isn't stored
722 e.g. data Eq a => T a = MkT a a
723
724 b) the constructor may store an unboxed version of a strict field.
725
726 Here's an example illustrating both:
727 data Ord a => T a = MkT Int! a
728 Here
729 T :: Ord a => Int -> a -> T a
730 but the rep type is
731 Trep :: Int# -> a -> T a
732 Actually, the unboxed part isn't implemented yet!
733
734
735
736 ************************************************************************
737 * *
738 \subsection{Instances}
739 * *
740 ************************************************************************
741 -}
742
743 instance Eq DataCon where
744 a == b = getUnique a == getUnique b
745 a /= b = getUnique a /= getUnique b
746
747 instance Uniquable DataCon where
748 getUnique = dcUnique
749
750 instance NamedThing DataCon where
751 getName = dcName
752
753 instance Outputable DataCon where
754 ppr con = ppr (dataConName con)
755
756 instance OutputableBndr DataCon where
757 pprInfixOcc con = pprInfixName (dataConName con)
758 pprPrefixOcc con = pprPrefixName (dataConName con)
759
760 instance Data.Data DataCon where
761 -- don't traverse?
762 toConstr _ = abstractConstr "DataCon"
763 gunfold _ _ = error "gunfold"
764 dataTypeOf _ = mkNoRepType "DataCon"
765
766 instance Outputable HsSrcBang where
767 ppr (HsSrcBang _ prag mark) = ppr prag <+> ppr mark
768
769 instance Outputable HsImplBang where
770 ppr HsLazy = text "Lazy"
771 ppr (HsUnpack Nothing) = text "Unpacked"
772 ppr (HsUnpack (Just co)) = text "Unpacked" <> parens (ppr co)
773 ppr HsStrict = text "StrictNotUnpacked"
774
775 instance Outputable SrcStrictness where
776 ppr SrcLazy = char '~'
777 ppr SrcStrict = char '!'
778 ppr NoSrcStrict = empty
779
780 instance Outputable SrcUnpackedness where
781 ppr SrcUnpack = text "{-# UNPACK #-}"
782 ppr SrcNoUnpack = text "{-# NOUNPACK #-}"
783 ppr NoSrcUnpack = empty
784
785 instance Outputable StrictnessMark where
786 ppr MarkedStrict = text "!"
787 ppr NotMarkedStrict = empty
788
789 instance Binary SrcStrictness where
790 put_ bh SrcLazy = putByte bh 0
791 put_ bh SrcStrict = putByte bh 1
792 put_ bh NoSrcStrict = putByte bh 2
793
794 get bh =
795 do h <- getByte bh
796 case h of
797 0 -> return SrcLazy
798 1 -> return SrcStrict
799 _ -> return NoSrcStrict
800
801 instance Binary SrcUnpackedness where
802 put_ bh SrcNoUnpack = putByte bh 0
803 put_ bh SrcUnpack = putByte bh 1
804 put_ bh NoSrcUnpack = putByte bh 2
805
806 get bh =
807 do h <- getByte bh
808 case h of
809 0 -> return SrcNoUnpack
810 1 -> return SrcUnpack
811 _ -> return NoSrcUnpack
812
813 -- | Compare strictness annotations
814 eqHsBang :: HsImplBang -> HsImplBang -> Bool
815 eqHsBang HsLazy HsLazy = True
816 eqHsBang HsStrict HsStrict = True
817 eqHsBang (HsUnpack Nothing) (HsUnpack Nothing) = True
818 eqHsBang (HsUnpack (Just c1)) (HsUnpack (Just c2))
819 = eqType (coercionType c1) (coercionType c2)
820 eqHsBang _ _ = False
821
822 isBanged :: HsImplBang -> Bool
823 isBanged (HsUnpack {}) = True
824 isBanged (HsStrict {}) = True
825 isBanged HsLazy = False
826
827 isSrcStrict :: SrcStrictness -> Bool
828 isSrcStrict SrcStrict = True
829 isSrcStrict _ = False
830
831 isSrcUnpacked :: SrcUnpackedness -> Bool
832 isSrcUnpacked SrcUnpack = True
833 isSrcUnpacked _ = False
834
835 isMarkedStrict :: StrictnessMark -> Bool
836 isMarkedStrict NotMarkedStrict = False
837 isMarkedStrict _ = True -- All others are strict
838
839 {- *********************************************************************
840 * *
841 \subsection{Construction}
842 * *
843 ********************************************************************* -}
844
845 -- | Build a new data constructor
846 mkDataCon :: Name
847 -> Bool -- ^ Is the constructor declared infix?
848 -> TyConRepName -- ^ TyConRepName for the promoted TyCon
849 -> [HsSrcBang] -- ^ Strictness/unpack annotations, from user
850 -> [FieldLabel] -- ^ Field labels for the constructor,
851 -- if it is a record, otherwise empty
852 -> [TyVar] -- ^ Universals.
853 -> [TyVar] -- ^ Existentials.
854 -> [TyVarBinder] -- ^ User-written 'TyVarBinder's.
855 -- These must be Inferred/Specified.
856 -- See @Note [TyVarBinders in DataCons]@
857 -> [EqSpec] -- ^ GADT equalities
858 -> KnotTied ThetaType -- ^ Theta-type occuring before the arguments proper
859 -> [KnotTied Type] -- ^ Original argument types
860 -> KnotTied Type -- ^ Original result type
861 -> RuntimeRepInfo -- ^ See comments on 'TyCon.RuntimeRepInfo'
862 -> KnotTied TyCon -- ^ Representation type constructor
863 -> ConTag -- ^ Constructor tag
864 -> ThetaType -- ^ The "stupid theta", context of the data
865 -- declaration e.g. @data Eq a => T a ...@
866 -> Id -- ^ Worker Id
867 -> DataConRep -- ^ Representation
868 -> DataCon
869 -- Can get the tag from the TyCon
870
871 mkDataCon name declared_infix prom_info
872 arg_stricts -- Must match orig_arg_tys 1-1
873 fields
874 univ_tvs ex_tvs user_tvbs
875 eq_spec theta
876 orig_arg_tys orig_res_ty rep_info rep_tycon tag
877 stupid_theta work_id rep
878 -- Warning: mkDataCon is not a good place to check certain invariants.
879 -- If the programmer writes the wrong result type in the decl, thus:
880 -- data T a where { MkT :: S }
881 -- then it's possible that the univ_tvs may hit an assertion failure
882 -- if you pull on univ_tvs. This case is checked by checkValidDataCon,
883 -- so the error is detected properly... it's just that assertions here
884 -- are a little dodgy.
885
886 = con
887 where
888 is_vanilla = null ex_tvs && null eq_spec && null theta
889
890 con = MkData {dcName = name, dcUnique = nameUnique name,
891 dcVanilla = is_vanilla, dcInfix = declared_infix,
892 dcUnivTyVars = univ_tvs,
893 dcExTyVars = ex_tvs,
894 dcUserTyVarBinders = user_tvbs,
895 dcEqSpec = eq_spec,
896 dcOtherTheta = theta,
897 dcStupidTheta = stupid_theta,
898 dcOrigArgTys = orig_arg_tys, dcOrigResTy = orig_res_ty,
899 dcRepTyCon = rep_tycon,
900 dcSrcBangs = arg_stricts,
901 dcFields = fields, dcTag = tag, dcRepType = rep_ty,
902 dcWorkId = work_id,
903 dcRep = rep,
904 dcSourceArity = length orig_arg_tys,
905 dcRepArity = length rep_arg_tys,
906 dcPromoted = promoted }
907
908 -- The 'arg_stricts' passed to mkDataCon are simply those for the
909 -- source-language arguments. We add extra ones for the
910 -- dictionary arguments right here.
911
912 rep_arg_tys = dataConRepArgTys con
913
914 rep_ty =
915 case rep of
916 -- If the DataCon has no wrapper, then the worker's type *is* the
917 -- user-facing type, so we can simply use dataConUserType.
918 NoDataConRep -> dataConUserType con
919 -- If the DataCon has a wrapper, then the worker's type is never seen
920 -- by the user. The visibilities we pick do not matter here.
921 DCR{} -> mkInvForAllTys univ_tvs $ mkInvForAllTys ex_tvs $
922 mkFunTys rep_arg_tys $
923 mkTyConApp rep_tycon (mkTyVarTys univ_tvs)
924
925 -- See Note [Promoted data constructors] in TyCon
926 prom_tv_bndrs = [ mkNamedTyConBinder vis tv
927 | TvBndr tv vis <- user_tvbs ]
928
929 prom_arg_bndrs = mkCleanAnonTyConBinders prom_tv_bndrs (theta ++ orig_arg_tys)
930 prom_res_kind = orig_res_ty
931 promoted = mkPromotedDataCon con name prom_info
932 (prom_tv_bndrs ++ prom_arg_bndrs)
933 prom_res_kind roles rep_info
934
935 roles = map (const Nominal) (univ_tvs ++ ex_tvs) ++
936 map (const Representational) orig_arg_tys
937
938 mkCleanAnonTyConBinders :: [TyConBinder] -> [Type] -> [TyConBinder]
939 -- Make sure that the "anonymous" tyvars don't clash in
940 -- name or unique with the universal/existential ones.
941 -- Tiresome! And unnecessary because these tyvars are never looked at
942 mkCleanAnonTyConBinders tc_bndrs tys
943 = [ mkAnonTyConBinder (mkTyVar name ty)
944 | (name, ty) <- fresh_names `zip` tys ]
945 where
946 fresh_names = freshNames (map getName (binderVars tc_bndrs))
947
948 freshNames :: [Name] -> [Name]
949 -- Make names whose Uniques and OccNames differ from
950 -- those in the 'avoid' list
951 freshNames avoids
952 = [ mkSystemName uniq occ
953 | n <- [0..]
954 , let uniq = mkAlphaTyVarUnique n
955 occ = mkTyVarOccFS (mkFastString ('x' : show n))
956
957 , not (uniq `elementOfUniqSet` avoid_uniqs)
958 , not (occ `elemOccSet` avoid_occs) ]
959
960 where
961 avoid_uniqs :: UniqSet Unique
962 avoid_uniqs = mkUniqSet (map getUnique avoids)
963
964 avoid_occs :: OccSet
965 avoid_occs = mkOccSet (map getOccName avoids)
966
967 -- | The 'Name' of the 'DataCon', giving it a unique, rooted identification
968 dataConName :: DataCon -> Name
969 dataConName = dcName
970
971 -- | The tag used for ordering 'DataCon's
972 dataConTag :: DataCon -> ConTag
973 dataConTag = dcTag
974
975 dataConTagZ :: DataCon -> ConTagZ
976 dataConTagZ con = dataConTag con - fIRST_TAG
977
978 -- | The type constructor that we are building via this data constructor
979 dataConTyCon :: DataCon -> TyCon
980 dataConTyCon = dcRepTyCon
981
982 -- | The original type constructor used in the definition of this data
983 -- constructor. In case of a data family instance, that will be the family
984 -- type constructor.
985 dataConOrigTyCon :: DataCon -> TyCon
986 dataConOrigTyCon dc
987 | Just (tc, _) <- tyConFamInst_maybe (dcRepTyCon dc) = tc
988 | otherwise = dcRepTyCon dc
989
990 -- | The representation type of the data constructor, i.e. the sort
991 -- type that will represent values of this type at runtime
992 dataConRepType :: DataCon -> Type
993 dataConRepType = dcRepType
994
995 -- | Should the 'DataCon' be presented infix?
996 dataConIsInfix :: DataCon -> Bool
997 dataConIsInfix = dcInfix
998
999 -- | The universally-quantified type variables of the constructor
1000 dataConUnivTyVars :: DataCon -> [TyVar]
1001 dataConUnivTyVars (MkData { dcUnivTyVars = tvbs }) = tvbs
1002
1003 -- | The existentially-quantified type variables of the constructor
1004 dataConExTyVars :: DataCon -> [TyVar]
1005 dataConExTyVars (MkData { dcExTyVars = tvbs }) = tvbs
1006
1007 -- | Both the universal and existential type variables of the constructor
1008 dataConUnivAndExTyVars :: DataCon -> [TyVar]
1009 dataConUnivAndExTyVars (MkData { dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs })
1010 = univ_tvs ++ ex_tvs
1011
1012 -- See Note [DataCon user type variable binders]
1013 -- | The type variables of the constructor, in the order the user wrote them
1014 dataConUserTyVars :: DataCon -> [TyVar]
1015 dataConUserTyVars (MkData { dcUserTyVarBinders = tvbs }) = binderVars tvbs
1016
1017 -- See Note [DataCon user type variable binders]
1018 -- | 'TyVarBinder's for the type variables of the constructor, in the order the
1019 -- user wrote them
1020 dataConUserTyVarBinders :: DataCon -> [TyVarBinder]
1021 dataConUserTyVarBinders = dcUserTyVarBinders
1022
1023 -- | Equalities derived from the result type of the data constructor, as written
1024 -- by the programmer in any GADT declaration. This includes *all* GADT-like
1025 -- equalities, including those written in by hand by the programmer.
1026 dataConEqSpec :: DataCon -> [EqSpec]
1027 dataConEqSpec (MkData { dcEqSpec = eq_spec, dcOtherTheta = theta })
1028 = eq_spec ++
1029 [ spec -- heterogeneous equality
1030 | Just (tc, [_k1, _k2, ty1, ty2]) <- map splitTyConApp_maybe theta
1031 , tc `hasKey` heqTyConKey
1032 , spec <- case (getTyVar_maybe ty1, getTyVar_maybe ty2) of
1033 (Just tv1, _) -> [mkEqSpec tv1 ty2]
1034 (_, Just tv2) -> [mkEqSpec tv2 ty1]
1035 _ -> []
1036 ] ++
1037 [ spec -- homogeneous equality
1038 | Just (tc, [_k, ty1, ty2]) <- map splitTyConApp_maybe theta
1039 , tc `hasKey` eqTyConKey
1040 , spec <- case (getTyVar_maybe ty1, getTyVar_maybe ty2) of
1041 (Just tv1, _) -> [mkEqSpec tv1 ty2]
1042 (_, Just tv2) -> [mkEqSpec tv2 ty1]
1043 _ -> []
1044 ]
1045
1046
1047 -- | The *full* constraints on the constructor type.
1048 dataConTheta :: DataCon -> ThetaType
1049 dataConTheta (MkData { dcEqSpec = eq_spec, dcOtherTheta = theta })
1050 = eqSpecPreds eq_spec ++ theta
1051
1052 -- | Get the Id of the 'DataCon' worker: a function that is the "actual"
1053 -- constructor and has no top level binding in the program. The type may
1054 -- be different from the obvious one written in the source program. Panics
1055 -- if there is no such 'Id' for this 'DataCon'
1056 dataConWorkId :: DataCon -> Id
1057 dataConWorkId dc = dcWorkId dc
1058
1059 -- | Get the Id of the 'DataCon' wrapper: a function that wraps the "actual"
1060 -- constructor so it has the type visible in the source program: c.f. 'dataConWorkId'.
1061 -- Returns Nothing if there is no wrapper, which occurs for an algebraic data constructor
1062 -- and also for a newtype (whose constructor is inlined compulsorily)
1063 dataConWrapId_maybe :: DataCon -> Maybe Id
1064 dataConWrapId_maybe dc = case dcRep dc of
1065 NoDataConRep -> Nothing
1066 DCR { dcr_wrap_id = wrap_id } -> Just wrap_id
1067
1068 -- | Returns an Id which looks like the Haskell-source constructor by using
1069 -- the wrapper if it exists (see 'dataConWrapId_maybe') and failing over to
1070 -- the worker (see 'dataConWorkId')
1071 dataConWrapId :: DataCon -> Id
1072 dataConWrapId dc = case dcRep dc of
1073 NoDataConRep-> dcWorkId dc -- worker=wrapper
1074 DCR { dcr_wrap_id = wrap_id } -> wrap_id
1075
1076 -- | Find all the 'Id's implicitly brought into scope by the data constructor. Currently,
1077 -- the union of the 'dataConWorkId' and the 'dataConWrapId'
1078 dataConImplicitTyThings :: DataCon -> [TyThing]
1079 dataConImplicitTyThings (MkData { dcWorkId = work, dcRep = rep })
1080 = [AnId work] ++ wrap_ids
1081 where
1082 wrap_ids = case rep of
1083 NoDataConRep -> []
1084 DCR { dcr_wrap_id = wrap } -> [AnId wrap]
1085
1086 -- | The labels for the fields of this particular 'DataCon'
1087 dataConFieldLabels :: DataCon -> [FieldLabel]
1088 dataConFieldLabels = dcFields
1089
1090 -- | Extract the type for any given labelled field of the 'DataCon'
1091 dataConFieldType :: DataCon -> FieldLabelString -> Type
1092 dataConFieldType con label = case dataConFieldType_maybe con label of
1093 Just (_, ty) -> ty
1094 Nothing -> pprPanic "dataConFieldType" (ppr con <+> ppr label)
1095
1096 -- | Extract the label and type for any given labelled field of the
1097 -- 'DataCon', or return 'Nothing' if the field does not belong to it
1098 dataConFieldType_maybe :: DataCon -> FieldLabelString
1099 -> Maybe (FieldLabel, Type)
1100 dataConFieldType_maybe con label
1101 = find ((== label) . flLabel . fst) (dcFields con `zip` dcOrigArgTys con)
1102
1103 -- | Strictness/unpack annotations, from user; or, for imported
1104 -- DataCons, from the interface file
1105 -- The list is in one-to-one correspondence with the arity of the 'DataCon'
1106
1107 dataConSrcBangs :: DataCon -> [HsSrcBang]
1108 dataConSrcBangs = dcSrcBangs
1109
1110 -- | Source-level arity of the data constructor
1111 dataConSourceArity :: DataCon -> Arity
1112 dataConSourceArity (MkData { dcSourceArity = arity }) = arity
1113
1114 -- | Gives the number of actual fields in the /representation/ of the
1115 -- data constructor. This may be more than appear in the source code;
1116 -- the extra ones are the existentially quantified dictionaries
1117 dataConRepArity :: DataCon -> Arity
1118 dataConRepArity (MkData { dcRepArity = arity }) = arity
1119
1120 -- | Return whether there are any argument types for this 'DataCon's original source type
1121 -- See Note [DataCon arities]
1122 isNullarySrcDataCon :: DataCon -> Bool
1123 isNullarySrcDataCon dc = dataConSourceArity dc == 0
1124
1125 -- | Return whether there are any argument types for this 'DataCon's runtime representation type
1126 -- See Note [DataCon arities]
1127 isNullaryRepDataCon :: DataCon -> Bool
1128 isNullaryRepDataCon dc = dataConRepArity dc == 0
1129
1130 dataConRepStrictness :: DataCon -> [StrictnessMark]
1131 -- ^ Give the demands on the arguments of a
1132 -- Core constructor application (Con dc args)
1133 dataConRepStrictness dc = case dcRep dc of
1134 NoDataConRep -> [NotMarkedStrict | _ <- dataConRepArgTys dc]
1135 DCR { dcr_stricts = strs } -> strs
1136
1137 dataConImplBangs :: DataCon -> [HsImplBang]
1138 -- The implementation decisions about the strictness/unpack of each
1139 -- source program argument to the data constructor
1140 dataConImplBangs dc
1141 = case dcRep dc of
1142 NoDataConRep -> replicate (dcSourceArity dc) HsLazy
1143 DCR { dcr_bangs = bangs } -> bangs
1144
1145 dataConBoxer :: DataCon -> Maybe DataConBoxer
1146 dataConBoxer (MkData { dcRep = DCR { dcr_boxer = boxer } }) = Just boxer
1147 dataConBoxer _ = Nothing
1148
1149 -- | The \"signature\" of the 'DataCon' returns, in order:
1150 --
1151 -- 1) The result of 'dataConUnivAndExTyVars',
1152 --
1153 -- 2) All the 'ThetaType's relating to the 'DataCon' (coercion, dictionary, implicit
1154 -- parameter - whatever)
1155 --
1156 -- 3) The type arguments to the constructor
1157 --
1158 -- 4) The /original/ result type of the 'DataCon'
1159 dataConSig :: DataCon -> ([TyVar], ThetaType, [Type], Type)
1160 dataConSig con@(MkData {dcOrigArgTys = arg_tys, dcOrigResTy = res_ty})
1161 = (dataConUnivAndExTyVars con, dataConTheta con, arg_tys, res_ty)
1162
1163 dataConInstSig
1164 :: DataCon
1165 -> [Type] -- Instantiate the *universal* tyvars with these types
1166 -> ([TyVar], ThetaType, [Type]) -- Return instantiated existentials
1167 -- theta and arg tys
1168 -- ^ Instantiate the universal tyvars of a data con,
1169 -- returning the instantiated existentials, constraints, and args
1170 dataConInstSig (MkData { dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs
1171 , dcEqSpec = eq_spec, dcOtherTheta = theta
1172 , dcOrigArgTys = arg_tys })
1173 univ_tys
1174 = ( ex_tvs'
1175 , substTheta subst (eqSpecPreds eq_spec ++ theta)
1176 , substTys subst arg_tys)
1177 where
1178 univ_subst = zipTvSubst univ_tvs univ_tys
1179 (subst, ex_tvs') = Type.substTyVarBndrs univ_subst ex_tvs
1180
1181
1182 -- | The \"full signature\" of the 'DataCon' returns, in order:
1183 --
1184 -- 1) The result of 'dataConUnivTyVars'
1185 --
1186 -- 2) The result of 'dataConExTyVars'
1187 --
1188 -- 3) The GADT equalities
1189 --
1190 -- 4) The result of 'dataConDictTheta'
1191 --
1192 -- 5) The original argument types to the 'DataCon' (i.e. before
1193 -- any change of the representation of the type)
1194 --
1195 -- 6) The original result type of the 'DataCon'
1196 dataConFullSig :: DataCon
1197 -> ([TyVar], [TyVar], [EqSpec], ThetaType, [Type], Type)
1198 dataConFullSig (MkData {dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs,
1199 dcEqSpec = eq_spec, dcOtherTheta = theta,
1200 dcOrigArgTys = arg_tys, dcOrigResTy = res_ty})
1201 = (univ_tvs, ex_tvs, eq_spec, theta, arg_tys, res_ty)
1202
1203 dataConOrigResTy :: DataCon -> Type
1204 dataConOrigResTy dc = dcOrigResTy dc
1205
1206 -- | The \"stupid theta\" of the 'DataCon', such as @data Eq a@ in:
1207 --
1208 -- > data Eq a => T a = ...
1209 dataConStupidTheta :: DataCon -> ThetaType
1210 dataConStupidTheta dc = dcStupidTheta dc
1211
1212 dataConUserType :: DataCon -> Type
1213 -- ^ The user-declared type of the data constructor
1214 -- in the nice-to-read form:
1215 --
1216 -- > T :: forall a b. a -> b -> T [a]
1217 --
1218 -- rather than:
1219 --
1220 -- > T :: forall a c. forall b. (c~[a]) => a -> b -> T c
1221 --
1222 -- The type variables are quantified in the order that the user wrote them.
1223 -- See @Note [DataCon user type variable binders]@.
1224 --
1225 -- NB: If the constructor is part of a data instance, the result type
1226 -- mentions the family tycon, not the internal one.
1227 dataConUserType (MkData { dcUserTyVarBinders = user_tvbs,
1228 dcOtherTheta = theta, dcOrigArgTys = arg_tys,
1229 dcOrigResTy = res_ty })
1230 = mkForAllTys user_tvbs $
1231 mkFunTys theta $
1232 mkFunTys arg_tys $
1233 res_ty
1234
1235 -- | Finds the instantiated types of the arguments required to construct a 'DataCon' representation
1236 -- NB: these INCLUDE any dictionary args
1237 -- but EXCLUDE the data-declaration context, which is discarded
1238 -- It's all post-flattening etc; this is a representation type
1239 dataConInstArgTys :: DataCon -- ^ A datacon with no existentials or equality constraints
1240 -- However, it can have a dcTheta (notably it can be a
1241 -- class dictionary, with superclasses)
1242 -> [Type] -- ^ Instantiated at these types
1243 -> [Type]
1244 dataConInstArgTys dc@(MkData {dcUnivTyVars = univ_tvs,
1245 dcExTyVars = ex_tvs}) inst_tys
1246 = ASSERT2( univ_tvs `equalLength` inst_tys
1247 , text "dataConInstArgTys" <+> ppr dc $$ ppr univ_tvs $$ ppr inst_tys)
1248 ASSERT2( null ex_tvs, ppr dc )
1249 map (substTyWith univ_tvs inst_tys) (dataConRepArgTys dc)
1250
1251 -- | Returns just the instantiated /value/ argument types of a 'DataCon',
1252 -- (excluding dictionary args)
1253 dataConInstOrigArgTys
1254 :: DataCon -- Works for any DataCon
1255 -> [Type] -- Includes existential tyvar args, but NOT
1256 -- equality constraints or dicts
1257 -> [Type]
1258 -- For vanilla datacons, it's all quite straightforward
1259 -- But for the call in MatchCon, we really do want just the value args
1260 dataConInstOrigArgTys dc@(MkData {dcOrigArgTys = arg_tys,
1261 dcUnivTyVars = univ_tvs,
1262 dcExTyVars = ex_tvs}) inst_tys
1263 = ASSERT2( tyvars `equalLength` inst_tys
1264 , text "dataConInstOrigArgTys" <+> ppr dc $$ ppr tyvars $$ ppr inst_tys )
1265 map (substTyWith tyvars inst_tys) arg_tys
1266 where
1267 tyvars = univ_tvs ++ ex_tvs
1268
1269 -- | Returns the argument types of the wrapper, excluding all dictionary arguments
1270 -- and without substituting for any type variables
1271 dataConOrigArgTys :: DataCon -> [Type]
1272 dataConOrigArgTys dc = dcOrigArgTys dc
1273
1274 -- | Returns the arg types of the worker, including *all*
1275 -- evidence, after any flattening has been done and without substituting for
1276 -- any type variables
1277 dataConRepArgTys :: DataCon -> [Type]
1278 dataConRepArgTys (MkData { dcRep = rep
1279 , dcEqSpec = eq_spec
1280 , dcOtherTheta = theta
1281 , dcOrigArgTys = orig_arg_tys })
1282 = case rep of
1283 NoDataConRep -> ASSERT( null eq_spec ) theta ++ orig_arg_tys
1284 DCR { dcr_arg_tys = arg_tys } -> arg_tys
1285
1286 -- | The string @package:module.name@ identifying a constructor, which is attached
1287 -- to its info table and used by the GHCi debugger and the heap profiler
1288 dataConIdentity :: DataCon -> [Word8]
1289 -- We want this string to be UTF-8, so we get the bytes directly from the FastStrings.
1290 dataConIdentity dc = bytesFS (unitIdFS (moduleUnitId mod)) ++
1291 fromIntegral (ord ':') : bytesFS (moduleNameFS (moduleName mod)) ++
1292 fromIntegral (ord '.') : bytesFS (occNameFS (nameOccName name))
1293 where name = dataConName dc
1294 mod = ASSERT( isExternalName name ) nameModule name
1295
1296 isTupleDataCon :: DataCon -> Bool
1297 isTupleDataCon (MkData {dcRepTyCon = tc}) = isTupleTyCon tc
1298
1299 isUnboxedTupleCon :: DataCon -> Bool
1300 isUnboxedTupleCon (MkData {dcRepTyCon = tc}) = isUnboxedTupleTyCon tc
1301
1302 isUnboxedSumCon :: DataCon -> Bool
1303 isUnboxedSumCon (MkData {dcRepTyCon = tc}) = isUnboxedSumTyCon tc
1304
1305 -- | Vanilla 'DataCon's are those that are nice boring Haskell 98 constructors
1306 isVanillaDataCon :: DataCon -> Bool
1307 isVanillaDataCon dc = dcVanilla dc
1308
1309 -- | Should this DataCon be allowed in a type even without -XDataKinds?
1310 -- Currently, only Lifted & Unlifted
1311 specialPromotedDc :: DataCon -> Bool
1312 specialPromotedDc = isKindTyCon . dataConTyCon
1313
1314 classDataCon :: Class -> DataCon
1315 classDataCon clas = case tyConDataCons (classTyCon clas) of
1316 (dict_constr:no_more) -> ASSERT( null no_more ) dict_constr
1317 [] -> panic "classDataCon"
1318
1319 dataConCannotMatch :: [Type] -> DataCon -> Bool
1320 -- Returns True iff the data con *definitely cannot* match a
1321 -- scrutinee of type (T tys)
1322 -- where T is the dcRepTyCon for the data con
1323 dataConCannotMatch tys con
1324 | null inst_theta = False -- Common
1325 | all isTyVarTy tys = False -- Also common
1326 | otherwise = typesCantMatch (concatMap predEqs inst_theta)
1327 where
1328 (_, inst_theta, _) = dataConInstSig con tys
1329
1330 -- TODO: could gather equalities from superclasses too
1331 predEqs pred = case classifyPredType pred of
1332 EqPred NomEq ty1 ty2 -> [(ty1, ty2)]
1333 ClassPred eq [_, ty1, ty2]
1334 | eq `hasKey` eqTyConKey -> [(ty1, ty2)]
1335 _ -> []
1336
1337 -- | Were the type variables of the data con written in a different order
1338 -- than the regular order (universal tyvars followed by existential tyvars)?
1339 --
1340 -- This is not a cheap test, so we minimize its use in GHC as much as possible.
1341 -- Currently, its only call site in the GHC codebase is in 'mkDataConRep' in
1342 -- "MkId", and so 'dataConUserTyVarsArePermuted' is only called at most once
1343 -- during a data constructor's lifetime.
1344
1345 -- See Note [DataCon user type variable binders], as well as
1346 -- Note [Data con wrappers and GADT syntax] for an explanation of what
1347 -- mkDataConRep is doing with this function.
1348 dataConUserTyVarsArePermuted :: DataCon -> Bool
1349 dataConUserTyVarsArePermuted (MkData { dcUnivTyVars = univ_tvs,
1350 dcExTyVars = ex_tvs, dcEqSpec = eq_spec,
1351 dcUserTyVarBinders = user_tvbs }) =
1352 (filterEqSpec eq_spec univ_tvs ++ ex_tvs) /= binderVars user_tvbs
1353
1354 {-
1355 %************************************************************************
1356 %* *
1357 Promoting of data types to the kind level
1358 * *
1359 ************************************************************************
1360
1361 -}
1362
1363 promoteDataCon :: DataCon -> TyCon
1364 promoteDataCon (MkData { dcPromoted = tc }) = tc
1365
1366 {-
1367 ************************************************************************
1368 * *
1369 \subsection{Splitting products}
1370 * *
1371 ************************************************************************
1372 -}
1373
1374 -- | Extract the type constructor, type argument, data constructor and it's
1375 -- /representation/ argument types from a type if it is a product type.
1376 --
1377 -- Precisely, we return @Just@ for any type that is all of:
1378 --
1379 -- * Concrete (i.e. constructors visible)
1380 --
1381 -- * Single-constructor
1382 --
1383 -- * Not existentially quantified
1384 --
1385 -- Whether the type is a @data@ type or a @newtype@
1386 splitDataProductType_maybe
1387 :: Type -- ^ A product type, perhaps
1388 -> Maybe (TyCon, -- The type constructor
1389 [Type], -- Type args of the tycon
1390 DataCon, -- The data constructor
1391 [Type]) -- Its /representation/ arg types
1392
1393 -- Rejecting existentials is conservative. Maybe some things
1394 -- could be made to work with them, but I'm not going to sweat
1395 -- it through till someone finds it's important.
1396
1397 splitDataProductType_maybe ty
1398 | Just (tycon, ty_args) <- splitTyConApp_maybe ty
1399 , Just con <- isDataProductTyCon_maybe tycon
1400 = Just (tycon, ty_args, con, dataConInstArgTys con ty_args)
1401 | otherwise
1402 = Nothing
1403
1404 {-
1405 ************************************************************************
1406 * *
1407 Building an algebraic data type
1408 * *
1409 ************************************************************************
1410
1411 buildAlgTyCon is here because it is called from TysWiredIn, which can
1412 depend on this module, but not on BuildTyCl.
1413 -}
1414
1415 buildAlgTyCon :: Name
1416 -> [TyVar] -- ^ Kind variables and type variables
1417 -> [Role]
1418 -> Maybe CType
1419 -> ThetaType -- ^ Stupid theta
1420 -> AlgTyConRhs
1421 -> Bool -- ^ True <=> was declared in GADT syntax
1422 -> AlgTyConFlav
1423 -> TyCon
1424
1425 buildAlgTyCon tc_name ktvs roles cType stupid_theta rhs
1426 gadt_syn parent
1427 = mkAlgTyCon tc_name binders liftedTypeKind roles cType stupid_theta
1428 rhs parent gadt_syn
1429 where
1430 binders = mkTyConBindersPreferAnon ktvs liftedTypeKind
1431
1432 buildSynTyCon :: Name -> [KnotTied TyConBinder] -> Kind -- ^ /result/ kind
1433 -> [Role] -> KnotTied Type -> TyCon
1434 buildSynTyCon name binders res_kind roles rhs
1435 = mkSynonymTyCon name binders res_kind roles rhs is_tau is_fam_free
1436 where
1437 is_tau = isTauTy rhs
1438 is_fam_free = isFamFreeTy rhs