feat(library/tactic): add 'revert' tactic, closes #346
This commit is contained in:
parent
c28e9b9234
commit
2bd8f969d5
6 changed files with 114 additions and 2 deletions
|
@ -65,6 +65,7 @@ opaque definition generalize_list (es : expr_list) : tactic := builtin
|
|||
notation `generalizes` `(` l:(foldr `,` (h t, expr_list.cons h t) expr_list.nil) `)` := generalize_list l
|
||||
|
||||
opaque definition clear (e : expr) : tactic := builtin
|
||||
opaque definition revert (e : expr) : tactic := builtin
|
||||
|
||||
opaque definition unfold (e : expr) : tactic := builtin
|
||||
opaque definition exact (e : expr) : tactic := builtin
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
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
|
||||
clear_tactic.cpp expr_to_tactic.cpp util.cpp init_module.cpp)
|
||||
exact_tactic.cpp unfold_tactic.cpp generalize_tactic.cpp
|
||||
whnf_tactic.cpp revert_tactic.cpp clear_tactic.cpp expr_to_tactic.cpp
|
||||
util.cpp init_module.cpp)
|
||||
|
||||
target_link_libraries(tactic ${LEAN_LIBS})
|
||||
|
|
|
@ -16,6 +16,7 @@ Author: Leonardo de Moura
|
|||
#include "library/tactic/generalize_tactic.h"
|
||||
#include "library/tactic/whnf_tactic.h"
|
||||
#include "library/tactic/clear_tactic.h"
|
||||
#include "library/tactic/revert_tactic.h"
|
||||
|
||||
namespace lean {
|
||||
void initialize_tactic_module() {
|
||||
|
@ -31,9 +32,11 @@ void initialize_tactic_module() {
|
|||
initialize_generalize_tactic();
|
||||
initialize_whnf_tactic();
|
||||
initialize_clear_tactic();
|
||||
initialize_revert_tactic();
|
||||
}
|
||||
|
||||
void finalize_tactic_module() {
|
||||
finalize_revert_tactic();
|
||||
finalize_clear_tactic();
|
||||
finalize_whnf_tactic();
|
||||
finalize_generalize_tactic();
|
||||
|
|
66
src/library/tactic/revert_tactic.cpp
Normal file
66
src/library/tactic/revert_tactic.cpp
Normal 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/abstract.h"
|
||||
#include "library/locals.h"
|
||||
#include "library/tactic/tactic.h"
|
||||
#include "library/tactic/expr_to_tactic.h"
|
||||
|
||||
namespace lean {
|
||||
tactic revert_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
|
||||
}
|
||||
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_type = Pi(locals[i], g.get_type());
|
||||
expr new_meta = mk_app(mk_metavar(ngen.next(), Pi(new_locals, new_type)), new_locals);
|
||||
goal new_g(new_meta, new_type);
|
||||
expr val = Fun(locals, mk_app(new_meta, locals[i]));
|
||||
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_revert_arg(expr const & e) {
|
||||
return tactic_expr_to_id(e, "invalid 'revert' tactic, argument must be an identifier");
|
||||
}
|
||||
|
||||
void initialize_revert_tactic() {
|
||||
register_tac(name({"tactic", "revert"}),
|
||||
[](type_checker &, elaborate_fn const &, expr const & e, pos_info_provider const *) {
|
||||
name n = get_revert_arg(app_arg(e));
|
||||
return revert_tactic(n);
|
||||
});
|
||||
}
|
||||
void finalize_revert_tactic() {}
|
||||
}
|
14
src/library/tactic/revert_tactic.h
Normal file
14
src/library/tactic/revert_tactic.h
Normal 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 revert_tactic(name const & n);
|
||||
void initialize_revert_tactic();
|
||||
void finalize_revert_tactic();
|
||||
}
|
27
tests/lean/run/revert_tac.lean
Normal file
27
tests/lean/run/revert_tac.lean
Normal file
|
@ -0,0 +1,27 @@
|
|||
import logic
|
||||
|
||||
theorem tst {a b c : Prop} : a → b → c → a ∧ b :=
|
||||
begin
|
||||
intros (Ha, Hb, Hc),
|
||||
revert Ha,
|
||||
intro Ha2,
|
||||
apply (and.intro Ha2 Hb),
|
||||
end
|
||||
|
||||
theorem foo1 {A : Type} (a b c : A) (P : A → Prop) : P a → a = b → P b :=
|
||||
begin
|
||||
intros (Hp, Heq),
|
||||
revert Hp,
|
||||
apply (eq.rec_on Heq),
|
||||
intro Hpa,
|
||||
apply Hpa
|
||||
end
|
||||
|
||||
theorem foo2 {A : Type} (a b c : A) (P : A → Prop) : P a → a = b → P b :=
|
||||
begin
|
||||
intros (Hp, Heq),
|
||||
apply (eq.rec_on Heq Hp)
|
||||
end
|
||||
|
||||
print definition foo1
|
||||
print definition foo2
|
Loading…
Add table
Reference in a new issue