feat(library/tactic): add 'clear' tactic, closes #341

This commit is contained in:
Leonardo de Moura 2014-11-26 13:09:42 -08:00
parent ffdeb0edc4
commit c28e9b9234
7 changed files with 123 additions and 1 deletions

View file

@ -7,6 +7,7 @@ Author: Leonardo de Moura
#include "util/name_set.h"
#include "kernel/expr.h"
#include "kernel/for_each_fn.h"
#include "kernel/find_fn.h"
namespace lean {
void collect_univ_params_core(level const & l, name_set & r) {
@ -58,4 +59,21 @@ void collect_locals(expr const & e, expr_struct_set & ls) {
return true;
});
}
bool contains_local(expr const & e, name const & n) {
if (!has_local(e))
return false;
bool result = false;
for_each(e, [&](expr const & e, unsigned) {
if (result || !has_local(e)) {
return false;
} else if (is_local(e) && mlocal_name(e) == n) {
result = true;
return false;
} else {
return true;
}
});
return result;
}
}

View file

@ -22,4 +22,7 @@ template<typename It> bool contains_local(expr const & local, It const & begin_l
inline bool contains_local(expr const & local, buffer<expr> const & locals) {
return contains_local(local, locals.begin(), locals.end());
}
/** \brief Return true iff \c e contains a local constant named \c n (it uses mlocal_name) */
bool contains_local(expr const & e, name const & n);
}

View file

@ -1,6 +1,6 @@
add_library(tactic goal.cpp proof_state.cpp tactic.cpp elaborate.cpp
apply_tactic.cpp intros_tactic.cpp rename_tactic.cpp trace_tactic.cpp
exact_tactic.cpp unfold_tactic.cpp generalize_tactic.cpp whnf_tactic.cpp
expr_to_tactic.cpp util.cpp init_module.cpp)
clear_tactic.cpp expr_to_tactic.cpp util.cpp init_module.cpp)
target_link_libraries(tactic ${LEAN_LIBS})

View file

@ -0,0 +1,67 @@
/*
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/abstract.h"
#include "library/locals.h"
#include "library/tactic/tactic.h"
#include "library/tactic/expr_to_tactic.h"
namespace lean {
tactic clear_tactic(name const & n) {
auto fn = [=](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 g = head(gs);
goals tail_gs = tail(gs);
expr meta = g.get_meta();
buffer<expr> locals;
get_app_args(meta, locals);
unsigned i = locals.size();
while (i > 0) {
--i;
if (local_pp_name(locals[i]) == n) {
// found target
name real_n = mlocal_name(locals[i]);
for (unsigned j = i+1; j < locals.size(); j++) {
if (contains_local(mlocal_type(locals[j]), real_n))
return none_proof_state(); // other variables depends on n
}
if (contains_local(g.get_type(), real_n))
return none_proof_state(); // type depends on n
buffer<expr> new_locals;
for (unsigned j = 0; j < i; j++)
new_locals.push_back(locals[j]);
for (unsigned j = i+1; j < locals.size(); j++)
new_locals.push_back(locals[j]);
name_generator ngen = s.get_ngen();
expr new_meta = mk_app(mk_metavar(ngen.next(), Pi(new_locals, g.get_type())), new_locals);
goal new_g(new_meta, g.get_type());
expr val = Fun(locals, new_meta);
substitution new_subst = s.get_subst();
new_subst.assign(g.get_name(), val);
proof_state new_s(s, goals(new_g, tail_gs), new_subst, ngen);
return some_proof_state(new_s);
}
}
return none_proof_state();
};
return tactic01(fn);
}
static name const & get_clear_arg(expr const & e) {
return tactic_expr_to_id(e, "invalid 'clear' tactic, argument must be an identifier");
}
void initialize_clear_tactic() {
register_tac(name({"tactic", "clear"}),
[](type_checker &, elaborate_fn const &, expr const & e, pos_info_provider const *) {
name n = get_clear_arg(app_arg(e));
return clear_tactic(n);
});
}
void finalize_clear_tactic() {}
}

View file

@ -0,0 +1,14 @@
/*
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 clear_tactic(name const & n);
void initialize_clear_tactic();
void finalize_clear_tactic();
}

View file

@ -15,6 +15,7 @@ Author: Leonardo de Moura
#include "library/tactic/unfold_tactic.h"
#include "library/tactic/generalize_tactic.h"
#include "library/tactic/whnf_tactic.h"
#include "library/tactic/clear_tactic.h"
namespace lean {
void initialize_tactic_module() {
@ -29,9 +30,11 @@ void initialize_tactic_module() {
initialize_unfold_tactic();
initialize_generalize_tactic();
initialize_whnf_tactic();
initialize_clear_tactic();
}
void finalize_tactic_module() {
finalize_clear_tactic();
finalize_whnf_tactic();
finalize_generalize_tactic();
finalize_unfold_tactic();

View file

@ -0,0 +1,17 @@
import logic
example {a b c : Prop} : a → b → c → a ∧ b :=
begin
intros (Ha, Hb, Hc),
clear Hc,
clear c,
apply (and.intro Ha Hb),
end
example {a b c : Prop} : a → b → c → c ∧ b :=
begin
intros (Ha, Hb, Hc),
clear Ha,
clear a,
apply (and.intro Hc Hb),
end