/* Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura */ #include "kernel/replace_fn.h" #include "library/tactic/tactic.h" #include "library/tactic/expr_to_tactic.h" namespace lean { tactic rename_tactic(name const & from, name const & to) { return tactic01([=](environment const &, io_state const &, proof_state const & s) -> optional { goals const & gs = s.get_goals(); if (empty(gs)) return none_proof_state(); goal const & g = head(gs); goals const & rest_gs = tail(gs); buffer locals; get_app_args(g.get_meta(), locals); unsigned i = locals.size(); optional from_local; while (i > 0) { --i; expr const & local = locals[i]; if (local_pp_name(local) == from) { from_local = local; break; } } if (!from_local) return none_proof_state(); expr to_local = mk_local(mlocal_name(*from_local), to, mlocal_type(*from_local), local_info(*from_local)); auto fn = [&](expr const & e) { if (is_local(e) && mlocal_name(e) == mlocal_name(*from_local)) return some_expr(to_local); else return none_expr(); }; goal new_g(replace(g.get_meta(), fn), replace(g.get_type(), fn)); return some(proof_state(s, goals(new_g, rest_gs))); }); } static name * g_rename_tactic_name = nullptr; expr mk_rename_tactic_macro(name const & from, name const & to) { expr args[2] = { Const(from), Const(to) }; return mk_tactic_macro(*g_rename_tactic_name, 2, args); } static name const & get_rename_arg(expr const & _e) { check_tactic_expr(_e, "invalid 'rename' tactic, arguments must be identifiers"); expr const & e = get_tactic_expr_expr(_e); if (is_constant(e)) return const_name(e); else if (is_local(e)) return local_pp_name(e); else throw expr_to_tactic_exception(e, "invalid 'rename' tactic, arguments must be identifiers"); } void initialize_rename_tactic() { g_rename_tactic_name = new name({"tactic", "rename"}); auto fn = [](type_checker &, elaborate_fn const &, expr const & e, pos_info_provider const *) { return rename_tactic(get_rename_arg(app_arg(app_fn(e))), get_rename_arg(app_arg(e))); }; register_tac(*g_rename_tactic_name, fn); } void finalize_rename_tactic() { delete g_rename_tactic_name; } }