2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrelVals]{Prelude values the compiler ``knows about''}
9 #include "HsVersions.h"
11 import {-# SOURCE #-} CoreUnfold ( mkUnfolding )
13 import Id ( Id, mkVanillaId, mkTemplateLocals )
14 import SpecEnv ( SpecEnv, emptySpecEnv )
22 import CoreSyn -- quite a bit
23 import IdInfo -- quite a bit
24 import Name ( mkWiredInIdName, Module )
26 import TyVar ( openAlphaTyVar, alphaTyVar, betaTyVar, TyVar )
27 import Unique -- lots of *Keys
34 mk_inline_unfolding expr = setUnfoldingInfo (mkUnfolding expr) $
35 setInlinePragInfo IWantToBeINLINEd noIdInfo
37 exactArityInfo n = exactArity n `setArityInfo` noIdInfo
39 pcMiscPrelId :: Unique{-IdKey-} -> Module -> FAST_STRING -> Type -> IdInfo -> Id
41 pcMiscPrelId key mod occ ty info
43 name = mkWiredInIdName key mod occ imp
44 imp = mkVanillaId name ty info -- the usual case...
47 -- We lie and say the thing is imported; otherwise, we get into
48 -- a mess with dependency analysis; e.g., core2stg may heave in
49 -- random calls to GHCbase.unpackPS__. If GHCbase is the module
50 -- being compiled, then it's just a matter of luck if the definition
51 -- will be in "the right place" to be in scope.
54 %************************************************************************
56 \subsection[PrelVals-error-related]{@error@ and friends; @trace@}
58 %************************************************************************
60 GHC randomly injects these into the code.
62 @patError@ is just a version of @error@ for pattern-matching
63 failures. It knows various ``codes'' which expand to longer
64 strings---this saves space!
66 @absentErr@ is a thing we put in for ``absent'' arguments. They jolly
67 well shouldn't be yanked on, but if one is, then you will get a
68 friendly message from @absentErr@ (rather a totally random crash).
70 @parError@ is a special version of @error@ which the compiler does
71 not know to be a bottoming Id. It is used in the @_par_@ and @_seq_@
72 templates, but we don't ever expect to generate code for it.
75 pc_bottoming_Id key mod name ty
76 = pcMiscPrelId key mod name ty bottoming_info
78 bottoming_info = mkBottomStrictnessInfo `setStrictnessInfo` noIdInfo
79 -- these "bottom" out, no matter what their arguments
82 = pc_bottoming_Id errorIdKey pREL_ERR SLIT("error") errorTy
85 = pc_bottoming_Id u pREL_ERR n errorTy
88 = generic_ERROR_ID recSelErrIdKey SLIT("patError")
90 = generic_ERROR_ID patErrorIdKey SLIT("patError")
92 = generic_ERROR_ID recConErrorIdKey SLIT("recConError")
94 = generic_ERROR_ID recUpdErrorIdKey SLIT("recUpdError")
96 = generic_ERROR_ID irrefutPatErrorIdKey SLIT("irrefutPatError")
97 nON_EXHAUSTIVE_GUARDS_ERROR_ID
98 = generic_ERROR_ID nonExhaustiveGuardsErrorIdKey SLIT("nonExhaustiveGuardsError")
99 nO_METHOD_BINDING_ERROR_ID
100 = generic_ERROR_ID noMethodBindingErrorIdKey SLIT("noMethodBindingError")
103 = pc_bottoming_Id absentErrorIdKey pREL_ERR SLIT("absentErr")
104 (mkSigmaTy [openAlphaTyVar] [] openAlphaTy)
107 = pcMiscPrelId parErrorIdKey pREL_ERR SLIT("parError")
108 (mkSigmaTy [openAlphaTyVar] [] openAlphaTy) noIdInfo
110 openAlphaTy = mkTyVarTy openAlphaTyVar
113 errorTy = mkSigmaTy [openAlphaTyVar] [] (mkFunTys [mkListTy charTy] openAlphaTy)
114 -- Notice the openAlphaTyVar. It says that "error" can be applied
115 -- to unboxed as well as boxed types. This is OK because it never
116 -- returns, so the return type is irrelevant.
119 unsafeCoerce# isn't so much a PrimOp as a phantom identifier, that
120 just gets expanded into a type coercion wherever it occurs. Hence we
121 add it as a built-in Id with an unfolding here.
125 = pcMiscPrelId unsafeCoerceIdKey pREL_GHC SLIT("unsafeCoerce#") ty
126 (mk_inline_unfolding template)
128 ty = mkForAllTys [alphaTyVar,betaTyVar] (mkFunTy alphaTy betaTy)
129 [x] = mkTemplateLocals [alphaTy]
130 template = mkLam [alphaTyVar,betaTyVar] [x] (
131 Note (Coerce betaTy alphaTy) (Var x))
134 We want \tr{GHCbase.trace} to be wired in
135 because we don't want the strictness analyser to get ahold of it,
136 decide that the second argument is strict, evaluate that first (!!),
137 and make a jolly old mess.
140 = pcMiscPrelId traceIdKey pREL_IO_BASE SLIT("trace") traceTy
141 (pcGenerateSpecs traceIdKey tRACE_ID noIdInfo traceTy `setSpecInfo` noIdInfo)
143 traceTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [mkListTy charTy, alphaTy] alphaTy)
146 %************************************************************************
148 \subsection[PrelVals-Integer-support]{To support @Integer@ and @String@ literals}
150 %************************************************************************
154 = pcMiscPrelId packCStringIdKey{-ToDo:rename-} pREL_PACK SLIT("packCString#")
155 (mkFunTys [stringTy] byteArrayPrimTy) noIdInfo
157 --------------------------------------------------------------------
160 = pcMiscPrelId unpackCStringIdKey pREL_PACK SLIT("unpackCString#")
161 (mkFunTys [addrPrimTy{-a char *-}] stringTy) noIdInfo
163 -- (FunTy addrPrimTy{-a char *-} stringTy) (exactArityInfo 1)
164 -- but I don't like wired-in IdInfos (WDP)
166 unpackCString2Id -- for cases when a string has a NUL in it
167 = pcMiscPrelId unpackCString2IdKey pREL_PACK SLIT("unpackNBytes#")
168 (mkFunTys [addrPrimTy{-a char *-}, intPrimTy{-length-}] stringTy)
171 --------------------------------------------------------------------
172 unpackCStringAppendId
173 = pcMiscPrelId unpackCStringAppendIdKey pREL_PACK SLIT("unpackAppendCString#")
174 (mkFunTys [addrPrimTy{-a "char *" pointer-},stringTy] stringTy)
178 = pcMiscPrelId unpackCStringFoldrIdKey pREL_PACK SLIT("unpackFoldrCString#")
179 (mkSigmaTy [alphaTyVar] []
180 (mkFunTys [addrPrimTy{-a "char *" pointer-},
181 mkFunTys [charTy, alphaTy] alphaTy,
187 OK, this is Will's idea: we should have magic values for Integers 0,
188 +1, +2, and -1 (go ahead, fire me):
192 = pcMiscPrelId integerZeroIdKey pREL_NUM SLIT("integer_0") integerTy noIdInfo
194 = pcMiscPrelId integerPlusOneIdKey pREL_NUM SLIT("integer_1") integerTy noIdInfo
196 = pcMiscPrelId integerPlusTwoIdKey pREL_NUM SLIT("integer_2") integerTy noIdInfo
198 = pcMiscPrelId integerMinusOneIdKey pREL_NUM SLIT("integer_m1") integerTy noIdInfo
201 %************************************************************************
203 \subsection[PrelVals-parallel]{@seq@ and @par@: for parallel operation (only)}
205 %************************************************************************
209 --------------------------------------------------------------------
210 -- seqId :: "seq", used w/ GRIP, etc., is really quite similar to
214 seq = /\ a b -> \ x y -> case x of { _ -> y }
217 seq = /\ a b -> \ x y -> case seq# x y of { _Lift y' -> y' }
220 seq = /\ a b -> \ x::a y::b -> case seq# x of { 0# -> parError#; _ -> y; }
224 seqId = pcMiscPrelId seqIdKey pRELUDE SLIT("seq")
225 (mkSigmaTy [alphaTyVar, betaTyVar] []
226 (mkFunTys [alphaTy, betaTy] betaTy))
227 (mk_inline_unfolding seq_template)
237 = mkLam [alphaTyVar, betaTyVar] [x, y] (
238 Case (Prim SeqOp [TyArg alphaTy, VarArg x]) (
240 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
241 (BindDefault z (Var y))))
243 --------------------------------------------------------------------
244 -- parId :: "par", also used w/ GRIP, etc.
248 par = /\ a b -> \ x y -> case (par# (case x of { _ -> () })) of { _ -> y }
252 par = /\ a b -> \ x y -> case par# x y of { _Lift y' -> y' }
256 par = /\ a b -> \ x::a y::b -> case par# x of { 0# -> parError#; _ -> y; }
259 parId = pcMiscPrelId parIdKey cONC_BASE SLIT("par")
260 (mkSigmaTy [alphaTyVar, betaTyVar] []
261 (mkFunTys [alphaTy, betaTy] betaTy))
262 (mk_inline_unfolding par_template)
272 = mkLam [alphaTyVar, betaTyVar] [x, y] (
273 Case (Prim ParOp [TyArg alphaTy, VarArg x]) (
275 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
276 (BindDefault z (Var y))))
278 -- forkId :: "fork", for *required* concurrent threads
280 _fork_ = /\ a b -> \ x::a y::b -> case fork# x of { 0# -> parError#; _ -> y; }
282 forkId = pcMiscPrelId forkIdKey cONC_BASE SLIT("fork")
283 (mkSigmaTy [alphaTyVar, betaTyVar] []
284 (mkFunTys [alphaTy, betaTy] betaTy))
285 (mk_inline_unfolding fork_template)
295 = mkLam [alphaTyVar, betaTyVar] [x, y] (
296 Case (Prim ForkOp [TyArg alphaTy, VarArg x]) (
298 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
299 (BindDefault z (Var y))))
306 parLocalId = pcMiscPrelId parLocalIdKey cONC_BASE SLIT("parLocal")
307 (mkSigmaTy [alphaTyVar, betaTyVar] []
308 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy, alphaTy, betaTy] betaTy))
309 (mk_inline_unfolding parLocal_template)
311 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
312 [w, g, s, p, x, y, z]
324 = mkLam [alphaTyVar, betaTyVar] [w, g, s, p, x, y] (
325 Case (Prim ParLocalOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
327 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
328 (BindDefault z (Var y))))
330 parGlobalId = pcMiscPrelId parGlobalIdKey cONC_BASE SLIT("parGlobal")
331 (mkSigmaTy [alphaTyVar, betaTyVar] []
332 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy, alphaTy, betaTy] betaTy))
333 (mk_inline_unfolding parGlobal_template)
335 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
336 [w, g, s, p, x, y, z]
348 = mkLam [alphaTyVar, betaTyVar] [w, g, s, p, x, y] (
349 Case (Prim ParGlobalOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
351 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
352 (BindDefault z (Var y))))
355 parAtId = pcMiscPrelId parAtIdKey cONC_BASE SLIT("parAt")
356 (mkSigmaTy [alphaTyVar, betaTyVar] []
357 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy,
358 alphaTy, betaTy, gammaTy] gammaTy))
359 (mk_inline_unfolding parAt_template)
361 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
362 [w, g, s, p, v, x, y, z]
375 = mkLam [alphaTyVar, betaTyVar, gammaTyVar] [w, g, s, p, v, x, y] (
376 Case (Prim ParAtOp [TyArg alphaTy, TyArg betaTy, TyArg gammaTy, VarArg x, VarArg v, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
378 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [gammaTy])]
379 (BindDefault z (Var y))))
381 parAtAbsId = pcMiscPrelId parAtAbsIdKey cONC_BASE SLIT("parAtAbs")
382 (mkSigmaTy [alphaTyVar, betaTyVar] []
383 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy, intPrimTy, alphaTy, betaTy] betaTy))
384 (mk_inline_unfolding parAtAbs_template)
386 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
387 [w, g, s, p, v, x, y, z]
400 = mkLam [alphaTyVar, betaTyVar] [w, g, s, p, v, x, y] (
401 Case (Prim ParAtAbsOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg v, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
403 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
404 (BindDefault z (Var y))))
406 parAtRelId = pcMiscPrelId parAtRelIdKey cONC_BASE SLIT("parAtRel")
407 (mkSigmaTy [alphaTyVar, betaTyVar] []
408 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy, intPrimTy, alphaTy, betaTy] betaTy))
409 (mk_inline_unfolding parAtRel_template)
411 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
412 [w, g, s, p, v, x, y, z]
425 = mkLam [alphaTyVar, betaTyVar] [w, g, s, p, v, x, y] (
426 Case (Prim ParAtRelOp [TyArg alphaTy, TyArg betaTy, VarArg x, VarArg v, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
428 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [betaTy])]
429 (BindDefault z (Var y))))
431 parAtForNowId = pcMiscPrelId parAtForNowIdKey cONC_BASE SLIT("parAtForNow")
432 (mkSigmaTy [alphaTyVar, betaTyVar] []
433 (mkFunTys [intPrimTy, intPrimTy, intPrimTy, intPrimTy,
434 alphaTy, betaTy, gammaTy] gammaTy))
435 (mk_inline_unfolding parAtForNow_template)
437 -- Annotations: w: name, g: gran. info, s: size info, p: par info -- HWL
438 [w, g, s, p, v, x, y, z]
451 = mkLam [alphaTyVar, betaTyVar, gammaTyVar] [w, g, s, p, v, x, y] (
452 Case (Prim ParAtForNowOp [TyArg alphaTy, TyArg betaTy, TyArg gammaTy, VarArg x, VarArg v, VarArg w, VarArg g, VarArg s, VarArg p, VarArg y]) (
454 [(mkMachInt 0, mkTyApp (Var pAR_ERROR_ID) [gammaTy])]
455 (BindDefault z (Var y))))
457 -- copyable and noFollow are currently merely hooks: they are translated into
458 -- calls to the macros COPYABLE and NOFOLLOW -- HWL
460 copyableId = pcMiscPrelId copyableIdKey cONC_BASE SLIT("copyable")
461 (mkSigmaTy [alphaTyVar] []
463 (mk_inline_unfolding copyable_template)
465 -- Annotations: x: closure that's tagged to by copyable
473 = mkLam [alphaTyVar] [x] ( Prim CopyableOp [TyArg alphaTy, VarArg x] )
475 noFollowId = pcMiscPrelId noFollowIdKey cONC_BASE SLIT("noFollow")
476 (mkSigmaTy [alphaTyVar] []
478 (mk_inline_unfolding noFollow_template)
480 -- Annotations: x: closure that's tagged to not follow
488 = mkLam [alphaTyVar] [x] ( Prim NoFollowOp [TyArg alphaTy, VarArg x] )
492 @realWorld#@ used to be a magic literal, \tr{void#}. If things get
493 nasty as-is, change it back to a literal (@Literal@).
496 = pcMiscPrelId realWorldPrimIdKey pREL_GHC SLIT("realWorld#")
502 voidId = pc_bottoming_Id voidIdKey pREL_BASE SLIT("void") voidTy
505 %************************************************************************
507 \subsection[PrelVals-foldr-build]{Values known about for ``foldr/build''}
509 %************************************************************************
513 = pcMiscPrelId buildIdKey pREL_ERR SLIT("build") buildTy
515 {- LATER:`addUnfoldInfo` mkMagicUnfolding buildIdKey)
516 `addStrictnessInfo` mkStrictnessInfo [WwStrict] False)
517 `addArgUsageInfo` mkArgUsageInfo [ArgUsage 2])
518 `setSpecInfo` pcGenerateSpecs buildIdKey buildId noIdInfo{-ToDo-} buildTy)
520 -- cheating, but since _build never actually exists ...
522 -- The type of this strange object is:
523 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a]
525 buildTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [build_ty] (mkListTy alphaTy))
527 build_ty = mkSigmaTy [betaTyVar] []
528 (mkFunTys [mkFunTys [alphaTy, betaTy] betaTy, betaTy] betaTy)
531 @mkBuild@ is sugar for building a build!
533 @mkbuild ty tv c n e@ $Rightarrow$ @build ty (/\ tv -> \ c n -> e)@
534 @ty@ is the type of the list.
535 @tv@ is always a new type variable.
536 @c,n@ are Id's for the abstract cons and nil, @g@ for let binding the argument argument.
539 v :: (\/ b . (a -> b -> b) -> b -> b) -> [a]
540 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a]
541 @e@ is the object right inside the @build@
549 -> CoreExpr -- template
550 -> CoreExpr -- template
552 mkBuild ty tv c n g expr
553 = Let (NonRec g (mkLam [tv] [c,n] expr))
554 (App (mkTyApp (Var buildId) [ty]) (VarArg g))
559 = pcMiscPrelId augmentIdKey pREL_ERR SLIT("augment") augmentTy
561 {- LATER:`addUnfoldInfo` mkMagicUnfolding augmentIdKey)
562 `addStrictnessInfo` mkStrictnessInfo [WwStrict,WwLazy False] False)
563 `addArgUsageInfo` mkArgUsageInfo [ArgUsage 2,UnknownArgUsage])
565 -- cheating, but since _augment never actually exists ...
567 -- The type of this strange object is:
568 -- \/ a . (\/ b . (a -> b -> b) -> b -> b) -> [a] -> [a]
570 augmentTy = mkSigmaTy [alphaTyVar] [] (mkFunTys [aug_ty, mkListTy alphaTy] (mkListTy alphaTy))
572 aug_ty = mkSigmaTy [betaTyVar] []
573 (mkFunTys [mkFunTys [alphaTy, betaTy] betaTy, betaTy] betaTy)
577 foldrId = pcMiscPrelId foldrIdKey pREL_BASE SLIT("foldr")
581 mkSigmaTy [alphaTyVar, betaTyVar] []
582 (mkFunTys [mkFunTys [alphaTy, betaTy] betaTy, betaTy, mkListTy alphaTy] betaTy)
585 {- LATER: mkStrictnessInfo [WwLazy False,WwLazy False,WwStrict] False `setStrictnessInfo`
586 exactArity 3 `setArityInfo`
587 mkUpdateInfo [2,2,1] `setUpdateInfo`
588 pcGenerateSpecs foldrIdKey foldrId noIdInfo{-ToDo-} foldrTy `setSpecInfo`
592 foldlId = pcMiscPrelId foldlIdKey pREL_LIST SLIT("foldl")
596 mkSigmaTy [alphaTyVar, betaTyVar] []
597 (mkFunTys [mkFunTys [alphaTy, betaTy] alphaTy, alphaTy, mkListTy betaTy] alphaTy)
600 {- LATER: `addUnfoldInfo` mkMagicUnfolding foldlIdKey)
601 `addStrictnessInfo` mkStrictnessInfo [WwLazy False,WwLazy False,WwStrict] False)
602 `addArityInfo` exactArity 3)
603 `addUpdateInfo` mkUpdateInfo [2,2,1])
604 `setSpecInfo` pcGenerateSpecs foldlIdKey foldlId noIdInfo{-ToDo-} foldlTy)
607 -- A bit of magic goes no here. We translate appendId into ++,
608 -- you have to be carefull when you actually compile append:
609 -- xs ++ ys = augment (\ c n -> foldr c n xs) ys
610 -- {- unfold augment -}
612 -- {- fold foldr to append -}
613 -- = ys `appendId` xs
614 -- = ys ++ xs -- ugg!
615 -- *BUT* you want (++) and not _append in your interfaces.
617 -- So you have to turn *off* unfolding of foldr inside FoldrBuild.hs inside
620 {- OLD: doesn't apply with 1.3
622 = pcMiscPrelId appendIdKey mONAD SLIT("++") appendTy idInfo
625 (mkSigmaTy [alphaTyVar] []
626 (mkFunTys [mkListTy alphaTy, mkListTy alphaTy] (mkListTy alphaTy)))
628 `addStrictnessInfo` mkStrictnessInfo [WwStrict,WwLazy False] False)
629 `addArityInfo` exactArity 2)
630 `addUpdateInfo` mkUpdateInfo [1,2])
634 %************************************************************************
636 \subsection[PrelUtils-specialisations]{Specialisations for builtin values}
638 %************************************************************************
640 The specialisations which exist for the builtin values must be recorded in
643 NOTE: THE USES OF THE pcGenerate... FUNCTIONS MUST CORRESPOND
644 TO THE SPECIALISATIONS DECLARED IN THE PRELUDE !!!
646 HACK: We currently use the same unique for the specialised Ids.
648 The list @specing_types@ determines the types for which specialised
649 versions are created. Note: This should correspond with the
650 types passed to the pre-processor with the -genSPECS arg (see ghc.lprl).
652 ToDo: Create single mkworld definition which is grabbed here and in ghc.lprl
655 pcGenerateSpecs :: Unique -> Id -> IdInfo -> Type -> IdSpecEnv
656 pcGenerateSpecs key id info ty
661 pc_gen_specs True key id info ty
663 pc_gen_specs is_id key id info ty
664 = mkSpecEnv spec_infos
666 spec_infos = [ let spec_ty = specialiseTy ty spec_tys 0
668 then mkSpecId key {- HACK WARNING: same unique! -}
669 id spec_tys spec_ty info
670 else panic "SpecData:SpecInfo:SpecId"
672 SpecInfo spec_tys (length ctxts) spec_id
673 | spec_tys <- specialisations ]
675 (tyvars, ctxts, _) = splitSigmaTy ty
676 no_tyvars = length tyvars
678 specialisations = if no_tyvars == 0
680 else tail (cross_product no_tyvars specing_types)
682 -- N.B. tail removes fully polymorphic specialisation
684 cross_product 0 tys = []
685 cross_product 1 tys = map (:[]) tys
686 cross_product n tys = concat [map (:cp) tys | cp <- cross_product (n-1) tys]
689 specing_types = [Nothing,