2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[DataCon]{@DataCon@: Data Constructors}
8 DataCon, DataConIds(..),
11 dataConRepType, dataConSig, dataConName, dataConTag, dataConTyCon,
12 dataConTyVars, dataConResTys,
14 dataConInstArgTys, dataConOrigArgTys, dataConInstResTy,
15 dataConInstOrigArgTys, dataConRepArgTys,
16 dataConFieldLabels, dataConFieldType,
17 dataConStrictMarks, dataConExStricts,
18 dataConSourceArity, dataConRepArity,
20 dataConWorkId, dataConWrapId, dataConWrapId_maybe, dataConImplicitIds,
22 isNullarySrcDataCon, isNullaryRepDataCon, isTupleCon, isUnboxedTupleCon,
23 isVanillaDataCon, classDataCon,
25 splitProductType_maybe, splitProductType,
28 #include "HsVersions.h"
30 import Type ( Type, ThetaType, substTyWith, substTy, zipOpenTvSubst,
31 mkForAllTys, mkFunTys, mkTyConApp,
33 mkPredTys, isStrictPred, pprType
35 import TyCon ( TyCon, FieldLabel, tyConDataCons,
36 isProductTyCon, isTupleTyCon, isUnboxedTupleTyCon )
37 import Class ( Class, classTyCon )
38 import Name ( Name, NamedThing(..), nameUnique )
39 import Var ( TyVar, Id )
40 import BasicTypes ( Arity, StrictnessMark(..) )
42 import Unique ( Unique, Uniquable(..) )
43 import ListSetOps ( assoc )
44 import Util ( zipEqual, zipWithEqual )
45 import Maybes ( expectJust )
49 Data constructor representation
50 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51 Consider the following Haskell data type declaration
53 data T = T !Int ![Int]
55 Using the strictness annotations, GHC will represent this as
59 That is, the Int has been unboxed. Furthermore, the Haskell source construction
69 That is, the first argument is unboxed, and the second is evaluated. Finally,
70 pattern matching is translated too:
72 case e of { T a b -> ... }
76 case e of { T a' b -> let a = I# a' in ... }
78 To keep ourselves sane, we name the different versions of the data constructor
79 differently, as follows.
82 Note [Data Constructor Naming]
83 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84 Each data constructor C has two, and possibly three, Names associated with it:
86 OccName Name space Used for
87 ---------------------------------------------------------------------------
88 * The "source data con" C DataName The DataCon itself
89 * The "real data con" C VarName Its worker Id
90 * The "wrapper data con" $WC VarName Wrapper Id (optional)
92 Each of these three has a distinct Unique. The "source data con" name
93 appears in the output of the renamer, and names the Haskell-source
94 data constructor. The type checker translates it into either the wrapper Id
95 (if it exists) or worker Id (otherwise).
97 The data con has one or two Ids associated with it:
99 The "worker Id", is the actual data constructor.
100 Its type may be different to the Haskell source constructor
102 - useless dict args are dropped
103 - strict args may be flattened
104 The worker is very like a primop, in that it has no binding.
106 Newtypes have no worker Id
109 The "wrapper Id", $WC, whose type is exactly what it looks like
110 in the source program. It is an ordinary function,
111 and it gets a top-level binding like any other function.
113 The wrapper Id isn't generated for a data type if the worker
114 and wrapper are identical. It's always generated for a newtype.
118 A note about the stupid context
119 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120 Data types can have a context:
122 data (Eq a, Ord b) => T a b = T1 a b | T2 a
124 and that makes the constructors have a context too
125 (notice that T2's context is "thinned"):
127 T1 :: (Eq a, Ord b) => a -> b -> T a b
128 T2 :: (Eq a) => a -> T a b
130 Furthermore, this context pops up when pattern matching
131 (though GHC hasn't implemented this, but it is in H98, and
132 I've fixed GHC so that it now does):
136 f :: Eq a => T a b -> a
138 I say the context is "stupid" because the dictionaries passed
139 are immediately discarded -- they do nothing and have no benefit.
140 It's a flaw in the language.
142 Up to now [March 2002] I have put this stupid context into the
143 type of the "wrapper" constructors functions, T1 and T2, but
144 that turned out to be jolly inconvenient for generics, and
145 record update, and other functions that build values of type T
146 (because they don't have suitable dictionaries available).
148 So now I've taken the stupid context out. I simply deal with
149 it separately in the type checker on occurrences of a
150 constructor, either in an expression or in a pattern.
152 [May 2003: actually I think this decision could evasily be
153 reversed now, and probably should be. Generics could be
154 disabled for types with a stupid context; record updates now
155 (H98) needs the context too; etc. It's an unforced change, so
156 I'm leaving it for now --- but it does seem odd that the
157 wrapper doesn't include the stupid context.]
159 [July 04] With the advent of generalised data types, it's less obvious
160 what the "stupid context" is. Consider
161 C :: forall a. Ord a => a -> a -> T (Foo a)
162 Does the C constructor in Core contain the Ord dictionary? Yes, it must:
167 C a (d:Ord a) (p:a) (q:a) -> compare d p q
169 Note that (Foo a) might not be an instance of Ord.
171 %************************************************************************
173 \subsection{Data constructors}
175 %************************************************************************
180 dcName :: Name, -- This is the name of the *source data con*
181 -- (see "Note [Data Constructor Naming]" above)
182 dcUnique :: Unique, -- Cached from Name
187 -- data Eq a => T a = forall b. Ord b => MkT a [b]
189 -- The next six fields express the type of the constructor, in pieces
193 -- dcStupidTheta = [Eq a]
195 -- dcOrigArgTys = [a,List b]
199 dcVanilla :: Bool, -- True <=> This is a vanilla Haskell 98 data constructor
200 -- Its type is of form
201 -- forall a1..an . t1 -> ... tm -> T a1..an
202 -- No existentials, no GADTs, nothing.
204 -- NB1: the order of the forall'd variables does matter;
205 -- for a vanilla constructor, we assume that if the result
206 -- type is (T t1 ... tn) then we can instantiate the constr
207 -- at types [t1, ..., tn]
209 -- NB2: a vanilla constructor can still be declared in GADT-style
210 -- syntax, provided its type looks like the above.
212 dcTyVars :: [TyVar], -- Universally-quantified type vars
213 -- for the data constructor.
214 -- See NB1 on dcVanilla for the conneciton between dcTyVars and dcResTys
216 -- In general, the dcTyVars are NOT NECESSARILY THE SAME AS THE TYVARS
217 -- FOR THE PARENT TyCon. With GADTs the data con might not even have
218 -- the same number of type variables.
219 -- [This is a change (Oct05): previously, vanilla datacons guaranteed to
220 -- have the same type variables as their parent TyCon, but that seems ugly.]
222 dcStupidTheta :: ThetaType, -- This is a "thinned" version of
223 -- the context of the data decl.
224 -- "Thinned", because the Report says
225 -- to eliminate any constraints that don't mention
226 -- tyvars free in the arg types for this constructor
228 -- "Stupid", because the dictionaries aren't used for anything.
230 -- Indeed, [as of March 02] they are no
231 -- longer in the type of the wrapper Id, because
232 -- that makes it harder to use the wrap-id to rebuild
233 -- values after record selection or in generics.
235 -- Fact: the free tyvars of dcStupidTheta are a subset of
236 -- the free tyvars of dcResTys
237 -- Reason: dcStupidTeta is gotten by instantiating the
238 -- stupid theta from the tycon (see BuildTyCl.mkDataConStupidTheta)
240 dcTheta :: ThetaType, -- The existentially quantified stuff
242 dcOrigArgTys :: [Type], -- Original argument types
243 -- (before unboxing and flattening of
246 -- Result type of constructor is T t1..tn
247 dcTyCon :: TyCon, -- Result tycon, T
248 dcResTys :: [Type], -- Result type args, t1..tn
250 -- Now the strictness annotations and field labels of the constructor
251 dcStrictMarks :: [StrictnessMark],
252 -- Strictness annotations as decided by the compiler.
253 -- Does *not* include the existential dictionaries
254 -- length = dataConSourceArity dataCon
256 dcFields :: [FieldLabel],
257 -- Field labels for this constructor, in the
258 -- same order as the argument types;
259 -- length = 0 (if not a record) or dataConSourceArity.
261 -- Constructor representation
262 dcRepArgTys :: [Type], -- Final, representation argument types,
263 -- after unboxing and flattening,
264 -- and *including* existential dictionaries
266 dcRepStrictness :: [StrictnessMark], -- One for each *representation* argument
268 dcRepType :: Type, -- Type of the constructor
269 -- forall a b . Ord b => a -> [b] -> MkT a
270 -- (this is *not* of the constructor wrapper Id:
271 -- see notes after this data type declaration)
273 -- Notice that the existential type parameters come *second*.
274 -- Reason: in a case expression we may find:
275 -- case (e :: T t) of { MkT b (d:Ord b) (x:t) (xs:[b]) -> ... }
276 -- It's convenient to apply the rep-type of MkT to 't', to get
277 -- forall b. Ord b => ...
278 -- and use that to check the pattern. Mind you, this is really only
282 -- Finally, the curried worker function that corresponds to the constructor
283 -- It doesn't have an unfolding; the code generator saturates these Ids
284 -- and allocates a real constructor when it finds one.
286 -- An entirely separate wrapper function is built in TcTyDecls
289 dcInfix :: Bool -- True <=> declared infix
290 -- Used for Template Haskell and 'deriving' only
291 -- The actual fixity is stored elsewhere
295 = NewDC Id -- Newtypes have only a wrapper, but no worker
296 | AlgDC (Maybe Id) Id -- Algebraic data types always have a worker, and
297 -- may or may not have a wrapper, depending on whether
298 -- the wrapper does anything.
300 -- _Neither_ the worker _nor_ the wrapper take the dcStupidTheta dicts as arguments
302 -- The wrapper takes dcOrigArgTys as its arguments
303 -- The worker takes dcRepArgTys as its arguments
304 -- If the worker is absent, dcRepArgTys is the same as dcOrigArgTys
306 -- The 'Nothing' case of AlgDC is important
307 -- Not only is this efficient,
308 -- but it also ensures that the wrapper is replaced
309 -- by the worker (becuase it *is* the wroker)
310 -- even when there are no args. E.g. in
312 -- the (:) *is* the worker.
313 -- This is really important in rule matching,
314 -- (We could match on the wrappers,
315 -- but that makes it less likely that rules will match
316 -- when we bring bits of unfoldings together.)
321 fIRST_TAG = 1 -- Tags allocated from here for real constructors
324 The dcRepType field contains the type of the representation of a contructor
325 This may differ from the type of the contructor *Id* (built
326 by MkId.mkDataConId) for two reasons:
327 a) the constructor Id may be overloaded, but the dictionary isn't stored
328 e.g. data Eq a => T a = MkT a a
330 b) the constructor may store an unboxed version of a strict field.
332 Here's an example illustrating both:
333 data Ord a => T a = MkT Int! a
335 T :: Ord a => Int -> a -> T a
337 Trep :: Int# -> a -> T a
338 Actually, the unboxed part isn't implemented yet!
341 %************************************************************************
343 \subsection{Instances}
345 %************************************************************************
348 instance Eq DataCon where
349 a == b = getUnique a == getUnique b
350 a /= b = getUnique a /= getUnique b
352 instance Ord DataCon where
353 a <= b = getUnique a <= getUnique b
354 a < b = getUnique a < getUnique b
355 a >= b = getUnique a >= getUnique b
356 a > b = getUnique a > getUnique b
357 compare a b = getUnique a `compare` getUnique b
359 instance Uniquable DataCon where
362 instance NamedThing DataCon where
365 instance Outputable DataCon where
366 ppr con = ppr (dataConName con)
368 instance Show DataCon where
369 showsPrec p con = showsPrecSDoc p (ppr con)
373 %************************************************************************
375 \subsection{Construction}
377 %************************************************************************
381 -> Bool -- Declared infix
382 -> Bool -- Vanilla (see notes with dcVanilla)
383 -> [StrictnessMark] -> [FieldLabel]
384 -> [TyVar] -> ThetaType -> ThetaType
385 -> [Type] -> TyCon -> [Type]
388 -- Can get the tag from the TyCon
390 mkDataCon name declared_infix vanilla
391 arg_stricts -- Must match orig_arg_tys 1-1
393 tyvars stupid_theta theta orig_arg_tys tycon res_tys
397 con = MkData {dcName = name,
398 dcUnique = nameUnique name, dcVanilla = vanilla,
399 dcTyVars = tyvars, dcStupidTheta = stupid_theta, dcTheta = theta,
400 dcOrigArgTys = orig_arg_tys, dcTyCon = tycon, dcResTys = res_tys,
401 dcRepArgTys = rep_arg_tys,
402 dcStrictMarks = arg_stricts, dcRepStrictness = rep_arg_stricts,
403 dcFields = fields, dcTag = tag, dcRepType = ty,
404 dcIds = ids, dcInfix = declared_infix}
406 -- Strictness marks for source-args
407 -- *after unboxing choices*,
408 -- but *including existential dictionaries*
410 -- The 'arg_stricts' passed to mkDataCon are simply those for the
411 -- source-language arguments. We add extra ones for the
412 -- dictionary arguments right here.
413 dict_tys = mkPredTys theta
414 real_arg_tys = dict_tys ++ orig_arg_tys
415 real_stricts = map mk_dict_strict_mark theta ++ arg_stricts
417 -- Representation arguments and demands
418 (rep_arg_stricts, rep_arg_tys) = computeRep real_stricts real_arg_tys
420 tag = assoc "mkDataCon" (tyConDataCons tycon `zip` [fIRST_TAG..]) con
421 ty = mkForAllTys tyvars (mkFunTys rep_arg_tys result_ty)
422 -- NB: the existential dict args are already in rep_arg_tys
424 result_ty = mkTyConApp tycon res_tys
426 mk_dict_strict_mark pred | isStrictPred pred = MarkedStrict
427 | otherwise = NotMarkedStrict
431 dataConName :: DataCon -> Name
434 dataConTag :: DataCon -> ConTag
437 dataConTyCon :: DataCon -> TyCon
438 dataConTyCon = dcTyCon
440 dataConRepType :: DataCon -> Type
441 dataConRepType = dcRepType
443 dataConIsInfix :: DataCon -> Bool
444 dataConIsInfix = dcInfix
446 dataConTyVars :: DataCon -> [TyVar]
447 dataConTyVars = dcTyVars
449 dataConWorkId :: DataCon -> Id
450 dataConWorkId dc = case dcIds dc of
451 AlgDC _ wrk_id -> wrk_id
452 NewDC _ -> pprPanic "dataConWorkId" (ppr dc)
454 dataConWrapId_maybe :: DataCon -> Maybe Id
455 dataConWrapId_maybe dc = case dcIds dc of
456 AlgDC mb_wrap _ -> mb_wrap
457 NewDC wrap -> Just wrap
459 dataConWrapId :: DataCon -> Id
460 -- Returns an Id which looks like the Haskell-source constructor
461 dataConWrapId dc = case dcIds dc of
462 AlgDC (Just wrap) _ -> wrap
463 AlgDC Nothing wrk -> wrk -- worker=wrapper
466 dataConImplicitIds :: DataCon -> [Id]
467 dataConImplicitIds dc = case dcIds dc of
468 AlgDC (Just wrap) work -> [wrap,work]
469 AlgDC Nothing work -> [work]
472 dataConFieldLabels :: DataCon -> [FieldLabel]
473 dataConFieldLabels = dcFields
475 dataConFieldType :: DataCon -> FieldLabel -> Type
476 dataConFieldType con label = expectJust "unexpected label" $
477 lookup label (dcFields con `zip` dcOrigArgTys con)
479 dataConStrictMarks :: DataCon -> [StrictnessMark]
480 dataConStrictMarks = dcStrictMarks
482 dataConExStricts :: DataCon -> [StrictnessMark]
483 -- Strictness of *existential* arguments only
484 -- Usually empty, so we don't bother to cache this
485 dataConExStricts dc = map mk_dict_strict_mark (dcTheta dc)
487 dataConSourceArity :: DataCon -> Arity
488 -- Source-level arity of the data constructor
489 dataConSourceArity dc = length (dcOrigArgTys dc)
491 -- dataConRepArity gives the number of actual fields in the
492 -- {\em representation} of the data constructor. This may be more than appear
493 -- in the source code; the extra ones are the existentially quantified
495 dataConRepArity (MkData {dcRepArgTys = arg_tys}) = length arg_tys
497 isNullarySrcDataCon, isNullaryRepDataCon :: DataCon -> Bool
498 isNullarySrcDataCon dc = null (dcOrigArgTys dc)
499 isNullaryRepDataCon dc = null (dcRepArgTys dc)
501 dataConRepStrictness :: DataCon -> [StrictnessMark]
502 -- Give the demands on the arguments of a
503 -- Core constructor application (Con dc args)
504 dataConRepStrictness dc = dcRepStrictness dc
506 dataConSig :: DataCon -> ([TyVar], ThetaType,
507 [Type], TyCon, [Type])
509 dataConSig (MkData {dcTyVars = tyvars, dcTheta = theta,
510 dcOrigArgTys = arg_tys, dcTyCon = tycon, dcResTys = res_tys})
511 = (tyvars, theta, arg_tys, tycon, res_tys)
513 dataConStupidTheta :: DataCon -> ThetaType
514 dataConStupidTheta dc = dcStupidTheta dc
516 dataConResTys :: DataCon -> [Type]
517 dataConResTys dc = dcResTys dc
519 dataConInstArgTys :: DataCon
520 -> [Type] -- Instantiated at these types
521 -- NB: these INCLUDE the existentially quantified arg types
522 -> [Type] -- Needs arguments of these types
523 -- NB: these INCLUDE the existentially quantified dict args
524 -- but EXCLUDE the data-decl context which is discarded
525 -- It's all post-flattening etc; this is a representation type
526 dataConInstArgTys (MkData {dcRepArgTys = arg_tys, dcTyVars = tyvars}) inst_tys
527 = ASSERT( length tyvars == length inst_tys )
528 map (substTyWith tyvars inst_tys) arg_tys
530 dataConInstResTy :: DataCon -> [Type] -> Type
531 dataConInstResTy (MkData {dcTyVars = tyvars, dcTyCon = tc, dcResTys = res_tys}) inst_tys
532 = ASSERT( length tyvars == length inst_tys )
533 substTy (zipOpenTvSubst tyvars inst_tys) (mkTyConApp tc res_tys)
534 -- res_tys can't currently contain any foralls,
535 -- but might in future; hence zipOpenTvSubst
537 -- And the same deal for the original arg tys
538 dataConInstOrigArgTys :: DataCon -> [Type] -> [Type]
539 dataConInstOrigArgTys (MkData {dcOrigArgTys = arg_tys, dcTyVars = tyvars}) inst_tys
540 = ASSERT( length tyvars == length inst_tys )
541 map (substTyWith tyvars inst_tys) arg_tys
544 These two functions get the real argument types of the constructor,
545 without substituting for any type variables.
547 dataConOrigArgTys returns the arg types of the wrapper, excluding all dictionary args.
549 dataConRepArgTys retuns the arg types of the worker, including all dictionaries, and
550 after any flattening has been done.
553 dataConOrigArgTys :: DataCon -> [Type]
554 dataConOrigArgTys dc = dcOrigArgTys dc
556 dataConRepArgTys :: DataCon -> [Type]
557 dataConRepArgTys dc = dcRepArgTys dc
562 isTupleCon :: DataCon -> Bool
563 isTupleCon (MkData {dcTyCon = tc}) = isTupleTyCon tc
565 isUnboxedTupleCon :: DataCon -> Bool
566 isUnboxedTupleCon (MkData {dcTyCon = tc}) = isUnboxedTupleTyCon tc
568 isVanillaDataCon :: DataCon -> Bool
569 isVanillaDataCon dc = dcVanilla dc
574 classDataCon :: Class -> DataCon
575 classDataCon clas = case tyConDataCons (classTyCon clas) of
576 (dict_constr:no_more) -> ASSERT( null no_more ) dict_constr
579 %************************************************************************
581 \subsection{Splitting products}
583 %************************************************************************
586 splitProductType_maybe
587 :: Type -- A product type, perhaps
588 -> Maybe (TyCon, -- The type constructor
589 [Type], -- Type args of the tycon
590 DataCon, -- The data constructor
591 [Type]) -- Its *representation* arg types
593 -- Returns (Just ...) for any
594 -- concrete (i.e. constructors visible)
595 -- single-constructor
596 -- not existentially quantified
597 -- type whether a data type or a new type
599 -- Rejecing existentials is conservative. Maybe some things
600 -- could be made to work with them, but I'm not going to sweat
601 -- it through till someone finds it's important.
603 splitProductType_maybe ty
604 = case splitTyConApp_maybe ty of
606 | isProductTyCon tycon -- Includes check for non-existential,
607 -- and for constructors visible
608 -> Just (tycon, ty_args, data_con, dataConInstArgTys data_con ty_args)
610 data_con = head (tyConDataCons tycon)
613 splitProductType str ty
614 = case splitProductType_maybe ty of
616 Nothing -> pprPanic (str ++ ": not a product") (pprType ty)
619 computeRep :: [StrictnessMark] -- Original arg strictness
620 -> [Type] -- and types
621 -> ([StrictnessMark], -- Representation arg strictness
624 computeRep stricts tys
625 = unzip $ concat $ zipWithEqual "computeRep" unbox stricts tys
627 unbox NotMarkedStrict ty = [(NotMarkedStrict, ty)]
628 unbox MarkedStrict ty = [(MarkedStrict, ty)]
629 unbox MarkedUnboxed ty = zipEqual "computeRep" (dataConRepStrictness arg_dc) arg_tys
631 (_, _, arg_dc, arg_tys) = splitProductType "unbox_strict_arg_ty" ty