@@ -497,9 +497,18 @@ buildDepMap ci used externs ctx startNames
497497 Nothing -> error $ "Erasure/getArity: definition not found for " ++ show n
498498
499499 -- convert applications of lambdas to lets
500− -- Note that this transformation preserves de bruijn numbering
500+ -- note that this transformation preserves de bruijn numbering
501501 lamToLet :: Term -> Term
502− lamToLet (App _ (Bind n (Lam ty) tm) val) = Bind n (Let ty val) tm
502+ lamToLet tm = lamToLet' args f
503+ where
504+ (f, args) = unApply tm
505+
506+ lamToLet' :: [Term] -> Term -> Term
507+ lamToLet' (v:vs) (Bind n (Lam ty) tm) = Bind n (Let ty v) $ lamToLet' vs tm
508+ lamToLet' [] tm = tm
509+ lamToLet' vs tm = error $
510+ "Erasure.hs:lamToLet': unexpected input: "
511+ ++ "vs = " ++ show vs ++ ", tm = " ++ show tm
503512
504513 -- split "\x_i -> T(x_i)" into [x_i] and T
505514 unfoldLams :: Term -> ([Name], Term)