+collectPatBinders pat = collect pat []
+
+collectOutPatBinders :: OutPat a -> [a]
+collectOutPatBinders pat = collectOut pat []
+
+collectPatsBinders :: [InPat a] -> [a]
+collectPatsBinders pats = foldr collect [] pats
+
+collect WildPatIn bndrs = bndrs
+collect (VarPatIn var) bndrs = var : bndrs
+collect (LitPatIn _) bndrs = bndrs
+collect (SigPatIn pat _) bndrs = collect pat bndrs
+collect (LazyPatIn pat) bndrs = collect pat bndrs
+collect (AsPatIn a pat) bndrs = a : collect pat bndrs
+collect (NPlusKPatIn n _ _) bndrs = n : bndrs
+collect (NPatIn _) bndrs = bndrs
+collect (ConPatIn c pats) bndrs = foldr collect bndrs pats
+collect (ConOpPatIn p1 c f p2) bndrs = collect p1 (collect p2 bndrs)
+collect (ParPatIn pat) bndrs = collect pat bndrs
+collect (ListPatIn pats) bndrs = foldr collect bndrs pats
+collect (PArrPatIn pats) bndrs = foldr collect bndrs pats
+collect (TuplePatIn pats _) bndrs = foldr collect bndrs pats
+collect (RecPatIn c fields) bndrs = foldr (\ (f,pat,_) bndrs -> collect pat bndrs) bndrs fields
+-- Generics
+collect (TypePatIn ty) bndrs = bndrs
+-- assume the type variables do not need to be bound
+
+-- collect the bounds *value* variables in renamed patterns; type variables
+-- are *not* collected
+--
+collectOut (WildPat _) bndrs = bndrs
+collectOut (VarPat var) bndrs = var : bndrs
+collectOut (LazyPat pat) bndrs = collectOut pat bndrs
+collectOut (AsPat a pat) bndrs = a : collectOut pat bndrs
+collectOut (ListPat _ pats) bndrs = foldr collectOut bndrs pats
+collectOut (PArrPat _ pats) bndrs = foldr collectOut bndrs pats
+collectOut (TuplePat pats _) bndrs = foldr collectOut bndrs pats
+collectOut (ConPat _ _ _ ds pats) bndrs = ds ++ foldr collectOut bndrs pats
+collectOut (RecPat _ _ _ ds fields) bndrs = ds ++ foldr comb bndrs fields
+ where
+ comb (_, pat, _) bndrs = collectOut pat bndrs
+collectOut (LitPat _ _) bndrs = bndrs
+collectOut (NPat _ _ _) bndrs = bndrs
+collectOut (NPlusKPat n _ _ _ _) bndrs = n : bndrs
+collectOut (DictPat ids1 ids2) bndrs = ids1 ++ ids2 ++ bndrs
+\end{code}