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 u pRELUDE_BUILTIN n errorTy
77 = generic_ERROR_ID patErrorIdKey SLIT("patError#")
79 = generic_ERROR_ID recConErrorIdKey SLIT("recConError#")
81 = generic_ERROR_ID recUpdErrorIdKey SLIT("recUpdError#")
83 = generic_ERROR_ID irrefutPatErrorIdKey SLIT("irrefutPatError#")
84 nON_EXHAUSTIVE_GUARDS_ERROR_ID
85 = generic_ERROR_ID nonExhaustiveGuardsErrorIdKey SLIT("nonExhaustiveGuardsError#")
86 nO_DEFAULT_METHOD_ERROR_ID
87 = generic_ERROR_ID noDefaultMethodErrorIdKey SLIT("noDefaultMethodError#")
88 nO_EXPLICIT_METHOD_ERROR_ID
89 = generic_ERROR_ID nonExplicitMethodErrorIdKey SLIT("noExplicitMethodError#")
92 = pc_bottoming_Id absentErrorIdKey pRELUDE_BUILTIN SLIT("absent#")
93 (mkSigmaTy [alphaTyVar] [] alphaTy)
96 = pcMiscPrelId parErrorIdKey pRELUDE_BUILTIN SLIT("parError#")
97 (mkSigmaTy [alphaTyVar] [] alphaTy) noIdInfo
100 errorTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [mkListTy charTy] alphaTy)
103 We want \tr{_trace} (NB: name not in user namespace) to be wired in
104 because we don't want the strictness analyser to get ahold of it,
105 decide that the second argument is strict, evaluate that first (!!),
106 and make a jolly old mess. Having \tr{_trace} wired in also helps when
107 attempting to re-export it---because it's in \tr{PreludeBuiltin}, it
108 won't get an \tr{import} declaration in the interface file, so the
109 importing-subsequently module needs to know it's magic.
112 = pcMiscPrelId traceIdKey pRELUDE_BUILTIN SLIT("_trace") traceTy
113 (noIdInfo `addInfo` pcGenerateSpecs traceIdKey tRACE_ID noIdInfo traceTy)
115 traceTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [mkListTy charTy, alphaTy] alphaTy)
118 %************************************************************************
120 \subsection[PrelVals-Integer-support]{To support @Integer@ and @String@ literals}
122 %************************************************************************
126 = pcMiscPrelId packCStringIdKey{-ToDo:rename-} pRELUDE_PS SLIT("_packStringForC")
127 (mkFunTys [stringTy] byteArrayPrimTy) noIdInfo
129 --------------------------------------------------------------------
132 = pcMiscPrelId unpackCStringIdKey pRELUDE_BUILTIN SLIT("unpackPS#")
133 (mkFunTys [addrPrimTy{-a char *-}] stringTy) noIdInfo
135 -- (FunTy addrPrimTy{-a char *-} stringTy) (noIdInfo `addInfo` mkArityInfo 1)
136 -- but I don't like wired-in IdInfos (WDP)
138 unpackCString2Id -- for cases when a string has a NUL in it
139 = pcMiscPrelId unpackCString2IdKey pRELUDE_BUILTIN SLIT("unpackPS2#")
140 (mkFunTys [addrPrimTy{-a char *-}, intPrimTy{-length-}] stringTy)
143 --------------------------------------------------------------------
144 unpackCStringAppendId
145 = pcMiscPrelId unpackCStringAppendIdKey pRELUDE_BUILTIN SLIT("unpackAppendPS#")
146 (mkFunTys [addrPrimTy{-a "char *" pointer-},stringTy] stringTy)
148 `addInfo_UF` mkMagicUnfolding unpackCStringAppendIdKey)
149 `addInfo` mkArityInfo 2)
152 = pcMiscPrelId unpackCStringFoldrIdKey pRELUDE_BUILTIN SLIT("unpackFoldrPS#")
153 (mkSigmaTy [alphaTyVar] []
154 (mkFunTys [addrPrimTy{-a "char *" pointer-},
155 mkFunTys [charTy, alphaTy] alphaTy,
159 `addInfo_UF` mkMagicUnfolding unpackCStringFoldrIdKey)
160 `addInfo` mkArityInfo 3)
163 OK, this is Will's idea: we should have magic values for Integers 0,
164 +1, +2, and -1 (go ahead, fire me):
167 = pcMiscPrelId integerZeroIdKey pRELUDE_CORE SLIT("__integer0") integerTy noIdInfo
169 = pcMiscPrelId integerPlusOneIdKey pRELUDE_CORE SLIT("__integer1") integerTy noIdInfo
171 = pcMiscPrelId integerPlusTwoIdKey pRELUDE_CORE SLIT("__integer2") integerTy noIdInfo
173 = pcMiscPrelId integerMinusOneIdKey pRELUDE_CORE SLIT("__integerm1") integerTy noIdInfo
176 %************************************************************************
178 \subsection[PrelVals-parallel]{@seq@ and @par@: for parallel operation (only)}
180 %************************************************************************
183 --------------------------------------------------------------------
184 -- seqId :: "_seq_", used w/ GRIP, etc., is really quite similar to
188 _seq_ = /\ a b -> \ x y -> case x of { _ -> y }
191 _seq_ = /\ a b -> \ x y -> case seq# x y of { _Lift y' -> y' }
194 _seq_ = /\ a b -> \ x::a y::b -> case seq# x of { 0# -> parError#; _ -> y; }
198 seqId = pcMiscPrelId seqIdKey pRELUDE_BUILTIN SLIT("_seq_")
199 (mkSigmaTy [alphaTyVar, betaTyVar] []
200 (mkFunTys [alphaTy, betaTy] betaTy))
201 (noIdInfo `addInfo_UF` (mkUnfolding EssentialUnfolding seq_template))
211 = mkLam [alphaTyVar, betaTyVar] [x, y] (
212 Case (Prim SeqOp [TyArg alphaTy, VarArg x]) (
214 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
215 (BindDefault z (Var y))))
217 --------------------------------------------------------------------
218 -- parId :: "_par_", also used w/ GRIP, etc.
222 par = /\ a b -> \ x y -> case (par# (case x of { _ -> () })) of { _ -> y }
226 _par_ = /\ a b -> \ x y -> case par# x y of { _Lift y' -> y' }
230 _par_ = /\ a b -> \ x::a y::b -> case par# x of { 0# -> parError#; _ -> y; }
233 parId = pcMiscPrelId parIdKey pRELUDE_BUILTIN SLIT("_par_")
234 (mkSigmaTy [alphaTyVar, betaTyVar] []
235 (mkFunTys [alphaTy, betaTy] betaTy))
236 (noIdInfo `addInfo_UF` (mkUnfolding EssentialUnfolding par_template))
246 = mkLam [alphaTyVar, betaTyVar] [x, y] (
247 Case (Prim ParOp [TyArg alphaTy, VarArg x]) (
249 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
250 (BindDefault z (Var y))))
252 -- forkId :: "_fork_", for *required* concurrent threads
254 _fork_ = /\ a b -> \ x::a y::b -> case fork# x of { 0# -> parError#; _ -> y; }
256 forkId = pcMiscPrelId forkIdKey pRELUDE_BUILTIN SLIT("_fork_")
257 (mkSigmaTy [alphaTyVar, betaTyVar] []
258 (mkFunTys [alphaTy, betaTy] betaTy))
259 (noIdInfo `addInfo_UF` (mkUnfolding EssentialUnfolding fork_template))
269 = mkLam [alphaTyVar, betaTyVar] [x, y] (
270 Case (Prim ForkOp [TyArg alphaTy, VarArg x]) (
272 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
273 (BindDefault z (Var y))))
280 parLocalId = pcMiscPrelId parLocalIdKey pRELUDE_BUILTIN SLIT("_parLocal_")
281 (mkSigmaTy [alphaTyVar, betaTyVar] []
282 (mkFunTys [intPrimTy, alphaTy, betaTy] betaTy))
283 (noIdInfo `addInfo_UF` (mkUnfolding EssentialUnfolding parLocal_template))
294 = mkLam [alphaTyVar, betaTyVar] [w, x, y] (
295 Case (Prim ParLocalOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg w, VarArg y]) (
297 [(liftDataCon, [z], Var z)]
300 parGlobalId = pcMiscPrelId parGlobalIdKey pRELUDE_BUILTIN SLIT("_parGlobal_")
301 (mkSigmaTy [alphaTyVar, betaTyVar] []
302 (mkFunTys [intPrimTy,alphaTy,betaTy] betaTy))
303 (noIdInfo `addInfo_UF` (mkUnfolding EssentialUnfolding parGlobal_template))
314 = mkLam [alphaTyVar, betaTyVar] [w, x, y] (
315 Case (Prim ParGlobalOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg w, VarArg y]) (
317 [(liftDataCon, [z], Var z)]
323 %************************************************************************
325 \subsection[PrelVals-deriving]{Values known about mainly for doing derived instance decls}
327 %************************************************************************
329 map :: (a -> b) -> [a] -> [b]
330 -- this is up in the here-because-of-unfolding list
332 --??showChar :: Char -> ShowS
333 showSpace :: ShowS -- non-std: == "showChar ' '"
334 showString :: String -> ShowS
335 showParen :: Bool -> ShowS -> ShowS
337 (++) :: [a] -> [a] -> [a]
338 readParen :: Bool -> ReadS a -> ReadS a
341 %************************************************************************
343 \subsection[PrelVals-void]{@void#@: Magic value of type @Void#@}
345 %************************************************************************
347 I don't think this is available to the user; it's used in the
348 simplifier (WDP 94/06).
351 = pcMiscPrelId voidPrimIdKey pRELUDE_BUILTIN SLIT("void#")
355 %************************************************************************
357 \subsection[PrelVals-runST]{@_runST@: Magic start-state-transformer function}
359 %************************************************************************
361 @_runST@ has a non-Haskell-able type:
363 -- _runST :: forall a. (forall s. _ST s a) -> a
364 -- which is to say ::
365 -- forall a. (forall s. (_State s -> (a, _State s))) -> a
367 _runST a m = case m _RealWorld (S# _RealWorld realWorld#) of
368 (r :: a, wild :: _State _RealWorld) -> r
370 We unfold always, just for simplicity:
373 = pcMiscPrelId runSTIdKey pRELUDE_BUILTIN SLIT("_runST") run_ST_ty id_info
378 st_ty a = mkSigmaTy [s_tv] [] (mkStateTransformerTy s a)
381 = mkSigmaTy [alphaTyVar] [] (mkFunTys [st_ty alphaTy] alphaTy)
382 -- NB: rank-2 polymorphism! (forall inside the st_ty...)
386 `addInfo` mkArityInfo 1
387 `addInfo` mkStrictnessInfo [WwStrict] Nothing
388 `addInfo` mkArgUsageInfo [ArgUsage 1]
389 -- ABSOLUTELY NO UNFOLDING, e.g.: (mkUnfolding EssentialUnfolding run_ST_template)
395 {-t-} realWorldStateTy,
397 {-_-} realWorldStateTy
401 = mkLam [alphaTyVar] [m] (
402 Let (NonRec t (Con stateDataCon [TyArg realWorldTy, VarArg realWorldPrimId])) (
403 Case (App (mkTyApp (Var m) [realWorldTy]) (VarArg t)) (
405 [(mkTupleCon 2, [r, wild], Var r)]
410 SLPJ 95/04: Why @_runST@ must not have an unfolding; consider:
414 (a, s') = newArray# 100 [] s
415 (_, s'') = fill_in_array_or_something a x s'
419 If we inline @_runST@, we'll get:
422 (a, s') = newArray# 100 [] realWorld#{-NB-}
423 (_, s'') = fill_in_array_or_something a x s'
427 And now the @newArray#@ binding can be floated to become a CAF, which
428 is totally and utterly wrong:
431 (a, s') = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
434 let (_, s'') = fill_in_array_or_something a x s' in
437 All calls to @f@ will share a {\em single} array! End SLPJ 95/04.
439 @realWorld#@ used to be a magic literal, \tr{void#}. If things get
440 nasty as-is, change it back to a literal (@Literal@).
443 = pcMiscPrelId realWorldPrimIdKey pRELUDE_BUILTIN SLIT("realWorld#")
448 %************************************************************************
450 \subsection[PrelVals-foldr-build]{Values known about for ``foldr/build''}
452 %************************************************************************
456 = pcMiscPrelId buildIdKey pRELUDE_CORE SLIT("_build") buildTy
458 `addInfo_UF` mkMagicUnfolding buildIdKey)
459 `addInfo` mkStrictnessInfo [WwStrict] Nothing)
460 `addInfo` mkArgUsageInfo [ArgUsage 2])
461 `addInfo` pcGenerateSpecs buildIdKey buildId noIdInfo{-ToDo-} buildTy)
462 -- cheating, but since _build never actually exists ...
464 -- The type of this strange object is:
465 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a]
467 buildTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [build_ty] (mkListTy alphaTy))
469 build_ty = mkSigmaTy [betaTyVar] []
470 (mkFunTys [mkFunTys [alphaTy, betaTy] betaTy, betaTy] betaTy)
473 @mkBuild@ is sugar for building a build!
475 @mkbuild ty tv c n e@ $Rightarrow$ @build ty (/\ tv -> \ c n -> e)@
476 @ty@ is the type of the list.
477 @tv@ is always a new type variable.
478 @c,n@ are Id's for the abstract cons and nil, @g@ for let binding the argument argument.
481 v :: (\/ b . (a -> b -> b) -> b -> b) -> [a]
482 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a]
483 @e@ is the object right inside the @build@
491 -> CoreExpr -- template
492 -> CoreExpr -- template
494 mkBuild ty tv c n g expr
495 = Let (NonRec g (mkLam [tv] [c,n] expr))
496 (App (mkTyApp (Var buildId) [ty]) (VarArg g))
501 = pcMiscPrelId augmentIdKey pRELUDE_CORE SLIT("_augment") augmentTy
503 `addInfo_UF` mkMagicUnfolding augmentIdKey)
504 `addInfo` mkStrictnessInfo [WwStrict,WwLazy False] Nothing)
505 `addInfo` mkArgUsageInfo [ArgUsage 2,UnknownArgUsage])
506 -- cheating, but since _augment never actually exists ...
508 -- The type of this strange object is:
509 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a] -> [a]
511 augmentTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [aug_ty, mkListTy alphaTy] (mkListTy alphaTy))
513 aug_ty = mkSigmaTy [betaTyVar] []
514 (mkFunTys [mkFunTys [alphaTy, betaTy] betaTy, betaTy] betaTy)
518 foldrId = pcMiscPrelId foldrIdKey pRELUDE_FB{-not "List"-} SLIT("foldr")
522 mkSigmaTy [alphaTyVar, betaTyVar] []
523 (mkFunTys [mkFunTys [alphaTy, betaTy] betaTy, betaTy, mkListTy alphaTy] betaTy)
525 idInfo = (((((noIdInfo
526 `addInfo_UF` mkMagicUnfolding foldrIdKey)
527 `addInfo` mkStrictnessInfo [WwLazy False,WwLazy False,WwStrict] Nothing)
528 `addInfo` mkArityInfo 3)
529 `addInfo` mkUpdateInfo [2,2,1])
530 `addInfo` pcGenerateSpecs foldrIdKey foldrId noIdInfo{-ToDo-} foldrTy)
532 foldlId = pcMiscPrelId foldlIdKey pRELUDE_FB{-not "List"-} SLIT("foldl")
536 mkSigmaTy [alphaTyVar, betaTyVar] []
537 (mkFunTys [mkFunTys [alphaTy, betaTy] alphaTy, alphaTy, mkListTy betaTy] alphaTy)
539 idInfo = (((((noIdInfo
540 `addInfo_UF` mkMagicUnfolding foldlIdKey)
541 `addInfo` mkStrictnessInfo [WwLazy False,WwLazy False,WwStrict] Nothing)
542 `addInfo` mkArityInfo 3)
543 `addInfo` mkUpdateInfo [2,2,1])
544 `addInfo` pcGenerateSpecs foldlIdKey foldlId noIdInfo{-ToDo-} foldlTy)
546 -- A bit of magic goes no here. We translate appendId into ++,
547 -- you have to be carefull when you actually compile append:
548 -- xs ++ ys = augment (\ c n -> foldr c n xs) ys
549 -- {- unfold augment -}
551 -- {- fold foldr to append -}
552 -- = ys `appendId` xs
553 -- = ys ++ xs -- ugg!
554 -- *BUT* you want (++) and not _append in your interfaces.
556 -- So you have to turn *off* unfolding of foldr inside FoldrBuild.hs inside
561 = pcMiscPrelId appendIdKey pRELUDE_LIST SLIT("++") appendTy idInfo
564 (mkSigmaTy [alphaTyVar] []
565 (mkFunTys [mkListTy alphaTy, mkListTy alphaTy] (mkListTy alphaTy)))
567 `addInfo` mkStrictnessInfo [WwStrict,WwLazy False] Nothing)
568 `addInfo` mkArityInfo 2)
569 `addInfo` mkUpdateInfo [1,2])
572 %************************************************************************
574 \subsection[PrelUtils-specialisations]{Specialisations for builtin values}
576 %************************************************************************
578 The specialisations which exist for the builtin values must be recorded in
581 NOTE: THE USES OF THE pcGenerate... FUNCTIONS MUST CORRESPOND
582 TO THE SPECIALISATIONS DECLARED IN THE PRELUDE !!!
584 HACK: We currently use the same unique for the specialised Ids.
586 The list @specing_types@ determines the types for which specialised
587 versions are created. Note: This should correspond with the
588 types passed to the pre-processor with the -genSPECS arg (see ghc.lprl).
590 ToDo: Create single mkworld definition which is grabbed here and in ghc.lprl
593 pcGenerateSpecs :: Unique -> Id -> IdInfo -> Type -> SpecEnv
594 pcGenerateSpecs key id info ty
599 pc_gen_specs True key id info ty
601 pc_gen_specs is_id key id info ty
602 = mkSpecEnv spec_infos
604 spec_infos = [ let spec_ty = specialiseTy ty spec_tys 0
606 then mkSpecId key {- HACK WARNING: same unique! -}
607 id spec_tys spec_ty info
608 else panic "SpecData:SpecInfo:SpecId"
610 SpecInfo spec_tys (length ctxts) spec_id
611 | spec_tys <- specialisations ]
613 (tyvars, ctxts, _) = splitSigmaTy ty
614 no_tyvars = length tyvars
616 specialisations = if no_tyvars == 0
618 else tail (cross_product no_tyvars specing_types)
620 -- N.B. tail removes fully polymorphic specialisation
622 cross_product 0 tys = []
623 cross_product 1 tys = map (:[]) tys
624 cross_product n tys = concat [map (:cp) tys | cp <- cross_product (n-1) tys]
627 specing_types = [Nothing,