More hacking on monad-comp; now works
[ghc-hetmet.git] / compiler / typecheck / TcMatches.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 TcMatches: Typecheck some @Matches@
7
8 \begin{code}
9 module TcMatches ( tcMatchesFun, tcGRHSsPat, tcMatchesCase, tcMatchLambda,
10                    TcMatchCtxt(..), 
11                    tcStmts, tcStmtsAndThen, tcDoStmts, tcBody,
12                    tcDoStmt, tcMDoStmt, tcGuardStmt
13        ) where
14
15 import {-# SOURCE #-}   TcExpr( tcSyntaxOp, tcInferRhoNC, tcCheckId,
16                                 tcMonoExpr, tcMonoExprNC, tcPolyExpr )
17
18 import HsSyn
19 import BasicTypes
20 import TcRnMonad
21 import TcEnv
22 import TcPat
23 import TcMType
24 import TcType
25 import TcBinds
26 import TcUnify
27 import Name
28 import TysWiredIn
29 import Id
30 import TyCon
31 import TysPrim
32 import Coercion         ( mkSymCoI )
33 import Outputable
34 import Util
35 import SrcLoc
36 import FastString
37
38 -- Create chunkified tuple tybes for monad comprehensions
39 import MkCore
40
41 import Control.Monad
42
43 #include "HsVersions.h"
44 \end{code}
45
46 %************************************************************************
47 %*                                                                      *
48 \subsection{tcMatchesFun, tcMatchesCase}
49 %*                                                                      *
50 %************************************************************************
51
52 @tcMatchesFun@ typechecks a @[Match]@ list which occurs in a
53 @FunMonoBind@.  The second argument is the name of the function, which
54 is used in error messages.  It checks that all the equations have the
55 same number of arguments before using @tcMatches@ to do the work.
56
57 Note [Polymorphic expected type for tcMatchesFun]
58 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59 tcMatchesFun may be given a *sigma* (polymorphic) type
60 so it must be prepared to use tcGen to skolemise it.
61 See Note [sig_tau may be polymorphic] in TcPat.
62
63 \begin{code}
64 tcMatchesFun :: Name -> Bool
65              -> MatchGroup Name
66              -> TcSigmaType                        -- Expected type of function
67              -> TcM (HsWrapper, MatchGroup TcId)   -- Returns type of body
68 tcMatchesFun fun_name inf matches exp_ty
69   = do  {  -- Check that they all have the same no of arguments
70            -- Location is in the monad, set the caller so that 
71            -- any inter-equation error messages get some vaguely
72            -- sensible location.        Note: we have to do this odd
73            -- ann-grabbing, because we don't always have annotations in
74            -- hand when we call tcMatchesFun...
75           traceTc "tcMatchesFun" (ppr fun_name $$ ppr exp_ty)
76         ; checkArgs fun_name matches
77
78         ; (wrap_gen, (wrap_fun, group)) 
79             <- tcGen (FunSigCtxt fun_name) exp_ty $ \ _ exp_rho ->
80                   -- Note [Polymorphic expected type for tcMatchesFun]
81                matchFunTys herald arity exp_rho $ \ pat_tys rhs_ty -> 
82                tcMatches match_ctxt pat_tys rhs_ty matches 
83         ; return (wrap_gen <.> wrap_fun, group) }
84   where
85     arity = matchGroupArity matches
86     herald = ptext (sLit "The equation(s) for")
87              <+> quotes (ppr fun_name) <+> ptext (sLit "have")
88     match_ctxt = MC { mc_what = FunRhs fun_name inf, mc_body = tcBody }
89 \end{code}
90
91 @tcMatchesCase@ doesn't do the argument-count check because the
92 parser guarantees that each equation has exactly one argument.
93
94 \begin{code}
95 tcMatchesCase :: TcMatchCtxt            -- Case context
96               -> TcRhoType              -- Type of scrutinee
97               -> MatchGroup Name        -- The case alternatives
98               -> TcRhoType              -- Type of whole case expressions
99               -> TcM (MatchGroup TcId)  -- Translated alternatives
100
101 tcMatchesCase ctxt scrut_ty matches res_ty
102   | isEmptyMatchGroup matches   -- Allow empty case expressions
103   = return (MatchGroup [] (mkFunTys [scrut_ty] res_ty)) 
104
105   | otherwise
106   = tcMatches ctxt [scrut_ty] res_ty matches
107
108 tcMatchLambda :: MatchGroup Name -> TcRhoType -> TcM (HsWrapper, MatchGroup TcId)
109 tcMatchLambda match res_ty 
110   = matchFunTys herald n_pats res_ty  $ \ pat_tys rhs_ty ->
111     tcMatches match_ctxt pat_tys rhs_ty match
112   where
113     n_pats = matchGroupArity match
114     herald = sep [ ptext (sLit "The lambda expression")
115                          <+> quotes (pprSetDepth (PartWay 1) $ 
116                              pprMatches (LambdaExpr :: HsMatchContext Name) match),
117                         -- The pprSetDepth makes the abstraction print briefly
118                 ptext (sLit "has")]
119     match_ctxt = MC { mc_what = LambdaExpr,
120                       mc_body = tcBody }
121 \end{code}
122
123 @tcGRHSsPat@ typechecks @[GRHSs]@ that occur in a @PatMonoBind@.
124
125 \begin{code}
126 tcGRHSsPat :: GRHSs Name -> TcRhoType -> TcM (GRHSs TcId)
127 -- Used for pattern bindings
128 tcGRHSsPat grhss res_ty = tcGRHSs match_ctxt grhss res_ty
129   where
130     match_ctxt = MC { mc_what = PatBindRhs,
131                       mc_body = tcBody }
132 \end{code}
133
134
135 \begin{code}
136 matchFunTys
137   :: SDoc       -- See Note [Herald for matchExpecteFunTys] in TcUnify
138   -> Arity
139   -> TcRhoType
140   -> ([TcSigmaType] -> TcRhoType -> TcM a)
141   -> TcM (HsWrapper, a)
142
143 -- Written in CPS style for historical reasons; 
144 -- could probably be un-CPSd, like matchExpectedTyConApp
145
146 matchFunTys herald arity res_ty thing_inside
147   = do  { (coi, pat_tys, res_ty) <- matchExpectedFunTys herald arity res_ty
148         ; res <- thing_inside pat_tys res_ty
149         ; return (coiToHsWrapper (mkSymCoI coi), res) }
150 \end{code}
151
152 %************************************************************************
153 %*                                                                      *
154 \subsection{tcMatch}
155 %*                                                                      *
156 %************************************************************************
157
158 \begin{code}
159 tcMatches :: TcMatchCtxt
160           -> [TcSigmaType]      -- Expected pattern types
161           -> TcRhoType          -- Expected result-type of the Match.
162           -> MatchGroup Name
163           -> TcM (MatchGroup TcId)
164
165 data TcMatchCtxt        -- c.f. TcStmtCtxt, also in this module
166   = MC { mc_what :: HsMatchContext Name,        -- What kind of thing this is
167          mc_body :: LHsExpr Name                -- Type checker for a body of
168                                                 -- an alternative
169                  -> TcRhoType
170                  -> TcM (LHsExpr TcId) }        
171
172 tcMatches ctxt pat_tys rhs_ty (MatchGroup matches _)
173   = ASSERT( not (null matches) )        -- Ensure that rhs_ty is filled in
174     do  { matches' <- mapM (tcMatch ctxt pat_tys rhs_ty) matches
175         ; return (MatchGroup matches' (mkFunTys pat_tys rhs_ty)) }
176
177 -------------
178 tcMatch :: TcMatchCtxt
179         -> [TcSigmaType]        -- Expected pattern types
180         -> TcRhoType            -- Expected result-type of the Match.
181         -> LMatch Name
182         -> TcM (LMatch TcId)
183
184 tcMatch ctxt pat_tys rhs_ty match 
185   = wrapLocM (tc_match ctxt pat_tys rhs_ty) match
186   where
187     tc_match ctxt pat_tys rhs_ty match@(Match pats maybe_rhs_sig grhss)
188       = add_match_ctxt match $
189         do { (pats', grhss') <- tcPats (mc_what ctxt) pats pat_tys $
190                                 tc_grhss ctxt maybe_rhs_sig grhss rhs_ty
191            ; return (Match pats' Nothing grhss') }
192
193     tc_grhss ctxt Nothing grhss rhs_ty 
194       = tcGRHSs ctxt grhss rhs_ty       -- No result signature
195
196         -- Result type sigs are no longer supported
197     tc_grhss _ (Just {}) _ _
198       = panic "tc_ghrss"        -- Rejected by renamer
199
200         -- For (\x -> e), tcExpr has already said "In the expresssion \x->e"
201         -- so we don't want to add "In the lambda abstraction \x->e"
202     add_match_ctxt match thing_inside
203         = case mc_what ctxt of
204             LambdaExpr -> thing_inside
205             m_ctxt     -> addErrCtxt (pprMatchInCtxt m_ctxt match) thing_inside
206
207 -------------
208 tcGRHSs :: TcMatchCtxt -> GRHSs Name -> TcRhoType
209         -> TcM (GRHSs TcId)
210
211 -- Notice that we pass in the full res_ty, so that we get
212 -- good inference from simple things like
213 --      f = \(x::forall a.a->a) -> <stuff>
214 -- We used to force it to be a monotype when there was more than one guard
215 -- but we don't need to do that any more
216
217 tcGRHSs ctxt (GRHSs grhss binds) res_ty
218   = do  { (binds', grhss') <- tcLocalBinds binds $
219                               mapM (wrapLocM (tcGRHS ctxt res_ty)) grhss
220
221         ; return (GRHSs grhss' binds') }
222
223 -------------
224 tcGRHS :: TcMatchCtxt -> TcRhoType -> GRHS Name -> TcM (GRHS TcId)
225
226 tcGRHS ctxt res_ty (GRHS guards rhs)
227   = do  { (guards', rhs') <- tcStmtsAndThen stmt_ctxt tcGuardStmt guards res_ty $
228                              mc_body ctxt rhs
229         ; return (GRHS guards' rhs') }
230   where
231     stmt_ctxt  = PatGuard (mc_what ctxt)
232 \end{code}
233
234
235 %************************************************************************
236 %*                                                                      *
237 \subsection{@tcDoStmts@ typechecks a {\em list} of do statements}
238 %*                                                                      *
239 %************************************************************************
240
241 \begin{code}
242 tcDoStmts :: HsStmtContext Name 
243           -> [LStmt Name]
244           -> TcRhoType
245           -> TcM (HsExpr TcId)          -- Returns a HsDo
246 tcDoStmts ListComp stmts res_ty
247   = do  { (coi, elt_ty) <- matchExpectedListTy res_ty
248         ; stmts' <- tcStmts ListComp (tcLcStmt listTyCon) stmts elt_ty
249         ; return $ mkHsWrapCoI coi 
250                      (HsDo ListComp stmts' (mkListTy elt_ty)) }
251
252 tcDoStmts PArrComp stmts res_ty
253   = do  { (coi, elt_ty) <- matchExpectedPArrTy res_ty
254         ; stmts' <- tcStmts PArrComp (tcLcStmt parrTyCon) stmts elt_ty
255         ; return $ mkHsWrapCoI coi 
256                      (HsDo PArrComp stmts' (mkPArrTy elt_ty)) }
257
258 tcDoStmts DoExpr stmts res_ty
259   = do  { stmts' <- tcStmts DoExpr tcDoStmt stmts res_ty
260         ; return (HsDo DoExpr stmts' res_ty) }
261
262 tcDoStmts MDoExpr stmts res_ty
263   = do  { stmts' <- tcStmts MDoExpr tcDoStmt stmts res_ty
264         ; return (HsDo MDoExpr stmts' res_ty) }
265
266 tcDoStmts MonadComp stmts res_ty
267   = do  { stmts' <- tcStmts MonadComp tcMcStmt stmts res_ty 
268         ; return (HsDo MonadComp stmts' res_ty) }
269
270 tcDoStmts ctxt _ _ = pprPanic "tcDoStmts" (pprStmtContext ctxt)
271
272 tcBody :: LHsExpr Name -> TcRhoType -> TcM (LHsExpr TcId)
273 tcBody body res_ty
274   = do  { traceTc "tcBody" (ppr res_ty)
275         ; body' <- tcMonoExpr body res_ty
276         ; return body' 
277         } 
278 \end{code}
279
280
281 %************************************************************************
282 %*                                                                      *
283 \subsection{tcStmts}
284 %*                                                                      *
285 %************************************************************************
286
287 \begin{code}
288 type TcStmtChecker
289   =  forall thing. HsStmtContext Name
290                 -> Stmt Name
291                 -> TcRhoType                    -- Result type for comprehension
292                 -> (TcRhoType -> TcM thing)     -- Checker for what follows the stmt
293                 -> TcM (Stmt TcId, thing)
294
295 tcStmts :: HsStmtContext Name
296         -> TcStmtChecker        -- NB: higher-rank type
297         -> [LStmt Name]
298         -> TcRhoType
299         -> TcM [LStmt TcId]
300 tcStmts ctxt stmt_chk stmts res_ty
301   = do { (stmts', _) <- tcStmtsAndThen ctxt stmt_chk stmts res_ty $
302                         const (return ())
303        ; return stmts' }
304
305 tcStmtsAndThen :: HsStmtContext Name
306                -> TcStmtChecker -- NB: higher-rank type
307                -> [LStmt Name]
308                -> TcRhoType
309                -> (TcRhoType -> TcM thing)
310                -> TcM ([LStmt TcId], thing)
311
312 -- Note the higher-rank type.  stmt_chk is applied at different
313 -- types in the equations for tcStmts
314
315 tcStmtsAndThen _ _ [] res_ty thing_inside
316   = do  { thing <- thing_inside res_ty
317         ; return ([], thing) }
318
319 -- LetStmts are handled uniformly, regardless of context
320 tcStmtsAndThen ctxt stmt_chk (L loc (LetStmt binds) : stmts) res_ty thing_inside
321   = do  { (binds', (stmts',thing)) <- tcLocalBinds binds $
322                                       tcStmtsAndThen ctxt stmt_chk stmts res_ty thing_inside
323         ; return (L loc (LetStmt binds') : stmts', thing) }
324
325 -- For the vanilla case, handle the location-setting part
326 tcStmtsAndThen ctxt stmt_chk (L loc stmt : stmts) res_ty thing_inside
327   = do  { (stmt', (stmts', thing)) <- 
328                 setSrcSpan loc                              $
329                 addErrCtxt (pprStmtInCtxt ctxt stmt)        $
330                 stmt_chk ctxt stmt res_ty                   $ \ res_ty' ->
331                 popErrCtxt                                  $
332                 tcStmtsAndThen ctxt stmt_chk stmts res_ty'  $
333                 thing_inside
334         ; return (L loc stmt' : stmts', thing) }
335
336 --------------------------------
337 --      Pattern guards
338 tcGuardStmt :: TcStmtChecker
339 tcGuardStmt _ (ExprStmt guard _ _ _) res_ty thing_inside
340   = do  { guard' <- tcMonoExpr guard boolTy
341         ; thing  <- thing_inside res_ty
342         ; return (ExprStmt guard' noSyntaxExpr noSyntaxExpr boolTy, thing) }
343
344 tcGuardStmt ctxt (BindStmt pat rhs _ _) res_ty thing_inside
345   = do  { (rhs', rhs_ty) <- tcInferRhoNC rhs    -- Stmt has a context already
346         ; (pat', thing)  <- tcPat (StmtCtxt ctxt) pat rhs_ty $
347                             thing_inside res_ty
348         ; return (BindStmt pat' rhs' noSyntaxExpr noSyntaxExpr, thing) }
349
350 tcGuardStmt _ stmt _ _
351   = pprPanic "tcGuardStmt: unexpected Stmt" (ppr stmt)
352
353
354 --------------------------------
355 --      List comprehensions and PArrays
356
357 tcLcStmt :: TyCon       -- The list/Parray type constructor ([] or PArray)
358          -> TcStmtChecker
359
360 tcLcStmt _ _ (LastStmt body _) elt_ty thing_inside
361   = do { body' <- tcMonoExpr body elt_ty
362        ; thing <- thing_inside (panic "tcLcStmt: thing_inside")
363        ; return (LastStmt body' noSyntaxExpr, thing) }
364
365 -- A generator, pat <- rhs
366 tcLcStmt m_tc ctxt (BindStmt pat rhs _ _) elt_ty thing_inside
367  = do   { pat_ty <- newFlexiTyVarTy liftedTypeKind
368         ; rhs'   <- tcMonoExpr rhs (mkTyConApp m_tc [pat_ty])
369         ; (pat', thing)  <- tcPat (StmtCtxt ctxt) pat pat_ty $
370                             thing_inside elt_ty
371         ; return (BindStmt pat' rhs' noSyntaxExpr noSyntaxExpr, thing) }
372
373 -- A boolean guard
374 tcLcStmt _ _ (ExprStmt rhs _ _ _) elt_ty thing_inside
375   = do  { rhs'  <- tcMonoExpr rhs boolTy
376         ; thing <- thing_inside elt_ty
377         ; return (ExprStmt rhs' noSyntaxExpr noSyntaxExpr boolTy, thing) }
378
379 -- A parallel set of comprehensions
380 --      [ (g x, h x) | ... ; let g v = ...
381 --                   | ... ; let h v = ... ]
382 --
383 -- It's possible that g,h are overloaded, so we need to feed the LIE from the
384 -- (g x, h x) up through both lots of bindings (so we get the bindLocalMethods).
385 -- Similarly if we had an existential pattern match:
386 --
387 --      data T = forall a. Show a => C a
388 --
389 --      [ (show x, show y) | ... ; C x <- ...
390 --                         | ... ; C y <- ... ]
391 --
392 -- Then we need the LIE from (show x, show y) to be simplified against
393 -- the bindings for x and y.  
394 -- 
395 -- It's difficult to do this in parallel, so we rely on the renamer to 
396 -- ensure that g,h and x,y don't duplicate, and simply grow the environment.
397 -- So the binders of the first parallel group will be in scope in the second
398 -- group.  But that's fine; there's no shadowing to worry about.
399
400 tcLcStmt m_tc ctxt (ParStmt bndr_stmts_s _ _ _) elt_ty thing_inside
401   = do  { (pairs', thing) <- loop bndr_stmts_s
402         ; return (ParStmt pairs' noSyntaxExpr noSyntaxExpr noSyntaxExpr, thing) }
403   where
404     -- loop :: [([LStmt Name], [Name])] -> TcM ([([LStmt TcId], [TcId])], thing)
405     loop [] = do { thing <- thing_inside elt_ty
406                  ; return ([], thing) }         -- matching in the branches
407
408     loop ((stmts, names) : pairs)
409       = do { (stmts', (ids, pairs', thing))
410                 <- tcStmtsAndThen ctxt (tcLcStmt m_tc) stmts elt_ty $ \ _elt_ty' ->
411                    do { ids <- tcLookupLocalIds names
412                       ; (pairs', thing) <- loop pairs
413                       ; return (ids, pairs', thing) }
414            ; return ( (stmts', ids) : pairs', thing ) }
415
416 tcLcStmt m_tc ctxt (TransformStmt stmts binders usingExpr maybeByExpr _ _) elt_ty thing_inside = do
417     (stmts', (binders', usingExpr', maybeByExpr', thing)) <- 
418         tcStmtsAndThen (TransformStmtCtxt ctxt) (tcLcStmt m_tc) stmts elt_ty $ \elt_ty' -> do
419             let alphaListTy = mkTyConApp m_tc [alphaTy]
420                     
421             (usingExpr', maybeByExpr') <- 
422                 case maybeByExpr of
423                     Nothing -> do
424                         -- We must validate that usingExpr :: forall a. [a] -> [a]
425                         let using_ty = mkForAllTy alphaTyVar (alphaListTy `mkFunTy` alphaListTy)
426                         usingExpr' <- tcPolyExpr usingExpr using_ty
427                         return (usingExpr', Nothing)
428                     Just byExpr -> do
429                         -- We must infer a type such that e :: t and then check that 
430                         -- usingExpr :: forall a. (a -> t) -> [a] -> [a]
431                         (byExpr', tTy) <- tcInferRhoNC byExpr
432                         let using_ty = mkForAllTy alphaTyVar $ 
433                                        (alphaTy `mkFunTy` tTy)
434                                        `mkFunTy` alphaListTy `mkFunTy` alphaListTy
435                         usingExpr' <- tcPolyExpr usingExpr using_ty
436                         return (usingExpr', Just byExpr')
437             
438             binders' <- tcLookupLocalIds binders
439             thing <- thing_inside elt_ty'
440             
441             return (binders', usingExpr', maybeByExpr', thing)
442
443     return (TransformStmt stmts' binders' usingExpr' maybeByExpr' noSyntaxExpr noSyntaxExpr, thing)
444
445 tcLcStmt m_tc ctxt (GroupStmt { grpS_stmts = stmts, grpS_bndrs =  bindersMap
446                               , grpS_by = by, grpS_using = using
447                               , grpS_explicit = explicit }) elt_ty thing_inside
448   = do { let (bndr_names, list_bndr_names) = unzip bindersMap
449
450        ; (stmts', (bndr_ids, by', using_ty, elt_ty')) <-
451             tcStmtsAndThen (TransformStmtCtxt ctxt) (tcLcStmt m_tc) stmts elt_ty $ \elt_ty' -> do
452                 (by', using_ty) <- 
453                    case by of
454                      Nothing   -> -- check that using :: forall a. [a] -> [[a]]
455                                   return (Nothing, mkForAllTy alphaTyVar $
456                                                    alphaListTy `mkFunTy` alphaListListTy)
457                                         
458                      Just by_e -> -- check that using :: forall a. (a -> t) -> [a] -> [[a]]
459                                   -- where by :: t
460                                   do { (by_e', t_ty) <- tcInferRhoNC by_e
461                                      ; return (Just by_e', mkForAllTy alphaTyVar $
462                                                            (alphaTy `mkFunTy` t_ty) 
463                                                            `mkFunTy` alphaListTy 
464                                                            `mkFunTy` alphaListListTy) }
465                 -- Find the Ids (and hence types) of all old binders
466                 bndr_ids <- tcLookupLocalIds bndr_names
467                 
468                 return (bndr_ids, by', using_ty, elt_ty')
469         
470                 -- Ensure that every old binder of type b is linked up with
471                 -- its new binder which should have type [b]
472        ; let list_bndr_ids = zipWith mk_list_bndr list_bndr_names bndr_ids
473              bindersMap' = bndr_ids `zip` list_bndr_ids
474              -- See Note [GroupStmt binder map] in HsExpr
475             
476        ; using' <- tcPolyExpr using using_ty
477
478              -- Type check the thing in the environment with 
479              -- these new binders and return the result
480        ; thing <- tcExtendIdEnv list_bndr_ids (thing_inside elt_ty')
481        ; return (emptyGroupStmt { grpS_stmts = stmts', grpS_bndrs = bindersMap'
482                                 , grpS_by = by', grpS_using = using'
483                                 , grpS_explicit = explicit }, thing) }
484   where
485     alphaListTy = mkTyConApp m_tc [alphaTy]
486     alphaListListTy = mkTyConApp m_tc [alphaListTy]
487             
488     mk_list_bndr :: Name -> TcId -> TcId
489     mk_list_bndr list_bndr_name bndr_id 
490       = mkLocalId list_bndr_name (mkTyConApp m_tc [idType bndr_id])
491     
492 tcLcStmt _ _ stmt _ _
493   = pprPanic "tcLcStmt: unexpected Stmt" (ppr stmt)
494         
495
496 --------------------------------
497 --      Monad comprehensions
498
499 tcMcStmt :: TcStmtChecker
500
501 tcMcStmt _ (LastStmt body return_op) res_ty thing_inside
502   = do  { a_ty       <- newFlexiTyVarTy liftedTypeKind
503         ; return_op' <- tcSyntaxOp MCompOrigin return_op
504                                    (a_ty `mkFunTy` res_ty)
505         ; body'      <- tcMonoExpr body a_ty
506         ; thing      <- thing_inside (panic "tcMcStmt: thing_inside")
507         ; return (LastStmt body' return_op', thing) } 
508
509 -- Generators for monad comprehensions ( pat <- rhs )
510 --
511 --   [ body | q <- gen ]  ->  gen :: m a
512 --                            q   ::   a
513 --
514
515 tcMcStmt ctxt (BindStmt pat rhs bind_op fail_op) res_ty thing_inside
516  = do   { rhs_ty     <- newFlexiTyVarTy liftedTypeKind
517         ; pat_ty     <- newFlexiTyVarTy liftedTypeKind
518         ; new_res_ty <- newFlexiTyVarTy liftedTypeKind
519
520            -- (>>=) :: rhs_ty -> (pat_ty -> new_res_ty) -> res_ty
521         ; bind_op'   <- tcSyntaxOp MCompOrigin bind_op 
522                              (mkFunTys [rhs_ty, mkFunTy pat_ty new_res_ty] res_ty)
523
524            -- If (but only if) the pattern can fail, typecheck the 'fail' operator
525         ; fail_op' <- if isIrrefutableHsPat pat 
526                       then return noSyntaxExpr
527                       else tcSyntaxOp MCompOrigin fail_op (mkFunTy stringTy new_res_ty)
528
529         ; rhs' <- tcMonoExprNC rhs rhs_ty
530         ; (pat', thing) <- tcPat (StmtCtxt ctxt) pat pat_ty $
531                            thing_inside new_res_ty
532
533         ; return (BindStmt pat' rhs' bind_op' fail_op', thing) }
534
535 -- Boolean expressions.
536 --
537 --   [ body | stmts, expr ]  ->  expr :: m Bool
538 --
539 tcMcStmt _ (ExprStmt rhs then_op guard_op _) res_ty thing_inside
540   = do  { -- Deal with rebindable syntax:
541           --    guard_op :: test_ty -> rhs_ty
542           --    then_op  :: rhs_ty -> new_res_ty -> res_ty
543           -- Where test_ty is, for example, Bool
544           test_ty    <- newFlexiTyVarTy liftedTypeKind
545         ; rhs_ty     <- newFlexiTyVarTy liftedTypeKind
546         ; new_res_ty <- newFlexiTyVarTy liftedTypeKind
547         ; rhs'       <- tcMonoExpr rhs test_ty
548         ; guard_op'  <- tcSyntaxOp MCompOrigin guard_op
549                                    (mkFunTy test_ty rhs_ty)
550         ; then_op'   <- tcSyntaxOp MCompOrigin then_op
551                                    (mkFunTys [rhs_ty, new_res_ty] res_ty)
552         ; thing      <- thing_inside new_res_ty
553         ; return (ExprStmt rhs' then_op' guard_op' rhs_ty, thing) }
554
555 -- Transform statements.
556 --
557 --   [ body | stmts, then f ]       ->  f :: forall a. m a -> m a
558 --   [ body | stmts, then f by e ]  ->  f :: forall a. (a -> t) -> m a -> m a
559 --
560 tcMcStmt ctxt (TransformStmt stmts binders usingExpr maybeByExpr return_op bind_op) res_ty thing_inside
561   = do  {
562         -- We don't know the types of binders yet, so we use this dummy and
563         -- later unify this type with the `m_bndr_ty`
564           ty_dummy <- newFlexiTyVarTy liftedTypeKind
565
566         ; (stmts', (binders', usingExpr', maybeByExpr', return_op', bind_op', thing)) <- 
567               tcStmtsAndThen (TransformStmtCtxt ctxt) tcMcStmt stmts ty_dummy $ \res_ty' -> do
568                   { (_, (m_ty, _)) <- matchExpectedAppTy res_ty'
569                   ; (usingExpr', maybeByExpr') <- 
570                         case maybeByExpr of
571                             Nothing -> do
572                                 -- We must validate that usingExpr :: forall a. m a -> m a
573                                 let using_ty = mkForAllTy alphaTyVar $
574                                                (m_ty `mkAppTy` alphaTy)
575                                                `mkFunTy`
576                                                (m_ty `mkAppTy` alphaTy)
577                                 usingExpr' <- tcPolyExpr usingExpr using_ty
578                                 return (usingExpr', Nothing)
579                             Just byExpr -> do
580                                 -- We must infer a type such that e :: t and then check that 
581                                 -- usingExpr :: forall a. (a -> t) -> m a -> m a
582                                 (byExpr', tTy) <- tcInferRhoNC byExpr
583                                 let using_ty = mkForAllTy alphaTyVar $ 
584                                                (alphaTy `mkFunTy` tTy)
585                                                `mkFunTy`
586                                                (m_ty `mkAppTy` alphaTy)
587                                                `mkFunTy`
588                                                (m_ty `mkAppTy` alphaTy)
589                                 usingExpr' <- tcPolyExpr usingExpr using_ty
590                                 return (usingExpr', Just byExpr')
591                     
592                   ; bndr_ids <- tcLookupLocalIds binders
593
594                   -- `return` and `>>=` are used to pass around/modify our
595                   -- binders, so we know their types:
596                   --
597                   --   return :: (a,b,c,..) -> m (a,b,c,..)
598                   --   (>>=)  :: m (a,b,c,..)
599                   --          -> ( (a,b,c,..) -> m (a,b,c,..) )
600                   --          -> m (a,b,c,..)
601                   --
602                   ; let bndr_ty   = mkBigCoreVarTupTy bndr_ids
603                         m_bndr_ty = m_ty `mkAppTy` bndr_ty
604
605                   ; return_op' <- tcSyntaxOp MCompOrigin return_op
606                                       (bndr_ty `mkFunTy` m_bndr_ty)
607
608                   ; bind_op'   <- tcSyntaxOp MCompOrigin bind_op $
609                                       m_bndr_ty `mkFunTy` (bndr_ty `mkFunTy` res_ty)
610                                                 `mkFunTy` res_ty
611
612                   -- Unify types of the inner comprehension and the binders type
613                   ; _ <- unifyType res_ty' m_bndr_ty
614
615                   -- Typecheck the `thing` with out old type (which is the type
616                   -- of the final result of our comprehension)
617                   ; thing <- thing_inside res_ty
618
619                   ; return (bndr_ids, usingExpr', maybeByExpr', return_op', bind_op', thing) }
620
621         ; return (TransformStmt stmts' binders' usingExpr' maybeByExpr' return_op' bind_op', thing) }
622
623 -- Grouping statements
624 --
625 --   [ body | stmts, then group by e ]
626 --     ->  e :: t
627 --   [ body | stmts, then group by e using f ]
628 --     ->  e :: t
629 --         f :: forall a. (a -> t) -> m a -> m (m a)
630 --   [ body | stmts, then group using f ]
631 --     ->  f :: forall a. m a -> m (m a)
632 --
633 tcMcStmt ctxt (GroupStmt { grpS_stmts = stmts, grpS_bndrs = bindersMap
634                          , grpS_by = by, grpS_using = using, grpS_explicit = explicit
635                          , grpS_ret = return_op, grpS_bind = bind_op 
636                          , grpS_fmap = fmap_op }) res_ty thing_inside
637   = do { let star_star_kind = liftedTypeKind `mkArrowKind` liftedTypeKind
638        ; m1_ty      <- newFlexiTyVarTy star_star_kind
639        ; m2_ty      <- newFlexiTyVarTy star_star_kind
640        ; n_ty       <- newFlexiTyVarTy star_star_kind
641        ; tup_ty_var <- newFlexiTyVarTy liftedTypeKind
642        ; new_res_ty <- newFlexiTyVarTy liftedTypeKind
643        ; let (bndr_names, n_bndr_names) = unzip bindersMap
644              m1_tup_ty = m1_ty `mkAppTy` tup_ty_var
645                              
646              -- 'stmts' returns a result of type (m1_ty tuple_ty),
647              -- typically something like [(Int,Bool,Int)]
648              -- We don't know what tuple_ty is yet, so we use a variable
649        ; (stmts', (bndr_ids, by_e_ty, return_op')) <-
650             tcStmtsAndThen (TransformStmtCtxt ctxt) tcMcStmt stmts m1_tup_ty $ \res_ty' -> do
651                 { by_e_ty <- case by of
652                                Nothing -> return Nothing
653                                Just e  -> do { e_ty <- tcInferRhoNC e; return (Just e_ty) }
654
655                 -- Find the Ids (and hence types) of all old binders
656                 ; bndr_ids <- tcLookupLocalIds bndr_names
657
658                 -- 'return' is only used for the binders, so we know its type.
659                 --
660                 --   return :: (a,b,c,..) -> m (a,b,c,..)
661                 ; return_op' <- tcSyntaxOp MCompOrigin return_op $ 
662                                 (mkBigCoreVarTupTy bndr_ids) `mkFunTy` res_ty'
663
664                 ; return (bndr_ids, by_e_ty, return_op') }
665
666
667
668        ; let tup_ty       = mkBigCoreVarTupTy bndr_ids  -- (a,b,c)
669              using_arg_ty = m1_ty `mkAppTy` tup_ty      -- m1 (a,b,c)
670              n_tup_ty     = n_ty  `mkAppTy` tup_ty      -- n (a,b,c)
671              using_res_ty = m2_ty `mkAppTy` n_tup_ty    -- m2 (n (a,b,c))
672              using_fun_ty = using_arg_ty `mkFunTy` using_arg_ty
673               
674                 -- (>>=) :: m2 (n (a,b,c)) -> ( n (a,b,c) -> new_res_ty ) -> res_ty
675                 -- using :: ((a,b,c)->t) -> m1 (a,b,c) -> m2 (n (a,b,c))
676
677        --------------- Typecheck the 'bind' function -------------
678        ; bind_op' <- tcSyntaxOp MCompOrigin bind_op $
679                                 using_res_ty `mkFunTy` (n_tup_ty `mkFunTy` new_res_ty)
680                                              `mkFunTy` res_ty
681
682        --------------- Typecheck the 'using' function -------------
683        ; let poly_fun_ty = (m1_ty `mkAppTy` alphaTy) `mkFunTy` 
684                                      (m2_ty `mkAppTy` (n_ty `mkAppTy` alphaTy))
685              using_poly_ty = case by_e_ty of
686                Nothing       -> mkForAllTy alphaTyVar poly_fun_ty
687                                 -- using :: forall a. m1 a -> m2 (n a)
688
689                Just (_,t_ty) -> mkForAllTy alphaTyVar $
690                                 (alphaTy `mkFunTy` t_ty) `mkFunTy` poly_fun_ty
691                                 -- using :: forall a. (a->t) -> m1 a -> m2 (n a)
692                                 -- where by :: t
693
694        ; using' <- tcPolyExpr using using_poly_ty
695        ; coi <- unifyType (applyTy using_poly_ty tup_ty)
696                           (case by_e_ty of
697                              Nothing       -> using_fun_ty
698                              Just (_,t_ty) -> (tup_ty `mkFunTy` t_ty) `mkFunTy` using_fun_ty)
699        ; let final_using = fmap (mkHsWrapCoI coi . HsWrap (WpTyApp tup_ty)) using' 
700
701        --------------- Typecheck the 'fmap' function -------------
702        ; fmap_op' <- fmap unLoc . tcPolyExpr (noLoc fmap_op) $
703                          mkForAllTy alphaTyVar $ mkForAllTy betaTyVar $
704                          (alphaTy `mkFunTy` betaTy)
705                          `mkFunTy` (n_ty `mkAppTy` alphaTy)
706                          `mkFunTy` (n_ty `mkAppTy` betaTy)
707
708        ; let mk_n_bndr :: Name -> TcId -> TcId
709              mk_n_bndr n_bndr_name bndr_id 
710                 = mkLocalId n_bndr_name (n_ty `mkAppTy` idType bndr_id)
711
712              -- Ensure that every old binder of type `b` is linked up with its
713              -- new binder which should have type `n b`
714              -- See Note [GroupStmt binder map] in HsExpr
715              n_bndr_ids = zipWith mk_n_bndr n_bndr_names bndr_ids
716              bindersMap' = bndr_ids `zip` n_bndr_ids
717
718        -- Type check the thing in the environment with these new binders and
719        -- return the result
720        ; thing <- tcExtendIdEnv n_bndr_ids (thing_inside res_ty)
721
722        ; return (GroupStmt { grpS_stmts = stmts', grpS_bndrs = bindersMap' 
723                            , grpS_by = fmap fst by_e_ty, grpS_using = final_using 
724                            , grpS_ret = return_op', grpS_bind = bind_op'
725                            , grpS_fmap = fmap_op', grpS_explicit = explicit }, thing) }
726
727 -- Typecheck `ParStmt`. See `tcLcStmt` for more informations about typechecking
728 -- of `ParStmt`s.
729 --
730 -- Note: The `mzip` function will get typechecked via:
731 --
732 --   ParStmt [st1::t1, st2::t2, st3::t3]
733 --   
734 --   mzip :: m st1
735 --        -> (m st2 -> m st3 -> m (st2, st3))   -- recursive call
736 --        -> m (st1, (st2, st3))
737 --
738 tcMcStmt ctxt (ParStmt bndr_stmts_s mzip_op bind_op return_op) res_ty thing_inside
739   = do  { (_,(m_ty,_)) <- matchExpectedAppTy res_ty
740         -- ToDo: what if the coercion isn't the identity?
741
742         ; (pairs', thing) <- loop m_ty bndr_stmts_s
743
744         ; let mzip_ty  = mkForAllTys [alphaTyVar, betaTyVar] $
745                          (m_ty `mkAppTy` alphaTy)
746                          `mkFunTy`
747                          (m_ty `mkAppTy` betaTy)
748                          `mkFunTy`
749                          (m_ty `mkAppTy` mkBoxedTupleTy [alphaTy, betaTy])
750         ; mzip_op' <- unLoc `fmap` tcPolyExpr (noLoc mzip_op) mzip_ty
751
752         -- Typecheck bind:
753         ; let tys      = map (mkBigCoreVarTupTy . snd) pairs'
754               tuple_ty = mk_tuple_ty tys
755
756         ; bind_op' <- tcSyntaxOp MCompOrigin bind_op $
757                          (m_ty `mkAppTy` tuple_ty)
758                          `mkFunTy`
759                          (tuple_ty `mkFunTy` res_ty)
760                          `mkFunTy`
761                          res_ty
762
763         ; return_op' <- fmap unLoc . tcPolyExpr (noLoc return_op) $
764                             mkForAllTy alphaTyVar $
765                             alphaTy `mkFunTy` (m_ty `mkAppTy` alphaTy)
766
767         ; return (ParStmt pairs' mzip_op' bind_op' return_op', thing) }
768
769  where mk_tuple_ty tys = foldr1 (\tn tm -> mkBoxedTupleTy [tn, tm]) tys
770
771        -- loop :: Type                                  -- m_ty
772        --      -> [([LStmt Name], [Name])]
773        --      -> TcM ([([LStmt TcId], [TcId])], thing)
774        loop _ [] = do { thing <- thing_inside res_ty
775                       ; return ([], thing) }           -- matching in the branches
776
777        loop m_ty ((stmts, names) : pairs)
778          = do { -- type dummy since we don't know all binder types yet
779                 ty_dummy <- newFlexiTyVarTy liftedTypeKind
780               ; (stmts', (ids, pairs', thing))
781                    <- tcStmtsAndThen ctxt tcMcStmt stmts ty_dummy $ \res_ty' ->
782                       do { ids <- tcLookupLocalIds names
783                          ; _ <- unifyType res_ty' (m_ty `mkAppTy` mkBigCoreVarTupTy ids)
784                          ; (pairs', thing) <- loop m_ty pairs
785                          ; return (ids, pairs', thing) }
786               ; return ( (stmts', ids) : pairs', thing ) }
787
788 tcMcStmt _ stmt _ _
789   = pprPanic "tcMcStmt: unexpected Stmt" (ppr stmt)
790
791 --------------------------------
792 --      Do-notation
793 -- The main excitement here is dealing with rebindable syntax
794
795 tcDoStmt :: TcStmtChecker
796
797 tcDoStmt _ (LastStmt body _) res_ty thing_inside
798   = do { body' <- tcMonoExprNC body res_ty
799        ; thing <- thing_inside (panic "tcDoStmt: thing_inside")
800        ; return (LastStmt body' noSyntaxExpr, thing) }
801
802 tcDoStmt ctxt (BindStmt pat rhs bind_op fail_op) res_ty thing_inside
803   = do  {       -- Deal with rebindable syntax:
804                 --       (>>=) :: rhs_ty -> (pat_ty -> new_res_ty) -> res_ty
805                 -- This level of generality is needed for using do-notation
806                 -- in full generality; see Trac #1537
807
808                 -- I'd like to put this *after* the tcSyntaxOp 
809                 -- (see Note [Treat rebindable syntax first], but that breaks 
810                 -- the rigidity info for GADTs.  When we move to the new story
811                 -- for GADTs, we can move this after tcSyntaxOp
812           rhs_ty     <- newFlexiTyVarTy liftedTypeKind
813         ; pat_ty     <- newFlexiTyVarTy liftedTypeKind
814         ; new_res_ty <- newFlexiTyVarTy liftedTypeKind
815         ; bind_op'   <- tcSyntaxOp DoOrigin bind_op 
816                              (mkFunTys [rhs_ty, mkFunTy pat_ty new_res_ty] res_ty)
817
818                 -- If (but only if) the pattern can fail, 
819                 -- typecheck the 'fail' operator
820         ; fail_op' <- if isIrrefutableHsPat pat 
821                       then return noSyntaxExpr
822                       else tcSyntaxOp DoOrigin fail_op (mkFunTy stringTy new_res_ty)
823
824         ; rhs' <- tcMonoExprNC rhs rhs_ty
825         ; (pat', thing) <- tcPat (StmtCtxt ctxt) pat pat_ty $
826                            thing_inside new_res_ty
827
828         ; return (BindStmt pat' rhs' bind_op' fail_op', thing) }
829
830
831 tcDoStmt _ (ExprStmt rhs then_op _ _) res_ty thing_inside
832   = do  {       -- Deal with rebindable syntax; 
833                 --   (>>) :: rhs_ty -> new_res_ty -> res_ty
834                 -- See also Note [Treat rebindable syntax first]
835           rhs_ty     <- newFlexiTyVarTy liftedTypeKind
836         ; new_res_ty <- newFlexiTyVarTy liftedTypeKind
837         ; then_op' <- tcSyntaxOp DoOrigin then_op 
838                            (mkFunTys [rhs_ty, new_res_ty] res_ty)
839
840         ; rhs' <- tcMonoExprNC rhs rhs_ty
841         ; thing <- thing_inside new_res_ty
842         ; return (ExprStmt rhs' then_op' noSyntaxExpr rhs_ty, thing) }
843
844 tcDoStmt ctxt (RecStmt { recS_stmts = stmts, recS_later_ids = later_names
845                        , recS_rec_ids = rec_names, recS_ret_fn = ret_op
846                        , recS_mfix_fn = mfix_op, recS_bind_fn = bind_op }) 
847          res_ty thing_inside
848   = do  { let tup_names = rec_names ++ filterOut (`elem` rec_names) later_names
849         ; tup_elt_tys <- newFlexiTyVarTys (length tup_names) liftedTypeKind
850         ; let tup_ids = zipWith mkLocalId tup_names tup_elt_tys
851               tup_ty  = mkBoxedTupleTy tup_elt_tys
852
853         ; tcExtendIdEnv tup_ids $ do
854         { stmts_ty <- newFlexiTyVarTy liftedTypeKind
855         ; (stmts', (ret_op', tup_rets))
856                 <- tcStmtsAndThen ctxt tcDoStmt stmts stmts_ty   $ \ inner_res_ty ->
857                    do { tup_rets <- zipWithM tcCheckId tup_names tup_elt_tys
858                              -- Unify the types of the "final" Ids (which may 
859                              -- be polymorphic) with those of "knot-tied" Ids
860                       ; ret_op' <- tcSyntaxOp DoOrigin ret_op (mkFunTy tup_ty inner_res_ty)
861                       ; return (ret_op', tup_rets) }
862
863         ; mfix_res_ty <- newFlexiTyVarTy liftedTypeKind
864         ; mfix_op' <- tcSyntaxOp DoOrigin mfix_op
865                                  (mkFunTy (mkFunTy tup_ty stmts_ty) mfix_res_ty)
866
867         ; new_res_ty <- newFlexiTyVarTy liftedTypeKind
868         ; bind_op' <- tcSyntaxOp DoOrigin bind_op 
869                                  (mkFunTys [mfix_res_ty, mkFunTy tup_ty new_res_ty] res_ty)
870
871         ; thing <- thing_inside new_res_ty
872 --         ; lie_binds <- bindLocalMethods lie tup_ids
873   
874         ; let rec_ids = takeList rec_names tup_ids
875         ; later_ids <- tcLookupLocalIds later_names
876         ; traceTc "tcdo" $ vcat [ppr rec_ids <+> ppr (map idType rec_ids),
877                                  ppr later_ids <+> ppr (map idType later_ids)]
878         ; return (RecStmt { recS_stmts = stmts', recS_later_ids = later_ids
879                           , recS_rec_ids = rec_ids, recS_ret_fn = ret_op' 
880                           , recS_mfix_fn = mfix_op', recS_bind_fn = bind_op'
881                           , recS_rec_rets = tup_rets, recS_ret_ty = stmts_ty }, thing)
882         }}
883
884 tcDoStmt _ stmt _ _
885   = pprPanic "tcDoStmt: unexpected Stmt" (ppr stmt)
886 \end{code}
887
888 Note [Treat rebindable syntax first]
889 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
890 When typechecking
891         do { bar; ... } :: IO ()
892 we want to typecheck 'bar' in the knowledge that it should be an IO thing,
893 pushing info from the context into the RHS.  To do this, we check the
894 rebindable syntax first, and push that information into (tcMonoExprNC rhs).
895 Otherwise the error shows up when cheking the rebindable syntax, and
896 the expected/inferred stuff is back to front (see Trac #3613).
897
898 \begin{code}
899 --------------------------------
900 --      Mdo-notation
901 -- The distinctive features here are
902 --      (a) RecStmts, and
903 --      (b) no rebindable syntax
904
905 tcMDoStmt :: (LHsExpr Name -> TcM (LHsExpr TcId, TcType))       -- RHS inference
906           -> TcStmtChecker
907 -- Used only by TcArrows... should be gotten rid of
908 tcMDoStmt tc_rhs ctxt (BindStmt pat rhs _ _) res_ty thing_inside
909   = do  { (rhs', pat_ty) <- tc_rhs rhs
910         ; (pat', thing)  <- tcPat (StmtCtxt ctxt) pat pat_ty $
911                             thing_inside res_ty
912         ; return (BindStmt pat' rhs' noSyntaxExpr noSyntaxExpr, thing) }
913
914 tcMDoStmt tc_rhs _ (ExprStmt rhs _ _ _) res_ty thing_inside
915   = do  { (rhs', elt_ty) <- tc_rhs rhs
916         ; thing          <- thing_inside res_ty
917         ; return (ExprStmt rhs' noSyntaxExpr noSyntaxExpr elt_ty, thing) }
918
919 tcMDoStmt tc_rhs ctxt (RecStmt { recS_stmts = stmts, recS_later_ids = laterNames
920                                , recS_rec_ids = recNames }) res_ty thing_inside
921   = do  { rec_tys <- newFlexiTyVarTys (length recNames) liftedTypeKind
922         ; let rec_ids = zipWith mkLocalId recNames rec_tys
923         ; tcExtendIdEnv rec_ids $ do
924         { (stmts', (later_ids, rec_rets))
925                 <- tcStmtsAndThen ctxt (tcMDoStmt tc_rhs) stmts res_ty  $ \ _res_ty' ->
926                         -- ToDo: res_ty not really right
927                    do { rec_rets <- zipWithM tcCheckId recNames rec_tys
928                       ; later_ids <- tcLookupLocalIds laterNames
929                       ; return (later_ids, rec_rets) }
930
931         ; thing <- tcExtendIdEnv later_ids (thing_inside res_ty)
932                 -- NB:  The rec_ids for the recursive things 
933                 --      already scope over this part. This binding may shadow
934                 --      some of them with polymorphic things with the same Name
935                 --      (see note [RecStmt] in HsExpr)
936
937         ; return (emptyRecStmt { recS_stmts = stmts', recS_later_ids = later_ids
938                                , recS_rec_ids = rec_ids, recS_rec_rets = rec_rets
939                                , recS_ret_ty = res_ty }, thing)
940         }}
941
942 tcMDoStmt _ _ stmt _ _
943   = pprPanic "tcMDoStmt: unexpected Stmt" (ppr stmt)
944 \end{code}
945
946
947 %************************************************************************
948 %*                                                                      *
949 \subsection{Errors and contexts}
950 %*                                                                      *
951 %************************************************************************
952
953 @sameNoOfArgs@ takes a @[RenamedMatch]@ and decides whether the same
954 number of args are used in each equation.
955
956 \begin{code}
957 checkArgs :: Name -> MatchGroup Name -> TcM ()
958 checkArgs fun (MatchGroup (match1:matches) _)
959     | null bad_matches = return ()
960     | otherwise
961     = failWithTc (vcat [ptext (sLit "Equations for") <+> quotes (ppr fun) <+> 
962                           ptext (sLit "have different numbers of arguments"),
963                         nest 2 (ppr (getLoc match1)),
964                         nest 2 (ppr (getLoc (head bad_matches)))])
965   where
966     n_args1 = args_in_match match1
967     bad_matches = [m | m <- matches, args_in_match m /= n_args1]
968
969     args_in_match :: LMatch Name -> Int
970     args_in_match (L _ (Match pats _ _)) = length pats
971 checkArgs fun _ = pprPanic "TcPat.checkArgs" (ppr fun) -- Matches always non-empty
972 \end{code}
973