[project @ 1996-04-20 10:37:06 by partain]
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreLint.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1995
3 %
4 \section[CoreLint]{A ``lint'' pass to check for Core correctness}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module CoreLint (
10         lintCoreBindings,
11         lintUnfolding
12     ) where
13
14 import Ubiq
15
16 import CoreSyn
17
18 import Bag
19 import Kind             ( isSubKindOf, Kind{-instance-} )
20 import Literal          ( literalType, Literal{-instance-} )
21 import Id               ( idType, isBottomingId,
22                           dataConArgTys, GenId{-instances-}
23                         )
24 import Maybes           ( catMaybes )
25 import Name             ( isLocallyDefined, getSrcLoc )
26 import Outputable       ( Outputable(..){-instance * []-} )
27 import PprCore
28 import PprStyle         ( PprStyle(..) )
29 import PprType          ( GenType, GenTyVar, TyCon )
30 import Pretty
31 import PrimOp           ( primOpType, PrimOp(..) )
32 import PrimRep          ( PrimRep(..) )
33 import SrcLoc           ( SrcLoc )
34 import Type             ( mkFunTy,getFunTy_maybe,mkForAllTy,getForAllTy_maybe,
35                           isPrimType,typeKind,instantiateTy,
36                           mkForAllUsageTy,getForAllUsageTy,instantiateUsage,
37                           maybeAppDataTyCon, eqTy
38                         )
39 import TyCon            ( isPrimTyCon, tyConFamilySize )
40 import TyVar            ( tyVarKind, GenTyVar{-instances-} )
41 import UniqSet          ( emptyUniqSet, mkUniqSet, intersectUniqSets,
42                           unionUniqSets, elementOfUniqSet, UniqSet(..)
43                         )
44 import Unique           ( Unique )
45 import Usage            ( GenUsage )
46 import Util             ( zipEqual, pprTrace, pprPanic, assertPanic, panic )
47
48 infixr 9 `thenL`, `seqL`, `thenMaybeL`, `seqMaybeL`
49 \end{code}
50
51 %************************************************************************
52 %*                                                                      *
53 \subsection[lintCoreBindings]{@lintCoreBindings@: Top-level interface}
54 %*                                                                      *
55 %************************************************************************
56
57 Checks that a set of core bindings is well-formed.  The PprStyle and String
58 just control what we print in the event of an error.  The Bool value
59 indicates whether we have done any specialisation yet (in which case we do
60 some extra checks).
61
62 We check for
63         (a) type errors
64         (b) Out-of-scope type variables
65         (c) Out-of-scope local variables
66         (d) Ill-kinded types
67
68 If we have done specialisation the we check that there are
69         (a) No top-level bindings of primitive (unboxed type)
70
71 Outstanding issues:
72
73     --
74     -- Things are *not* OK if:
75     --
76     -- * Unsaturated type app before specialisation has been done;
77     --
78     -- * Oversaturated type app after specialisation (eta reduction
79     --   may well be happening...);
80     --
81     -- Note: checkTyApp is usually followed by a call to checkSpecTyApp.
82     --
83
84 \begin{code}
85 lintCoreBindings
86         :: PprStyle -> String -> Bool -> [CoreBinding] -> [CoreBinding]
87
88 lintCoreBindings sty whoDunnit spec_done binds
89   = case (initL (lint_binds binds) spec_done) of
90       Nothing  -> binds
91       Just msg ->
92         pprPanic "" (ppAboves [
93           ppStr ("*** Core Lint Errors: in " ++ whoDunnit ++ " ***"),
94           msg sty,
95           ppStr "*** Offending Program ***",
96           ppAboves (map (pprCoreBinding sty) binds),
97           ppStr "*** End of Offense ***"
98         ])
99   where
100     lint_binds [] = returnL ()
101     lint_binds (bind:binds)
102       = lintCoreBinding bind `thenL` \binders ->
103         addInScopeVars binders (lint_binds binds)
104 \end{code}
105
106 %************************************************************************
107 %*                                                                      *
108 \subsection[lintUnfolding]{lintUnfolding}
109 %*                                                                      *
110 %************************************************************************
111
112 We use this to check all unfoldings that come in from interfaces
113 (it is very painful to catch errors otherwise):
114
115 \begin{code}
116 lintUnfolding :: SrcLoc -> CoreExpr -> Maybe CoreExpr
117
118 lintUnfolding locn expr
119   = case
120       (initL (addLoc (ImportedUnfolding locn) (lintCoreExpr expr))
121        True{-pretend spec done-})
122     of
123       Nothing  -> Just expr
124       Just msg ->
125         pprTrace "WARNING: Discarded bad unfolding from interface:\n"
126         (ppAboves [msg PprForUser,
127                    ppStr "*** Bad unfolding ***",
128                    ppr PprDebug expr,
129                    ppStr "*** End unfolding ***"])
130         Nothing
131 \end{code}
132
133 %************************************************************************
134 %*                                                                      *
135 \subsection[lintCoreBinding]{lintCoreBinding}
136 %*                                                                      *
137 %************************************************************************
138
139 Check a core binding, returning the list of variables bound.
140
141 \begin{code}
142 lintCoreBinding :: CoreBinding -> LintM [Id]
143
144 lintCoreBinding (NonRec binder rhs)
145   = lintSingleBinding (binder,rhs) `seqL` returnL [binder]
146
147 lintCoreBinding (Rec pairs)
148   = addInScopeVars binders (
149       mapL lintSingleBinding pairs `seqL` returnL binders
150     )
151   where
152     binders = [b | (b,_) <- pairs]
153
154 lintSingleBinding (binder,rhs)
155   = addLoc (RhsOf binder) (
156         -- Check the rhs
157         lintCoreExpr rhs
158
159         `thenL` \maybe_ty ->
160         -- Check match to RHS type
161         (case maybe_ty of
162           Nothing -> returnL ()
163           Just ty -> checkTys (idType binder) ty (mkRhsMsg binder ty))
164
165         `seqL`
166         -- Check (not isPrimType)
167         checkIfSpecDoneL (not (isPrimType (idType binder)))
168           (mkRhsPrimMsg binder rhs)
169
170         -- We should check the unfolding, if any, but this is tricky because
171         -- the unfolding is a SimplifiableCoreExpr. Give up for now.
172     )
173 \end{code}
174
175 %************************************************************************
176 %*                                                                      *
177 \subsection[lintCoreExpr]{lintCoreExpr}
178 %*                                                                      *
179 %************************************************************************
180
181 \begin{code}
182 lintCoreExpr :: CoreExpr -> LintM (Maybe Type)  -- Nothing if error found
183
184 lintCoreExpr (Var var) = checkInScope var `seqL` returnL (Just (idType var))
185 lintCoreExpr (Lit lit) = returnL (Just (literalType lit))
186 lintCoreExpr (SCC _ expr) = lintCoreExpr expr
187
188 lintCoreExpr (Let binds body)
189   = lintCoreBinding binds `thenL` \binders ->
190     if (null binders) then
191         lintCoreExpr body  -- Can't add a new source location
192     else
193       addLoc (BodyOfLetRec binders)
194         (addInScopeVars binders (lintCoreExpr body))
195
196 lintCoreExpr e@(Con con args)
197   = lintCoreArgs False e (idType con) args
198     -- Note: we don't check for primitive types in these arguments
199
200 lintCoreExpr e@(Prim op args)
201   = lintCoreArgs True e (primOpType op) args
202     -- Note: we do check for primitive types in these arguments
203
204 lintCoreExpr e@(App fun@(Var v) arg) | isBottomingId v
205   = lintCoreExpr fun `thenMaybeL` \ ty -> lintCoreArg False e ty arg
206     -- Note: we don't check for primitive types in argument to 'error'
207
208 lintCoreExpr e@(App fun arg)
209   = lintCoreExpr fun `thenMaybeL` \ty -> lintCoreArg True e ty arg
210     -- Note: we do check for primitive types in this argument
211
212 lintCoreExpr (Lam (ValBinder var) expr)
213   = addLoc (LambdaBodyOf var)
214       (addInScopeVars [var]
215         (lintCoreExpr expr `thenMaybeL` \ty ->
216          returnL (Just (mkFunTy (idType var) ty))))
217
218 lintCoreExpr (Lam (TyBinder tyvar) expr)
219   = lintCoreExpr expr `thenMaybeL` \ty ->
220     returnL (Just(mkForAllTy tyvar ty))
221     -- ToDo: Should add in-scope type variable at this point
222
223 lintCoreExpr e@(Case scrut alts)
224  = lintCoreExpr scrut `thenMaybeL` \ty ->
225    -- Check that it is a data type
226    case maybeAppDataTyCon ty of
227      Nothing -> addErrL (mkCaseDataConMsg e) `seqL` returnL Nothing
228      Just(tycon, _, _) -> lintCoreAlts alts ty tycon
229 \end{code}
230
231 %************************************************************************
232 %*                                                                      *
233 \subsection[lintCoreArgs]{lintCoreArgs}
234 %*                                                                      *
235 %************************************************************************
236
237 The boolean argument indicates whether we should flag type
238 applications to primitive types as being errors.
239
240 \begin{code}
241 lintCoreArgs :: Bool -> CoreExpr -> Type -> [CoreArg] -> LintM (Maybe Type)
242
243 lintCoreArgs _          _ ty [] = returnL (Just ty)
244 lintCoreArgs checkTyApp e ty (a : args)
245   = lintCoreArg  checkTyApp e ty  a `thenMaybeL` \ res ->
246     lintCoreArgs checkTyApp e res args
247 \end{code}
248
249 %************************************************************************
250 %*                                                                      *
251 \subsection[lintCoreArg]{lintCoreArg}
252 %*                                                                      *
253 %************************************************************************
254
255 \begin{code}
256 lintCoreArg :: Bool -> CoreExpr -> Type -> CoreArg -> LintM (Maybe Type)
257
258 lintCoreArg _ e ty (LitArg lit)
259   = -- Make sure function type matches argument
260     case (getFunTy_maybe ty) of
261       Just (arg,res) | (literalType lit `eqTy` arg) -> returnL(Just res)
262       _ -> addErrL (mkAppMsg ty (literalType lit) e) `seqL` returnL Nothing
263
264 lintCoreArg _ e ty (VarArg v)
265   = -- Make sure variable is bound
266     checkInScope v `seqL`
267     -- Make sure function type matches argument
268     case (getFunTy_maybe ty) of
269       Just (arg,res) | (idType v `eqTy` arg) -> returnL(Just res)
270       _ -> addErrL (mkAppMsg ty (idType v) e) `seqL` returnL Nothing
271
272 lintCoreArg checkTyApp e ty a@(TyArg arg_ty)
273   = -- ToDo: Check that ty is well-kinded and has no unbound tyvars
274     checkIfSpecDoneL (not (isPrimType arg_ty)) (mkSpecTyAppMsg a)
275     `seqL`
276     case (getForAllTy_maybe ty) of
277       Nothing -> addErrL (mkTyAppMsg SLIT("Illegal") ty arg_ty e) `seqL` returnL Nothing
278
279       Just (tyvar,body) ->
280         if (tyVarKind tyvar `isSubKindOf` typeKind arg_ty) then
281             returnL(Just(instantiateTy [(tyvar,arg_ty)] body))
282         else
283             pprTrace "lintCoreArg:kinds:" (ppCat [ppr PprDebug (tyVarKind tyvar), ppr PprDebug (typeKind arg_ty)]) $
284             addErrL (mkTyAppMsg SLIT("Kinds not right in") ty arg_ty e) `seqL` returnL Nothing
285         
286 lintCoreArg _ e ty (UsageArg u)
287   = -- ToDo: Check that usage has no unbound usage variables
288     case (getForAllUsageTy ty) of
289       Just (uvar,bounds,body) ->
290         -- ToDo: Check argument satisfies bounds
291         returnL(Just(panic "lintCoreArg:instantiateUsage uvar u body"))
292       _ -> addErrL (mkUsageAppMsg ty u e) `seqL` returnL Nothing
293 \end{code}
294
295 %************************************************************************
296 %*                                                                      *
297 \subsection[lintCoreAlts]{lintCoreAlts}
298 %*                                                                      *
299 %************************************************************************
300
301 \begin{code}
302 lintCoreAlts :: CoreCaseAlts
303              -> Type                    -- Type of scrutinee
304              -> TyCon                   -- TyCon pinned on the case
305              -> LintM (Maybe Type)      -- Type of alternatives
306
307 lintCoreAlts whole_alts@(AlgAlts alts deflt) ty tycon
308   = -- Check tycon is not a primitive tycon
309     addErrIfL (isPrimTyCon tycon) (mkCasePrimMsg tycon)
310     `seqL`
311     -- Check we are scrutinising a proper datatype
312     -- (ToDo: robustify)
313     addErrIfL (not (tyConFamilySize tycon >= 1)) (mkCaseAbstractMsg tycon)
314     `seqL`
315     lintDeflt deflt ty
316     `thenL` \maybe_deflt_ty ->
317     mapL (lintAlgAlt ty tycon) alts
318     `thenL` \maybe_alt_tys ->
319     -- Check the result types
320     case catMaybes (maybe_deflt_ty : maybe_alt_tys) of
321       []             -> returnL Nothing
322
323       (first_ty:tys) -> mapL check tys  `seqL`
324                         returnL (Just first_ty)
325         where
326           check ty = checkTys first_ty ty (mkCaseAltMsg whole_alts)
327
328 lintCoreAlts whole_alts@(PrimAlts alts deflt) ty tycon
329   = -- Check tycon is a primitive tycon
330     addErrIfL (not (isPrimTyCon tycon)) (mkCaseNotPrimMsg tycon)
331     `seqL`
332     mapL (lintPrimAlt ty) alts
333     `thenL` \maybe_alt_tys ->
334     lintDeflt deflt ty
335     `thenL` \maybe_deflt_ty ->
336     -- Check the result types
337     case catMaybes (maybe_deflt_ty : maybe_alt_tys) of
338       []             -> returnL Nothing
339
340       (first_ty:tys) -> mapL check tys  `seqL`
341                         returnL (Just first_ty)
342         where
343           check ty = checkTys first_ty ty (mkCaseAltMsg whole_alts)
344
345 lintAlgAlt scrut_ty tycon{-ToDo: use it!-} (con,args,rhs)
346   = (case maybeAppDataTyCon scrut_ty of
347       Nothing ->
348          addErrL (mkAlgAltMsg1 scrut_ty)
349       Just (tycon, tys_applied, cons) ->
350          let
351            arg_tys = dataConArgTys con tys_applied
352          in
353          checkL (con `elem` cons) (mkAlgAltMsg2 scrut_ty con) `seqL`
354          checkL (length arg_tys == length args) (mkAlgAltMsg3 con args)
355                                                                  `seqL`
356          mapL check (arg_tys `zipEqual` args)                    `seqL`
357          returnL ()
358     )                                                            `seqL`
359     addInScopeVars args         (
360          lintCoreExpr rhs
361     )
362   where
363     check (ty, arg) = checkTys ty (idType arg) (mkAlgAltMsg4 ty arg)
364
365     -- elem: yes, the elem-list here can sometimes be long-ish,
366     -- but as it's use-once, probably not worth doing anything different
367     -- We give it its own copy, so it isn't overloaded.
368     elem _ []       = False
369     elem x (y:ys)   = x==y || elem x ys
370
371 lintPrimAlt ty alt@(lit,rhs)
372  = checkTys (literalType lit) ty (mkPrimAltMsg alt) `seqL`
373    lintCoreExpr rhs
374
375 lintDeflt NoDefault _ = returnL Nothing
376 lintDeflt deflt@(BindDefault binder rhs) ty
377   = checkTys (idType binder) ty (mkDefltMsg deflt) `seqL`
378     addInScopeVars [binder] (lintCoreExpr rhs)
379 \end{code}
380
381 %************************************************************************
382 %*                                                                      *
383 \subsection[lint-monad]{The Lint monad}
384 %*                                                                      *
385 %************************************************************************
386
387 \begin{code}
388 type LintM a = Bool             -- True <=> specialisation has been done
389             -> [LintLocInfo]    -- Locations
390             -> UniqSet Id       -- Local vars in scope
391             -> Bag ErrMsg       -- Error messages so far
392             -> (a, Bag ErrMsg)  -- Result and error messages (if any)
393
394 type ErrMsg = PprStyle -> Pretty
395
396 data LintLocInfo
397   = RhsOf Id            -- The variable bound
398   | LambdaBodyOf Id     -- The lambda-binder
399   | BodyOfLetRec [Id]   -- One of the binders
400   | ImportedUnfolding SrcLoc -- Some imported unfolding (ToDo: say which)
401
402 instance Outputable LintLocInfo where
403     ppr sty (RhsOf v)
404       = ppBesides [ppr sty (getSrcLoc v), ppStr ": [RHS of ", pp_binders sty [v], ppStr "]"]
405
406     ppr sty (LambdaBodyOf b)
407       = ppBesides [ppr sty (getSrcLoc b),
408                 ppStr ": [in body of lambda with binder ", pp_binder sty b, ppStr "]"]
409
410     ppr sty (BodyOfLetRec bs)
411       = ppBesides [ppr sty (getSrcLoc (head bs)),
412                 ppStr ": [in body of letrec with binders ", pp_binders sty bs, ppStr "]"]
413
414     ppr sty (ImportedUnfolding locn)
415       = ppBeside (ppr sty locn) (ppStr ": [in an imported unfolding]")
416
417 pp_binders :: PprStyle -> [Id] -> Pretty
418 pp_binders sty bs = ppInterleave ppComma (map (pp_binder sty) bs)
419
420 pp_binder :: PprStyle -> Id -> Pretty
421 pp_binder sty b = ppCat [ppr sty b, ppStr "::", ppr sty (idType b)]
422 \end{code}
423
424 \begin{code}
425 initL :: LintM a -> Bool -> Maybe ErrMsg
426 initL m spec_done
427   = case (m spec_done [] emptyUniqSet emptyBag) of { (_, errs) ->
428     if isEmptyBag errs then
429         Nothing
430     else
431         Just ( \ sty ->
432           ppAboves [ msg sty | msg <- bagToList errs ]
433         )
434     }
435
436 returnL :: a -> LintM a
437 returnL r spec loc scope errs = (r, errs)
438
439 thenL :: LintM a -> (a -> LintM b) -> LintM b
440 thenL m k spec loc scope errs
441   = case m spec loc scope errs of
442       (r, errs') -> k r spec loc scope errs'
443
444 seqL :: LintM a -> LintM b -> LintM b
445 seqL m k spec loc scope errs
446   = case m spec loc scope errs of
447       (_, errs') -> k spec loc scope errs'
448
449 thenMaybeL :: LintM (Maybe a) -> (a -> LintM (Maybe b)) -> LintM (Maybe b)
450 thenMaybeL m k spec loc scope errs
451   = case m spec loc scope errs of
452       (Nothing, errs2) -> (Nothing, errs2)
453       (Just r,  errs2) -> k r spec loc scope errs2
454
455 seqMaybeL :: LintM (Maybe a) -> LintM (Maybe b) -> LintM (Maybe b)
456 seqMaybeL m k spec loc scope errs
457   = case m spec loc scope errs of
458       (Nothing, errs2) -> (Nothing, errs2)
459       (Just _,  errs2) -> k spec loc scope errs2
460
461 mapL :: (a -> LintM b) -> [a] -> LintM [b]
462 mapL f [] = returnL []
463 mapL f (x:xs)
464   = f x         `thenL` \ r ->
465     mapL f xs   `thenL` \ rs ->
466     returnL (r:rs)
467
468 mapMaybeL :: (a -> LintM (Maybe b)) -> [a] -> LintM (Maybe [b])
469         -- Returns Nothing if anything fails
470 mapMaybeL f [] = returnL (Just [])
471 mapMaybeL f (x:xs)
472   = f x             `thenMaybeL` \ r ->
473     mapMaybeL f xs  `thenMaybeL` \ rs ->
474     returnL (Just (r:rs))
475 \end{code}
476
477 \begin{code}
478 checkL :: Bool -> ErrMsg -> LintM ()
479 checkL True  msg spec loc scope errs = ((), errs)
480 checkL False msg spec loc scope errs = ((), addErr errs msg loc)
481
482 checkIfSpecDoneL :: Bool -> ErrMsg -> LintM ()
483 checkIfSpecDoneL True  msg spec  loc scope errs = ((), errs)
484 checkIfSpecDoneL False msg True  loc scope errs = ((), addErr errs msg loc)
485 checkIfSpecDoneL False msg False loc scope errs = ((), errs)
486
487 addErrIfL pred spec
488   = if pred then addErrL spec else returnL ()
489
490 addErrL :: ErrMsg -> LintM ()
491 addErrL msg spec loc scope errs = ((), addErr errs msg loc)
492
493 addErr :: Bag ErrMsg -> ErrMsg -> [LintLocInfo] -> Bag ErrMsg
494
495 addErr errs_so_far msg locs
496   = ASSERT (not (null locs))
497     errs_so_far `snocBag` ( \ sty ->
498     ppHang (ppr sty (head locs)) 4 (msg sty)
499     )
500
501 addLoc :: LintLocInfo -> LintM a -> LintM a
502 addLoc extra_loc m spec loc scope errs
503   = m spec (extra_loc:loc) scope errs
504
505 addInScopeVars :: [Id] -> LintM a -> LintM a
506 addInScopeVars ids m spec loc scope errs
507   = -- We check if these "new" ids are already
508     -- in scope, i.e., we have *shadowing* going on.
509     -- For now, it's just a "trace"; we may make
510     -- a real error out of it...
511     let
512         new_set = mkUniqSet ids
513
514         shadowed = scope `intersectUniqSets` new_set
515     in
516 --  After adding -fliberate-case, Simon decided he likes shadowed
517 --  names after all.  WDP 94/07
518 --  (if isEmptyUniqSet shadowed
519 --  then id
520 --  else pprTrace "Shadowed vars:" (ppr PprDebug (uniqSetToList shadowed))) (
521     m spec loc (scope `unionUniqSets` new_set) errs
522 --  )
523 \end{code}
524
525 \begin{code}
526 checkInScope :: Id -> LintM ()
527 checkInScope id spec loc scope errs
528   = if isLocallyDefined id && not (id `elementOfUniqSet` scope) then
529       ((),addErr errs (\sty -> ppCat [ppr sty id,ppStr "is out of scope"]) loc)
530     else
531       ((),errs)
532
533 checkTys :: Type -> Type -> ErrMsg -> LintM ()
534 checkTys ty1 ty2 msg spec loc scope errs
535   = if ty1 `eqTy` ty2 then ((), errs) else ((), addErr errs msg loc)
536 \end{code}
537
538 \begin{code}
539 mkCaseAltMsg :: CoreCaseAlts -> ErrMsg
540 mkCaseAltMsg alts sty
541   = ppAbove (ppStr "Type of case alternatives not the same:")
542             (ppr sty alts)
543
544 mkCaseDataConMsg :: CoreExpr -> ErrMsg
545 mkCaseDataConMsg expr sty
546   = ppAbove (ppStr "A case scrutinee not of data constructor type:")
547             (pp_expr sty expr)
548
549 mkCaseNotPrimMsg :: TyCon -> ErrMsg
550 mkCaseNotPrimMsg tycon sty
551   = ppAbove (ppStr "A primitive case on a non-primitive type:")
552             (ppr sty tycon)
553
554 mkCasePrimMsg :: TyCon -> ErrMsg
555 mkCasePrimMsg tycon sty
556   = ppAbove (ppStr "An algebraic case on a primitive type:")
557             (ppr sty tycon)
558
559 mkCaseAbstractMsg :: TyCon -> ErrMsg
560 mkCaseAbstractMsg tycon sty
561   = ppAbove (ppStr "An algebraic case on some weird type:")
562             (ppr sty tycon)
563
564 mkDefltMsg :: CoreCaseDefault -> ErrMsg
565 mkDefltMsg deflt sty
566   = ppAbove (ppStr "Binder in case default doesn't match type of scrutinee:")
567             (ppr sty deflt)
568
569 mkAppMsg :: Type -> Type -> CoreExpr -> ErrMsg
570 mkAppMsg fun arg expr sty
571   = ppAboves [ppStr "Argument values doesn't match argument type:",
572               ppHang (ppStr "Fun type:") 4 (ppr sty fun),
573               ppHang (ppStr "Arg type:") 4 (ppr sty arg),
574               ppHang (ppStr "Expression:") 4 (pp_expr sty expr)]
575
576 mkTyAppMsg :: FAST_STRING -> Type -> Type -> CoreExpr -> ErrMsg
577 mkTyAppMsg msg ty arg expr sty
578   = ppAboves [ppCat [ppPStr msg, ppStr "type application:"],
579               ppHang (ppStr "Exp type:")   4 (ppr sty ty),
580               ppHang (ppStr "Arg type:")   4 (ppr sty arg),
581               ppHang (ppStr "Expression:") 4 (pp_expr sty expr)]
582
583 mkUsageAppMsg :: Type -> Usage -> CoreExpr -> ErrMsg
584 mkUsageAppMsg ty u expr sty
585   = ppAboves [ppStr "Illegal usage application:",
586               ppHang (ppStr "Exp type:") 4 (ppr sty ty),
587               ppHang (ppStr "Usage exp:") 4 (ppr sty u),
588               ppHang (ppStr "Expression:") 4 (pp_expr sty expr)]
589
590 mkAlgAltMsg1 :: Type -> ErrMsg
591 mkAlgAltMsg1 ty sty
592   = ppAbove (ppStr "In some case statement, type of scrutinee is not a data type:")
593             (ppr sty ty)
594
595 mkAlgAltMsg2 :: Type -> Id -> ErrMsg
596 mkAlgAltMsg2 ty con sty
597   = ppAboves [
598         ppStr "In some algebraic case alternative, constructor is not a constructor of scrutinee type:",
599         ppr sty ty,
600         ppr sty con
601     ]
602
603 mkAlgAltMsg3 :: Id -> [Id] -> ErrMsg
604 mkAlgAltMsg3 con alts sty
605   = ppAboves [
606         ppStr "In some algebraic case alternative, number of arguments doesn't match constructor:",
607         ppr sty con,
608         ppr sty alts
609     ]
610
611 mkAlgAltMsg4 :: Type -> Id -> ErrMsg
612 mkAlgAltMsg4 ty arg sty
613   = ppAboves [
614         ppStr "In some algebraic case alternative, type of argument doesn't match data constructor:",
615         ppr sty ty,
616         ppr sty arg
617     ]
618
619 mkPrimAltMsg :: (Literal, CoreExpr) -> ErrMsg
620 mkPrimAltMsg alt sty
621   = ppAbove
622     (ppStr "In a primitive case alternative, type of literal doesn't match type of scrutinee:")
623             (ppr sty alt)
624
625 mkRhsMsg :: Id -> Type -> ErrMsg
626 mkRhsMsg binder ty sty
627   = ppAboves
628     [ppCat [ppStr "The type of this binder doesn't match the type of its RHS:",
629             ppr sty binder],
630      ppCat [ppStr "Binder's type:", ppr sty (idType binder)],
631      ppCat [ppStr "Rhs type:", ppr sty ty]]
632
633 mkRhsPrimMsg :: Id -> CoreExpr -> ErrMsg
634 mkRhsPrimMsg binder rhs sty
635   = ppAboves [ppCat [ppStr "The type of this binder is primitive:",
636                      ppr sty binder],
637               ppCat [ppStr "Binder's type:", ppr sty (idType binder)]
638              ]
639
640 mkSpecTyAppMsg :: CoreArg -> ErrMsg
641 mkSpecTyAppMsg arg sty
642   = ppAbove
643       (ppStr "Unboxed types in a type application (after specialisation):")
644       (ppr sty arg)
645
646 pp_expr :: PprStyle -> CoreExpr -> Pretty
647 pp_expr sty expr
648   = pprCoreExpr sty (pprBigCoreBinder sty) (pprTypedCoreBinder sty) (pprTypedCoreBinder sty) expr
649 \end{code}