2015-09-25 19:45:16 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2015-09-28 23:40:19 +00:00
|
|
|
#include "kernel/for_each_fn.h"
|
2015-09-25 21:43:42 +00:00
|
|
|
#include "kernel/type_checker.h"
|
|
|
|
#include "library/replace_visitor.h"
|
2015-09-29 17:42:35 +00:00
|
|
|
#include "library/util.h"
|
|
|
|
#include "library/reducible.h"
|
|
|
|
#include "library/normalize.h"
|
2015-10-01 23:21:17 +00:00
|
|
|
#include "library/projection.h"
|
2015-09-25 19:45:16 +00:00
|
|
|
#include "library/blast/expr.h"
|
|
|
|
#include "library/blast/state.h"
|
2015-10-01 23:21:17 +00:00
|
|
|
#include "library/blast/infer_type.h"
|
2015-09-25 19:45:16 +00:00
|
|
|
#include "library/blast/blast.h"
|
2015-09-29 17:04:11 +00:00
|
|
|
#include "library/blast/blast_context.h"
|
2015-09-29 16:02:58 +00:00
|
|
|
#include "library/blast/blast_exception.h"
|
2015-09-25 19:45:16 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
namespace blast {
|
2015-09-25 21:43:42 +00:00
|
|
|
level to_blast_level(level const & l) {
|
|
|
|
level lhs;
|
|
|
|
switch (l.kind()) {
|
|
|
|
case level_kind::Succ: return blast::mk_succ(to_blast_level(succ_of(l)));
|
|
|
|
case level_kind::Zero: return blast::mk_level_zero();
|
|
|
|
case level_kind::Param: return blast::mk_param_univ(param_id(l));
|
|
|
|
case level_kind::Meta: return blast::mk_meta_univ(meta_id(l));
|
|
|
|
case level_kind::Global: return blast::mk_global_univ(global_id(l));
|
|
|
|
case level_kind::Max:
|
|
|
|
lhs = to_blast_level(max_lhs(l));
|
|
|
|
return blast::mk_max(lhs, to_blast_level(max_rhs(l)));
|
|
|
|
case level_kind::IMax:
|
|
|
|
lhs = to_blast_level(imax_lhs(l));
|
|
|
|
return blast::mk_imax(lhs, to_blast_level(imax_rhs(l)));
|
|
|
|
}
|
|
|
|
lean_unreachable();
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:42:35 +00:00
|
|
|
static name * g_prefix = nullptr;
|
|
|
|
|
2015-09-25 19:45:16 +00:00
|
|
|
class context {
|
2015-10-01 23:21:17 +00:00
|
|
|
environment m_env;
|
|
|
|
io_state m_ios;
|
|
|
|
name_set m_lemma_hints;
|
|
|
|
name_set m_unfold_hints;
|
|
|
|
name_map<expr> m_mvar2mref; // map goal metavariables to blast mref's
|
|
|
|
name_predicate m_not_reducible_pred;
|
|
|
|
name_map<projection_info> m_projection_info;
|
|
|
|
state m_curr_state; // current state
|
2015-09-25 21:43:42 +00:00
|
|
|
|
|
|
|
class to_blast_expr_fn : public replace_visitor {
|
|
|
|
type_checker m_tc;
|
|
|
|
state & m_state;
|
2015-09-28 23:40:19 +00:00
|
|
|
// We map each metavariable to a metavariable application and the mref associated with it.
|
2015-09-25 21:43:42 +00:00
|
|
|
name_map<pair<expr, expr>> & m_mvar2meta_mref;
|
2015-09-29 19:13:20 +00:00
|
|
|
name_map<expr> & m_local2href;
|
2015-09-25 21:43:42 +00:00
|
|
|
|
|
|
|
expr visit_sort(expr const & e) {
|
|
|
|
return blast::mk_sort(to_blast_level(sort_level(e)));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual expr visit_macro(expr const & e) {
|
|
|
|
buffer<expr> new_args;
|
|
|
|
for (unsigned i = 0; i < macro_num_args(e); i++) {
|
|
|
|
new_args.push_back(visit(macro_arg(e, i)));
|
|
|
|
}
|
|
|
|
return blast::mk_macro(macro_def(e), new_args.size(), new_args.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual expr visit_constant(expr const & e) {
|
|
|
|
levels new_ls = map(const_levels(e), [&](level const & l) { return to_blast_level(l); });
|
|
|
|
return blast::mk_constant(const_name(e), new_ls);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual expr visit_var(expr const & e) {
|
|
|
|
return blast::mk_var(var_idx(e));
|
|
|
|
}
|
|
|
|
|
2015-09-29 16:02:58 +00:00
|
|
|
void throw_unsupported_metavar_occ(expr const & e) {
|
2015-09-25 21:43:42 +00:00
|
|
|
// TODO(Leo): improve error message
|
2015-09-29 16:02:58 +00:00
|
|
|
throw blast_exception("'blast' tactic failed, goal contains a "
|
|
|
|
"meta-variable application that is not supported", e);
|
2015-09-25 21:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr mk_mref_app(expr const & mref, unsigned nargs, expr const * args) {
|
|
|
|
lean_assert(is_mref(mref));
|
|
|
|
buffer<expr> new_args;
|
|
|
|
for (unsigned i = 0; i < nargs; i++) {
|
|
|
|
new_args.push_back(visit(args[i]));
|
|
|
|
}
|
|
|
|
return blast::mk_app(mref, new_args.size(), new_args.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
expr visit_meta_app(expr const & e) {
|
|
|
|
lean_assert(is_meta(e));
|
|
|
|
buffer<expr> args;
|
|
|
|
expr const & mvar = get_app_args(e, args);
|
|
|
|
if (pair<expr, expr> const * meta_mref = m_mvar2meta_mref.find(mlocal_name(mvar))) {
|
|
|
|
lean_assert(is_meta(meta_mref->first));
|
|
|
|
lean_assert(is_mref(meta_mref->second));
|
|
|
|
buffer<expr> decl_args;
|
|
|
|
get_app_args(meta_mref->first, decl_args);
|
|
|
|
if (decl_args.size() > args.size())
|
|
|
|
throw_unsupported_metavar_occ(e);
|
2015-09-28 23:40:19 +00:00
|
|
|
// Make sure the the current metavariable application prefix matches the one
|
|
|
|
// found before.
|
2015-09-25 21:43:42 +00:00
|
|
|
for (unsigned i = 0; i < decl_args.size(); i++) {
|
2015-09-28 23:40:19 +00:00
|
|
|
if (is_local(decl_args[i])) {
|
|
|
|
if (!is_local(args[i]) || mlocal_name(args[i]) != mlocal_name(decl_args[i]))
|
|
|
|
throw_unsupported_metavar_occ(e);
|
|
|
|
} else if (decl_args[i] != args[i]) {
|
2015-09-25 21:43:42 +00:00
|
|
|
throw_unsupported_metavar_occ(e);
|
2015-09-28 23:40:19 +00:00
|
|
|
}
|
2015-09-25 21:43:42 +00:00
|
|
|
}
|
|
|
|
return mk_mref_app(meta_mref->second, args.size() - decl_args.size(), args.data() + decl_args.size());
|
|
|
|
} else {
|
|
|
|
unsigned i = 0;
|
2015-09-28 23:40:19 +00:00
|
|
|
hypothesis_idx_buffer ctx;
|
|
|
|
// Find prefix that contains only closed terms.
|
2015-09-25 21:43:42 +00:00
|
|
|
for (; i < args.size(); i++) {
|
2015-09-28 23:40:19 +00:00
|
|
|
if (!closed(args[i]))
|
2015-09-25 21:43:42 +00:00
|
|
|
break;
|
2015-09-28 23:40:19 +00:00
|
|
|
if (!is_local(args[i])) {
|
|
|
|
// Ignore arguments that are not local constants.
|
|
|
|
// In the blast tactic we only support higher-order patterns.
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-25 21:43:42 +00:00
|
|
|
expr const & l = args[i];
|
|
|
|
if (!std::all_of(args.begin(), args.begin() + i,
|
2015-09-28 23:40:19 +00:00
|
|
|
[&](expr const & prev) { return mlocal_name(prev) != mlocal_name(l); })) {
|
|
|
|
// Local has already been processed
|
|
|
|
continue;
|
2015-09-25 21:43:42 +00:00
|
|
|
}
|
2015-09-29 19:13:20 +00:00
|
|
|
auto href = m_local2href.find(mlocal_name(l));
|
|
|
|
if (!href) {
|
|
|
|
// One of the arguments is a local constant that is not in m_local2href
|
2015-09-28 23:40:19 +00:00
|
|
|
throw_unsupported_metavar_occ(e);
|
|
|
|
}
|
2015-09-29 19:13:20 +00:00
|
|
|
ctx.push_back(href_index(*href));
|
2015-09-25 21:43:42 +00:00
|
|
|
}
|
|
|
|
unsigned prefix_sz = i;
|
|
|
|
expr aux = e;
|
|
|
|
for (; i < args.size(); i++)
|
|
|
|
aux = app_fn(aux);
|
|
|
|
lean_assert(is_meta(aux));
|
2015-09-28 23:40:19 +00:00
|
|
|
expr type = visit(m_tc.infer(aux).first);
|
|
|
|
expr mref = m_state.mk_metavar(ctx, type);
|
2015-09-25 21:43:42 +00:00
|
|
|
return mk_mref_app(mref, args.size() - prefix_sz, args.data() + prefix_sz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual expr visit_meta(expr const & e) {
|
|
|
|
return visit_meta_app(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual expr visit_local(expr const & e) {
|
2015-09-29 19:13:20 +00:00
|
|
|
if (auto r = m_local2href.find(mlocal_name(e)))
|
2015-09-25 21:43:42 +00:00
|
|
|
return * r;
|
|
|
|
else
|
2015-09-29 16:02:58 +00:00
|
|
|
throw blast_exception("blast tactic failed, ill-formed input goal", e);
|
2015-09-25 21:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual expr visit_app(expr const & e) {
|
|
|
|
if (is_meta(e)) {
|
|
|
|
return visit_meta_app(e);
|
|
|
|
} else {
|
|
|
|
expr f = visit(app_fn(e));
|
|
|
|
return blast::mk_app(f, visit(app_arg(e)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual expr visit_lambda(expr const & e) {
|
|
|
|
expr d = visit(binding_domain(e));
|
|
|
|
return blast::mk_lambda(binding_name(e), d, visit(binding_body(e)), binding_info(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual expr visit_pi(expr const & e) {
|
|
|
|
expr d = visit(binding_domain(e));
|
|
|
|
return blast::mk_pi(binding_name(e), d, visit(binding_body(e)), binding_info(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2015-09-28 23:40:19 +00:00
|
|
|
to_blast_expr_fn(environment const & env, state & s,
|
2015-09-29 19:13:20 +00:00
|
|
|
name_map<pair<expr, expr>> & mvar2meta_mref, name_map<expr> & local2href):
|
|
|
|
m_tc(env), m_state(s), m_mvar2meta_mref(mvar2meta_mref), m_local2href(local2href) {}
|
2015-09-25 21:43:42 +00:00
|
|
|
};
|
|
|
|
|
2015-09-28 23:40:19 +00:00
|
|
|
void init_mvar2mref(name_map<pair<expr, expr>> & m) {
|
|
|
|
m.for_each([&](name const & n, pair<expr, expr> const & p) {
|
|
|
|
m_mvar2mref.insert(n, p.second);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-25 21:43:42 +00:00
|
|
|
state to_state(goal const & g) {
|
|
|
|
state s;
|
2015-09-29 17:42:35 +00:00
|
|
|
type_checker_ptr norm_tc = mk_type_checker(m_env, name_generator(*g_prefix), UnfoldReducible);
|
2015-09-25 21:43:42 +00:00
|
|
|
name_map<pair<expr, expr>> mvar2meta_mref;
|
2015-09-29 19:13:20 +00:00
|
|
|
name_map<expr> local2href;
|
|
|
|
to_blast_expr_fn to_blast_expr(m_env, s, mvar2meta_mref, local2href);
|
2015-09-25 21:43:42 +00:00
|
|
|
buffer<expr> hs;
|
|
|
|
g.get_hyps(hs);
|
|
|
|
for (expr const & h : hs) {
|
2015-09-29 00:39:30 +00:00
|
|
|
lean_assert(is_local(h));
|
2015-09-29 17:42:35 +00:00
|
|
|
expr type = normalize(*norm_tc, mlocal_type(h));
|
|
|
|
expr new_type = to_blast_expr(type);
|
2015-09-29 19:13:20 +00:00
|
|
|
expr href = s.add_hypothesis(local_pp_name(h), new_type, none_expr(), some_expr(h));
|
|
|
|
local2href.insert(mlocal_name(h), href);
|
2015-09-25 21:43:42 +00:00
|
|
|
}
|
2015-09-29 17:42:35 +00:00
|
|
|
expr target = normalize(*norm_tc, g.get_type());
|
|
|
|
expr new_target = to_blast_expr(target);
|
2015-09-29 00:39:30 +00:00
|
|
|
s.set_target(new_target);
|
2015-09-28 23:40:19 +00:00
|
|
|
init_mvar2mref(mvar2meta_mref);
|
2015-09-29 01:55:24 +00:00
|
|
|
lean_assert(s.check_invariant());
|
2015-09-25 21:43:42 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-09-25 19:45:16 +00:00
|
|
|
public:
|
|
|
|
context(environment const & env, io_state const & ios, list<name> const & ls, list<name> const & ds):
|
2015-10-01 23:21:17 +00:00
|
|
|
m_env(env), m_ios(ios), m_lemma_hints(to_name_set(ls)), m_unfold_hints(to_name_set(ds)),
|
|
|
|
m_not_reducible_pred(mk_not_reducible_pred(env)) {
|
2015-09-25 19:45:16 +00:00
|
|
|
}
|
2015-09-25 21:43:42 +00:00
|
|
|
|
|
|
|
optional<expr> operator()(goal const & g) {
|
2015-09-29 17:04:11 +00:00
|
|
|
m_curr_state = to_state(g);
|
2015-09-29 17:42:35 +00:00
|
|
|
|
2015-09-29 17:04:11 +00:00
|
|
|
// TODO(Leo): blast main loop
|
2015-09-29 17:42:35 +00:00
|
|
|
display("Blast tactic initial state");
|
|
|
|
display_curr_state();
|
|
|
|
|
2015-09-25 19:45:16 +00:00
|
|
|
return none_expr();
|
|
|
|
}
|
2015-09-29 17:04:11 +00:00
|
|
|
|
|
|
|
environment const & get_env() const { return m_env; }
|
|
|
|
|
|
|
|
io_state const & get_ios() const { return m_ios; }
|
|
|
|
|
2015-10-01 23:21:17 +00:00
|
|
|
state & get_curr_state() { return m_curr_state; }
|
|
|
|
|
|
|
|
bool is_reducible(name const & n) const {
|
|
|
|
if (m_not_reducible_pred(n))
|
|
|
|
return false;
|
|
|
|
return !m_projection_info.contains(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
projection_info const * get_projection_info(name const & n) const {
|
|
|
|
return m_projection_info.find(n);
|
|
|
|
}
|
2015-09-25 19:45:16 +00:00
|
|
|
};
|
2015-09-29 17:04:11 +00:00
|
|
|
|
|
|
|
LEAN_THREAD_PTR(context, g_context);
|
|
|
|
struct scope_context {
|
|
|
|
context * m_prev_context;
|
|
|
|
public:
|
|
|
|
scope_context(context & c):m_prev_context(g_context) { g_context = &c; }
|
|
|
|
~scope_context() { g_context = m_prev_context; }
|
|
|
|
};
|
|
|
|
|
|
|
|
environment const & env() {
|
|
|
|
lean_assert(g_context);
|
|
|
|
return g_context->get_env();
|
|
|
|
}
|
|
|
|
|
|
|
|
io_state const & ios() {
|
|
|
|
lean_assert(g_context);
|
|
|
|
return g_context->get_ios();
|
|
|
|
}
|
|
|
|
|
2015-10-01 23:21:17 +00:00
|
|
|
state & curr_state() {
|
2015-09-29 17:04:11 +00:00
|
|
|
lean_assert(g_context);
|
|
|
|
return g_context->get_curr_state();
|
|
|
|
}
|
|
|
|
|
2015-10-01 23:21:17 +00:00
|
|
|
bool is_reducible(name const & n) {
|
|
|
|
lean_assert(g_context);
|
|
|
|
return g_context->is_reducible(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
projection_info const * get_projection_info(name const & n) {
|
|
|
|
lean_assert(g_context);
|
|
|
|
return g_context->get_projection_info(n);
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:04:11 +00:00
|
|
|
void display_curr_state() {
|
|
|
|
curr_state().display(env(), ios());
|
|
|
|
display("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void display(char const * msg) {
|
|
|
|
ios().get_diagnostic_channel() << msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void display(sstream const & msg) {
|
|
|
|
ios().get_diagnostic_channel() << msg.str();
|
|
|
|
}
|
2015-10-01 23:21:17 +00:00
|
|
|
|
|
|
|
/** \brief Auxiliary object to interface blast state with existing kernel extensions */
|
|
|
|
class ext_context : public extension_context {
|
|
|
|
name_generator m_ngen;
|
|
|
|
public:
|
|
|
|
virtual environment const & env() const { return env(); }
|
|
|
|
|
|
|
|
virtual pair<expr, constraint_seq> whnf(expr const & e) {
|
|
|
|
return mk_pair(blast::whnf(e), constraint_seq());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual pair<bool, constraint_seq> is_def_eq(expr const & e1, expr const & e2, delayed_justification &) {
|
|
|
|
return mk_pair(blast::is_def_eq(e1, e2), constraint_seq());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual pair<expr, constraint_seq> check_type(expr const & e, bool) {
|
|
|
|
return mk_pair(blast::infer_type(e), constraint_seq());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual name mk_fresh_name() {
|
|
|
|
return m_ngen.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual optional<expr> is_stuck(expr const &) {
|
|
|
|
return none_expr();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
LEAN_THREAD_PTR(ext_context, g_ext_context);
|
|
|
|
struct scope_ext_context {
|
|
|
|
ext_context * m_prev_context;
|
|
|
|
public:
|
|
|
|
scope_ext_context(ext_context & c):m_prev_context(g_ext_context) { g_ext_context = &c; }
|
|
|
|
~scope_ext_context() { g_ext_context = m_prev_context; }
|
|
|
|
};
|
|
|
|
|
|
|
|
extension_context & ext_ctx() {
|
|
|
|
lean_assert(g_ext_context);
|
|
|
|
return *g_ext_context;
|
|
|
|
}
|
2015-09-25 19:45:16 +00:00
|
|
|
}
|
|
|
|
optional<expr> blast_goal(environment const & env, io_state const & ios, list<name> const & ls, list<name> const & ds,
|
|
|
|
goal const & g) {
|
2015-09-29 17:04:11 +00:00
|
|
|
blast::scope_hash_consing scope1;
|
2015-09-25 19:45:16 +00:00
|
|
|
blast::context c(env, ios, ls, ds);
|
2015-10-01 23:21:17 +00:00
|
|
|
blast::ext_context x;
|
2015-09-29 17:04:11 +00:00
|
|
|
blast::scope_context scope2(c);
|
2015-10-01 23:21:17 +00:00
|
|
|
blast::scope_ext_context scope3(x);
|
2015-09-25 19:45:16 +00:00
|
|
|
return c(g);
|
|
|
|
}
|
2015-09-29 17:42:35 +00:00
|
|
|
void initialize_blast() {
|
|
|
|
blast::g_prefix = new name(name::mk_internal_unique_name());
|
|
|
|
}
|
|
|
|
void finalize_blast() {
|
|
|
|
delete blast::g_prefix;
|
|
|
|
}
|
2015-09-25 19:45:16 +00:00
|
|
|
}
|