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