Add kind equalities to GHC.
[ghc.git] / compiler / basicTypes / MkId.hs
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
15 {-# LANGUAGE CPP #-}
16
17 module MkId (
18 mkDictFunId, mkDictFunTy, mkDictSelId, mkDictSelRhs,
19
20 mkPrimOpId, mkFCallId,
21
22 wrapNewTypeBody, unwrapNewTypeBody,
23 wrapFamInstBody, unwrapFamInstScrut,
24 wrapTypeUnbranchedFamInstBody, unwrapTypeUnbranchedFamInstScrut,
25
26 DataConBoxer(..), mkDataConRep, mkDataConWorkId,
27
28 -- And some particular Ids; see below for why they are wired in
29 wiredInIds, ghcPrimIds,
30 unsafeCoerceName, unsafeCoerceId, realWorldPrimId,
31 voidPrimId, voidArgId,
32 nullAddrId, seqId, lazyId, lazyIdKey, runRWId,
33 coercionTokenId, magicDictId, coerceId,
34 proxyHashId,
35
36 -- Re-export error Ids
37 module PrelRules
38 ) where
39
40 #include "HsVersions.h"
41
42 import Rules
43 import TysPrim
44 import TysWiredIn
45 import PrelRules
46 import Type
47 import FamInstEnv
48 import Coercion
49 import TcType
50 import MkCore
51 import CoreUtils ( exprType, mkCast )
52 import CoreUnfold
53 import Literal
54 import TyCon
55 import CoAxiom
56 import Class
57 import NameSet
58 import VarSet
59 import Name
60 import PrimOp
61 import ForeignCall
62 import DataCon
63 import Id
64 import IdInfo
65 import Demand
66 import CoreSyn
67 import Unique
68 import UniqSupply
69 import PrelNames
70 import BasicTypes hiding ( SuccessFlag(..) )
71 import Util
72 import Pair
73 import DynFlags
74 import Outputable
75 import FastString
76 import ListSetOps
77
78 import Data.Maybe ( maybeToList )
79
80 {-
81 ************************************************************************
82 * *
83 \subsection{Wired in Ids}
84 * *
85 ************************************************************************
86
87 Note [Wired-in Ids]
88 ~~~~~~~~~~~~~~~~~~~
89 There are several reasons why an Id might appear in the wiredInIds:
90
91 (1) The ghcPrimIds are wired in because they can't be defined in
92 Haskell at all, although the can be defined in Core. They have
93 compulsory unfoldings, so they are always inlined and they have
94 no definition site. Their home module is GHC.Prim, so they
95 also have a description in primops.txt.pp, where they are called
96 'pseudoops'.
97
98 (2) The 'error' function, eRROR_ID, is wired in because we don't yet have
99 a way to express in an interface file that the result type variable
100 is 'open'; that is can be unified with an unboxed type
101
102 [The interface file format now carry such information, but there's
103 no way yet of expressing at the definition site for these
104 error-reporting functions that they have an 'open'
105 result type. -- sof 1/99]
106
107 (3) Other error functions (rUNTIME_ERROR_ID) are wired in (a) because
108 the desugarer generates code that mentions them directly, and
109 (b) for the same reason as eRROR_ID
110
111 (4) lazyId is wired in because the wired-in version overrides the
112 strictness of the version defined in GHC.Base
113
114 In cases (2-4), the function has a definition in a library module, and
115 can be called; but the wired-in version means that the details are
116 never read from that module's interface file; instead, the full definition
117 is right here.
118 -}
119
120 wiredInIds :: [Id]
121 wiredInIds
122 = [lazyId, dollarId, oneShotId, runRWId]
123 ++ errorIds -- Defined in MkCore
124 ++ ghcPrimIds
125
126 -- These Ids are exported from GHC.Prim
127 ghcPrimIds :: [Id]
128 ghcPrimIds
129 = [ -- These can't be defined in Haskell, but they have
130 -- perfectly reasonable unfoldings in Core
131 realWorldPrimId,
132 voidPrimId,
133 unsafeCoerceId,
134 nullAddrId,
135 seqId,
136 magicDictId,
137 coerceId,
138 proxyHashId
139 ]
140
141 {-
142 ************************************************************************
143 * *
144 \subsection{Data constructors}
145 * *
146 ************************************************************************
147
148 The wrapper for a constructor is an ordinary top-level binding that evaluates
149 any strict args, unboxes any args that are going to be flattened, and calls
150 the worker.
151
152 We're going to build a constructor that looks like:
153
154 data (Data a, C b) => T a b = T1 !a !Int b
155
156 T1 = /\ a b ->
157 \d1::Data a, d2::C b ->
158 \p q r -> case p of { p ->
159 case q of { q ->
160 Con T1 [a,b] [p,q,r]}}
161
162 Notice that
163
164 * d2 is thrown away --- a context in a data decl is used to make sure
165 one *could* construct dictionaries at the site the constructor
166 is used, but the dictionary isn't actually used.
167
168 * We have to check that we can construct Data dictionaries for
169 the types a and Int. Once we've done that we can throw d1 away too.
170
171 * We use (case p of q -> ...) to evaluate p, rather than "seq" because
172 all that matters is that the arguments are evaluated. "seq" is
173 very careful to preserve evaluation order, which we don't need
174 to be here.
175
176 You might think that we could simply give constructors some strictness
177 info, like PrimOps, and let CoreToStg do the let-to-case transformation.
178 But we don't do that because in the case of primops and functions strictness
179 is a *property* not a *requirement*. In the case of constructors we need to
180 do something active to evaluate the argument.
181
182 Making an explicit case expression allows the simplifier to eliminate
183 it in the (common) case where the constructor arg is already evaluated.
184
185 Note [Wrappers for data instance tycons]
186 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187 In the case of data instances, the wrapper also applies the coercion turning
188 the representation type into the family instance type to cast the result of
189 the wrapper. For example, consider the declarations
190
191 data family Map k :: * -> *
192 data instance Map (a, b) v = MapPair (Map a (Pair b v))
193
194 The tycon to which the datacon MapPair belongs gets a unique internal
195 name of the form :R123Map, and we call it the representation tycon.
196 In contrast, Map is the family tycon (accessible via
197 tyConFamInst_maybe). A coercion allows you to move between
198 representation and family type. It is accessible from :R123Map via
199 tyConFamilyCoercion_maybe and has kind
200
201 Co123Map a b v :: {Map (a, b) v ~ :R123Map a b v}
202
203 The wrapper and worker of MapPair get the types
204
205 -- Wrapper
206 $WMapPair :: forall a b v. Map a (Map a b v) -> Map (a, b) v
207 $WMapPair a b v = MapPair a b v `cast` sym (Co123Map a b v)
208
209 -- Worker
210 MapPair :: forall a b v. Map a (Map a b v) -> :R123Map a b v
211
212 This coercion is conditionally applied by wrapFamInstBody.
213
214 It's a bit more complicated if the data instance is a GADT as well!
215
216 data instance T [a] where
217 T1 :: forall b. b -> T [Maybe b]
218
219 Hence we translate to
220
221 -- Wrapper
222 $WT1 :: forall b. b -> T [Maybe b]
223 $WT1 b v = T1 (Maybe b) b (Maybe b) v
224 `cast` sym (Co7T (Maybe b))
225
226 -- Worker
227 T1 :: forall c b. (c ~ Maybe b) => b -> :R7T c
228
229 -- Coercion from family type to representation type
230 Co7T a :: T [a] ~ :R7T a
231
232 Note [Newtype datacons]
233 ~~~~~~~~~~~~~~~~~~~~~~~
234 The "data constructor" for a newtype should always be vanilla. At one
235 point this wasn't true, because the newtype arising from
236 class C a => D a
237 looked like
238 newtype T:D a = D:D (C a)
239 so the data constructor for T:C had a single argument, namely the
240 predicate (C a). But now we treat that as an ordinary argument, not
241 part of the theta-type, so all is well.
242
243
244 ************************************************************************
245 * *
246 \subsection{Dictionary selectors}
247 * *
248 ************************************************************************
249
250 Selecting a field for a dictionary. If there is just one field, then
251 there's nothing to do.
252
253 Dictionary selectors may get nested forall-types. Thus:
254
255 class Foo a where
256 op :: forall b. Ord b => a -> b -> b
257
258 Then the top-level type for op is
259
260 op :: forall a. Foo a =>
261 forall b. Ord b =>
262 a -> b -> b
263
264 This is unlike ordinary record selectors, which have all the for-alls
265 at the outside. When dealing with classes it's very convenient to
266 recover the original type signature from the class op selector.
267 -}
268
269 mkDictSelId :: Name -- Name of one of the *value* selectors
270 -- (dictionary superclass or method)
271 -> Class -> Id
272 mkDictSelId name clas
273 = mkGlobalId (ClassOpId clas) name sel_ty info
274 where
275 tycon = classTyCon clas
276 sel_names = map idName (classAllSelIds clas)
277 new_tycon = isNewTyCon tycon
278 [data_con] = tyConDataCons tycon
279 tyvars = dataConUnivTyVars data_con
280 arg_tys = dataConRepArgTys data_con -- Includes the dictionary superclasses
281 val_index = assoc "MkId.mkDictSelId" (sel_names `zip` [0..]) name
282
283 sel_ty = mkInvForAllTys tyvars (mkFunTy (mkClassPred clas (mkTyVarTys tyvars))
284 (getNth arg_tys val_index))
285
286 base_info = noCafIdInfo
287 `setArityInfo` 1
288 `setStrictnessInfo` strict_sig
289
290 info | new_tycon
291 = base_info `setInlinePragInfo` alwaysInlinePragma
292 `setUnfoldingInfo` mkInlineUnfolding (Just 1) (mkDictSelRhs clas val_index)
293 -- See Note [Single-method classes] in TcInstDcls
294 -- for why alwaysInlinePragma
295
296 | otherwise
297 = base_info `setRuleInfo` mkRuleInfo [rule]
298 -- Add a magic BuiltinRule, but no unfolding
299 -- so that the rule is always available to fire.
300 -- See Note [ClassOp/DFun selection] in TcInstDcls
301
302 n_ty_args = length tyvars
303
304 -- This is the built-in rule that goes
305 -- op (dfT d1 d2) ---> opT d1 d2
306 rule = BuiltinRule { ru_name = fsLit "Class op " `appendFS`
307 occNameFS (getOccName name)
308 , ru_fn = name
309 , ru_nargs = n_ty_args + 1
310 , ru_try = dictSelRule val_index n_ty_args }
311
312 -- The strictness signature is of the form U(AAAVAAAA) -> T
313 -- where the V depends on which item we are selecting
314 -- It's worth giving one, so that absence info etc is generated
315 -- even if the selector isn't inlined
316
317 strict_sig = mkClosedStrictSig [arg_dmd] topRes
318 arg_dmd | new_tycon = evalDmd
319 | otherwise = mkManyUsedDmd $
320 mkProdDmd [ if name == sel_name then evalDmd else absDmd
321 | sel_name <- sel_names ]
322
323 mkDictSelRhs :: Class
324 -> Int -- 0-indexed selector among (superclasses ++ methods)
325 -> CoreExpr
326 mkDictSelRhs clas val_index
327 = mkLams tyvars (Lam dict_id rhs_body)
328 where
329 tycon = classTyCon clas
330 new_tycon = isNewTyCon tycon
331 [data_con] = tyConDataCons tycon
332 tyvars = dataConUnivTyVars data_con
333 arg_tys = dataConRepArgTys data_con -- Includes the dictionary superclasses
334
335 the_arg_id = getNth arg_ids val_index
336 pred = mkClassPred clas (mkTyVarTys tyvars)
337 dict_id = mkTemplateLocal 1 pred
338 arg_ids = mkTemplateLocalsNum 2 arg_tys
339
340 rhs_body | new_tycon = unwrapNewTypeBody tycon (mkTyVarTys tyvars) (Var dict_id)
341 | otherwise = Case (Var dict_id) dict_id (idType the_arg_id)
342 [(DataAlt data_con, arg_ids, varToCoreExpr the_arg_id)]
343 -- varToCoreExpr needed for equality superclass selectors
344 -- sel a b d = case x of { MkC _ (g:a~b) _ -> CO g }
345
346 dictSelRule :: Int -> Arity -> RuleFun
347 -- Tries to persuade the argument to look like a constructor
348 -- application, using exprIsConApp_maybe, and then selects
349 -- from it
350 -- sel_i t1..tk (D t1..tk op1 ... opm) = opi
351 --
352 dictSelRule val_index n_ty_args _ id_unf _ args
353 | (dict_arg : _) <- drop n_ty_args args
354 , Just (_, _, con_args) <- exprIsConApp_maybe id_unf dict_arg
355 = Just (getNth con_args val_index)
356 | otherwise
357 = Nothing
358
359 {-
360 ************************************************************************
361 * *
362 Data constructors
363 * *
364 ************************************************************************
365 -}
366
367 mkDataConWorkId :: Name -> DataCon -> Id
368 mkDataConWorkId wkr_name data_con
369 | isNewTyCon tycon
370 = mkGlobalId (DataConWrapId data_con) wkr_name nt_wrap_ty nt_work_info
371 | otherwise
372 = mkGlobalId (DataConWorkId data_con) wkr_name alg_wkr_ty wkr_info
373
374 where
375 tycon = dataConTyCon data_con
376
377 ----------- Workers for data types --------------
378 alg_wkr_ty = dataConRepType data_con
379 wkr_arity = dataConRepArity data_con
380 wkr_info = noCafIdInfo
381 `setArityInfo` wkr_arity
382 `setStrictnessInfo` wkr_sig
383 `setUnfoldingInfo` evaldUnfolding -- Record that it's evaluated,
384 -- even if arity = 0
385
386 wkr_sig = mkClosedStrictSig (replicate wkr_arity topDmd) (dataConCPR data_con)
387 -- Note [Data-con worker strictness]
388 -- Notice that we do *not* say the worker is strict
389 -- even if the data constructor is declared strict
390 -- e.g. data T = MkT !(Int,Int)
391 -- Why? Because the *wrapper* is strict (and its unfolding has case
392 -- expressions that do the evals) but the *worker* itself is not.
393 -- If we pretend it is strict then when we see
394 -- case x of y -> $wMkT y
395 -- the simplifier thinks that y is "sure to be evaluated" (because
396 -- $wMkT is strict) and drops the case. No, $wMkT is not strict.
397 --
398 -- When the simplifer sees a pattern
399 -- case e of MkT x -> ...
400 -- it uses the dataConRepStrictness of MkT to mark x as evaluated;
401 -- but that's fine... dataConRepStrictness comes from the data con
402 -- not from the worker Id.
403
404 ----------- Workers for newtypes --------------
405 (nt_tvs, _, nt_arg_tys, _) = dataConSig data_con
406 res_ty_args = mkTyVarTys nt_tvs
407 nt_wrap_ty = dataConUserType data_con
408 nt_work_info = noCafIdInfo -- The NoCaf-ness is set by noCafIdInfo
409 `setArityInfo` 1 -- Arity 1
410 `setInlinePragInfo` alwaysInlinePragma
411 `setUnfoldingInfo` newtype_unf
412 id_arg1 = mkTemplateLocal 1 (head nt_arg_tys)
413 newtype_unf = ASSERT2( isVanillaDataCon data_con &&
414 isSingleton nt_arg_tys, ppr data_con )
415 -- Note [Newtype datacons]
416 mkCompulsoryUnfolding $
417 mkLams nt_tvs $ Lam id_arg1 $
418 wrapNewTypeBody tycon res_ty_args (Var id_arg1)
419
420 dataConCPR :: DataCon -> DmdResult
421 dataConCPR con
422 | isDataTyCon tycon -- Real data types only; that is,
423 -- not unboxed tuples or newtypes
424 , null (dataConExTyVars con) -- No existentials
425 , wkr_arity > 0
426 , wkr_arity <= mAX_CPR_SIZE
427 = if is_prod then vanillaCprProdRes (dataConRepArity con)
428 else cprSumRes (dataConTag con)
429 | otherwise
430 = topRes
431 where
432 is_prod = isProductTyCon tycon
433 tycon = dataConTyCon con
434 wkr_arity = dataConRepArity con
435
436 mAX_CPR_SIZE :: Arity
437 mAX_CPR_SIZE = 10
438 -- We do not treat very big tuples as CPR-ish:
439 -- a) for a start we get into trouble because there aren't
440 -- "enough" unboxed tuple types (a tiresome restriction,
441 -- but hard to fix),
442 -- b) more importantly, big unboxed tuples get returned mainly
443 -- on the stack, and are often then allocated in the heap
444 -- by the caller. So doing CPR for them may in fact make
445 -- things worse.
446
447 {-
448 -------------------------------------------------
449 -- Data constructor representation
450 --
451 -- This is where we decide how to wrap/unwrap the
452 -- constructor fields
453 --
454 --------------------------------------------------
455 -}
456
457 type Unboxer = Var -> UniqSM ([Var], CoreExpr -> CoreExpr)
458 -- Unbox: bind rep vars by decomposing src var
459
460 data Boxer = UnitBox | Boxer (TCvSubst -> UniqSM ([Var], CoreExpr))
461 -- Box: build src arg using these rep vars
462
463 newtype DataConBoxer = DCB ([Type] -> [Var] -> UniqSM ([Var], [CoreBind]))
464 -- Bind these src-level vars, returning the
465 -- rep-level vars to bind in the pattern
466
467 mkDataConRep :: DynFlags
468 -> FamInstEnvs
469 -> Name
470 -> Maybe [HsImplBang]
471 -- See Note [Bangs on imported data constructors]
472 -> DataCon
473 -> UniqSM DataConRep
474 mkDataConRep dflags fam_envs wrap_name mb_bangs data_con
475 | not wrapper_reqd
476 = return NoDataConRep
477
478 | otherwise
479 = do { wrap_args <- mapM newLocal wrap_arg_tys
480 ; wrap_body <- mk_rep_app (wrap_args `zip` dropList eq_spec unboxers)
481 initial_wrap_app
482
483 ; let wrap_id = mkGlobalId (DataConWrapId data_con) wrap_name wrap_ty wrap_info
484 wrap_info = noCafIdInfo
485 `setArityInfo` wrap_arity
486 -- It's important to specify the arity, so that partial
487 -- applications are treated as values
488 `setInlinePragInfo` alwaysInlinePragma
489 `setUnfoldingInfo` wrap_unf
490 `setStrictnessInfo` wrap_sig
491 -- We need to get the CAF info right here because TidyPgm
492 -- does not tidy the IdInfo of implicit bindings (like the wrapper)
493 -- so it not make sure that the CAF info is sane
494
495 wrap_sig = mkClosedStrictSig wrap_arg_dmds (dataConCPR data_con)
496 wrap_arg_dmds = map mk_dmd arg_ibangs
497 mk_dmd str | isBanged str = evalDmd
498 | otherwise = topDmd
499 -- The Cpr info can be important inside INLINE rhss, where the
500 -- wrapper constructor isn't inlined.
501 -- And the argument strictness can be important too; we
502 -- may not inline a contructor when it is partially applied.
503 -- For example:
504 -- data W = C !Int !Int !Int
505 -- ...(let w = C x in ...(w p q)...)...
506 -- we want to see that w is strict in its two arguments
507
508 wrap_unf = mkInlineUnfolding (Just wrap_arity) wrap_rhs
509 wrap_tvs = (univ_tvs `minusList` map eqSpecTyVar eq_spec) ++ ex_tvs
510 wrap_rhs = mkLams wrap_tvs $
511 mkLams wrap_args $
512 wrapFamInstBody tycon res_ty_args $
513 wrap_body
514
515 ; return (DCR { dcr_wrap_id = wrap_id
516 , dcr_boxer = mk_boxer boxers
517 , dcr_arg_tys = rep_tys
518 , dcr_stricts = rep_strs
519 , dcr_bangs = arg_ibangs }) }
520
521 where
522 (univ_tvs, ex_tvs, eq_spec, theta, orig_arg_tys, _orig_res_ty)
523 = dataConFullSig data_con
524 res_ty_args = substTyVars (mkTopTCvSubst (map eqSpecPair eq_spec)) univ_tvs
525
526 tycon = dataConTyCon data_con -- The representation TyCon (not family)
527 wrap_ty = dataConUserType data_con
528 ev_tys = eqSpecPreds eq_spec ++ theta
529 all_arg_tys = ev_tys ++ orig_arg_tys
530 ev_ibangs = map (const HsLazy) ev_tys
531 orig_bangs = dataConSrcBangs data_con
532
533 wrap_arg_tys = theta ++ orig_arg_tys
534 wrap_arity = length wrap_arg_tys
535 -- The wrap_args are the arguments *other than* the eq_spec
536 -- Because we are going to apply the eq_spec args manually in the
537 -- wrapper
538
539 arg_ibangs =
540 case mb_bangs of
541 Nothing -> zipWith (dataConSrcToImplBang dflags fam_envs)
542 orig_arg_tys orig_bangs
543 Just bangs -> bangs
544
545 (rep_tys_w_strs, wrappers)
546 = unzip (zipWith dataConArgRep all_arg_tys (ev_ibangs ++ arg_ibangs))
547
548 (unboxers, boxers) = unzip wrappers
549 (rep_tys, rep_strs) = unzip (concat rep_tys_w_strs)
550
551 wrapper_reqd = not (isNewTyCon tycon) -- Newtypes have only a worker
552 && (any isBanged (ev_ibangs ++ arg_ibangs)
553 -- Some forcing/unboxing (includes eq_spec)
554 || isFamInstTyCon tycon -- Cast result
555 || (not $ null eq_spec)) -- GADT
556
557 initial_wrap_app = Var (dataConWorkId data_con)
558 `mkTyApps` res_ty_args
559 `mkVarApps` ex_tvs
560 `mkCoApps` map (mkReflCo Nominal . eqSpecType) eq_spec
561
562 mk_boxer :: [Boxer] -> DataConBoxer
563 mk_boxer boxers = DCB (\ ty_args src_vars ->
564 do { let (ex_vars, term_vars) = splitAtList ex_tvs src_vars
565 subst1 = mkTopTCvSubst (univ_tvs `zip` ty_args)
566 subst2 = extendTCvSubstList subst1 ex_tvs
567 (mkTyVarTys ex_vars)
568 ; (rep_ids, binds) <- go subst2 boxers term_vars
569 ; return (ex_vars ++ rep_ids, binds) } )
570
571 go _ [] src_vars = ASSERT2( null src_vars, ppr data_con ) return ([], [])
572 go subst (UnitBox : boxers) (src_var : src_vars)
573 = do { (rep_ids2, binds) <- go subst boxers src_vars
574 ; return (src_var : rep_ids2, binds) }
575 go subst (Boxer boxer : boxers) (src_var : src_vars)
576 = do { (rep_ids1, arg) <- boxer subst
577 ; (rep_ids2, binds) <- go subst boxers src_vars
578 ; return (rep_ids1 ++ rep_ids2, NonRec src_var arg : binds) }
579 go _ (_:_) [] = pprPanic "mk_boxer" (ppr data_con)
580
581 mk_rep_app :: [(Id,Unboxer)] -> CoreExpr -> UniqSM CoreExpr
582 mk_rep_app [] con_app
583 = return con_app
584 mk_rep_app ((wrap_arg, unboxer) : prs) con_app
585 = do { (rep_ids, unbox_fn) <- unboxer wrap_arg
586 ; expr <- mk_rep_app prs (mkVarApps con_app rep_ids)
587 ; return (unbox_fn expr) }
588
589 {-
590 Note [Bangs on imported data constructors]
591 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
592
593 We pass Maybe [HsImplBang] to mkDataConRep to make use of HsImplBangs
594 from imported modules.
595
596 - Nothing <=> use HsSrcBangs
597 - Just bangs <=> use HsImplBangs
598
599 For imported types we can't work it all out from the HsSrcBangs,
600 because we want to be very sure to follow what the original module
601 (where the data type was declared) decided, and that depends on what
602 flags were enabled when it was compiled. So we record the decisions in
603 the interface file.
604
605 The HsImplBangs passed are in 1-1 correspondence with the
606 dataConOrigArgTys of the DataCon.
607
608 -}
609
610 -------------------------
611 newLocal :: Type -> UniqSM Var
612 newLocal ty = do { uniq <- getUniqueM
613 ; return (mkSysLocalOrCoVar (fsLit "dt") uniq ty) }
614
615 -- | Unpack/Strictness decisions from source module
616 dataConSrcToImplBang
617 :: DynFlags
618 -> FamInstEnvs
619 -> Type
620 -> HsSrcBang
621 -> HsImplBang
622
623 dataConSrcToImplBang dflags fam_envs arg_ty
624 (HsSrcBang ann unpk NoSrcStrict)
625 | xopt Opt_StrictData dflags -- StrictData => strict field
626 = dataConSrcToImplBang dflags fam_envs arg_ty
627 (HsSrcBang ann unpk SrcStrict)
628 | otherwise -- no StrictData => lazy field
629 = HsLazy
630
631 dataConSrcToImplBang _ _ _ (HsSrcBang _ _ SrcLazy)
632 = HsLazy
633
634 dataConSrcToImplBang dflags fam_envs arg_ty
635 (HsSrcBang _ unpk_prag SrcStrict)
636 | not (gopt Opt_OmitInterfacePragmas dflags) -- Don't unpack if -fomit-iface-pragmas
637 -- Don't unpack if we aren't optimising; rather arbitrarily,
638 -- we use -fomit-iface-pragmas as the indication
639 , let mb_co = topNormaliseType_maybe fam_envs arg_ty
640 -- Unwrap type families and newtypes
641 arg_ty' = case mb_co of { Just (_,ty) -> ty; Nothing -> arg_ty }
642 , isUnpackableType dflags fam_envs arg_ty'
643 , (rep_tys, _) <- dataConArgUnpack arg_ty'
644 , case unpk_prag of
645 NoSrcUnpack ->
646 gopt Opt_UnboxStrictFields dflags
647 || (gopt Opt_UnboxSmallStrictFields dflags
648 && length rep_tys <= 1) -- See Note [Unpack one-wide fields]
649 srcUnpack -> isSrcUnpacked srcUnpack
650 = case mb_co of
651 Nothing -> HsUnpack Nothing
652 Just (co,_) -> HsUnpack (Just co)
653
654 | otherwise -- Record the strict-but-no-unpack decision
655 = HsStrict
656
657
658 -- | Wrappers/Workers and representation following Unpack/Strictness
659 -- decisions
660 dataConArgRep
661 :: Type
662 -> HsImplBang
663 -> ([(Type,StrictnessMark)] -- Rep types
664 ,(Unboxer,Boxer))
665
666 dataConArgRep arg_ty HsLazy
667 = ([(arg_ty, NotMarkedStrict)], (unitUnboxer, unitBoxer))
668
669 dataConArgRep arg_ty HsStrict
670 = ([(arg_ty, MarkedStrict)], (seqUnboxer, unitBoxer))
671
672 dataConArgRep arg_ty (HsUnpack Nothing)
673 | (rep_tys, wrappers) <- dataConArgUnpack arg_ty
674 = (rep_tys, wrappers)
675
676 dataConArgRep _ (HsUnpack (Just co))
677 | let co_rep_ty = pSnd (coercionKind co)
678 , (rep_tys, wrappers) <- dataConArgUnpack co_rep_ty
679 = (rep_tys, wrapCo co co_rep_ty wrappers)
680
681
682 -------------------------
683 wrapCo :: Coercion -> Type -> (Unboxer, Boxer) -> (Unboxer, Boxer)
684 wrapCo co rep_ty (unbox_rep, box_rep) -- co :: arg_ty ~ rep_ty
685 = (unboxer, boxer)
686 where
687 unboxer arg_id = do { rep_id <- newLocal rep_ty
688 ; (rep_ids, rep_fn) <- unbox_rep rep_id
689 ; let co_bind = NonRec rep_id (Var arg_id `Cast` co)
690 ; return (rep_ids, Let co_bind . rep_fn) }
691 boxer = Boxer $ \ subst ->
692 do { (rep_ids, rep_expr)
693 <- case box_rep of
694 UnitBox -> do { rep_id <- newLocal (TcType.substTy subst rep_ty)
695 ; return ([rep_id], Var rep_id) }
696 Boxer boxer -> boxer subst
697 ; let sco = substCo subst co
698 ; return (rep_ids, rep_expr `Cast` mkSymCo sco) }
699
700 ------------------------
701 seqUnboxer :: Unboxer
702 seqUnboxer v = return ([v], \e -> Case (Var v) v (exprType e) [(DEFAULT, [], e)])
703
704 unitUnboxer :: Unboxer
705 unitUnboxer v = return ([v], \e -> e)
706
707 unitBoxer :: Boxer
708 unitBoxer = UnitBox
709
710 -------------------------
711 dataConArgUnpack
712 :: Type
713 -> ( [(Type, StrictnessMark)] -- Rep types
714 , (Unboxer, Boxer) )
715
716 dataConArgUnpack arg_ty
717 | Just (tc, tc_args) <- splitTyConApp_maybe arg_ty
718 , Just con <- tyConSingleAlgDataCon_maybe tc
719 -- NB: check for an *algebraic* data type
720 -- A recursive newtype might mean that
721 -- 'arg_ty' is a newtype
722 , let rep_tys = dataConInstArgTys con tc_args
723 = ASSERT( isVanillaDataCon con )
724 ( rep_tys `zip` dataConRepStrictness con
725 ,( \ arg_id ->
726 do { rep_ids <- mapM newLocal rep_tys
727 ; let unbox_fn body
728 = Case (Var arg_id) arg_id (exprType body)
729 [(DataAlt con, rep_ids, body)]
730 ; return (rep_ids, unbox_fn) }
731 , Boxer $ \ subst ->
732 do { rep_ids <- mapM (newLocal . TcType.substTy subst) rep_tys
733 ; return (rep_ids, Var (dataConWorkId con)
734 `mkTyApps` (substTys subst tc_args)
735 `mkVarApps` rep_ids ) } ) )
736 | otherwise
737 = pprPanic "dataConArgUnpack" (ppr arg_ty)
738 -- An interface file specified Unpacked, but we couldn't unpack it
739
740 isUnpackableType :: DynFlags -> FamInstEnvs -> Type -> Bool
741 -- True if we can unpack the UNPACK the argument type
742 -- See Note [Recursive unboxing]
743 -- We look "deeply" inside rather than relying on the DataCons
744 -- we encounter on the way, because otherwise we might well
745 -- end up relying on ourselves!
746 isUnpackableType dflags fam_envs ty
747 | Just (tc, _) <- splitTyConApp_maybe ty
748 , Just con <- tyConSingleAlgDataCon_maybe tc
749 , isVanillaDataCon con
750 = ok_con_args (unitNameSet (getName tc)) con
751 | otherwise
752 = False
753 where
754 ok_arg tcs (ty, bang) = not (attempt_unpack bang) || ok_ty tcs norm_ty
755 where
756 norm_ty = topNormaliseType fam_envs ty
757 ok_ty tcs ty
758 | Just (tc, _) <- splitTyConApp_maybe ty
759 , let tc_name = getName tc
760 = not (tc_name `elemNameSet` tcs)
761 && case tyConSingleAlgDataCon_maybe tc of
762 Just con | isVanillaDataCon con
763 -> ok_con_args (tcs `extendNameSet` getName tc) con
764 _ -> True
765 | otherwise
766 = True
767
768 ok_con_args tcs con
769 = all (ok_arg tcs) (dataConOrigArgTys con `zip` dataConSrcBangs con)
770 -- NB: dataConSrcBangs gives the *user* request;
771 -- We'd get a black hole if we used dataConImplBangs
772
773 attempt_unpack (HsSrcBang _ SrcUnpack NoSrcStrict)
774 = xopt Opt_StrictData dflags
775 attempt_unpack (HsSrcBang _ SrcUnpack SrcStrict)
776 = True
777 attempt_unpack (HsSrcBang _ NoSrcUnpack SrcStrict)
778 = True -- Be conservative
779 attempt_unpack (HsSrcBang _ NoSrcUnpack NoSrcStrict)
780 = xopt Opt_StrictData dflags -- Be conservative
781 attempt_unpack _ = False
782
783 {-
784 Note [Unpack one-wide fields]
785 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
786 The flag UnboxSmallStrictFields ensures that any field that can
787 (safely) be unboxed to a word-sized unboxed field, should be so unboxed.
788 For example:
789
790 data A = A Int#
791 newtype B = B A
792 data C = C !B
793 data D = D !C
794 data E = E !()
795 data F = F !D
796 data G = G !F !F
797
798 All of these should have an Int# as their representation, except
799 G which should have two Int#s.
800
801 However
802
803 data T = T !(S Int)
804 data S = S !a
805
806 Here we can represent T with an Int#.
807
808 Note [Recursive unboxing]
809 ~~~~~~~~~~~~~~~~~~~~~~~~~
810 Consider
811 data R = MkR {-# UNPACK #-} !S Int
812 data S = MkS {-# UNPACK #-} !Int
813 The representation arguments of MkR are the *representation* arguments
814 of S (plus Int); the rep args of MkS are Int#. This is all fine.
815
816 But be careful not to try to unbox this!
817 data T = MkT {-# UNPACK #-} !T Int
818 Because then we'd get an infinite number of arguments.
819
820 Here is a more complicated case:
821 data S = MkS {-# UNPACK #-} !T Int
822 data T = MkT {-# UNPACK #-} !S Int
823 Each of S and T must decide independently whether to unpack
824 and they had better not both say yes. So they must both say no.
825
826 Also behave conservatively when there is no UNPACK pragma
827 data T = MkS !T Int
828 with -funbox-strict-fields or -funbox-small-strict-fields
829 we need to behave as if there was an UNPACK pragma there.
830
831 But it's the *argument* type that matters. This is fine:
832 data S = MkS S !Int
833 because Int is non-recursive.
834
835 ************************************************************************
836 * *
837 Wrapping and unwrapping newtypes and type families
838 * *
839 ************************************************************************
840 -}
841
842 wrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
843 -- The wrapper for the data constructor for a newtype looks like this:
844 -- newtype T a = MkT (a,Int)
845 -- MkT :: forall a. (a,Int) -> T a
846 -- MkT = /\a. \(x:(a,Int)). x `cast` sym (CoT a)
847 -- where CoT is the coercion TyCon associated with the newtype
848 --
849 -- The call (wrapNewTypeBody T [a] e) returns the
850 -- body of the wrapper, namely
851 -- e `cast` (CoT [a])
852 --
853 -- If a coercion constructor is provided in the newtype, then we use
854 -- it, otherwise the wrap/unwrap are both no-ops
855 --
856 -- If the we are dealing with a newtype *instance*, we have a second coercion
857 -- identifying the family instance with the constructor of the newtype
858 -- instance. This coercion is applied in any case (ie, composed with the
859 -- coercion constructor of the newtype or applied by itself).
860
861 wrapNewTypeBody tycon args result_expr
862 = ASSERT( isNewTyCon tycon )
863 wrapFamInstBody tycon args $
864 mkCast result_expr (mkSymCo co)
865 where
866 co = mkUnbranchedAxInstCo Representational (newTyConCo tycon) args []
867
868 -- When unwrapping, we do *not* apply any family coercion, because this will
869 -- be done via a CoPat by the type checker. We have to do it this way as
870 -- computing the right type arguments for the coercion requires more than just
871 -- a spliting operation (cf, TcPat.tcConPat).
872
873 unwrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
874 unwrapNewTypeBody tycon args result_expr
875 = ASSERT( isNewTyCon tycon )
876 mkCast result_expr (mkUnbranchedAxInstCo Representational (newTyConCo tycon) args [])
877
878 -- If the type constructor is a representation type of a data instance, wrap
879 -- the expression into a cast adjusting the expression type, which is an
880 -- instance of the representation type, to the corresponding instance of the
881 -- family instance type.
882 -- See Note [Wrappers for data instance tycons]
883 wrapFamInstBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
884 wrapFamInstBody tycon args body
885 | Just co_con <- tyConFamilyCoercion_maybe tycon
886 = mkCast body (mkSymCo (mkUnbranchedAxInstCo Representational co_con args []))
887 | otherwise
888 = body
889
890 -- Same as `wrapFamInstBody`, but for type family instances, which are
891 -- represented by a `CoAxiom`, and not a `TyCon`
892 wrapTypeFamInstBody :: CoAxiom br -> Int -> [Type] -> [Coercion]
893 -> CoreExpr -> CoreExpr
894 wrapTypeFamInstBody axiom ind args cos body
895 = mkCast body (mkSymCo (mkAxInstCo Representational axiom ind args cos))
896
897 wrapTypeUnbranchedFamInstBody :: CoAxiom Unbranched -> [Type] -> [Coercion]
898 -> CoreExpr -> CoreExpr
899 wrapTypeUnbranchedFamInstBody axiom
900 = wrapTypeFamInstBody axiom 0
901
902 unwrapFamInstScrut :: TyCon -> [Type] -> CoreExpr -> CoreExpr
903 unwrapFamInstScrut tycon args scrut
904 | Just co_con <- tyConFamilyCoercion_maybe tycon
905 = mkCast scrut (mkUnbranchedAxInstCo Representational co_con args []) -- data instances only
906 | otherwise
907 = scrut
908
909 unwrapTypeFamInstScrut :: CoAxiom br -> Int -> [Type] -> [Coercion]
910 -> CoreExpr -> CoreExpr
911 unwrapTypeFamInstScrut axiom ind args cos scrut
912 = mkCast scrut (mkAxInstCo Representational axiom ind args cos)
913
914 unwrapTypeUnbranchedFamInstScrut :: CoAxiom Unbranched -> [Type] -> [Coercion]
915 -> CoreExpr -> CoreExpr
916 unwrapTypeUnbranchedFamInstScrut axiom
917 = unwrapTypeFamInstScrut axiom 0
918
919 {-
920 ************************************************************************
921 * *
922 \subsection{Primitive operations}
923 * *
924 ************************************************************************
925 -}
926
927 mkPrimOpId :: PrimOp -> Id
928 mkPrimOpId prim_op
929 = id
930 where
931 (tyvars,arg_tys,res_ty, arity, strict_sig) = primOpSig prim_op
932 ty = mkInvForAllTys tyvars (mkFunTys arg_tys res_ty)
933 name = mkWiredInName gHC_PRIM (primOpOcc prim_op)
934 (mkPrimOpIdUnique (primOpTag prim_op))
935 (AnId id) UserSyntax
936 id = mkGlobalId (PrimOpId prim_op) name ty info
937
938 info = noCafIdInfo
939 `setRuleInfo` mkRuleInfo (maybeToList $ primOpRules name prim_op)
940 `setArityInfo` arity
941 `setStrictnessInfo` strict_sig
942 `setInlinePragInfo` neverInlinePragma
943 -- We give PrimOps a NOINLINE pragma so that we don't
944 -- get silly warnings from Desugar.dsRule (the inline_shadows_rule
945 -- test) about a RULE conflicting with a possible inlining
946 -- cf Trac #7287
947
948 -- For each ccall we manufacture a separate CCallOpId, giving it
949 -- a fresh unique, a type that is correct for this particular ccall,
950 -- and a CCall structure that gives the correct details about calling
951 -- convention etc.
952 --
953 -- The *name* of this Id is a local name whose OccName gives the full
954 -- details of the ccall, type and all. This means that the interface
955 -- file reader can reconstruct a suitable Id
956
957 mkFCallId :: DynFlags -> Unique -> ForeignCall -> Type -> Id
958 mkFCallId dflags uniq fcall ty
959 = ASSERT( isEmptyVarSet (tyCoVarsOfType ty) )
960 -- A CCallOpId should have no free type variables;
961 -- when doing substitutions won't substitute over it
962 mkGlobalId (FCallId fcall) name ty info
963 where
964 occ_str = showSDoc dflags (braces (ppr fcall <+> ppr ty))
965 -- The "occurrence name" of a ccall is the full info about the
966 -- ccall; it is encoded, but may have embedded spaces etc!
967
968 name = mkFCallName uniq occ_str
969
970 info = noCafIdInfo
971 `setArityInfo` arity
972 `setStrictnessInfo` strict_sig
973
974 (bndrs, _) = tcSplitPiTys ty
975 arity = count isIdLikeBinder bndrs
976
977 strict_sig = mkClosedStrictSig (replicate arity topDmd) topRes
978 -- the call does not claim to be strict in its arguments, since they
979 -- may be lifted (foreign import prim) and the called code doesn't
980 -- necessarily force them. See Trac #11076.
981 {-
982 ************************************************************************
983 * *
984 \subsection{DictFuns and default methods}
985 * *
986 ************************************************************************
987
988 Note [Dict funs and default methods]
989 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
990 Dict funs and default methods are *not* ImplicitIds. Their definition
991 involves user-written code, so we can't figure out their strictness etc
992 based on fixed info, as we can for constructors and record selectors (say).
993
994 NB: See also Note [Exported LocalIds] in Id
995 -}
996
997 mkDictFunId :: Name -- Name to use for the dict fun;
998 -> [TyVar]
999 -> ThetaType
1000 -> Class
1001 -> [Type]
1002 -> Id
1003 -- Implements the DFun Superclass Invariant (see TcInstDcls)
1004 -- See Note [Dict funs and default methods]
1005
1006 mkDictFunId dfun_name tvs theta clas tys
1007 = mkExportedLocalId (DFunId is_nt)
1008 dfun_name
1009 dfun_ty
1010 where
1011 is_nt = isNewTyCon (classTyCon clas)
1012 dfun_ty = mkDictFunTy tvs theta clas tys
1013
1014 mkDictFunTy :: [TyVar] -> ThetaType -> Class -> [Type] -> Type
1015 mkDictFunTy tvs theta clas tys
1016 = mkInvSigmaTy tvs theta (mkClassPred clas tys)
1017
1018 {-
1019 ************************************************************************
1020 * *
1021 \subsection{Un-definable}
1022 * *
1023 ************************************************************************
1024
1025 These Ids can't be defined in Haskell. They could be defined in
1026 unfoldings in the wired-in GHC.Prim interface file, but we'd have to
1027 ensure that they were definitely, definitely inlined, because there is
1028 no curried identifier for them. That's what mkCompulsoryUnfolding
1029 does. If we had a way to get a compulsory unfolding from an interface
1030 file, we could do that, but we don't right now.
1031
1032 unsafeCoerce# isn't so much a PrimOp as a phantom identifier, that
1033 just gets expanded into a type coercion wherever it occurs. Hence we
1034 add it as a built-in Id with an unfolding here.
1035
1036 The type variables we use here are "open" type variables: this means
1037 they can unify with both unlifted and lifted types. Hence we provide
1038 another gun with which to shoot yourself in the foot.
1039 -}
1040
1041 lazyIdName, unsafeCoerceName, nullAddrName, seqName,
1042 realWorldName, voidPrimIdName, coercionTokenName,
1043 magicDictName, coerceName, proxyName, dollarName, oneShotName,
1044 runRWName :: Name
1045 unsafeCoerceName = mkWiredInIdName gHC_PRIM (fsLit "unsafeCoerce#") unsafeCoerceIdKey unsafeCoerceId
1046 nullAddrName = mkWiredInIdName gHC_PRIM (fsLit "nullAddr#") nullAddrIdKey nullAddrId
1047 seqName = mkWiredInIdName gHC_PRIM (fsLit "seq") seqIdKey seqId
1048 realWorldName = mkWiredInIdName gHC_PRIM (fsLit "realWorld#") realWorldPrimIdKey realWorldPrimId
1049 voidPrimIdName = mkWiredInIdName gHC_PRIM (fsLit "void#") voidPrimIdKey voidPrimId
1050 lazyIdName = mkWiredInIdName gHC_MAGIC (fsLit "lazy") lazyIdKey lazyId
1051 coercionTokenName = mkWiredInIdName gHC_PRIM (fsLit "coercionToken#") coercionTokenIdKey coercionTokenId
1052 magicDictName = mkWiredInIdName gHC_PRIM (fsLit "magicDict") magicDictKey magicDictId
1053 coerceName = mkWiredInIdName gHC_PRIM (fsLit "coerce") coerceKey coerceId
1054 proxyName = mkWiredInIdName gHC_PRIM (fsLit "proxy#") proxyHashKey proxyHashId
1055 dollarName = mkWiredInIdName gHC_BASE (fsLit "$") dollarIdKey dollarId
1056 oneShotName = mkWiredInIdName gHC_MAGIC (fsLit "oneShot") oneShotKey oneShotId
1057 runRWName = mkWiredInIdName gHC_MAGIC (fsLit "runRW#") runRWKey runRWId
1058
1059 dollarId :: Id -- Note [dollarId magic]
1060 dollarId = pcMiscPrelId dollarName ty
1061 (noCafIdInfo `setUnfoldingInfo` unf)
1062 where
1063 fun_ty = mkFunTy alphaTy openBetaTy
1064 ty = mkInvForAllTys [levity2TyVar, alphaTyVar, openBetaTyVar] $
1065 mkFunTy fun_ty fun_ty
1066 unf = mkInlineUnfolding (Just 2) rhs
1067 [f,x] = mkTemplateLocals [fun_ty, alphaTy]
1068 rhs = mkLams [levity2TyVar, alphaTyVar, openBetaTyVar, f, x] $
1069 App (Var f) (Var x)
1070
1071 ------------------------------------------------
1072 -- proxy# :: forall a. Proxy# a
1073 proxyHashId :: Id
1074 proxyHashId
1075 = pcMiscPrelId proxyName ty
1076 (noCafIdInfo `setUnfoldingInfo` evaldUnfolding) -- Note [evaldUnfoldings]
1077 where
1078 ty = mkInvForAllTys [kv, tv] (mkProxyPrimTy k t)
1079 kv = kKiVar
1080 k = mkTyVarTy kv
1081 [tv] = mkTemplateTyVars [k]
1082 t = mkTyVarTy tv
1083
1084 ------------------------------------------------
1085 -- unsafeCoerce# :: forall a b. a -> b
1086 unsafeCoerceId :: Id
1087 unsafeCoerceId
1088 = pcMiscPrelId unsafeCoerceName ty info
1089 where
1090 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1091 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1092
1093 ty = mkInvForAllTys [ levity1TyVar, levity2TyVar
1094 , openAlphaTyVar, openBetaTyVar ]
1095 (mkFunTy openAlphaTy openBetaTy)
1096
1097 [x] = mkTemplateLocals [openAlphaTy]
1098 rhs = mkLams [ levity1TyVar, levity2TyVar
1099 , openAlphaTyVar, openBetaTyVar
1100 , x] $
1101 Cast (Var x) (mkUnsafeCo Representational openAlphaTy openBetaTy)
1102
1103 ------------------------------------------------
1104 nullAddrId :: Id
1105 -- nullAddr# :: Addr#
1106 -- The reason is is here is because we don't provide
1107 -- a way to write this literal in Haskell.
1108 nullAddrId = pcMiscPrelId nullAddrName addrPrimTy info
1109 where
1110 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1111 `setUnfoldingInfo` mkCompulsoryUnfolding (Lit nullAddrLit)
1112
1113 ------------------------------------------------
1114 seqId :: Id -- See Note [seqId magic]
1115 seqId = pcMiscPrelId seqName ty info
1116 where
1117 info = noCafIdInfo `setInlinePragInfo` inline_prag
1118 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1119 `setRuleInfo` mkRuleInfo [seq_cast_rule]
1120
1121 inline_prag = alwaysInlinePragma `setInlinePragmaActivation` ActiveAfter 0
1122 -- Make 'seq' not inline-always, so that simpleOptExpr
1123 -- (see CoreSubst.simple_app) won't inline 'seq' on the
1124 -- LHS of rules. That way we can have rules for 'seq';
1125 -- see Note [seqId magic]
1126
1127 ty = mkInvForAllTys [alphaTyVar,betaTyVar]
1128 (mkFunTy alphaTy (mkFunTy betaTy betaTy))
1129
1130 [x,y] = mkTemplateLocals [alphaTy, betaTy]
1131 rhs = mkLams [alphaTyVar,betaTyVar,x,y] (Case (Var x) x betaTy [(DEFAULT, [], Var y)])
1132
1133 -- See Note [Built-in RULES for seq]
1134 -- NB: ru_nargs = 3, not 4, to match the code in
1135 -- Simplify.rebuildCase which tries to apply this rule
1136 seq_cast_rule = BuiltinRule { ru_name = fsLit "seq of cast"
1137 , ru_fn = seqName
1138 , ru_nargs = 3
1139 , ru_try = match_seq_of_cast }
1140
1141 match_seq_of_cast :: RuleFun
1142 -- See Note [Built-in RULES for seq]
1143 match_seq_of_cast _ _ _ [Type _, Type res_ty, Cast scrut co]
1144 = Just (fun `App` scrut)
1145 where
1146 fun = Lam x $ Lam y $
1147 Case (Var x) x res_ty [(DEFAULT,[],Var y)]
1148 -- Generate a Case directly, not a call to seq, which
1149 -- might be ill-kinded if res_ty is unboxed
1150 [x,y] = mkTemplateLocals [scrut_ty, res_ty]
1151 scrut_ty = pFst (coercionKind co)
1152
1153 match_seq_of_cast _ _ _ _ = Nothing
1154
1155 ------------------------------------------------
1156 lazyId :: Id -- See Note [lazyId magic]
1157 lazyId = pcMiscPrelId lazyIdName ty info
1158 where
1159 info = noCafIdInfo
1160 ty = mkInvForAllTys [alphaTyVar] (mkFunTy alphaTy alphaTy)
1161
1162 oneShotId :: Id -- See Note [The oneShot function]
1163 oneShotId = pcMiscPrelId oneShotName ty info
1164 where
1165 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1166 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1167 ty = mkInvForAllTys [ levity1TyVar, levity2TyVar
1168 , openAlphaTyVar, openBetaTyVar ]
1169 (mkFunTy fun_ty fun_ty)
1170 fun_ty = mkFunTy alphaTy betaTy
1171 [body, x] = mkTemplateLocals [fun_ty, openAlphaTy]
1172 x' = setOneShotLambda x
1173 rhs = mkLams [ levity1TyVar, levity2TyVar
1174 , openAlphaTyVar, openBetaTyVar
1175 , body, x'] $
1176 Var body `App` Var x
1177
1178 runRWId :: Id -- See Note [runRW magic] in this module
1179 runRWId = pcMiscPrelId runRWName ty info
1180 where
1181 info = noCafIdInfo `setInlinePragInfo` neverInlinePragma
1182 -- State# RealWorld
1183 stateRW = mkTyConApp statePrimTyCon [realWorldTy]
1184 -- (# State# RealWorld, o #)
1185 ret_ty = mkTupleTy Unboxed [stateRW, openAlphaTy]
1186 -- State# RealWorld -> (# State# RealWorld, o #)
1187 arg_ty = stateRW `mkFunTy` ret_ty
1188 -- (State# RealWorld -> (# State# RealWorld, o #))
1189 -- -> (# State# RealWorld, o #)
1190 ty = mkInvForAllTys [levity1TyVar, openAlphaTyVar] $
1191 arg_ty `mkFunTy` ret_ty
1192
1193 --------------------------------------------------------------------------------
1194 magicDictId :: Id -- See Note [magicDictId magic]
1195 magicDictId = pcMiscPrelId magicDictName ty info
1196 where
1197 info = noCafIdInfo `setInlinePragInfo` neverInlinePragma
1198 ty = mkInvForAllTys [alphaTyVar] alphaTy
1199
1200 --------------------------------------------------------------------------------
1201
1202 coerceId :: Id
1203 coerceId = pcMiscPrelId coerceName ty info
1204 where
1205 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1206 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1207 eqRTy = mkTyConApp coercibleTyCon [ liftedTypeKind
1208 , alphaTy, betaTy ]
1209 eqRPrimTy = mkTyConApp eqReprPrimTyCon [ liftedTypeKind
1210 , liftedTypeKind
1211 , alphaTy, betaTy ]
1212 ty = mkInvForAllTys [alphaTyVar, betaTyVar] $
1213 mkFunTys [eqRTy, alphaTy] betaTy
1214
1215 [eqR,x,eq] = mkTemplateLocals [eqRTy, alphaTy, eqRPrimTy]
1216 rhs = mkLams [alphaTyVar, betaTyVar, eqR, x] $
1217 mkWildCase (Var eqR) eqRTy betaTy $
1218 [(DataAlt coercibleDataCon, [eq], Cast (Var x) (mkCoVarCo eq))]
1219
1220 {-
1221 Note [dollarId magic]
1222 ~~~~~~~~~~~~~~~~~~~~~
1223 The only reason that ($) is wired in is so that its type can be
1224 forall (a:*, b:Open). (a->b) -> a -> b
1225 That is, the return type can be unboxed. E.g. this is OK
1226 foo $ True where foo :: Bool -> Int#
1227 because ($) doesn't inspect or move the result of the call to foo.
1228 See Trac #8739.
1229
1230 There is a special typing rule for ($) in TcExpr, so the type of ($)
1231 isn't looked at there, BUT Lint subsequently (and rightly) complains
1232 if sees ($) applied to Int# (say), unless we give it a wired-in type
1233 as we do here.
1234
1235 Note [Unsafe coerce magic]
1236 ~~~~~~~~~~~~~~~~~~~~~~~~~~
1237 We define a *primitive*
1238 GHC.Prim.unsafeCoerce#
1239 and then in the base library we define the ordinary function
1240 Unsafe.Coerce.unsafeCoerce :: forall (a:*) (b:*). a -> b
1241 unsafeCoerce x = unsafeCoerce# x
1242
1243 Notice that unsafeCoerce has a civilized (albeit still dangerous)
1244 polymorphic type, whose type args have kind *. So you can't use it on
1245 unboxed values (unsafeCoerce 3#).
1246
1247 In contrast unsafeCoerce# is even more dangerous because you *can* use
1248 it on unboxed things, (unsafeCoerce# 3#) :: Int. Its type is
1249 forall (a:OpenKind) (b:OpenKind). a -> b
1250
1251 Note [seqId magic]
1252 ~~~~~~~~~~~~~~~~~~
1253 'GHC.Prim.seq' is special in several ways.
1254
1255 a) In source Haskell its second arg can have an unboxed type
1256 x `seq` (v +# w)
1257 But see Note [Typing rule for seq] in TcExpr, which
1258 explains why we give seq itself an ordinary type
1259 seq :: forall a b. a -> b -> b
1260 and treat it as a language construct from a typing point of view.
1261
1262 b) Its fixity is set in LoadIface.ghcPrimIface
1263
1264 c) It has quite a bit of desugaring magic.
1265 See DsUtils.hs Note [Desugaring seq (1)] and (2) and (3)
1266
1267 d) There is some special rule handing: Note [User-defined RULES for seq]
1268
1269 Note [User-defined RULES for seq]
1270 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1271 Roman found situations where he had
1272 case (f n) of _ -> e
1273 where he knew that f (which was strict in n) would terminate if n did.
1274 Notice that the result of (f n) is discarded. So it makes sense to
1275 transform to
1276 case n of _ -> e
1277
1278 Rather than attempt some general analysis to support this, I've added
1279 enough support that you can do this using a rewrite rule:
1280
1281 RULE "f/seq" forall n. seq (f n) = seq n
1282
1283 You write that rule. When GHC sees a case expression that discards
1284 its result, it mentally transforms it to a call to 'seq' and looks for
1285 a RULE. (This is done in Simplify.rebuildCase.) As usual, the
1286 correctness of the rule is up to you.
1287
1288 VERY IMPORTANT: to make this work, we give the RULE an arity of 1, not 2.
1289 If we wrote
1290 RULE "f/seq" forall n e. seq (f n) e = seq n e
1291 with rule arity 2, then two bad things would happen:
1292
1293 - The magical desugaring done in Note [seqId magic] item (c)
1294 for saturated application of 'seq' would turn the LHS into
1295 a case expression!
1296
1297 - The code in Simplify.rebuildCase would need to actually supply
1298 the value argument, which turns out to be awkward.
1299
1300 Note [Built-in RULES for seq]
1301 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1302 We also have the following built-in rule for seq
1303
1304 seq (x `cast` co) y = seq x y
1305
1306 This eliminates unnecessary casts and also allows other seq rules to
1307 match more often. Notably,
1308
1309 seq (f x `cast` co) y --> seq (f x) y
1310
1311 and now a user-defined rule for seq (see Note [User-defined RULES for seq])
1312 may fire.
1313
1314
1315 Note [lazyId magic]
1316 ~~~~~~~~~~~~~~~~~~~
1317 lazy :: forall a?. a? -> a? (i.e. works for unboxed types too)
1318
1319 Used to lazify pseq: pseq a b = a `seq` lazy b
1320
1321 Also, no strictness: by being a built-in Id, all the info about lazyId comes from here,
1322 not from GHC.Base.hi. This is important, because the strictness
1323 analyser will spot it as strict!
1324
1325 Also no unfolding in lazyId: it gets "inlined" by a HACK in CorePrep.
1326 It's very important to do this inlining *after* unfoldings are exposed
1327 in the interface file. Otherwise, the unfolding for (say) pseq in the
1328 interface file will not mention 'lazy', so if we inline 'pseq' we'll totally
1329 miss the very thing that 'lazy' was there for in the first place.
1330 See Trac #3259 for a real world example.
1331
1332 lazyId is defined in GHC.Base, so we don't *have* to inline it. If it
1333 appears un-applied, we'll end up just calling it.
1334
1335 Note [runRW magic]
1336 ~~~~~~~~~~~~~~~~~~
1337 Some definitions, for instance @runST@, must have careful control over float out
1338 of the bindings in their body. Consider this use of @runST@,
1339
1340 f x = runST ( \ s -> let (a, s') = newArray# 100 [] s
1341 (_, s'') = fill_in_array_or_something a x s'
1342 in freezeArray# a s'' )
1343
1344 If we inline @runST@, we'll get:
1345
1346 f x = let (a, s') = newArray# 100 [] realWorld#{-NB-}
1347 (_, s'') = fill_in_array_or_something a x s'
1348 in freezeArray# a s''
1349
1350 And now if we allow the @newArray#@ binding to float out to become a CAF,
1351 we end up with a result that is totally and utterly wrong:
1352
1353 f = let (a, s') = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
1354 in \ x ->
1355 let (_, s'') = fill_in_array_or_something a x s'
1356 in freezeArray# a s''
1357
1358 All calls to @f@ will share a {\em single} array! Clearly this is nonsense and
1359 must be prevented.
1360
1361 This is what @runRW#@ gives us: by being inlined extremely late in the
1362 optimization (right before lowering to STG, in CorePrep), we can ensure that
1363 no further floating will occur. This allows us to safely inline things like
1364 @runST@, which are otherwise needlessly expensive (see #10678 and #5916).
1365
1366 While the definition of @GHC.Magic.runRW#@, we override its type in @MkId@
1367 to be open-kinded,
1368
1369 runRW# :: (o :: OpenKind) => (State# RealWorld -> (# State# RealWorld, o #))
1370 -> (# State# RealWorld, o #)
1371
1372
1373 Note [The oneShot function]
1374 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1375 In the context of making left-folds fuse somewhat okish (see ticket #7994
1376 and Note [Left folds via right fold]) it was determined that it would be useful
1377 if library authors could explicitly tell the compiler that a certain lambda is
1378 called at most once. The oneShot function allows that.
1379
1380 'oneShot' is open kinded, i.e. the type variables can refer to unlifted
1381 types as well (Trac #10744); e.g.
1382 oneShot (\x:Int# -> x +# 1#)
1383
1384 Like most magic functions it has a compulsary unfolding, so there is no need
1385 for a real definition somewhere. We have one in GHC.Magic for the convenience
1386 of putting the documentation there.
1387
1388 It uses `setOneShotLambda` on the lambda's binder. That is the whole magic:
1389
1390 A typical call looks like
1391 oneShot (\y. e)
1392 after unfolding the definition `oneShot = \f \x[oneshot]. f x` we get
1393 (\f \x[oneshot]. f x) (\y. e)
1394 --> \x[oneshot]. ((\y.e) x)
1395 --> \x[oneshot] e[x/y]
1396 which is what we want.
1397
1398 It is only effective if the one-shot info survives as long as possible; in
1399 particular it must make it into the interface in unfoldings. See Note [Preserve
1400 OneShotInfo] in CoreTidy.
1401
1402 Also see https://ghc.haskell.org/trac/ghc/wiki/OneShot.
1403
1404
1405 Note [magicDictId magic]
1406 ~~~~~~~~~~~~~~~~~~~~~~~~~
1407 The identifier `magicDict` is just a place-holder, which is used to
1408 implement a primitve that we cannot define in Haskell but we can write
1409 in Core. It is declared with a place-holder type:
1410
1411 magicDict :: forall a. a
1412
1413 The intention is that the identifier will be used in a very specific way,
1414 to create dictionaries for classes with a single method. Consider a class
1415 like this:
1416
1417 class C a where
1418 f :: T a
1419
1420 We are going to use `magicDict`, in conjunction with a built-in Prelude
1421 rule, to cast values of type `T a` into dictionaries for `C a`. To do
1422 this, we define a function like this in the library:
1423
1424 data WrapC a b = WrapC (C a => Proxy a -> b)
1425
1426 withT :: (C a => Proxy a -> b)
1427 -> T a -> Proxy a -> b
1428 withT f x y = magicDict (WrapC f) x y
1429
1430 The purpose of `WrapC` is to avoid having `f` instantiated.
1431 Also, it avoids impredicativity, because `magicDict`'s type
1432 cannot be instantiated with a forall. The field of `WrapC` contains
1433 a `Proxy` parameter which is used to link the type of the constraint,
1434 `C a`, with the type of the `Wrap` value being made.
1435
1436 Next, we add a built-in Prelude rule (see prelude/PrelRules.hs),
1437 which will replace the RHS of this definition with the appropriate
1438 definition in Core. The rewrite rule works as follows:
1439
1440 magicDict @t (wrap @a @b f) x y
1441 ---->
1442 f (x `cast` co a) y
1443
1444 The `co` coercion is the newtype-coercion extracted from the type-class.
1445 The type class is obtain by looking at the type of wrap.
1446
1447
1448 -------------------------------------------------------------
1449 @realWorld#@ used to be a magic literal, \tr{void#}. If things get
1450 nasty as-is, change it back to a literal (@Literal@).
1451
1452 voidArgId is a Local Id used simply as an argument in functions
1453 where we just want an arg to avoid having a thunk of unlifted type.
1454 E.g.
1455 x = \ void :: Void# -> (# p, q #)
1456
1457 This comes up in strictness analysis
1458
1459 Note [evaldUnfoldings]
1460 ~~~~~~~~~~~~~~~~~~~~~~
1461 The evaldUnfolding makes it look that some primitive value is
1462 evaluated, which in turn makes Simplify.interestingArg return True,
1463 which in turn makes INLINE things applied to said value likely to be
1464 inlined.
1465 -}
1466
1467 realWorldPrimId :: Id -- :: State# RealWorld
1468 realWorldPrimId = pcMiscPrelId realWorldName realWorldStatePrimTy
1469 (noCafIdInfo `setUnfoldingInfo` evaldUnfolding -- Note [evaldUnfoldings]
1470 `setOneShotInfo` stateHackOneShot)
1471
1472 voidPrimId :: Id -- Global constant :: Void#
1473 voidPrimId = pcMiscPrelId voidPrimIdName voidPrimTy
1474 (noCafIdInfo `setUnfoldingInfo` evaldUnfolding) -- Note [evaldUnfoldings]
1475
1476 voidArgId :: Id -- Local lambda-bound :: Void#
1477 voidArgId = mkSysLocal (fsLit "void") voidArgIdKey voidPrimTy
1478
1479 coercionTokenId :: Id -- :: () ~ ()
1480 coercionTokenId -- Used to replace Coercion terms when we go to STG
1481 = pcMiscPrelId coercionTokenName
1482 (mkTyConApp eqPrimTyCon [liftedTypeKind, liftedTypeKind, unitTy, unitTy])
1483 noCafIdInfo
1484
1485 pcMiscPrelId :: Name -> Type -> IdInfo -> Id
1486 pcMiscPrelId name ty info
1487 = mkVanillaGlobalWithInfo name ty info
1488 -- We lie and say the thing is imported; otherwise, we get into
1489 -- a mess with dependency analysis; e.g., core2stg may heave in
1490 -- random calls to GHCbase.unpackPS__. If GHCbase is the module
1491 -- being compiled, then it's just a matter of luck if the definition
1492 -- will be in "the right place" to be in scope.