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;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2014-02-23 06:53:34 +00:00
|
|
|
expr r;
|
2013-07-24 17:14:22 +00:00
|
|
|
switch (a.kind()) {
|
2014-02-23 06:53:34 +00:00
|
|
|
case expr_kind::Var:
|
|
|
|
case expr_kind::Constant:
|
|
|
|
case expr_kind::Sort:
|
|
|
|
case expr_kind::Macro: r = copy(a); break;
|
|
|
|
case expr_kind::App: r = mk_app(apply(app_fn(a)), apply(app_arg(a))); break;
|
2014-05-16 18:13:50 +00:00
|
|
|
case expr_kind::Lambda: r = mk_lambda(binding_name(a), apply(binding_domain(a)), apply(binding_body(a))); break;
|
|
|
|
case expr_kind::Pi: r = mk_pi(binding_name(a), apply(binding_domain(a)), apply(binding_body(a))); break;
|
2014-02-23 06:53:34 +00:00
|
|
|
case expr_kind::Let: r = mk_let(let_name(a), apply(let_type(a)), apply(let_value(a)), apply(let_body(a))); break;
|
|
|
|
case expr_kind::Meta: r = mk_metavar(mlocal_name(a), apply(mlocal_type(a))); break;
|
2014-05-16 20:08:09 +00:00
|
|
|
case expr_kind::Local: r = mk_local(mlocal_name(a), local_pp_name(a), apply(mlocal_type(a))); break;
|
2013-07-24 17:14:22 +00:00
|
|
|
}
|
2014-02-23 06:53:34 +00:00
|
|
|
if (sh)
|
|
|
|
m_cache.insert(std::make_pair(a.raw(), r));
|
|
|
|
return r;
|
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); }
|
|
|
|
};
|
2014-02-23 06:53:34 +00:00
|
|
|
expr deep_copy(expr const & e) { return deep_copy_fn()(e); }
|
2013-07-24 17:14:22 +00:00
|
|
|
}
|