+
+Local tyvar-lifting
+~~~~~~~~~~~~~~~~~~~
+mkRhsTyLam tries this transformation, when the big lambda appears as
+the RHS of a let(rec) binding:
+
+ /\abc -> let(rec) x = e in b
+ ==>
+ let(rec) x' = /\abc -> let x = x' a b c in e
+ in
+ /\abc -> let x = x' a b c in b
+
+This is good because it can turn things like:
+
+ let f = /\a -> letrec g = ... g ... in g
+into
+ letrec g' = /\a -> ... g' a ...
+ in
+ let f = /\ a -> f a
+
+which is better. In effect, it means that big lambdas don't impede
+let-floating.
+
+This optimisation is CRUCIAL in eliminating the junk introduced by
+desugaring mutually recursive definitions. Don't eliminate it lightly!
+
+So far as the implemtation is concerned:
+
+ Invariant: go F e = /\tvs -> F e
+
+ Equalities:
+ go F (Let x=e in b)
+ = Let x' = /\tvs -> F e
+ in
+ go G b
+ where
+ G = F . Let x = x' tvs
+
+ go F (Letrec xi=ei in b)
+ = Letrec {xi' = /\tvs -> G ei}
+ in
+ go G b
+ where
+ G = F . Let {xi = xi' tvs}
+
+\begin{code}
+mkRhsTyLam [] body = returnSmpl body
+
+mkRhsTyLam tyvars body
+ = go (\x -> x) body
+ where
+ tyvar_tys = mkTyVarTys tyvars
+
+ go fn (Let bind@(NonRec var rhs) body) | exprIsTrivial rhs
+ = go (fn . Let bind) body
+
+ go fn (Let bind@(NonRec var rhs) body)
+ = mk_poly var `thenSmpl` \ (var', rhs') ->
+ go (fn . Let (mk_silly_bind var rhs')) body `thenSmpl` \ body' ->
+ returnSmpl (Let (NonRec var' (mkTyLam tyvars (fn rhs))) body')
+
+ go fn (Let (Rec prs) body)
+ = mapAndUnzipSmpl mk_poly vars `thenSmpl` \ (vars', rhss') ->
+ let
+ gn body = fn $ foldr Let body (zipWith mk_silly_bind vars rhss')
+ in
+ go gn body `thenSmpl` \ body' ->
+ returnSmpl (Let (Rec (vars' `zip` [mkTyLam tyvars (gn rhs) | rhs <- rhss])) body')
+ where
+ (vars,rhss) = unzip prs
+
+ go fn body = returnSmpl (mkTyLam tyvars (fn body))
+
+ mk_poly var
+ = newId (mkForAllTys tyvars (idType var)) `thenSmpl` \ poly_id ->
+ returnSmpl (poly_id, mkTyApp (Var poly_id) tyvar_tys)
+
+ mk_silly_bind var rhs = NonRec (addInlinePragma var) rhs
+ -- The addInlinePragma is really important! If we don't say
+ -- INLINE on these silly little bindings then look what happens!
+ -- Suppose we start with:
+ --
+ -- x = let g = /\a -> \x -> f x x
+ -- in
+ -- /\ b -> let g* = g b in E
+ --
+ -- Then: * the binding for g gets floated out
+ -- * but then it gets inlined into the rhs of g*
+ -- * then the binding for g* is floated out of the /\b
+ -- * so we're back to square one
+ -- The silly binding for g* must be INLINE, so that no inlining
+ -- will happen in its RHS.
+\end{code}
+