DataCon.lhs 39.8 KB
 simonm committed Dec 02, 1998 1 %  Simon Marlow committed Oct 11, 2006 2 % (c) The University of Glasgow 2006  simonm committed Dec 02, 1998 3 4 % (c) The GRASP/AQUA Project, Glasgow University, 1998 %  simonm committed Apr 06, 1999 5 \section[DataCon]{@DataCon@: Data Constructors}  simonm committed Dec 02, 1998 6 7  \begin{code}  Ian Lynagh committed Nov 04, 2011 8 9 10 11 12 13 14 {-# OPTIONS -fno-warn-tabs #-} -- The above warning supression flag is a temporary kludge. -- While working on this module you are encouraged to remove it and -- detab the module (please do the detabbing in a separate patch). See -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces -- for details  simonm committed Dec 02, 1998 15 module DataCon (  batterseapower committed Jul 31, 2008 16  -- * Main data types  Simon Peyton Jones committed Dec 23, 2012 17  DataCon, DataConRep(..), HsBang(..), StrictnessMark(..),  batterseapower committed Jul 31, 2008 18 19 20 21  ConTag, -- ** Type construction mkDataCon, fIRST_TAG,  Simon Peyton Jones committed Jan 25, 2013 22  buildAlgTyCon,  batterseapower committed Jul 31, 2008 23 24  -- ** Type deconstruction  chak@cse.unsw.edu.au. committed Sep 18, 2006 25  dataConRepType, dataConSig, dataConFullSig,  chak@cse.unsw.edu.au. committed Mar 25, 2009 26 27  dataConName, dataConIdentity, dataConTag, dataConTyCon, dataConOrigTyCon, dataConUserType,  simonpj@microsoft.com committed Apr 22, 2007 28  dataConUnivTyVars, dataConExTyVars, dataConAllTyVars,  29  dataConEqSpec, eqSpecPreds, dataConTheta,  chak@cse.unsw.edu.au. committed Mar 25, 2009 30  dataConStupidTheta,  simonpj@microsoft.com committed Apr 22, 2007 31  dataConInstArgTys, dataConOrigArgTys, dataConOrigResTy,  simonpj@microsoft.com committed Sep 10, 2008 32  dataConInstOrigArgTys, dataConRepArgTys,  simonmar committed Oct 06, 2005 33  dataConFieldLabels, dataConFieldType,  Simon Peyton Jones committed Dec 14, 2012 34  dataConStrictMarks,  batterseapower committed May 15, 2012 35  dataConSourceArity, dataConRepArity, dataConRepRepArity,  simonpj committed Sep 30, 2004 36  dataConIsInfix,  simonpj committed Oct 09, 2003 37  dataConWorkId, dataConWrapId, dataConWrapId_maybe, dataConImplicitIds,  Simon Peyton Jones committed Dec 14, 2012 38  dataConRepStrictness, dataConRepBangs, dataConBoxer,  batterseapower committed Jul 31, 2008 39 40  -- ** Predicates on DataCons  Simon Peyton Jones committed Aug 31, 2012 41  isNullarySrcDataCon, isNullaryRepDataCon, isTupleDataCon, isUnboxedTupleCon,  42  isVanillaDataCon, classDataCon, dataConCannotMatch,  Simon Peyton Jones committed Dec 23, 2012 43  isBanged, isMarkedStrict, eqHsBang,  simonpj committed Mar 23, 2000 44   Simon Peyton Jones committed Feb 06, 2012 45  -- ** Promotion related functions  Simon Peyton Jones committed Jan 25, 2013 46  promoteKind, promoteDataCon, promoteDataCon_maybe  simonm committed Dec 02, 1998 47 48 49 50  ) where #include "HsVersions.h"  Simon Peyton Jones committed Dec 14, 2012 51 import {-# SOURCE #-} MkId( DataConBoxer )  Simon Marlow committed Oct 11, 2006 52 import Type  Simon Peyton Jones committed Feb 06, 2012 53 import TypeRep( Type(..) ) -- Used in promoteType  Simon Peyton Jones committed Feb 16, 2012 54 import PrelNames( liftedTypeKindTyConKey )  Simon Peyton Jones committed Jan 25, 2013 55 import ForeignCall( CType )  Simon Peyton Jones committed Dec 23, 2012 56 import Coercion  Simon Peyton Jones committed Feb 06, 2012 57 import Kind  58 import Unify  Simon Marlow committed Oct 11, 2006 59 60 61 62 import TyCon import Class import Name import Var  simonm committed Dec 02, 1998 63 import Outputable  Simon Marlow committed Oct 11, 2006 64 65 66 import Unique import ListSetOps import Util  batterseapower committed Sep 06, 2011 67 import BasicTypes  chak@cse.unsw.edu.au. committed Sep 20, 2006 68 import FastString  Simon Marlow committed May 09, 2007 69 import Module  Simon Peyton Jones committed Feb 06, 2012 70 import VarEnv  Simon Marlow committed May 09, 2007 71   waern committed Mar 30, 2010 72 import qualified Data.Data as Data  Simon Marlow committed Jul 12, 2011 73 import qualified Data.Typeable  Simon Peyton Jones committed Jan 25, 2013 74 import Data.Maybe  Simon Marlow committed May 09, 2007 75 76 import Data.Char import Data.Word  simonm committed Dec 02, 1998 77 78 79 \end{code}  simonpj committed Feb 12, 2003 80 81 82 Data constructor representation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Consider the following Haskell data type declaration  simonpj committed Mar 23, 2000 83   simonpj committed Feb 12, 2003 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114  data T = T !Int ![Int] Using the strictness annotations, GHC will represent this as data T = T Int# [Int] That is, the Int has been unboxed. Furthermore, the Haskell source construction T e1 e2 is translated to case e1 of { I# x -> case e2 of { r -> T x r }} That is, the first argument is unboxed, and the second is evaluated. Finally, pattern matching is translated too: case e of { T a b -> ... } becomes case e of { T a' b -> let a = I# a' in ... } To keep ourselves sane, we name the different versions of the data constructor differently, as follows. Note [Data Constructor Naming] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  Dan Licata committed Oct 10, 2007 115 Each data constructor C has two, and possibly up to four, Names associated with it:  simonpj committed Mar 23, 2000 116   simonpj@microsoft.com committed May 28, 2009 117 118 119 120 121 122  OccName Name space Name of Notes --------------------------------------------------------------------------- The "data con itself" C DataName DataCon In dom( GlobalRdrEnv ) The "worker data con" C VarName Id The worker The "wrapper data con" $WC VarName Id The wrapper The "newtype coercion" :CoT TcClsName TyCon  Dan Licata committed Oct 10, 2007 123 124 125 126 127 128  EVERY data constructor (incl for newtypes) has the former two (the data con itself, and its worker. But only some data constructors have a wrapper (see Note [The need for a wrapper]). Each of these three has a distinct Unique. The "data con itself" name  simonpj committed Feb 12, 2003 129 130 131 132 133 134 appears in the output of the renamer, and names the Haskell-source data constructor. The type checker translates it into either the wrapper Id (if it exists) or worker Id (otherwise). The data con has one or two Ids associated with it:  chak@cse.unsw.edu.au. committed Sep 20, 2006 135 136 The "worker Id", is the actual data constructor. * Every data constructor (newtype or data type) has a worker  simonpj committed Feb 12, 2003 137   chak@cse.unsw.edu.au. committed Sep 20, 2006 138 * The worker is very like a primop, in that it has no binding.  simonpj committed Feb 12, 2003 139   chak@cse.unsw.edu.au. committed Sep 20, 2006 140 141 * For a *data* type, the worker *is* the data constructor; it has no unfolding  simonpj committed Feb 12, 2003 142   chak@cse.unsw.edu.au. committed Sep 20, 2006 143 144 145 146 * For a *newtype*, the worker has a compulsory unfolding which does a cast, e.g. newtype T = MkT Int The worker for MkT has unfolding  batterseapower committed Jul 31, 2008 147  \\(x:Int). x cast sym CoT  chak@cse.unsw.edu.au. committed Sep 20, 2006 148 149  Here CoT is the type constructor, witnessing the FC axiom axiom CoT : T = Int  simonpj committed Feb 12, 2003 150   batterseapower committed Jul 31, 2008 151 The "wrapper Id", \$WC, goes as follows  chak@cse.unsw.edu.au. committed Sep 20, 2006 152 153 154 155 156 157 158 159  * Its type is exactly what it looks like in the source program. * It is an ordinary function, and it gets a top-level binding like any other function. * The wrapper Id isn't generated for a data type if there is nothing for the wrapper to do. That is, if its defn would be  batterseapower committed Jul 31, 2008 160  \$wC = C  chak@cse.unsw.edu.au. committed Sep 20, 2006 161   Dan Licata committed Oct 10, 2007 162 163 Note [The need for a wrapper] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  chak@cse.unsw.edu.au. committed Sep 20, 2006 164 165 166 167 Why might the wrapper have anything to do? Two reasons: * Unboxing strict fields (with -funbox-strict-fields) data T = MkT !(Int,Int)  batterseapower committed Jul 31, 2008 168 169  \$wMkT :: (Int,Int) -> T \$wMkT (x,y) = MkT x y  chak@cse.unsw.edu.au. committed Sep 20, 2006 170 171 172 173 174 175 176 177 178 179 180 181  Notice that the worker has two fields where the wapper has just one. That is, the worker has type MkT :: Int -> Int -> T * Equality constraints for GADTs data T a where { MkT :: a -> T [a] } The worker gets a type with explicit equality constraints, thus: MkT :: forall a b. (a=[b]) => b -> T a The wrapper has the programmer-specified type:  batterseapower committed Jul 31, 2008 182 183  \$wMkT :: a -> T [a] \$wMkT a x = MkT [a] a [a] x  chak@cse.unsw.edu.au. committed Sep 20, 2006 184  The third argument is a coerion  simonpj@microsoft.com committed Sep 20, 2008 185  [a] :: [a]~[a]  simonpj committed Mar 23, 2000 186   Dan Licata committed Oct 10, 2007 187 188 INVARIANT: the dictionary constructor for a class never has a wrapper.  simonpj committed Mar 23, 2000 189 190   simonpj committed Apr 01, 2002 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 A note about the stupid context ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Data types can have a context: data (Eq a, Ord b) => T a b = T1 a b | T2 a and that makes the constructors have a context too (notice that T2's context is "thinned"): T1 :: (Eq a, Ord b) => a -> b -> T a b T2 :: (Eq a) => a -> T a b Furthermore, this context pops up when pattern matching (though GHC hasn't implemented this, but it is in H98, and I've fixed GHC so that it now does): f (T2 x) = x gets inferred type f :: Eq a => T a b -> a I say the context is "stupid" because the dictionaries passed are immediately discarded -- they do nothing and have no benefit. It's a flaw in the language.  simonpj committed Sep 30, 2004 215 216 217 218 219 220 221 222 223  Up to now [March 2002] I have put this stupid context into the type of the "wrapper" constructors functions, T1 and T2, but that turned out to be jolly inconvenient for generics, and record update, and other functions that build values of type T (because they don't have suitable dictionaries available). So now I've taken the stupid context out. I simply deal with it separately in the type checker on occurrences of a constructor, either in an expression or in a pattern.  simonpj committed Apr 01, 2002 224   simonpj committed Sep 30, 2004 225 226 227 228 229 230  [May 2003: actually I think this decision could evasily be reversed now, and probably should be. Generics could be disabled for types with a stupid context; record updates now (H98) needs the context too; etc. It's an unforced change, so I'm leaving it for now --- but it does seem odd that the wrapper doesn't include the stupid context.]  simonpj committed Apr 01, 2002 231   simonpj committed Sep 30, 2004 232 233 234 235 [July 04] With the advent of generalised data types, it's less obvious what the "stupid context" is. Consider C :: forall a. Ord a => a -> a -> T (Foo a) Does the C constructor in Core contain the Ord dictionary? Yes, it must:  simonpj committed May 07, 2003 236   simonpj committed Sep 30, 2004 237 238 239 240  f :: T b -> Ordering f = /\b. \x:T b. case x of C a (d:Ord a) (p:a) (q:a) -> compare d p q  simonpj committed Apr 01, 2002 241   simonpj committed Sep 30, 2004 242 Note that (Foo a) might not be an instance of Ord.  simonpj committed Mar 23, 2000 243   simonm committed Dec 02, 1998 244 245 246 247 248 249 250 %************************************************************************ %* * \subsection{Data constructors} %* * %************************************************************************ \begin{code}  batterseapower committed Jul 31, 2008 251 -- | A data constructor  simonm committed Dec 02, 1998 252 data DataCon  simonpj committed Sep 30, 2004 253  = MkData {  simonmar committed May 22, 2003 254 255  dcName :: Name, -- This is the name of the *source data con* -- (see "Note [Data Constructor Naming]" above)  simonpj committed Sep 30, 2004 256  dcUnique :: Unique, -- Cached from Name  batterseapower committed Jul 31, 2008 257  dcTag :: ConTag, -- ^ Tag, used for ordering 'DataCon's  simonpj committed Dec 18, 1998 258 259 260  -- Running example: --  chak@cse.unsw.edu.au. committed Sep 18, 2006 261 262  -- *** As declared by the user -- data T a where  chak@cse.unsw.edu.au. committed Aug 28, 2007 263  -- MkT :: forall x y. (x~y,Ord x) => x -> y -> T (x,y)  simonpj committed Dec 18, 1998 264   chak@cse.unsw.edu.au. committed Sep 18, 2006 265 266  -- *** As represented internally -- data T a where  simonpj@microsoft.com committed Sep 20, 2008 267  -- MkT :: forall a. forall x y. (a~(x,y),x~y,Ord x) => x -> y -> T a  chak@cse.unsw.edu.au. committed Sep 18, 2006 268  --  simonpj committed Dec 18, 1998 269 270 271  -- The next six fields express the type of the constructor, in pieces -- e.g. --  chak@cse.unsw.edu.au. committed Sep 18, 2006 272 273  -- dcUnivTyVars = [a] -- dcExTyVars = [x,y]  simonpj@microsoft.com committed Sep 20, 2008 274  -- dcEqSpec = [a~(x,y)]  275  -- dcOtherTheta = [x~y, Ord x]  Simon Peyton Jones committed Nov 06, 2012 276  -- dcOrigArgTys = [x,y]  simonpj@microsoft.com committed Apr 22, 2007 277  -- dcRepTyCon = T  simonpj committed Sep 30, 2004 278 279 280 281  dcVanilla :: Bool, -- True <=> This is a vanilla Haskell 98 data constructor -- Its type is of form -- forall a1..an . t1 -> ... tm -> T a1..an  chak@cse.unsw.edu.au. committed Sep 18, 2006 282  -- No existentials, no coercions, nothing.  283  -- That is: dcExTyVars = dcEqSpec = dcOtherTheta = []  chak@cse.unsw.edu.au. committed Sep 18, 2006 284 285 286 287 288  -- NB 1: newtypes always have a vanilla data con -- NB 2: a vanilla constructor can still be declared in GADT-style -- syntax, provided its type looks like the above. -- The declaration format is held in the TyCon (algTcGadtSyntax)  simonpj@microsoft.com committed May 13, 2009 289  dcUnivTyVars :: [TyVar], -- Universally-quantified type vars [a,b,c]  simonpj@microsoft.com committed May 02, 2007 290  -- INVARIANT: length matches arity of the dcRepTyCon  simonpj@microsoft.com committed May 13, 2009 291  --- result type of (rep) data con is exactly (T a b c)  simonpj@microsoft.com committed May 02, 2007 292   chak@cse.unsw.edu.au. committed Sep 18, 2006 293 294  dcExTyVars :: [TyVar], -- Existentially-quantified type vars -- In general, the dcUnivTyVars are NOT NECESSARILY THE SAME AS THE TYVARS  simonpj committed Oct 14, 2005 295 296 297 298  -- FOR THE PARENT TyCon. With GADTs the data con might not even have -- the same number of type variables. -- [This is a change (Oct05): previously, vanilla datacons guaranteed to -- have the same type variables as their parent TyCon, but that seems ugly.]  simonpj committed Sep 30, 2004 299   simonpj@microsoft.com committed Oct 04, 2006 300 301 302  -- INVARIANT: the UnivTyVars and ExTyVars all have distinct OccNames -- Reason: less confusing, and easier to generate IfaceSyn  chak@cse.unsw.edu.au. committed Sep 18, 2006 303  dcEqSpec :: [(TyVar,Type)], -- Equalities derived from the result type,  Ian Lynagh committed Jul 21, 2008 304  -- _as written by the programmer_  chak@cse.unsw.edu.au. committed Sep 18, 2006 305 306  -- This field allows us to move conveniently between the two ways -- of representing a GADT constructor's type:  simonpj@microsoft.com committed Sep 20, 2008 307  -- MkT :: forall a b. (a ~ [b]) => b -> T a  chak@cse.unsw.edu.au. committed Sep 18, 2006 308  -- MkT :: forall b. b -> T [b]  simonpj@microsoft.com committed Sep 20, 2008 309  -- Each equality is of the form (a ~ ty), where 'a' is one of  chak@cse.unsw.edu.au. committed Sep 18, 2006 310 311  -- the universally quantified type variables  chak@cse.unsw.edu.au. committed Aug 28, 2007 312 313 314  -- The next two fields give the type context of the data constructor -- (aside from the GADT constraints, -- which are given by the dcExpSpec)  chak@cse.unsw.edu.au. committed Sep 18, 2006 315 316  -- In GADT form, this is *exactly* what the programmer writes, even if -- the context constrains only universally quantified variables  chak@cse.unsw.edu.au. committed Aug 28, 2007 317  -- MkT :: forall a b. (a ~ b, Ord b) => a -> T a b  318  dcOtherTheta :: ThetaType, -- The other constraints in the data con's type  Simon Peyton Jones committed Apr 20, 2011 319  -- other than those in the dcEqSpec  chak@cse.unsw.edu.au. committed Sep 18, 2006 320 321 322 323  dcStupidTheta :: ThetaType, -- The context of the data type declaration -- data Eq a => T a = ... -- or, rather, a "thinned" version thereof  simonpj committed Apr 01, 2002 324 325 326 327  -- "Thinned", because the Report says -- to eliminate any constraints that don't mention -- tyvars free in the arg types for this constructor --  chak@cse.unsw.edu.au. committed Sep 18, 2006 328 329  -- INVARIANT: the free tyvars of dcStupidTheta are a subset of dcUnivTyVars -- Reason: dcStupidTeta is gotten by thinning the stupid theta from the tycon  simonpj committed Apr 01, 2002 330  --  chak@cse.unsw.edu.au. committed Sep 18, 2006 331 332 333 334  -- "Stupid", because the dictionaries aren't used for anything. -- Indeed, [as of March 02] they are no longer in the type of -- the wrapper Id, because that makes it harder to use the wrap-id -- to rebuild values after record selection or in generics.  simonm committed Dec 02, 1998 335   simonm committed Apr 06, 1999 336  dcOrigArgTys :: [Type], -- Original argument types  chak@cse.unsw.edu.au. committed Sep 18, 2006 337  -- (before unboxing and flattening of strict fields)  simonpj@microsoft.com committed Sep 23, 2008 338  dcOrigResTy :: Type, -- Original result type, as seen by the user  simonpj@microsoft.com committed Apr 22, 2007 339 340 341 342  -- NB: for a data instance, the original user result type may -- differ from the DataCon's representation TyCon. Example -- data instance T [a] where MkT :: a -> T [a] -- The OrigResTy is T [a], but the dcRepTyCon might be :T123  simonpj committed Sep 30, 2004 343   simonpj committed Dec 18, 1998 344  -- Now the strictness annotations and field labels of the constructor  Simon Peyton Jones committed Dec 19, 2012 345  -- See Note [Bangs on data constructor arguments]  Simon Peyton Jones committed Dec 14, 2012 346  dcArgBangs :: [HsBang],  simonpj committed Oct 09, 2003 347  -- Strictness annotations as decided by the compiler.  Simon Peyton Jones committed Dec 14, 2012 348 349  -- Matches 1-1 with dcOrigArgTys -- Hence length = dataConSourceArity dataCon  simonm committed Apr 06, 1999 350 351 352  dcFields :: [FieldLabel], -- Field labels for this constructor, in the  simonpj@microsoft.com committed Apr 22, 2007 353  -- same order as the dcOrigArgTys;  simonm committed Apr 06, 1999 354  -- length = 0 (if not a record) or dataConSourceArity.  simonm committed Dec 02, 1998 355   Simon Peyton Jones committed Dec 14, 2012 356 357 358 359 360  -- The curried worker function that corresponds to the constructor: -- It doesn't have an unfolding; the code generator saturates these Ids -- and allocates a real constructor when it finds one. dcWorkId :: Id,  simonpj committed Oct 09, 2003 361  -- Constructor representation  Simon Peyton Jones committed Dec 14, 2012 362  dcRep :: DataConRep,  simonpj committed Oct 09, 2003 363   Simon Peyton Jones committed Dec 14, 2012 364 365 366  -- Cached dcRepArity :: Arity, -- == length dataConRepArgTys dcSourceArity :: Arity, -- == length dcOrigArgTys  simonpj committed Sep 30, 2004 367   simonpj@microsoft.com committed Apr 22, 2007 368 369 370  -- Result type of constructor is T t1..tn dcRepTyCon :: TyCon, -- Result tycon, T  simonpj committed Sep 30, 2004 371  dcRepType :: Type, -- Type of the constructor  simonpj@microsoft.com committed Sep 20, 2008 372  -- forall a x y. (a~(x,y), x~y, Ord x) =>  chak@cse.unsw.edu.au. committed Dec 07, 2007 373  -- x -> y -> T a  simonpj committed Sep 30, 2004 374  -- (this is *not* of the constructor wrapper Id:  chak@cse.unsw.edu.au. committed Sep 18, 2006 375  -- see Note [Data con representation] below)  simonpj committed Sep 30, 2004 376 377  -- Notice that the existential type parameters come *second*. -- Reason: in a case expression we may find:  chak@cse.unsw.edu.au. committed Dec 07, 2007 378 379  -- case (e :: T t) of -- MkT x y co1 co2 (d:Ord x) (v:r) (w:F s) -> ...  simonpj committed Sep 30, 2004 380  -- It's convenient to apply the rep-type of MkT to 't', to get  simonpj@microsoft.com committed Sep 20, 2008 381  -- forall x y. (t~(x,y), x~y, Ord x) => x -> y -> T t  simonpj committed Sep 30, 2004 382  -- and use that to check the pattern. Mind you, this is really only  chak@cse.unsw.edu.au. committed Dec 07, 2007 383  -- used in CoreLint.  simonpj committed Oct 09, 2003 384 385   Simon Peyton Jones committed Oct 19, 2012 386  dcInfix :: Bool, -- True <=> declared infix  simonpj committed Jun 02, 2004 387 388  -- Used for Template Haskell and 'deriving' only -- The actual fixity is stored elsewhere  Simon Peyton Jones committed Oct 19, 2012 389 390  dcPromoted :: Maybe TyCon -- The promoted TyCon if this DataCon is promotable  Simon Peyton Jones committed Dec 05, 2012 391  -- See Note [Promoted data constructors] in TyCon  simonm committed Dec 02, 1998 392  }  Simon Marlow committed Jul 12, 2011 393  deriving Data.Typeable.Typeable  simonm committed Dec 02, 1998 394   Simon Peyton Jones committed Dec 14, 2012 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 data DataConRep = NoDataConRep -- No wrapper | DCR { dcr_wrap_id :: Id -- Takes src args, unboxes/flattens, -- and constructs the representation , dcr_boxer :: DataConBoxer , dcr_arg_tys :: [Type] -- Final, representation argument types, -- after unboxing and flattening, -- and *including* all evidence args , dcr_stricts :: [StrictnessMark] -- 1-1 with dcr_arg_tys -- See also Note [Data-con worker strictness] in MkId.lhs , dcr_bangs :: [HsBang] -- The actual decisions made (including failures) -- 1-1 with orig_arg_tys  Simon Peyton Jones committed Dec 19, 2012 412 413  -- See Note [Bangs on data constructor arguments]  Simon Peyton Jones committed Dec 14, 2012 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439  } -- Algebraic data types always have a worker, and -- may or may not have a wrapper, depending on whether -- the wrapper does anything. -- -- Data types have a worker with no unfolding -- Newtypes just have a worker, which has a compulsory unfolding (just a cast) -- _Neither_ the worker _nor_ the wrapper take the dcStupidTheta dicts as arguments -- The wrapper (if it exists) takes dcOrigArgTys as its arguments -- The worker takes dataConRepArgTys as its arguments -- If the worker is absent, dataConRepArgTys is the same as dcOrigArgTys -- The 'NoDataConRep' case is important -- Not only is this efficient, -- but it also ensures that the wrapper is replaced -- by the worker (because it *is* the worker) -- even when there are no args. E.g. in -- f (:) x -- the (:) *is* the worker. -- This is really important in rule matching, -- (We could match on the wrappers, -- but that makes it less likely that rules will match -- when we bring bits of unfoldings together.)  Simon Peyton Jones committed Dec 23, 2012 440 441 442 443 ------------------------- -- HsBang describes what the *programmer* wrote -- This info is retained in the DataCon.dcStrictMarks field data HsBang  Simon Peyton Jones committed Jan 14, 2013 444 445 446 447 448  = HsUserBang -- The user's source-code request (Maybe Bool) -- Just True {-# UNPACK #-} -- Just False {-# NOUNPACK #-} -- Nothing no pragma Bool -- True <=> '!' specified  Simon Peyton Jones committed Dec 23, 2012 449   Simon Peyton Jones committed Jan 14, 2013 450 451  | HsNoBang -- Lazy field -- HsUserBang Nothing False means the same as HsNoBang  Simon Peyton Jones committed Dec 23, 2012 452 453 454 455  | HsUnpack -- Definite commitment: this field is strict and unboxed (Maybe Coercion) -- co :: arg-ty ~ product-ty  Simon Peyton Jones committed Jan 14, 2013 456  | HsStrict -- Definite commitment: this field is strict but not unboxed  Simon Peyton Jones committed Dec 23, 2012 457 458 459 460 461 462  deriving (Data.Data, Data.Typeable) ------------------------- -- StrictnessMark is internal only, used to indicate strictness -- of the DataCon *worker* fields data StrictnessMark = MarkedStrict | NotMarkedStrict  simonm committed Dec 02, 1998 463 464 \end{code}  chak@cse.unsw.edu.au. committed Sep 18, 2006 465 466 Note [Data con representation] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  simonpj committed Mar 23, 2000 467 The dcRepType field contains the type of the representation of a contructor  simonm committed Dec 02, 1998 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 This may differ from the type of the contructor *Id* (built by MkId.mkDataConId) for two reasons: a) the constructor Id may be overloaded, but the dictionary isn't stored e.g. data Eq a => T a = MkT a a b) the constructor may store an unboxed version of a strict field. Here's an example illustrating both: data Ord a => T a = MkT Int! a Here T :: Ord a => Int -> a -> T a but the rep type is Trep :: Int# -> a -> T a Actually, the unboxed part isn't implemented yet!  Simon Peyton Jones committed Dec 19, 2012 483 484 485 486 487 Note [Bangs on data constructor arguments] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Consider data T = MkT !Int {-# UNPACK #-} !Int Bool Its dcArgBangs field records the *users* specifications, in this case  Simon Peyton Jones committed Jan 14, 2013 488 489 490  [ HsUserBang Nothing True , HsUserBang (Just True) True , HsNoBang]  Simon Peyton Jones committed Dec 19, 2012 491 492 493 494 495 496 497 498 499 500 501 502 503 See the declaration of HsBang in BasicTypes The dcr_bangs field of the dcRep field records the *actual, decided* representation of the data constructor. Without -O this might be [HsStrict, HsStrict, HsNoBang] With -O it might be [HsStrict, HsUnpack, HsNoBang] With -funbox-small-strict-fields it might be [HsUnpack, HsUnpack, HsNoBang] For imported data types, the dcArgBangs field is just the same as the dcr_bangs field; we don't know what the user originally said.  simonm committed Dec 02, 1998 504   simonpj committed May 18, 1999 505 506 507 508 509 510 %************************************************************************ %* * \subsection{Instances} %* * %************************************************************************  simonm committed Dec 02, 1998 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 \begin{code} instance Eq DataCon where a == b = getUnique a == getUnique b a /= b = getUnique a /= getUnique b instance Ord DataCon where a <= b = getUnique a <= getUnique b a < b = getUnique a < getUnique b a >= b = getUnique a >= getUnique b a > b = getUnique a > getUnique b compare a b = getUnique a compare getUnique b instance Uniquable DataCon where getUnique = dcUnique instance NamedThing DataCon where getName = dcName instance Outputable DataCon where ppr con = ppr (dataConName con)  Krzysztof Gogolewski committed Apr 21, 2013 532 533 534 535 instance OutputableBndr DataCon where pprInfixOcc con = pprInfixName (dataConName con) pprPrefixOcc con = pprPrefixName (dataConName con)  waern committed Mar 30, 2010 536 537 538 539 540 instance Data.Data DataCon where -- don't traverse? toConstr _ = abstractConstr "DataCon" gunfold _ _ = error "gunfold" dataTypeOf _ = mkNoRepType "DataCon"  Simon Peyton Jones committed Dec 23, 2012 541 542  instance Outputable HsBang where  Simon Peyton Jones committed Jan 14, 2013 543 544 545 546 547 548 549 550 551 552  ppr HsNoBang = empty ppr (HsUserBang prag bang) = pp_unpk prag <+> ppWhen bang (char '!') ppr (HsUnpack Nothing) = ptext (sLit "Unpk") ppr (HsUnpack (Just co)) = ptext (sLit "Unpk") <> parens (ppr co) ppr HsStrict = ptext (sLit "SrictNotUnpacked") pp_unpk :: Maybe Bool -> SDoc pp_unpk Nothing = empty pp_unpk (Just True) = ptext (sLit "{-# UNPACK #-}") pp_unpk (Just False) = ptext (sLit "{-# NOUNPACK #-}")  Simon Peyton Jones committed Dec 23, 2012 553 554 555 556 557 558 559  instance Outputable StrictnessMark where ppr MarkedStrict = ptext (sLit "!") ppr NotMarkedStrict = empty eqHsBang :: HsBang -> HsBang -> Bool  Simon Peyton Jones committed Jan 18, 2013 560 eqHsBang HsNoBang HsNoBang = True  Simon Peyton Jones committed Dec 23, 2012 561 eqHsBang HsStrict HsStrict = True  Simon Peyton Jones committed Jan 14, 2013 562 eqHsBang (HsUserBang u1 b1) (HsUserBang u2 b2) = u1==u2 && b1==b2  Simon Peyton Jones committed Dec 23, 2012 563 564 565 566 567 eqHsBang (HsUnpack Nothing) (HsUnpack Nothing) = True eqHsBang (HsUnpack (Just c1)) (HsUnpack (Just c2)) = eqType (coercionType c1) (coercionType c2) eqHsBang _ _ = False isBanged :: HsBang -> Bool  Simon Peyton Jones committed Jan 14, 2013 568 569 570 isBanged HsNoBang = False isBanged (HsUserBang Nothing bang) = bang isBanged _ = True  Simon Peyton Jones committed Dec 23, 2012 571 572 573 574  isMarkedStrict :: StrictnessMark -> Bool isMarkedStrict NotMarkedStrict = False isMarkedStrict _ = True -- All others are strict  simonm committed Dec 02, 1998 575 576 \end{code}  simonpj committed May 18, 1999 577 578 579  %************************************************************************ %* *  simonpj committed Apr 01, 2002 580 \subsection{Construction}  simonpj committed May 18, 1999 581 582 583 %* * %************************************************************************  simonm committed Dec 02, 1998 584 \begin{code}  batterseapower committed Jul 31, 2008 585 -- | Build a new data constructor  simonpj committed Feb 12, 2003 586 mkDataCon :: Name  batterseapower committed Jul 31, 2008 587  -> Bool -- ^ Is the constructor declared infix?  simonpj@microsoft.com committed May 06, 2010 588  -> [HsBang] -- ^ Strictness annotations written in the source file  simonpj@microsoft.com committed Sep 23, 2008 589 590  -> [FieldLabel] -- ^ Field labels for the constructor, if it is a record, -- otherwise empty  batterseapower committed Jul 31, 2008 591 592 593 594  -> [TyVar] -- ^ Universally quantified type variables -> [TyVar] -- ^ Existentially quantified type variables -> [(TyVar,Type)] -- ^ GADT equalities -> ThetaType -- ^ Theta-type occuring before the arguments proper  simonpj@microsoft.com committed Sep 23, 2008 595 596 597 598 599  -> [Type] -- ^ Original argument types -> Type -- ^ Original result type -> TyCon -- ^ Representation type constructor -> ThetaType -- ^ The "stupid theta", context of the data declaration -- e.g. @data Eq a => T a ...@  Simon Peyton Jones committed Dec 14, 2012 600 601  -> Id -- ^ Worker Id -> DataConRep -- ^ Representation  simonm committed Dec 02, 1998 602 603 604  -> DataCon -- Can get the tag from the TyCon  chak@cse.unsw.edu.au. committed Sep 18, 2006 605 mkDataCon name declared_infix  simonpj committed Oct 09, 2003 606  arg_stricts -- Must match orig_arg_tys 1-1  simonpj committed Feb 12, 2003 607  fields  chak@cse.unsw.edu.au. committed Sep 18, 2006 608 609  univ_tvs ex_tvs eq_spec theta  simonpj@microsoft.com committed Sep 23, 2008 610  orig_arg_tys orig_res_ty rep_tycon  Simon Peyton Jones committed Dec 14, 2012 611  stupid_theta work_id rep  simonpj@microsoft.com committed Sep 23, 2006 612 613 614 615 616 617 618 619 -- Warning: mkDataCon is not a good place to check invariants. -- If the programmer writes the wrong result type in the decl, thus: -- data T a where { MkT :: S } -- then it's possible that the univ_tvs may hit an assertion failure -- if you pull on univ_tvs. This case is checked by checkValidDataCon, -- so the error is detected properly... it's just that asaertions here -- are a little dodgy.  Simon Peyton Jones committed Oct 19, 2012 620  = con  simonm committed Dec 02, 1998 621  where  chak@cse.unsw.edu.au. committed Sep 18, 2006 622  is_vanilla = null ex_tvs && null eq_spec && null theta  simonpj@microsoft.com committed Sep 23, 2006 623  con = MkData {dcName = name, dcUnique = nameUnique name,  chak@cse.unsw.edu.au. committed Sep 18, 2006 624 625 626  dcVanilla = is_vanilla, dcInfix = declared_infix, dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs, dcEqSpec = eq_spec,  627  dcOtherTheta = theta,  chak@cse.unsw.edu.au. committed Aug 28, 2007 628  dcStupidTheta = stupid_theta,  simonpj@microsoft.com committed Apr 22, 2007 629  dcOrigArgTys = orig_arg_tys, dcOrigResTy = orig_res_ty,  simonpj@microsoft.com committed Sep 23, 2008 630  dcRepTyCon = rep_tycon,  Simon Peyton Jones committed Dec 14, 2012 631 632 633 634 635 636  dcArgBangs = arg_stricts, dcFields = fields, dcTag = tag, dcRepType = rep_ty, dcWorkId = work_id, dcRep = rep, dcSourceArity = length orig_arg_tys, dcRepArity = length rep_arg_tys,  Simon Peyton Jones committed Oct 19, 2012 637  dcPromoted = mb_promoted }  simonm committed Dec 02, 1998 638   simonpj committed Feb 12, 2003 639 640 641  -- The 'arg_stricts' passed to mkDataCon are simply those for the -- source-language arguments. We add extra ones for the -- dictionary arguments right here.  simonm committed Dec 02, 1998 642   simonpj@microsoft.com committed Sep 23, 2008 643  tag = assoc "mkDataCon" (tyConDataCons rep_tycon zip [fIRST_TAG..]) con  Simon Peyton Jones committed Dec 14, 2012 644 645 646 647  rep_arg_tys = dataConRepArgTys con rep_ty = mkForAllTys univ_tvs$ mkForAllTys ex_tvs $mkFunTys rep_arg_tys$ mkTyConApp rep_tycon (mkTyVarTys univ_tvs)  simonpj committed Mar 23, 2000 648   Simon Peyton Jones committed Dec 05, 2012 649  mb_promoted -- See Note [Promoted data constructors] in TyCon  Simon Peyton Jones committed Jan 25, 2013 650 651 652  | isJust (promotableTyCon_maybe rep_tycon) -- The TyCon is promotable only if all its datacons -- are, so the promoteType for prom_kind should succeed  eir@cis.upenn.edu committed Aug 02, 2013 653  = Just (mkPromotedDataCon con name (getUnique name) prom_kind roles)  Simon Peyton Jones committed Oct 19, 2012 654 655 656  | otherwise = Nothing prom_kind = promoteType (dataConUserType con)  eir@cis.upenn.edu committed Aug 02, 2013 657 658  roles = map (const Nominal) (univ_tvs ++ ex_tvs) ++ map (const Representational) orig_arg_tys  Simon Peyton Jones committed Oct 19, 2012 659   chak@cse.unsw.edu.au. committed Sep 18, 2006 660 eqSpecPreds :: [(TyVar,Type)] -> ThetaType  Simon Peyton Jones committed Mar 02, 2012 661 eqSpecPreds spec = [ mkEqPred (mkTyVarTy tv) ty | (tv,ty) <- spec ]  simonm committed Dec 02, 1998 662 663 \end{code}  Simon Peyton Jones committed Sep 07, 2011 664 665 666 667 668 669 670 671 672 Note [Unpack equality predicates] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ If we have a GADT with a contructor C :: (a~[b]) => b -> T a we definitely want that equality predicate *unboxed* so that it takes no space at all. This is easily done: just give it an UNPACK pragma. The rest of the unpack/repack code does the heavy lifting. This one line makes every GADT take a word less space for each equality predicate, so it's pretty important!  simonm committed Dec 02, 1998 673 \begin{code}  batterseapower committed Jul 31, 2008 674 -- | The 'Name' of the 'DataCon', giving it a unique, rooted identification  simonm committed Dec 02, 1998 675 676 677 dataConName :: DataCon -> Name dataConName = dcName  batterseapower committed Jul 31, 2008 678 -- | The tag used for ordering 'DataCon's  simonm committed Dec 02, 1998 679 680 681 dataConTag :: DataCon -> ConTag dataConTag = dcTag  batterseapower committed Jul 31, 2008 682 -- | The type constructor that we are building via this data constructor  simonm committed Dec 02, 1998 683 dataConTyCon :: DataCon -> TyCon  simonpj@microsoft.com committed Apr 22, 2007 684 dataConTyCon = dcRepTyCon  simonm committed Dec 02, 1998 685   chak@cse.unsw.edu.au. committed Mar 25, 2009 686 687 688 689 690 691 692 693 -- | The original type constructor used in the definition of this data -- constructor. In case of a data family instance, that will be the family -- type constructor. dataConOrigTyCon :: DataCon -> TyCon dataConOrigTyCon dc | Just (tc, _) <- tyConFamInst_maybe (dcRepTyCon dc) = tc | otherwise = dcRepTyCon dc  batterseapower committed Jul 31, 2008 694 695 -- | The representation type of the data constructor, i.e. the sort -- type that will represent values of this type at runtime  simonpj committed Mar 23, 2000 696 697 dataConRepType :: DataCon -> Type dataConRepType = dcRepType  simonm committed Dec 02, 1998 698   batterseapower committed Jul 31, 2008 699 -- | Should the 'DataCon' be presented infix?  simonpj committed Jun 02, 2004 700 701 702 dataConIsInfix :: DataCon -> Bool dataConIsInfix = dcInfix  batterseapower committed Jul 31, 2008 703 -- | The universally-quantified type variables of the constructor  chak@cse.unsw.edu.au. committed Sep 18, 2006 704 705 706 dataConUnivTyVars :: DataCon -> [TyVar] dataConUnivTyVars = dcUnivTyVars  batterseapower committed Jul 31, 2008 707 -- | The existentially-quantified type variables of the constructor  chak@cse.unsw.edu.au. committed Sep 18, 2006 708 709 710 dataConExTyVars :: DataCon -> [TyVar] dataConExTyVars = dcExTyVars  batterseapower committed Jul 31, 2008 711 -- | Both the universal and existentiatial type variables of the constructor  chak@cse.unsw.edu.au. committed Sep 18, 2006 712 713 714 715 dataConAllTyVars :: DataCon -> [TyVar] dataConAllTyVars (MkData { dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs }) = univ_tvs ++ ex_tvs  batterseapower committed Jul 31, 2008 716 717 -- | Equalities derived from the result type of the data constructor, as written -- by the programmer in any GADT declaration  chak@cse.unsw.edu.au. committed Sep 18, 2006 718 719 720 dataConEqSpec :: DataCon -> [(TyVar,Type)] dataConEqSpec = dcEqSpec  721 722 723 724 -- | The *full* constraints on the constructor type dataConTheta :: DataCon -> ThetaType dataConTheta (MkData { dcEqSpec = eq_spec, dcOtherTheta = theta }) = eqSpecPreds eq_spec ++ theta  simonpj committed Sep 30, 2004 725   batterseapower committed Jul 31, 2008 726 727 728 729 -- | Get the Id of the 'DataCon' worker: a function that is the "actual" -- constructor and has no top level binding in the program. The type may -- be different from the obvious one written in the source program. Panics -- if there is no such 'Id' for this 'DataCon'  simonpj committed Apr 01, 2002 730 dataConWorkId :: DataCon -> Id  Simon Peyton Jones committed Dec 14, 2012 731 dataConWorkId dc = dcWorkId dc  simonm committed Dec 02, 1998 732   batterseapower committed Jul 31, 2008 733 734 735 736 -- | Get the Id of the 'DataCon' wrapper: a function that wraps the "actual" -- constructor so it has the type visible in the source program: c.f. 'dataConWorkId'. -- Returns Nothing if there is no wrapper, which occurs for an algebraic data constructor -- and also for a newtype (whose constructor is inlined compulsorily)  simonpj committed Feb 12, 2003 737 dataConWrapId_maybe :: DataCon -> Maybe Id  Simon Peyton Jones committed Dec 14, 2012 738 739 740 dataConWrapId_maybe dc = case dcRep dc of NoDataConRep -> Nothing DCR { dcr_wrap_id = wrap_id } -> Just wrap_id  simonpj committed Feb 12, 2003 741   batterseapower committed Jul 31, 2008 742 743 744 -- | Returns an Id which looks like the Haskell-source constructor by using -- the wrapper if it exists (see 'dataConWrapId_maybe') and failing over to -- the worker (see 'dataConWorkId')  simonpj committed Mar 23, 2000 745 dataConWrapId :: DataCon -> Id  Simon Peyton Jones committed Dec 14, 2012 746 747 748 dataConWrapId dc = case dcRep dc of NoDataConRep-> dcWorkId dc -- worker=wrapper DCR { dcr_wrap_id = wrap_id } -> wrap_id  simonpj committed Oct 09, 2003 749   batterseapower committed Jul 31, 2008 750 751 -- | Find all the 'Id's implicitly brought into scope by the data constructor. Currently, -- the union of the 'dataConWorkId' and the 'dataConWrapId'  simonpj committed Oct 09, 2003 752 dataConImplicitIds :: DataCon -> [Id]  Simon Peyton Jones committed Dec 14, 2012 753 754 755 756 dataConImplicitIds (MkData { dcWorkId = work, dcRep = rep}) = case rep of NoDataConRep -> [work] DCR { dcr_wrap_id = wrap } -> [wrap,work]  simonm committed Dec 02, 1998 757   batterseapower committed Jul 31, 2008 758 -- | The labels for the fields of this particular 'DataCon'  simonm committed Dec 02, 1998 759 760 761 dataConFieldLabels :: DataCon -> [FieldLabel] dataConFieldLabels = dcFields  batterseapower committed Jul 31, 2008 762 -- | Extract the type for any given labelled field of the 'DataCon'  simonmar committed Oct 06, 2005 763 dataConFieldType :: DataCon -> FieldLabel -> Type  simonpj@microsoft.com committed Jan 02, 2009 764 765 766 767 dataConFieldType con label = case lookup label (dcFields con zip dcOrigArgTys con) of Just ty -> ty Nothing -> pprPanic "dataConFieldType" (ppr con <+> ppr label)  simonmar committed Oct 06, 2005 768   batterseapower committed Jul 31, 2008 769 770 -- | The strictness markings decided on by the compiler. Does not include those for -- existential dictionaries. The list is in one-to-one correspondence with the arity of the 'DataCon'  simonpj@microsoft.com committed May 06, 2010 771 dataConStrictMarks :: DataCon -> [HsBang]  Simon Peyton Jones committed Dec 14, 2012 772 dataConStrictMarks = dcArgBangs  simonpj committed Mar 23, 2000 773   batterseapower committed Jul 31, 2008 774 -- | Source-level arity of the data constructor  simonm committed Dec 02, 1998 775 dataConSourceArity :: DataCon -> Arity  Simon Peyton Jones committed Dec 14, 2012 776 dataConSourceArity (MkData { dcSourceArity = arity }) = arity  simonm committed Dec 02, 1998 777   batterseapower committed Jul 31, 2008 778 779 780 -- | Gives the number of actual fields in the /representation/ of the -- data constructor. This may be more than appear in the source code; -- the extra ones are the existentially quantified dictionaries  batterseapower committed May 15, 2012 781 dataConRepArity :: DataCon -> Arity  Simon Peyton Jones committed Dec 14, 2012 782 783 dataConRepArity (MkData { dcRepArity = arity }) = arity  simonpj committed Mar 23, 2000 784   batterseapower committed May 15, 2012 785 786 787 788 789 -- | The number of fields in the /representation/ of the constructor -- AFTER taking into account the unpacking of any unboxed tuple fields dataConRepRepArity :: DataCon -> RepArity dataConRepRepArity dc = typeRepArity (dataConRepArity dc) (dataConRepType dc)  batterseapower committed Jul 31, 2008 790 791 -- | Return whether there are any argument types for this 'DataCon's original source type isNullarySrcDataCon :: DataCon -> Bool  simonpj committed Sep 30, 2004 792 isNullarySrcDataCon dc = null (dcOrigArgTys dc)  batterseapower committed Jul 31, 2008 793 794 795  -- | Return whether there are any argument types for this 'DataCon's runtime representation type isNullaryRepDataCon :: DataCon -> Bool  Simon Peyton Jones committed Dec 14, 2012 796 isNullaryRepDataCon dc = dataConRepArity dc == 0  simonpj committed Mar 23, 2000 797   simonpj committed Sep 14, 2001 798 dataConRepStrictness :: DataCon -> [StrictnessMark]  batterseapower committed Jul 31, 2008 799 800 -- ^ Give the demands on the arguments of a -- Core constructor application (Con dc args)  Simon Peyton Jones committed Dec 14, 2012 801 802 803 804 805 806 807 808 809 810 811 812 dataConRepStrictness dc = case dcRep dc of NoDataConRep -> [NotMarkedStrict | _ <- dataConRepArgTys dc] DCR { dcr_stricts = strs } -> strs dataConRepBangs :: DataCon -> [HsBang] dataConRepBangs dc = case dcRep dc of NoDataConRep -> dcArgBangs dc DCR { dcr_bangs = bangs } -> bangs dataConBoxer :: DataCon -> Maybe DataConBoxer dataConBoxer (MkData { dcRep = DCR { dcr_boxer = boxer } }) = Just boxer dataConBoxer _ = Nothing  simonpj committed May 18, 1999 813   batterseapower committed Jul 31, 2008 814 815 816 817 818 819 820 821 822 823 -- | The \"signature\" of the 'DataCon' returns, in order: -- -- 1) The result of 'dataConAllTyVars', -- -- 2) All the 'ThetaType's relating to the 'DataCon' (coercion, dictionary, implicit -- parameter - whatever) -- -- 3) The type arguments to the constructor -- -- 4) The /original/ result type of the 'DataCon'  simonpj@microsoft.com committed Apr 22, 2007 824 dataConSig :: DataCon -> ([TyVar], ThetaType, [Type], Type)  825 826 dataConSig (MkData {dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs, dcEqSpec = eq_spec, dcOtherTheta = theta,  simonpj@microsoft.com committed Sep 23, 2008 827  dcOrigArgTys = arg_tys, dcOrigResTy = res_ty})  828  = (univ_tvs ++ ex_tvs, eqSpecPreds eq_spec ++ theta, arg_tys, res_ty)  simonm committed Dec 02, 1998 829   batterseapower committed Jul 31, 2008 830 831 832 833 834 835 836 837 838 839 -- | The \"full signature\" of the 'DataCon' returns, in order: -- -- 1) The result of 'dataConUnivTyVars' -- -- 2) The result of 'dataConExTyVars' -- -- 3) The result of 'dataConEqSpec' -- -- 4) The result of 'dataConDictTheta' --  simonpj@microsoft.com committed Sep 23, 2008 840 841 -- 5) The original argument types to the 'DataCon' (i.e. before -- any change of the representation of the type)  batterseapower committed Jul 31, 2008 842 843 -- -- 6) The original result type of the 'DataCon'  chak@cse.unsw.edu.au. committed Sep 18, 2006 844 dataConFullSig :: DataCon  845 846 847  -> ([TyVar], [TyVar], [(TyVar,Type)], ThetaType, [Type], Type) dataConFullSig (MkData {dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs, dcEqSpec = eq_spec, dcOtherTheta = theta,  simonpj@microsoft.com committed Sep 23, 2008 848  dcOrigArgTys = arg_tys, dcOrigResTy = res_ty})  849  = (univ_tvs, ex_tvs, eq_spec, theta, arg_tys, res_ty)  simonpj@microsoft.com committed Apr 22, 2007 850 851 852  dataConOrigResTy :: DataCon -> Type dataConOrigResTy dc = dcOrigResTy dc  simonm committed Dec 02, 1998 853   batterseapower committed Jul 31, 2008 854 855 856 -- | The \"stupid theta\" of the 'DataCon', such as @data Eq a@ in: -- -- > data Eq a => T a = ...  simonpj committed Oct 14, 2005 857 858 859 dataConStupidTheta :: DataCon -> ThetaType dataConStupidTheta dc = dcStupidTheta dc  chak@cse.unsw.edu.au. committed Sep 18, 2006 860 dataConUserType :: DataCon -> Type  batterseapower committed Jul 31, 2008 861 862 863 864 865 866 867 -- ^ The user-declared type of the data constructor -- in the nice-to-read form: -- -- > T :: forall a b. a -> b -> T [a] -- -- rather than: --  simonpj@microsoft.com committed Jan 02, 2009 868 -- > T :: forall a c. forall b. (c~[a]) => a -> b -> T c  batterseapower committed Jul 31, 2008 869 --  chak@cse.unsw.edu.au. committed Sep 20, 2006 870 871 -- NB: If the constructor is part of a data instance, the result type -- mentions the family tycon, not the internal one.  chak@cse.unsw.edu.au. committed Sep 18, 2006 872 873 dataConUserType (MkData { dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs, dcEqSpec = eq_spec,  874  dcOtherTheta = theta, dcOrigArgTys = arg_tys,  simonpj@microsoft.com committed Apr 22, 2007 875  dcOrigResTy = res_ty })  chak@cse.unsw.edu.au. committed Sep 18, 2006 876  = mkForAllTys ((univ_tvs minusList map fst eq_spec) ++ ex_tvs) $ batterseapower committed Sep 06, 2011 877  mkFunTys theta$  chak@cse.unsw.edu.au. committed Sep 18, 2006 878  mkFunTys arg_tys \$  simonpj@microsoft.com committed Apr 22, 2007 879  res_ty  simonpj committed Oct 14, 2005 880   batterseapower committed Jul 31, 2008 881 882 883 884 885 -- | Finds the instantiated types of the arguments required to construct a 'DataCon' representation -- NB: these INCLUDE any dictionary args -- but EXCLUDE the data-declaration context, which is discarded -- It's all post-flattening etc; this is a representation type dataConInstArgTys :: DataCon -- ^ A datacon with no existentials or equality constraints  David Himmelstrup committed Jun 07, 2007 886 887  -- However, it can have a dcTheta (notably it can be a -- class dictionary, with superclasses)  batterseapower committed Jul 31, 2008 888 889  -> [Type] -- ^ Instantiated at these types -> [Type]  Simon Peyton Jones committed Dec 14, 2012 890 dataConInstArgTys dc@(MkData {dcUnivTyVars = univ_tvs, dcEqSpec = eq_spec,  mnislaih committed Apr 19, 2007 891  dcExTyVars = ex_tvs}) inst_tys  thoughtpolice committed Jun 18, 2013 892 893 894  = ASSERT2( length univ_tvs == length inst_tys , ptext (sLit "dataConInstArgTys") <+> ppr dc $$ppr univ_tvs$$ ppr inst_tys) ASSERT2( null ex_tvs && null eq_spec, ppr dc )  Simon Peyton Jones committed Dec 14, 2012 895  map (substTyWith univ_tvs inst_tys) (dataConRepArgTys dc)  David Himmelstrup committed Jun 07, 2007 896   simonpj@microsoft.com committed Sep 10, 2008 897 898 -- | Returns just the instantiated /value/ argument types of a 'DataCon', -- (excluding dictionary args)  David Himmelstrup committed Jun 07, 2007 899 900 901 902 dataConInstOrigArgTys :: DataCon -- Works for any DataCon -> [Type] -- Includes existential tyvar args, but NOT -- equality constraints or dicts  batterseapower committed Jul 31, 2008 903  -> [Type]  David Himmelstrup committed Jun 07, 2007 904 905 -- For vanilla datacons, it's all quite straightforward -- But for the call in MatchCon, we really do want just the value args  chak@cse.unsw.edu.au. committed Sep 18, 2006 906 dataConInstOrigArgTys dc@(MkData {dcOrigArgTys = arg_tys,  mnislaih committed Apr 19, 2007 907 908  dcUnivTyVars = univ_tvs, dcExTyVars = ex_tvs}) inst_tys  David Himmelstrup committed Jun 07, 2007 909  = ASSERT2( length tyvars == length inst_tys  Ian Lynagh committed Apr 12, 2008 910  , ptext (sLit "dataConInstOrigArgTys") <+> ppr dc $$ppr tyvars$$ ppr inst_tys )  David Himmelstrup committed Jun 07, 2007 911 912 913  map (substTyWith tyvars inst_tys) arg_tys where tyvars = univ_tvs ++ ex_tvs  simonm committed Dec 02, 1998 914 915 \end{code}  simonmar committed Jun 07, 1999 916 \begin{code}  batterseapower committed Jul 31, 2008 917 918 -- | Returns the argument types of the wrapper, excluding all dictionary arguments -- and without substituting for any type variables  simonpj committed Dec 07, 1999 919 920 921 dataConOrigArgTys :: DataCon -> [Type] dataConOrigArgTys dc = dcOrigArgTys dc  Simon Peyton Jones committed Dec 14, 2012 922 -- | Returns the arg types of the worker, including *all* evidence, after any  batterseapower committed Jul 31, 2008 923 -- flattening has been done and without substituting for any type variables  simonpj committed Nov 26, 2001 924 dataConRepArgTys :: DataCon -> [Type]  Simon Peyton Jones committed Dec 14, 2012 925 926 927 928 929 930 931 dataConRepArgTys (MkData { dcRep = rep , dcEqSpec = eq_spec , dcOtherTheta = theta , dcOrigArgTys = orig_arg_tys }) = case rep of NoDataConRep -> ASSERT( null eq_spec ) theta ++ orig_arg_tys DCR { dcr_arg_tys = arg_tys } -> arg_tys  simonmar committed Jun 07, 1999 932 933 \end{code}  Simon Marlow committed May 09, 2007 934 \begin{code}  batterseapower committed Jul 31, 2008 935 936 -- | The string @package:module.name@ identifying a constructor, which is attached -- to its info table and used by the GHCi debugger and the heap profiler  Simon Marlow committed May 09, 2007 937 dataConIdentity :: DataCon -> [Word8]  batterseapower committed Jul 31, 2008 938 -- We want this string to be UTF-8, so we get the bytes directly from the FastStrings.  Simon Marlow committed May 09, 2007 939 940 941 942 dataConIdentity dc = bytesFS (packageIdFS (modulePackageId mod)) ++ fromIntegral (ord ':') : bytesFS (moduleNameFS (moduleName mod)) ++ fromIntegral (ord '.') : bytesFS (occNameFS (nameOccName name)) where name = dataConName dc  simonpj@microsoft.com committed Oct 03, 2008 943  mod = ASSERT( isExternalName name ) nameModule name  Simon Marlow committed May 09, 2007 944 945 \end{code}  simonm committed Dec 02, 1998 946 \begin{code}  Simon Peyton Jones committed Aug 31, 2012 947 948 isTupleDataCon :: DataCon -> Bool isTupleDataCon (MkData {dcRepTyCon = tc}) = isTupleTyCon tc  simonm committed Dec 02, 1998 949 950  isUnboxedTupleCon :: DataCon -> Bool  simonpj@microsoft.com committed Apr 22, 2007 951 isUnboxedTupleCon (MkData {dcRepTyCon = tc}) = isUnboxedTupleTyCon tc  simonm committed Dec 02, 1998 952   batterseapower committed Jul 31, 2008 953 -- | Vanilla 'DataCon's are those that are nice boring Haskell 98 constructors  simonpj committed Sep 30, 2004 954 955 isVanillaDataCon :: DataCon -> Bool isVanillaDataCon dc = dcVanilla dc  simonm committed Dec 02, 1998 956 \end{code}  simonpj committed Jul 14, 1999 957   simonpj committed Nov 03, 2000 958 959 960 961 \begin{code} classDataCon :: Class -> DataCon classDataCon clas = case tyConDataCons (classTyCon clas) of (dict_constr:no_more) -> ASSERT( null no_more ) dict_constr  simonpj@microsoft.com committed Apr 22, 2007 962  [] -> panic "classDataCon"  simonpj committed Nov 03, 2000