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