2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrelVals]{Prelude values the compiler ``knows about''}
7 #include "HsVersions.h"
12 import IdLoop ( UnfoldingGuidance(..) )
13 import Id ( Id(..), GenId, mkPreludeId, mkTemplateLocals )
22 import CoreSyn -- quite a bit
23 import IdInfo -- quite a bit
24 import Literal ( mkMachInt )
25 import PrimOp ( PrimOp(..) )
26 import SpecEnv ( SpecEnv(..), nullSpecEnv )
27 import TyVar ( alphaTyVar, betaTyVar )
28 import Unique -- lots of *Keys
37 pcMiscPrelId :: Unique{-IdKey-} -> FAST_STRING -> FAST_STRING -> Type -> IdInfo -> Id
39 pcMiscPrelId key mod name ty info
40 = mkPreludeId (mkBuiltinName key mod name) ty info
43 %************************************************************************
45 \subsection[PrelVals-error-related]{@error@ and friends; @trace@}
47 %************************************************************************
49 GHC randomly injects these into the code.
51 @patError#@ is just a version of @error@ for pattern-matching
52 failures. It knows various ``codes'' which expand to longer
53 strings---this saves space!
55 @absent#@ is a thing we put in for ``absent'' arguments. They jolly
56 well shouldn't be yanked on, but if one is, then you will get a
57 friendly message from @absent#@ (rather a totally random crash).
59 @parError#@ is a special version of @error@ which the compiler does
60 not know to be a bottoming Id. It is used in the @_par_@ and @_seq_@
61 templates, but we don't ever expect to generate code for it.
64 pc_bottoming_Id key mod name ty
65 = pcMiscPrelId key mod name ty bottoming_info
67 bottoming_info = noIdInfo `addInfo` mkBottomStrictnessInfo
68 -- these "bottom" out, no matter what their arguments
71 = pc_bottoming_Id errorIdKey pRELUDE_BUILTIN SLIT("error") errorTy
74 = pc_bottoming_Id patErrorIdKey pRELUDE_BUILTIN SLIT("patError#") errorTy
77 = pc_bottoming_Id absentErrorIdKey pRELUDE_BUILTIN SLIT("absent#")
78 (mkSigmaTy [alphaTyVar] [] alphaTy)
81 = pcMiscPrelId parErrorIdKey pRELUDE_BUILTIN SLIT("parError#")
82 (mkSigmaTy [alphaTyVar] [] alphaTy) noIdInfo
85 errorTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [mkListTy charTy] alphaTy)
88 We want \tr{_trace} (NB: name not in user namespace) to be wired in
89 because we don't want the strictness analyser to get ahold of it,
90 decide that the second argument is strict, evaluate that first (!!),
91 and make a jolly old mess. Having \tr{_trace} wired in also helps when
92 attempting to re-export it---because it's in \tr{PreludeBuiltin}, it
93 won't get an \tr{import} declaration in the interface file, so the
94 importing-subsequently module needs to know it's magic.
97 = pcMiscPrelId traceIdKey pRELUDE_BUILTIN SLIT("_trace") traceTy
98 (noIdInfo `addInfo` pcGenerateSpecs traceIdKey tRACE_ID noIdInfo traceTy)
100 traceTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [mkListTy charTy, alphaTy] alphaTy)
103 %************************************************************************
105 \subsection[PrelVals-Integer-support]{To support @Integer@ and @String@ literals}
107 %************************************************************************
111 = pcMiscPrelId packCStringIdKey{-ToDo:rename-} pRELUDE_PS SLIT("_packStringForC")
112 (mkFunTys [stringTy] byteArrayPrimTy) noIdInfo
114 --------------------------------------------------------------------
117 = pcMiscPrelId unpackCStringIdKey pRELUDE_BUILTIN SLIT("unpackPS#")
118 (mkFunTys [addrPrimTy{-a char *-}] stringTy) noIdInfo
120 -- (FunTy addrPrimTy{-a char *-} stringTy) (noIdInfo `addInfo` mkArityInfo 1)
121 -- but I don't like wired-in IdInfos (WDP)
123 unpackCString2Id -- for cases when a string has a NUL in it
124 = pcMiscPrelId unpackCString2IdKey pRELUDE_BUILTIN SLIT("unpackPS2#")
125 (mkFunTys [addrPrimTy{-a char *-}, intPrimTy{-length-}] stringTy)
128 --------------------------------------------------------------------
129 unpackCStringAppendId
130 = pcMiscPrelId unpackCStringAppendIdKey pRELUDE_BUILTIN SLIT("unpackAppendPS#")
131 (mkFunTys [addrPrimTy{-a "char *" pointer-},stringTy] stringTy)
133 `addInfo_UF` mkMagicUnfolding unpackCStringAppendIdKey)
134 `addInfo` mkArityInfo 2)
137 = pcMiscPrelId unpackCStringFoldrIdKey pRELUDE_BUILTIN SLIT("unpackFoldrPS#")
138 (mkSigmaTy [alphaTyVar] []
139 (mkFunTys [addrPrimTy{-a "char *" pointer-},
140 mkFunTys [charTy, alphaTy] alphaTy,
144 `addInfo_UF` mkMagicUnfolding unpackCStringFoldrIdKey)
145 `addInfo` mkArityInfo 3)
148 OK, this is Will's idea: we should have magic values for Integers 0,
149 +1, +2, and -1 (go ahead, fire me):
152 = pcMiscPrelId integerZeroIdKey pRELUDE_CORE SLIT("__integer0") integerTy noIdInfo
154 = pcMiscPrelId integerPlusOneIdKey pRELUDE_CORE SLIT("__integer1") integerTy noIdInfo
156 = pcMiscPrelId integerPlusTwoIdKey pRELUDE_CORE SLIT("__integer2") integerTy noIdInfo
158 = pcMiscPrelId integerMinusOneIdKey pRELUDE_CORE SLIT("__integerm1") integerTy noIdInfo
161 %************************************************************************
163 \subsection[PrelVals-parallel]{@seq@ and @par@: for parallel operation (only)}
165 %************************************************************************
168 --------------------------------------------------------------------
169 -- seqId :: "_seq_", used w/ GRIP, etc., is really quite similar to
173 _seq_ = /\ a b -> \ x y -> case x of { _ -> y }
176 _seq_ = /\ a b -> \ x y -> case seq# x y of { _Lift y' -> y' }
179 _seq_ = /\ a b -> \ x::a y::b -> case seq# x of { 0# -> parError#; _ -> y; }
183 seqId = pcMiscPrelId seqIdKey pRELUDE_BUILTIN SLIT("_seq_")
184 (mkSigmaTy [alphaTyVar, betaTyVar] []
185 (mkFunTys [alphaTy, betaTy] betaTy))
186 (noIdInfo `addInfo_UF` (mkUnfolding EssentialUnfolding seq_template))
196 = mkLam [alphaTyVar, betaTyVar] [x, y] (
197 Case (Prim SeqOp [TyArg alphaTy, VarArg x]) (
199 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
200 (BindDefault z (Var y))))
202 --------------------------------------------------------------------
203 -- parId :: "_par_", also used w/ GRIP, etc.
207 par = /\ a b -> \ x y -> case (par# (case x of { _ -> () })) of { _ -> y }
211 _par_ = /\ a b -> \ x y -> case par# x y of { _Lift y' -> y' }
215 _par_ = /\ a b -> \ x::a y::b -> case par# x of { 0# -> parError#; _ -> y; }
218 parId = pcMiscPrelId parIdKey pRELUDE_BUILTIN SLIT("_par_")
219 (mkSigmaTy [alphaTyVar, betaTyVar] []
220 (mkFunTys [alphaTy, betaTy] betaTy))
221 (noIdInfo `addInfo_UF` (mkUnfolding EssentialUnfolding par_template))
231 = mkLam [alphaTyVar, betaTyVar] [x, y] (
232 Case (Prim ParOp [TyArg alphaTy, VarArg x]) (
234 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
235 (BindDefault z (Var y))))
237 -- forkId :: "_fork_", for *required* concurrent threads
239 _fork_ = /\ a b -> \ x::a y::b -> case fork# x of { 0# -> parError#; _ -> y; }
241 forkId = pcMiscPrelId forkIdKey pRELUDE_BUILTIN SLIT("_fork_")
242 (mkSigmaTy [alphaTyVar, betaTyVar] []
243 (mkFunTys [alphaTy, betaTy] betaTy))
244 (noIdInfo `addInfo_UF` (mkUnfolding EssentialUnfolding fork_template))
254 = mkLam [alphaTyVar, betaTyVar] [x, y] (
255 Case (Prim ForkOp [TyArg alphaTy, VarArg x]) (
257 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
258 (BindDefault z (Var y))))
265 parLocalId = pcMiscPrelId parLocalIdKey pRELUDE_BUILTIN SLIT("_parLocal_")
266 (mkSigmaTy [alphaTyVar, betaTyVar] []
267 (mkFunTys [intPrimTy, alphaTy, betaTy] betaTy))
268 (noIdInfo `addInfo_UF` (mkUnfolding EssentialUnfolding parLocal_template))
279 = mkLam [alphaTyVar, betaTyVar] [w, x, y] (
280 Case (Prim ParLocalOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg w, VarArg y]) (
282 [(liftDataCon, [z], Var z)]
285 parGlobalId = pcMiscPrelId parGlobalIdKey pRELUDE_BUILTIN SLIT("_parGlobal_")
286 (mkSigmaTy [alphaTyVar, betaTyVar] []
287 (mkFunTys [intPrimTy,alphaTy,betaTy] betaTy))
288 (noIdInfo `addInfo_UF` (mkUnfolding EssentialUnfolding parGlobal_template))
299 = mkLam [alphaTyVar, betaTyVar] [w, x, y] (
300 Case (Prim ParGlobalOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg w, VarArg y]) (
302 [(liftDataCon, [z], Var z)]
308 %************************************************************************
310 \subsection[PrelVals-deriving]{Values known about mainly for doing derived instance decls}
312 %************************************************************************
314 map :: (a -> b) -> [a] -> [b]
315 -- this is up in the here-because-of-unfolding list
317 --??showChar :: Char -> ShowS
318 showSpace :: ShowS -- non-std: == "showChar ' '"
319 showString :: String -> ShowS
320 showParen :: Bool -> ShowS -> ShowS
322 (++) :: [a] -> [a] -> [a]
323 readParen :: Bool -> ReadS a -> ReadS a
326 %************************************************************************
328 \subsection[PrelVals-void]{@void#@: Magic value of type @Void#@}
330 %************************************************************************
332 I don't think this is available to the user; it's used in the
333 simplifier (WDP 94/06).
336 = pcMiscPrelId voidPrimIdKey pRELUDE_BUILTIN SLIT("void#")
340 %************************************************************************
342 \subsection[PrelVals-runST]{@_runST@: Magic start-state-transformer function}
344 %************************************************************************
346 @_runST@ has a non-Haskell-able type:
348 -- _runST :: forall a. (forall s. _ST s a) -> a
349 -- which is to say ::
350 -- forall a. (forall s. (_State s -> (a, _State s))) -> a
352 _runST a m = case m _RealWorld (S# _RealWorld realWorld#) of
353 (r :: a, wild :: _State _RealWorld) -> r
355 We unfold always, just for simplicity:
358 = pcMiscPrelId runSTIdKey pRELUDE_BUILTIN SLIT("_runST") run_ST_ty id_info
363 st_ty a = mkSigmaTy [s_tv] [] (mkStateTransformerTy s a)
366 = mkSigmaTy [alphaTyVar] [] (mkFunTys [st_ty alphaTy] alphaTy)
367 -- NB: rank-2 polymorphism! (forall inside the st_ty...)
371 `addInfo` mkArityInfo 1
372 `addInfo` mkStrictnessInfo [WwStrict] Nothing
373 `addInfo` mkArgUsageInfo [ArgUsage 1]
374 -- ABSOLUTELY NO UNFOLDING, e.g.: (mkUnfolding EssentialUnfolding run_ST_template)
380 {-t-} realWorldStateTy,
382 {-_-} realWorldStateTy
386 = mkLam [alphaTyVar] [m] (
387 Let (NonRec t (Con stateDataCon [TyArg realWorldTy, VarArg realWorldPrimId])) (
388 Case (App (mkTyApp (Var m) [realWorldTy]) (VarArg t)) (
390 [(mkTupleCon 2, [r, wild], Var r)]
395 SLPJ 95/04: Why @_runST@ must not have an unfolding; consider:
399 (a, s') = newArray# 100 [] s
400 (_, s'') = fill_in_array_or_something a x s'
404 If we inline @_runST@, we'll get:
407 (a, s') = newArray# 100 [] realWorld#{-NB-}
408 (_, s'') = fill_in_array_or_something a x s'
412 And now the @newArray#@ binding can be floated to become a CAF, which
413 is totally and utterly wrong:
416 (a, s') = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
419 let (_, s'') = fill_in_array_or_something a x s' in
422 All calls to @f@ will share a {\em single} array! End SLPJ 95/04.
424 @realWorld#@ used to be a magic literal, \tr{void#}. If things get
425 nasty as-is, change it back to a literal (@Literal@).
428 = pcMiscPrelId realWorldPrimIdKey pRELUDE_BUILTIN SLIT("realWorld#")
433 %************************************************************************
435 \subsection[PrelVals-foldr-build]{Values known about for ``foldr/build''}
437 %************************************************************************
441 = pcMiscPrelId buildIdKey pRELUDE_CORE SLIT("_build") buildTy
443 `addInfo_UF` mkMagicUnfolding buildIdKey)
444 `addInfo` mkStrictnessInfo [WwStrict] Nothing)
445 `addInfo` mkArgUsageInfo [ArgUsage 2])
446 `addInfo` pcGenerateSpecs buildIdKey buildId noIdInfo{-ToDo-} buildTy)
447 -- cheating, but since _build never actually exists ...
449 -- The type of this strange object is:
450 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a]
452 buildTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [build_ty] (mkListTy alphaTy))
454 build_ty = mkSigmaTy [betaTyVar] []
455 (mkFunTys [alphaTy, mkFunTys [betaTy] betaTy, betaTy] betaTy)
458 @mkBuild@ is sugar for building a build!
460 @mkbuild ty tv c n e@ $Rightarrow$ @build ty (/\ tv -> \ c n -> e)@
461 @ty@ is the type of the list.
462 @tv@ is always a new type variable.
463 @c,n@ are Id's for the abstract cons and nil, @g@ for let binding the argument argument.
466 v :: (\/ b . (a -> b -> b) -> b -> b) -> [a]
467 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a]
468 @e@ is the object right inside the @build@
476 -> CoreExpr -- template
477 -> CoreExpr -- template
479 mkBuild ty tv c n g expr
480 = Let (NonRec g (mkLam [tv] [c,n] expr))
481 (App (mkTyApp (Var buildId) [ty]) (VarArg g))
486 = pcMiscPrelId augmentIdKey pRELUDE_CORE SLIT("_augment") augmentTy
488 `addInfo_UF` mkMagicUnfolding augmentIdKey)
489 `addInfo` mkStrictnessInfo [WwStrict,WwLazy False] Nothing)
490 `addInfo` mkArgUsageInfo [ArgUsage 2,UnknownArgUsage])
491 -- cheating, but since _augment never actually exists ...
493 -- The type of this strange object is:
494 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a] -> [a]
496 augmentTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [aug_ty, mkListTy alphaTy] (mkListTy alphaTy))
498 aug_ty = mkSigmaTy [betaTyVar] []
499 (mkFunTys [alphaTy, mkFunTys [betaTy] betaTy, betaTy] betaTy)
503 foldrId = pcMiscPrelId foldrIdKey pRELUDE_FB{-not "List"-} SLIT("foldr")
507 mkSigmaTy [alphaTyVar, betaTyVar] []
508 (mkFunTys [alphaTy, mkFunTys [betaTy] betaTy, betaTy, mkListTy alphaTy] betaTy)
510 idInfo = (((((noIdInfo
511 `addInfo_UF` mkMagicUnfolding foldrIdKey)
512 `addInfo` mkStrictnessInfo [WwLazy False,WwLazy False,WwStrict] Nothing)
513 `addInfo` mkArityInfo 3)
514 `addInfo` mkUpdateInfo [2,2,1])
515 `addInfo` pcGenerateSpecs foldrIdKey foldrId noIdInfo{-ToDo-} foldrTy)
517 foldlId = pcMiscPrelId foldlIdKey pRELUDE_FB{-not "List"-} SLIT("foldl")
521 mkSigmaTy [alphaTyVar, betaTyVar] []
522 (mkFunTys [alphaTy, mkFunTys [betaTy] betaTy, alphaTy, mkListTy betaTy] alphaTy)
524 idInfo = (((((noIdInfo
525 `addInfo_UF` mkMagicUnfolding foldlIdKey)
526 `addInfo` mkStrictnessInfo [WwLazy False,WwLazy False,WwStrict] Nothing)
527 `addInfo` mkArityInfo 3)
528 `addInfo` mkUpdateInfo [2,2,1])
529 `addInfo` pcGenerateSpecs foldlIdKey foldlId noIdInfo{-ToDo-} foldlTy)
531 -- A bit of magic goes no here. We translate appendId into ++,
532 -- you have to be carefull when you actually compile append:
533 -- xs ++ ys = augment (\ c n -> foldr c n xs) ys
534 -- {- unfold augment -}
536 -- {- fold foldr to append -}
537 -- = ys `appendId` xs
538 -- = ys ++ xs -- ugg!
539 -- *BUT* you want (++) and not _append in your interfaces.
541 -- So you have to turn *off* unfolding of foldr inside FoldrBuild.hs inside
546 = pcMiscPrelId appendIdKey pRELUDE_LIST SLIT("++") appendTy idInfo
549 (mkSigmaTy [alphaTyVar] []
550 (mkFunTys [mkListTy alphaTy, mkListTy alphaTy] (mkListTy alphaTy)))
552 `addInfo` mkStrictnessInfo [WwStrict,WwLazy False] Nothing)
553 `addInfo` mkArityInfo 2)
554 `addInfo` mkUpdateInfo [1,2])
557 %************************************************************************
559 \subsection[PrelUtils-specialisations]{Specialisations for builtin values}
561 %************************************************************************
563 The specialisations which exist for the builtin values must be recorded in
566 NOTE: THE USES OF THE pcGenerate... FUNCTIONS MUST CORRESPOND
567 TO THE SPECIALISATIONS DECLARED IN THE PRELUDE !!!
569 HACK: We currently use the same unique for the specialised Ids.
571 The list @specing_types@ determines the types for which specialised
572 versions are created. Note: This should correspond with the
573 types passed to the pre-processor with the -genSPECS arg (see ghc.lprl).
575 ToDo: Create single mkworld definition which is grabbed here and in ghc.lprl
578 pcGenerateSpecs :: Unique -> Id -> IdInfo -> Type -> SpecEnv
579 pcGenerateSpecs key id info ty
584 pc_gen_specs True key id info ty
586 pc_gen_specs is_id key id info ty
587 = mkSpecEnv spec_infos
589 spec_infos = [ let spec_ty = specialiseTy ty spec_tys 0
591 then mkSpecId key {- HACK WARNING: same unique! -}
592 id spec_tys spec_ty info
593 else panic "SpecData:SpecInfo:SpecId"
595 SpecInfo spec_tys (length ctxts) spec_id
596 | spec_tys <- specialisations ]
598 (tyvars, ctxts, _) = splitSigmaTy ty
599 no_tyvars = length tyvars
601 specialisations = if no_tyvars == 0
603 else tail (cross_product no_tyvars specing_types)
605 -- N.B. tail removes fully polymorphic specialisation
607 cross_product 0 tys = []
608 cross_product 1 tys = map (:[]) tys
609 cross_product n tys = concat [map (:cp) tys | cp <- cross_product (n-1) tys]
612 specing_types = [Nothing,