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
|
|
|
|
*/
|
|
|
|
#include "expr.h"
|
|
|
|
#include "maps.h"
|
2013-07-24 21:41:03 +00:00
|
|
|
#include "buffer.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) {
|
|
|
|
if (is_shared(a)) {
|
|
|
|
auto r = m_cache.find(a.raw());
|
|
|
|
if (r != m_cache.end())
|
|
|
|
return r->second;
|
|
|
|
}
|
|
|
|
expr r;
|
|
|
|
switch (a.kind()) {
|
2013-07-30 02:49:34 +00:00
|
|
|
case expr_kind::Var: case expr_kind::Constant: case expr_kind::Type: case expr_kind::Numeral:
|
2013-07-24 17:14:22 +00:00
|
|
|
r = copy(a); break; // shallow copy is equivalent to deep copy for these ones.
|
|
|
|
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));
|
|
|
|
r = app(new_args.size(), new_args.data());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case expr_kind::Lambda: r = lambda(abst_name(a), apply(abst_type(a)), apply(abst_body(a))); break;
|
|
|
|
case expr_kind::Pi: r = pi(abst_name(a), apply(abst_type(a)), apply(abst_body(a))); break;
|
|
|
|
}
|
|
|
|
if (is_shared(a))
|
|
|
|
m_cache.insert(std::make_pair(a.raw(), r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|