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 dataConArgTys, dataConOrigArgTys, dataConInstOrigArgTys,
13 dataConRepArgTys, dataConTheta,
14 dataConFieldLabels, dataConStrictMarks, dataConExStricts,
15 dataConSourceArity, dataConRepArity,
17 dataConWorkId, dataConWrapId, dataConWrapId_maybe, dataConImplicitIds,
19 isNullaryDataCon, isTupleCon, isUnboxedTupleCon,
20 isExistentialDataCon, classDataCon, dataConExistentialTyVars,
22 splitProductType_maybe, splitProductType,
25 #include "HsVersions.h"
27 import {-# SOURCE #-} Subst( substTyWith )
28 import {-# SOURCE #-} PprType( pprType )
30 import Type ( Type, ThetaType,
31 mkForAllTys, mkFunTys, mkTyConApp,
32 mkTyVarTys, splitTyConApp_maybe,
33 mkPredTys, isStrictPred
35 import TyCon ( TyCon, tyConDataCons, tyConDataCons, isProductTyCon,
36 isTupleTyCon, isUnboxedTupleTyCon )
37 import Class ( Class, classTyCon )
38 import Name ( Name, NamedThing(..), nameUnique )
39 import Var ( TyVar, Id )
40 import FieldLabel ( FieldLabel )
41 import BasicTypes ( Arity, StrictnessMark(..) )
43 import Unique ( Unique, Uniquable(..) )
44 import ListSetOps ( assoc )
45 import Util ( zipEqual, zipWithEqual, notNull )
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 currently do get a worker-Id, but it is never used.
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 type of
143 the "wrapper" constructors functions, T1 and T2, but that turned out
144 to be jolly inconvenient for generics, and record update, and other
145 functions that build values of type T (because they don't have
146 suitable dictionaries available).
148 So now I've taken the stupid context out. I simply deal with it
149 separately in the type checker on occurrences of a constructor, either
150 in an expression or in a pattern.
152 [May 2003: actually I think this decision could evasily be reversed now,
153 and probably should be. Generics could be disabled for types with
154 a stupid context; record updates now (H98) needs the context too; etc.
155 It's an unforced change, so I'm leaving it for now --- but it does seem
156 odd that the wrapper doesn't include the stupid context.]
160 %************************************************************************
162 \subsection{Data constructors}
164 %************************************************************************
168 = MkData { -- Used for data constructors only;
169 -- there *is* no constructor for a newtype
171 dcName :: Name, -- This is the name of the *source data con*
172 -- (see "Note [Data Constructor Naming]" above)
174 dcUnique :: Unique, -- Cached from Name
179 -- data Eq a => T a = forall b. Ord b => MkT a [b]
181 dcRepType :: Type, -- Type of the constructor
182 -- forall a b . Ord b => a -> [b] -> MkT a
183 -- (this is *not* of the constructor wrapper Id:
184 -- see notes after this data type declaration)
186 -- Notice that the existential type parameters come *second*.
187 -- Reason: in a case expression we may find:
188 -- case (e :: T t) of { MkT b (d:Ord b) (x:t) (xs:[b]) -> ... }
189 -- It's convenient to apply the rep-type of MkT to 't', to get
190 -- forall b. Ord b => ...
191 -- and use that to check the pattern. Mind you, this is really only
195 -- The next six fields express the type of the constructor, in pieces
201 -- dcExTheta = [Ord b]
202 -- dcOrigArgTys = [a,List b]
205 dcTyVars :: [TyVar], -- Type vars for the data type decl
206 -- These are ALWAYS THE SAME AS THE TYVARS
207 -- FOR THE PARENT TyCon. We occasionally rely on
208 -- this just to avoid redundant instantiation
210 dcStupidTheta :: ThetaType, -- This is a "thinned" version of the context of
212 -- "Thinned", because the Report says
213 -- to eliminate any constraints that don't mention
214 -- tyvars free in the arg types for this constructor
216 -- "Stupid", because the dictionaries aren't used for anything.
218 -- Indeed, [as of March 02] they are no
219 -- longer in the type of the wrapper Id, because
220 -- that makes it harder to use the wrap-id to rebuild
221 -- values after record selection or in generics.
223 dcExTyVars :: [TyVar], -- Ditto for the context of the constructor,
224 dcExTheta :: ThetaType, -- the existentially quantified stuff
226 dcOrigArgTys :: [Type], -- Original argument types
227 -- (before unboxing and flattening of
230 -- Now the strictness annotations and field labels of the constructor
231 dcStrictMarks :: [StrictnessMark],
232 -- Strictness annotations as decided by the compiler.
233 -- Does *not* include the existential dictionaries
234 -- length = dataConSourceArity dataCon
236 dcFields :: [FieldLabel],
237 -- Field labels for this constructor, in the
238 -- same order as the argument types;
239 -- length = 0 (if not a record) or dataConSourceArity.
241 -- Constructor representation
242 dcRepArgTys :: [Type], -- Final, representation argument types,
243 -- after unboxing and flattening,
244 -- and *including* existential dictionaries
246 dcRepStrictness :: [StrictnessMark], -- One for each representation argument
248 dcTyCon :: TyCon, -- Result tycon
250 -- Finally, the curried worker function that corresponds to the constructor
251 -- It doesn't have an unfolding; the code generator saturates these Ids
252 -- and allocates a real constructor when it finds one.
254 -- An entirely separate wrapper function is built in TcTyDecls
260 = NewDC Id -- Newtypes have only a wrapper, but no worker
261 | AlgDC (Maybe Id) Id -- Algebraic data types always have a worker, and
262 -- may or may not have a wrapper, depending on whether
263 -- the wrapper does anything.
265 -- *Neither* the worker *nor* the wrapper take the dcStupidTheta dicts as arguments
267 -- The wrapper takes dcOrigArgTys as its arguments
268 -- The worker takes dcRepArgTys as its arguments
269 -- If the worker is absent, dcRepArgTys is the same as dcOrigArgTys
271 -- The 'Nothing' case of AlgDC is important
272 -- Not only is this efficient,
273 -- but it also ensures that the wrapper is replaced
274 -- by the worker (becuase it *is* the wroker)
275 -- even when there are no args. E.g. in
277 -- the (:) *is* the worker.
278 -- This is really important in rule matching,
279 -- (We could match on the wrappers,
280 -- but that makes it less likely that rules will match
281 -- when we bring bits of unfoldings together.)
286 fIRST_TAG = 1 -- Tags allocated from here for real constructors
289 The dcRepType field contains the type of the representation of a contructor
290 This may differ from the type of the contructor *Id* (built
291 by MkId.mkDataConId) for two reasons:
292 a) the constructor Id may be overloaded, but the dictionary isn't stored
293 e.g. data Eq a => T a = MkT a a
295 b) the constructor may store an unboxed version of a strict field.
297 Here's an example illustrating both:
298 data Ord a => T a = MkT Int! a
300 T :: Ord a => Int -> a -> T a
302 Trep :: Int# -> a -> T a
303 Actually, the unboxed part isn't implemented yet!
306 %************************************************************************
308 \subsection{Instances}
310 %************************************************************************
313 instance Eq DataCon where
314 a == b = getUnique a == getUnique b
315 a /= b = getUnique a /= getUnique b
317 instance Ord DataCon where
318 a <= b = getUnique a <= getUnique b
319 a < b = getUnique a < getUnique b
320 a >= b = getUnique a >= getUnique b
321 a > b = getUnique a > getUnique b
322 compare a b = getUnique a `compare` getUnique b
324 instance Uniquable DataCon where
327 instance NamedThing DataCon where
330 instance Outputable DataCon where
331 ppr con = ppr (dataConName con)
333 instance Show DataCon where
334 showsPrec p con = showsPrecSDoc p (ppr con)
338 %************************************************************************
340 \subsection{Construction}
342 %************************************************************************
346 -> [StrictnessMark] -> [FieldLabel]
347 -> [TyVar] -> ThetaType
348 -> [TyVar] -> ThetaType
352 -- Can get the tag from the TyCon
355 arg_stricts -- Must match orig_arg_tys 1-1
357 tyvars theta ex_tyvars ex_theta orig_arg_tys tycon
361 con = MkData {dcName = name,
362 dcUnique = nameUnique name,
363 dcTyVars = tyvars, dcStupidTheta = theta,
364 dcOrigArgTys = orig_arg_tys,
365 dcRepArgTys = rep_arg_tys,
366 dcExTyVars = ex_tyvars, dcExTheta = ex_theta,
367 dcStrictMarks = arg_stricts, dcRepStrictness = rep_arg_stricts,
368 dcFields = fields, dcTag = tag, dcTyCon = tycon, dcRepType = ty,
371 -- Strictness marks for source-args
372 -- *after unboxing choices*,
373 -- but *including existential dictionaries*
375 -- The 'arg_stricts' passed to mkDataCon are simply those for the
376 -- source-language arguments. We add extra ones for the
377 -- dictionary arguments right here.
378 ex_dict_tys = mkPredTys ex_theta
379 real_arg_tys = ex_dict_tys ++ orig_arg_tys
380 real_stricts = map mk_dict_strict_mark ex_theta ++ arg_stricts
382 -- Representation arguments and demands
383 (rep_arg_stricts, rep_arg_tys) = computeRep real_stricts real_arg_tys
385 tag = assoc "mkDataCon" (tyConDataCons tycon `zip` [fIRST_TAG..]) con
386 ty = mkForAllTys (tyvars ++ ex_tyvars)
387 (mkFunTys rep_arg_tys result_ty)
388 -- NB: the existential dict args are already in rep_arg_tys
390 result_ty = mkTyConApp tycon (mkTyVarTys tyvars)
392 mk_dict_strict_mark pred | isStrictPred pred = MarkedStrict
393 | otherwise = NotMarkedStrict
397 dataConName :: DataCon -> Name
400 dataConTag :: DataCon -> ConTag
403 dataConTyCon :: DataCon -> TyCon
404 dataConTyCon = dcTyCon
406 dataConRepType :: DataCon -> Type
407 dataConRepType = dcRepType
409 dataConWorkId :: DataCon -> Id
410 dataConWorkId dc = case dcIds dc of
411 AlgDC _ wrk_id -> wrk_id
412 NewDC _ -> pprPanic "dataConWorkId" (ppr dc)
414 dataConWrapId_maybe :: DataCon -> Maybe Id
415 dataConWrapId_maybe dc = case dcIds dc of
416 AlgDC mb_wrap _ -> mb_wrap
417 NewDC wrap -> Just wrap
419 dataConWrapId :: DataCon -> Id
420 -- Returns an Id which looks like the Haskell-source constructor
421 dataConWrapId dc = case dcIds dc of
422 AlgDC (Just wrap) _ -> wrap
423 AlgDC Nothing wrk -> wrk -- worker=wrapper
426 dataConImplicitIds :: DataCon -> [Id]
427 dataConImplicitIds dc = case dcIds dc of
428 AlgDC (Just wrap) work -> [wrap,work]
429 AlgDC Nothing work -> [work]
432 dataConFieldLabels :: DataCon -> [FieldLabel]
433 dataConFieldLabels = dcFields
435 dataConStrictMarks :: DataCon -> [StrictnessMark]
436 dataConStrictMarks = dcStrictMarks
438 dataConExStricts :: DataCon -> [StrictnessMark]
439 -- Strictness of *existential* arguments only
440 -- Usually empty, so we don't bother to cache this
441 dataConExStricts dc = map mk_dict_strict_mark (dcExTheta dc)
443 -- Number of type-instantiation arguments
444 -- All the remaining arguments of the DataCon are (notionally)
445 -- stored in the DataCon, and are matched in a case expression
446 dataConNumInstArgs (MkData {dcTyVars = tyvars}) = length tyvars
448 dataConSourceArity :: DataCon -> Arity
449 -- Source-level arity of the data constructor
450 dataConSourceArity dc = length (dcOrigArgTys dc)
452 -- dataConRepArity gives the number of actual fields in the
453 -- {\em representation} of the data constructor. This may be more than appear
454 -- in the source code; the extra ones are the existentially quantified
456 dataConRepArity (MkData {dcRepArgTys = arg_tys}) = length arg_tys
458 isNullaryDataCon con = dataConRepArity con == 0
460 dataConRepStrictness :: DataCon -> [StrictnessMark]
461 -- Give the demands on the arguments of a
462 -- Core constructor application (Con dc args)
463 dataConRepStrictness dc = dcRepStrictness dc
465 dataConSig :: DataCon -> ([TyVar], ThetaType,
469 dataConSig (MkData {dcTyVars = tyvars, dcStupidTheta = theta,
470 dcExTyVars = ex_tyvars, dcExTheta = ex_theta,
471 dcOrigArgTys = arg_tys, dcTyCon = tycon})
472 = (tyvars, theta, ex_tyvars, ex_theta, arg_tys, tycon)
474 dataConArgTys :: DataCon
475 -> [Type] -- Instantiated at these types
476 -- NB: these INCLUDE the existentially quantified arg types
477 -> [Type] -- Needs arguments of these types
478 -- NB: these INCLUDE the existentially quantified dict args
479 -- but EXCLUDE the data-decl context which is discarded
480 -- It's all post-flattening etc; this is a representation type
482 dataConArgTys (MkData {dcRepArgTys = arg_tys, dcTyVars = tyvars,
483 dcExTyVars = ex_tyvars}) inst_tys
484 = map (substTyWith (tyvars ++ ex_tyvars) inst_tys) arg_tys
486 dataConTheta :: DataCon -> ThetaType
487 dataConTheta dc = dcStupidTheta dc
489 dataConExistentialTyVars :: DataCon -> [TyVar]
490 dataConExistentialTyVars dc = dcExTyVars dc
492 -- And the same deal for the original arg tys:
494 dataConInstOrigArgTys :: DataCon -> [Type] -> [Type]
495 dataConInstOrigArgTys (MkData {dcOrigArgTys = arg_tys, dcTyVars = tyvars,
496 dcExTyVars = ex_tyvars}) inst_tys
497 = map (substTyWith (tyvars ++ ex_tyvars) inst_tys) arg_tys
500 These two functions get the real argument types of the constructor,
501 without substituting for any type variables.
503 dataConOrigArgTys returns the arg types of the wrapper, excluding all dictionary args.
505 dataConRepArgTys retuns the arg types of the worker, including all dictionaries, and
506 after any flattening has been done.
509 dataConOrigArgTys :: DataCon -> [Type]
510 dataConOrigArgTys dc = dcOrigArgTys dc
512 dataConRepArgTys :: DataCon -> [Type]
513 dataConRepArgTys dc = dcRepArgTys dc
518 isTupleCon :: DataCon -> Bool
519 isTupleCon (MkData {dcTyCon = tc}) = isTupleTyCon tc
521 isUnboxedTupleCon :: DataCon -> Bool
522 isUnboxedTupleCon (MkData {dcTyCon = tc}) = isUnboxedTupleTyCon tc
524 isExistentialDataCon :: DataCon -> Bool
525 isExistentialDataCon (MkData {dcExTyVars = tvs}) = notNull tvs
530 classDataCon :: Class -> DataCon
531 classDataCon clas = case tyConDataCons (classTyCon clas) of
532 (dict_constr:no_more) -> ASSERT( null no_more ) dict_constr
535 %************************************************************************
537 \subsection{Splitting products}
539 %************************************************************************
542 splitProductType_maybe
543 :: Type -- A product type, perhaps
544 -> Maybe (TyCon, -- The type constructor
545 [Type], -- Type args of the tycon
546 DataCon, -- The data constructor
547 [Type]) -- Its *representation* arg types
549 -- Returns (Just ...) for any
550 -- concrete (i.e. constructors visible)
551 -- single-constructor
552 -- not existentially quantified
553 -- type whether a data type or a new type
555 -- Rejecing existentials is conservative. Maybe some things
556 -- could be made to work with them, but I'm not going to sweat
557 -- it through till someone finds it's important.
559 splitProductType_maybe ty
560 = case splitTyConApp_maybe ty of
562 | isProductTyCon tycon -- Includes check for non-existential,
563 -- and for constructors visible
564 -> Just (tycon, ty_args, data_con, dataConArgTys data_con ty_args)
566 data_con = head (tyConDataCons tycon)
569 splitProductType str ty
570 = case splitProductType_maybe ty of
572 Nothing -> pprPanic (str ++ ": not a product") (pprType ty)
575 computeRep :: [StrictnessMark] -- Original arg strictness
576 -> [Type] -- and types
577 -> ([StrictnessMark], -- Representation arg strictness
580 computeRep stricts tys
581 = unzip $ concat $ zipWithEqual "computeRep" unbox stricts tys
583 unbox NotMarkedStrict ty = [(NotMarkedStrict, ty)]
584 unbox MarkedStrict ty = [(MarkedStrict, ty)]
585 unbox MarkedUnboxed ty = zipEqual "computeRep" (dataConRepStrictness arg_dc) arg_tys
587 (_, _, arg_dc, arg_tys) = splitProductType "unbox_strict_arg_ty" ty