Make record selectors into ordinary functions
[ghc-hetmet.git] / compiler / basicTypes / Id.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5 \section[Id]{@Ids@: Value and constructor identifiers}
6
7 \begin{code}
8 -- |
9 -- #name_types#
10 -- GHC uses several kinds of name internally:
11 --
12 -- * 'OccName.OccName': see "OccName#name_types"
13 --
14 -- * 'RdrName.RdrName': see "RdrName#name_types"
15 --
16 -- * 'Name.Name': see "Name#name_types"
17 --
18 -- * 'Id.Id' represents names that not only have a 'Name.Name' but also a 'TypeRep.Type' and some additional
19 --   details (a 'IdInfo.IdInfo' and one of 'Var.LocalIdDetails' or 'IdInfo.GlobalIdDetails') that
20 --   are added, modified and inspected by various compiler passes. These 'Var.Var' names may either 
21 --   be global or local, see "Var#globalvslocal"
22 --
23 -- * 'Var.Var': see "Var#name_types"
24 module Id (
25         -- * The main types
26         Id, DictId,
27
28         -- ** Simple construction
29         mkGlobalId, mkVanillaGlobal, mkVanillaGlobalWithInfo,
30         mkLocalId, mkLocalIdWithInfo, mkExportedLocalId,
31         mkSysLocal, mkSysLocalM, mkUserLocal, mkUserLocalM,
32         mkTemplateLocals, mkTemplateLocalsNum, mkTemplateLocal,
33         mkWorkerId, 
34
35         -- ** Taking an Id apart
36         idName, idType, idUnique, idInfo, idDetails,
37         isId, idPrimRep,
38         recordSelectorFieldLabel,
39
40         -- ** Modifying an Id
41         setIdName, setIdUnique, Id.setIdType, 
42         setIdExported, setIdNotExported, 
43         globaliseId, localiseId, 
44         setIdInfo, lazySetIdInfo, modifyIdInfo, maybeModifyIdInfo,
45         zapLamIdInfo, zapDemandIdInfo, zapFragileIdInfo, transferPolyIdInfo,
46         
47
48         -- ** Predicates on Ids
49         isImplicitId, isDeadBinder, isDictId, isStrictId,
50         isExportedId, isLocalId, isGlobalId,
51         isRecordSelector, isNaughtyRecordSelector,
52         isClassOpId_maybe,
53         isPrimOpId, isPrimOpId_maybe, 
54         isFCallId, isFCallId_maybe,
55         isDataConWorkId, isDataConWorkId_maybe, isDataConId_maybe, idDataCon,
56         isBottomingId, idIsFrom,
57         isTickBoxOp, isTickBoxOp_maybe,
58         hasNoBinding, 
59
60         -- ** Inline pragma stuff
61         idInlinePragma, setInlinePragma, modifyInlinePragma, 
62
63         -- ** One-shot lambdas
64         isOneShotBndr, isOneShotLambda, isStateHackType,
65         setOneShotLambda, clearOneShotLambda,
66
67         -- ** Reading 'IdInfo' fields
68         idArity, 
69         idNewDemandInfo, idNewDemandInfo_maybe,
70         idNewStrictness, idNewStrictness_maybe, 
71         idWorkerInfo,
72         idUnfolding,
73         idSpecialisation, idCoreRules, idHasRules,
74         idCafInfo,
75         idLBVarInfo,
76         idOccInfo,
77
78 #ifdef OLD_STRICTNESS
79         idDemandInfo, 
80         idStrictness, 
81         idCprInfo,
82 #endif
83
84         -- ** Writing 'IdInfo' fields
85         setIdUnfolding,
86         setIdArity,
87         setIdNewDemandInfo, 
88         setIdNewStrictness, zapIdNewStrictness,
89         setIdWorkerInfo,
90         setIdSpecialisation,
91         setIdCafInfo,
92         setIdOccInfo, zapIdOccInfo,
93
94 #ifdef OLD_STRICTNESS
95         setIdStrictness, 
96         setIdDemandInfo, 
97         setIdCprInfo,
98 #endif
99     ) where
100
101 #include "HsVersions.h"
102
103 import CoreSyn ( CoreRule, Unfolding )
104
105 import IdInfo
106 import BasicTypes
107
108 -- Imported and re-exported 
109 import Var( Id, DictId,
110             idInfo, idDetails, globaliseId,
111             isId, isLocalId, isGlobalId, isExportedId )
112 import qualified Var
113
114 import TyCon
115 import Type
116 import TcType
117 import TysPrim
118 #ifdef OLD_STRICTNESS
119 import qualified Demand
120 #endif
121 import DataCon
122 import NewDemand
123 import Name
124 import Module
125 import Class
126 import PrimOp
127 import ForeignCall
128 import OccName
129 import Maybes
130 import SrcLoc
131 import Outputable
132 import Unique
133 import UniqSupply
134 import FastString
135 import StaticFlags
136
137 -- infixl so you can say (id `set` a `set` b)
138 infixl  1 `setIdUnfolding`,
139           `setIdArity`,
140           `setIdNewDemandInfo`,
141           `setIdNewStrictness`,
142           `setIdWorkerInfo`,
143           `setIdSpecialisation`,
144           `setInlinePragma`,
145           `idCafInfo`
146 #ifdef OLD_STRICTNESS
147           ,`idCprInfo`
148           ,`setIdStrictness`
149           ,`setIdDemandInfo`
150 #endif
151 \end{code}
152
153 %************************************************************************
154 %*                                                                      *
155 \subsection{Basic Id manipulation}
156 %*                                                                      *
157 %************************************************************************
158
159 \begin{code}
160 idName   :: Id -> Name
161 idName    = Var.varName
162
163 idUnique :: Id -> Unique
164 idUnique  = Var.varUnique
165
166 idType   :: Id -> Kind
167 idType    = Var.varType
168
169 idPrimRep :: Id -> PrimRep
170 idPrimRep id = typePrimRep (idType id)
171
172 setIdName :: Id -> Name -> Id
173 setIdName = Var.setVarName
174
175 setIdUnique :: Id -> Unique -> Id
176 setIdUnique = Var.setVarUnique
177
178 -- | Not only does this set the 'Id' 'Type', it also evaluates the type to try and
179 -- reduce space usage
180 setIdType :: Id -> Type -> Id
181 setIdType id ty = seqType ty `seq` Var.setVarType id ty
182
183 setIdExported :: Id -> Id
184 setIdExported = Var.setIdExported
185
186 setIdNotExported :: Id -> Id
187 setIdNotExported = Var.setIdNotExported
188
189 localiseId :: Id -> Id
190 -- Make an with the same unique and type as the 
191 -- incoming Id, but with an *Internal* Name and *LocalId* flavour
192 localiseId id 
193   | isLocalId id && isInternalName name
194   = id
195   | otherwise
196   = mkLocalIdWithInfo (localiseName name) (idType id) (idInfo id)
197   where
198     name = idName id
199
200 lazySetIdInfo :: Id -> IdInfo -> Id
201 lazySetIdInfo = Var.lazySetIdInfo
202
203 setIdInfo :: Id -> IdInfo -> Id
204 setIdInfo id info = seqIdInfo info `seq` (lazySetIdInfo id info)
205         -- Try to avoid spack leaks by seq'ing
206
207 modifyIdInfo :: (IdInfo -> IdInfo) -> Id -> Id
208 modifyIdInfo fn id = setIdInfo id (fn (idInfo id))
209
210 -- maybeModifyIdInfo tries to avoid unnecesary thrashing
211 maybeModifyIdInfo :: Maybe IdInfo -> Id -> Id
212 maybeModifyIdInfo (Just new_info) id = lazySetIdInfo id new_info
213 maybeModifyIdInfo Nothing         id = id
214 \end{code}
215
216 %************************************************************************
217 %*                                                                      *
218 \subsection{Simple Id construction}
219 %*                                                                      *
220 %************************************************************************
221
222 Absolutely all Ids are made by mkId.  It is just like Var.mkId,
223 but in addition it pins free-tyvar-info onto the Id's type, 
224 where it can easily be found.
225
226 Note [Free type variables]
227 ~~~~~~~~~~~~~~~~~~~~~~~~~~
228 At one time we cached the free type variables of the type of an Id
229 at the root of the type in a TyNote.  The idea was to avoid repeating
230 the free-type-variable calculation.  But it turned out to slow down
231 the compiler overall. I don't quite know why; perhaps finding free
232 type variables of an Id isn't all that common whereas applying a 
233 substitution (which changes the free type variables) is more common.
234 Anyway, we removed it in March 2008.
235
236 \begin{code}
237 -- | For an explanation of global vs. local 'Id's, see "Var#globalvslocal"
238 mkGlobalId :: IdDetails -> Name -> Type -> IdInfo -> Id
239 mkGlobalId = Var.mkGlobalVar
240
241 -- | Make a global 'Id' without any extra information at all
242 mkVanillaGlobal :: Name -> Type -> Id
243 mkVanillaGlobal name ty = mkVanillaGlobalWithInfo name ty vanillaIdInfo
244
245 -- | Make a global 'Id' with no global information but some generic 'IdInfo'
246 mkVanillaGlobalWithInfo :: Name -> Type -> IdInfo -> Id
247 mkVanillaGlobalWithInfo = mkGlobalId VanillaId
248
249
250 -- | For an explanation of global vs. local 'Id's, see "Var#globalvslocal"
251 mkLocalId :: Name -> Type -> Id
252 mkLocalId name ty = mkLocalIdWithInfo name ty vanillaIdInfo
253
254 mkLocalIdWithInfo :: Name -> Type -> IdInfo -> Id
255 mkLocalIdWithInfo name ty info = Var.mkLocalVar VanillaId name ty info
256         -- Note [Free type variables]
257
258 -- | Create a local 'Id' that is marked as exported. 
259 -- This prevents things attached to it from being removed as dead code.
260 mkExportedLocalId :: Name -> Type -> Id
261 mkExportedLocalId name ty = Var.mkExportedLocalVar VanillaId name ty vanillaIdInfo
262         -- Note [Free type variables]
263
264
265 -- | Create a system local 'Id'. These are local 'Id's (see "Var#globalvslocal") 
266 -- that are created by the compiler out of thin air
267 mkSysLocal :: FastString -> Unique -> Type -> Id
268 mkSysLocal fs uniq ty = mkLocalId (mkSystemVarName uniq fs) ty
269
270 mkSysLocalM :: MonadUnique m => FastString -> Type -> m Id
271 mkSysLocalM fs ty = getUniqueM >>= (\uniq -> return (mkSysLocal fs uniq ty))
272
273
274 -- | Create a user local 'Id'. These are local 'Id's (see "Var#globalvslocal") with a name and location that the user might recognize
275 mkUserLocal :: OccName -> Unique -> Type -> SrcSpan -> Id
276 mkUserLocal occ uniq ty loc = mkLocalId (mkInternalName uniq occ loc) ty
277
278 mkUserLocalM :: MonadUnique m => OccName -> Type -> SrcSpan -> m Id
279 mkUserLocalM occ ty loc = getUniqueM >>= (\uniq -> return (mkUserLocal occ uniq ty loc))
280
281 \end{code}
282
283 Make some local @Ids@ for a template @CoreExpr@.  These have bogus
284 @Uniques@, but that's OK because the templates are supposed to be
285 instantiated before use.
286  
287 \begin{code}
288 -- | Workers get local names. "CoreTidy" will externalise these if necessary
289 mkWorkerId :: Unique -> Id -> Type -> Id
290 mkWorkerId uniq unwrkr ty
291   = mkLocalId wkr_name ty
292   where
293     wkr_name = mkInternalName uniq (mkWorkerOcc (getOccName unwrkr)) (getSrcSpan unwrkr)
294
295 -- | Create a /template local/: a family of system local 'Id's in bijection with @Int@s, typically used in unfoldings
296 mkTemplateLocal :: Int -> Type -> Id
297 mkTemplateLocal i ty = mkSysLocal (fsLit "tpl") (mkBuiltinUnique i) ty
298
299 -- | Create a template local for a series of types
300 mkTemplateLocals :: [Type] -> [Id]
301 mkTemplateLocals = mkTemplateLocalsNum 1
302
303 -- | Create a template local for a series of type, but start from a specified template local
304 mkTemplateLocalsNum :: Int -> [Type] -> [Id]
305 mkTemplateLocalsNum n tys = zipWith mkTemplateLocal [n..] tys
306 \end{code}
307
308
309 %************************************************************************
310 %*                                                                      *
311 \subsection{Special Ids}
312 %*                                                                      *
313 %************************************************************************
314
315 \begin{code}
316 -- | If the 'Id' is that for a record selector, extract the 'sel_tycon' and label. Panic otherwise
317 recordSelectorFieldLabel :: Id -> (TyCon, FieldLabel)
318 recordSelectorFieldLabel id
319   = case Var.idDetails id of
320         RecSelId { sel_tycon = tycon } -> (tycon, idName id)
321         _ -> panic "recordSelectorFieldLabel"
322
323 isRecordSelector        :: Id -> Bool
324 isNaughtyRecordSelector :: Id -> Bool
325 isPrimOpId              :: Id -> Bool
326 isFCallId               :: Id -> Bool
327 isDataConWorkId         :: Id -> Bool
328
329 isClassOpId_maybe       :: Id -> Maybe Class
330 isPrimOpId_maybe        :: Id -> Maybe PrimOp
331 isFCallId_maybe         :: Id -> Maybe ForeignCall
332 isDataConWorkId_maybe   :: Id -> Maybe DataCon
333
334 isRecordSelector id = case Var.idDetails id of
335                         RecSelId {}  -> True
336                         _               -> False
337
338 isNaughtyRecordSelector id = case Var.idDetails id of
339                         RecSelId { sel_naughty = n } -> n
340                         _                               -> False
341
342 isClassOpId_maybe id = case Var.idDetails id of
343                         ClassOpId cls -> Just cls
344                         _other        -> Nothing
345
346 isPrimOpId id = case Var.idDetails id of
347                         PrimOpId _ -> True
348                         _          -> False
349
350 isPrimOpId_maybe id = case Var.idDetails id of
351                         PrimOpId op -> Just op
352                         _           -> Nothing
353
354 isFCallId id = case Var.idDetails id of
355                         FCallId _ -> True
356                         _         -> False
357
358 isFCallId_maybe id = case Var.idDetails id of
359                         FCallId call -> Just call
360                         _            -> Nothing
361
362 isDataConWorkId id = case Var.idDetails id of
363                         DataConWorkId _ -> True
364                         _               -> False
365
366 isDataConWorkId_maybe id = case Var.idDetails id of
367                         DataConWorkId con -> Just con
368                         _                 -> Nothing
369
370 isDataConId_maybe :: Id -> Maybe DataCon
371 isDataConId_maybe id = case Var.idDetails id of
372                          DataConWorkId con -> Just con
373                          DataConWrapId con -> Just con
374                          _                 -> Nothing
375
376 idDataCon :: Id -> DataCon
377 -- ^ Get from either the worker or the wrapper 'Id' to the 'DataCon'. Currently used only in the desugarer.
378 --
379 -- INVARIANT: @idDataCon (dataConWrapId d) = d@: remember, 'dataConWrapId' can return either the wrapper or the worker
380 idDataCon id = isDataConId_maybe id `orElse` pprPanic "idDataCon" (ppr id)
381
382
383 isDictId :: Id -> Bool
384 isDictId id = isDictTy (idType id)
385
386 hasNoBinding :: Id -> Bool
387 -- ^ Returns @True@ of an 'Id' which may not have a
388 -- binding, even though it is defined in this module.
389
390 -- Data constructor workers used to be things of this kind, but
391 -- they aren't any more.  Instead, we inject a binding for 
392 -- them at the CorePrep stage. 
393 -- EXCEPT: unboxed tuples, which definitely have no binding
394 hasNoBinding id = case Var.idDetails id of
395                         PrimOpId _       -> True        -- See Note [Primop wrappers]
396                         FCallId _        -> True
397                         DataConWorkId dc -> isUnboxedTupleCon dc
398                         _                -> False
399
400 isImplicitId :: Id -> Bool
401 -- ^ 'isImplicitId' tells whether an 'Id's info is implied by other
402 -- declarations, so we don't need to put its signature in an interface
403 -- file, even if it's mentioned in some other interface unfolding.
404 isImplicitId id
405   = case Var.idDetails id of
406         FCallId _       -> True
407         ClassOpId _     -> True
408         PrimOpId _      -> True
409         DataConWorkId _ -> True
410         DataConWrapId _ -> True
411                 -- These are are implied by their type or class decl;
412                 -- remember that all type and class decls appear in the interface file.
413                 -- The dfun id is not an implicit Id; it must *not* be omitted, because 
414                 -- it carries version info for the instance decl
415         _               -> False
416
417 idIsFrom :: Module -> Id -> Bool
418 idIsFrom mod id = nameIsLocalOrFrom mod (idName id)
419 \end{code}
420
421 Note [Primop wrappers]
422 ~~~~~~~~~~~~~~~~~~~~~~
423 Currently hasNoBinding claims that PrimOpIds don't have a curried
424 function definition.  But actually they do, in GHC.PrimopWrappers,
425 which is auto-generated from prelude/primops.txt.pp.  So actually, hasNoBinding
426 could return 'False' for PrimOpIds.
427
428 But we'd need to add something in CoreToStg to swizzle any unsaturated
429 applications of GHC.Prim.plusInt# to GHC.PrimopWrappers.plusInt#.
430
431 Nota Bene: GHC.PrimopWrappers is needed *regardless*, because it's
432 used by GHCi, which does not implement primops direct at all.
433
434
435
436 \begin{code}
437 isDeadBinder :: Id -> Bool
438 isDeadBinder bndr | isId bndr = isDeadOcc (idOccInfo bndr)
439                   | otherwise = False   -- TyVars count as not dead
440 \end{code}
441
442 \begin{code}
443 isTickBoxOp :: Id -> Bool
444 isTickBoxOp id = 
445   case Var.idDetails id of
446     TickBoxOpId _    -> True
447     _                -> False
448
449 isTickBoxOp_maybe :: Id -> Maybe TickBoxOp
450 isTickBoxOp_maybe id = 
451   case Var.idDetails id of
452     TickBoxOpId tick -> Just tick
453     _                -> Nothing
454 \end{code}
455
456 %************************************************************************
457 %*                                                                      *
458 \subsection{IdInfo stuff}
459 %*                                                                      *
460 %************************************************************************
461
462 \begin{code}
463         ---------------------------------
464         -- ARITY
465 idArity :: Id -> Arity
466 idArity id = arityInfo (idInfo id)
467
468 setIdArity :: Id -> Arity -> Id
469 setIdArity id arity = modifyIdInfo (`setArityInfo` arity) id
470
471 #ifdef OLD_STRICTNESS
472         ---------------------------------
473         -- (OLD) STRICTNESS 
474 idStrictness :: Id -> StrictnessInfo
475 idStrictness id = strictnessInfo (idInfo id)
476
477 setIdStrictness :: Id -> StrictnessInfo -> Id
478 setIdStrictness id strict_info = modifyIdInfo (`setStrictnessInfo` strict_info) id
479 #endif
480
481 -- | Returns true if an application to n args would diverge
482 isBottomingId :: Id -> Bool
483 isBottomingId id = isBottomingSig (idNewStrictness id)
484
485 idNewStrictness_maybe :: Id -> Maybe StrictSig
486 idNewStrictness :: Id -> StrictSig
487
488 idNewStrictness_maybe id = newStrictnessInfo (idInfo id)
489 idNewStrictness       id = idNewStrictness_maybe id `orElse` topSig
490
491 setIdNewStrictness :: Id -> StrictSig -> Id
492 setIdNewStrictness id sig = modifyIdInfo (`setNewStrictnessInfo` Just sig) id
493
494 zapIdNewStrictness :: Id -> Id
495 zapIdNewStrictness id = modifyIdInfo (`setNewStrictnessInfo` Nothing) id
496
497 -- | This predicate says whether the 'Id' has a strict demand placed on it or
498 -- has a type such that it can always be evaluated strictly (e.g., an
499 -- unlifted type, but see the comment for 'isStrictType').  We need to
500 -- check separately whether the 'Id' has a so-called \"strict type\" because if
501 -- the demand for the given @id@ hasn't been computed yet but @id@ has a strict
502 -- type, we still want @isStrictId id@ to be @True@.
503 isStrictId :: Id -> Bool
504 isStrictId id
505   = ASSERT2( isId id, text "isStrictId: not an id: " <+> ppr id )
506            (isStrictDmd (idNewDemandInfo id)) || 
507            (isStrictType (idType id))
508
509         ---------------------------------
510         -- WORKER ID
511 idWorkerInfo :: Id -> WorkerInfo
512 idWorkerInfo id = workerInfo (idInfo id)
513
514 setIdWorkerInfo :: Id -> WorkerInfo -> Id
515 setIdWorkerInfo id work_info = modifyIdInfo (`setWorkerInfo` work_info) id
516
517         ---------------------------------
518         -- UNFOLDING
519 idUnfolding :: Id -> Unfolding
520 idUnfolding id = unfoldingInfo (idInfo id)
521
522 setIdUnfolding :: Id -> Unfolding -> Id
523 setIdUnfolding id unfolding = modifyIdInfo (`setUnfoldingInfo` unfolding) id
524
525 #ifdef OLD_STRICTNESS
526         ---------------------------------
527         -- (OLD) DEMAND
528 idDemandInfo :: Id -> Demand.Demand
529 idDemandInfo id = demandInfo (idInfo id)
530
531 setIdDemandInfo :: Id -> Demand.Demand -> Id
532 setIdDemandInfo id demand_info = modifyIdInfo (`setDemandInfo` demand_info) id
533 #endif
534
535 idNewDemandInfo_maybe :: Id -> Maybe NewDemand.Demand
536 idNewDemandInfo       :: Id -> NewDemand.Demand
537
538 idNewDemandInfo_maybe id = newDemandInfo (idInfo id)
539 idNewDemandInfo       id = newDemandInfo (idInfo id) `orElse` NewDemand.topDmd
540
541 setIdNewDemandInfo :: Id -> NewDemand.Demand -> Id
542 setIdNewDemandInfo id dmd = modifyIdInfo (`setNewDemandInfo` Just dmd) id
543
544         ---------------------------------
545         -- SPECIALISATION
546 idSpecialisation :: Id -> SpecInfo
547 idSpecialisation id = specInfo (idInfo id)
548
549 idCoreRules :: Id -> [CoreRule]
550 idCoreRules id = specInfoRules (idSpecialisation id)
551
552 idHasRules :: Id -> Bool
553 idHasRules id = not (isEmptySpecInfo (idSpecialisation id))
554
555 setIdSpecialisation :: Id -> SpecInfo -> Id
556 setIdSpecialisation id spec_info = modifyIdInfo (`setSpecInfo` spec_info) id
557
558         ---------------------------------
559         -- CAF INFO
560 idCafInfo :: Id -> CafInfo
561 #ifdef OLD_STRICTNESS
562 idCafInfo id = case cgInfo (idInfo id) of
563                   NoCgInfo -> pprPanic "idCafInfo" (ppr id)
564                   info     -> cgCafInfo info
565 #else
566 idCafInfo id = cafInfo (idInfo id)
567 #endif
568
569 setIdCafInfo :: Id -> CafInfo -> Id
570 setIdCafInfo id caf_info = modifyIdInfo (`setCafInfo` caf_info) id
571
572         ---------------------------------
573         -- CPR INFO
574 #ifdef OLD_STRICTNESS
575 idCprInfo :: Id -> CprInfo
576 idCprInfo id = cprInfo (idInfo id)
577
578 setIdCprInfo :: Id -> CprInfo -> Id
579 setIdCprInfo id cpr_info = modifyIdInfo (`setCprInfo` cpr_info) id
580 #endif
581
582         ---------------------------------
583         -- Occcurrence INFO
584 idOccInfo :: Id -> OccInfo
585 idOccInfo id = occInfo (idInfo id)
586
587 setIdOccInfo :: Id -> OccInfo -> Id
588 setIdOccInfo id occ_info = modifyIdInfo (`setOccInfo` occ_info) id
589
590 zapIdOccInfo :: Id -> Id
591 zapIdOccInfo b = b `setIdOccInfo` NoOccInfo
592 \end{code}
593
594
595         ---------------------------------
596         -- INLINING
597 The inline pragma tells us to be very keen to inline this Id, but it's still
598 OK not to if optimisation is switched off.
599
600 \begin{code}
601 idInlinePragma :: Id -> InlinePragInfo
602 idInlinePragma id = inlinePragInfo (idInfo id)
603
604 setInlinePragma :: Id -> InlinePragInfo -> Id
605 setInlinePragma id prag = modifyIdInfo (`setInlinePragInfo` prag) id
606
607 modifyInlinePragma :: Id -> (InlinePragInfo -> InlinePragInfo) -> Id
608 modifyInlinePragma id fn = modifyIdInfo (\info -> info `setInlinePragInfo` (fn (inlinePragInfo info))) id
609 \end{code}
610
611
612         ---------------------------------
613         -- ONE-SHOT LAMBDAS
614 \begin{code}
615 idLBVarInfo :: Id -> LBVarInfo
616 idLBVarInfo id = lbvarInfo (idInfo id)
617
618 -- | Returns whether the lambda associated with the 'Id' is certainly applied at most once
619 -- OR we are applying the \"state hack\" which makes it appear as if theis is the case for
620 -- lambdas used in @IO@. You should prefer using this over 'isOneShotLambda'
621 isOneShotBndr :: Id -> Bool
622 -- This one is the "business end", called externally.
623 -- Its main purpose is to encapsulate the Horrible State Hack
624 isOneShotBndr id = isOneShotLambda id || isStateHackType (idType id)
625
626 -- | Should we apply the state hack to values of this 'Type'?
627 isStateHackType :: Type -> Bool
628 isStateHackType ty
629   | opt_NoStateHack 
630   = False
631   | otherwise
632   = case splitTyConApp_maybe ty of
633         Just (tycon,_) -> tycon == statePrimTyCon
634         _              -> False
635         -- This is a gross hack.  It claims that 
636         -- every function over realWorldStatePrimTy is a one-shot
637         -- function.  This is pretty true in practice, and makes a big
638         -- difference.  For example, consider
639         --      a `thenST` \ r -> ...E...
640         -- The early full laziness pass, if it doesn't know that r is one-shot
641         -- will pull out E (let's say it doesn't mention r) to give
642         --      let lvl = E in a `thenST` \ r -> ...lvl...
643         -- When `thenST` gets inlined, we end up with
644         --      let lvl = E in \s -> case a s of (r, s') -> ...lvl...
645         -- and we don't re-inline E.
646         --
647         -- It would be better to spot that r was one-shot to start with, but
648         -- I don't want to rely on that.
649         --
650         -- Another good example is in fill_in in PrelPack.lhs.  We should be able to
651         -- spot that fill_in has arity 2 (and when Keith is done, we will) but we can't yet.
652
653
654 -- | Returns whether the lambda associated with the 'Id' is certainly applied at most once.
655 -- You probably want to use 'isOneShotBndr' instead
656 isOneShotLambda :: Id -> Bool
657 isOneShotLambda id = case idLBVarInfo id of
658                        IsOneShotLambda  -> True
659                        NoLBVarInfo      -> False
660
661 setOneShotLambda :: Id -> Id
662 setOneShotLambda id = modifyIdInfo (`setLBVarInfo` IsOneShotLambda) id
663
664 clearOneShotLambda :: Id -> Id
665 clearOneShotLambda id 
666   | isOneShotLambda id = modifyIdInfo (`setLBVarInfo` NoLBVarInfo) id
667   | otherwise          = id                     
668
669 -- The OneShotLambda functions simply fiddle with the IdInfo flag
670 -- But watch out: this may change the type of something else
671 --      f = \x -> e
672 -- If we change the one-shot-ness of x, f's type changes
673 \end{code}
674
675 \begin{code}
676 zapInfo :: (IdInfo -> Maybe IdInfo) -> Id -> Id
677 zapInfo zapper id = maybeModifyIdInfo (zapper (idInfo id)) id
678
679 zapLamIdInfo :: Id -> Id
680 zapLamIdInfo = zapInfo zapLamInfo
681
682 zapDemandIdInfo :: Id -> Id
683 zapDemandIdInfo = zapInfo zapDemandInfo
684
685 zapFragileIdInfo :: Id -> Id
686 zapFragileIdInfo = zapInfo zapFragileInfo 
687 \end{code}
688
689 Note [transferPolyIdInfo]
690 ~~~~~~~~~~~~~~~~~~~~~~~~~
691 Suppose we have
692
693    f = /\a. let g = rhs in ...
694
695 where g has interesting strictness information.  Then if we float thus
696
697    g' = /\a. rhs
698    f = /\a. ...[g' a/g]
699
700 we *do not* want to lose the strictness information on g.  Nor arity.
701
702 It's simple to retain strictness and arity, but not so simple to retain
703         worker info
704         rules
705 so we simply discard those.  Sooner or later this may bite us.
706
707 This transfer is used in two places: 
708         FloatOut (long-distance let-floating)
709         SimplUtils.abstractFloats (short-distance let-floating)
710
711 \begin{code}
712 transferPolyIdInfo :: Id -> Id -> Id
713 transferPolyIdInfo old_id new_id
714   = modifyIdInfo transfer new_id
715   where
716     old_info = idInfo old_id
717     transfer new_info = new_info `setNewStrictnessInfo` (newStrictnessInfo old_info)
718                                  `setArityInfo` (arityInfo old_info)
719 \end{code}