lean4-htt/src/library/deep_copy.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

39 lines
1.6 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 "util/buffer.h"
#include "kernel/expr.h"
#include "kernel/replace_fn.h"
namespace lean {
expr copy(expr const & a) {
switch (a.kind()) {
case expr_kind::Lit: return mk_lit(lit_value(a));
case expr_kind::MData: return mk_mdata(mdata_data(a), mdata_expr(a));
case expr_kind::Proj: return mk_proj(proj_sname(a), proj_idx(a), proj_expr(a));
case expr_kind::BVar: return mk_bvar(bvar_idx(a));
case expr_kind::FVar: return mk_fvar(fvar_name(a));
case expr_kind::Const: return mk_const(const_name(a), const_levels(a));
case expr_kind::Sort: return mk_sort(sort_level(a));
case expr_kind::App: return mk_app(app_fn(a), app_arg(a));
case expr_kind::Lambda: return mk_lambda(binding_name(a), binding_domain(a), binding_body(a), binding_info(a));
case expr_kind::Pi: return mk_pi(binding_name(a), binding_domain(a), binding_body(a), binding_info(a));
case expr_kind::MVar: return mk_mvar(mvar_name(a));
case expr_kind::Let: return mk_let(let_name(a), let_type(a), let_value(a), let_body(a));
case expr_kind::Local: return mk_local(local_name(a), local_pp_name(a), local_type(a), local_info(a));
}
lean_unreachable(); // LCOV_EXCL_LINE
}
expr deep_copy(expr const & e) {
return replace(e, [](expr const & e) {
if (is_atomic(e))
return some_expr(copy(e));
else
return none_expr();
});
}
}