+ keyd_binds = bagToList binds `zip` [0::BKey ..]
+
+ key_map :: NameEnv BKey -- Which binding it comes from
+ key_map = mkNameEnv [(bndr, key) | (L _ bind, key) <- keyd_binds
+ , bndr <- bindersOfHsBind bind ]
+
+bindersOfHsBind :: HsBind Name -> [Name]
+bindersOfHsBind (PatBind { pat_lhs = pat }) = collectPatBinders pat
+bindersOfHsBind (FunBind { fun_id = L _ f }) = [f]
+
+------------------------
+tcPolyBinds :: TopLevelFlag
+ -> RecFlag -- Whether the group is really recursive
+ -> RecFlag -- Whether it's recursive for typechecking purposes
+ -> TcSigFun -> TcPragFun
+ -> LHsBinds Name
+ -> TcM thing
+ -> TcM ([LHsBinds TcId], thing)
+
+-- Typechecks a single bunch of bindings all together,
+-- and generalises them. The bunch may be only part of a recursive
+-- group, because we use type signatures to maximise polymorphism
+--
+-- Deals with the bindInstsOfLocalFuns thing too
+--
+-- Returns a list because the input may be a single non-recursive binding,
+-- in which case the dependency order of the resulting bindings is
+-- important.
+
+tcPolyBinds top_lvl rec_group rec_tc sig_fn prag_fn scc thing_inside
+ = -- NB: polymorphic recursion means that a function
+ -- may use an instance of itself, we must look at the LIE arising
+ -- from the function's own right hand side. Hence the getLIE
+ -- encloses the tc_poly_binds.
+ do { traceTc (text "tcPolyBinds" <+> ppr scc)
+ ; ((binds1, poly_ids, thing), lie) <- getLIE $
+ do { (binds1, poly_ids) <- tc_poly_binds top_lvl rec_group rec_tc
+ sig_fn prag_fn scc
+ ; thing <- tcExtendIdEnv poly_ids thing_inside
+ ; return (binds1, poly_ids, thing) }
+
+ ; if isTopLevel top_lvl
+ then -- For the top level don't bother will all this
+ -- bindInstsOfLocalFuns stuff. All the top level
+ -- things are rec'd together anyway, so it's fine to
+ -- leave them to the tcSimplifyTop,
+ -- and quite a bit faster too
+ do { extendLIEs lie; return (binds1, thing) }
+
+ else do -- Nested case
+ { lie_binds <- bindInstsOfLocalFuns lie poly_ids
+ ; return (binds1 ++ [lie_binds], thing) }}
+
+------------------------
+tc_poly_binds :: TopLevelFlag -- See comments on tcPolyBinds
+ -> RecFlag -> RecFlag
+ -> TcSigFun -> TcPragFun
+ -> LHsBinds Name
+ -> TcM ([LHsBinds TcId], [TcId])
+-- Typechecks the bindings themselves
+-- Knows nothing about the scope of the bindings
+
+tc_poly_binds top_lvl rec_group rec_tc sig_fn prag_fn binds
+ = let
+ binder_names = collectHsBindBinders binds
+ bind_list = bagToList binds
+
+ loc = getLoc (head bind_list)
+ -- TODO: location a bit awkward, but the mbinds have been
+ -- dependency analysed and may no longer be adjacent
+ in