[project @ 2005-02-28 16:02:57 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcExpr.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[TcExpr]{Typecheck an expression}
5
6 \begin{code}
7 module TcExpr ( tcCheckSigma, tcCheckRho, tcInferRho, tcMonoExpr ) where
8
9 #include "HsVersions.h"
10
11 #ifdef GHCI     /* Only if bootstrapped */
12 import {-# SOURCE #-}   TcSplice( tcSpliceExpr, tcBracket )
13 import Id               ( Id )
14 import Name             ( isExternalName )
15 import TcType           ( isTauTy )
16 import TcEnv            ( checkWellStaged )
17 import HsSyn            ( nlHsApp )
18 import qualified DsMeta
19 #endif
20
21 import HsSyn            ( HsExpr(..), LHsExpr, HsLit(..), ArithSeqInfo(..), recBindFields,
22                           HsMatchContext(..), HsRecordBinds, mkHsApp, nlHsVar )
23 import TcHsSyn          ( hsLitType, (<$>) )
24 import TcRnMonad
25 import TcUnify          ( Expected(..), tcInfer, zapExpectedType, zapExpectedTo, tcSubExp, tcGen,
26                           unifyFunTys, zapToListTy, zapToTyConApp )
27 import BasicTypes       ( isMarkedStrict )
28 import Inst             ( newOverloadedLit, newMethodFromName, newIPDict,
29                           newDicts, newMethodWithGivenTy, tcInstStupidTheta, tcInstCall )
30 import TcBinds          ( tcBindsAndThen )
31 import TcEnv            ( tcLookup, tcLookupId, checkProcLevel,
32                           tcLookupDataCon, tcLookupGlobalId
33                         )
34 import TcArrows         ( tcProc )
35 import TcMatches        ( tcMatchesCase, tcMatchLambda, tcDoStmts, tcThingWithSig, TcMatchCtxt(..) )
36 import TcHsType         ( tcHsSigType, UserTypeCtxt(..) )
37 import TcPat            ( badFieldCon, refineTyVars )
38 import TcMType          ( tcInstTyVars, tcInstType, newTyFlexiVarTy, zonkTcType )
39 import TcType           ( Type, TcTyVar, TcType, TcSigmaType, TcRhoType, 
40                           tcSplitFunTys, tcSplitTyConApp, mkTyVarTys,
41                           isSigmaTy, mkFunTy, mkTyConApp, tyVarsOfTypes, isLinearPred,
42                           tcSplitSigmaTy, tidyOpenType
43                         )
44 import Kind             ( openTypeKind, liftedTypeKind, argTypeKind )
45
46 import Id               ( idType, recordSelectorFieldLabel, isRecordSelector )
47 import DataCon          ( DataCon, dataConFieldLabels, dataConStrictMarks, dataConWrapId )
48 import Name             ( Name )
49 import TyCon            ( TyCon, FieldLabel, tyConTyVars, tyConStupidTheta, 
50                           tyConDataCons, tyConFields )
51 import Type             ( zipTopTvSubst, substTheta, substTy )
52 import Var              ( tyVarKind )
53 import VarSet           ( emptyVarSet, elemVarSet )
54 import TysWiredIn       ( boolTy, parrTyCon, tupleTyCon )
55 import PrelNames        ( enumFromName, enumFromThenName, 
56                           enumFromToName, enumFromThenToName,
57                           enumFromToPName, enumFromThenToPName
58                         )
59 import ListSetOps       ( minusList )
60 import CmdLineOpts
61 import HscTypes         ( TyThing(..) )
62 import SrcLoc           ( Located(..), unLoc, getLoc )
63 import Util
64 import Outputable
65 import FastString
66
67 #ifdef DEBUG
68 import TyCon            ( isAlgTyCon )
69 #endif
70 \end{code}
71
72 %************************************************************************
73 %*                                                                      *
74 \subsection{Main wrappers}
75 %*                                                                      *
76 %************************************************************************
77
78 \begin{code}
79 -- tcCheckSigma does type *checking*; it's passed the expected type of the result
80 tcCheckSigma :: LHsExpr Name            -- Expession to type check
81              -> TcSigmaType             -- Expected type (could be a polytpye)
82              -> TcM (LHsExpr TcId)      -- Generalised expr with expected type
83
84 tcCheckSigma expr expected_ty 
85   = traceTc (text "tcExpr" <+> (ppr expected_ty $$ ppr expr)) `thenM_`
86     tc_expr' expr expected_ty
87
88 tc_expr' expr sigma_ty
89   | isSigmaTy sigma_ty
90   = tcGen sigma_ty emptyVarSet (
91         \ rho_ty -> tcCheckRho expr rho_ty
92     )                           `thenM` \ (gen_fn, expr') ->
93     returnM (L (getLoc expr') (gen_fn <$> unLoc expr'))
94
95 tc_expr' expr rho_ty    -- Monomorphic case
96   = tcCheckRho expr rho_ty
97 \end{code}
98
99 Typecheck expression which in most cases will be an Id.
100 The expression can return a higher-ranked type, such as
101         (forall a. a->a) -> Int
102 so we must create a hole to pass in as the expected tyvar.
103
104 \begin{code}
105 tcCheckRho :: LHsExpr Name -> TcRhoType -> TcM (LHsExpr TcId)
106 tcCheckRho expr rho_ty = tcMonoExpr expr (Check rho_ty)
107
108 tcInferRho :: LHsExpr Name -> TcM (LHsExpr TcId, TcRhoType)
109 tcInferRho (L loc (HsVar name)) = setSrcSpan loc $ do 
110                                   { (e,_,ty) <- tcId name; return (L loc e, ty)}
111 tcInferRho expr                 = tcInfer (tcMonoExpr expr)
112 \end{code}
113
114
115
116 %************************************************************************
117 %*                                                                      *
118 \subsection{The TAUT rules for variables}TcExpr
119 %*                                                                      *
120 %************************************************************************
121
122 \begin{code}
123 tcMonoExpr :: LHsExpr Name              -- Expession to type check
124            -> Expected TcRhoType        -- Expected type (could be a type variable)
125                                         -- Definitely no foralls at the top
126                                         -- Can be a 'hole'.
127            -> TcM (LHsExpr TcId)
128
129 tcMonoExpr (L loc expr) res_ty
130   = setSrcSpan loc (do { expr' <- tc_expr expr res_ty
131                        ; return (L loc expr') })
132
133 tc_expr :: HsExpr Name -> Expected TcRhoType -> TcM (HsExpr TcId)
134 tc_expr (HsVar name) res_ty
135   = do  { (expr', _, id_ty) <- tcId name
136         ; co_fn <- tcSubExp res_ty id_ty
137         ; returnM (co_fn <$> expr') }
138
139 tc_expr (HsIPVar ip) res_ty
140   =     -- Implicit parameters must have a *tau-type* not a 
141         -- type scheme.  We enforce this by creating a fresh
142         -- type variable as its type.  (Because res_ty may not
143         -- be a tau-type.)
144     newTyFlexiVarTy argTypeKind         `thenM` \ ip_ty ->
145         -- argTypeKind: it can't be an unboxed tuple
146     newIPDict (IPOccOrigin ip) ip ip_ty `thenM` \ (ip', inst) ->
147     extendLIE inst                      `thenM_`
148     tcSubExp res_ty ip_ty               `thenM` \ co_fn ->
149     returnM (co_fn <$> HsIPVar ip')
150 \end{code}
151
152
153 %************************************************************************
154 %*                                                                      *
155 \subsection{Expressions type signatures}
156 %*                                                                      *
157 %************************************************************************
158
159 \begin{code}
160 tc_expr in_expr@(ExprWithTySig expr poly_ty) res_ty
161  = addErrCtxt (exprCtxt in_expr)                        $
162    tcHsSigType ExprSigCtxt poly_ty                      `thenM` \ sig_tc_ty ->
163    tcThingWithSig sig_tc_ty (tcCheckRho expr) res_ty    `thenM` \ (co_fn, expr') ->
164    returnM (co_fn <$> ExprWithTySigOut expr' poly_ty)
165
166 tc_expr (HsType ty) res_ty
167   = failWithTc (text "Can't handle type argument:" <+> ppr ty)
168         -- This is the syntax for type applications that I was planning
169         -- but there are difficulties (e.g. what order for type args)
170         -- so it's not enabled yet.
171         -- Can't eliminate it altogether from the parser, because the
172         -- same parser parses *patterns*.
173 \end{code}
174
175
176 %************************************************************************
177 %*                                                                      *
178 \subsection{Other expression forms}
179 %*                                                                      *
180 %************************************************************************
181
182 \begin{code}
183 tc_expr (HsPar expr)    res_ty  = tcMonoExpr expr res_ty        `thenM` \ expr' -> 
184                                   returnM (HsPar expr')
185 tc_expr (HsSCC lbl expr) res_ty = tcMonoExpr expr res_ty        `thenM` \ expr' ->
186                                   returnM (HsSCC lbl expr')
187 tc_expr (HsCoreAnn lbl expr) res_ty = tcMonoExpr expr res_ty `thenM` \ expr' ->  -- hdaume: core annotation
188                                          returnM (HsCoreAnn lbl expr')
189
190 tc_expr (HsLit lit) res_ty  = tcLit lit res_ty
191
192 tc_expr (HsOverLit lit) res_ty  
193   = zapExpectedType res_ty liftedTypeKind               `thenM` \ res_ty' ->
194         -- Overloaded literals must have liftedTypeKind, because
195         -- we're instantiating an overloaded function here,
196         -- whereas res_ty might be openTypeKind. This was a bug in 6.2.2
197     newOverloadedLit (LiteralOrigin lit) lit res_ty'    `thenM` \ lit_expr ->
198     returnM (unLoc lit_expr)    -- ToDo: nasty unLoc
199
200 tc_expr (NegApp expr neg_name) res_ty
201   = tc_expr (HsApp (nlHsVar neg_name) expr) res_ty
202         -- ToDo: use tcSyntaxName
203
204 tc_expr (HsLam match) res_ty
205   = tcMatchLambda match res_ty          `thenM` \ match' ->
206     returnM (HsLam match')
207
208 tc_expr (HsApp e1 e2) res_ty 
209   = tcApp e1 [e2] res_ty
210 \end{code}
211
212 Note that the operators in sections are expected to be binary, and
213 a type error will occur if they aren't.
214
215 \begin{code}
216 -- Left sections, equivalent to
217 --      \ x -> e op x,
218 -- or
219 --      \ x -> op e x,
220 -- or just
221 --      op e
222
223 tc_expr in_expr@(SectionL arg1 op) res_ty
224   = tcInferRho op                               `thenM` \ (op', op_ty) ->
225     unifyFunTys 2 op_ty {- two args -}          `thenM` \ ([arg1_ty, arg2_ty], op_res_ty) ->
226     tcArg op (arg1, arg1_ty, 1)                 `thenM` \ arg1' ->
227     addErrCtxt (exprCtxt in_expr)               $
228     tcSubExp res_ty (mkFunTy arg2_ty op_res_ty) `thenM` \ co_fn ->
229     returnM (co_fn <$> SectionL arg1' op')
230
231 -- Right sections, equivalent to \ x -> x op expr, or
232 --      \ x -> op x expr
233
234 tc_expr in_expr@(SectionR op arg2) res_ty
235   = tcInferRho op                               `thenM` \ (op', op_ty) ->
236     unifyFunTys 2 op_ty {- two args -}          `thenM` \ ([arg1_ty, arg2_ty], op_res_ty) ->
237     tcArg op (arg2, arg2_ty, 2)                 `thenM` \ arg2' ->
238     addErrCtxt (exprCtxt in_expr)               $
239     tcSubExp res_ty (mkFunTy arg1_ty op_res_ty) `thenM` \ co_fn ->
240     returnM (co_fn <$> SectionR op' arg2')
241
242 -- equivalent to (op e1) e2:
243
244 tc_expr in_expr@(OpApp arg1 op fix arg2) res_ty
245   = tcInferRho op                               `thenM` \ (op', op_ty) ->
246     unifyFunTys 2 op_ty {- two args -}          `thenM` \ ([arg1_ty, arg2_ty], op_res_ty) ->
247     tcArg op (arg1, arg1_ty, 1)                 `thenM` \ arg1' ->
248     tcArg op (arg2, arg2_ty, 2)                 `thenM` \ arg2' ->
249     addErrCtxt (exprCtxt in_expr)               $
250     tcSubExp res_ty op_res_ty                   `thenM` \ co_fn ->
251     returnM (OpApp arg1' op' fix arg2')
252 \end{code}
253
254 \begin{code}
255 tc_expr (HsLet binds (L loc expr)) res_ty
256   = tcBindsAndThen
257         glue
258         binds                   -- Bindings to check
259         (setSrcSpan loc $ tc_expr expr res_ty)
260   where
261     glue bind expr = HsLet [bind] (L loc expr)
262
263 tc_expr in_expr@(HsCase scrut matches) exp_ty
264   =     -- We used to typecheck the case alternatives first.
265         -- The case patterns tend to give good type info to use
266         -- when typechecking the scrutinee.  For example
267         --      case (map f) of
268         --        (x:xs) -> ...
269         -- will report that map is applied to too few arguments
270         --
271         -- But now, in the GADT world, we need to typecheck the scrutinee
272         -- first, to get type info that may be refined in the case alternatives
273     addErrCtxt (caseScrutCtxt scrut)
274                (tcInferRho scrut)       `thenM`    \ (scrut', scrut_ty) ->
275
276     addErrCtxt (caseCtxt in_expr)                       $
277     tcMatchesCase match_ctxt scrut_ty matches exp_ty    `thenM` \ matches' ->
278     returnM (HsCase scrut' matches') 
279  where
280     match_ctxt = MC { mc_what = CaseAlt,
281                       mc_body = tcMonoExpr }
282
283 tc_expr (HsIf pred b1 b2) res_ty
284   = addErrCtxt (predCtxt pred) (
285     tcCheckRho pred boolTy      )       `thenM`    \ pred' ->
286
287     zapExpectedType res_ty openTypeKind `thenM`    \ res_ty' ->
288         -- C.f. the call to zapToType in TcMatches.tcMatches
289
290     tcCheckRho b1 res_ty'               `thenM`    \ b1' ->
291     tcCheckRho b2 res_ty'               `thenM`    \ b2' ->
292     returnM (HsIf pred' b1' b2')
293
294 tc_expr (HsDo do_or_lc stmts method_names _) res_ty
295   = zapExpectedType res_ty liftedTypeKind               `thenM` \ res_ty' ->
296         -- All comprehensions yield a monotype of kind *
297     tcDoStmts do_or_lc stmts method_names res_ty'       `thenM` \ (stmts', methods') ->
298     returnM (HsDo do_or_lc stmts' methods' res_ty')
299
300 tc_expr in_expr@(ExplicitList _ exprs) res_ty   -- Non-empty list
301   = zapToListTy res_ty                `thenM` \ elt_ty ->  
302     mappM (tc_elt elt_ty) exprs       `thenM` \ exprs' ->
303     returnM (ExplicitList elt_ty exprs')
304   where
305     tc_elt elt_ty expr
306       = addErrCtxt (listCtxt expr) $
307         tcCheckRho expr elt_ty
308
309 tc_expr in_expr@(ExplicitPArr _ exprs) res_ty   -- maybe empty
310   = do  { [elt_ty] <- zapToTyConApp parrTyCon res_ty
311         ; exprs' <- mappM (tc_elt elt_ty) exprs 
312         ; return (ExplicitPArr elt_ty exprs') }
313   where
314     tc_elt elt_ty expr
315       = addErrCtxt (parrCtxt expr) (tcCheckRho expr elt_ty)
316
317 tc_expr (ExplicitTuple exprs boxity) res_ty
318   = do  { arg_tys <- zapToTyConApp (tupleTyCon boxity (length exprs)) res_ty
319         ; exprs' <-  tcCheckRhos exprs arg_tys
320         ; return (ExplicitTuple exprs' boxity) }
321
322 tc_expr (HsProc pat cmd) res_ty
323   = tcProc pat cmd res_ty                       `thenM` \ (pat', cmd') ->
324     returnM (HsProc pat' cmd')
325
326 tc_expr e@(HsArrApp _ _ _ _ _) _
327   = failWithTc (vcat [ptext SLIT("The arrow command"), nest 2 (ppr e), 
328                       ptext SLIT("was found where an expression was expected")])
329
330 tc_expr e@(HsArrForm _ _ _) _
331   = failWithTc (vcat [ptext SLIT("The arrow command"), nest 2 (ppr e), 
332                       ptext SLIT("was found where an expression was expected")])
333 \end{code}
334
335 %************************************************************************
336 %*                                                                      *
337                 Record construction and update
338 %*                                                                      *
339 %************************************************************************
340
341 \begin{code}
342 tc_expr expr@(RecordCon con@(L loc con_name) rbinds) res_ty
343   = addErrCtxt (recordConCtxt expr)             $
344     addLocM tcId con                    `thenM` \ (con_expr, _, con_tau) ->
345     let
346         (_, record_ty)   = tcSplitFunTys con_tau
347         (tycon, ty_args) = tcSplitTyConApp record_ty
348     in
349     ASSERT( isAlgTyCon tycon )
350     zapExpectedTo res_ty record_ty      `thenM_`
351
352         -- Check that the record bindings match the constructor
353         -- con_name is syntactically constrained to be a data constructor
354     tcLookupDataCon con_name            `thenM` \ data_con ->
355     let
356         bad_fields = badFields rbinds data_con
357     in
358     if notNull bad_fields then
359         mappM (addErrTc . badFieldCon data_con) bad_fields      `thenM_`
360         failM   -- Fail now, because tcRecordBinds will crash on a bad field
361     else
362
363         -- Typecheck the record bindings
364     tcRecordBinds tycon ty_args rbinds          `thenM` \ rbinds' ->
365     
366         -- Check for missing fields
367     checkMissingFields data_con rbinds          `thenM_` 
368
369     returnM (RecordConOut data_con (L loc con_expr) rbinds')
370
371 -- The main complication with RecordUpd is that we need to explicitly
372 -- handle the *non-updated* fields.  Consider:
373 --
374 --      data T a b = MkT1 { fa :: a, fb :: b }
375 --                 | MkT2 { fa :: a, fc :: Int -> Int }
376 --                 | MkT3 { fd :: a }
377 --      
378 --      upd :: T a b -> c -> T a c
379 --      upd t x = t { fb = x}
380 --
381 -- The type signature on upd is correct (i.e. the result should not be (T a b))
382 -- because upd should be equivalent to:
383 --
384 --      upd t x = case t of 
385 --                      MkT1 p q -> MkT1 p x
386 --                      MkT2 a b -> MkT2 p b
387 --                      MkT3 d   -> error ...
388 --
389 -- So we need to give a completely fresh type to the result record,
390 -- and then constrain it by the fields that are *not* updated ("p" above).
391 --
392 -- Note that because MkT3 doesn't contain all the fields being updated,
393 -- its RHS is simply an error, so it doesn't impose any type constraints
394 --
395 -- All this is done in STEP 4 below.
396
397 tc_expr expr@(RecordUpd record_expr rbinds) res_ty
398   = addErrCtxt (recordUpdCtxt   expr)           $
399
400         -- STEP 0
401         -- Check that the field names are really field names
402     ASSERT( notNull rbinds )
403     let 
404         field_names = map fst rbinds
405     in
406     mappM (tcLookupGlobalId.unLoc) field_names  `thenM` \ sel_ids ->
407         -- The renamer has already checked that they
408         -- are all in scope
409     let
410         bad_guys = [ setSrcSpan loc $ addErrTc (notSelector field_name) 
411                    | (L loc field_name, sel_id) <- field_names `zip` sel_ids,
412                      not (isRecordSelector sel_id)      -- Excludes class ops
413                    ]
414     in
415     checkM (null bad_guys) (sequenceM bad_guys `thenM_` failM)  `thenM_`
416     
417         -- STEP 1
418         -- Figure out the tycon and data cons from the first field name
419     let
420                 -- It's OK to use the non-tc splitters here (for a selector)
421         sel_id : _   = sel_ids
422         (tycon, _)   = recordSelectorFieldLabel sel_id  -- We've failed already if
423         data_cons    = tyConDataCons tycon              -- it's not a field label
424         tycon_tyvars = tyConTyVars tycon                -- The data cons use the same type vars
425     in
426     tcInstTyVars tycon_tyvars           `thenM` \ (_, result_inst_tys, inst_env) ->
427
428         -- STEP 2
429         -- Check that at least one constructor has all the named fields
430         -- i.e. has an empty set of bad fields returned by badFields
431     checkTc (any (null . badFields rbinds) data_cons)
432             (badFieldsUpd rbinds)       `thenM_`
433
434         -- STEP 3
435         -- Typecheck the update bindings.
436         -- (Do this after checking for bad fields in case there's a field that
437         --  doesn't match the constructor.)
438     let
439         result_record_ty = mkTyConApp tycon result_inst_tys
440     in
441     zapExpectedTo res_ty result_record_ty       `thenM_`
442     tcRecordBinds tycon result_inst_tys rbinds  `thenM` \ rbinds' ->
443
444         -- STEP 4
445         -- Use the un-updated fields to find a vector of booleans saying
446         -- which type arguments must be the same in updatee and result.
447         --
448         -- WARNING: this code assumes that all data_cons in a common tycon
449         -- have FieldLabels abstracted over the same tyvars.
450     let
451         upd_field_lbls      = recBindFields rbinds
452         con_field_lbls_s    = map dataConFieldLabels data_cons
453
454                 -- A constructor is only relevant to this process if
455                 -- it contains all the fields that are being updated
456         relevant_field_lbls_s      = filter is_relevant con_field_lbls_s
457         is_relevant con_field_lbls = all (`elem` con_field_lbls) upd_field_lbls
458
459         non_upd_field_lbls  = concat relevant_field_lbls_s `minusList` upd_field_lbls
460         common_tyvars       = tyVarsOfTypes [ty | (fld,ty,_) <- tyConFields tycon,
461                                                   fld `elem` non_upd_field_lbls]
462         is_common_tv tv = tv `elemVarSet` common_tyvars
463
464         mk_inst_ty tv result_inst_ty 
465           | is_common_tv tv = returnM result_inst_ty            -- Same as result type
466           | otherwise       = newTyFlexiVarTy (tyVarKind tv)    -- Fresh type, of correct kind
467     in
468     zipWithM mk_inst_ty tycon_tyvars result_inst_tys    `thenM` \ inst_tys ->
469
470         -- STEP 5
471         -- Typecheck the expression to be updated
472     let
473         record_ty = mkTyConApp tycon inst_tys
474     in
475     tcCheckRho record_expr record_ty            `thenM` \ record_expr' ->
476
477         -- STEP 6
478         -- Figure out the LIE we need.  We have to generate some 
479         -- dictionaries for the data type context, since we are going to
480         -- do pattern matching over the data cons.
481         --
482         -- What dictionaries do we need?  
483         -- We just take the context of the type constructor
484     let
485         theta' = substTheta inst_env (tyConStupidTheta tycon)
486     in
487     newDicts RecordUpdOrigin theta'     `thenM` \ dicts ->
488     extendLIEs dicts                    `thenM_`
489
490         -- Phew!
491     returnM (RecordUpdOut record_expr' record_ty result_record_ty rbinds') 
492 \end{code}
493
494
495 %************************************************************************
496 %*                                                                      *
497         Arithmetic sequences                    e.g. [a,b..]
498         and their parallel-array counterparts   e.g. [: a,b.. :]
499                 
500 %*                                                                      *
501 %************************************************************************
502
503 \begin{code}
504 tc_expr (ArithSeqIn seq@(From expr)) res_ty
505   = zapToListTy res_ty                          `thenM` \ elt_ty ->  
506     tcCheckRho expr elt_ty                      `thenM` \ expr' ->
507
508     newMethodFromName (ArithSeqOrigin seq) 
509                       elt_ty enumFromName       `thenM` \ enum_from ->
510
511     returnM (ArithSeqOut (nlHsVar enum_from) (From expr'))
512
513 tc_expr in_expr@(ArithSeqIn seq@(FromThen expr1 expr2)) res_ty
514   = addErrCtxt (arithSeqCtxt in_expr) $ 
515     zapToListTy  res_ty                                 `thenM`    \ elt_ty ->  
516     tcCheckRho expr1 elt_ty                             `thenM`    \ expr1' ->
517     tcCheckRho expr2 elt_ty                             `thenM`    \ expr2' ->
518     newMethodFromName (ArithSeqOrigin seq) 
519                       elt_ty enumFromThenName           `thenM` \ enum_from_then ->
520
521     returnM (ArithSeqOut (nlHsVar enum_from_then) (FromThen expr1' expr2'))
522
523
524 tc_expr in_expr@(ArithSeqIn seq@(FromTo expr1 expr2)) res_ty
525   = addErrCtxt (arithSeqCtxt in_expr) $
526     zapToListTy  res_ty                                 `thenM`    \ elt_ty ->  
527     tcCheckRho expr1 elt_ty                             `thenM`    \ expr1' ->
528     tcCheckRho expr2 elt_ty                             `thenM`    \ expr2' ->
529     newMethodFromName (ArithSeqOrigin seq) 
530                       elt_ty enumFromToName             `thenM` \ enum_from_to ->
531
532     returnM (ArithSeqOut (nlHsVar enum_from_to) (FromTo expr1' expr2'))
533
534 tc_expr in_expr@(ArithSeqIn seq@(FromThenTo expr1 expr2 expr3)) res_ty
535   = addErrCtxt  (arithSeqCtxt in_expr) $
536     zapToListTy  res_ty                                 `thenM`    \ elt_ty ->  
537     tcCheckRho expr1 elt_ty                             `thenM`    \ expr1' ->
538     tcCheckRho expr2 elt_ty                             `thenM`    \ expr2' ->
539     tcCheckRho expr3 elt_ty                             `thenM`    \ expr3' ->
540     newMethodFromName (ArithSeqOrigin seq) 
541                       elt_ty enumFromThenToName         `thenM` \ eft ->
542
543     returnM (ArithSeqOut (nlHsVar eft) (FromThenTo expr1' expr2' expr3'))
544
545 tc_expr in_expr@(PArrSeqIn seq@(FromTo expr1 expr2)) res_ty
546   = addErrCtxt (parrSeqCtxt in_expr) $
547     zapToTyConApp parrTyCon res_ty                      `thenM`    \ [elt_ty] ->  
548     tcCheckRho expr1 elt_ty                             `thenM`    \ expr1' ->
549     tcCheckRho expr2 elt_ty                             `thenM`    \ expr2' ->
550     newMethodFromName (PArrSeqOrigin seq) 
551                       elt_ty enumFromToPName            `thenM` \ enum_from_to ->
552
553     returnM (PArrSeqOut (nlHsVar enum_from_to) (FromTo expr1' expr2'))
554
555 tc_expr in_expr@(PArrSeqIn seq@(FromThenTo expr1 expr2 expr3)) res_ty
556   = addErrCtxt  (parrSeqCtxt in_expr) $
557     zapToTyConApp parrTyCon res_ty                      `thenM`    \ [elt_ty] ->  
558     tcCheckRho expr1 elt_ty                             `thenM`    \ expr1' ->
559     tcCheckRho expr2 elt_ty                             `thenM`    \ expr2' ->
560     tcCheckRho expr3 elt_ty                             `thenM`    \ expr3' ->
561     newMethodFromName (PArrSeqOrigin seq)
562                       elt_ty enumFromThenToPName        `thenM` \ eft ->
563
564     returnM (PArrSeqOut (nlHsVar eft) (FromThenTo expr1' expr2' expr3'))
565
566 tc_expr (PArrSeqIn _) _ 
567   = panic "TcExpr.tcMonoExpr: Infinite parallel array!"
568     -- the parser shouldn't have generated it and the renamer shouldn't have
569     -- let it through
570 \end{code}
571
572
573 %************************************************************************
574 %*                                                                      *
575                 Template Haskell
576 %*                                                                      *
577 %************************************************************************
578
579 \begin{code}
580 #ifdef GHCI     /* Only if bootstrapped */
581         -- Rename excludes these cases otherwise
582 tc_expr (HsSpliceE splice) res_ty = tcSpliceExpr splice res_ty
583 tc_expr (HsBracket brack)  res_ty = do  { e <- tcBracket brack res_ty
584                                         ; return (unLoc e) }
585 #endif /* GHCI */
586 \end{code}
587
588
589 %************************************************************************
590 %*                                                                      *
591                 Catch-all
592 %*                                                                      *
593 %************************************************************************
594
595 \begin{code}
596 tc_expr other _ = pprPanic "tcMonoExpr" (ppr other)
597 \end{code}
598
599
600 %************************************************************************
601 %*                                                                      *
602 \subsection{@tcApp@ typchecks an application}
603 %*                                                                      *
604 %************************************************************************
605
606 \begin{code}
607
608 tcApp :: LHsExpr Name -> [LHsExpr Name]         -- Function and args
609       -> Expected TcRhoType                     -- Expected result type of application
610       -> TcM (HsExpr TcId)                      -- Translated fun and args
611
612 tcApp (L _ (HsApp e1 e2)) args res_ty 
613   = tcApp e1 (e2:args) res_ty           -- Accumulate the arguments
614
615 tcApp fun args res_ty
616   = do  { (fun', fun_tvs, fun_tau) <- tcFun fun         -- Type-check the function
617
618         -- Extract its argument types
619         ; (expected_arg_tys, actual_res_ty)
620               <- addErrCtxt (wrongArgsCtxt "too many" fun args) $ do
621                  { traceTc (text "tcApp" <+> (ppr fun $$ ppr fun_tau))
622                  ; unifyFunTys (length args) fun_tau }
623
624
625         ; case res_ty of
626             Check _ -> do       -- Connect to result type first
627                                 -- See Note [Push result type in]
628                 { co_fn    <- tcResult fun args res_ty actual_res_ty
629                 ; the_app' <- tcArgs fun fun' args expected_arg_tys
630                 ; traceTc (text "tcApp: check" <+> vcat [ppr fun <+> ppr args,
631                                                          ppr the_app', ppr actual_res_ty])
632                 ; returnM (co_fn <$> the_app') }
633
634             Infer _ -> do       -- Type check args first, then
635                                 -- refine result type, then do tcResult
636                 { the_app'       <- tcArgs fun fun' args expected_arg_tys
637                 ; subst          <- refineTyVars fun_tvs
638                 ; let actual_res_ty' = substTy subst actual_res_ty
639                 ; co_fn          <- tcResult fun args res_ty actual_res_ty'
640                 ; traceTc (text "tcApp: infer" <+> vcat [ppr fun <+> ppr args, ppr the_app',
641                                                          ppr actual_res_ty, ppr actual_res_ty'])
642                 ; returnM (co_fn <$> the_app') }
643         }
644
645 --      Note [Push result type in]
646 --
647 -- Unify with expected result before (was: after) type-checking the args
648 -- so that the info from res_ty (was: args) percolates to args (was actual_res_ty).
649 -- This is when we might detect a too-few args situation.
650 -- (One can think of cases when the opposite order would give
651 -- a better error message.)
652 -- [March 2003: I'm experimenting with putting this first.  Here's an 
653 --              example where it actually makes a real difference
654 --    class C t a b | t a -> b
655 --    instance C Char a Bool
656 --
657 --    data P t a = forall b. (C t a b) => MkP b
658 --    data Q t   = MkQ (forall a. P t a)
659
660 --    f1, f2 :: Q Char;
661 --    f1 = MkQ (MkP True)
662 --    f2 = MkQ (MkP True :: forall a. P Char a)
663 --
664 -- With the change, f1 will type-check, because the 'Char' info from
665 -- the signature is propagated into MkQ's argument. With the check
666 -- in the other order, the extra signature in f2 is reqd.]
667
668 ----------------
669 tcFun :: LHsExpr Name -> TcM (LHsExpr TcId, [TcTyVar], TcRhoType)
670 -- Instantiate the function, returning the type variables used
671 -- If the function isn't simple, infer its type, and return no 
672 -- type variables
673 tcFun (L loc (HsVar f)) = setSrcSpan loc $ do
674                           { (fun', tvs, fun_tau) <- tcId f
675                           ; return (L loc fun', tvs, fun_tau) }
676 tcFun fun = do { (fun', fun_tau) <- tcInfer (tcMonoExpr fun)
677                ; return (fun', [], fun_tau) }
678
679 ----------------
680 tcArgs :: LHsExpr Name                          -- The function (for error messages)
681        -> LHsExpr TcId                          -- The function (to build into result)
682        -> [LHsExpr Name] -> [TcSigmaType]       -- Actual arguments and expected arg types
683        -> TcM (HsExpr TcId)                     -- Resulting application
684
685 tcArgs fun fun' args expected_arg_tys
686   = do  { args' <- mappM (tcArg fun) (zip3 args expected_arg_tys [1..])
687         ; return (unLoc (foldl mkHsApp fun' args')) }
688
689 tcArg :: LHsExpr Name                           -- The function (for error messages)
690        -> (LHsExpr Name, TcSigmaType, Int)      -- Actual argument and expected arg type
691        -> TcM (LHsExpr TcId)                    -- Resulting argument
692 tcArg fun (arg, ty, arg_no) = addErrCtxt (funAppCtxt fun arg arg_no)
693                                          (tcCheckSigma arg ty)
694
695 ----------------
696 tcResult fun args res_ty actual_res_ty
697   = addErrCtxtM (checkArgsCtxt fun args res_ty actual_res_ty)
698                 (tcSubExp res_ty actual_res_ty)
699
700 ----------------
701 -- If an error happens we try to figure out whether the
702 -- function has been given too many or too few arguments,
703 -- and say so.
704 -- The ~(Check...) is because in the Infer case the tcSubExp 
705 -- definitely won't fail, so we can be certain we're in the Check branch
706 checkArgsCtxt fun args (Infer _) actual_res_ty tidy_env
707   = return (tidy_env, ptext SLIT("Urk infer"))
708
709 checkArgsCtxt fun args (Check expected_res_ty) actual_res_ty tidy_env
710   = zonkTcType expected_res_ty    `thenM` \ exp_ty' ->
711     zonkTcType actual_res_ty      `thenM` \ act_ty' ->
712     let
713       (env1, exp_ty'') = tidyOpenType tidy_env exp_ty'
714       (env2, act_ty'') = tidyOpenType env1     act_ty'
715       (exp_args, _)    = tcSplitFunTys exp_ty''
716       (act_args, _)    = tcSplitFunTys act_ty''
717
718       len_act_args     = length act_args
719       len_exp_args     = length exp_args
720
721       message | len_exp_args < len_act_args = wrongArgsCtxt "too few" fun args
722               | len_exp_args > len_act_args = wrongArgsCtxt "too many" fun args
723               | otherwise                   = appCtxt fun args
724     in
725     returnM (env2, message)
726 \end{code}
727
728
729 %************************************************************************
730 %*                                                                      *
731 \subsection{@tcId@ typchecks an identifier occurrence}
732 %*                                                                      *
733 %************************************************************************
734
735 tcId instantiates an occurrence of an Id.
736 The instantiate_it loop runs round instantiating the Id.
737 It has to be a loop because we are now prepared to entertain
738 types like
739         f:: forall a. Eq a => forall b. Baz b => tau
740 We want to instantiate this to
741         f2::tau         {f2 = f1 b (Baz b), f1 = f a (Eq a)}
742
743 The -fno-method-sharing flag controls what happens so far as the LIE
744 is concerned.  The default case is that for an overloaded function we 
745 generate a "method" Id, and add the Method Inst to the LIE.  So you get
746 something like
747         f :: Num a => a -> a
748         f = /\a (d:Num a) -> let m = (+) a d in \ (x:a) -> m x x
749 If you specify -fno-method-sharing, the dictionary application 
750 isn't shared, so we get
751         f :: Num a => a -> a
752         f = /\a (d:Num a) (x:a) -> (+) a d x x
753 This gets a bit less sharing, but
754         a) it's better for RULEs involving overloaded functions
755         b) perhaps fewer separated lambdas
756
757 \begin{code}
758 tcId :: Name -> TcM (HsExpr TcId, [TcTyVar], TcRhoType)
759         -- Return the type variables at which the function
760         -- is instantiated, as well as the translated variable and its type
761
762 tcId id_name    -- Look up the Id and instantiate its type
763   = tcLookup id_name    `thenM` \ thing ->
764     case thing of {
765         AGlobal (ADataCon con)  -- Similar, but instantiate the stupid theta too
766           -> do { (expr, tvs, tau) <- instantiate (dataConWrapId con)
767                 ; tcInstStupidTheta con (mkTyVarTys tvs)
768                 -- Remember to chuck in the constraints from the "silly context"
769                 ; return (expr, tvs, tau) }
770
771     ;   AGlobal (AnId id) -> instantiate id
772                 -- A global cannot possibly be ill-staged
773                 -- nor does it need the 'lifting' treatment
774
775     ;   ATcId id th_level proc_level 
776           -> do { checkProcLevel id proc_level
777                 ; tc_local_id id th_level }
778
779         -- THis 
780     ;   other -> failWithTc (ppr other <+> ptext SLIT("used where a value identifer was expected"))
781     }
782   where
783
784 #ifndef GHCI
785     tc_local_id id th_bind_lvl                  -- Non-TH case
786         = instantiate id
787
788 #else /* GHCI and TH is on */
789     tc_local_id id th_bind_lvl                  -- TH case
790         =       -- Check for cross-stage lifting
791           getStage                              `thenM` \ use_stage -> 
792           case use_stage of
793               Brack use_lvl ps_var lie_var
794                 | use_lvl > th_bind_lvl 
795                 -> if isExternalName id_name then       
796                         -- Top-level identifiers in this module,
797                         -- (which have External Names)
798                         -- are just like the imported case:
799                         -- no need for the 'lifting' treatment
800                         -- E.g.  this is fine:
801                         --   f x = x
802                         --   g y = [| f 3 |]
803                         -- But we do need to put f into the keep-alive
804                         -- set, because after desugaring the code will
805                         -- only mention f's *name*, not f itself.
806                         keepAliveTc id_name     `thenM_` 
807                         instantiate id
808
809                    else -- Nested identifiers, such as 'x' in
810                         -- E.g. \x -> [| h x |]
811                         -- We must behave as if the reference to x was
812                         --      h $(lift x)     
813                         -- We use 'x' itself as the splice proxy, used by 
814                         -- the desugarer to stitch it all back together.
815                         -- If 'x' occurs many times we may get many identical
816                         -- bindings of the same splice proxy, but that doesn't
817                         -- matter, although it's a mite untidy.
818                    let
819                        id_ty = idType id
820                    in
821                    checkTc (isTauTy id_ty)      (polySpliceErr id)      `thenM_` 
822                        -- If x is polymorphic, its occurrence sites might
823                        -- have different instantiations, so we can't use plain
824                        -- 'x' as the splice proxy name.  I don't know how to 
825                        -- solve this, and it's probably unimportant, so I'm
826                        -- just going to flag an error for now
827    
828                    setLIEVar lie_var    (
829                    newMethodFromName orig id_ty DsMeta.liftName `thenM` \ lift ->
830                            -- Put the 'lift' constraint into the right LIE
831            
832                    -- Update the pending splices
833                    readMutVar ps_var                    `thenM` \ ps ->
834                    writeMutVar ps_var ((id_name, nlHsApp (nlHsVar lift) (nlHsVar id)) : ps)     `thenM_`
835            
836                    returnM (HsVar id, [], id_ty))
837
838               other -> 
839                 checkWellStaged (quotes (ppr id)) th_bind_lvl use_stage `thenM_`
840                 instantiate id
841 #endif /* GHCI */
842
843     instantiate :: TcId -> TcM (HsExpr TcId, [TcTyVar], TcRhoType)
844     instantiate fun_id = loop (HsVar fun_id) [] (idType fun_id)
845
846     loop (HsVar fun_id) tvs fun_ty
847         | want_method_inst fun_ty
848         = tcInstType fun_ty             `thenM` \ (tyvars, theta, tau) ->
849           newMethodWithGivenTy orig fun_id 
850                 (mkTyVarTys tyvars) theta tau   `thenM` \ meth_id ->
851           loop (HsVar meth_id) (tvs ++ tyvars) tau
852
853     loop fun tvs fun_ty 
854         | isSigmaTy fun_ty
855         = tcInstCall orig fun_ty        `thenM` \ (inst_fn, new_tvs, tau) ->
856           loop (inst_fn <$> fun) (tvs ++ new_tvs) tau
857
858         | otherwise
859         = returnM (fun, tvs, fun_ty)
860
861         --      Hack Alert (want_method_inst)!
862         -- If   f :: (%x :: T) => Int -> Int
863         -- Then if we have two separate calls, (f 3, f 4), we cannot
864         -- make a method constraint that then gets shared, thus:
865         --      let m = f %x in (m 3, m 4)
866         -- because that loses the linearity of the constraint.
867         -- The simplest thing to do is never to construct a method constraint
868         -- in the first place that has a linear implicit parameter in it.
869     want_method_inst fun_ty 
870         | opt_NoMethodSharing = False   
871         | otherwise           = case tcSplitSigmaTy fun_ty of
872                                   (_,[],_)    -> False  -- Not overloaded
873                                   (_,theta,_) -> not (any isLinearPred theta)
874
875     orig = OccurrenceOf id_name
876 \end{code}
877
878 %************************************************************************
879 %*                                                                      *
880 \subsection{Record bindings}
881 %*                                                                      *
882 %************************************************************************
883
884 Game plan for record bindings
885 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
886 1. Find the TyCon for the bindings, from the first field label.
887
888 2. Instantiate its tyvars and unify (T a1 .. an) with expected_ty.
889
890 For each binding field = value
891
892 3. Instantiate the field type (from the field label) using the type
893    envt from step 2.
894
895 4  Type check the value using tcArg, passing the field type as 
896    the expected argument type.
897
898 This extends OK when the field types are universally quantified.
899
900         
901 \begin{code}
902 tcRecordBinds
903         :: TyCon                -- Type constructor for the record
904         -> [TcType]             -- Args of this type constructor
905         -> HsRecordBinds Name
906         -> TcM (HsRecordBinds TcId)
907
908 tcRecordBinds tycon ty_args rbinds
909   = mappM do_bind rbinds
910   where
911     tenv = zipTopTvSubst (tyConTyVars tycon) ty_args
912
913     do_bind (L loc field_lbl, rhs)
914       = addErrCtxt (fieldCtxt field_lbl)        $
915         let
916             field_ty  = tyConFieldType tycon field_lbl
917             field_ty' = substTy tenv field_ty
918         in
919         tcCheckSigma rhs field_ty'              `thenM` \ rhs' ->
920         tcLookupId field_lbl                    `thenM` \ sel_id ->
921         ASSERT( isRecordSelector sel_id )
922         returnM (L loc sel_id, rhs')
923
924 tyConFieldType :: TyCon -> FieldLabel -> Type
925 tyConFieldType tycon field_lbl
926   = case [ty | (f,ty,_) <- tyConFields tycon, f == field_lbl] of
927         (ty:other) -> ASSERT( null other) ty
928                 -- This lookup and assertion will surely succeed, because
929                 -- we check that the fields are indeed record selectors
930                 -- before calling tcRecordBinds
931
932 badFields rbinds data_con
933   = filter (not . (`elem` field_names)) (recBindFields rbinds)
934   where
935     field_names = dataConFieldLabels data_con
936
937 checkMissingFields :: DataCon -> HsRecordBinds Name -> TcM ()
938 checkMissingFields data_con rbinds
939   | null field_labels   -- Not declared as a record;
940                         -- But C{} is still valid if no strict fields
941   = if any isMarkedStrict field_strs then
942         -- Illegal if any arg is strict
943         addErrTc (missingStrictFields data_con [])
944     else
945         returnM ()
946                         
947   | otherwise           -- A record
948   = checkM (null missing_s_fields)
949            (addErrTc (missingStrictFields data_con missing_s_fields))   `thenM_`
950
951     doptM Opt_WarnMissingFields         `thenM` \ warn ->
952     checkM (not (warn && notNull missing_ns_fields))
953            (warnTc True (missingFields data_con missing_ns_fields))
954
955   where
956     missing_s_fields
957         = [ fl | (fl, str) <- field_info,
958                  isMarkedStrict str,
959                  not (fl `elem` field_names_used)
960           ]
961     missing_ns_fields
962         = [ fl | (fl, str) <- field_info,
963                  not (isMarkedStrict str),
964                  not (fl `elem` field_names_used)
965           ]
966
967     field_names_used = recBindFields rbinds
968     field_labels     = dataConFieldLabels data_con
969
970     field_info = zipEqual "missingFields"
971                           field_labels
972                           field_strs
973
974     field_strs = dataConStrictMarks data_con
975 \end{code}
976
977 %************************************************************************
978 %*                                                                      *
979 \subsection{@tcCheckRhos@ typechecks a {\em list} of expressions}
980 %*                                                                      *
981 %************************************************************************
982
983 \begin{code}
984 tcCheckRhos :: [LHsExpr Name] -> [TcType] -> TcM [LHsExpr TcId]
985
986 tcCheckRhos [] [] = returnM []
987 tcCheckRhos (expr:exprs) (ty:tys)
988  = tcCheckRho  expr  ty         `thenM` \ expr' ->
989    tcCheckRhos exprs tys        `thenM` \ exprs' ->
990    returnM (expr':exprs')
991 \end{code}
992
993
994 %************************************************************************
995 %*                                                                      *
996 \subsection{Literals}
997 %*                                                                      *
998 %************************************************************************
999
1000 Overloaded literals.
1001
1002 \begin{code}
1003 tcLit :: HsLit -> Expected TcRhoType -> TcM (HsExpr TcId)
1004 tcLit lit res_ty 
1005   = zapExpectedTo res_ty (hsLitType lit)                `thenM_`
1006     returnM (HsLit lit)
1007 \end{code}
1008
1009
1010 %************************************************************************
1011 %*                                                                      *
1012 \subsection{Errors and contexts}
1013 %*                                                                      *
1014 %************************************************************************
1015
1016 Boring and alphabetical:
1017 \begin{code}
1018 arithSeqCtxt expr
1019   = hang (ptext SLIT("In an arithmetic sequence:")) 4 (ppr expr)
1020
1021 parrSeqCtxt expr
1022   = hang (ptext SLIT("In a parallel array sequence:")) 4 (ppr expr)
1023
1024 caseCtxt expr
1025   = hang (ptext SLIT("In the case expression:")) 4 (ppr expr)
1026
1027 caseScrutCtxt expr
1028   = hang (ptext SLIT("In the scrutinee of a case expression:")) 4 (ppr expr)
1029
1030 exprCtxt expr
1031   = hang (ptext SLIT("In the expression:")) 4 (ppr expr)
1032
1033 fieldCtxt field_name
1034   = ptext SLIT("In the") <+> quotes (ppr field_name) <+> ptext SLIT("field of a record")
1035
1036 funAppCtxt fun arg arg_no
1037   = hang (hsep [ ptext SLIT("In the"), speakNth arg_no, ptext SLIT("argument of"), 
1038                     quotes (ppr fun) <> text ", namely"])
1039          4 (quotes (ppr arg))
1040
1041 listCtxt expr
1042   = hang (ptext SLIT("In the list element:")) 4 (ppr expr)
1043
1044 parrCtxt expr
1045   = hang (ptext SLIT("In the parallel array element:")) 4 (ppr expr)
1046
1047 predCtxt expr
1048   = hang (ptext SLIT("In the predicate expression:")) 4 (ppr expr)
1049
1050 appCtxt fun args
1051   = ptext SLIT("In the application") <+> quotes (ppr the_app)
1052   where
1053     the_app = foldl mkHsApp fun args    -- Used in error messages
1054
1055 badFieldsUpd rbinds
1056   = hang (ptext SLIT("No constructor has all these fields:"))
1057          4 (pprQuotedList (recBindFields rbinds))
1058
1059 recordUpdCtxt expr = ptext SLIT("In the record update:") <+> ppr expr
1060 recordConCtxt expr = ptext SLIT("In the record construction:") <+> ppr expr
1061
1062 notSelector field
1063   = hsep [quotes (ppr field), ptext SLIT("is not a record selector")]
1064
1065 missingStrictFields :: DataCon -> [FieldLabel] -> SDoc
1066 missingStrictFields con fields
1067   = header <> rest
1068   where
1069     rest | null fields = empty  -- Happens for non-record constructors 
1070                                 -- with strict fields
1071          | otherwise   = colon <+> pprWithCommas ppr fields
1072
1073     header = ptext SLIT("Constructor") <+> quotes (ppr con) <+> 
1074              ptext SLIT("does not have the required strict field(s)") 
1075           
1076 missingFields :: DataCon -> [FieldLabel] -> SDoc
1077 missingFields con fields
1078   = ptext SLIT("Fields of") <+> quotes (ppr con) <+> ptext SLIT("not initialised:") 
1079         <+> pprWithCommas ppr fields
1080
1081 wrongArgsCtxt too_many_or_few fun args
1082   = hang (ptext SLIT("Probable cause:") <+> quotes (ppr fun)
1083                     <+> ptext SLIT("is applied to") <+> text too_many_or_few 
1084                     <+> ptext SLIT("arguments in the call"))
1085          4 (parens (ppr the_app))
1086   where
1087     the_app = foldl mkHsApp fun args    -- Used in error messages
1088
1089 #ifdef GHCI
1090 polySpliceErr :: Id -> SDoc
1091 polySpliceErr id
1092   = ptext SLIT("Can't splice the polymorphic local variable") <+> quotes (ppr id)
1093 #endif
1094 \end{code}