2013-10-23 22:01:33 +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-10-26 18:07:06 +00:00
|
|
|
#include <tuple>
|
2013-12-05 13:42:12 +00:00
|
|
|
#include "util/interrupt.h"
|
2014-10-17 17:23:35 +00:00
|
|
|
#include "library/replace_visitor.h"
|
2013-10-23 22:01:33 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-05-14 22:53:09 +00:00
|
|
|
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_constant(expr const & e) { lean_assert(is_constant(e)); return e; }
|
|
|
|
expr replace_visitor::visit_mlocal(expr const & e) {
|
2014-02-19 07:15:55 +00:00
|
|
|
lean_assert(is_mlocal(e));
|
2014-05-14 22:53:09 +00:00
|
|
|
return update_mlocal(e, visit(mlocal_type(e)));
|
2013-11-19 19:21:45 +00:00
|
|
|
}
|
2014-05-14 22:53:09 +00:00
|
|
|
expr replace_visitor::visit_meta(expr const & e) { return visit_mlocal(e); }
|
|
|
|
expr replace_visitor::visit_local(expr const & e) { return visit_mlocal(e); }
|
|
|
|
expr replace_visitor::visit_app(expr const & e) {
|
2013-10-23 22:01:33 +00:00
|
|
|
lean_assert(is_app(e));
|
2014-05-14 22:53:09 +00:00
|
|
|
return update_app(e, visit(app_fn(e)), visit(app_arg(e)));
|
2013-10-23 22:01:33 +00:00
|
|
|
}
|
2014-05-17 18:37:27 +00:00
|
|
|
expr replace_visitor::visit_binding(expr const & e) {
|
|
|
|
lean_assert(is_binding(e));
|
2014-05-16 18:13:50 +00:00
|
|
|
expr new_d = visit(binding_domain(e));
|
|
|
|
expr new_b = visit(binding_body(e));
|
2014-05-17 18:37:27 +00:00
|
|
|
return update_binding(e, new_d, new_b);
|
2014-02-04 00:52:49 +00:00
|
|
|
}
|
2014-05-17 18:37:27 +00:00
|
|
|
expr replace_visitor::visit_lambda(expr const & e) { return visit_binding(e); }
|
|
|
|
expr replace_visitor::visit_pi(expr const & e) { return visit_binding(e); }
|
2014-05-14 22:53:09 +00:00
|
|
|
expr replace_visitor::visit_macro(expr const & e) {
|
2014-04-25 22:02:52 +00:00
|
|
|
lean_assert(is_macro(e));
|
|
|
|
buffer<expr> new_args;
|
|
|
|
for (unsigned i = 0; i < macro_num_args(e); i++)
|
2014-05-14 22:53:09 +00:00
|
|
|
new_args.push_back(visit(macro_arg(e, i)));
|
2014-04-25 22:02:52 +00:00
|
|
|
return update_macro(e, new_args.size(), new_args.data());
|
|
|
|
}
|
2013-12-08 07:21:07 +00:00
|
|
|
expr replace_visitor::save_result(expr const & e, expr && r, bool shared) {
|
|
|
|
if (shared)
|
|
|
|
m_cache.insert(std::make_pair(e, r));
|
|
|
|
return r;
|
|
|
|
}
|
2014-05-14 22:53:09 +00:00
|
|
|
expr replace_visitor::visit(expr const & e) {
|
2013-12-07 22:59:21 +00:00
|
|
|
check_system("expression replacer");
|
2013-10-23 22:01:33 +00:00
|
|
|
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()) {
|
2014-05-14 22:53:09 +00:00
|
|
|
case expr_kind::Sort: return save_result(e, visit_sort(e), shared);
|
|
|
|
case expr_kind::Macro: return save_result(e, visit_macro(e), shared);
|
|
|
|
case expr_kind::Constant: return save_result(e, visit_constant(e), shared);
|
|
|
|
case expr_kind::Var: return save_result(e, visit_var(e), shared);
|
|
|
|
case expr_kind::Meta: return save_result(e, visit_meta(e), shared);
|
|
|
|
case expr_kind::Local: return save_result(e, visit_local(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);
|
2013-10-23 22:01:33 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 07:21:07 +00:00
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
|
|
|
}
|
2013-10-23 22:01:33 +00:00
|
|
|
}
|