+mkClosureApp :: Type -> Type -> VExpr -> VExpr -> VM VExpr
+mkClosureApp arg_ty res_ty (vclo, lclo) (varg, larg)
+ = do
+ vapply <- builtin applyClosureVar
+ lapply <- builtin applyClosurePVar
+ return (Var vapply `mkTyApps` [arg_ty, res_ty] `mkApps` [vclo, varg],
+ Var lapply `mkTyApps` [arg_ty, res_ty] `mkApps` [lclo, larg])
+
+buildClosures :: [TyVar] -> [VVar] -> [Type] -> Type -> VM VExpr -> VM VExpr
+buildClosures tvs vars [] res_ty mk_body
+ = mk_body
+buildClosures tvs vars [arg_ty] res_ty mk_body
+ = buildClosure tvs vars arg_ty res_ty mk_body
+buildClosures tvs vars (arg_ty : arg_tys) res_ty mk_body
+ = do
+ res_ty' <- mkClosureTypes arg_tys res_ty
+ arg <- newLocalVVar FSLIT("x") arg_ty
+ buildClosure tvs vars arg_ty res_ty'
+ . hoistPolyVExpr tvs
+ $ do
+ lc <- builtin liftingContext
+ clo <- buildClosures tvs (vars ++ [arg]) arg_tys res_ty mk_body
+ return $ vLams lc (vars ++ [arg]) clo
+