Tidy up the treatment of SPECIALISE pragmas
[ghc-hetmet.git] / compiler / typecheck / TcHsSyn.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1996-1998
4 %
5
6 TcHsSyn: Specialisations of the @HsSyn@ syntax for the typechecker
7
8 This module is an extension of @HsSyn@ syntax, for use in the type
9 checker.
10
11 \begin{code}
12 {-# OPTIONS -w #-}
13 -- The above warning supression flag is a temporary kludge.
14 -- While working on this module you are encouraged to remove it and fix
15 -- any warnings in the module. See
16 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
17 -- for details
18
19 module TcHsSyn (
20         mkHsConApp, mkHsDictLet, mkHsApp,
21         hsLitType, hsLPatType, hsPatType, 
22         mkHsAppTy, mkSimpleHsAlt,
23         nlHsIntLit, mkVanillaTuplePat,
24         
25         mkArbitraryType,        -- Put this elsewhere?
26
27         -- re-exported from TcMonad
28         TcId, TcIdSet, TcDictBinds,
29
30         zonkTopDecls, zonkTopExpr, zonkTopLExpr,
31         zonkId, zonkTopBndrs
32   ) where
33
34 #include "HsVersions.h"
35
36 -- friends:
37 import HsSyn    -- oodles of it
38
39 -- others:
40 import Id
41
42 import TcRnMonad
43 import Type
44 import TcType
45 import TcMType
46 import TysPrim
47 import TysWiredIn
48 import TyCon
49 import Name
50 import Var
51 import VarSet
52 import VarEnv
53 import BasicTypes
54 import Maybes
55 import Unique
56 import SrcLoc
57 import Util
58 import Bag
59 import Outputable
60 \end{code}
61
62
63 %************************************************************************
64 %*                                                                      *
65 \subsection[mkFailurePair]{Code for pattern-matching and other failures}
66 %*                                                                      *
67 %************************************************************************
68
69 Note: If @hsLPatType@ doesn't bear a strong resemblance to @exprType@,
70 then something is wrong.
71 \begin{code}
72 mkVanillaTuplePat :: [OutPat Id] -> Boxity -> Pat Id
73 -- A vanilla tuple pattern simply gets its type from its sub-patterns
74 mkVanillaTuplePat pats box 
75   = TuplePat pats box (mkTupleTy box (length pats) (map hsLPatType pats))
76
77 hsLPatType :: OutPat Id -> Type
78 hsLPatType (L _ pat) = hsPatType pat
79
80 hsPatType (ParPat pat)              = hsLPatType pat
81 hsPatType (WildPat ty)              = ty
82 hsPatType (VarPat var)              = idType var
83 hsPatType (VarPatOut var _)         = idType var
84 hsPatType (BangPat pat)             = hsLPatType pat
85 hsPatType (LazyPat pat)             = hsLPatType pat
86 hsPatType (LitPat lit)              = hsLitType lit
87 hsPatType (AsPat var pat)           = idType (unLoc var)
88 hsPatType (ViewPat expr pat ty)     = ty
89 hsPatType (ListPat _ ty)            = mkListTy ty
90 hsPatType (PArrPat _ ty)            = mkPArrTy ty
91 hsPatType (TuplePat pats box ty)    = ty
92 hsPatType (ConPatOut{ pat_ty = ty })= ty
93 hsPatType (SigPatOut pat ty)        = ty
94 hsPatType (NPat lit _ _)            = overLitType lit
95 hsPatType (NPlusKPat id _ _ _)      = idType (unLoc id)
96 hsPatType (CoPat _ _ ty)            = ty
97
98 hsLitType :: HsLit -> TcType
99 hsLitType (HsChar c)       = charTy
100 hsLitType (HsCharPrim c)   = charPrimTy
101 hsLitType (HsString str)   = stringTy
102 hsLitType (HsStringPrim s) = addrPrimTy
103 hsLitType (HsInt i)        = intTy
104 hsLitType (HsIntPrim i)    = intPrimTy
105 hsLitType (HsInteger i ty) = ty
106 hsLitType (HsRat _ ty)     = ty
107 hsLitType (HsFloatPrim f)  = floatPrimTy
108 hsLitType (HsDoublePrim d) = doublePrimTy
109 \end{code}
110
111
112 %************************************************************************
113 %*                                                                      *
114 \subsection[BackSubst-HsBinds]{Running a substitution over @HsBinds@}
115 %*                                                                      *
116 %************************************************************************
117
118 \begin{code}
119 -- zonkId is used *during* typechecking just to zonk the Id's type
120 zonkId :: TcId -> TcM TcId
121 zonkId id
122   = zonkTcType (idType id) `thenM` \ ty' ->
123     returnM (Id.setIdType id ty')
124 \end{code}
125
126 The rest of the zonking is done *after* typechecking.
127 The main zonking pass runs over the bindings
128
129  a) to convert TcTyVars to TyVars etc, dereferencing any bindings etc
130  b) convert unbound TcTyVar to Void
131  c) convert each TcId to an Id by zonking its type
132
133 The type variables are converted by binding mutable tyvars to immutable ones
134 and then zonking as normal.
135
136 The Ids are converted by binding them in the normal Tc envt; that
137 way we maintain sharing; eg an Id is zonked at its binding site and they
138 all occurrences of that Id point to the common zonked copy
139
140 It's all pretty boring stuff, because HsSyn is such a large type, and 
141 the environment manipulation is tiresome.
142
143 \begin{code}
144 data ZonkEnv = ZonkEnv  (TcType -> TcM Type)    -- How to zonk a type
145                         (IdEnv Id)              -- What variables are in scope
146         -- Maps an Id to its zonked version; both have the same Name
147         -- Is only consulted lazily; hence knot-tying
148
149 emptyZonkEnv = ZonkEnv zonkTypeZapping emptyVarEnv
150
151 extendZonkEnv :: ZonkEnv -> [Id] -> ZonkEnv
152 extendZonkEnv (ZonkEnv zonk_ty env) ids 
153   = ZonkEnv zonk_ty (extendVarEnvList env [(id,id) | id <- ids])
154
155 extendZonkEnv1 :: ZonkEnv -> Id -> ZonkEnv
156 extendZonkEnv1 (ZonkEnv zonk_ty env) id 
157   = ZonkEnv zonk_ty (extendVarEnv env id id)
158
159 setZonkType :: ZonkEnv -> (TcType -> TcM Type) -> ZonkEnv
160 setZonkType (ZonkEnv _ env) zonk_ty = ZonkEnv zonk_ty env
161
162 zonkEnvIds :: ZonkEnv -> [Id]
163 zonkEnvIds (ZonkEnv _ env) = varEnvElts env
164
165 zonkIdOcc :: ZonkEnv -> TcId -> Id
166 -- Ids defined in this module should be in the envt; 
167 -- ignore others.  (Actually, data constructors are also
168 -- not LocalVars, even when locally defined, but that is fine.)
169 -- (Also foreign-imported things aren't currently in the ZonkEnv;
170 --  that's ok because they don't need zonking.)
171 --
172 -- Actually, Template Haskell works in 'chunks' of declarations, and
173 -- an earlier chunk won't be in the 'env' that the zonking phase 
174 -- carries around.  Instead it'll be in the tcg_gbl_env, already fully
175 -- zonked.  There's no point in looking it up there (except for error 
176 -- checking), and it's not conveniently to hand; hence the simple
177 -- 'orElse' case in the LocalVar branch.
178 --
179 -- Even without template splices, in module Main, the checking of
180 -- 'main' is done as a separate chunk.
181 zonkIdOcc (ZonkEnv zonk_ty env) id 
182   | isLocalVar id = lookupVarEnv env id `orElse` id
183   | otherwise     = id
184
185 zonkIdOccs env ids = map (zonkIdOcc env) ids
186
187 -- zonkIdBndr is used *after* typechecking to get the Id's type
188 -- to its final form.  The TyVarEnv give 
189 zonkIdBndr :: ZonkEnv -> TcId -> TcM Id
190 zonkIdBndr env id
191   = zonkTcTypeToType env (idType id)    `thenM` \ ty' ->
192     returnM (Id.setIdType id ty')
193
194 zonkIdBndrs :: ZonkEnv -> [TcId] -> TcM [Id]
195 zonkIdBndrs env ids = mappM (zonkIdBndr env) ids
196
197 zonkDictBndrs :: ZonkEnv -> [Var] -> TcM [Var]
198 -- "Dictionary" binders can be coercion variables or dictionary variables
199 zonkDictBndrs env ids = mappM (zonkDictBndr env) ids
200
201 zonkDictBndr env var | isTyVar var = return var
202                      | otherwise   = zonkIdBndr env var
203
204 zonkTopBndrs :: [TcId] -> TcM [Id]
205 zonkTopBndrs ids = zonkIdBndrs emptyZonkEnv ids
206 \end{code}
207
208
209 \begin{code}
210 zonkTopExpr :: HsExpr TcId -> TcM (HsExpr Id)
211 zonkTopExpr e = zonkExpr emptyZonkEnv e
212
213 zonkTopLExpr :: LHsExpr TcId -> TcM (LHsExpr Id)
214 zonkTopLExpr e = zonkLExpr emptyZonkEnv e
215
216 zonkTopDecls :: LHsBinds TcId -> [LRuleDecl TcId] -> [LForeignDecl TcId]
217              -> TcM ([Id], 
218                      Bag (LHsBind  Id),
219                      [LForeignDecl Id],
220                      [LRuleDecl    Id])
221 zonkTopDecls binds rules fords
222   = do  { (env, binds') <- zonkRecMonoBinds emptyZonkEnv binds
223                         -- Top level is implicitly recursive
224         ; rules' <- zonkRules env rules
225         ; fords' <- zonkForeignExports env fords
226         ; return (zonkEnvIds env, binds', fords', rules') }
227
228 ---------------------------------------------
229 zonkLocalBinds :: ZonkEnv -> HsLocalBinds TcId -> TcM (ZonkEnv, HsLocalBinds Id)
230 zonkLocalBinds env EmptyLocalBinds
231   = return (env, EmptyLocalBinds)
232
233 zonkLocalBinds env (HsValBinds binds)
234   = do  { (env1, new_binds) <- zonkValBinds env binds
235         ; return (env1, HsValBinds new_binds) }
236
237 zonkLocalBinds env (HsIPBinds (IPBinds binds dict_binds))
238   = mappM (wrapLocM zonk_ip_bind) binds `thenM` \ new_binds ->
239     let
240         env1 = extendZonkEnv env [ipNameName n | L _ (IPBind n _) <- new_binds]
241     in
242     zonkRecMonoBinds env1 dict_binds    `thenM` \ (env2, new_dict_binds) -> 
243     returnM (env2, HsIPBinds (IPBinds new_binds new_dict_binds))
244   where
245     zonk_ip_bind (IPBind n e)
246         = mapIPNameTc (zonkIdBndr env) n        `thenM` \ n' ->
247           zonkLExpr env e                       `thenM` \ e' ->
248           returnM (IPBind n' e')
249
250
251 ---------------------------------------------
252 zonkValBinds :: ZonkEnv -> HsValBinds TcId -> TcM (ZonkEnv, HsValBinds Id)
253 zonkValBinds env bs@(ValBindsIn _ _) 
254   = panic "zonkValBinds"        -- Not in typechecker output
255 zonkValBinds env (ValBindsOut binds sigs) 
256   = do  { (env1, new_binds) <- go env binds
257         ; return (env1, ValBindsOut new_binds sigs) }
258   where
259     go env []         = return (env, [])
260     go env ((r,b):bs) = do { (env1, b')  <- zonkRecMonoBinds env b
261                            ; (env2, bs') <- go env1 bs
262                            ; return (env2, (r,b'):bs') }
263
264 ---------------------------------------------
265 zonkRecMonoBinds :: ZonkEnv -> LHsBinds TcId -> TcM (ZonkEnv, LHsBinds Id)
266 zonkRecMonoBinds env binds 
267  = fixM (\ ~(_, new_binds) -> do 
268         { let env1 = extendZonkEnv env (collectHsBindBinders new_binds)
269         ; binds' <- zonkMonoBinds env1 binds
270         ; return (env1, binds') })
271
272 ---------------------------------------------
273 zonkMonoBinds :: ZonkEnv -> LHsBinds TcId -> TcM (LHsBinds Id)
274 zonkMonoBinds env binds = mapBagM (wrapLocM (zonk_bind env)) binds
275
276 zonk_bind :: ZonkEnv -> HsBind TcId -> TcM (HsBind Id)
277 zonk_bind env bind@(PatBind { pat_lhs = pat, pat_rhs = grhss, pat_rhs_ty = ty})
278   = do  { (_env, new_pat) <- zonkPat env pat            -- Env already extended
279         ; new_grhss <- zonkGRHSs env grhss
280         ; new_ty    <- zonkTcTypeToType env ty
281         ; return (bind { pat_lhs = new_pat, pat_rhs = new_grhss, pat_rhs_ty = new_ty }) }
282
283 zonk_bind env (VarBind { var_id = var, var_rhs = expr })
284   = zonkIdBndr env var                  `thenM` \ new_var ->
285     zonkLExpr env expr                  `thenM` \ new_expr ->
286     returnM (VarBind { var_id = new_var, var_rhs = new_expr })
287
288 zonk_bind env bind@(FunBind { fun_id = var, fun_matches = ms, fun_co_fn = co_fn })
289   = wrapLocM (zonkIdBndr env) var       `thenM` \ new_var ->
290     zonkCoFn env co_fn                  `thenM` \ (env1, new_co_fn) ->
291     zonkMatchGroup env1 ms              `thenM` \ new_ms ->
292     returnM (bind { fun_id = new_var, fun_matches = new_ms, fun_co_fn = new_co_fn })
293
294 zonk_bind env (AbsBinds { abs_tvs = tyvars, abs_dicts = dicts, 
295                           abs_exports = exports, abs_binds = val_binds })
296   = ASSERT( all isImmutableTyVar tyvars )
297     zonkDictBndrs env dicts                     `thenM` \ new_dicts ->
298     fixM (\ ~(new_val_binds, _) ->
299         let
300           env1 = extendZonkEnv env new_dicts
301           env2 = extendZonkEnv env1 (collectHsBindBinders new_val_binds)
302         in
303         zonkMonoBinds env2 val_binds            `thenM` \ new_val_binds ->
304         mappM (zonkExport env2) exports         `thenM` \ new_exports ->
305         returnM (new_val_binds, new_exports)
306     )                                           `thenM` \ (new_val_bind, new_exports) ->
307     returnM (AbsBinds { abs_tvs = tyvars, abs_dicts = new_dicts, 
308                         abs_exports = new_exports, abs_binds = new_val_bind })
309   where
310     zonkExport env (tyvars, global, local, prags)
311         -- The tyvars are already zonked
312         = zonkIdBndr env global                 `thenM` \ new_global ->
313           mapM zonk_prag prags                  `thenM` \ new_prags -> 
314           returnM (tyvars, new_global, zonkIdOcc env local, new_prags)
315     zonk_prag prag@(L _ (InlinePrag {}))  = return prag
316     zonk_prag (L loc (SpecPrag expr ty inl))
317         = do { expr' <- zonkExpr env expr 
318              ; ty'   <- zonkTcTypeToType env ty
319              ; return (L loc (SpecPrag expr' ty' inl)) }
320 \end{code}
321
322 %************************************************************************
323 %*                                                                      *
324 \subsection[BackSubst-Match-GRHSs]{Match and GRHSs}
325 %*                                                                      *
326 %************************************************************************
327
328 \begin{code}
329 zonkMatchGroup :: ZonkEnv -> MatchGroup TcId-> TcM (MatchGroup Id)
330 zonkMatchGroup env (MatchGroup ms ty) 
331   = do  { ms' <- mapM (zonkMatch env) ms
332         ; ty' <- zonkTcTypeToType env ty
333         ; return (MatchGroup ms' ty') }
334
335 zonkMatch :: ZonkEnv -> LMatch TcId-> TcM (LMatch Id)
336 zonkMatch env (L loc (Match pats _ grhss))
337   = do  { (env1, new_pats) <- zonkPats env pats
338         ; new_grhss <- zonkGRHSs env1 grhss
339         ; return (L loc (Match new_pats Nothing new_grhss)) }
340
341 -------------------------------------------------------------------------
342 zonkGRHSs :: ZonkEnv -> GRHSs TcId -> TcM (GRHSs Id)
343
344 zonkGRHSs env (GRHSs grhss binds)
345   = zonkLocalBinds env binds    `thenM` \ (new_env, new_binds) ->
346     let
347         zonk_grhs (GRHS guarded rhs)
348           = zonkStmts new_env guarded   `thenM` \ (env2, new_guarded) ->
349             zonkLExpr env2 rhs          `thenM` \ new_rhs ->
350             returnM (GRHS new_guarded new_rhs)
351     in
352     mappM (wrapLocM zonk_grhs) grhss    `thenM` \ new_grhss ->
353     returnM (GRHSs new_grhss new_binds)
354 \end{code}
355
356 %************************************************************************
357 %*                                                                      *
358 \subsection[BackSubst-HsExpr]{Running a zonkitution over a TypeCheckedExpr}
359 %*                                                                      *
360 %************************************************************************
361
362 \begin{code}
363 zonkLExprs :: ZonkEnv -> [LHsExpr TcId] -> TcM [LHsExpr Id]
364 zonkLExpr  :: ZonkEnv -> LHsExpr TcId   -> TcM (LHsExpr Id)
365 zonkExpr   :: ZonkEnv -> HsExpr TcId    -> TcM (HsExpr Id)
366
367 zonkLExprs env exprs = mappM (zonkLExpr env) exprs
368 zonkLExpr  env expr  = wrapLocM (zonkExpr env) expr
369
370 zonkExpr env (HsVar id)
371   = returnM (HsVar (zonkIdOcc env id))
372
373 zonkExpr env (HsIPVar id)
374   = returnM (HsIPVar (mapIPName (zonkIdOcc env) id))
375
376 zonkExpr env (HsLit (HsRat f ty))
377   = zonkTcTypeToType env ty        `thenM` \ new_ty  ->
378     returnM (HsLit (HsRat f new_ty))
379
380 zonkExpr env (HsLit lit)
381   = returnM (HsLit lit)
382
383 zonkExpr env (HsOverLit lit)
384   = do  { lit' <- zonkOverLit env lit
385         ; return (HsOverLit lit') }
386
387 zonkExpr env (HsLam matches)
388   = zonkMatchGroup env matches  `thenM` \ new_matches ->
389     returnM (HsLam new_matches)
390
391 zonkExpr env (HsApp e1 e2)
392   = zonkLExpr env e1    `thenM` \ new_e1 ->
393     zonkLExpr env e2    `thenM` \ new_e2 ->
394     returnM (HsApp new_e1 new_e2)
395
396 zonkExpr env (HsBracketOut body bs) 
397   = mappM zonk_b bs     `thenM` \ bs' ->
398     returnM (HsBracketOut body bs')
399   where
400     zonk_b (n,e) = zonkLExpr env e      `thenM` \ e' ->
401                    returnM (n,e')
402
403 zonkExpr env (HsSpliceE s) = WARN( True, ppr s )        -- Should not happen
404                              returnM (HsSpliceE s)
405
406 zonkExpr env (OpApp e1 op fixity e2)
407   = zonkLExpr env e1    `thenM` \ new_e1 ->
408     zonkLExpr env op    `thenM` \ new_op ->
409     zonkLExpr env e2    `thenM` \ new_e2 ->
410     returnM (OpApp new_e1 new_op fixity new_e2)
411
412 zonkExpr env (NegApp expr op)
413   = zonkLExpr env expr  `thenM` \ new_expr ->
414     zonkExpr env op     `thenM` \ new_op ->
415     returnM (NegApp new_expr new_op)
416
417 zonkExpr env (HsPar e)    
418   = zonkLExpr env e     `thenM` \new_e ->
419     returnM (HsPar new_e)
420
421 zonkExpr env (SectionL expr op)
422   = zonkLExpr env expr  `thenM` \ new_expr ->
423     zonkLExpr env op            `thenM` \ new_op ->
424     returnM (SectionL new_expr new_op)
425
426 zonkExpr env (SectionR op expr)
427   = zonkLExpr env op            `thenM` \ new_op ->
428     zonkLExpr env expr          `thenM` \ new_expr ->
429     returnM (SectionR new_op new_expr)
430
431 zonkExpr env (HsCase expr ms)
432   = zonkLExpr env expr          `thenM` \ new_expr ->
433     zonkMatchGroup env ms       `thenM` \ new_ms ->
434     returnM (HsCase new_expr new_ms)
435
436 zonkExpr env (HsIf e1 e2 e3)
437   = zonkLExpr env e1    `thenM` \ new_e1 ->
438     zonkLExpr env e2    `thenM` \ new_e2 ->
439     zonkLExpr env e3    `thenM` \ new_e3 ->
440     returnM (HsIf new_e1 new_e2 new_e3)
441
442 zonkExpr env (HsLet binds expr)
443   = zonkLocalBinds env binds    `thenM` \ (new_env, new_binds) ->
444     zonkLExpr new_env expr      `thenM` \ new_expr ->
445     returnM (HsLet new_binds new_expr)
446
447 zonkExpr env (HsDo do_or_lc stmts body ty)
448   = zonkStmts env stmts         `thenM` \ (new_env, new_stmts) ->
449     zonkLExpr new_env body      `thenM` \ new_body ->
450     zonkTcTypeToType env ty     `thenM` \ new_ty   ->
451     returnM (HsDo (zonkDo env do_or_lc) 
452                   new_stmts new_body new_ty)
453
454 zonkExpr env (ExplicitList ty exprs)
455   = zonkTcTypeToType env ty     `thenM` \ new_ty ->
456     zonkLExprs env exprs        `thenM` \ new_exprs ->
457     returnM (ExplicitList new_ty new_exprs)
458
459 zonkExpr env (ExplicitPArr ty exprs)
460   = zonkTcTypeToType env ty     `thenM` \ new_ty ->
461     zonkLExprs env exprs        `thenM` \ new_exprs ->
462     returnM (ExplicitPArr new_ty new_exprs)
463
464 zonkExpr env (ExplicitTuple exprs boxed)
465   = zonkLExprs env exprs        `thenM` \ new_exprs ->
466     returnM (ExplicitTuple new_exprs boxed)
467
468 zonkExpr env (RecordCon data_con con_expr rbinds)
469   = do  { new_con_expr <- zonkExpr env con_expr
470         ; new_rbinds   <- zonkRecFields env rbinds
471         ; return (RecordCon data_con new_con_expr new_rbinds) }
472
473 zonkExpr env (RecordUpd expr rbinds cons in_tys out_tys)
474   = do  { new_expr    <- zonkLExpr env expr
475         ; new_in_tys  <- mapM (zonkTcTypeToType env) in_tys
476         ; new_out_tys <- mapM (zonkTcTypeToType env) out_tys
477         ; new_rbinds  <- zonkRecFields env rbinds
478         ; return (RecordUpd new_expr new_rbinds cons new_in_tys new_out_tys) }
479
480 zonkExpr env (ExprWithTySigOut e ty) 
481   = do { e' <- zonkLExpr env e
482        ; return (ExprWithTySigOut e' ty) }
483
484 zonkExpr env (ExprWithTySig _ _) = panic "zonkExpr env:ExprWithTySig"
485
486 zonkExpr env (ArithSeq expr info)
487   = zonkExpr env expr           `thenM` \ new_expr ->
488     zonkArithSeq env info       `thenM` \ new_info ->
489     returnM (ArithSeq new_expr new_info)
490
491 zonkExpr env (PArrSeq expr info)
492   = zonkExpr env expr           `thenM` \ new_expr ->
493     zonkArithSeq env info       `thenM` \ new_info ->
494     returnM (PArrSeq new_expr new_info)
495
496 zonkExpr env (HsSCC lbl expr)
497   = zonkLExpr env expr  `thenM` \ new_expr ->
498     returnM (HsSCC lbl new_expr)
499
500 zonkExpr env (HsTickPragma info expr)
501   = zonkLExpr env expr  `thenM` \ new_expr ->
502     returnM (HsTickPragma info new_expr)
503
504 -- hdaume: core annotations
505 zonkExpr env (HsCoreAnn lbl expr)
506   = zonkLExpr env expr   `thenM` \ new_expr ->
507     returnM (HsCoreAnn lbl new_expr)
508
509 -- arrow notation extensions
510 zonkExpr env (HsProc pat body)
511   = do  { (env1, new_pat) <- zonkPat env pat
512         ; new_body <- zonkCmdTop env1 body
513         ; return (HsProc new_pat new_body) }
514
515 zonkExpr env (HsArrApp e1 e2 ty ho rl)
516   = zonkLExpr env e1                    `thenM` \ new_e1 ->
517     zonkLExpr env e2                    `thenM` \ new_e2 ->
518     zonkTcTypeToType env ty             `thenM` \ new_ty ->
519     returnM (HsArrApp new_e1 new_e2 new_ty ho rl)
520
521 zonkExpr env (HsArrForm op fixity args)
522   = zonkLExpr env op                    `thenM` \ new_op ->
523     mappM (zonkCmdTop env) args         `thenM` \ new_args ->
524     returnM (HsArrForm new_op fixity new_args)
525
526 zonkExpr env (HsWrap co_fn expr)
527   = zonkCoFn env co_fn  `thenM` \ (env1, new_co_fn) ->
528     zonkExpr env1 expr  `thenM` \ new_expr ->
529     return (HsWrap new_co_fn new_expr)
530
531 zonkExpr env other = pprPanic "zonkExpr" (ppr other)
532
533 zonkCmdTop :: ZonkEnv -> LHsCmdTop TcId -> TcM (LHsCmdTop Id)
534 zonkCmdTop env cmd = wrapLocM (zonk_cmd_top env) cmd
535
536 zonk_cmd_top env (HsCmdTop cmd stack_tys ty ids)
537   = zonkLExpr env cmd                   `thenM` \ new_cmd ->
538     zonkTcTypeToTypes env stack_tys     `thenM` \ new_stack_tys ->
539     zonkTcTypeToType env ty             `thenM` \ new_ty ->
540     mapSndM (zonkExpr env) ids          `thenM` \ new_ids ->
541     returnM (HsCmdTop new_cmd new_stack_tys new_ty new_ids)
542
543 -------------------------------------------------------------------------
544 zonkCoFn :: ZonkEnv -> HsWrapper -> TcM (ZonkEnv, HsWrapper)
545 zonkCoFn env WpHole   = return (env, WpHole)
546 zonkCoFn env WpInline = return (env, WpInline)
547 zonkCoFn env (WpCompose c1 c2) = do { (env1, c1') <- zonkCoFn env c1
548                                     ; (env2, c2') <- zonkCoFn env1 c2
549                                     ; return (env2, WpCompose c1' c2') }
550 zonkCoFn env (WpCo co)      = do { co' <- zonkTcTypeToType env co
551                                  ; return (env, WpCo co') }
552 zonkCoFn env (WpLam id)     = do { id' <- zonkDictBndr env id
553                                  ; let env1 = extendZonkEnv1 env id'
554                                  ; return (env1, WpLam id') }
555 zonkCoFn env (WpTyLam tv)   = ASSERT( isImmutableTyVar tv )
556                               do { return (env, WpTyLam tv) }
557 zonkCoFn env (WpApp id)     = do { return (env, WpApp (zonkIdOcc env id)) }
558 zonkCoFn env (WpTyApp ty)   = do { ty' <- zonkTcTypeToType env ty
559                                  ; return (env, WpTyApp ty') }
560 zonkCoFn env (WpLet bs)     = do { (env1, bs') <- zonkRecMonoBinds env bs
561                                  ; return (env1, WpLet bs') }
562
563
564 -------------------------------------------------------------------------
565 zonkDo :: ZonkEnv -> HsStmtContext Name -> HsStmtContext Name
566 -- Only used for 'do', so the only Ids are in a MDoExpr table
567 zonkDo env (MDoExpr tbl) = MDoExpr (mapSnd (zonkIdOcc env) tbl)
568 zonkDo env do_or_lc      = do_or_lc
569
570 -------------------------------------------------------------------------
571 zonkOverLit :: ZonkEnv -> HsOverLit TcId -> TcM (HsOverLit Id)
572 zonkOverLit env ol = 
573     let 
574         zonkedStuff = do ty' <- zonkTcTypeToType env (overLitType ol)
575                          e' <- zonkExpr env (overLitExpr ol)
576                          return (e', ty')
577         ru f (x, y) = return (f x y)
578     in
579       case ol of 
580         (HsIntegral i _ _)   -> ru (HsIntegral i) =<< zonkedStuff
581         (HsFractional r _ _) -> ru (HsFractional r) =<< zonkedStuff
582         (HsIsString s _ _)   -> ru (HsIsString s) =<< zonkedStuff
583
584 -------------------------------------------------------------------------
585 zonkArithSeq :: ZonkEnv -> ArithSeqInfo TcId -> TcM (ArithSeqInfo Id)
586
587 zonkArithSeq env (From e)
588   = zonkLExpr env e             `thenM` \ new_e ->
589     returnM (From new_e)
590
591 zonkArithSeq env (FromThen e1 e2)
592   = zonkLExpr env e1    `thenM` \ new_e1 ->
593     zonkLExpr env e2    `thenM` \ new_e2 ->
594     returnM (FromThen new_e1 new_e2)
595
596 zonkArithSeq env (FromTo e1 e2)
597   = zonkLExpr env e1    `thenM` \ new_e1 ->
598     zonkLExpr env e2    `thenM` \ new_e2 ->
599     returnM (FromTo new_e1 new_e2)
600
601 zonkArithSeq env (FromThenTo e1 e2 e3)
602   = zonkLExpr env e1    `thenM` \ new_e1 ->
603     zonkLExpr env e2    `thenM` \ new_e2 ->
604     zonkLExpr env e3    `thenM` \ new_e3 ->
605     returnM (FromThenTo new_e1 new_e2 new_e3)
606
607
608 -------------------------------------------------------------------------
609 zonkStmts :: ZonkEnv -> [LStmt TcId] -> TcM (ZonkEnv, [LStmt Id])
610 zonkStmts env []     = return (env, [])
611 zonkStmts env (s:ss) = do { (env1, s')  <- wrapLocSndM (zonkStmt env) s
612                           ; (env2, ss') <- zonkStmts env1 ss
613                           ; return (env2, s' : ss') }
614
615 zonkStmt :: ZonkEnv -> Stmt TcId -> TcM (ZonkEnv, Stmt Id)
616 zonkStmt env (ParStmt stmts_w_bndrs)
617   = mappM zonk_branch stmts_w_bndrs     `thenM` \ new_stmts_w_bndrs ->
618     let 
619         new_binders = concat (map snd new_stmts_w_bndrs)
620         env1 = extendZonkEnv env new_binders
621     in
622     return (env1, ParStmt new_stmts_w_bndrs)
623   where
624     zonk_branch (stmts, bndrs) = zonkStmts env stmts    `thenM` \ (env1, new_stmts) ->
625                                  returnM (new_stmts, zonkIdOccs env1 bndrs)
626
627 zonkStmt env (RecStmt segStmts lvs rvs rets binds)
628   = zonkIdBndrs env rvs         `thenM` \ new_rvs ->
629     let
630         env1 = extendZonkEnv env new_rvs
631     in
632     zonkStmts env1 segStmts     `thenM` \ (env2, new_segStmts) ->
633         -- Zonk the ret-expressions in an envt that 
634         -- has the polymorphic bindings in the envt
635     mapM (zonkExpr env2) rets   `thenM` \ new_rets ->
636     let
637         new_lvs = zonkIdOccs env2 lvs
638         env3 = extendZonkEnv env new_lvs        -- Only the lvs are needed
639     in
640     zonkRecMonoBinds env3 binds `thenM` \ (env4, new_binds) ->
641     returnM (env4, RecStmt new_segStmts new_lvs new_rvs new_rets new_binds)
642
643 zonkStmt env (ExprStmt expr then_op ty)
644   = zonkLExpr env expr          `thenM` \ new_expr ->
645     zonkExpr env then_op        `thenM` \ new_then ->
646     zonkTcTypeToType env ty     `thenM` \ new_ty ->
647     returnM (env, ExprStmt new_expr new_then new_ty)
648
649 zonkStmt env (TransformStmt (stmts, binders) usingExpr maybeByExpr)
650   = do { (env', stmts') <- zonkStmts env stmts 
651     ; let binders' = zonkIdOccs env' binders
652     ; usingExpr' <- zonkLExpr env' usingExpr
653     ; maybeByExpr' <- zonkMaybeLExpr env' maybeByExpr
654     ; return (env', TransformStmt (stmts', binders') usingExpr' maybeByExpr') }
655     
656 zonkStmt env (GroupStmt (stmts, binderMap) groupByClause)
657   = do { (env', stmts') <- zonkStmts env stmts 
658     ; binderMap' <- mappM (zonkBinderMapEntry env') binderMap
659     ; groupByClause' <- 
660         case groupByClause of
661             GroupByNothing usingExpr -> (zonkLExpr env' usingExpr) >>= (return . GroupByNothing)
662             GroupBySomething eitherUsingExpr byExpr -> do
663                 eitherUsingExpr' <- mapEitherM (zonkLExpr env') (zonkExpr env') eitherUsingExpr
664                 byExpr' <- zonkLExpr env' byExpr
665                 return $ GroupBySomething eitherUsingExpr' byExpr'
666                 
667     ; let env'' = extendZonkEnv env' (map snd binderMap')
668     ; return (env'', GroupStmt (stmts', binderMap') groupByClause') }
669   where
670     mapEitherM f g x = do
671       case x of
672         Left a -> f a >>= (return . Left)
673         Right b -> g b >>= (return . Right)
674   
675     zonkBinderMapEntry env (oldBinder, newBinder) = do 
676         let oldBinder' = zonkIdOcc env oldBinder
677         newBinder' <- zonkIdBndr env newBinder
678         return (oldBinder', newBinder') 
679
680 zonkStmt env (LetStmt binds)
681   = zonkLocalBinds env binds    `thenM` \ (env1, new_binds) ->
682     returnM (env1, LetStmt new_binds)
683
684 zonkStmt env (BindStmt pat expr bind_op fail_op)
685   = do  { new_expr <- zonkLExpr env expr
686         ; (env1, new_pat) <- zonkPat env pat
687         ; new_bind <- zonkExpr env bind_op
688         ; new_fail <- zonkExpr env fail_op
689         ; return (env1, BindStmt new_pat new_expr new_bind new_fail) }
690
691 zonkMaybeLExpr env Nothing = return Nothing
692 zonkMaybeLExpr env (Just e) = (zonkLExpr env e) >>= (return . Just)
693
694
695 -------------------------------------------------------------------------
696 zonkRecFields :: ZonkEnv -> HsRecordBinds TcId -> TcM (HsRecordBinds TcId)
697 zonkRecFields env (HsRecFields flds dd)
698   = do  { flds' <- mappM zonk_rbind flds
699         ; return (HsRecFields flds' dd) }
700   where
701     zonk_rbind fld
702       = do { new_expr <- zonkLExpr env (hsRecFieldArg fld)
703            ; return (fld { hsRecFieldArg = new_expr }) }
704         -- Field selectors have declared types; hence no zonking
705
706 -------------------------------------------------------------------------
707 mapIPNameTc :: (a -> TcM b) -> IPName a -> TcM (IPName b)
708 mapIPNameTc f (IPName n) = f n  `thenM` \ r -> returnM (IPName r)
709 \end{code}
710
711
712 %************************************************************************
713 %*                                                                      *
714 \subsection[BackSubst-Pats]{Patterns}
715 %*                                                                      *
716 %************************************************************************
717
718 \begin{code}
719 zonkPat :: ZonkEnv -> OutPat TcId -> TcM (ZonkEnv, OutPat Id)
720 -- Extend the environment as we go, because it's possible for one
721 -- pattern to bind something that is used in another (inside or
722 -- to the right)
723 zonkPat env pat = wrapLocSndM (zonk_pat env) pat
724
725 zonk_pat env (ParPat p)
726   = do  { (env', p') <- zonkPat env p
727         ; return (env', ParPat p') }
728
729 zonk_pat env (WildPat ty)
730   = do  { ty' <- zonkTcTypeToType env ty
731         ; return (env, WildPat ty') }
732
733 zonk_pat env (VarPat v)
734   = do  { v' <- zonkIdBndr env v
735         ; return (extendZonkEnv1 env v', VarPat v') }
736
737 zonk_pat env (VarPatOut v binds)
738   = do  { v' <- zonkIdBndr env v
739         ; (env', binds') <- zonkRecMonoBinds (extendZonkEnv1 env v') binds
740         ; returnM (env', VarPatOut v' binds') }
741
742 zonk_pat env (LazyPat pat)
743   = do  { (env', pat') <- zonkPat env pat
744         ; return (env',  LazyPat pat') }
745
746 zonk_pat env (BangPat pat)
747   = do  { (env', pat') <- zonkPat env pat
748         ; return (env',  BangPat pat') }
749
750 zonk_pat env (AsPat (L loc v) pat)
751   = do  { v' <- zonkIdBndr env v
752         ; (env', pat') <- zonkPat (extendZonkEnv1 env v') pat
753         ; return (env', AsPat (L loc v') pat') }
754
755 zonk_pat env (ViewPat expr pat ty)
756   = do  { expr' <- zonkLExpr env expr
757         ; (env', pat') <- zonkPat env pat
758         ; return (env', ViewPat expr' pat' ty) }
759
760 zonk_pat env (ListPat pats ty)
761   = do  { ty' <- zonkTcTypeToType env ty
762         ; (env', pats') <- zonkPats env pats
763         ; return (env', ListPat pats' ty') }
764
765 zonk_pat env (PArrPat pats ty)
766   = do  { ty' <- zonkTcTypeToType env ty
767         ; (env', pats') <- zonkPats env pats
768         ; return (env', PArrPat pats' ty') }
769
770 zonk_pat env (TuplePat pats boxed ty)
771   = do  { ty' <- zonkTcTypeToType env ty
772         ; (env', pats') <- zonkPats env pats
773         ; return (env', TuplePat pats' boxed ty') }
774
775 zonk_pat env p@(ConPatOut { pat_ty = ty, pat_dicts = dicts, pat_binds = binds, pat_args = args })
776   = ASSERT( all isImmutableTyVar (pat_tvs p) ) 
777     do  { new_ty <- zonkTcTypeToType env ty
778         ; new_dicts <- zonkDictBndrs env dicts
779         ; let env1 = extendZonkEnv env new_dicts
780         ; (env2, new_binds) <- zonkRecMonoBinds env1 binds
781         ; (env', new_args) <- zonkConStuff env2 args
782         ; returnM (env', p { pat_ty = new_ty, pat_dicts = new_dicts, 
783                              pat_binds = new_binds, pat_args = new_args }) }
784
785 zonk_pat env (LitPat lit) = return (env, LitPat lit)
786
787 zonk_pat env (SigPatOut pat ty)
788   = do  { ty' <- zonkTcTypeToType env ty
789         ; (env', pat') <- zonkPat env pat
790         ; return (env', SigPatOut pat' ty') }
791
792 zonk_pat env (NPat lit mb_neg eq_expr)
793   = do  { lit' <- zonkOverLit env lit
794         ; mb_neg' <- case mb_neg of
795                         Nothing  -> return Nothing
796                         Just neg -> do { neg' <- zonkExpr env neg
797                                        ; return (Just neg') }
798         ; eq_expr' <- zonkExpr env eq_expr
799         ; return (env, NPat lit' mb_neg' eq_expr') }
800
801 zonk_pat env (NPlusKPat (L loc n) lit e1 e2)
802   = do  { n' <- zonkIdBndr env n
803         ; lit' <- zonkOverLit env lit
804         ; e1' <- zonkExpr env e1
805         ; e2' <- zonkExpr env e2
806         ; return (extendZonkEnv1 env n', NPlusKPat (L loc n') lit' e1' e2') }
807
808 zonk_pat env (CoPat co_fn pat ty) 
809   = do { (env', co_fn') <- zonkCoFn env co_fn
810        ; (env'', pat') <- zonkPat env' (noLoc pat)
811        ; ty' <- zonkTcTypeToType env'' ty
812        ; return (env'', CoPat co_fn' (unLoc pat') ty') }
813
814 zonk_pat env pat = pprPanic "zonk_pat" (ppr pat)
815
816 ---------------------------
817 zonkConStuff env (PrefixCon pats)
818   = do  { (env', pats') <- zonkPats env pats
819         ; return (env', PrefixCon pats') }
820
821 zonkConStuff env (InfixCon p1 p2)
822   = do  { (env1, p1') <- zonkPat env  p1
823         ; (env', p2') <- zonkPat env1 p2
824         ; return (env', InfixCon p1' p2') }
825
826 zonkConStuff env (RecCon (HsRecFields rpats dd))
827   = do  { (env', pats') <- zonkPats env (map hsRecFieldArg rpats)
828         ; let rpats' = zipWith (\rp p' -> rp { hsRecFieldArg = p' }) rpats pats'
829         ; returnM (env', RecCon (HsRecFields rpats' dd)) }
830         -- Field selectors have declared types; hence no zonking
831
832 ---------------------------
833 zonkPats env []         = return (env, [])
834 zonkPats env (pat:pats) = do { (env1, pat') <- zonkPat env pat
835                      ; (env', pats') <- zonkPats env1 pats
836                      ; return (env', pat':pats') }
837 \end{code}
838
839 %************************************************************************
840 %*                                                                      *
841 \subsection[BackSubst-Foreign]{Foreign exports}
842 %*                                                                      *
843 %************************************************************************
844
845
846 \begin{code}
847 zonkForeignExports :: ZonkEnv -> [LForeignDecl TcId] -> TcM [LForeignDecl Id]
848 zonkForeignExports env ls = mappM (wrapLocM (zonkForeignExport env)) ls
849
850 zonkForeignExport :: ZonkEnv -> ForeignDecl TcId -> TcM (ForeignDecl Id)
851 zonkForeignExport env (ForeignExport i hs_ty spec) =
852    returnM (ForeignExport (fmap (zonkIdOcc env) i) undefined spec)
853 zonkForeignExport env for_imp 
854   = returnM for_imp     -- Foreign imports don't need zonking
855 \end{code}
856
857 \begin{code}
858 zonkRules :: ZonkEnv -> [LRuleDecl TcId] -> TcM [LRuleDecl Id]
859 zonkRules env rs = mappM (wrapLocM (zonkRule env)) rs
860
861 zonkRule :: ZonkEnv -> RuleDecl TcId -> TcM (RuleDecl Id)
862 zonkRule env (HsRule name act (vars{-::[RuleBndr TcId]-}) lhs fv_lhs rhs fv_rhs)
863   = mappM zonk_bndr vars                `thenM` \ new_bndrs ->
864     newMutVar emptyVarSet               `thenM` \ unbound_tv_set ->
865     let
866         env_rhs = extendZonkEnv env [id | b <- new_bndrs, let id = unLoc b, isId id]
867         -- Type variables don't need an envt
868         -- They are bound through the mutable mechanism
869
870         env_lhs = setZonkType env_rhs (zonkTypeCollecting unbound_tv_set)
871         -- We need to gather the type variables mentioned on the LHS so we can 
872         -- quantify over them.  Example:
873         --   data T a = C
874         -- 
875         --   foo :: T a -> Int
876         --   foo C = 1
877         --
878         --   {-# RULES "myrule"  foo C = 1 #-}
879         -- 
880         -- After type checking the LHS becomes (foo a (C a))
881         -- and we do not want to zap the unbound tyvar 'a' to (), because
882         -- that limits the applicability of the rule.  Instead, we
883         -- want to quantify over it!  
884         --
885         -- It's easiest to find the free tyvars here. Attempts to do so earlier
886         -- are tiresome, because (a) the data type is big and (b) finding the 
887         -- free type vars of an expression is necessarily monadic operation.
888         --      (consider /\a -> f @ b, where b is side-effected to a)
889     in
890     zonkLExpr env_lhs lhs               `thenM` \ new_lhs ->
891     zonkLExpr env_rhs rhs               `thenM` \ new_rhs ->
892
893     readMutVar unbound_tv_set           `thenM` \ unbound_tvs ->
894     let
895         final_bndrs :: [Located Var]
896         final_bndrs = map noLoc (varSetElems unbound_tvs) ++ new_bndrs
897     in
898     returnM (HsRule name act (map RuleBndr final_bndrs) new_lhs fv_lhs new_rhs fv_rhs)
899                 -- I hate this map RuleBndr stuff
900   where
901    zonk_bndr (RuleBndr v) 
902         | isId (unLoc v) = wrapLocM (zonkIdBndr env)   v
903         | otherwise      = ASSERT( isImmutableTyVar (unLoc v) )
904                            return v
905 \end{code}
906
907
908 %************************************************************************
909 %*                                                                      *
910 \subsection[BackSubst-Foreign]{Foreign exports}
911 %*                                                                      *
912 %************************************************************************
913
914 \begin{code}
915 zonkTcTypeToType :: ZonkEnv -> TcType -> TcM Type
916 zonkTcTypeToType (ZonkEnv zonk_ty _) ty = zonk_ty ty
917
918 zonkTcTypeToTypes :: ZonkEnv -> [TcType] -> TcM [Type]
919 zonkTcTypeToTypes env tys = mapM (zonkTcTypeToType env) tys
920
921 zonkTypeCollecting :: TcRef TyVarSet -> TcType -> TcM Type
922 -- This variant collects unbound type variables in a mutable variable
923 zonkTypeCollecting unbound_tv_set
924   = zonkType zonk_unbound_tyvar
925   where
926     zonk_unbound_tyvar tv 
927         = zonkQuantifiedTyVar tv                                `thenM` \ tv' ->
928           readMutVar unbound_tv_set                             `thenM` \ tv_set ->
929           writeMutVar unbound_tv_set (extendVarSet tv_set tv')  `thenM_`
930           return (mkTyVarTy tv')
931
932 zonkTypeZapping :: TcType -> TcM Type
933 -- This variant is used for everything except the LHS of rules
934 -- It zaps unbound type variables to (), or some other arbitrary type
935 zonkTypeZapping ty 
936   = zonkType zonk_unbound_tyvar ty 
937   where
938         -- Zonk a mutable but unbound type variable to an arbitrary type
939         -- We know it's unbound even though we don't carry an environment,
940         -- because at the binding site for a type variable we bind the
941         -- mutable tyvar to a fresh immutable one.  So the mutable store
942         -- plays the role of an environment.  If we come across a mutable
943         -- type variable that isn't so bound, it must be completely free.
944     zonk_unbound_tyvar tv = do { ty <- mkArbitraryType warn tv
945                                ; writeMetaTyVar tv ty
946                                ; return ty }
947         where
948             warn span msg = setSrcSpan span (addWarnTc msg)
949
950
951 {-      Note [Strangely-kinded void TyCons]
952         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
953         See Trac #959 for more examples
954
955 When the type checker finds a type variable with no binding, which
956 means it can be instantiated with an arbitrary type, it usually
957 instantiates it to Void.  Eg.
958
959         length []
960 ===>
961         length Void (Nil Void)
962
963 But in really obscure programs, the type variable might have a kind
964 other than *, so we need to invent a suitably-kinded type.
965
966 This commit uses
967         Void for kind *
968         List for kind *->*
969         Tuple for kind *->...*->*
970
971 which deals with most cases.  (Previously, it only dealt with
972 kind *.)   
973
974 In the other cases, it just makes up a TyCon with a suitable kind.  If
975 this gets into an interface file, anyone reading that file won't
976 understand it.  This is fixable (by making the client of the interface
977 file make up a TyCon too) but it is tiresome and never happens, so I
978 am leaving it.
979
980 Meanwhile I have now fixed GHC to emit a civilized warning.
981  -}
982
983 mkArbitraryType :: (SrcSpan -> SDoc -> TcRnIf g l a)    -- How to complain
984                 -> TcTyVar
985                 -> TcRnIf g l Type              -- Used by desugarer too
986 -- Make up an arbitrary type whose kind is the same as the tyvar.
987 -- We'll use this to instantiate the (unbound) tyvar.
988 --
989 -- Also used by the desugarer; hence the (tiresome) parameter
990 -- to use when generating a warning
991 mkArbitraryType warn tv 
992   | liftedTypeKind `isSubKind` kind             -- The vastly common case
993    = return anyPrimTy                   
994   | eqKind kind (tyConKind anyPrimTyCon1)       --  *->*
995   = return (mkTyConApp anyPrimTyCon1 [])        --     No tuples this size
996   | all isLiftedTypeKind args                   -- *-> ... ->*->*
997   , isLiftedTypeKind res                        --    Horrible hack to make less use 
998   = return (mkTyConApp tup_tc [])               --    of mkAnyPrimTyCon
999   | otherwise
1000   = do  { warn (getSrcSpan tv) msg
1001         ; return (mkTyConApp (mkAnyPrimTyCon (getUnique tv) kind) []) }
1002                 -- Same name as the tyvar, apart from making it start with a colon (sigh)
1003                 -- I dread to think what will happen if this gets out into an 
1004                 -- interface file.  Catastrophe likely.  Major sigh.
1005   where
1006     kind       = tyVarKind tv
1007     (args,res) = splitKindFunTys kind
1008     tup_tc     = tupleTyCon Boxed (length args)
1009                 
1010     msg = vcat [ hang (ptext SLIT("Inventing strangely-kinded Any TyCon"))
1011                     2 (ptext SLIT("of kind") <+> quotes (ppr kind))
1012                , nest 2 (ptext SLIT("from an instantiation of type variable") <+> quotes (ppr tv))
1013                , ptext SLIT("This warning can be suppressed by a type signature fixing") <+> quotes (ppr tv)
1014                , nest 2 (ptext SLIT("but is harmless without -O (and usually harmless anyway)."))
1015                , ptext SLIT("See http://hackage.haskell.org/trac/ghc/ticket/959 for details")  ]
1016 \end{code}