refactor(library/tactic): move rename_tactic to separate module

This commit is contained in:
Leonardo de Moura 2014-10-20 17:41:40 -07:00
parent ac9397816f
commit 3c4419ff23
9 changed files with 88 additions and 52 deletions

View file

@ -6,6 +6,8 @@ Author: Leonardo de Moura
*/
#include <limits>
#include "library/explicit.h"
#include "library/tactic/apply_tactic.h"
#include "library/tactic/rename_tactic.h"
#include "library/tactic/expr_to_tactic.h"
#include "frontends/lean/parser.h"
#include "frontends/lean/parse_table.h"

View file

@ -1,4 +1,4 @@
add_library(tactic goal.cpp proof_state.cpp tactic.cpp apply_tactic.cpp
intros_tactic.cpp expr_to_tactic.cpp init_module.cpp)
intros_tactic.cpp rename_tactic.cpp expr_to_tactic.cpp init_module.cpp)
target_link_libraries(tactic ${LEAN_LIBS})

View file

@ -11,7 +11,7 @@ namespace lean {
tactic apply_tactic(elaborate_fn const & fn, expr const & e, bool relax_main_opaque = true);
tactic eassumption_tactic(bool relax_main_opaque = true);
void open_apply_tactic(lua_State * L);
expr mk_apply_tactic_macro(expr const & e);
void initialize_apply_tactic();
void finalize_apply_tactic();
}

View file

@ -143,14 +143,8 @@ bool is_tactic_macro(expr const & e) {
return is_macro(e) && macro_def(e).get_name() == get_tactic_name();
}
static name * g_rename_tactic_name = nullptr;
static name * g_intros_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);
}
expr mk_intros_tactic_macro(buffer<name> const & ns) {
buffer<expr> args;
for (name const & n : ns) {
@ -323,7 +317,6 @@ void initialize_expr_to_tactic() {
return mk_tactic_macro(kind, num, args);
});
g_rename_tactic_name = new name(*g_tactic_name, "rename");
g_intros_tactic_name = new name(*g_tactic_name, "intros");
name builtin_tac_name(*g_tactic_name, "builtin");
@ -386,13 +379,6 @@ void initialize_expr_to_tactic() {
else
throw expr_to_tactic_exception(e, "invalid trace tactic, string value expected");
});
register_tacm(*g_rename_tactic_name,
[](type_checker &, elaborate_fn const &, expr const & e, pos_info_provider const *) {
check_macro_args(e, 2, "invalid 'rename' tactic, it must have two arguments");
if (!is_constant(macro_arg(e, 0)) || !is_constant(macro_arg(e, 1)))
throw expr_to_tactic_exception(e, "invalid 'rename' tactic, arguments must be identifiers");
return rename_tactic(const_name(macro_arg(e, 0)), const_name(macro_arg(e, 1)));
});
register_tacm(*g_intros_tactic_name,
[](type_checker &, elaborate_fn const &, expr const & e, pos_info_provider const *) {
buffer<name> ns;
@ -444,7 +430,6 @@ void finalize_expr_to_tactic() {
delete g_and_then_tac_fn;
delete g_id_tac_fn;
delete g_exact_tac_fn;
delete g_rename_tactic_name;
delete g_intros_tactic_name;
delete g_tactic_macros;
delete g_map;

View file

@ -52,8 +52,6 @@ expr mk_tactic_macro(name const & kind, unsigned num_args, expr const * args);
expr mk_tactic_macro(name const & kind, expr const & e);
bool is_tactic_macro(expr const & e);
expr mk_apply_tactic_macro(expr const & e);
expr mk_rename_tactic_macro(name const & from, name const & to);
expr mk_intros_tactic_macro(buffer<name> const & ns);
/** \brief Exception used to report a problem when an expression is being converted into a tactic. */

View file

@ -7,15 +7,18 @@ Author: Leonardo de Moura
#include "library/tactic/proof_state.h"
#include "library/tactic/expr_to_tactic.h"
#include "library/tactic/apply_tactic.h"
#include "library/tactic/rename_tactic.h"
namespace lean {
void initialize_tactic_module() {
initialize_proof_state();
initialize_expr_to_tactic();
initialize_apply_tactic();
initialize_rename_tactic();
}
void finalize_tactic_module() {
finalize_rename_tactic();
finalize_apply_tactic();
finalize_expr_to_tactic();
finalize_proof_state();

View file

@ -0,0 +1,66 @@
/*
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<proof_state> {
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<expr> locals;
get_app_args(g.get_meta(), locals);
unsigned i = locals.size();
optional<expr> 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);
}
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 *) {
check_macro_args(e, 2, "invalid 'rename' tactic, it must have two arguments");
if (!is_constant(macro_arg(e, 0)) || !is_constant(macro_arg(e, 1)))
throw expr_to_tactic_exception(e, "invalid 'rename' tactic, arguments must be identifiers");
return rename_tactic(const_name(macro_arg(e, 0)), const_name(macro_arg(e, 1)));
};
register_tactic_macro(*g_rename_tactic_name, fn);
}
void finalize_rename_tactic() {
delete g_rename_tactic_name;
}
}

View file

@ -0,0 +1,15 @@
/*
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#pragma once
#include "library/tactic/tactic.h"
namespace lean {
tactic rename_tactic(name const & from, name const & to);
expr mk_rename_tactic_macro(name const & from, name const & to);
void initialize_rename_tactic();
void finalize_rename_tactic();
}

View file

@ -212,39 +212,6 @@ tactic discard(tactic const & t, unsigned k) {
});
}
tactic rename_tactic(name const & from, name const & to) {
return tactic01([=](environment const &, io_state const &, proof_state const & s) -> optional<proof_state> {
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<expr> locals;
get_app_args(g.get_meta(), locals);
unsigned i = locals.size();
optional<expr> 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)));
});
}
tactic assumption_tactic() {
return tactic01([](environment const &, io_state const &, proof_state const & s) -> optional<proof_state> {
substitution subst = s.get_subst();