Refactor part of the renamer to fix Trac #3901
[ghc-hetmet.git] / compiler / deSugar / DsArrows.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 Desugaring arrow commands
7
8 \begin{code}
9 module DsArrows ( dsProcExpr ) where
10
11 #include "HsVersions.h"
12
13 import Match
14 import DsUtils
15 import DsMonad
16
17 import HsSyn    hiding (collectPatBinders, collectPatsBinders )
18 import TcHsSyn
19
20 -- NB: The desugarer, which straddles the source and Core worlds, sometimes
21 --     needs to see source types (newtypes etc), and sometimes not
22 --     So WATCH OUT; check each use of split*Ty functions.
23 -- Sigh.  This is a pain.
24
25 import {-# SOURCE #-} DsExpr ( dsExpr, dsLExpr, dsLocalBinds )
26
27 import TcType
28 import Type
29 import CoreSyn
30 import CoreFVs
31 import CoreUtils
32 import MkCore
33
34 import Name
35 import Var
36 import Id
37 import PrelInfo
38 import DataCon
39 import TysWiredIn
40 import BasicTypes
41 import PrelNames
42 import Outputable
43
44 import VarSet
45 import SrcLoc
46
47 import Data.List
48 \end{code}
49
50 \begin{code}
51 data DsCmdEnv = DsCmdEnv {
52         meth_binds :: [CoreBind],
53         arr_id, compose_id, first_id, app_id, choice_id, loop_id :: CoreExpr
54     }
55
56 mkCmdEnv :: SyntaxTable Id -> DsM DsCmdEnv
57 mkCmdEnv ids = do
58     (meth_binds, ds_meths) <- dsSyntaxTable ids
59     return $ DsCmdEnv {
60                meth_binds = meth_binds,
61                arr_id     = Var (lookupEvidence ds_meths arrAName),
62                compose_id = Var (lookupEvidence ds_meths composeAName),
63                first_id   = Var (lookupEvidence ds_meths firstAName),
64                app_id     = Var (lookupEvidence ds_meths appAName),
65                choice_id  = Var (lookupEvidence ds_meths choiceAName),
66                loop_id    = Var (lookupEvidence ds_meths loopAName)
67              }
68
69 bindCmdEnv :: DsCmdEnv -> CoreExpr -> CoreExpr
70 bindCmdEnv ids body = foldr Let body (meth_binds ids)
71
72 -- arr :: forall b c. (b -> c) -> a b c
73 do_arr :: DsCmdEnv -> Type -> Type -> CoreExpr -> CoreExpr
74 do_arr ids b_ty c_ty f = mkApps (arr_id ids) [Type b_ty, Type c_ty, f]
75
76 -- (>>>) :: forall b c d. a b c -> a c d -> a b d
77 do_compose :: DsCmdEnv -> Type -> Type -> Type ->
78                 CoreExpr -> CoreExpr -> CoreExpr
79 do_compose ids b_ty c_ty d_ty f g
80   = mkApps (compose_id ids) [Type b_ty, Type c_ty, Type d_ty, f, g]
81
82 -- first :: forall b c d. a b c -> a (b,d) (c,d)
83 do_first :: DsCmdEnv -> Type -> Type -> Type -> CoreExpr -> CoreExpr
84 do_first ids b_ty c_ty d_ty f
85   = mkApps (first_id ids) [Type b_ty, Type c_ty, Type d_ty, f]
86
87 -- app :: forall b c. a (a b c, b) c
88 do_app :: DsCmdEnv -> Type -> Type -> CoreExpr
89 do_app ids b_ty c_ty = mkApps (app_id ids) [Type b_ty, Type c_ty]
90
91 -- (|||) :: forall b d c. a b d -> a c d -> a (Either b c) d
92 -- note the swapping of d and c
93 do_choice :: DsCmdEnv -> Type -> Type -> Type ->
94                 CoreExpr -> CoreExpr -> CoreExpr
95 do_choice ids b_ty c_ty d_ty f g
96   = mkApps (choice_id ids) [Type b_ty, Type d_ty, Type c_ty, f, g]
97
98 -- loop :: forall b d c. a (b,d) (c,d) -> a b c
99 -- note the swapping of d and c
100 do_loop :: DsCmdEnv -> Type -> Type -> Type -> CoreExpr -> CoreExpr
101 do_loop ids b_ty c_ty d_ty f
102   = mkApps (loop_id ids) [Type b_ty, Type d_ty, Type c_ty, f]
103
104 -- map_arrow (f :: b -> c) (g :: a c d) = arr f >>> g :: a b d
105 do_map_arrow :: DsCmdEnv -> Type -> Type -> Type ->
106                 CoreExpr -> CoreExpr -> CoreExpr
107 do_map_arrow ids b_ty c_ty d_ty f c
108    = do_compose ids b_ty c_ty d_ty (do_arr ids b_ty c_ty f) c
109
110 mkFailExpr :: HsMatchContext Id -> Type -> DsM CoreExpr
111 mkFailExpr ctxt ty
112   = mkErrorAppDs pAT_ERROR_ID ty (matchContextErrString ctxt)
113
114 -- construct CoreExpr for \ (a :: a_ty, b :: b_ty) -> b
115 mkSndExpr :: Type -> Type -> DsM CoreExpr
116 mkSndExpr a_ty b_ty = do
117     a_var <- newSysLocalDs a_ty
118     b_var <- newSysLocalDs b_ty
119     pair_var <- newSysLocalDs (mkCorePairTy a_ty b_ty)
120     return (Lam pair_var
121                (coreCasePair pair_var a_var b_var (Var b_var)))
122 \end{code}
123
124 Build case analysis of a tuple.  This cannot be done in the DsM monad,
125 because the list of variables is typically not yet defined.
126
127 \begin{code}
128 -- coreCaseTuple [u1..] v [x1..xn] body
129 --      = case v of v { (x1, .., xn) -> body }
130 -- But the matching may be nested if the tuple is very big
131
132 coreCaseTuple :: UniqSupply -> Id -> [Id] -> CoreExpr -> CoreExpr
133 coreCaseTuple uniqs scrut_var vars body
134   = mkTupleCase uniqs vars body scrut_var (Var scrut_var)
135
136 coreCasePair :: Id -> Id -> Id -> CoreExpr -> CoreExpr
137 coreCasePair scrut_var var1 var2 body
138   = Case (Var scrut_var) scrut_var (exprType body)
139          [(DataAlt (tupleCon Boxed 2), [var1, var2], body)]
140 \end{code}
141
142 \begin{code}
143 mkCorePairTy :: Type -> Type -> Type
144 mkCorePairTy t1 t2 = mkBoxedTupleTy [t1, t2]
145
146 mkCorePairExpr :: CoreExpr -> CoreExpr -> CoreExpr
147 mkCorePairExpr e1 e2 = mkCoreTup [e1, e2]
148 \end{code}
149
150 The input is divided into a local environment, which is a flat tuple
151 (unless it's too big), and a stack, each element of which is paired
152 with the stack in turn.  In general, the input has the form
153
154         (...((x1,...,xn),s1),...sk)
155
156 where xi are the environment values, and si the ones on the stack,
157 with s1 being the "top", the first one to be matched with a lambda.
158
159 \begin{code}
160 envStackType :: [Id] -> [Type] -> Type
161 envStackType ids stack_tys = foldl mkCorePairTy (mkBigCoreVarTupTy ids) stack_tys
162
163 ----------------------------------------------
164 --              buildEnvStack
165 --
166 --      (...((x1,...,xn),s1),...sk)
167
168 buildEnvStack :: [Id] -> [Id] -> CoreExpr
169 buildEnvStack env_ids stack_ids
170   = foldl mkCorePairExpr (mkBigCoreVarTup env_ids) (map Var stack_ids)
171
172 ----------------------------------------------
173 --              matchEnvStack
174 --
175 --      \ (...((x1,...,xn),s1),...sk) -> e
176 --      =>
177 --      \ zk ->
178 --      case zk of (zk-1,sk) ->
179 --      ...
180 --      case z1 of (z0,s1) ->
181 --      case z0 of (x1,...,xn) ->
182 --      e
183
184 matchEnvStack   :: [Id]         -- x1..xn
185                 -> [Id]         -- s1..sk
186                 -> CoreExpr     -- e
187                 -> DsM CoreExpr
188 matchEnvStack env_ids stack_ids body = do
189     uniqs <- newUniqueSupply
190     tup_var <- newSysLocalDs (mkBigCoreVarTupTy env_ids)
191     matchVarStack tup_var stack_ids
192                (coreCaseTuple uniqs tup_var env_ids body)
193
194
195 ----------------------------------------------
196 --              matchVarStack
197 --
198 --      \ (...(z0,s1),...sk) -> e
199 --      =>
200 --      \ zk ->
201 --      case zk of (zk-1,sk) ->
202 --      ...
203 --      case z1 of (z0,s1) ->
204 --      e
205
206 matchVarStack :: Id             -- z0
207               -> [Id]           -- s1..sk
208               -> CoreExpr       -- e
209               -> DsM CoreExpr
210 matchVarStack env_id [] body
211   = return (Lam env_id body)
212 matchVarStack env_id (stack_id:stack_ids) body = do
213     pair_id <- newSysLocalDs (mkCorePairTy (idType env_id) (idType stack_id))
214     matchVarStack pair_id stack_ids
215                (coreCasePair pair_id env_id stack_id body)
216 \end{code}
217
218 \begin{code}
219 mkHsEnvStackExpr :: [Id] -> [Id] -> LHsExpr Id
220 mkHsEnvStackExpr env_ids stack_ids
221   = foldl (\a b -> mkLHsTupleExpr [a,b]) 
222           (mkLHsVarTuple env_ids) 
223           (map nlHsVar stack_ids)
224 \end{code}
225
226 Translation of arrow abstraction
227
228 \begin{code}
229
230 --      A | xs |- c :: [] t'        ---> c'
231 --      --------------------------
232 --      A |- proc p -> c :: a t t'  ---> arr (\ p -> (xs)) >>> c'
233 --
234 --              where (xs) is the tuple of variables bound by p
235
236 dsProcExpr
237         :: LPat Id
238         -> LHsCmdTop Id
239         -> DsM CoreExpr
240 dsProcExpr pat (L _ (HsCmdTop cmd [] cmd_ty ids)) = do
241     meth_ids <- mkCmdEnv ids
242     let locals = mkVarSet (collectPatBinders pat)
243     (core_cmd, _free_vars, env_ids) <- dsfixCmd meth_ids locals [] cmd_ty cmd
244     let env_ty = mkBigCoreVarTupTy env_ids
245     fail_expr <- mkFailExpr ProcExpr env_ty
246     var <- selectSimpleMatchVarL pat
247     match_code <- matchSimply (Var var) ProcExpr pat (mkBigCoreVarTup env_ids) fail_expr
248     let pat_ty = hsLPatType pat
249         proc_code = do_map_arrow meth_ids pat_ty env_ty cmd_ty
250                     (Lam var match_code)
251                     core_cmd
252     return (bindCmdEnv meth_ids proc_code)
253 dsProcExpr _ c = pprPanic "dsProcExpr" (ppr c)
254 \end{code}
255
256 Translation of command judgements of the form
257
258         A | xs |- c :: [ts] t
259
260 \begin{code}
261 dsLCmd :: DsCmdEnv -> IdSet -> [Id] -> [Type] -> Type -> LHsCmd Id
262        -> DsM (CoreExpr, IdSet)
263 dsLCmd ids local_vars env_ids stack res_ty cmd
264   = dsCmd ids local_vars env_ids stack res_ty (unLoc cmd)
265
266 dsCmd   :: DsCmdEnv             -- arrow combinators
267         -> IdSet                -- set of local vars available to this command
268         -> [Id]                 -- list of vars in the input to this command
269                                 -- This is typically fed back,
270                                 -- so don't pull on it too early
271         -> [Type]               -- type of the stack
272         -> Type                 -- return type of the command
273         -> HsCmd Id             -- command to desugar
274         -> DsM (CoreExpr,       -- desugared expression
275                 IdSet)          -- set of local vars that occur free
276
277 --      A |- f :: a (t*ts) t'
278 --      A, xs |- arg :: t
279 --      -----------------------------
280 --      A | xs |- f -< arg :: [ts] t'
281 --
282 --              ---> arr (\ ((xs)*ts) -> (arg*ts)) >>> f
283
284 dsCmd ids local_vars env_ids stack res_ty
285         (HsArrApp arrow arg arrow_ty HsFirstOrderApp _)= do
286     let
287         (a_arg_ty, _res_ty') = tcSplitAppTy arrow_ty
288         (_a_ty, arg_ty) = tcSplitAppTy a_arg_ty
289     core_arrow <- dsLExpr arrow
290     core_arg   <- dsLExpr arg
291     stack_ids  <- mapM newSysLocalDs stack
292     core_make_arg <- matchEnvStack env_ids stack_ids
293                       (foldl mkCorePairExpr core_arg (map Var stack_ids))
294     return (do_map_arrow ids
295               (envStackType env_ids stack)
296               arg_ty
297               res_ty
298               core_make_arg
299               core_arrow,
300                exprFreeVars core_arg `intersectVarSet` local_vars)
301
302 --      A, xs |- f :: a (t*ts) t'
303 --      A, xs |- arg :: t
304 --      ------------------------------
305 --      A | xs |- f -<< arg :: [ts] t'
306 --
307 --              ---> arr (\ ((xs)*ts) -> (f,(arg*ts))) >>> app
308
309 dsCmd ids local_vars env_ids stack res_ty
310         (HsArrApp arrow arg arrow_ty HsHigherOrderApp _) = do
311     let
312         (a_arg_ty, _res_ty') = tcSplitAppTy arrow_ty
313         (_a_ty, arg_ty) = tcSplitAppTy a_arg_ty
314     
315     core_arrow <- dsLExpr arrow
316     core_arg   <- dsLExpr arg
317     stack_ids  <- mapM newSysLocalDs stack
318     core_make_pair <- matchEnvStack env_ids stack_ids
319           (mkCorePairExpr core_arrow
320              (foldl mkCorePairExpr core_arg (map Var stack_ids)))
321                              
322     return (do_map_arrow ids
323               (envStackType env_ids stack)
324               (mkCorePairTy arrow_ty arg_ty)
325               res_ty
326               core_make_pair
327               (do_app ids arg_ty res_ty),
328             (exprFreeVars core_arrow `unionVarSet` exprFreeVars core_arg)
329               `intersectVarSet` local_vars)
330
331 --      A | ys |- c :: [t:ts] t'
332 --      A, xs  |- e :: t
333 --      ------------------------
334 --      A | xs |- c e :: [ts] t'
335 --
336 --              ---> arr (\ ((xs)*ts) -> let z = e in (((ys),z)*ts)) >>> c
337
338 dsCmd ids local_vars env_ids stack res_ty (HsApp cmd arg) = do
339     core_arg <- dsLExpr arg
340     let
341         arg_ty = exprType core_arg
342         stack' = arg_ty:stack
343     (core_cmd, free_vars, env_ids')
344              <- dsfixCmd ids local_vars stack' res_ty cmd
345     stack_ids <- mapM newSysLocalDs stack
346     arg_id <- newSysLocalDs arg_ty
347     -- push the argument expression onto the stack
348     let
349         core_body = bindNonRec arg_id core_arg
350                         (buildEnvStack env_ids' (arg_id:stack_ids))
351     -- match the environment and stack against the input
352     core_map <- matchEnvStack env_ids stack_ids core_body
353     return (do_map_arrow ids
354                       (envStackType env_ids stack)
355                       (envStackType env_ids' stack')
356                       res_ty
357                       core_map
358                       core_cmd,
359       (exprFreeVars core_arg `intersectVarSet` local_vars)
360               `unionVarSet` free_vars)
361
362 --      A | ys |- c :: [ts] t'
363 --      -----------------------------------------------
364 --      A | xs |- \ p1 ... pk -> c :: [t1:...:tk:ts] t'
365 --
366 --              ---> arr (\ ((((xs), p1), ... pk)*ts) -> ((ys)*ts)) >>> c
367
368 dsCmd ids local_vars env_ids stack res_ty
369     (HsLam (MatchGroup [L _ (Match pats _ (GRHSs [L _ (GRHS [] body)] _ ))] _)) = do
370     let
371         pat_vars = mkVarSet (collectPatsBinders pats)
372         local_vars' = local_vars `unionVarSet` pat_vars
373         stack' = drop (length pats) stack
374     (core_body, free_vars, env_ids') <- dsfixCmd ids local_vars' stack' res_ty body
375     stack_ids <- mapM newSysLocalDs stack
376
377     -- the expression is built from the inside out, so the actions
378     -- are presented in reverse order
379
380     let
381         (actual_ids, stack_ids') = splitAt (length pats) stack_ids
382         -- build a new environment, plus what's left of the stack
383         core_expr = buildEnvStack env_ids' stack_ids'
384         in_ty = envStackType env_ids stack
385         in_ty' = envStackType env_ids' stack'
386     
387     fail_expr <- mkFailExpr LambdaExpr in_ty'
388     -- match the patterns against the top of the old stack
389     match_code <- matchSimplys (map Var actual_ids) LambdaExpr pats core_expr fail_expr
390     -- match the old environment and stack against the input
391     select_code <- matchEnvStack env_ids stack_ids match_code
392     return (do_map_arrow ids in_ty in_ty' res_ty select_code core_body,
393             free_vars `minusVarSet` pat_vars)
394
395 dsCmd ids local_vars env_ids stack res_ty (HsPar cmd)
396   = dsLCmd ids local_vars env_ids stack res_ty cmd
397
398 --      A, xs |- e :: Bool
399 --      A | xs1 |- c1 :: [ts] t
400 --      A | xs2 |- c2 :: [ts] t
401 --      ----------------------------------------
402 --      A | xs |- if e then c1 else c2 :: [ts] t
403 --
404 --              ---> arr (\ ((xs)*ts) ->
405 --                      if e then Left ((xs1)*ts) else Right ((xs2)*ts)) >>>
406 --                   c1 ||| c2
407
408 dsCmd ids local_vars env_ids stack res_ty (HsIf cond then_cmd else_cmd) = do
409     core_cond <- dsLExpr cond
410     (core_then, fvs_then, then_ids) <- dsfixCmd ids local_vars stack res_ty then_cmd
411     (core_else, fvs_else, else_ids) <- dsfixCmd ids local_vars stack res_ty else_cmd
412     stack_ids  <- mapM newSysLocalDs stack
413     either_con <- dsLookupTyCon eitherTyConName
414     left_con   <- dsLookupDataCon leftDataConName
415     right_con  <- dsLookupDataCon rightDataConName
416     let
417         left_expr ty1 ty2 e = mkConApp left_con [Type ty1, Type ty2, e]
418         right_expr ty1 ty2 e = mkConApp right_con [Type ty1, Type ty2, e]
419
420         in_ty = envStackType env_ids stack
421         then_ty = envStackType then_ids stack
422         else_ty = envStackType else_ids stack
423         sum_ty = mkTyConApp either_con [then_ty, else_ty]
424         fvs_cond = exprFreeVars core_cond `intersectVarSet` local_vars
425     
426     core_if <- matchEnvStack env_ids stack_ids
427                 (mkIfThenElse core_cond
428                     (left_expr  then_ty else_ty (buildEnvStack then_ids stack_ids))
429                     (right_expr then_ty else_ty (buildEnvStack else_ids stack_ids)))
430     return (do_map_arrow ids in_ty sum_ty res_ty
431                 core_if
432                 (do_choice ids then_ty else_ty res_ty core_then core_else),
433         fvs_cond `unionVarSet` fvs_then `unionVarSet` fvs_else)
434 \end{code}
435
436 Case commands are treated in much the same way as if commands
437 (see above) except that there are more alternatives.  For example
438
439         case e of { p1 -> c1; p2 -> c2; p3 -> c3 }
440
441 is translated to
442
443         arr (\ ((xs)*ts) -> case e of
444                 p1 -> (Left (Left (xs1)*ts))
445                 p2 -> Left ((Right (xs2)*ts))
446                 p3 -> Right ((xs3)*ts)) >>>
447         (c1 ||| c2) ||| c3
448
449 The idea is to extract the commands from the case, build a balanced tree
450 of choices, and replace the commands with expressions that build tagged
451 tuples, obtaining a case expression that can be desugared normally.
452 To build all this, we use quadruples decribing segments of the list of
453 case bodies, containing the following fields:
454 1. an IdSet containing the environment variables free in the case bodies
455 2. a list of expressions of the form (Left|Right)* ((xs)*ts), to be put
456    into the case replacing the commands
457 3. a sum type that is the common type of these expressions, and also the
458    input type of the arrow
459 4. a CoreExpr for an arrow built by combining the translated command
460    bodies with |||.
461
462 \begin{code}
463 dsCmd ids local_vars env_ids stack res_ty (HsCase exp (MatchGroup matches match_ty)) = do
464     core_exp <- dsLExpr exp
465     stack_ids <- mapM newSysLocalDs stack
466
467     -- Extract and desugar the leaf commands in the case, building tuple
468     -- expressions that will (after tagging) replace these leaves
469
470     let
471         leaves = concatMap leavesMatch matches
472         make_branch (leaf, bound_vars) = do
473             (core_leaf, fvs, leaf_ids) <-
474                   dsfixCmd ids (local_vars `unionVarSet` bound_vars) stack res_ty leaf
475             return (fvs `minusVarSet` bound_vars,
476                     [mkHsEnvStackExpr leaf_ids stack_ids],
477                     envStackType leaf_ids stack,
478                     core_leaf)
479     
480     branches <- mapM make_branch leaves
481     either_con <- dsLookupTyCon eitherTyConName
482     left_con <- dsLookupDataCon leftDataConName
483     right_con <- dsLookupDataCon rightDataConName
484     let
485         left_id  = HsVar (dataConWrapId left_con)
486         right_id = HsVar (dataConWrapId right_con)
487         left_expr  ty1 ty2 e = noLoc $ HsApp (noLoc $ HsWrap (mkWpTyApps [ty1, ty2]) left_id ) e
488         right_expr ty1 ty2 e = noLoc $ HsApp (noLoc $ HsWrap (mkWpTyApps [ty1, ty2]) right_id) e
489
490         -- Prefix each tuple with a distinct series of Left's and Right's,
491         -- in a balanced way, keeping track of the types.
492
493         merge_branches (fvs1, builds1, in_ty1, core_exp1)
494                        (fvs2, builds2, in_ty2, core_exp2) 
495           = (fvs1 `unionVarSet` fvs2,
496              map (left_expr in_ty1 in_ty2) builds1 ++
497                 map (right_expr in_ty1 in_ty2) builds2,
498              mkTyConApp either_con [in_ty1, in_ty2],
499              do_choice ids in_ty1 in_ty2 res_ty core_exp1 core_exp2)
500         (fvs_alts, leaves', sum_ty, core_choices)
501           = foldb merge_branches branches
502
503         -- Replace the commands in the case with these tagged tuples,
504         -- yielding a HsExpr Id we can feed to dsExpr.
505
506         (_, matches') = mapAccumL (replaceLeavesMatch res_ty) leaves' matches
507         in_ty = envStackType env_ids stack
508         fvs_exp = exprFreeVars core_exp `intersectVarSet` local_vars
509
510         pat_ty    = funArgTy match_ty
511         match_ty' = mkFunTy pat_ty sum_ty
512         -- Note that we replace the HsCase result type by sum_ty,
513         -- which is the type of matches'
514     
515     core_body <- dsExpr (HsCase exp (MatchGroup matches' match_ty'))
516     core_matches <- matchEnvStack env_ids stack_ids core_body
517     return (do_map_arrow ids in_ty sum_ty res_ty core_matches core_choices,
518             fvs_exp `unionVarSet` fvs_alts)
519
520 --      A | ys |- c :: [ts] t
521 --      ----------------------------------
522 --      A | xs |- let binds in c :: [ts] t
523 --
524 --              ---> arr (\ ((xs)*ts) -> let binds in ((ys)*ts)) >>> c
525
526 dsCmd ids local_vars env_ids stack res_ty (HsLet binds body) = do
527     let
528         defined_vars = mkVarSet (collectLocalBinders binds)
529         local_vars' = local_vars `unionVarSet` defined_vars
530     
531     (core_body, _free_vars, env_ids') <- dsfixCmd ids local_vars' stack res_ty body
532     stack_ids <- mapM newSysLocalDs stack
533     -- build a new environment, plus the stack, using the let bindings
534     core_binds <- dsLocalBinds binds (buildEnvStack env_ids' stack_ids)
535     -- match the old environment and stack against the input
536     core_map <- matchEnvStack env_ids stack_ids core_binds
537     return (do_map_arrow ids
538                         (envStackType env_ids stack)
539                         (envStackType env_ids' stack)
540                         res_ty
541                         core_map
542                         core_body,
543         exprFreeVars core_binds `intersectVarSet` local_vars)
544
545 dsCmd ids local_vars env_ids [] res_ty (HsDo _ctxt stmts body _)
546   = dsCmdDo ids local_vars env_ids res_ty stmts body
547
548 --      A |- e :: forall e. a1 (e*ts1) t1 -> ... an (e*tsn) tn -> a (e*ts) t
549 --      A | xs |- ci :: [tsi] ti
550 --      -----------------------------------
551 --      A | xs |- (|e c1 ... cn|) :: [ts] t     ---> e [t_xs] c1 ... cn
552
553 dsCmd _ids local_vars env_ids _stack _res_ty (HsArrForm op _ args) = do
554     let env_ty = mkBigCoreVarTupTy env_ids
555     core_op <- dsLExpr op
556     (core_args, fv_sets) <- mapAndUnzipM (dsTrimCmdArg local_vars env_ids) args
557     return (mkApps (App core_op (Type env_ty)) core_args,
558             unionVarSets fv_sets)
559
560
561 dsCmd ids local_vars env_ids stack res_ty (HsTick ix vars expr) = do
562     (expr1,id_set) <- dsLCmd ids local_vars env_ids stack res_ty expr
563     expr2 <- mkTickBox ix vars expr1
564     return (expr2,id_set)
565
566 dsCmd _ _ _ _ _ c = pprPanic "dsCmd" (ppr c)
567
568 --      A | ys |- c :: [ts] t   (ys <= xs)
569 --      ---------------------
570 --      A | xs |- c :: [ts] t   ---> arr_ts (\ (xs) -> (ys)) >>> c
571
572 dsTrimCmdArg
573         :: IdSet                -- set of local vars available to this command
574         -> [Id]                 -- list of vars in the input to this command
575         -> LHsCmdTop Id -- command argument to desugar
576         -> DsM (CoreExpr,       -- desugared expression
577                 IdSet)          -- set of local vars that occur free
578 dsTrimCmdArg local_vars env_ids (L _ (HsCmdTop cmd stack cmd_ty ids)) = do
579     meth_ids <- mkCmdEnv ids
580     (core_cmd, free_vars, env_ids') <- dsfixCmd meth_ids local_vars stack cmd_ty cmd
581     stack_ids <- mapM newSysLocalDs stack
582     trim_code <- matchEnvStack env_ids stack_ids (buildEnvStack env_ids' stack_ids)
583     let
584         in_ty = envStackType env_ids stack
585         in_ty' = envStackType env_ids' stack
586         arg_code = if env_ids' == env_ids then core_cmd else
587                 do_map_arrow meth_ids in_ty in_ty' cmd_ty trim_code core_cmd
588     return (bindCmdEnv meth_ids arg_code, free_vars)
589
590 -- Given A | xs |- c :: [ts] t, builds c with xs fed back.
591 -- Typically needs to be prefixed with arr (\p -> ((xs)*ts))
592
593 dsfixCmd
594         :: DsCmdEnv             -- arrow combinators
595         -> IdSet                -- set of local vars available to this command
596         -> [Type]               -- type of the stack
597         -> Type                 -- return type of the command
598         -> LHsCmd Id            -- command to desugar
599         -> DsM (CoreExpr,       -- desugared expression
600                 IdSet,          -- set of local vars that occur free
601                 [Id])           -- set as a list, fed back
602 dsfixCmd ids local_vars stack cmd_ty cmd
603   = fixDs (\ ~(_,_,env_ids') -> do
604         (core_cmd, free_vars) <- dsLCmd ids local_vars env_ids' stack cmd_ty cmd
605         return (core_cmd, free_vars, varSetElems free_vars))
606
607 \end{code}
608
609 Translation of command judgements of the form
610
611         A | xs |- do { ss } :: [] t
612
613 \begin{code}
614
615 dsCmdDo :: DsCmdEnv             -- arrow combinators
616         -> IdSet                -- set of local vars available to this statement
617         -> [Id]                 -- list of vars in the input to this statement
618                                 -- This is typically fed back,
619                                 -- so don't pull on it too early
620         -> Type                 -- return type of the statement
621         -> [LStmt Id]           -- statements to desugar
622         -> LHsExpr Id           -- body
623         -> DsM (CoreExpr,       -- desugared expression
624                 IdSet)          -- set of local vars that occur free
625
626 --      A | xs |- c :: [] t
627 --      --------------------------
628 --      A | xs |- do { c } :: [] t
629
630 dsCmdDo ids local_vars env_ids res_ty [] body
631   = dsLCmd ids local_vars env_ids [] res_ty body
632
633 dsCmdDo ids local_vars env_ids res_ty (stmt:stmts) body = do
634     let
635         bound_vars = mkVarSet (collectLStmtBinders stmt)
636         local_vars' = local_vars `unionVarSet` bound_vars
637     (core_stmts, _, env_ids') <- fixDs (\ ~(_,_,env_ids') -> do
638         (core_stmts, fv_stmts) <- dsCmdDo ids local_vars' env_ids' res_ty stmts body
639         return (core_stmts, fv_stmts, varSetElems fv_stmts))
640     (core_stmt, fv_stmt) <- dsCmdLStmt ids local_vars env_ids env_ids' stmt
641     return (do_compose ids
642                 (mkBigCoreVarTupTy env_ids)
643                 (mkBigCoreVarTupTy env_ids')
644                 res_ty
645                 core_stmt
646                 core_stmts,
647               fv_stmt)
648
649 \end{code}
650 A statement maps one local environment to another, and is represented
651 as an arrow from one tuple type to another.  A statement sequence is
652 translated to a composition of such arrows.
653 \begin{code}
654 dsCmdLStmt :: DsCmdEnv -> IdSet -> [Id] -> [Id] -> LStmt Id
655            -> DsM (CoreExpr, IdSet)
656 dsCmdLStmt ids local_vars env_ids out_ids cmd
657   = dsCmdStmt ids local_vars env_ids out_ids (unLoc cmd)
658
659 dsCmdStmt
660         :: DsCmdEnv             -- arrow combinators
661         -> IdSet                -- set of local vars available to this statement
662         -> [Id]                 -- list of vars in the input to this statement
663                                 -- This is typically fed back,
664                                 -- so don't pull on it too early
665         -> [Id]                 -- list of vars in the output of this statement
666         -> Stmt Id      -- statement to desugar
667         -> DsM (CoreExpr,       -- desugared expression
668                 IdSet)          -- set of local vars that occur free
669
670 --      A | xs1 |- c :: [] t
671 --      A | xs' |- do { ss } :: [] t'
672 --      ------------------------------
673 --      A | xs |- do { c; ss } :: [] t'
674 --
675 --              ---> arr (\ (xs) -> ((xs1),(xs'))) >>> first c >>>
676 --                      arr snd >>> ss
677
678 dsCmdStmt ids local_vars env_ids out_ids (ExprStmt cmd _ c_ty) = do
679     (core_cmd, fv_cmd, env_ids1) <- dsfixCmd ids local_vars [] c_ty cmd
680     core_mux <- matchEnvStack env_ids []
681         (mkCorePairExpr (mkBigCoreVarTup env_ids1) (mkBigCoreVarTup out_ids))
682     let
683         in_ty = mkBigCoreVarTupTy env_ids
684         in_ty1 = mkBigCoreVarTupTy env_ids1
685         out_ty = mkBigCoreVarTupTy out_ids
686         before_c_ty = mkCorePairTy in_ty1 out_ty
687         after_c_ty = mkCorePairTy c_ty out_ty
688     snd_fn <- mkSndExpr c_ty out_ty
689     return (do_map_arrow ids in_ty before_c_ty out_ty core_mux $
690                 do_compose ids before_c_ty after_c_ty out_ty
691                         (do_first ids in_ty1 c_ty out_ty core_cmd) $
692                 do_arr ids after_c_ty out_ty snd_fn,
693               extendVarSetList fv_cmd out_ids)
694   where
695
696 --      A | xs1 |- c :: [] t
697 --      A | xs' |- do { ss } :: [] t'           xs2 = xs' - defs(p)
698 --      -----------------------------------
699 --      A | xs |- do { p <- c; ss } :: [] t'
700 --
701 --              ---> arr (\ (xs) -> ((xs1),(xs2))) >>> first c >>>
702 --                      arr (\ (p, (xs2)) -> (xs')) >>> ss
703 --
704 -- It would be simpler and more consistent to do this using second,
705 -- but that's likely to be defined in terms of first.
706
707 dsCmdStmt ids local_vars env_ids out_ids (BindStmt pat cmd _ _) = do
708     (core_cmd, fv_cmd, env_ids1) <- dsfixCmd ids local_vars [] (hsLPatType pat) cmd
709     let
710         pat_ty = hsLPatType pat
711         pat_vars = mkVarSet (collectPatBinders pat)
712         env_ids2 = varSetElems (mkVarSet out_ids `minusVarSet` pat_vars)
713         env_ty2 = mkBigCoreVarTupTy env_ids2
714
715     -- multiplexing function
716     --          \ (xs) -> ((xs1),(xs2))
717
718     core_mux <- matchEnvStack env_ids []
719         (mkCorePairExpr (mkBigCoreVarTup env_ids1) (mkBigCoreVarTup env_ids2))
720
721     -- projection function
722     --          \ (p, (xs2)) -> (zs)
723
724     env_id <- newSysLocalDs env_ty2
725     uniqs <- newUniqueSupply
726     let
727         after_c_ty = mkCorePairTy pat_ty env_ty2
728         out_ty = mkBigCoreVarTupTy out_ids
729         body_expr = coreCaseTuple uniqs env_id env_ids2 (mkBigCoreVarTup out_ids)
730     
731     fail_expr <- mkFailExpr (StmtCtxt DoExpr) out_ty
732     pat_id    <- selectSimpleMatchVarL pat
733     match_code <- matchSimply (Var pat_id) (StmtCtxt DoExpr) pat body_expr fail_expr
734     pair_id   <- newSysLocalDs after_c_ty
735     let
736         proj_expr = Lam pair_id (coreCasePair pair_id pat_id env_id match_code)
737
738     -- put it all together
739     let
740         in_ty = mkBigCoreVarTupTy env_ids
741         in_ty1 = mkBigCoreVarTupTy env_ids1
742         in_ty2 = mkBigCoreVarTupTy env_ids2
743         before_c_ty = mkCorePairTy in_ty1 in_ty2
744     return (do_map_arrow ids in_ty before_c_ty out_ty core_mux $
745                 do_compose ids before_c_ty after_c_ty out_ty
746                         (do_first ids in_ty1 pat_ty in_ty2 core_cmd) $
747                 do_arr ids after_c_ty out_ty proj_expr,
748               fv_cmd `unionVarSet` (mkVarSet out_ids `minusVarSet` pat_vars))
749
750 --      A | xs' |- do { ss } :: [] t
751 --      --------------------------------------
752 --      A | xs |- do { let binds; ss } :: [] t
753 --
754 --              ---> arr (\ (xs) -> let binds in (xs')) >>> ss
755
756 dsCmdStmt ids local_vars env_ids out_ids (LetStmt binds) = do
757     -- build a new environment using the let bindings
758     core_binds <- dsLocalBinds binds (mkBigCoreVarTup out_ids)
759     -- match the old environment against the input
760     core_map <- matchEnvStack env_ids [] core_binds
761     return (do_arr ids
762                         (mkBigCoreVarTupTy env_ids)
763                         (mkBigCoreVarTupTy out_ids)
764                         core_map,
765         exprFreeVars core_binds `intersectVarSet` local_vars)
766
767 --      A | ys |- do { ss; returnA -< ((xs1), (ys2)) } :: [] ...
768 --      A | xs' |- do { ss' } :: [] t
769 --      ------------------------------------
770 --      A | xs |- do { rec ss; ss' } :: [] t
771 --
772 --                      xs1 = xs' /\ defs(ss)
773 --                      xs2 = xs' - defs(ss)
774 --                      ys1 = ys - defs(ss)
775 --                      ys2 = ys /\ defs(ss)
776 --
777 --              ---> arr (\(xs) -> ((ys1),(xs2))) >>>
778 --                      first (loop (arr (\((ys1),~(ys2)) -> (ys)) >>> ss)) >>>
779 --                      arr (\((xs1),(xs2)) -> (xs')) >>> ss'
780
781 dsCmdStmt ids local_vars env_ids out_ids 
782           (RecStmt { recS_stmts = stmts, recS_later_ids = later_ids, recS_rec_ids = rec_ids
783                    , recS_rec_rets = rhss, recS_dicts = _binds }) = do
784     let         -- ToDo: ****** binds not desugared; ROSS PLEASE FIX ********
785         env2_id_set = mkVarSet out_ids `minusVarSet` mkVarSet later_ids
786         env2_ids = varSetElems env2_id_set
787         env2_ty = mkBigCoreVarTupTy env2_ids
788
789     -- post_loop_fn = \((later_ids),(env2_ids)) -> (out_ids)
790
791     uniqs <- newUniqueSupply
792     env2_id <- newSysLocalDs env2_ty
793     let
794         later_ty = mkBigCoreVarTupTy later_ids
795         post_pair_ty = mkCorePairTy later_ty env2_ty
796         post_loop_body = coreCaseTuple uniqs env2_id env2_ids (mkBigCoreVarTup out_ids)
797
798     post_loop_fn <- matchEnvStack later_ids [env2_id] post_loop_body
799
800     --- loop (...)
801
802     (core_loop, env1_id_set, env1_ids)
803                <- dsRecCmd ids local_vars stmts later_ids rec_ids rhss
804
805     -- pre_loop_fn = \(env_ids) -> ((env1_ids),(env2_ids))
806
807     let
808         env1_ty = mkBigCoreVarTupTy env1_ids
809         pre_pair_ty = mkCorePairTy env1_ty env2_ty
810         pre_loop_body = mkCorePairExpr (mkBigCoreVarTup env1_ids)
811                                         (mkBigCoreVarTup env2_ids)
812
813     pre_loop_fn <- matchEnvStack env_ids [] pre_loop_body
814
815     -- arr pre_loop_fn >>> first (loop (...)) >>> arr post_loop_fn
816
817     let
818         env_ty = mkBigCoreVarTupTy env_ids
819         out_ty = mkBigCoreVarTupTy out_ids
820         core_body = do_map_arrow ids env_ty pre_pair_ty out_ty
821                 pre_loop_fn
822                 (do_compose ids pre_pair_ty post_pair_ty out_ty
823                         (do_first ids env1_ty later_ty env2_ty
824                                 core_loop)
825                         (do_arr ids post_pair_ty out_ty
826                                 post_loop_fn))
827
828     return (core_body, env1_id_set `unionVarSet` env2_id_set)
829
830 dsCmdStmt _ _ _ _ s = pprPanic "dsCmdStmt" (ppr s)
831
832 --      loop (arr (\ ((env1_ids), ~(rec_ids)) -> (env_ids)) >>>
833 --            ss >>>
834 --            arr (\ (out_ids) -> ((later_ids),(rhss))) >>>
835
836 dsRecCmd :: DsCmdEnv -> VarSet -> [LStmt Id] -> [Var] -> [Var] -> [HsExpr Id]
837          -> DsM (CoreExpr, VarSet, [Var])
838 dsRecCmd ids local_vars stmts later_ids rec_ids rhss = do
839     let
840         rec_id_set = mkVarSet rec_ids
841         out_ids = varSetElems (mkVarSet later_ids `unionVarSet` rec_id_set)
842         out_ty = mkBigCoreVarTupTy out_ids
843         local_vars' = local_vars `unionVarSet` rec_id_set
844
845     -- mk_pair_fn = \ (out_ids) -> ((later_ids),(rhss))
846
847     core_rhss <- mapM dsExpr rhss
848     let
849         later_tuple = mkBigCoreVarTup later_ids
850         later_ty = mkBigCoreVarTupTy later_ids
851         rec_tuple = mkBigCoreTup core_rhss
852         rec_ty = mkBigCoreVarTupTy rec_ids
853         out_pair = mkCorePairExpr later_tuple rec_tuple
854         out_pair_ty = mkCorePairTy later_ty rec_ty
855
856     mk_pair_fn <- matchEnvStack out_ids [] out_pair
857
858     -- ss
859
860     (core_stmts, fv_stmts, env_ids) <- dsfixCmdStmts ids local_vars' out_ids stmts
861
862     -- squash_pair_fn = \ ((env1_ids), ~(rec_ids)) -> (env_ids)
863
864     rec_id <- newSysLocalDs rec_ty
865     let
866         env1_id_set = fv_stmts `minusVarSet` rec_id_set
867         env1_ids = varSetElems env1_id_set
868         env1_ty = mkBigCoreVarTupTy env1_ids
869         in_pair_ty = mkCorePairTy env1_ty rec_ty
870         core_body = mkBigCoreTup (map selectVar env_ids)
871           where
872             selectVar v
873                 | v `elemVarSet` rec_id_set
874                   = mkTupleSelector rec_ids v rec_id (Var rec_id)
875                 | otherwise = Var v
876
877     squash_pair_fn <- matchEnvStack env1_ids [rec_id] core_body
878
879     -- loop (arr squash_pair_fn >>> ss >>> arr mk_pair_fn)
880
881     let
882         env_ty = mkBigCoreVarTupTy env_ids
883         core_loop = do_loop ids env1_ty later_ty rec_ty
884                 (do_map_arrow ids in_pair_ty env_ty out_pair_ty
885                         squash_pair_fn
886                         (do_compose ids env_ty out_ty out_pair_ty
887                                 core_stmts
888                                 (do_arr ids out_ty out_pair_ty mk_pair_fn)))
889
890     return (core_loop, env1_id_set, env1_ids)
891
892 \end{code}
893 A sequence of statements (as in a rec) is desugared to an arrow between
894 two environments
895 \begin{code}
896
897 dsfixCmdStmts
898         :: DsCmdEnv             -- arrow combinators
899         -> IdSet                -- set of local vars available to this statement
900         -> [Id]                 -- output vars of these statements
901         -> [LStmt Id]   -- statements to desugar
902         -> DsM (CoreExpr,       -- desugared expression
903                 IdSet,          -- set of local vars that occur free
904                 [Id])           -- input vars
905
906 dsfixCmdStmts ids local_vars out_ids stmts
907   = fixDs (\ ~(_,_,env_ids) -> do
908         (core_stmts, fv_stmts) <- dsCmdStmts ids local_vars env_ids out_ids stmts
909         return (core_stmts, fv_stmts, varSetElems fv_stmts))
910
911 dsCmdStmts
912         :: DsCmdEnv             -- arrow combinators
913         -> IdSet                -- set of local vars available to this statement
914         -> [Id]                 -- list of vars in the input to these statements
915         -> [Id]                 -- output vars of these statements
916         -> [LStmt Id]   -- statements to desugar
917         -> DsM (CoreExpr,       -- desugared expression
918                 IdSet)          -- set of local vars that occur free
919
920 dsCmdStmts ids local_vars env_ids out_ids [stmt]
921   = dsCmdLStmt ids local_vars env_ids out_ids stmt
922
923 dsCmdStmts ids local_vars env_ids out_ids (stmt:stmts) = do
924     let
925         bound_vars = mkVarSet (collectLStmtBinders stmt)
926         local_vars' = local_vars `unionVarSet` bound_vars
927     (core_stmts, _fv_stmts, env_ids') <- dsfixCmdStmts ids local_vars' out_ids stmts
928     (core_stmt, fv_stmt) <- dsCmdLStmt ids local_vars env_ids env_ids' stmt
929     return (do_compose ids
930                 (mkBigCoreVarTupTy env_ids)
931                 (mkBigCoreVarTupTy env_ids')
932                 (mkBigCoreVarTupTy out_ids)
933                 core_stmt
934                 core_stmts,
935               fv_stmt)
936
937 dsCmdStmts _ _ _ _ [] = panic "dsCmdStmts []"
938
939 \end{code}
940
941 Match a list of expressions against a list of patterns, left-to-right.
942
943 \begin{code}
944 matchSimplys :: [CoreExpr]              -- Scrutinees
945              -> HsMatchContext Name     -- Match kind
946              -> [LPat Id]               -- Patterns they should match
947              -> CoreExpr                -- Return this if they all match
948              -> CoreExpr                -- Return this if they don't
949              -> DsM CoreExpr
950 matchSimplys [] _ctxt [] result_expr _fail_expr = return result_expr
951 matchSimplys (exp:exps) ctxt (pat:pats) result_expr fail_expr = do
952     match_code <- matchSimplys exps ctxt pats result_expr fail_expr
953     matchSimply exp ctxt pat match_code fail_expr
954 matchSimplys _ _ _ _ _ = panic "matchSimplys"
955 \end{code}
956
957 List of leaf expressions, with set of variables bound in each
958
959 \begin{code}
960 leavesMatch :: LMatch Id -> [(LHsExpr Id, IdSet)]
961 leavesMatch (L _ (Match pats _ (GRHSs grhss binds)))
962   = let
963         defined_vars = mkVarSet (collectPatsBinders pats)
964                         `unionVarSet`
965                        mkVarSet (collectLocalBinders binds)
966     in
967     [(expr, 
968       mkVarSet (collectLStmtsBinders stmts) 
969         `unionVarSet` defined_vars) 
970     | L _ (GRHS stmts expr) <- grhss]
971 \end{code}
972
973 Replace the leaf commands in a match
974
975 \begin{code}
976 replaceLeavesMatch
977         :: Type                 -- new result type
978         -> [LHsExpr Id] -- replacement leaf expressions of that type
979         -> LMatch Id    -- the matches of a case command
980         -> ([LHsExpr Id],-- remaining leaf expressions
981             LMatch Id)  -- updated match
982 replaceLeavesMatch _res_ty leaves (L loc (Match pat mt (GRHSs grhss binds)))
983   = let
984         (leaves', grhss') = mapAccumL replaceLeavesGRHS leaves grhss
985     in
986     (leaves', L loc (Match pat mt (GRHSs grhss' binds)))
987
988 replaceLeavesGRHS
989         :: [LHsExpr Id] -- replacement leaf expressions of that type
990         -> LGRHS Id     -- rhss of a case command
991         -> ([LHsExpr Id],-- remaining leaf expressions
992             LGRHS Id)   -- updated GRHS
993 replaceLeavesGRHS (leaf:leaves) (L loc (GRHS stmts _))
994   = (leaves, L loc (GRHS stmts leaf))
995 replaceLeavesGRHS [] _ = panic "replaceLeavesGRHS []"
996 \end{code}
997
998 Balanced fold of a non-empty list.
999
1000 \begin{code}
1001 foldb :: (a -> a -> a) -> [a] -> a
1002 foldb _ [] = error "foldb of empty list"
1003 foldb _ [x] = x
1004 foldb f xs = foldb f (fold_pairs xs)
1005   where
1006     fold_pairs [] = []
1007     fold_pairs [x] = [x]
1008     fold_pairs (x1:x2:xs) = f x1 x2:fold_pairs xs
1009 \end{code}
1010
1011 Note [Dictionary binders in ConPatOut] See also same Note in HsUtils
1012 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1013 The following functions to collect value variables from patterns are
1014 copied from HsUtils, with one change: we also collect the dictionary
1015 bindings (pat_binds) from ConPatOut.  We need them for cases like
1016
1017 h :: Arrow a => Int -> a (Int,Int) Int
1018 h x = proc (y,z) -> case compare x y of
1019                 GT -> returnA -< z+x
1020
1021 The type checker turns the case into
1022
1023                 case compare x y of
1024                   GT { p77 = plusInt } -> returnA -< p77 z x
1025
1026 Here p77 is a local binding for the (+) operation.
1027
1028 See comments in HsUtils for why the other version does not include
1029 these bindings.
1030
1031 \begin{code}
1032 collectPatBinders :: OutputableBndr a => LPat a -> [a]
1033 collectPatBinders pat = collectl pat []
1034
1035 collectPatsBinders :: OutputableBndr a => [LPat a] -> [a]
1036 collectPatsBinders pats = foldr collectl [] pats
1037
1038 ---------------------
1039 collectl :: OutputableBndr a => LPat a -> [a] -> [a]
1040 -- See Note [Dictionary binders in ConPatOut]
1041 collectl (L _ pat) bndrs
1042   = go pat
1043   where
1044     go (VarPat var)               = var : bndrs
1045     go (VarPatOut var bs)         = var : collectHsBindsBinders bs
1046                                     ++ bndrs
1047     go (WildPat _)                = bndrs
1048     go (LazyPat pat)              = collectl pat bndrs
1049     go (BangPat pat)              = collectl pat bndrs
1050     go (AsPat (L _ a) pat)        = a : collectl pat bndrs
1051     go (ParPat  pat)              = collectl pat bndrs
1052
1053     go (ListPat pats _)           = foldr collectl bndrs pats
1054     go (PArrPat pats _)           = foldr collectl bndrs pats
1055     go (TuplePat pats _ _)        = foldr collectl bndrs pats
1056
1057     go (ConPatIn _ ps)            = foldr collectl bndrs (hsConPatArgs ps)
1058     go (ConPatOut {pat_args=ps, pat_binds=ds}) =
1059                                     collectHsBindsBinders ds
1060                                     ++ foldr collectl bndrs (hsConPatArgs ps)
1061     go (LitPat _)                 = bndrs
1062     go (NPat _ _ _)               = bndrs
1063     go (NPlusKPat (L _ n) _ _ _)  = n : bndrs
1064
1065     go (SigPatIn pat _)           = collectl pat bndrs
1066     go (SigPatOut pat _)          = collectl pat bndrs
1067     go (TypePat _)                = bndrs
1068     go (CoPat _ pat _)            = collectl (noLoc pat) bndrs
1069     go p                          = pprPanic "collectl/go" (ppr p)
1070 \end{code}