2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[DataCon]{@DataCon@: Data Constructors}
8 DataCon, DataConIds(..),
11 dataConRepType, dataConSig, dataConFullSig,
12 dataConName, dataConTag, dataConTyCon, dataConUserType,
13 dataConUnivTyVars, dataConExTyVars, dataConAllTyVars, dataConResTys,
15 dataConEqSpec, eqSpecPreds, dataConTheta, dataConStupidTheta,
16 dataConInstArgTys, dataConOrigArgTys,
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"
33 import Type ( Type, ThetaType,
34 substTyWith, substTyVar, mkTopTvSubst,
35 mkForAllTys, mkFunTys, mkTyConApp, mkTyVarTy, mkTyVarTys,
36 splitTyConApp_maybe, newTyConInstRhs,
37 mkPredTys, isStrictPred, pprType, mkPredTy
39 import Coercion ( isEqPred, mkEqPred )
40 import TyCon ( TyCon, FieldLabel, tyConDataCons,
41 isProductTyCon, isTupleTyCon, isUnboxedTupleTyCon,
42 isNewTyCon, isRecursiveTyCon, tyConFamily_maybe )
43 import Class ( Class, classTyCon )
44 import Name ( Name, NamedThing(..), nameUnique, mkSysTvName, mkSystemName )
45 import Var ( TyVar, CoVar, Id, mkTyVar, tyVarKind, setVarUnique,
47 import BasicTypes ( Arity, StrictnessMark(..) )
49 import Unique ( Unique, Uniquable(..) )
50 import ListSetOps ( assoc, minusList )
51 import Util ( zipEqual, zipWithEqual )
52 import List ( partition )
53 import Maybes ( expectJust )
58 Data constructor representation
59 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60 Consider the following Haskell data type declaration
62 data T = T !Int ![Int]
64 Using the strictness annotations, GHC will represent this as
68 That is, the Int has been unboxed. Furthermore, the Haskell source construction
78 That is, the first argument is unboxed, and the second is evaluated. Finally,
79 pattern matching is translated too:
81 case e of { T a b -> ... }
85 case e of { T a' b -> let a = I# a' in ... }
87 To keep ourselves sane, we name the different versions of the data constructor
88 differently, as follows.
91 Note [Data Constructor Naming]
92 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93 Each data constructor C has two, and possibly three, Names associated with it:
95 OccName Name space Used for
96 ---------------------------------------------------------------------------
97 * The "source data con" C DataName The DataCon itself
98 * The "real data con" C VarName Its worker Id
99 * The "wrapper data con" $WC VarName Wrapper Id (optional)
101 Each of these three has a distinct Unique. The "source data con" name
102 appears in the output of the renamer, and names the Haskell-source
103 data constructor. The type checker translates it into either the wrapper Id
104 (if it exists) or worker Id (otherwise).
106 The data con has one or two Ids associated with it:
108 The "worker Id", is the actual data constructor.
109 * Every data constructor (newtype or data type) has a worker
111 * The worker is very like a primop, in that it has no binding.
113 * For a *data* type, the worker *is* the data constructor;
116 * For a *newtype*, the worker has a compulsory unfolding which
119 The worker for MkT has unfolding
120 \(x:Int). x `cast` sym CoT
121 Here CoT is the type constructor, witnessing the FC axiom
124 The "wrapper Id", $WC, goes as follows
126 * Its type is exactly what it looks like in the source program.
128 * It is an ordinary function, and it gets a top-level binding
129 like any other function.
131 * The wrapper Id isn't generated for a data type if there is
132 nothing for the wrapper to do. That is, if its defn would be
135 Why might the wrapper have anything to do? Two reasons:
137 * Unboxing strict fields (with -funbox-strict-fields)
138 data T = MkT !(Int,Int)
139 $wMkT :: (Int,Int) -> T
140 $wMkT (x,y) = MkT x y
141 Notice that the worker has two fields where the wapper has
142 just one. That is, the worker has type
143 MkT :: Int -> Int -> T
145 * Equality constraints for GADTs
146 data T a where { MkT :: a -> T [a] }
148 The worker gets a type with explicit equality
150 MkT :: forall a b. (a=[b]) => b -> T a
152 The wrapper has the programmer-specified type:
154 $wMkT a x = MkT [a] a [a] x
155 The third argument is a coerion
160 A note about the stupid context
161 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
162 Data types can have a context:
164 data (Eq a, Ord b) => T a b = T1 a b | T2 a
166 and that makes the constructors have a context too
167 (notice that T2's context is "thinned"):
169 T1 :: (Eq a, Ord b) => a -> b -> T a b
170 T2 :: (Eq a) => a -> T a b
172 Furthermore, this context pops up when pattern matching
173 (though GHC hasn't implemented this, but it is in H98, and
174 I've fixed GHC so that it now does):
178 f :: Eq a => T a b -> a
180 I say the context is "stupid" because the dictionaries passed
181 are immediately discarded -- they do nothing and have no benefit.
182 It's a flaw in the language.
184 Up to now [March 2002] I have put this stupid context into the
185 type of the "wrapper" constructors functions, T1 and T2, but
186 that turned out to be jolly inconvenient for generics, and
187 record update, and other functions that build values of type T
188 (because they don't have suitable dictionaries available).
190 So now I've taken the stupid context out. I simply deal with
191 it separately in the type checker on occurrences of a
192 constructor, either in an expression or in a pattern.
194 [May 2003: actually I think this decision could evasily be
195 reversed now, and probably should be. Generics could be
196 disabled for types with a stupid context; record updates now
197 (H98) needs the context too; etc. It's an unforced change, so
198 I'm leaving it for now --- but it does seem odd that the
199 wrapper doesn't include the stupid context.]
201 [July 04] With the advent of generalised data types, it's less obvious
202 what the "stupid context" is. Consider
203 C :: forall a. Ord a => a -> a -> T (Foo a)
204 Does the C constructor in Core contain the Ord dictionary? Yes, it must:
209 C a (d:Ord a) (p:a) (q:a) -> compare d p q
211 Note that (Foo a) might not be an instance of Ord.
213 %************************************************************************
215 \subsection{Data constructors}
217 %************************************************************************
222 dcName :: Name, -- This is the name of the *source data con*
223 -- (see "Note [Data Constructor Naming]" above)
224 dcUnique :: Unique, -- Cached from Name
229 -- *** As declared by the user
231 -- MkT :: forall x y. (Ord x) => x -> y -> T (x,y)
233 -- *** As represented internally
235 -- MkT :: forall a. forall x y. (a:=:(x,y), Ord x) => x -> y -> T a
237 -- The next six fields express the type of the constructor, in pieces
240 -- dcUnivTyVars = [a]
241 -- dcExTyVars = [x,y]
242 -- dcEqSpec = [a:=:(x,y)]
244 -- dcOrigArgTys = [a,List b]
247 dcVanilla :: Bool, -- True <=> This is a vanilla Haskell 98 data constructor
248 -- Its type is of form
249 -- forall a1..an . t1 -> ... tm -> T a1..an
250 -- No existentials, no coercions, nothing.
251 -- That is: dcExTyVars = dcEqSpec = dcTheta = []
252 -- NB 1: newtypes always have a vanilla data con
253 -- NB 2: a vanilla constructor can still be declared in GADT-style
254 -- syntax, provided its type looks like the above.
255 -- The declaration format is held in the TyCon (algTcGadtSyntax)
257 dcUnivTyVars :: [TyVar], -- Universally-quantified type vars
258 dcExTyVars :: [TyVar], -- Existentially-quantified type vars
259 -- In general, the dcUnivTyVars are NOT NECESSARILY THE SAME AS THE TYVARS
260 -- FOR THE PARENT TyCon. With GADTs the data con might not even have
261 -- the same number of type variables.
262 -- [This is a change (Oct05): previously, vanilla datacons guaranteed to
263 -- have the same type variables as their parent TyCon, but that seems ugly.]
265 dcEqSpec :: [(TyVar,Type)], -- Equalities derived from the result type,
266 -- *as written by the programmer*
267 -- This field allows us to move conveniently between the two ways
268 -- of representing a GADT constructor's type:
269 -- MkT :: forall a b. (a :=: [b]) => b -> T a
270 -- MkT :: forall b. b -> T [b]
271 -- Each equality is of the form (a :=: ty), where 'a' is one of
272 -- the universally quantified type variables
274 dcTheta :: ThetaType, -- The context of the constructor
275 -- In GADT form, this is *exactly* what the programmer writes, even if
276 -- the context constrains only universally quantified variables
277 -- MkT :: forall a. Eq a => a -> T a
278 -- It may contain user-written equality predicates too
280 dcStupidTheta :: ThetaType, -- The context of the data type declaration
281 -- data Eq a => T a = ...
282 -- or, rather, a "thinned" version thereof
283 -- "Thinned", because the Report says
284 -- to eliminate any constraints that don't mention
285 -- tyvars free in the arg types for this constructor
287 -- INVARIANT: the free tyvars of dcStupidTheta are a subset of dcUnivTyVars
288 -- Reason: dcStupidTeta is gotten by thinning the stupid theta from the tycon
290 -- "Stupid", because the dictionaries aren't used for anything.
291 -- Indeed, [as of March 02] they are no longer in the type of
292 -- the wrapper Id, because that makes it harder to use the wrap-id
293 -- to rebuild values after record selection or in generics.
295 dcOrigArgTys :: [Type], -- Original argument types
296 -- (before unboxing and flattening of strict fields)
298 -- Result type of constructor is T t1..tn
299 dcTyCon :: TyCon, -- Result tycon, T
301 -- Now the strictness annotations and field labels of the constructor
302 dcStrictMarks :: [StrictnessMark],
303 -- Strictness annotations as decided by the compiler.
304 -- Does *not* include the existential dictionaries
305 -- length = dataConSourceArity dataCon
307 dcFields :: [FieldLabel],
308 -- Field labels for this constructor, in the
309 -- same order as the argument types;
310 -- length = 0 (if not a record) or dataConSourceArity.
312 -- Constructor representation
313 dcRepArgTys :: [Type], -- Final, representation argument types,
314 -- after unboxing and flattening,
315 -- and *including* existential dictionaries
317 dcRepStrictness :: [StrictnessMark], -- One for each *representation* argument
319 dcRepType :: Type, -- Type of the constructor
320 -- forall a x y. (a:=:(x,y), Ord x) => x -> y -> MkT a
321 -- (this is *not* of the constructor wrapper Id:
322 -- see Note [Data con representation] below)
323 -- Notice that the existential type parameters come *second*.
324 -- Reason: in a case expression we may find:
325 -- case (e :: T t) of { MkT b (d:Ord b) (x:t) (xs:[b]) -> ... }
326 -- It's convenient to apply the rep-type of MkT to 't', to get
327 -- forall b. Ord b => ...
328 -- and use that to check the pattern. Mind you, this is really only
332 -- Finally, the curried worker function that corresponds to the constructor
333 -- It doesn't have an unfolding; the code generator saturates these Ids
334 -- and allocates a real constructor when it finds one.
336 -- An entirely separate wrapper function is built in TcTyDecls
339 dcInfix :: Bool, -- True <=> declared infix
340 -- Used for Template Haskell and 'deriving' only
341 -- The actual fixity is stored elsewhere
343 dcInstTys :: Maybe [Type] -- If this data constructor is part of a
344 -- data instance, then these are the type
345 -- patterns of the instance.
349 = DCIds (Maybe Id) Id -- Algebraic data types always have a worker, and
350 -- may or may not have a wrapper, depending on whether
351 -- the wrapper does anything. Newtypes just have a worker
353 -- _Neither_ the worker _nor_ the wrapper take the dcStupidTheta dicts as arguments
355 -- The wrapper takes dcOrigArgTys as its arguments
356 -- The worker takes dcRepArgTys as its arguments
357 -- If the worker is absent, dcRepArgTys is the same as dcOrigArgTys
359 -- The 'Nothing' case of DCIds is important
360 -- Not only is this efficient,
361 -- but it also ensures that the wrapper is replaced
362 -- by the worker (becuase it *is* the wroker)
363 -- even when there are no args. E.g. in
365 -- the (:) *is* the worker.
366 -- This is really important in rule matching,
367 -- (We could match on the wrappers,
368 -- but that makes it less likely that rules will match
369 -- when we bring bits of unfoldings together.)
374 fIRST_TAG = 1 -- Tags allocated from here for real constructors
377 Note [Data con representation]
378 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
379 The dcRepType field contains the type of the representation of a contructor
380 This may differ from the type of the contructor *Id* (built
381 by MkId.mkDataConId) for two reasons:
382 a) the constructor Id may be overloaded, but the dictionary isn't stored
383 e.g. data Eq a => T a = MkT a a
385 b) the constructor may store an unboxed version of a strict field.
387 Here's an example illustrating both:
388 data Ord a => T a = MkT Int! a
390 T :: Ord a => Int -> a -> T a
392 Trep :: Int# -> a -> T a
393 Actually, the unboxed part isn't implemented yet!
396 %************************************************************************
398 \subsection{Instances}
400 %************************************************************************
403 instance Eq DataCon where
404 a == b = getUnique a == getUnique b
405 a /= b = getUnique a /= getUnique b
407 instance Ord DataCon where
408 a <= b = getUnique a <= getUnique b
409 a < b = getUnique a < getUnique b
410 a >= b = getUnique a >= getUnique b
411 a > b = getUnique a > getUnique b
412 compare a b = getUnique a `compare` getUnique b
414 instance Uniquable DataCon where
417 instance NamedThing DataCon where
420 instance Outputable DataCon where
421 ppr con = ppr (dataConName con)
423 instance Show DataCon where
424 showsPrec p con = showsPrecSDoc p (ppr con)
428 %************************************************************************
430 \subsection{Construction}
432 %************************************************************************
436 -> Bool -- Declared infix
437 -> [StrictnessMark] -> [FieldLabel]
438 -> [TyVar] -> [TyVar]
439 -> [(TyVar,Type)] -> ThetaType
442 -> ThetaType -> DataConIds
444 -- Can get the tag from the TyCon
446 mkDataCon name declared_infix
447 arg_stricts -- Must match orig_arg_tys 1-1
454 = ASSERT( not (any isEqPred theta) )
455 -- We don't currently allow any equality predicates on
456 -- a data constructor (apart from the GADT ones in eq_spec)
459 is_vanilla = null ex_tvs && null eq_spec && null theta
460 con = ASSERT( is_vanilla || not (isNewTyCon tycon) )
461 -- Invariant: newtypes have a vanilla data-con
462 MkData {dcName = name, dcUnique = nameUnique name,
463 dcVanilla = is_vanilla, dcInfix = declared_infix,
464 dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs,
466 dcStupidTheta = stupid_theta, dcTheta = theta,
467 dcOrigArgTys = orig_arg_tys, dcTyCon = tycon,
468 dcRepArgTys = rep_arg_tys,
469 dcStrictMarks = arg_stricts,
470 dcRepStrictness = rep_arg_stricts,
471 dcFields = fields, dcTag = tag, dcRepType = ty,
473 dcInstTys = mb_typats }
475 -- Strictness marks for source-args
476 -- *after unboxing choices*,
477 -- but *including existential dictionaries*
479 -- The 'arg_stricts' passed to mkDataCon are simply those for the
480 -- source-language arguments. We add extra ones for the
481 -- dictionary arguments right here.
482 dict_tys = mkPredTys theta
483 real_arg_tys = dict_tys ++ orig_arg_tys
484 real_stricts = map mk_dict_strict_mark theta ++ arg_stricts
486 -- Representation arguments and demands
487 -- To do: eliminate duplication with MkId
488 (rep_arg_stricts, rep_arg_tys) = computeRep real_stricts real_arg_tys
490 tag = assoc "mkDataCon" (tyConDataCons tycon `zip` [fIRST_TAG..]) con
491 ty = mkForAllTys univ_tvs $ mkForAllTys ex_tvs $
492 mkFunTys (mkPredTys (eqSpecPreds eq_spec)) $
493 -- NB: the dict args are already in rep_arg_tys
494 -- because they might be flattened..
495 -- but the equality predicates are not
496 mkFunTys rep_arg_tys $
497 mkTyConApp tycon (mkTyVarTys univ_tvs)
499 eqSpecPreds :: [(TyVar,Type)] -> ThetaType
500 eqSpecPreds spec = [ mkEqPred (mkTyVarTy tv, ty) | (tv,ty) <- spec ]
502 mk_dict_strict_mark pred | isStrictPred pred = MarkedStrict
503 | otherwise = NotMarkedStrict
507 dataConName :: DataCon -> Name
510 dataConTag :: DataCon -> ConTag
513 dataConTyCon :: DataCon -> TyCon
514 dataConTyCon = dcTyCon
516 dataConRepType :: DataCon -> Type
517 dataConRepType = dcRepType
519 dataConIsInfix :: DataCon -> Bool
520 dataConIsInfix = dcInfix
522 dataConUnivTyVars :: DataCon -> [TyVar]
523 dataConUnivTyVars = dcUnivTyVars
525 dataConExTyVars :: DataCon -> [TyVar]
526 dataConExTyVars = dcExTyVars
528 dataConAllTyVars :: DataCon -> [TyVar]
529 dataConAllTyVars (MkData { dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs })
532 dataConEqSpec :: DataCon -> [(TyVar,Type)]
533 dataConEqSpec = dcEqSpec
535 dataConTheta :: DataCon -> ThetaType
536 dataConTheta = dcTheta
538 dataConWorkId :: DataCon -> Id
539 dataConWorkId dc = case dcIds dc of
540 DCIds _ wrk_id -> wrk_id
542 dataConWrapId_maybe :: DataCon -> Maybe Id
543 -- Returns Nothing if there is no wrapper for an algebraic data con
544 -- and also for a newtype (whose constructor is inlined compulsorily)
545 dataConWrapId_maybe dc = case dcIds dc of
546 DCIds mb_wrap _ -> mb_wrap
548 dataConWrapId :: DataCon -> Id
549 -- Returns an Id which looks like the Haskell-source constructor
550 dataConWrapId dc = case dcIds dc of
551 DCIds (Just wrap) _ -> wrap
552 DCIds Nothing wrk -> wrk -- worker=wrapper
554 dataConImplicitIds :: DataCon -> [Id]
555 dataConImplicitIds dc = case dcIds dc of
556 DCIds (Just wrap) work -> [wrap,work]
557 DCIds Nothing work -> [work]
559 dataConFieldLabels :: DataCon -> [FieldLabel]
560 dataConFieldLabels = dcFields
562 dataConFieldType :: DataCon -> FieldLabel -> Type
563 dataConFieldType con label = expectJust "unexpected label" $
564 lookup label (dcFields con `zip` dcOrigArgTys con)
566 dataConStrictMarks :: DataCon -> [StrictnessMark]
567 dataConStrictMarks = dcStrictMarks
569 dataConExStricts :: DataCon -> [StrictnessMark]
570 -- Strictness of *existential* arguments only
571 -- Usually empty, so we don't bother to cache this
572 dataConExStricts dc = map mk_dict_strict_mark (dcTheta dc)
574 dataConSourceArity :: DataCon -> Arity
575 -- Source-level arity of the data constructor
576 dataConSourceArity dc = length (dcOrigArgTys dc)
578 -- dataConRepArity gives the number of actual fields in the
579 -- {\em representation} of the data constructor. This may be more than appear
580 -- in the source code; the extra ones are the existentially quantified
582 dataConRepArity (MkData {dcRepArgTys = arg_tys}) = length arg_tys
584 isNullarySrcDataCon, isNullaryRepDataCon :: DataCon -> Bool
585 isNullarySrcDataCon dc = null (dcOrigArgTys dc)
586 isNullaryRepDataCon dc = null (dcRepArgTys dc)
588 dataConRepStrictness :: DataCon -> [StrictnessMark]
589 -- Give the demands on the arguments of a
590 -- Core constructor application (Con dc args)
591 dataConRepStrictness dc = dcRepStrictness dc
593 dataConSig :: DataCon -> ([TyVar], ThetaType, [Type])
594 dataConSig (MkData {dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs, dcEqSpec = eq_spec,
595 dcTheta = theta, dcOrigArgTys = arg_tys, dcTyCon = tycon})
596 = (univ_tvs ++ ex_tvs, eqSpecPreds eq_spec ++ theta, arg_tys)
598 dataConFullSig :: DataCon
599 -> ([TyVar], [TyVar], [(TyVar,Type)], ThetaType, [Type])
600 dataConFullSig (MkData {dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs, dcEqSpec = eq_spec,
601 dcTheta = theta, dcOrigArgTys = arg_tys, dcTyCon = tycon})
602 = (univ_tvs, ex_tvs, eq_spec, theta, arg_tys)
604 dataConStupidTheta :: DataCon -> ThetaType
605 dataConStupidTheta dc = dcStupidTheta dc
607 dataConResTys :: DataCon -> [Type]
608 dataConResTys dc = [substTyVar env tv | tv <- dcUnivTyVars dc]
610 env = mkTopTvSubst (dcEqSpec dc)
612 dataConInstTys :: DataCon -> Maybe [Type]
613 dataConInstTys = dcInstTys
615 dataConUserType :: DataCon -> Type
616 -- The user-declared type of the data constructor
617 -- in the nice-to-read form
618 -- T :: forall a. a -> T [a]
620 -- T :: forall b. forall a. (a=[b]) => a -> T b
621 -- NB: If the constructor is part of a data instance, the result type
622 -- mentions the family tycon, not the internal one.
623 dataConUserType (MkData { dcUnivTyVars = univ_tvs,
624 dcExTyVars = ex_tvs, dcEqSpec = eq_spec,
625 dcTheta = theta, dcOrigArgTys = arg_tys,
626 dcTyCon = tycon, dcInstTys = mb_insttys })
627 = mkForAllTys ((univ_tvs `minusList` map fst eq_spec) ++ ex_tvs) $
628 mkFunTys (mkPredTys theta) $
631 Nothing -> mkTyConApp tycon (map (substTyVar subst) univ_tvs)
632 Just insttys -> mkTyConApp ftycon insttys -- data instance
634 ftycon = case tyConFamily_maybe tycon of
635 Just ftycon -> ftycon
637 err = "dataConUserType: type patterns without family tycon"
639 subst = mkTopTvSubst eq_spec
641 dataConInstArgTys :: DataCon
642 -> [Type] -- Instantiated at these types
643 -- NB: these INCLUDE the existentially quantified arg types
644 -> [Type] -- Needs arguments of these types
645 -- NB: these INCLUDE the existentially quantified dict args
646 -- but EXCLUDE the data-decl context which is discarded
647 -- It's all post-flattening etc; this is a representation type
648 dataConInstArgTys (MkData {dcRepArgTys = arg_tys,
649 dcUnivTyVars = univ_tvs,
650 dcExTyVars = ex_tvs}) inst_tys
651 = ASSERT( length tyvars == length inst_tys )
652 map (substTyWith tyvars inst_tys) arg_tys
654 tyvars = univ_tvs ++ ex_tvs
657 -- And the same deal for the original arg tys
658 dataConInstOrigArgTys :: DataCon -> [Type] -> [Type]
659 dataConInstOrigArgTys dc@(MkData {dcOrigArgTys = arg_tys,
660 dcUnivTyVars = univ_tvs,
661 dcExTyVars = ex_tvs}) inst_tys
662 = ASSERT2( length tyvars == length inst_tys, ptext SLIT("dataConInstOrigArgTys") <+> ppr dc <+> ppr inst_tys )
663 map (substTyWith tyvars inst_tys) arg_tys
665 tyvars = univ_tvs ++ ex_tvs
668 These two functions get the real argument types of the constructor,
669 without substituting for any type variables.
671 dataConOrigArgTys returns the arg types of the wrapper, excluding all dictionary args.
673 dataConRepArgTys retuns the arg types of the worker, including all dictionaries, and
674 after any flattening has been done.
677 dataConOrigArgTys :: DataCon -> [Type]
678 dataConOrigArgTys dc = dcOrigArgTys dc
680 dataConRepArgTys :: DataCon -> [Type]
681 dataConRepArgTys dc = dcRepArgTys dc
686 isTupleCon :: DataCon -> Bool
687 isTupleCon (MkData {dcTyCon = tc}) = isTupleTyCon tc
689 isUnboxedTupleCon :: DataCon -> Bool
690 isUnboxedTupleCon (MkData {dcTyCon = tc}) = isUnboxedTupleTyCon tc
692 isVanillaDataCon :: DataCon -> Bool
693 isVanillaDataCon dc = dcVanilla dc
698 classDataCon :: Class -> DataCon
699 classDataCon clas = case tyConDataCons (classTyCon clas) of
700 (dict_constr:no_more) -> ASSERT( null no_more ) dict_constr
703 %************************************************************************
705 \subsection{Splitting products}
707 %************************************************************************
710 splitProductType_maybe
711 :: Type -- A product type, perhaps
712 -> Maybe (TyCon, -- The type constructor
713 [Type], -- Type args of the tycon
714 DataCon, -- The data constructor
715 [Type]) -- Its *representation* arg types
717 -- Returns (Just ...) for any
718 -- concrete (i.e. constructors visible)
719 -- single-constructor
720 -- not existentially quantified
721 -- type whether a data type or a new type
723 -- Rejecing existentials is conservative. Maybe some things
724 -- could be made to work with them, but I'm not going to sweat
725 -- it through till someone finds it's important.
727 splitProductType_maybe ty
728 = case splitTyConApp_maybe ty of
730 | isProductTyCon tycon -- Includes check for non-existential,
731 -- and for constructors visible
732 -> Just (tycon, ty_args, data_con, dataConInstArgTys data_con ty_args)
734 data_con = head (tyConDataCons tycon)
737 splitProductType str ty
738 = case splitProductType_maybe ty of
740 Nothing -> pprPanic (str ++ ": not a product") (pprType ty)
743 deepSplitProductType_maybe ty
744 = do { (res@(tycon, tycon_args, _, _)) <- splitProductType_maybe ty
746 | isNewTyCon tycon && not (isRecursiveTyCon tycon)
747 = deepSplitProductType_maybe (newTyConInstRhs tycon tycon_args)
748 | isNewTyCon tycon = Nothing -- cannot unbox through recursive newtypes
749 | otherwise = Just res}
753 deepSplitProductType str ty
754 = case deepSplitProductType_maybe ty of
756 Nothing -> pprPanic (str ++ ": not a product") (pprType ty)
758 computeRep :: [StrictnessMark] -- Original arg strictness
759 -> [Type] -- and types
760 -> ([StrictnessMark], -- Representation arg strictness
763 computeRep stricts tys
764 = unzip $ concat $ zipWithEqual "computeRep" unbox stricts tys
766 unbox NotMarkedStrict ty = [(NotMarkedStrict, ty)]
767 unbox MarkedStrict ty = [(MarkedStrict, ty)]
768 unbox MarkedUnboxed ty = zipEqual "computeRep" (dataConRepStrictness arg_dc) arg_tys
770 (tycon, tycon_args, arg_dc, arg_tys)
771 = deepSplitProductType "unbox_strict_arg_ty" ty