lean4-htt/src/library/replace_visitor.cpp
Leonardo de Moura 85092412c7 refactor: remove Expr.FVar hack
@Kha @dselsam:
This hack was preventing us from making `Expr` a "real" Lean type.
This was bad for a few reasons:
- It was hard to extend/modify `Expr` in Lean since we would also have
to modify the C++ code that creates the `Expr` objects with the hidden
fields.
- `Expr.lam` and `Expr.forallE` were not following the Lean layout
standard where we sort fields by size. @Kha: recall we used that to
avoid a UB. The issue with `Expr.lam` and `Expr.forallE` is that they
have a "visible" field (`BinderInfo`), which is smaller than
hidden fields such as hash code.
- `Expr.fvar` had only one field at `Expr.lean,` but four behind the
scenes.

I added a new constructor `Local` that is only accessible from C++.
It is only used in legacy code we inherited from Lean2.
We will eventually delete it.

This refactoring was quite painful since many parts of the codebase
were mixing the new `Expr.fvar` with the old `Expr.local`.
I doubt I would be able to do it without the new staging framework
@Kha built.

BTW, some of the patches are horrible. I didn't care much since we
are going to deleted the super ugly files. That being said,
you should expect new weird bevaior due to `Expr.fvar` vs `Expr.local`.

Next step: use the new `ExprCachedData` to make all `Expr` hidden visibles
accessible from Lean.

checkpoint
2019-11-15 14:04:26 -08:00

85 lines
3.5 KiB
C++

/*
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include <tuple>
#include "runtime/interrupt.h"
#include "util/fresh_name.h"
#include "kernel/instantiate.h"
#include "kernel/abstract.h"
#include "library/replace_visitor.h"
#include "library/pos_info_provider.h"
namespace lean {
expr replace_visitor::visit_sort(expr const & e) { lean_assert(is_sort(e)); return e; }
expr replace_visitor::visit_var(expr const & e) { lean_assert(is_var(e)); return e; }
expr replace_visitor::visit_lit(expr const & e) { lean_assert(is_lit(e)); return e; }
expr replace_visitor::visit_constant(expr const & e) { lean_assert(is_constant(e)); return e; }
expr replace_visitor::visit_meta(expr const & e) { lean_assert(is_mvar(e)); return e; }
expr replace_visitor::visit_fvar(expr const & e) { lean_assert(is_fvar(e)); return e; }
expr replace_visitor::visit_local(expr const & e) {
lean_assert(is_local(e));
return update_local(e, visit(local_type(e)));
}
expr replace_visitor::visit_mdata(expr const & e) {
return update_mdata(e, visit(mdata_expr(e)));
}
expr replace_visitor::visit_proj(expr const & e) {
return update_proj(e, visit(proj_expr(e)));
}
expr replace_visitor::visit_app(expr const & e) {
lean_assert(is_app(e));
expr new_fn = visit(app_fn(e));
expr new_arg = visit(app_arg(e));
return update_app(e, new_fn, new_arg);
}
expr replace_visitor::visit_binding(expr const & e) {
lean_assert(is_binding(e));
expr new_d = visit(binding_domain(e));
expr new_b = visit(binding_body(e));
return update_binding(e, new_d, new_b);
}
expr replace_visitor::visit_lambda(expr const & e) { return visit_binding(e); }
expr replace_visitor::visit_pi(expr const & e) { return visit_binding(e); }
expr replace_visitor::visit_let(expr const & e) {
lean_assert(is_let(e));
expr new_t = visit(let_type(e));
expr new_v = visit(let_value(e));
expr new_b = visit(let_body(e));
return update_let(e, new_t, new_v, new_b);
}
expr replace_visitor::save_result(expr const & e, expr && r, bool shared) {
if (shared)
m_cache.insert(std::make_pair(e, r));
return copy_pos(e, r);
}
expr replace_visitor::visit(expr const & e) {
check_system("expression replacer");
bool shared = false;
if (is_shared(e)) {
shared = true;
auto it = m_cache.find(e);
if (it != m_cache.end())
return it->second;
}
switch (e.kind()) {
case expr_kind::Lit: return save_result(e, visit_lit(e), shared);
case expr_kind::MData: return save_result(e, visit_mdata(e), shared);
case expr_kind::Proj: return save_result(e, visit_proj(e), shared);
case expr_kind::Sort: return save_result(e, visit_sort(e), shared);
case expr_kind::Const: return save_result(e, visit_constant(e), shared);
case expr_kind::BVar: return save_result(e, visit_var(e), shared);
case expr_kind::MVar: return save_result(e, visit_meta(e), shared);
case expr_kind::FVar: return save_result(e, visit_fvar(e), shared);
case expr_kind::App: return save_result(e, visit_app(e), shared);
case expr_kind::Lambda: return save_result(e, visit_lambda(e), shared);
case expr_kind::Pi: return save_result(e, visit_pi(e), shared);
case expr_kind::Let: return save_result(e, visit_let(e), shared);
case expr_kind::Local: return save_result(e, visit_local(e), shared);
}
lean_unreachable(); // LCOV_EXCL_LINE
}
}