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-25 19:45:16 +00:00
|
|
|
#include "library/blast/expr.h"
|
|
|
|
#include "library/blast/state.h"
|
|
|
|
#include "library/blast/blast.h"
|
|
|
|
|
|
|
|
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-25 19:45:16 +00:00
|
|
|
class context {
|
2015-09-28 23:40:19 +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
|
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;
|
|
|
|
name_map<expr> & m_local2lref;
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
void throw_unsupported_metavar_occ(expr const &) {
|
|
|
|
// TODO(Leo): improve error message
|
|
|
|
throw exception("'blast' tactic failed, goal contains a meta-variable application that is not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
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-28 23:40:19 +00:00
|
|
|
auto lref = m_local2lref.find(mlocal_name(l));
|
|
|
|
if (!lref) {
|
|
|
|
// One of the arguments is a local constant that is not in m_local2lref
|
|
|
|
throw_unsupported_metavar_occ(e);
|
|
|
|
}
|
|
|
|
ctx.push_back(lref_index(*lref));
|
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) {
|
|
|
|
if (auto r = m_local2lref.find(mlocal_name(e)))
|
|
|
|
return * r;
|
|
|
|
else
|
|
|
|
throw exception("blast tactic failed, ill-formed input goal");
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
name_map<pair<expr, expr>> & mvar2meta_mref, name_map<expr> & local2lref):
|
2015-09-25 21:43:42 +00:00
|
|
|
m_tc(env), m_state(s), m_mvar2meta_mref(mvar2meta_mref), m_local2lref(local2lref) {}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
name_map<pair<expr, expr>> mvar2meta_mref;
|
2015-09-28 23:40:19 +00:00
|
|
|
name_map<expr> local2lref;
|
2015-09-25 21:43:42 +00:00
|
|
|
to_blast_expr_fn to_blast_expr(m_env, s, mvar2meta_mref, local2lref);
|
|
|
|
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-25 21:43:42 +00:00
|
|
|
expr new_type = to_blast_expr(mlocal_type(h));
|
2015-09-29 00:39:30 +00:00
|
|
|
s.add_hypothesis(local_pp_name(h), new_type, none_expr(), some_expr(h));
|
2015-09-25 21:43:42 +00:00
|
|
|
}
|
|
|
|
expr new_target = to_blast_expr(g.get_type());
|
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):
|
|
|
|
m_env(env), m_ios(ios), m_lemma_hints(to_name_set(ls)), m_unfold_hints(to_name_set(ds)) {
|
|
|
|
}
|
2015-09-25 21:43:42 +00:00
|
|
|
|
|
|
|
optional<expr> operator()(goal const & g) {
|
|
|
|
state s = to_state(g);
|
2015-09-25 19:45:16 +00:00
|
|
|
return none_expr();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
optional<expr> blast_goal(environment const & env, io_state const & ios, list<name> const & ls, list<name> const & ds,
|
|
|
|
goal const & g) {
|
|
|
|
blast::scope_hash_consing scope;
|
|
|
|
blast::context c(env, ios, ls, ds);
|
|
|
|
return c(g);
|
|
|
|
}
|
2015-09-25 21:43:42 +00:00
|
|
|
void initialize_blast() {}
|
|
|
|
void finalize_blast() {}
|
2015-09-25 19:45:16 +00:00
|
|
|
}
|