2013-12-05 11:22:12 +00:00
|
|
|
/*
|
2014-06-30 01:26:07 +00:00
|
|
|
Copyright (c) 2013-2014 Microsoft Corporation. All rights reserved.
|
2013-12-05 11:22:12 +00:00
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include <utility>
|
2014-06-30 01:26:07 +00:00
|
|
|
#include "util/lazy_list_fn.h"
|
2014-07-02 21:09:01 +00:00
|
|
|
#include "util/sstream.h"
|
2014-07-08 23:55:11 +00:00
|
|
|
#include "util/name_map.h"
|
2014-06-30 01:26:07 +00:00
|
|
|
#include "kernel/for_each_fn.h"
|
2014-07-08 23:55:11 +00:00
|
|
|
#include "kernel/replace_fn.h"
|
2013-12-05 11:22:12 +00:00
|
|
|
#include "kernel/instantiate.h"
|
2013-12-24 22:23:06 +00:00
|
|
|
#include "kernel/abstract.h"
|
2014-06-30 01:26:07 +00:00
|
|
|
#include "kernel/type_checker.h"
|
2013-12-05 11:22:12 +00:00
|
|
|
#include "library/kernel_bindings.h"
|
2014-06-30 01:26:07 +00:00
|
|
|
#include "library/unifier.h"
|
2014-07-03 19:59:48 +00:00
|
|
|
#include "library/occurs.h"
|
2013-12-05 11:22:12 +00:00
|
|
|
#include "library/tactic/apply_tactic.h"
|
|
|
|
|
|
|
|
namespace lean {
|
2013-12-25 06:40:34 +00:00
|
|
|
/**
|
2014-06-30 01:26:07 +00:00
|
|
|
\brief Traverse \c e and collect metavariable applications (?m l1 ... ln), and store in result.
|
|
|
|
The function only succeeds if all metavariable applications are "simple", i.e., the arguments
|
|
|
|
are distinct local constants.
|
2013-12-25 06:40:34 +00:00
|
|
|
*/
|
2014-06-30 01:26:07 +00:00
|
|
|
bool collect_simple_metas(expr const & e, buffer<expr> & result) {
|
|
|
|
bool failed = false;
|
|
|
|
// collect metavariables
|
|
|
|
for_each(e, [&](expr const & e, unsigned) {
|
|
|
|
if (is_meta(e)) {
|
|
|
|
if (!is_simple_meta(e)) {
|
|
|
|
failed = true;
|
|
|
|
} else {
|
|
|
|
result.push_back(e);
|
|
|
|
return false; /* do not visit type */
|
2013-12-25 06:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-30 01:26:07 +00:00
|
|
|
return !failed && has_metavar(e);
|
2013-12-25 06:40:34 +00:00
|
|
|
});
|
2014-06-30 01:26:07 +00:00
|
|
|
return !failed;
|
2013-12-25 06:40:34 +00:00
|
|
|
}
|
|
|
|
|
2014-07-02 20:14:50 +00:00
|
|
|
unsigned get_expect_num_args(type_checker & tc, expr e) {
|
|
|
|
unsigned r = 0;
|
|
|
|
while (true) {
|
2014-08-20 05:31:26 +00:00
|
|
|
e = tc.whnf(e).first;
|
2014-07-02 20:14:50 +00:00
|
|
|
if (!is_pi(e))
|
|
|
|
return r;
|
|
|
|
e = binding_body(e);
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void collect_simple_meta(expr const & e, buffer<expr> & metas) {
|
|
|
|
for_each(e, [&](expr const & e, unsigned) {
|
|
|
|
if (is_meta(e)) {
|
|
|
|
if (is_simple_meta(e))
|
|
|
|
metas.push_back(e);
|
|
|
|
return false; /* do not visit its type */
|
2013-12-25 06:40:34 +00:00
|
|
|
}
|
2014-07-02 20:14:50 +00:00
|
|
|
return has_metavar(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-03 19:59:48 +00:00
|
|
|
/**
|
|
|
|
\brief Given a sequence metas: <tt>(?m_1 ...) (?m_2 ... ) ... (?m_k ...)</tt>,
|
|
|
|
we say ?m_i is "redundant" if it occurs in the type of some ?m_j.
|
|
|
|
This procedure removes from metas any redundant element.
|
|
|
|
*/
|
|
|
|
static void remove_redundant_metas(buffer<expr> & metas) {
|
|
|
|
buffer<expr> mvars;
|
|
|
|
for (expr const & m : metas)
|
|
|
|
mvars.push_back(get_app_fn(m));
|
|
|
|
unsigned k = 0;
|
|
|
|
for (unsigned i = 0; i < metas.size(); i++) {
|
|
|
|
bool found = false;
|
|
|
|
for (unsigned j = 0; j < metas.size(); j++) {
|
|
|
|
if (j != i) {
|
|
|
|
if (occurs(mvars[i], mlocal_type(mvars[j]))) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
metas[k] = metas[i];
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
metas.shrink(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
proof_state_seq apply_tactic_core(environment const & env, io_state const & ios, proof_state const & s, expr const & _e,
|
2014-07-27 19:01:06 +00:00
|
|
|
bool add_meta, bool add_subgoals, bool relax_main_opaque) {
|
2014-08-20 05:31:26 +00:00
|
|
|
// TODO(Leo): we are ignoring constraints produces by type checker
|
2014-07-03 19:59:48 +00:00
|
|
|
goals const & gs = s.get_goals();
|
|
|
|
if (empty(gs))
|
|
|
|
return proof_state_seq();
|
|
|
|
name_generator ngen = s.get_ngen();
|
|
|
|
type_checker tc(env, ngen.mk_child());
|
|
|
|
goal g = head(gs);
|
|
|
|
goals tail_gs = tail(gs);
|
|
|
|
expr t = g.get_type();
|
|
|
|
expr e = _e;
|
2014-08-20 05:31:26 +00:00
|
|
|
expr e_t = tc.infer(e).first;
|
2014-07-03 19:59:48 +00:00
|
|
|
buffer<expr> metas;
|
|
|
|
collect_simple_meta(e, metas);
|
|
|
|
if (add_meta) {
|
|
|
|
unsigned num_t = get_expect_num_args(tc, t);
|
|
|
|
unsigned num_e_t = get_expect_num_args(tc, e_t);
|
|
|
|
if (num_t > num_e_t)
|
|
|
|
return proof_state_seq(); // no hope to unify then
|
|
|
|
for (unsigned i = 0; i < num_e_t - num_t; i++) {
|
2014-08-20 05:31:26 +00:00
|
|
|
e_t = tc.whnf(e_t).first;
|
2014-07-03 19:59:48 +00:00
|
|
|
expr meta = g.mk_meta(ngen.next(), binding_domain(e_t));
|
|
|
|
e = mk_app(e, meta);
|
|
|
|
e_t = instantiate(binding_body(e_t), meta);
|
|
|
|
metas.push_back(meta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list<expr> meta_lst = to_list(metas.begin(), metas.end());
|
2014-08-21 00:30:08 +00:00
|
|
|
lazy_list<substitution> substs = unify(env, t, e_t, ngen.mk_child(), relax_main_opaque, s.get_subst(),
|
|
|
|
unifier_config(ios.get_options()));
|
2014-07-03 19:59:48 +00:00
|
|
|
return map2<proof_state>(substs, [=](substitution const & subst) -> proof_state {
|
|
|
|
name_generator new_ngen(ngen);
|
|
|
|
type_checker tc(env, new_ngen.mk_child());
|
2014-07-23 15:51:24 +00:00
|
|
|
substitution new_subst = subst;
|
2014-07-23 21:21:47 +00:00
|
|
|
expr new_e = new_subst.instantiate_all(e);
|
2014-07-03 19:59:48 +00:00
|
|
|
expr new_p = g.abstract(new_e);
|
|
|
|
check_has_no_local(new_p, _e, "apply");
|
2014-07-23 15:51:24 +00:00
|
|
|
new_subst.assign(g.get_name(), new_p);
|
2014-07-03 19:59:48 +00:00
|
|
|
goals new_gs = tail_gs;
|
|
|
|
if (add_subgoals) {
|
|
|
|
buffer<expr> metas;
|
|
|
|
for (auto m : meta_lst) {
|
|
|
|
if (!new_subst.is_assigned(get_app_fn(m)))
|
|
|
|
metas.push_back(m);
|
|
|
|
}
|
|
|
|
remove_redundant_metas(metas);
|
|
|
|
unsigned i = metas.size();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
2014-08-20 05:31:26 +00:00
|
|
|
new_gs = cons(goal(metas[i], new_subst.instantiate_all(tc.infer(metas[i]).first)), new_gs);
|
2014-07-03 19:59:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return proof_state(new_gs, new_subst, new_ngen);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-08 23:55:11 +00:00
|
|
|
|
|
|
|
level refresh_univ_metavars(level const & l, name_generator & ngen, name_map<level> & level_map) {
|
|
|
|
return replace(l, [&](level const & l) {
|
|
|
|
if (!has_meta(l))
|
|
|
|
return some_level(l);
|
|
|
|
if (is_meta(l)) {
|
|
|
|
name id = meta_id(l);
|
|
|
|
if (auto it = level_map.find(id))
|
|
|
|
return some_level(*it);
|
|
|
|
level r = mk_meta_univ(ngen.next());
|
|
|
|
level_map.insert(id, r);
|
|
|
|
return some_level(r);
|
|
|
|
}
|
|
|
|
return none_level();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
expr refresh_univ_metavars(expr const & e, name_generator & ngen) {
|
|
|
|
if (has_univ_metavar(e)) {
|
|
|
|
name_map<level> level_map;
|
2014-07-19 11:53:01 +00:00
|
|
|
return replace(e, [&](expr const & e) {
|
2014-07-08 23:55:11 +00:00
|
|
|
if (!has_univ_metavar(e))
|
|
|
|
return some_expr(e);
|
|
|
|
if (is_sort(e))
|
|
|
|
return some_expr(update_sort(e, refresh_univ_metavars(sort_level(e), ngen, level_map)));
|
|
|
|
if (is_constant(e))
|
|
|
|
return some_expr(update_constant(e, map(const_levels(e), [&](level const & l) {
|
|
|
|
return refresh_univ_metavars(l, ngen, level_map);
|
|
|
|
})));
|
|
|
|
return none_expr();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-27 19:01:06 +00:00
|
|
|
tactic apply_tactic(expr const & e, bool relax_main_opaque, bool refresh_univ_mvars) {
|
2014-07-03 19:59:48 +00:00
|
|
|
return tactic([=](environment const & env, io_state const & ios, proof_state const & s) {
|
2014-07-08 23:55:11 +00:00
|
|
|
if (refresh_univ_mvars) {
|
2014-07-23 15:51:24 +00:00
|
|
|
name_generator ngen = s.get_ngen();
|
|
|
|
substitution new_subst = s.get_subst();
|
2014-07-23 21:21:47 +00:00
|
|
|
expr new_e = refresh_univ_metavars(new_subst.instantiate_all(e), ngen);
|
2014-07-23 15:51:24 +00:00
|
|
|
proof_state new_s(s.get_goals(), new_subst, ngen);
|
2014-07-27 19:01:06 +00:00
|
|
|
return apply_tactic_core(env, ios, new_s, new_e, true, true, relax_main_opaque);
|
2014-07-08 23:55:11 +00:00
|
|
|
} else {
|
2014-07-27 19:01:06 +00:00
|
|
|
return apply_tactic_core(env, ios, s, e, true, true, relax_main_opaque);
|
2014-07-08 23:55:11 +00:00
|
|
|
}
|
2014-07-03 19:59:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-27 19:01:06 +00:00
|
|
|
tactic eassumption_tactic(bool relax_main_opaque) {
|
2014-07-02 20:14:50 +00:00
|
|
|
return tactic([=](environment const & env, io_state const & ios, proof_state const & s) {
|
|
|
|
goals const & gs = s.get_goals();
|
|
|
|
if (empty(gs))
|
2014-06-30 01:26:07 +00:00
|
|
|
return proof_state_seq();
|
2014-07-03 19:59:48 +00:00
|
|
|
proof_state_seq r;
|
|
|
|
goal g = head(gs);
|
|
|
|
buffer<expr> hs;
|
|
|
|
get_app_args(g.get_meta(), hs);
|
|
|
|
for (expr const & h : hs) {
|
2014-07-27 19:01:06 +00:00
|
|
|
r = append(r, apply_tactic_core(env, ios, s, h, false, false, relax_main_opaque));
|
2013-12-05 11:22:12 +00:00
|
|
|
}
|
2014-07-03 19:59:48 +00:00
|
|
|
return r;
|
2013-12-05 11:22:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-30 01:26:07 +00:00
|
|
|
int mk_apply_tactic(lua_State * L) { return push_tactic(L, apply_tactic(to_expr(L, 1))); }
|
2014-07-03 19:59:48 +00:00
|
|
|
int mk_eassumption_tactic(lua_State * L) { return push_tactic(L, eassumption_tactic()); }
|
2013-12-05 11:22:12 +00:00
|
|
|
void open_apply_tactic(lua_State * L) {
|
2014-07-03 19:59:48 +00:00
|
|
|
SET_GLOBAL_FUN(mk_apply_tactic, "apply_tac");
|
|
|
|
SET_GLOBAL_FUN(mk_eassumption_tactic, "eassumption_tac");
|
2013-12-05 11:22:12 +00:00
|
|
|
}
|
|
|
|
}
|