| 1 | % |
|---|
| 2 | % (c) The University of Glasgow 2006 |
|---|
| 3 | % (c) The AQUA Project, Glasgow University, 1998 |
|---|
| 4 | % |
|---|
| 5 | |
|---|
| 6 | This module contains definitions for the IdInfo for things that |
|---|
| 7 | have a standard form, namely: |
|---|
| 8 | |
|---|
| 9 | - data constructors |
|---|
| 10 | - record selectors |
|---|
| 11 | - method and superclass selectors |
|---|
| 12 | - primitive operations |
|---|
| 13 | |
|---|
| 14 | \begin{code} |
|---|
| 15 | {-# OPTIONS -fno-warn-tabs #-} |
|---|
| 16 | -- The above warning supression flag is a temporary kludge. |
|---|
| 17 | -- While working on this module you are encouraged to remove it and |
|---|
| 18 | -- detab the module (please do the detabbing in a separate patch). See |
|---|
| 19 | -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces |
|---|
| 20 | -- for details |
|---|
| 21 | |
|---|
| 22 | module MkId ( |
|---|
| 23 | mkDictFunId, mkDictFunTy, mkDictSelId, |
|---|
| 24 | |
|---|
| 25 | mkDataConIds, mkPrimOpId, mkFCallId, |
|---|
| 26 | |
|---|
| 27 | mkReboxingAlt, wrapNewTypeBody, unwrapNewTypeBody, |
|---|
| 28 | wrapFamInstBody, unwrapFamInstScrut, |
|---|
| 29 | wrapTypeFamInstBody, unwrapTypeFamInstScrut, |
|---|
| 30 | mkUnpackCase, mkProductBox, |
|---|
| 31 | |
|---|
| 32 | -- And some particular Ids; see below for why they are wired in |
|---|
| 33 | wiredInIds, ghcPrimIds, |
|---|
| 34 | unsafeCoerceName, unsafeCoerceId, realWorldPrimId, |
|---|
| 35 | voidArgId, nullAddrId, seqId, lazyId, lazyIdKey, |
|---|
| 36 | coercionTokenId, |
|---|
| 37 | |
|---|
| 38 | -- Re-export error Ids |
|---|
| 39 | module PrelRules |
|---|
| 40 | ) where |
|---|
| 41 | |
|---|
| 42 | #include "HsVersions.h" |
|---|
| 43 | |
|---|
| 44 | import Rules |
|---|
| 45 | import TysPrim |
|---|
| 46 | import TysWiredIn |
|---|
| 47 | import PrelRules |
|---|
| 48 | import Type |
|---|
| 49 | import Coercion |
|---|
| 50 | import TcType |
|---|
| 51 | import MkCore |
|---|
| 52 | import CoreUtils ( exprType, mkCast ) |
|---|
| 53 | import CoreUnfold |
|---|
| 54 | import Literal |
|---|
| 55 | import TyCon |
|---|
| 56 | import Class |
|---|
| 57 | import VarSet |
|---|
| 58 | import Name |
|---|
| 59 | import PrimOp |
|---|
| 60 | import ForeignCall |
|---|
| 61 | import DataCon |
|---|
| 62 | import Id |
|---|
| 63 | import Var ( mkExportedLocalVar ) |
|---|
| 64 | import IdInfo |
|---|
| 65 | import Demand |
|---|
| 66 | import CoreSyn |
|---|
| 67 | import Unique |
|---|
| 68 | import PrelNames |
|---|
| 69 | import BasicTypes hiding ( SuccessFlag(..) ) |
|---|
| 70 | import Util |
|---|
| 71 | import Pair |
|---|
| 72 | import Outputable |
|---|
| 73 | import FastString |
|---|
| 74 | import ListSetOps |
|---|
| 75 | \end{code} |
|---|
| 76 | |
|---|
| 77 | %************************************************************************ |
|---|
| 78 | %* * |
|---|
| 79 | \subsection{Wired in Ids} |
|---|
| 80 | %* * |
|---|
| 81 | %************************************************************************ |
|---|
| 82 | |
|---|
| 83 | Note [Wired-in Ids] |
|---|
| 84 | ~~~~~~~~~~~~~~~~~~~ |
|---|
| 85 | There are several reasons why an Id might appear in the wiredInIds: |
|---|
| 86 | |
|---|
| 87 | (1) The ghcPrimIds are wired in because they can't be defined in |
|---|
| 88 | Haskell at all, although the can be defined in Core. They have |
|---|
| 89 | compulsory unfoldings, so they are always inlined and they have |
|---|
| 90 | no definition site. Their home module is GHC.Prim, so they |
|---|
| 91 | also have a description in primops.txt.pp, where they are called |
|---|
| 92 | 'pseudoops'. |
|---|
| 93 | |
|---|
| 94 | (2) The 'error' function, eRROR_ID, is wired in because we don't yet have |
|---|
| 95 | a way to express in an interface file that the result type variable |
|---|
| 96 | is 'open'; that is can be unified with an unboxed type |
|---|
| 97 | |
|---|
| 98 | [The interface file format now carry such information, but there's |
|---|
| 99 | no way yet of expressing at the definition site for these |
|---|
| 100 | error-reporting functions that they have an 'open' |
|---|
| 101 | result type. -- sof 1/99] |
|---|
| 102 | |
|---|
| 103 | (3) Other error functions (rUNTIME_ERROR_ID) are wired in (a) because |
|---|
| 104 | the desugarer generates code that mentiones them directly, and |
|---|
| 105 | (b) for the same reason as eRROR_ID |
|---|
| 106 | |
|---|
| 107 | (4) lazyId is wired in because the wired-in version overrides the |
|---|
| 108 | strictness of the version defined in GHC.Base |
|---|
| 109 | |
|---|
| 110 | In cases (2-4), the function has a definition in a library module, and |
|---|
| 111 | can be called; but the wired-in version means that the details are |
|---|
| 112 | never read from that module's interface file; instead, the full definition |
|---|
| 113 | is right here. |
|---|
| 114 | |
|---|
| 115 | \begin{code} |
|---|
| 116 | wiredInIds :: [Id] |
|---|
| 117 | wiredInIds |
|---|
| 118 | = [lazyId] |
|---|
| 119 | ++ errorIds -- Defined in MkCore |
|---|
| 120 | ++ ghcPrimIds |
|---|
| 121 | |
|---|
| 122 | -- These Ids are exported from GHC.Prim |
|---|
| 123 | ghcPrimIds :: [Id] |
|---|
| 124 | ghcPrimIds |
|---|
| 125 | = [ -- These can't be defined in Haskell, but they have |
|---|
| 126 | -- perfectly reasonable unfoldings in Core |
|---|
| 127 | realWorldPrimId, |
|---|
| 128 | unsafeCoerceId, |
|---|
| 129 | nullAddrId, |
|---|
| 130 | seqId |
|---|
| 131 | ] |
|---|
| 132 | \end{code} |
|---|
| 133 | |
|---|
| 134 | %************************************************************************ |
|---|
| 135 | %* * |
|---|
| 136 | \subsection{Data constructors} |
|---|
| 137 | %* * |
|---|
| 138 | %************************************************************************ |
|---|
| 139 | |
|---|
| 140 | The wrapper for a constructor is an ordinary top-level binding that evaluates |
|---|
| 141 | any strict args, unboxes any args that are going to be flattened, and calls |
|---|
| 142 | the worker. |
|---|
| 143 | |
|---|
| 144 | We're going to build a constructor that looks like: |
|---|
| 145 | |
|---|
| 146 | data (Data a, C b) => T a b = T1 !a !Int b |
|---|
| 147 | |
|---|
| 148 | T1 = /\ a b -> |
|---|
| 149 | \d1::Data a, d2::C b -> |
|---|
| 150 | \p q r -> case p of { p -> |
|---|
| 151 | case q of { q -> |
|---|
| 152 | Con T1 [a,b] [p,q,r]}} |
|---|
| 153 | |
|---|
| 154 | Notice that |
|---|
| 155 | |
|---|
| 156 | * d2 is thrown away --- a context in a data decl is used to make sure |
|---|
| 157 | one *could* construct dictionaries at the site the constructor |
|---|
| 158 | is used, but the dictionary isn't actually used. |
|---|
| 159 | |
|---|
| 160 | * We have to check that we can construct Data dictionaries for |
|---|
| 161 | the types a and Int. Once we've done that we can throw d1 away too. |
|---|
| 162 | |
|---|
| 163 | * We use (case p of q -> ...) to evaluate p, rather than "seq" because |
|---|
| 164 | all that matters is that the arguments are evaluated. "seq" is |
|---|
| 165 | very careful to preserve evaluation order, which we don't need |
|---|
| 166 | to be here. |
|---|
| 167 | |
|---|
| 168 | You might think that we could simply give constructors some strictness |
|---|
| 169 | info, like PrimOps, and let CoreToStg do the let-to-case transformation. |
|---|
| 170 | But we don't do that because in the case of primops and functions strictness |
|---|
| 171 | is a *property* not a *requirement*. In the case of constructors we need to |
|---|
| 172 | do something active to evaluate the argument. |
|---|
| 173 | |
|---|
| 174 | Making an explicit case expression allows the simplifier to eliminate |
|---|
| 175 | it in the (common) case where the constructor arg is already evaluated. |
|---|
| 176 | |
|---|
| 177 | Note [Wrappers for data instance tycons] |
|---|
| 178 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 179 | In the case of data instances, the wrapper also applies the coercion turning |
|---|
| 180 | the representation type into the family instance type to cast the result of |
|---|
| 181 | the wrapper. For example, consider the declarations |
|---|
| 182 | |
|---|
| 183 | data family Map k :: * -> * |
|---|
| 184 | data instance Map (a, b) v = MapPair (Map a (Pair b v)) |
|---|
| 185 | |
|---|
| 186 | The tycon to which the datacon MapPair belongs gets a unique internal |
|---|
| 187 | name of the form :R123Map, and we call it the representation tycon. |
|---|
| 188 | In contrast, Map is the family tycon (accessible via |
|---|
| 189 | tyConFamInst_maybe). A coercion allows you to move between |
|---|
| 190 | representation and family type. It is accessible from :R123Map via |
|---|
| 191 | tyConFamilyCoercion_maybe and has kind |
|---|
| 192 | |
|---|
| 193 | Co123Map a b v :: {Map (a, b) v ~ :R123Map a b v} |
|---|
| 194 | |
|---|
| 195 | The wrapper and worker of MapPair get the types |
|---|
| 196 | |
|---|
| 197 | -- Wrapper |
|---|
| 198 | $WMapPair :: forall a b v. Map a (Map a b v) -> Map (a, b) v |
|---|
| 199 | $WMapPair a b v = MapPair a b v `cast` sym (Co123Map a b v) |
|---|
| 200 | |
|---|
| 201 | -- Worker |
|---|
| 202 | MapPair :: forall a b v. Map a (Map a b v) -> :R123Map a b v |
|---|
| 203 | |
|---|
| 204 | This coercion is conditionally applied by wrapFamInstBody. |
|---|
| 205 | |
|---|
| 206 | It's a bit more complicated if the data instance is a GADT as well! |
|---|
| 207 | |
|---|
| 208 | data instance T [a] where |
|---|
| 209 | T1 :: forall b. b -> T [Maybe b] |
|---|
| 210 | |
|---|
| 211 | Hence we translate to |
|---|
| 212 | |
|---|
| 213 | -- Wrapper |
|---|
| 214 | $WT1 :: forall b. b -> T [Maybe b] |
|---|
| 215 | $WT1 b v = T1 (Maybe b) b (Maybe b) v |
|---|
| 216 | `cast` sym (Co7T (Maybe b)) |
|---|
| 217 | |
|---|
| 218 | -- Worker |
|---|
| 219 | T1 :: forall c b. (c ~ Maybe b) => b -> :R7T c |
|---|
| 220 | |
|---|
| 221 | -- Coercion from family type to representation type |
|---|
| 222 | Co7T a :: T [a] ~ :R7T a |
|---|
| 223 | |
|---|
| 224 | \begin{code} |
|---|
| 225 | mkDataConIds :: Name -> Name -> DataCon -> DataConIds |
|---|
| 226 | mkDataConIds wrap_name wkr_name data_con |
|---|
| 227 | | isNewTyCon tycon -- Newtype, only has a worker |
|---|
| 228 | = DCIds Nothing nt_work_id |
|---|
| 229 | |
|---|
| 230 | | any isBanged all_strict_marks -- Algebraic, needs wrapper |
|---|
| 231 | || not (null eq_spec) -- NB: LoadIface.ifaceDeclImplicitBndrs |
|---|
| 232 | || isFamInstTyCon tycon -- depends on this test |
|---|
| 233 | = DCIds (Just alg_wrap_id) wrk_id |
|---|
| 234 | |
|---|
| 235 | | otherwise -- Algebraic, no wrapper |
|---|
| 236 | = DCIds Nothing wrk_id |
|---|
| 237 | where |
|---|
| 238 | (univ_tvs, ex_tvs, eq_spec, |
|---|
| 239 | other_theta, orig_arg_tys, res_ty) = dataConFullSig data_con |
|---|
| 240 | tycon = dataConTyCon data_con -- The representation TyCon (not family) |
|---|
| 241 | |
|---|
| 242 | ----------- Worker (algebraic data types only) -------------- |
|---|
| 243 | -- The *worker* for the data constructor is the function that |
|---|
| 244 | -- takes the representation arguments and builds the constructor. |
|---|
| 245 | wrk_id = mkGlobalId (DataConWorkId data_con) wkr_name |
|---|
| 246 | (dataConRepType data_con) wkr_info |
|---|
| 247 | |
|---|
| 248 | wkr_arity = dataConRepArity data_con |
|---|
| 249 | wkr_info = noCafIdInfo |
|---|
| 250 | `setArityInfo` wkr_arity |
|---|
| 251 | `setStrictnessInfo` Just wkr_sig |
|---|
| 252 | `setUnfoldingInfo` evaldUnfolding -- Record that it's evaluated, |
|---|
| 253 | -- even if arity = 0 |
|---|
| 254 | |
|---|
| 255 | wkr_sig = mkStrictSig (mkTopDmdType (replicate wkr_arity topDmd) cpr_info) |
|---|
| 256 | -- Note [Data-con worker strictness] |
|---|
| 257 | -- Notice that we do *not* say the worker is strict |
|---|
| 258 | -- even if the data constructor is declared strict |
|---|
| 259 | -- e.g. data T = MkT !(Int,Int) |
|---|
| 260 | -- Why? Because the *wrapper* is strict (and its unfolding has case |
|---|
| 261 | -- expresssions that do the evals) but the *worker* itself is not. |
|---|
| 262 | -- If we pretend it is strict then when we see |
|---|
| 263 | -- case x of y -> $wMkT y |
|---|
| 264 | -- the simplifier thinks that y is "sure to be evaluated" (because |
|---|
| 265 | -- $wMkT is strict) and drops the case. No, $wMkT is not strict. |
|---|
| 266 | -- |
|---|
| 267 | -- When the simplifer sees a pattern |
|---|
| 268 | -- case e of MkT x -> ... |
|---|
| 269 | -- it uses the dataConRepStrictness of MkT to mark x as evaluated; |
|---|
| 270 | -- but that's fine... dataConRepStrictness comes from the data con |
|---|
| 271 | -- not from the worker Id. |
|---|
| 272 | |
|---|
| 273 | cpr_info | isProductTyCon tycon && |
|---|
| 274 | isDataTyCon tycon && |
|---|
| 275 | wkr_arity > 0 && |
|---|
| 276 | wkr_arity <= mAX_CPR_SIZE = retCPR |
|---|
| 277 | | otherwise = TopRes |
|---|
| 278 | -- RetCPR is only true for products that are real data types; |
|---|
| 279 | -- that is, not unboxed tuples or [non-recursive] newtypes |
|---|
| 280 | |
|---|
| 281 | ----------- Workers for newtypes -------------- |
|---|
| 282 | nt_work_id = mkGlobalId (DataConWrapId data_con) wkr_name wrap_ty nt_work_info |
|---|
| 283 | nt_work_info = noCafIdInfo -- The NoCaf-ness is set by noCafIdInfo |
|---|
| 284 | `setArityInfo` 1 -- Arity 1 |
|---|
| 285 | `setInlinePragInfo` alwaysInlinePragma |
|---|
| 286 | `setUnfoldingInfo` newtype_unf |
|---|
| 287 | id_arg1 = mkTemplateLocal 1 (head orig_arg_tys) |
|---|
| 288 | newtype_unf = ASSERT2( isVanillaDataCon data_con && |
|---|
| 289 | isSingleton orig_arg_tys, ppr data_con ) |
|---|
| 290 | -- Note [Newtype datacons] |
|---|
| 291 | mkCompulsoryUnfolding $ |
|---|
| 292 | mkLams wrap_tvs $ Lam id_arg1 $ |
|---|
| 293 | wrapNewTypeBody tycon res_ty_args (Var id_arg1) |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | ----------- Wrapper -------------- |
|---|
| 297 | -- We used to include the stupid theta in the wrapper's args |
|---|
| 298 | -- but now we don't. Instead the type checker just injects these |
|---|
| 299 | -- extra constraints where necessary. |
|---|
| 300 | wrap_tvs = (univ_tvs `minusList` map fst eq_spec) ++ ex_tvs |
|---|
| 301 | res_ty_args = substTyVars (mkTopTvSubst eq_spec) univ_tvs |
|---|
| 302 | ev_tys = other_theta |
|---|
| 303 | wrap_ty = mkForAllTys wrap_tvs $ |
|---|
| 304 | mkFunTys ev_tys $ |
|---|
| 305 | mkFunTys orig_arg_tys $ res_ty |
|---|
| 306 | |
|---|
| 307 | ----------- Wrappers for algebraic data types -------------- |
|---|
| 308 | alg_wrap_id = mkGlobalId (DataConWrapId data_con) wrap_name wrap_ty alg_wrap_info |
|---|
| 309 | alg_wrap_info = noCafIdInfo |
|---|
| 310 | `setArityInfo` wrap_arity |
|---|
| 311 | -- It's important to specify the arity, so that partial |
|---|
| 312 | -- applications are treated as values |
|---|
| 313 | `setInlinePragInfo` alwaysInlinePragma |
|---|
| 314 | `setUnfoldingInfo` wrap_unf |
|---|
| 315 | `setStrictnessInfo` Just wrap_sig |
|---|
| 316 | -- We need to get the CAF info right here because TidyPgm |
|---|
| 317 | -- does not tidy the IdInfo of implicit bindings (like the wrapper) |
|---|
| 318 | -- so it not make sure that the CAF info is sane |
|---|
| 319 | |
|---|
| 320 | all_strict_marks = dataConExStricts data_con ++ dataConStrictMarks data_con |
|---|
| 321 | wrap_sig = mkStrictSig (mkTopDmdType wrap_arg_dmds cpr_info) |
|---|
| 322 | wrap_stricts = dropList eq_spec all_strict_marks |
|---|
| 323 | wrap_arg_dmds = map mk_dmd wrap_stricts |
|---|
| 324 | mk_dmd str | isBanged str = evalDmd |
|---|
| 325 | | otherwise = lazyDmd |
|---|
| 326 | -- The Cpr info can be important inside INLINE rhss, where the |
|---|
| 327 | -- wrapper constructor isn't inlined. |
|---|
| 328 | -- And the argument strictness can be important too; we |
|---|
| 329 | -- may not inline a contructor when it is partially applied. |
|---|
| 330 | -- For example: |
|---|
| 331 | -- data W = C !Int !Int !Int |
|---|
| 332 | -- ...(let w = C x in ...(w p q)...)... |
|---|
| 333 | -- we want to see that w is strict in its two arguments |
|---|
| 334 | |
|---|
| 335 | wrap_unf = mkInlineUnfolding (Just (length ev_args + length id_args)) wrap_rhs |
|---|
| 336 | wrap_rhs = mkLams wrap_tvs $ |
|---|
| 337 | mkLams ev_args $ |
|---|
| 338 | mkLams id_args $ |
|---|
| 339 | foldr mk_case con_app |
|---|
| 340 | (zip (ev_args ++ id_args) wrap_stricts) |
|---|
| 341 | i3 [] |
|---|
| 342 | -- The ev_args is the evidence arguments *other than* the eq_spec |
|---|
| 343 | -- Because we are going to apply the eq_spec args manually in the |
|---|
| 344 | -- wrapper |
|---|
| 345 | |
|---|
| 346 | con_app _ rep_ids = wrapFamInstBody tycon res_ty_args $ |
|---|
| 347 | Var wrk_id `mkTyApps` res_ty_args |
|---|
| 348 | `mkVarApps` ex_tvs |
|---|
| 349 | `mkCoApps` map (mkReflCo . snd) eq_spec |
|---|
| 350 | `mkVarApps` reverse rep_ids |
|---|
| 351 | -- Dont box the eq_spec coercions since they are |
|---|
| 352 | -- marked as HsUnpack by mk_dict_strict_mark |
|---|
| 353 | |
|---|
| 354 | (ev_args,i2) = mkLocals 1 ev_tys |
|---|
| 355 | (id_args,i3) = mkLocals i2 orig_arg_tys |
|---|
| 356 | wrap_arity = i3-1 |
|---|
| 357 | |
|---|
| 358 | mk_case |
|---|
| 359 | :: (Id, HsBang) -- Arg, strictness |
|---|
| 360 | -> (Int -> [Id] -> CoreExpr) -- Body |
|---|
| 361 | -> Int -- Next rep arg id |
|---|
| 362 | -> [Id] -- Rep args so far, reversed |
|---|
| 363 | -> CoreExpr |
|---|
| 364 | mk_case (arg,strict) body i rep_args |
|---|
| 365 | = case strict of |
|---|
| 366 | HsNoBang -> body i (arg:rep_args) |
|---|
| 367 | HsUnpack -> unboxProduct i (Var arg) (idType arg) the_body |
|---|
| 368 | where |
|---|
| 369 | the_body i con_args = body i (reverse con_args ++ rep_args) |
|---|
| 370 | _other -- HsUnpackFailed and HsStrict |
|---|
| 371 | | isUnLiftedType (idType arg) -> body i (arg:rep_args) |
|---|
| 372 | | otherwise -> Case (Var arg) arg res_ty |
|---|
| 373 | [(DEFAULT,[], body i (arg:rep_args))] |
|---|
| 374 | |
|---|
| 375 | mAX_CPR_SIZE :: Arity |
|---|
| 376 | mAX_CPR_SIZE = 10 |
|---|
| 377 | -- We do not treat very big tuples as CPR-ish: |
|---|
| 378 | -- a) for a start we get into trouble because there aren't |
|---|
| 379 | -- "enough" unboxed tuple types (a tiresome restriction, |
|---|
| 380 | -- but hard to fix), |
|---|
| 381 | -- b) more importantly, big unboxed tuples get returned mainly |
|---|
| 382 | -- on the stack, and are often then allocated in the heap |
|---|
| 383 | -- by the caller. So doing CPR for them may in fact make |
|---|
| 384 | -- things worse. |
|---|
| 385 | |
|---|
| 386 | mkLocals :: Int -> [Type] -> ([Id], Int) |
|---|
| 387 | mkLocals i tys = (zipWith mkTemplateLocal [i..i+n-1] tys, i+n) |
|---|
| 388 | where |
|---|
| 389 | n = length tys |
|---|
| 390 | \end{code} |
|---|
| 391 | |
|---|
| 392 | Note [Newtype datacons] |
|---|
| 393 | ~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 394 | The "data constructor" for a newtype should always be vanilla. At one |
|---|
| 395 | point this wasn't true, because the newtype arising from |
|---|
| 396 | class C a => D a |
|---|
| 397 | looked like |
|---|
| 398 | newtype T:D a = D:D (C a) |
|---|
| 399 | so the data constructor for T:C had a single argument, namely the |
|---|
| 400 | predicate (C a). But now we treat that as an ordinary argument, not |
|---|
| 401 | part of the theta-type, so all is well. |
|---|
| 402 | |
|---|
| 403 | |
|---|
| 404 | %************************************************************************ |
|---|
| 405 | %* * |
|---|
| 406 | \subsection{Dictionary selectors} |
|---|
| 407 | %* * |
|---|
| 408 | %************************************************************************ |
|---|
| 409 | |
|---|
| 410 | Selecting a field for a dictionary. If there is just one field, then |
|---|
| 411 | there's nothing to do. |
|---|
| 412 | |
|---|
| 413 | Dictionary selectors may get nested forall-types. Thus: |
|---|
| 414 | |
|---|
| 415 | class Foo a where |
|---|
| 416 | op :: forall b. Ord b => a -> b -> b |
|---|
| 417 | |
|---|
| 418 | Then the top-level type for op is |
|---|
| 419 | |
|---|
| 420 | op :: forall a. Foo a => |
|---|
| 421 | forall b. Ord b => |
|---|
| 422 | a -> b -> b |
|---|
| 423 | |
|---|
| 424 | This is unlike ordinary record selectors, which have all the for-alls |
|---|
| 425 | at the outside. When dealing with classes it's very convenient to |
|---|
| 426 | recover the original type signature from the class op selector. |
|---|
| 427 | |
|---|
| 428 | \begin{code} |
|---|
| 429 | mkDictSelId :: Bool -- True <=> don't include the unfolding |
|---|
| 430 | -- Little point on imports without -O, because the |
|---|
| 431 | -- dictionary itself won't be visible |
|---|
| 432 | -> Name -- Name of one of the *value* selectors |
|---|
| 433 | -- (dictionary superclass or method) |
|---|
| 434 | -> Class -> Id |
|---|
| 435 | mkDictSelId no_unf name clas |
|---|
| 436 | = mkGlobalId (ClassOpId clas) name sel_ty info |
|---|
| 437 | where |
|---|
| 438 | sel_ty = mkForAllTys tyvars (mkFunTy (idType dict_id) (idType the_arg_id)) |
|---|
| 439 | -- We can't just say (exprType rhs), because that would give a type |
|---|
| 440 | -- C a -> C a |
|---|
| 441 | -- for a single-op class (after all, the selector is the identity) |
|---|
| 442 | -- But it's type must expose the representation of the dictionary |
|---|
| 443 | -- to get (say) C a -> (a -> a) |
|---|
| 444 | |
|---|
| 445 | base_info = noCafIdInfo |
|---|
| 446 | `setArityInfo` 1 |
|---|
| 447 | `setStrictnessInfo` Just strict_sig |
|---|
| 448 | `setUnfoldingInfo` (if no_unf then noUnfolding |
|---|
| 449 | else mkImplicitUnfolding rhs) |
|---|
| 450 | -- In module where class op is defined, we must add |
|---|
| 451 | -- the unfolding, even though it'll never be inlined |
|---|
| 452 | -- becuase we use that to generate a top-level binding |
|---|
| 453 | -- for the ClassOp |
|---|
| 454 | |
|---|
| 455 | info | new_tycon = base_info `setInlinePragInfo` alwaysInlinePragma |
|---|
| 456 | -- See Note [Single-method classes] in TcInstDcls |
|---|
| 457 | -- for why alwaysInlinePragma |
|---|
| 458 | | otherwise = base_info `setSpecInfo` mkSpecInfo [rule] |
|---|
| 459 | `setInlinePragInfo` neverInlinePragma |
|---|
| 460 | -- Add a magic BuiltinRule, and never inline it |
|---|
| 461 | -- so that the rule is always available to fire. |
|---|
| 462 | -- See Note [ClassOp/DFun selection] in TcInstDcls |
|---|
| 463 | |
|---|
| 464 | n_ty_args = length tyvars |
|---|
| 465 | |
|---|
| 466 | -- This is the built-in rule that goes |
|---|
| 467 | -- op (dfT d1 d2) ---> opT d1 d2 |
|---|
| 468 | rule = BuiltinRule { ru_name = fsLit "Class op " `appendFS` |
|---|
| 469 | occNameFS (getOccName name) |
|---|
| 470 | , ru_fn = name |
|---|
| 471 | , ru_nargs = n_ty_args + 1 |
|---|
| 472 | , ru_try = dictSelRule val_index n_ty_args } |
|---|
| 473 | |
|---|
| 474 | -- The strictness signature is of the form U(AAAVAAAA) -> T |
|---|
| 475 | -- where the V depends on which item we are selecting |
|---|
| 476 | -- It's worth giving one, so that absence info etc is generated |
|---|
| 477 | -- even if the selector isn't inlined |
|---|
| 478 | strict_sig = mkStrictSig (mkTopDmdType [arg_dmd] TopRes) |
|---|
| 479 | arg_dmd | new_tycon = evalDmd |
|---|
| 480 | | otherwise = Eval (Prod [ if the_arg_id == id then evalDmd else Abs |
|---|
| 481 | | id <- arg_ids ]) |
|---|
| 482 | |
|---|
| 483 | tycon = classTyCon clas |
|---|
| 484 | new_tycon = isNewTyCon tycon |
|---|
| 485 | [data_con] = tyConDataCons tycon |
|---|
| 486 | tyvars = dataConUnivTyVars data_con |
|---|
| 487 | arg_tys = dataConRepArgTys data_con -- Includes the dictionary superclasses |
|---|
| 488 | |
|---|
| 489 | -- 'index' is a 0-index into the *value* arguments of the dictionary |
|---|
| 490 | val_index = assoc "MkId.mkDictSelId" sel_index_prs name |
|---|
| 491 | sel_index_prs = map idName (classAllSelIds clas) `zip` [0..] |
|---|
| 492 | |
|---|
| 493 | the_arg_id = arg_ids !! val_index |
|---|
| 494 | pred = mkClassPred clas (mkTyVarTys tyvars) |
|---|
| 495 | dict_id = mkTemplateLocal 1 pred |
|---|
| 496 | arg_ids = mkTemplateLocalsNum 2 arg_tys |
|---|
| 497 | |
|---|
| 498 | rhs = mkLams tyvars (Lam dict_id rhs_body) |
|---|
| 499 | rhs_body | new_tycon = unwrapNewTypeBody tycon (map mkTyVarTy tyvars) (Var dict_id) |
|---|
| 500 | | otherwise = Case (Var dict_id) dict_id (idType the_arg_id) |
|---|
| 501 | [(DataAlt data_con, arg_ids, varToCoreExpr the_arg_id)] |
|---|
| 502 | -- varToCoreExpr needed for equality superclass selectors |
|---|
| 503 | -- sel a b d = case x of { MkC _ (g:a~b) _ -> CO g } |
|---|
| 504 | |
|---|
| 505 | dictSelRule :: Int -> Arity |
|---|
| 506 | -> IdUnfoldingFun -> [CoreExpr] -> Maybe CoreExpr |
|---|
| 507 | -- Tries to persuade the argument to look like a constructor |
|---|
| 508 | -- application, using exprIsConApp_maybe, and then selects |
|---|
| 509 | -- from it |
|---|
| 510 | -- sel_i t1..tk (D t1..tk op1 ... opm) = opi |
|---|
| 511 | -- |
|---|
| 512 | dictSelRule val_index n_ty_args id_unf args |
|---|
| 513 | | (dict_arg : _) <- drop n_ty_args args |
|---|
| 514 | , Just (_, _, con_args) <- exprIsConApp_maybe id_unf dict_arg |
|---|
| 515 | = Just (con_args !! val_index) |
|---|
| 516 | | otherwise |
|---|
| 517 | = Nothing |
|---|
| 518 | \end{code} |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | %************************************************************************ |
|---|
| 522 | %* * |
|---|
| 523 | Boxing and unboxing |
|---|
| 524 | %* * |
|---|
| 525 | %************************************************************************ |
|---|
| 526 | |
|---|
| 527 | \begin{code} |
|---|
| 528 | -- unbox a product type... |
|---|
| 529 | -- we will recurse into newtypes, casting along the way, and unbox at the |
|---|
| 530 | -- first product data constructor we find. e.g. |
|---|
| 531 | -- |
|---|
| 532 | -- data PairInt = PairInt Int Int |
|---|
| 533 | -- newtype S = MkS PairInt |
|---|
| 534 | -- newtype T = MkT S |
|---|
| 535 | -- |
|---|
| 536 | -- If we have e = MkT (MkS (PairInt 0 1)) and some body expecting a list of |
|---|
| 537 | -- ids, we get (modulo int passing) |
|---|
| 538 | -- |
|---|
| 539 | -- case (e `cast` CoT) `cast` CoS of |
|---|
| 540 | -- PairInt a b -> body [a,b] |
|---|
| 541 | -- |
|---|
| 542 | -- The Ints passed around are just for creating fresh locals |
|---|
| 543 | unboxProduct :: Int -> CoreExpr -> Type -> (Int -> [Id] -> CoreExpr) -> CoreExpr |
|---|
| 544 | unboxProduct i arg arg_ty body |
|---|
| 545 | = result |
|---|
| 546 | where |
|---|
| 547 | result = mkUnpackCase the_id arg con_args boxing_con rhs |
|---|
| 548 | (_tycon, _tycon_args, boxing_con, tys) = deepSplitProductType "unboxProduct" arg_ty |
|---|
| 549 | ([the_id], i') = mkLocals i [arg_ty] |
|---|
| 550 | (con_args, i'') = mkLocals i' tys |
|---|
| 551 | rhs = body i'' con_args |
|---|
| 552 | |
|---|
| 553 | mkUnpackCase :: Id -> CoreExpr -> [Id] -> DataCon -> CoreExpr -> CoreExpr |
|---|
| 554 | -- (mkUnpackCase x e args Con body) |
|---|
| 555 | -- returns |
|---|
| 556 | -- case (e `cast` ...) of bndr { Con args -> body } |
|---|
| 557 | -- |
|---|
| 558 | -- the type of the bndr passed in is irrelevent |
|---|
| 559 | mkUnpackCase bndr arg unpk_args boxing_con body |
|---|
| 560 | = Case cast_arg (setIdType bndr bndr_ty) (exprType body) [(DataAlt boxing_con, unpk_args, body)] |
|---|
| 561 | where |
|---|
| 562 | (cast_arg, bndr_ty) = go (idType bndr) arg |
|---|
| 563 | go ty arg |
|---|
| 564 | | (tycon, tycon_args, _, _) <- splitProductType "mkUnpackCase" ty |
|---|
| 565 | , isNewTyCon tycon && not (isRecursiveTyCon tycon) |
|---|
| 566 | = go (newTyConInstRhs tycon tycon_args) |
|---|
| 567 | (unwrapNewTypeBody tycon tycon_args arg) |
|---|
| 568 | | otherwise = (arg, ty) |
|---|
| 569 | |
|---|
| 570 | -- ...and the dual |
|---|
| 571 | reboxProduct :: [Unique] -- uniques to create new local binders |
|---|
| 572 | -> Type -- type of product to box |
|---|
| 573 | -> ([Unique], -- remaining uniques |
|---|
| 574 | CoreExpr, -- boxed product |
|---|
| 575 | [Id]) -- Ids being boxed into product |
|---|
| 576 | reboxProduct us ty |
|---|
| 577 | = let |
|---|
| 578 | (_tycon, _tycon_args, _pack_con, con_arg_tys) = deepSplitProductType "reboxProduct" ty |
|---|
| 579 | |
|---|
| 580 | us' = dropList con_arg_tys us |
|---|
| 581 | |
|---|
| 582 | arg_ids = zipWith (mkSysLocal (fsLit "rb")) us con_arg_tys |
|---|
| 583 | |
|---|
| 584 | bind_rhs = mkProductBox arg_ids ty |
|---|
| 585 | |
|---|
| 586 | in |
|---|
| 587 | (us', bind_rhs, arg_ids) |
|---|
| 588 | |
|---|
| 589 | mkProductBox :: [Id] -> Type -> CoreExpr |
|---|
| 590 | mkProductBox arg_ids ty |
|---|
| 591 | = result_expr |
|---|
| 592 | where |
|---|
| 593 | (tycon, tycon_args, pack_con, _con_arg_tys) = splitProductType "mkProductBox" ty |
|---|
| 594 | |
|---|
| 595 | result_expr |
|---|
| 596 | | isNewTyCon tycon && not (isRecursiveTyCon tycon) |
|---|
| 597 | = wrap (mkProductBox arg_ids (newTyConInstRhs tycon tycon_args)) |
|---|
| 598 | | otherwise = mkConApp pack_con (map Type tycon_args ++ varsToCoreExprs arg_ids) |
|---|
| 599 | |
|---|
| 600 | wrap expr = wrapNewTypeBody tycon tycon_args expr |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | -- (mkReboxingAlt us con xs rhs) basically constructs the case |
|---|
| 604 | -- alternative (con, xs, rhs) |
|---|
| 605 | -- but it does the reboxing necessary to construct the *source* |
|---|
| 606 | -- arguments, xs, from the representation arguments ys. |
|---|
| 607 | -- For example: |
|---|
| 608 | -- data T = MkT !(Int,Int) Bool |
|---|
| 609 | -- |
|---|
| 610 | -- mkReboxingAlt MkT [x,b] r |
|---|
| 611 | -- = (DataAlt MkT, [y::Int,z::Int,b], let x = (y,z) in r) |
|---|
| 612 | -- |
|---|
| 613 | -- mkDataAlt should really be in DataCon, but it can't because |
|---|
| 614 | -- it manipulates CoreSyn. |
|---|
| 615 | |
|---|
| 616 | mkReboxingAlt |
|---|
| 617 | :: [Unique] -- Uniques for the new Ids |
|---|
| 618 | -> DataCon |
|---|
| 619 | -> [Var] -- Source-level args, *including* all evidence vars |
|---|
| 620 | -> CoreExpr -- RHS |
|---|
| 621 | -> CoreAlt |
|---|
| 622 | |
|---|
| 623 | mkReboxingAlt us con args rhs |
|---|
| 624 | | not (any isMarkedUnboxed stricts) |
|---|
| 625 | = (DataAlt con, args, rhs) |
|---|
| 626 | |
|---|
| 627 | | otherwise |
|---|
| 628 | = let |
|---|
| 629 | (binds, args') = go args stricts us |
|---|
| 630 | in |
|---|
| 631 | (DataAlt con, args', mkLets binds rhs) |
|---|
| 632 | |
|---|
| 633 | where |
|---|
| 634 | stricts = dataConExStricts con ++ dataConStrictMarks con |
|---|
| 635 | |
|---|
| 636 | go [] _stricts _us = ([], []) |
|---|
| 637 | |
|---|
| 638 | -- Type variable case |
|---|
| 639 | go (arg:args) stricts us |
|---|
| 640 | | isTyVar arg |
|---|
| 641 | = let (binds, args') = go args stricts us |
|---|
| 642 | in (binds, arg:args') |
|---|
| 643 | |
|---|
| 644 | -- Term variable case |
|---|
| 645 | go (arg:args) (str:stricts) us |
|---|
| 646 | | isMarkedUnboxed str |
|---|
| 647 | = let (binds, unpacked_args') = go args stricts us' |
|---|
| 648 | (us', bind_rhs, unpacked_args) = reboxProduct us (idType arg) |
|---|
| 649 | in |
|---|
| 650 | (NonRec arg bind_rhs : binds, unpacked_args ++ unpacked_args') |
|---|
| 651 | | otherwise |
|---|
| 652 | = let (binds, args') = go args stricts us |
|---|
| 653 | in (binds, arg:args') |
|---|
| 654 | go (_ : _) [] _ = panic "mkReboxingAlt" |
|---|
| 655 | \end{code} |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | %************************************************************************ |
|---|
| 659 | %* * |
|---|
| 660 | Wrapping and unwrapping newtypes and type families |
|---|
| 661 | %* * |
|---|
| 662 | %************************************************************************ |
|---|
| 663 | |
|---|
| 664 | \begin{code} |
|---|
| 665 | wrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr |
|---|
| 666 | -- The wrapper for the data constructor for a newtype looks like this: |
|---|
| 667 | -- newtype T a = MkT (a,Int) |
|---|
| 668 | -- MkT :: forall a. (a,Int) -> T a |
|---|
| 669 | -- MkT = /\a. \(x:(a,Int)). x `cast` sym (CoT a) |
|---|
| 670 | -- where CoT is the coercion TyCon assoicated with the newtype |
|---|
| 671 | -- |
|---|
| 672 | -- The call (wrapNewTypeBody T [a] e) returns the |
|---|
| 673 | -- body of the wrapper, namely |
|---|
| 674 | -- e `cast` (CoT [a]) |
|---|
| 675 | -- |
|---|
| 676 | -- If a coercion constructor is provided in the newtype, then we use |
|---|
| 677 | -- it, otherwise the wrap/unwrap are both no-ops |
|---|
| 678 | -- |
|---|
| 679 | -- If the we are dealing with a newtype *instance*, we have a second coercion |
|---|
| 680 | -- identifying the family instance with the constructor of the newtype |
|---|
| 681 | -- instance. This coercion is applied in any case (ie, composed with the |
|---|
| 682 | -- coercion constructor of the newtype or applied by itself). |
|---|
| 683 | |
|---|
| 684 | wrapNewTypeBody tycon args result_expr |
|---|
| 685 | = ASSERT( isNewTyCon tycon ) |
|---|
| 686 | wrapFamInstBody tycon args $ |
|---|
| 687 | mkCast result_expr (mkSymCo co) |
|---|
| 688 | where |
|---|
| 689 | co = mkAxInstCo (newTyConCo tycon) args |
|---|
| 690 | |
|---|
| 691 | -- When unwrapping, we do *not* apply any family coercion, because this will |
|---|
| 692 | -- be done via a CoPat by the type checker. We have to do it this way as |
|---|
| 693 | -- computing the right type arguments for the coercion requires more than just |
|---|
| 694 | -- a spliting operation (cf, TcPat.tcConPat). |
|---|
| 695 | |
|---|
| 696 | unwrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr |
|---|
| 697 | unwrapNewTypeBody tycon args result_expr |
|---|
| 698 | = ASSERT( isNewTyCon tycon ) |
|---|
| 699 | mkCast result_expr (mkAxInstCo (newTyConCo tycon) args) |
|---|
| 700 | |
|---|
| 701 | -- If the type constructor is a representation type of a data instance, wrap |
|---|
| 702 | -- the expression into a cast adjusting the expression type, which is an |
|---|
| 703 | -- instance of the representation type, to the corresponding instance of the |
|---|
| 704 | -- family instance type. |
|---|
| 705 | -- See Note [Wrappers for data instance tycons] |
|---|
| 706 | wrapFamInstBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr |
|---|
| 707 | wrapFamInstBody tycon args body |
|---|
| 708 | | Just co_con <- tyConFamilyCoercion_maybe tycon |
|---|
| 709 | = mkCast body (mkSymCo (mkAxInstCo co_con args)) |
|---|
| 710 | | otherwise |
|---|
| 711 | = body |
|---|
| 712 | |
|---|
| 713 | -- Same as `wrapFamInstBody`, but for type family instances, which are |
|---|
| 714 | -- represented by a `CoAxiom`, and not a `TyCon` |
|---|
| 715 | wrapTypeFamInstBody :: CoAxiom -> [Type] -> CoreExpr -> CoreExpr |
|---|
| 716 | wrapTypeFamInstBody axiom args body |
|---|
| 717 | = mkCast body (mkSymCo (mkAxInstCo axiom args)) |
|---|
| 718 | |
|---|
| 719 | unwrapFamInstScrut :: TyCon -> [Type] -> CoreExpr -> CoreExpr |
|---|
| 720 | unwrapFamInstScrut tycon args scrut |
|---|
| 721 | | Just co_con <- tyConFamilyCoercion_maybe tycon |
|---|
| 722 | = mkCast scrut (mkAxInstCo co_con args) |
|---|
| 723 | | otherwise |
|---|
| 724 | = scrut |
|---|
| 725 | |
|---|
| 726 | unwrapTypeFamInstScrut :: CoAxiom -> [Type] -> CoreExpr -> CoreExpr |
|---|
| 727 | unwrapTypeFamInstScrut axiom args scrut |
|---|
| 728 | = mkCast scrut (mkAxInstCo axiom args) |
|---|
| 729 | \end{code} |
|---|
| 730 | |
|---|
| 731 | |
|---|
| 732 | %************************************************************************ |
|---|
| 733 | %* * |
|---|
| 734 | \subsection{Primitive operations} |
|---|
| 735 | %* * |
|---|
| 736 | %************************************************************************ |
|---|
| 737 | |
|---|
| 738 | \begin{code} |
|---|
| 739 | mkPrimOpId :: PrimOp -> Id |
|---|
| 740 | mkPrimOpId prim_op |
|---|
| 741 | = id |
|---|
| 742 | where |
|---|
| 743 | (tyvars,arg_tys,res_ty, arity, strict_sig) = primOpSig prim_op |
|---|
| 744 | ty = mkForAllTys tyvars (mkFunTys arg_tys res_ty) |
|---|
| 745 | name = mkWiredInName gHC_PRIM (primOpOcc prim_op) |
|---|
| 746 | (mkPrimOpIdUnique (primOpTag prim_op)) |
|---|
| 747 | (AnId id) UserSyntax |
|---|
| 748 | id = mkGlobalId (PrimOpId prim_op) name ty info |
|---|
| 749 | |
|---|
| 750 | info = noCafIdInfo |
|---|
| 751 | `setSpecInfo` mkSpecInfo (primOpRules prim_op name) |
|---|
| 752 | `setArityInfo` arity |
|---|
| 753 | `setStrictnessInfo` Just strict_sig |
|---|
| 754 | |
|---|
| 755 | -- For each ccall we manufacture a separate CCallOpId, giving it |
|---|
| 756 | -- a fresh unique, a type that is correct for this particular ccall, |
|---|
| 757 | -- and a CCall structure that gives the correct details about calling |
|---|
| 758 | -- convention etc. |
|---|
| 759 | -- |
|---|
| 760 | -- The *name* of this Id is a local name whose OccName gives the full |
|---|
| 761 | -- details of the ccall, type and all. This means that the interface |
|---|
| 762 | -- file reader can reconstruct a suitable Id |
|---|
| 763 | |
|---|
| 764 | mkFCallId :: Unique -> ForeignCall -> Type -> Id |
|---|
| 765 | mkFCallId uniq fcall ty |
|---|
| 766 | = ASSERT( isEmptyVarSet (tyVarsOfType ty) ) |
|---|
| 767 | -- A CCallOpId should have no free type variables; |
|---|
| 768 | -- when doing substitutions won't substitute over it |
|---|
| 769 | mkGlobalId (FCallId fcall) name ty info |
|---|
| 770 | where |
|---|
| 771 | occ_str = showSDoc (braces (ppr fcall <+> ppr ty)) |
|---|
| 772 | -- The "occurrence name" of a ccall is the full info about the |
|---|
| 773 | -- ccall; it is encoded, but may have embedded spaces etc! |
|---|
| 774 | |
|---|
| 775 | name = mkFCallName uniq occ_str |
|---|
| 776 | |
|---|
| 777 | info = noCafIdInfo |
|---|
| 778 | `setArityInfo` arity |
|---|
| 779 | `setStrictnessInfo` Just strict_sig |
|---|
| 780 | |
|---|
| 781 | (_, tau) = tcSplitForAllTys ty |
|---|
| 782 | (arg_tys, _) = tcSplitFunTys tau |
|---|
| 783 | arity = length arg_tys |
|---|
| 784 | strict_sig = mkStrictSig (mkTopDmdType (replicate arity evalDmd) TopRes) |
|---|
| 785 | \end{code} |
|---|
| 786 | |
|---|
| 787 | |
|---|
| 788 | %************************************************************************ |
|---|
| 789 | %* * |
|---|
| 790 | \subsection{DictFuns and default methods} |
|---|
| 791 | %* * |
|---|
| 792 | %************************************************************************ |
|---|
| 793 | |
|---|
| 794 | Important notes about dict funs and default methods |
|---|
| 795 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 796 | Dict funs and default methods are *not* ImplicitIds. Their definition |
|---|
| 797 | involves user-written code, so we can't figure out their strictness etc |
|---|
| 798 | based on fixed info, as we can for constructors and record selectors (say). |
|---|
| 799 | |
|---|
| 800 | We build them as LocalIds, but with External Names. This ensures that |
|---|
| 801 | they are taken to account by free-variable finding and dependency |
|---|
| 802 | analysis (e.g. CoreFVs.exprFreeVars). |
|---|
| 803 | |
|---|
| 804 | Why shouldn't they be bound as GlobalIds? Because, in particular, if |
|---|
| 805 | they are globals, the specialiser floats dict uses above their defns, |
|---|
| 806 | which prevents good simplifications happening. Also the strictness |
|---|
| 807 | analyser treats a occurrence of a GlobalId as imported and assumes it |
|---|
| 808 | contains strictness in its IdInfo, which isn't true if the thing is |
|---|
| 809 | bound in the same module as the occurrence. |
|---|
| 810 | |
|---|
| 811 | It's OK for dfuns to be LocalIds, because we form the instance-env to |
|---|
| 812 | pass on to the next module (md_insts) in CoreTidy, afer tidying |
|---|
| 813 | and globalising the top-level Ids. |
|---|
| 814 | |
|---|
| 815 | BUT make sure they are *exported* LocalIds (mkExportedLocalId) so |
|---|
| 816 | that they aren't discarded by the occurrence analyser. |
|---|
| 817 | |
|---|
| 818 | \begin{code} |
|---|
| 819 | mkDictFunId :: Name -- Name to use for the dict fun; |
|---|
| 820 | -> [TyVar] |
|---|
| 821 | -> ThetaType |
|---|
| 822 | -> Class |
|---|
| 823 | -> [Type] |
|---|
| 824 | -> Id |
|---|
| 825 | -- Implements the DFun Superclass Invariant (see TcInstDcls) |
|---|
| 826 | |
|---|
| 827 | mkDictFunId dfun_name tvs theta clas tys |
|---|
| 828 | = mkExportedLocalVar (DFunId is_nt) |
|---|
| 829 | dfun_name |
|---|
| 830 | dfun_ty |
|---|
| 831 | vanillaIdInfo |
|---|
| 832 | where |
|---|
| 833 | is_nt = isNewTyCon (classTyCon clas) |
|---|
| 834 | dfun_ty = mkDictFunTy tvs theta clas tys |
|---|
| 835 | |
|---|
| 836 | mkDictFunTy :: [TyVar] -> ThetaType -> Class -> [Type] -> Type |
|---|
| 837 | mkDictFunTy tvs theta clas tys |
|---|
| 838 | = mkSigmaTy tvs theta (mkClassPred clas tys) |
|---|
| 839 | \end{code} |
|---|
| 840 | |
|---|
| 841 | |
|---|
| 842 | %************************************************************************ |
|---|
| 843 | %* * |
|---|
| 844 | \subsection{Un-definable} |
|---|
| 845 | %* * |
|---|
| 846 | %************************************************************************ |
|---|
| 847 | |
|---|
| 848 | These Ids can't be defined in Haskell. They could be defined in |
|---|
| 849 | unfoldings in the wired-in GHC.Prim interface file, but we'd have to |
|---|
| 850 | ensure that they were definitely, definitely inlined, because there is |
|---|
| 851 | no curried identifier for them. That's what mkCompulsoryUnfolding |
|---|
| 852 | does. If we had a way to get a compulsory unfolding from an interface |
|---|
| 853 | file, we could do that, but we don't right now. |
|---|
| 854 | |
|---|
| 855 | unsafeCoerce# isn't so much a PrimOp as a phantom identifier, that |
|---|
| 856 | just gets expanded into a type coercion wherever it occurs. Hence we |
|---|
| 857 | add it as a built-in Id with an unfolding here. |
|---|
| 858 | |
|---|
| 859 | The type variables we use here are "open" type variables: this means |
|---|
| 860 | they can unify with both unlifted and lifted types. Hence we provide |
|---|
| 861 | another gun with which to shoot yourself in the foot. |
|---|
| 862 | |
|---|
| 863 | \begin{code} |
|---|
| 864 | lazyIdName, unsafeCoerceName, nullAddrName, seqName, realWorldName, coercionTokenName :: Name |
|---|
| 865 | unsafeCoerceName = mkWiredInIdName gHC_PRIM (fsLit "unsafeCoerce#") unsafeCoerceIdKey unsafeCoerceId |
|---|
| 866 | nullAddrName = mkWiredInIdName gHC_PRIM (fsLit "nullAddr#") nullAddrIdKey nullAddrId |
|---|
| 867 | seqName = mkWiredInIdName gHC_PRIM (fsLit "seq") seqIdKey seqId |
|---|
| 868 | realWorldName = mkWiredInIdName gHC_PRIM (fsLit "realWorld#") realWorldPrimIdKey realWorldPrimId |
|---|
| 869 | lazyIdName = mkWiredInIdName gHC_BASE (fsLit "lazy") lazyIdKey lazyId |
|---|
| 870 | coercionTokenName = mkWiredInIdName gHC_PRIM (fsLit "coercionToken#") coercionTokenIdKey coercionTokenId |
|---|
| 871 | \end{code} |
|---|
| 872 | |
|---|
| 873 | \begin{code} |
|---|
| 874 | ------------------------------------------------ |
|---|
| 875 | -- unsafeCoerce# :: forall a b. a -> b |
|---|
| 876 | unsafeCoerceId :: Id |
|---|
| 877 | unsafeCoerceId |
|---|
| 878 | = pcMiscPrelId unsafeCoerceName ty info |
|---|
| 879 | where |
|---|
| 880 | info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma |
|---|
| 881 | `setUnfoldingInfo` mkCompulsoryUnfolding rhs |
|---|
| 882 | |
|---|
| 883 | |
|---|
| 884 | ty = mkForAllTys [openAlphaTyVar,openBetaTyVar] |
|---|
| 885 | (mkFunTy openAlphaTy openBetaTy) |
|---|
| 886 | [x] = mkTemplateLocals [openAlphaTy] |
|---|
| 887 | rhs = mkLams [openAlphaTyVar,openBetaTyVar,x] $ |
|---|
| 888 | Cast (Var x) (mkUnsafeCo openAlphaTy openBetaTy) |
|---|
| 889 | |
|---|
| 890 | ------------------------------------------------ |
|---|
| 891 | nullAddrId :: Id |
|---|
| 892 | -- nullAddr# :: Addr# |
|---|
| 893 | -- The reason is is here is because we don't provide |
|---|
| 894 | -- a way to write this literal in Haskell. |
|---|
| 895 | nullAddrId = pcMiscPrelId nullAddrName addrPrimTy info |
|---|
| 896 | where |
|---|
| 897 | info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma |
|---|
| 898 | `setUnfoldingInfo` mkCompulsoryUnfolding (Lit nullAddrLit) |
|---|
| 899 | |
|---|
| 900 | ------------------------------------------------ |
|---|
| 901 | seqId :: Id -- See Note [seqId magic] |
|---|
| 902 | seqId = pcMiscPrelId seqName ty info |
|---|
| 903 | where |
|---|
| 904 | info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma |
|---|
| 905 | `setUnfoldingInfo` mkCompulsoryUnfolding rhs |
|---|
| 906 | `setSpecInfo` mkSpecInfo [seq_cast_rule] |
|---|
| 907 | |
|---|
| 908 | |
|---|
| 909 | ty = mkForAllTys [alphaTyVar,betaTyVar] |
|---|
| 910 | (mkFunTy alphaTy (mkFunTy betaTy betaTy)) |
|---|
| 911 | -- NB argBetaTyVar; see Note [seqId magic] |
|---|
| 912 | |
|---|
| 913 | [x,y] = mkTemplateLocals [alphaTy, betaTy] |
|---|
| 914 | rhs = mkLams [alphaTyVar,betaTyVar,x,y] (Case (Var x) x betaTy [(DEFAULT, [], Var y)]) |
|---|
| 915 | |
|---|
| 916 | -- See Note [Built-in RULES for seq] |
|---|
| 917 | seq_cast_rule = BuiltinRule { ru_name = fsLit "seq of cast" |
|---|
| 918 | , ru_fn = seqName |
|---|
| 919 | , ru_nargs = 4 |
|---|
| 920 | , ru_try = match_seq_of_cast |
|---|
| 921 | } |
|---|
| 922 | |
|---|
| 923 | match_seq_of_cast :: IdUnfoldingFun -> [CoreExpr] -> Maybe CoreExpr |
|---|
| 924 | -- See Note [Built-in RULES for seq] |
|---|
| 925 | match_seq_of_cast _ [Type _, Type res_ty, Cast scrut co, expr] |
|---|
| 926 | = Just (Var seqId `mkApps` [Type (pFst (coercionKind co)), Type res_ty, |
|---|
| 927 | scrut, expr]) |
|---|
| 928 | match_seq_of_cast _ _ = Nothing |
|---|
| 929 | |
|---|
| 930 | ------------------------------------------------ |
|---|
| 931 | lazyId :: Id -- See Note [lazyId magic] |
|---|
| 932 | lazyId = pcMiscPrelId lazyIdName ty info |
|---|
| 933 | where |
|---|
| 934 | info = noCafIdInfo |
|---|
| 935 | ty = mkForAllTys [alphaTyVar] (mkFunTy alphaTy alphaTy) |
|---|
| 936 | \end{code} |
|---|
| 937 | |
|---|
| 938 | Note [Unsafe coerce magic] |
|---|
| 939 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 940 | We define a *primitive* |
|---|
| 941 | GHC.Prim.unsafeCoerce# |
|---|
| 942 | and then in the base library we define the ordinary function |
|---|
| 943 | Unsafe.Coerce.unsafeCoerce :: forall (a:*) (b:*). a -> b |
|---|
| 944 | unsafeCoerce x = unsafeCoerce# x |
|---|
| 945 | |
|---|
| 946 | Notice that unsafeCoerce has a civilized (albeit still dangerous) |
|---|
| 947 | polymorphic type, whose type args have kind *. So you can't use it on |
|---|
| 948 | unboxed values (unsafeCoerce 3#). |
|---|
| 949 | |
|---|
| 950 | In contrast unsafeCoerce# is even more dangerous because you *can* use |
|---|
| 951 | it on unboxed things, (unsafeCoerce# 3#) :: Int. Its type is |
|---|
| 952 | forall (a:OpenKind) (b:OpenKind). a -> b |
|---|
| 953 | |
|---|
| 954 | Note [seqId magic] |
|---|
| 955 | ~~~~~~~~~~~~~~~~~~ |
|---|
| 956 | 'GHC.Prim.seq' is special in several ways. |
|---|
| 957 | |
|---|
| 958 | a) Its second arg can have an unboxed type |
|---|
| 959 | x `seq` (v +# w) |
|---|
| 960 | Hence its second type variable has ArgKind |
|---|
| 961 | |
|---|
| 962 | b) Its fixity is set in LoadIface.ghcPrimIface |
|---|
| 963 | |
|---|
| 964 | c) It has quite a bit of desugaring magic. |
|---|
| 965 | See DsUtils.lhs Note [Desugaring seq (1)] and (2) and (3) |
|---|
| 966 | |
|---|
| 967 | d) There is some special rule handing: Note [User-defined RULES for seq] |
|---|
| 968 | |
|---|
| 969 | e) See Note [Typing rule for seq] in TcExpr. |
|---|
| 970 | |
|---|
| 971 | Note [User-defined RULES for seq] |
|---|
| 972 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 973 | Roman found situations where he had |
|---|
| 974 | case (f n) of _ -> e |
|---|
| 975 | where he knew that f (which was strict in n) would terminate if n did. |
|---|
| 976 | Notice that the result of (f n) is discarded. So it makes sense to |
|---|
| 977 | transform to |
|---|
| 978 | case n of _ -> e |
|---|
| 979 | |
|---|
| 980 | Rather than attempt some general analysis to support this, I've added |
|---|
| 981 | enough support that you can do this using a rewrite rule: |
|---|
| 982 | |
|---|
| 983 | RULE "f/seq" forall n. seq (f n) e = seq n e |
|---|
| 984 | |
|---|
| 985 | You write that rule. When GHC sees a case expression that discards |
|---|
| 986 | its result, it mentally transforms it to a call to 'seq' and looks for |
|---|
| 987 | a RULE. (This is done in Simplify.rebuildCase.) As usual, the |
|---|
| 988 | correctness of the rule is up to you. |
|---|
| 989 | |
|---|
| 990 | To make this work, we need to be careful that the magical desugaring |
|---|
| 991 | done in Note [seqId magic] item (c) is *not* done on the LHS of a rule. |
|---|
| 992 | Or rather, we arrange to un-do it, in DsBinds.decomposeRuleLhs. |
|---|
| 993 | |
|---|
| 994 | Note [Built-in RULES for seq] |
|---|
| 995 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 996 | We also have the following built-in rule for seq |
|---|
| 997 | |
|---|
| 998 | seq (x `cast` co) y = seq x y |
|---|
| 999 | |
|---|
| 1000 | This eliminates unnecessary casts and also allows other seq rules to |
|---|
| 1001 | match more often. Notably, |
|---|
| 1002 | |
|---|
| 1003 | seq (f x `cast` co) y --> seq (f x) y |
|---|
| 1004 | |
|---|
| 1005 | and now a user-defined rule for seq (see Note [User-defined RULES for seq]) |
|---|
| 1006 | may fire. |
|---|
| 1007 | |
|---|
| 1008 | |
|---|
| 1009 | Note [lazyId magic] |
|---|
| 1010 | ~~~~~~~~~~~~~~~~~~~ |
|---|
| 1011 | lazy :: forall a?. a? -> a? (i.e. works for unboxed types too) |
|---|
| 1012 | |
|---|
| 1013 | Used to lazify pseq: pseq a b = a `seq` lazy b |
|---|
| 1014 | |
|---|
| 1015 | Also, no strictness: by being a built-in Id, all the info about lazyId comes from here, |
|---|
| 1016 | not from GHC.Base.hi. This is important, because the strictness |
|---|
| 1017 | analyser will spot it as strict! |
|---|
| 1018 | |
|---|
| 1019 | Also no unfolding in lazyId: it gets "inlined" by a HACK in CorePrep. |
|---|
| 1020 | It's very important to do this inlining *after* unfoldings are exposed |
|---|
| 1021 | in the interface file. Otherwise, the unfolding for (say) pseq in the |
|---|
| 1022 | interface file will not mention 'lazy', so if we inline 'pseq' we'll totally |
|---|
| 1023 | miss the very thing that 'lazy' was there for in the first place. |
|---|
| 1024 | See Trac #3259 for a real world example. |
|---|
| 1025 | |
|---|
| 1026 | lazyId is defined in GHC.Base, so we don't *have* to inline it. If it |
|---|
| 1027 | appears un-applied, we'll end up just calling it. |
|---|
| 1028 | |
|---|
| 1029 | ------------------------------------------------------------- |
|---|
| 1030 | @realWorld#@ used to be a magic literal, \tr{void#}. If things get |
|---|
| 1031 | nasty as-is, change it back to a literal (@Literal@). |
|---|
| 1032 | |
|---|
| 1033 | voidArgId is a Local Id used simply as an argument in functions |
|---|
| 1034 | where we just want an arg to avoid having a thunk of unlifted type. |
|---|
| 1035 | E.g. |
|---|
| 1036 | x = \ void :: State# RealWorld -> (# p, q #) |
|---|
| 1037 | |
|---|
| 1038 | This comes up in strictness analysis |
|---|
| 1039 | |
|---|
| 1040 | \begin{code} |
|---|
| 1041 | realWorldPrimId :: Id |
|---|
| 1042 | realWorldPrimId -- :: State# RealWorld |
|---|
| 1043 | = pcMiscPrelId realWorldName realWorldStatePrimTy |
|---|
| 1044 | (noCafIdInfo `setUnfoldingInfo` evaldUnfolding) |
|---|
| 1045 | -- The evaldUnfolding makes it look that realWorld# is evaluated |
|---|
| 1046 | -- which in turn makes Simplify.interestingArg return True, |
|---|
| 1047 | -- which in turn makes INLINE things applied to realWorld# likely |
|---|
| 1048 | -- to be inlined |
|---|
| 1049 | |
|---|
| 1050 | voidArgId :: Id |
|---|
| 1051 | voidArgId -- :: State# RealWorld |
|---|
| 1052 | = mkSysLocal (fsLit "void") voidArgIdKey realWorldStatePrimTy |
|---|
| 1053 | |
|---|
| 1054 | coercionTokenId :: Id -- :: () ~ () |
|---|
| 1055 | coercionTokenId -- Used to replace Coercion terms when we go to STG |
|---|
| 1056 | = pcMiscPrelId coercionTokenName |
|---|
| 1057 | (mkTyConApp eqPrimTyCon [liftedTypeKind, unitTy, unitTy]) |
|---|
| 1058 | noCafIdInfo |
|---|
| 1059 | \end{code} |
|---|
| 1060 | |
|---|
| 1061 | |
|---|
| 1062 | \begin{code} |
|---|
| 1063 | pcMiscPrelId :: Name -> Type -> IdInfo -> Id |
|---|
| 1064 | pcMiscPrelId name ty info |
|---|
| 1065 | = mkVanillaGlobalWithInfo name ty info |
|---|
| 1066 | -- We lie and say the thing is imported; otherwise, we get into |
|---|
| 1067 | -- a mess with dependency analysis; e.g., core2stg may heave in |
|---|
| 1068 | -- random calls to GHCbase.unpackPS__. If GHCbase is the module |
|---|
| 1069 | -- being compiled, then it's just a matter of luck if the definition |
|---|
| 1070 | -- will be in "the right place" to be in scope. |
|---|
| 1071 | \end{code} |
|---|