2013-07-24 17:14:22 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/buffer.h"
|
|
|
|
#include "kernel/expr.h"
|
|
|
|
#include "kernel/expr_maps.h"
|
2013-07-24 17:14:22 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-07-26 18:43:53 +00:00
|
|
|
/** \brief Implements deep copy of kernel expressions. */
|
2013-07-24 17:14:22 +00:00
|
|
|
class deep_copy_fn {
|
|
|
|
expr_cell_map<expr> m_cache;
|
|
|
|
|
2013-12-08 07:21:07 +00:00
|
|
|
expr save_result(expr const & a, expr && r, bool shared) {
|
|
|
|
if (shared)
|
|
|
|
m_cache.insert(std::make_pair(a.raw(), r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<expr> apply(optional<expr> const & a) {
|
|
|
|
if (a)
|
2013-12-08 18:34:38 +00:00
|
|
|
return some_expr(apply(*a));
|
2013-12-08 07:21:07 +00:00
|
|
|
else
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-07-24 17:14:22 +00:00
|
|
|
expr apply(expr const & a) {
|
2013-07-30 03:59:15 +00:00
|
|
|
bool sh = false;
|
2013-07-24 17:14:22 +00:00
|
|
|
if (is_shared(a)) {
|
|
|
|
auto r = m_cache.find(a.raw());
|
|
|
|
if (r != m_cache.end())
|
|
|
|
return r->second;
|
2013-07-30 03:59:15 +00:00
|
|
|
sh = true;
|
2013-07-24 17:14:22 +00:00
|
|
|
}
|
|
|
|
switch (a.kind()) {
|
2013-08-03 23:09:21 +00:00
|
|
|
case expr_kind::Var: case expr_kind::Constant: case expr_kind::Type: case expr_kind::Value:
|
2013-12-08 07:21:07 +00:00
|
|
|
return save_result(a, copy(a), sh);
|
2013-07-24 17:14:22 +00:00
|
|
|
case expr_kind::App: {
|
2013-07-24 21:41:03 +00:00
|
|
|
buffer<expr> new_args;
|
2013-07-24 17:14:22 +00:00
|
|
|
for (expr const & old_arg : args(a))
|
|
|
|
new_args.push_back(apply(old_arg));
|
2013-12-08 07:21:07 +00:00
|
|
|
return save_result(a, mk_app(new_args), sh);
|
2013-07-24 17:14:22 +00:00
|
|
|
}
|
2013-12-08 07:21:07 +00:00
|
|
|
case expr_kind::Lambda: return save_result(a, mk_lambda(abst_name(a), apply(abst_domain(a)), apply(abst_body(a))), sh);
|
|
|
|
case expr_kind::Pi: return save_result(a, mk_pi(abst_name(a), apply(abst_domain(a)), apply(abst_body(a))), sh);
|
|
|
|
case expr_kind::Let: return save_result(a, mk_let(let_name(a), apply(let_type(a)), apply(let_value(a)), apply(let_body(a))), sh);
|
2013-09-13 01:25:38 +00:00
|
|
|
case expr_kind::MetaVar:
|
2013-12-08 07:21:07 +00:00
|
|
|
return save_result(a,
|
|
|
|
update_metavar(a, [&](local_entry const & e) -> local_entry {
|
|
|
|
if (e.is_inst())
|
|
|
|
return mk_inst(e.s(), apply(e.v()));
|
|
|
|
else
|
|
|
|
return e;
|
|
|
|
}),
|
|
|
|
sh);
|
2013-07-24 17:14:22 +00:00
|
|
|
}
|
2013-12-08 07:21:07 +00:00
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2013-07-24 17:14:22 +00:00
|
|
|
}
|
|
|
|
public:
|
2013-07-26 18:43:53 +00:00
|
|
|
/**
|
|
|
|
\brief Return a new expression that is equal to the given
|
|
|
|
argument, but does not share any memory cell with it.
|
|
|
|
*/
|
2013-07-24 17:14:22 +00:00
|
|
|
expr operator()(expr const & a) { return apply(a); }
|
|
|
|
};
|
|
|
|
|
|
|
|
expr deep_copy(expr const & e) {
|
|
|
|
return deep_copy_fn()(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|