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