diff --git a/opt.py b/opt.py index a906dfe..e322f39 100644 --- a/opt.py +++ b/opt.py @@ -10,8 +10,9 @@ Optimization: TypeAlias = Callable[[Expression], Option[Expression]] def eliminate_single_let(expr: Expression) -> Option[Expression]: match expr: case LetBinding(lhs, rhs, body): - if count_uses(lhs, body) <= 1: - # RHS is used at most once i nthe body + rhs_is_simple = isinstance(rhs, Int | Variable | Builtin) + if count_uses(lhs, body) <= 1 or rhs_is_simple: + # RHS is used at most once i nthe body or replication wouldnt be costly if count_uses(lhs, rhs) > 0: # RHS is recursive if not isinstance(body, Variable):