2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1998
5 \section[DataCon]{@DataCon@: Data Constructors}
9 DataCon, DataConIds(..),
12 dataConRepType, dataConSig, dataConFullSig,
13 dataConName, dataConIdentity, dataConTag, dataConTyCon, dataConUserType,
14 dataConUnivTyVars, dataConExTyVars, dataConAllTyVars,
15 dataConEqSpec, eqSpecPreds, dataConTheta, dataConStupidTheta,
16 dataConInstArgTys, dataConOrigArgTys, dataConOrigResTy,
17 dataConInstOrigArgTys, dataConRepArgTys,
18 dataConFieldLabels, dataConFieldType,
19 dataConStrictMarks, dataConExStricts,
20 dataConSourceArity, dataConRepArity,
22 dataConWorkId, dataConWrapId, dataConWrapId_maybe, dataConImplicitIds,
24 isNullarySrcDataCon, isNullaryRepDataCon, isTupleCon, isUnboxedTupleCon,
25 isVanillaDataCon, classDataCon,
27 splitProductType_maybe, splitProductType, deepSplitProductType,
28 deepSplitProductType_maybe
31 #include "HsVersions.h"
54 Data constructor representation
55 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56 Consider the following Haskell data type declaration
58 data T = T !Int ![Int]
60 Using the strictness annotations, GHC will represent this as
64 That is, the Int has been unboxed. Furthermore, the Haskell source construction
74 That is, the first argument is unboxed, and the second is evaluated. Finally,
75 pattern matching is translated too:
77 case e of { T a b -> ... }
81 case e of { T a' b -> let a = I# a' in ... }
83 To keep ourselves sane, we name the different versions of the data constructor
84 differently, as follows.
87 Note [Data Constructor Naming]
88 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89 Each data constructor C has two, and possibly three, Names associated with it:
91 OccName Name space Used for
92 ---------------------------------------------------------------------------
93 * The "source data con" C DataName The DataCon itself
94 * The "real data con" C VarName Its worker Id
95 * The "wrapper data con" $WC VarName Wrapper Id (optional)
97 Each of these three has a distinct Unique. The "source data con" name
98 appears in the output of the renamer, and names the Haskell-source
99 data constructor. The type checker translates it into either the wrapper Id
100 (if it exists) or worker Id (otherwise).
102 The data con has one or two Ids associated with it:
104 The "worker Id", is the actual data constructor.
105 * Every data constructor (newtype or data type) has a worker
107 * The worker is very like a primop, in that it has no binding.
109 * For a *data* type, the worker *is* the data constructor;
112 * For a *newtype*, the worker has a compulsory unfolding which
115 The worker for MkT has unfolding
116 \(x:Int). x `cast` sym CoT
117 Here CoT is the type constructor, witnessing the FC axiom
120 The "wrapper Id", $WC, goes as follows
122 * Its type is exactly what it looks like in the source program.
124 * It is an ordinary function, and it gets a top-level binding
125 like any other function.
127 * The wrapper Id isn't generated for a data type if there is
128 nothing for the wrapper to do. That is, if its defn would be
131 Why might the wrapper have anything to do? Two reasons:
133 * Unboxing strict fields (with -funbox-strict-fields)
134 data T = MkT !(Int,Int)
135 $wMkT :: (Int,Int) -> T
136 $wMkT (x,y) = MkT x y
137 Notice that the worker has two fields where the wapper has
138 just one. That is, the worker has type
139 MkT :: Int -> Int -> T
141 * Equality constraints for GADTs
142 data T a where { MkT :: a -> T [a] }
144 The worker gets a type with explicit equality
146 MkT :: forall a b. (a=[b]) => b -> T a
148 The wrapper has the programmer-specified type:
150 $wMkT a x = MkT [a] a [a] x
151 The third argument is a coerion
156 A note about the stupid context
157 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158 Data types can have a context:
160 data (Eq a, Ord b) => T a b = T1 a b | T2 a
162 and that makes the constructors have a context too
163 (notice that T2's context is "thinned"):
165 T1 :: (Eq a, Ord b) => a -> b -> T a b
166 T2 :: (Eq a) => a -> T a b
168 Furthermore, this context pops up when pattern matching
169 (though GHC hasn't implemented this, but it is in H98, and
170 I've fixed GHC so that it now does):
174 f :: Eq a => T a b -> a
176 I say the context is "stupid" because the dictionaries passed
177 are immediately discarded -- they do nothing and have no benefit.
178 It's a flaw in the language.
180 Up to now [March 2002] I have put this stupid context into the
181 type of the "wrapper" constructors functions, T1 and T2, but
182 that turned out to be jolly inconvenient for generics, and
183 record update, and other functions that build values of type T
184 (because they don't have suitable dictionaries available).
186 So now I've taken the stupid context out. I simply deal with
187 it separately in the type checker on occurrences of a
188 constructor, either in an expression or in a pattern.
190 [May 2003: actually I think this decision could evasily be
191 reversed now, and probably should be. Generics could be
192 disabled for types with a stupid context; record updates now
193 (H98) needs the context too; etc. It's an unforced change, so
194 I'm leaving it for now --- but it does seem odd that the
195 wrapper doesn't include the stupid context.]
197 [July 04] With the advent of generalised data types, it's less obvious
198 what the "stupid context" is. Consider
199 C :: forall a. Ord a => a -> a -> T (Foo a)
200 Does the C constructor in Core contain the Ord dictionary? Yes, it must:
205 C a (d:Ord a) (p:a) (q:a) -> compare d p q
207 Note that (Foo a) might not be an instance of Ord.
209 %************************************************************************
211 \subsection{Data constructors}
213 %************************************************************************
218 dcName :: Name, -- This is the name of the *source data con*
219 -- (see "Note [Data Constructor Naming]" above)
220 dcUnique :: Unique, -- Cached from Name
225 -- *** As declared by the user
227 -- MkT :: forall x y. (Ord x) => x -> y -> T (x,y)
229 -- *** As represented internally
231 -- MkT :: forall a. forall x y. (a:=:(x,y), Ord x) => x -> y -> T a
233 -- The next six fields express the type of the constructor, in pieces
236 -- dcUnivTyVars = [a]
237 -- dcExTyVars = [x,y]
238 -- dcEqSpec = [a:=:(x,y)]
240 -- dcOrigArgTys = [a,List b]
243 dcVanilla :: Bool, -- True <=> This is a vanilla Haskell 98 data constructor
244 -- Its type is of form
245 -- forall a1..an . t1 -> ... tm -> T a1..an
246 -- No existentials, no coercions, nothing.
247 -- That is: dcExTyVars = dcEqSpec = dcTheta = []
248 -- NB 1: newtypes always have a vanilla data con
249 -- NB 2: a vanilla constructor can still be declared in GADT-style
250 -- syntax, provided its type looks like the above.
251 -- The declaration format is held in the TyCon (algTcGadtSyntax)
253 dcUnivTyVars :: [TyVar], -- Universally-quantified type vars
254 -- INVARIANT: length matches arity of the dcRepTyCon
256 dcExTyVars :: [TyVar], -- Existentially-quantified type vars
257 -- In general, the dcUnivTyVars are NOT NECESSARILY THE SAME AS THE TYVARS
258 -- FOR THE PARENT TyCon. With GADTs the data con might not even have
259 -- the same number of type variables.
260 -- [This is a change (Oct05): previously, vanilla datacons guaranteed to
261 -- have the same type variables as their parent TyCon, but that seems ugly.]
263 -- INVARIANT: the UnivTyVars and ExTyVars all have distinct OccNames
264 -- Reason: less confusing, and easier to generate IfaceSyn
266 dcEqSpec :: [(TyVar,Type)], -- Equalities derived from the result type,
267 -- *as written by the programmer*
268 -- This field allows us to move conveniently between the two ways
269 -- of representing a GADT constructor's type:
270 -- MkT :: forall a b. (a :=: [b]) => b -> T a
271 -- MkT :: forall b. b -> T [b]
272 -- Each equality is of the form (a :=: ty), where 'a' is one of
273 -- the universally quantified type variables
275 dcTheta :: ThetaType, -- The context of the constructor
276 -- In GADT form, this is *exactly* what the programmer writes, even if
277 -- the context constrains only universally quantified variables
278 -- MkT :: forall a. Eq a => a -> T a
279 -- It may contain user-written equality predicates too
281 dcStupidTheta :: ThetaType, -- The context of the data type declaration
282 -- data Eq a => T a = ...
283 -- or, rather, a "thinned" version thereof
284 -- "Thinned", because the Report says
285 -- to eliminate any constraints that don't mention
286 -- tyvars free in the arg types for this constructor
288 -- INVARIANT: the free tyvars of dcStupidTheta are a subset of dcUnivTyVars
289 -- Reason: dcStupidTeta is gotten by thinning the stupid theta from the tycon
291 -- "Stupid", because the dictionaries aren't used for anything.
292 -- Indeed, [as of March 02] they are no longer in the type of
293 -- the wrapper Id, because that makes it harder to use the wrap-id
294 -- to rebuild values after record selection or in generics.
296 dcOrigArgTys :: [Type], -- Original argument types
297 -- (before unboxing and flattening of strict fields)
298 dcOrigResTy :: Type, -- Original result type
299 -- NB: for a data instance, the original user result type may
300 -- differ from the DataCon's representation TyCon. Example
301 -- data instance T [a] where MkT :: a -> T [a]
302 -- The OrigResTy is T [a], but the dcRepTyCon might be :T123
304 -- Now the strictness annotations and field labels of the constructor
305 dcStrictMarks :: [StrictnessMark],
306 -- Strictness annotations as decided by the compiler.
307 -- Does *not* include the existential dictionaries
308 -- length = dataConSourceArity dataCon
310 dcFields :: [FieldLabel],
311 -- Field labels for this constructor, in the
312 -- same order as the dcOrigArgTys;
313 -- length = 0 (if not a record) or dataConSourceArity.
315 -- Constructor representation
316 dcRepArgTys :: [Type], -- Final, representation argument types,
317 -- after unboxing and flattening,
318 -- and *including* existential dictionaries
320 dcRepStrictness :: [StrictnessMark], -- One for each *representation* argument
321 -- See also Note [Data-con worker strictness] in MkId.lhs
323 -- Result type of constructor is T t1..tn
324 dcRepTyCon :: TyCon, -- Result tycon, T
326 dcRepType :: Type, -- Type of the constructor
327 -- forall a x y. (a:=:(x,y), Ord x) => x -> y -> MkT a
328 -- (this is *not* of the constructor wrapper Id:
329 -- see Note [Data con representation] below)
330 -- Notice that the existential type parameters come *second*.
331 -- Reason: in a case expression we may find:
332 -- case (e :: T t) of { MkT b (d:Ord b) (x:t) (xs:[b]) -> ... }
333 -- It's convenient to apply the rep-type of MkT to 't', to get
334 -- forall b. Ord b => ...
335 -- and use that to check the pattern. Mind you, this is really only
339 -- Finally, the curried worker function that corresponds to the constructor
340 -- It doesn't have an unfolding; the code generator saturates these Ids
341 -- and allocates a real constructor when it finds one.
343 -- An entirely separate wrapper function is built in TcTyDecls
346 dcInfix :: Bool -- True <=> declared infix
347 -- Used for Template Haskell and 'deriving' only
348 -- The actual fixity is stored elsewhere
352 = DCIds (Maybe Id) Id -- Algebraic data types always have a worker, and
353 -- may or may not have a wrapper, depending on whether
354 -- the wrapper does anything. Newtypes just have a worker
356 -- _Neither_ the worker _nor_ the wrapper take the dcStupidTheta dicts as arguments
358 -- The wrapper takes dcOrigArgTys as its arguments
359 -- The worker takes dcRepArgTys as its arguments
360 -- If the worker is absent, dcRepArgTys is the same as dcOrigArgTys
362 -- The 'Nothing' case of DCIds is important
363 -- Not only is this efficient,
364 -- but it also ensures that the wrapper is replaced
365 -- by the worker (becuase it *is* the worker)
366 -- even when there are no args. E.g. in
368 -- the (:) *is* the worker.
369 -- This is really important in rule matching,
370 -- (We could match on the wrappers,
371 -- but that makes it less likely that rules will match
372 -- when we bring bits of unfoldings together.)
377 fIRST_TAG = 1 -- Tags allocated from here for real constructors
380 Note [Data con representation]
381 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
382 The dcRepType field contains the type of the representation of a contructor
383 This may differ from the type of the contructor *Id* (built
384 by MkId.mkDataConId) for two reasons:
385 a) the constructor Id may be overloaded, but the dictionary isn't stored
386 e.g. data Eq a => T a = MkT a a
388 b) the constructor may store an unboxed version of a strict field.
390 Here's an example illustrating both:
391 data Ord a => T a = MkT Int! a
393 T :: Ord a => Int -> a -> T a
395 Trep :: Int# -> a -> T a
396 Actually, the unboxed part isn't implemented yet!
399 %************************************************************************
401 \subsection{Instances}
403 %************************************************************************
406 instance Eq DataCon where
407 a == b = getUnique a == getUnique b
408 a /= b = getUnique a /= getUnique b
410 instance Ord DataCon where
411 a <= b = getUnique a <= getUnique b
412 a < b = getUnique a < getUnique b
413 a >= b = getUnique a >= getUnique b
414 a > b = getUnique a > getUnique b
415 compare a b = getUnique a `compare` getUnique b
417 instance Uniquable DataCon where
420 instance NamedThing DataCon where
423 instance Outputable DataCon where
424 ppr con = ppr (dataConName con)
426 instance Show DataCon where
427 showsPrec p con = showsPrecSDoc p (ppr con)
431 %************************************************************************
433 \subsection{Construction}
435 %************************************************************************
439 -> Bool -- Declared infix
440 -> [StrictnessMark] -> [FieldLabel]
441 -> [TyVar] -> [TyVar]
442 -> [(TyVar,Type)] -> ThetaType
444 -> ThetaType -> DataConIds
446 -- Can get the tag from the TyCon
448 mkDataCon name declared_infix
449 arg_stricts -- Must match orig_arg_tys 1-1
455 -- Warning: mkDataCon is not a good place to check invariants.
456 -- If the programmer writes the wrong result type in the decl, thus:
457 -- data T a where { MkT :: S }
458 -- then it's possible that the univ_tvs may hit an assertion failure
459 -- if you pull on univ_tvs. This case is checked by checkValidDataCon,
460 -- so the error is detected properly... it's just that asaertions here
461 -- are a little dodgy.
463 = ASSERT( not (any isEqPred theta) )
464 -- We don't currently allow any equality predicates on
465 -- a data constructor (apart from the GADT ones in eq_spec)
468 is_vanilla = null ex_tvs && null eq_spec && null theta
469 con = MkData {dcName = name, dcUnique = nameUnique name,
470 dcVanilla = is_vanilla, dcInfix = declared_infix,
471 dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs,
473 dcStupidTheta = stupid_theta, dcTheta = theta,
474 dcOrigArgTys = orig_arg_tys, dcOrigResTy = orig_res_ty,
476 dcRepArgTys = rep_arg_tys,
477 dcStrictMarks = arg_stricts,
478 dcRepStrictness = rep_arg_stricts,
479 dcFields = fields, dcTag = tag, dcRepType = ty,
482 -- Strictness marks for source-args
483 -- *after unboxing choices*,
484 -- but *including existential dictionaries*
486 -- The 'arg_stricts' passed to mkDataCon are simply those for the
487 -- source-language arguments. We add extra ones for the
488 -- dictionary arguments right here.
489 dict_tys = mkPredTys theta
490 real_arg_tys = dict_tys ++ orig_arg_tys
491 real_stricts = map mk_dict_strict_mark theta ++ arg_stricts
494 -- data instance T (b,c) where
495 -- TI :: forall e. e -> T (e,e)
497 -- The representation tycon looks like this:
498 -- data :R7T b c where
499 -- TI :: forall b1 c1. (b1 ~ c1) => b1 -> :R7T b1 c1
500 orig_res_ty = mkFamilyTyConApp tycon (substTyVars (mkTopTvSubst eq_spec) univ_tvs)
502 -- Representation arguments and demands
503 -- To do: eliminate duplication with MkId
504 (rep_arg_stricts, rep_arg_tys) = computeRep real_stricts real_arg_tys
506 tag = assoc "mkDataCon" (tyConDataCons tycon `zip` [fIRST_TAG..]) con
507 ty = mkForAllTys univ_tvs $ mkForAllTys ex_tvs $
508 mkFunTys (mkPredTys (eqSpecPreds eq_spec)) $
509 -- NB: the dict args are already in rep_arg_tys
510 -- because they might be flattened..
511 -- but the equality predicates are not
512 mkFunTys rep_arg_tys $
513 mkTyConApp tycon (mkTyVarTys univ_tvs)
515 eqSpecPreds :: [(TyVar,Type)] -> ThetaType
516 eqSpecPreds spec = [ mkEqPred (mkTyVarTy tv, ty) | (tv,ty) <- spec ]
518 mk_dict_strict_mark pred | isStrictPred pred = MarkedStrict
519 | otherwise = NotMarkedStrict
523 dataConName :: DataCon -> Name
526 dataConTag :: DataCon -> ConTag
529 dataConTyCon :: DataCon -> TyCon
530 dataConTyCon = dcRepTyCon
532 dataConRepType :: DataCon -> Type
533 dataConRepType = dcRepType
535 dataConIsInfix :: DataCon -> Bool
536 dataConIsInfix = dcInfix
538 dataConUnivTyVars :: DataCon -> [TyVar]
539 dataConUnivTyVars = dcUnivTyVars
541 dataConExTyVars :: DataCon -> [TyVar]
542 dataConExTyVars = dcExTyVars
544 dataConAllTyVars :: DataCon -> [TyVar]
545 dataConAllTyVars (MkData { dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs })
548 dataConEqSpec :: DataCon -> [(TyVar,Type)]
549 dataConEqSpec = dcEqSpec
551 dataConTheta :: DataCon -> ThetaType
552 dataConTheta = dcTheta
554 dataConWorkId :: DataCon -> Id
555 dataConWorkId dc = case dcIds dc of
556 DCIds _ wrk_id -> wrk_id
558 dataConWrapId_maybe :: DataCon -> Maybe Id
559 -- Returns Nothing if there is no wrapper for an algebraic data con
560 -- and also for a newtype (whose constructor is inlined compulsorily)
561 dataConWrapId_maybe dc = case dcIds dc of
562 DCIds mb_wrap _ -> mb_wrap
564 dataConWrapId :: DataCon -> Id
565 -- Returns an Id which looks like the Haskell-source constructor
566 dataConWrapId dc = case dcIds dc of
567 DCIds (Just wrap) _ -> wrap
568 DCIds Nothing wrk -> wrk -- worker=wrapper
570 dataConImplicitIds :: DataCon -> [Id]
571 dataConImplicitIds dc = case dcIds dc of
572 DCIds (Just wrap) work -> [wrap,work]
573 DCIds Nothing work -> [work]
575 dataConFieldLabels :: DataCon -> [FieldLabel]
576 dataConFieldLabels = dcFields
578 dataConFieldType :: DataCon -> FieldLabel -> Type
579 dataConFieldType con label = expectJust "unexpected label" $
580 lookup label (dcFields con `zip` dcOrigArgTys con)
582 dataConStrictMarks :: DataCon -> [StrictnessMark]
583 dataConStrictMarks = dcStrictMarks
585 dataConExStricts :: DataCon -> [StrictnessMark]
586 -- Strictness of *existential* arguments only
587 -- Usually empty, so we don't bother to cache this
588 dataConExStricts dc = map mk_dict_strict_mark (dcTheta dc)
590 dataConSourceArity :: DataCon -> Arity
591 -- Source-level arity of the data constructor
592 dataConSourceArity dc = length (dcOrigArgTys dc)
594 -- dataConRepArity gives the number of actual fields in the
595 -- {\em representation} of the data constructor. This may be more than appear
596 -- in the source code; the extra ones are the existentially quantified
598 dataConRepArity (MkData {dcRepArgTys = arg_tys}) = length arg_tys
600 isNullarySrcDataCon, isNullaryRepDataCon :: DataCon -> Bool
601 isNullarySrcDataCon dc = null (dcOrigArgTys dc)
602 isNullaryRepDataCon dc = null (dcRepArgTys dc)
604 dataConRepStrictness :: DataCon -> [StrictnessMark]
605 -- Give the demands on the arguments of a
606 -- Core constructor application (Con dc args)
607 dataConRepStrictness dc = dcRepStrictness dc
609 dataConSig :: DataCon -> ([TyVar], ThetaType, [Type], Type)
610 dataConSig (MkData {dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs, dcEqSpec = eq_spec,
611 dcTheta = theta, dcOrigArgTys = arg_tys, dcOrigResTy = res_ty})
612 = (univ_tvs ++ ex_tvs, eqSpecPreds eq_spec ++ theta, arg_tys, res_ty)
614 dataConFullSig :: DataCon
615 -> ([TyVar], [TyVar], [(TyVar,Type)], ThetaType, [Type], Type)
616 dataConFullSig (MkData {dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs, dcEqSpec = eq_spec,
617 dcTheta = theta, dcOrigArgTys = arg_tys, dcOrigResTy = res_ty})
618 = (univ_tvs, ex_tvs, eq_spec, theta, arg_tys, res_ty)
620 dataConOrigResTy :: DataCon -> Type
621 dataConOrigResTy dc = dcOrigResTy dc
623 dataConStupidTheta :: DataCon -> ThetaType
624 dataConStupidTheta dc = dcStupidTheta dc
626 dataConUserType :: DataCon -> Type
627 -- The user-declared type of the data constructor
628 -- in the nice-to-read form
629 -- T :: forall a b. a -> b -> T [a]
631 -- T :: forall a c. forall b. (c=[a]) => a -> b -> T c
632 -- NB: If the constructor is part of a data instance, the result type
633 -- mentions the family tycon, not the internal one.
634 dataConUserType (MkData { dcUnivTyVars = univ_tvs,
635 dcExTyVars = ex_tvs, dcEqSpec = eq_spec,
636 dcTheta = theta, dcOrigArgTys = arg_tys,
637 dcOrigResTy = res_ty })
638 = mkForAllTys ((univ_tvs `minusList` map fst eq_spec) ++ ex_tvs) $
639 mkFunTys (mkPredTys theta) $
643 dataConInstArgTys :: DataCon -- A datacon with no existentials or equality constraints
644 -- However, it can have a dcTheta (notably it can be a
645 -- class dictionary, with superclasses)
646 -> [Type] -- Instantiated at these types
647 -> [Type] -- Needs arguments of these types
648 -- NB: these INCLUDE any dict args
649 -- but EXCLUDE the data-decl context which is discarded
650 -- It's all post-flattening etc; this is a representation type
651 dataConInstArgTys dc@(MkData {dcRepArgTys = rep_arg_tys,
652 dcUnivTyVars = univ_tvs, dcEqSpec = eq_spec,
653 dcExTyVars = ex_tvs}) inst_tys
654 = ASSERT2 ( length univ_tvs == length inst_tys
655 , ptext SLIT("dataConInstArgTys") <+> ppr dc $$ ppr univ_tvs $$ ppr inst_tys)
656 ASSERT2 ( null ex_tvs && null eq_spec, ppr dc )
657 map (substTyWith univ_tvs inst_tys) rep_arg_tys
659 dataConInstOrigArgTys
660 :: DataCon -- Works for any DataCon
661 -> [Type] -- Includes existential tyvar args, but NOT
662 -- equality constraints or dicts
663 -> [Type] -- Returns just the instsantiated *value* arguments
664 -- For vanilla datacons, it's all quite straightforward
665 -- But for the call in MatchCon, we really do want just the value args
666 dataConInstOrigArgTys dc@(MkData {dcOrigArgTys = arg_tys,
667 dcUnivTyVars = univ_tvs,
668 dcExTyVars = ex_tvs}) inst_tys
669 = ASSERT2( length tyvars == length inst_tys
670 , ptext SLIT("dataConInstOrigArgTys") <+> ppr dc $$ ppr tyvars $$ ppr inst_tys )
671 map (substTyWith tyvars inst_tys) arg_tys
673 tyvars = univ_tvs ++ ex_tvs
676 These two functions get the real argument types of the constructor,
677 without substituting for any type variables.
679 dataConOrigArgTys returns the arg types of the wrapper, excluding all dictionary args.
681 dataConRepArgTys retuns the arg types of the worker, including all dictionaries, and
682 after any flattening has been done.
685 dataConOrigArgTys :: DataCon -> [Type]
686 dataConOrigArgTys dc = dcOrigArgTys dc
688 dataConRepArgTys :: DataCon -> [Type]
689 dataConRepArgTys dc = dcRepArgTys dc
692 The string <package>:<module>.<name> identifying a constructor, which is attached
693 to its info table and used by the GHCi debugger and the heap profiler. We want
694 this string to be UTF-8, so we get the bytes directly from the FastStrings.
697 dataConIdentity :: DataCon -> [Word8]
698 dataConIdentity dc = bytesFS (packageIdFS (modulePackageId mod)) ++
699 fromIntegral (ord ':') : bytesFS (moduleNameFS (moduleName mod)) ++
700 fromIntegral (ord '.') : bytesFS (occNameFS (nameOccName name))
701 where name = dataConName dc
702 mod = nameModule name
707 isTupleCon :: DataCon -> Bool
708 isTupleCon (MkData {dcRepTyCon = tc}) = isTupleTyCon tc
710 isUnboxedTupleCon :: DataCon -> Bool
711 isUnboxedTupleCon (MkData {dcRepTyCon = tc}) = isUnboxedTupleTyCon tc
713 isVanillaDataCon :: DataCon -> Bool
714 isVanillaDataCon dc = dcVanilla dc
719 classDataCon :: Class -> DataCon
720 classDataCon clas = case tyConDataCons (classTyCon clas) of
721 (dict_constr:no_more) -> ASSERT( null no_more ) dict_constr
722 [] -> panic "classDataCon"
725 %************************************************************************
727 \subsection{Splitting products}
729 %************************************************************************
732 splitProductType_maybe
733 :: Type -- A product type, perhaps
734 -> Maybe (TyCon, -- The type constructor
735 [Type], -- Type args of the tycon
736 DataCon, -- The data constructor
737 [Type]) -- Its *representation* arg types
739 -- Returns (Just ...) for any
740 -- concrete (i.e. constructors visible)
741 -- single-constructor
742 -- not existentially quantified
743 -- type whether a data type or a new type
745 -- Rejecing existentials is conservative. Maybe some things
746 -- could be made to work with them, but I'm not going to sweat
747 -- it through till someone finds it's important.
749 splitProductType_maybe ty
750 = case splitTyConApp_maybe ty of
752 | isProductTyCon tycon -- Includes check for non-existential,
753 -- and for constructors visible
754 -> Just (tycon, ty_args, data_con, dataConInstArgTys data_con ty_args)
756 data_con = ASSERT( not (null (tyConDataCons tycon)) )
757 head (tyConDataCons tycon)
760 splitProductType str ty
761 = case splitProductType_maybe ty of
763 Nothing -> pprPanic (str ++ ": not a product") (pprType ty)
766 deepSplitProductType_maybe ty
767 = do { (res@(tycon, tycon_args, _, _)) <- splitProductType_maybe ty
769 | isClosedNewTyCon tycon && not (isRecursiveTyCon tycon)
770 = deepSplitProductType_maybe (newTyConInstRhs tycon tycon_args)
771 | isNewTyCon tycon = Nothing -- cannot unbox through recursive
772 -- newtypes nor through families
773 | otherwise = Just res}
777 deepSplitProductType str ty
778 = case deepSplitProductType_maybe ty of
780 Nothing -> pprPanic (str ++ ": not a product") (pprType ty)
782 computeRep :: [StrictnessMark] -- Original arg strictness
783 -> [Type] -- and types
784 -> ([StrictnessMark], -- Representation arg strictness
787 computeRep stricts tys
788 = unzip $ concat $ zipWithEqual "computeRep" unbox stricts tys
790 unbox NotMarkedStrict ty = [(NotMarkedStrict, ty)]
791 unbox MarkedStrict ty = [(MarkedStrict, ty)]
792 unbox MarkedUnboxed ty = zipEqual "computeRep" (dataConRepStrictness arg_dc) arg_tys
794 (_tycon, _tycon_args, arg_dc, arg_tys)
795 = deepSplitProductType "unbox_strict_arg_ty" ty