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