2015-11-05 05:53:12 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2015 Daniel Selsam. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
Author: Daniel Selsam
|
|
|
|
*/
|
|
|
|
#include "kernel/abstract.h"
|
|
|
|
#include "kernel/expr_maps.h"
|
|
|
|
#include "kernel/instantiate.h"
|
|
|
|
#include "library/constants.h"
|
|
|
|
#include "library/expr_lt.h"
|
|
|
|
#include "library/class_instance_resolution.h"
|
|
|
|
#include "library/relation_manager.h"
|
|
|
|
#include "library/blast/expr.h"
|
|
|
|
#include "library/blast/blast_exception.h"
|
|
|
|
#include "library/blast/blast.h"
|
|
|
|
#include "library/blast/simplifier.h"
|
|
|
|
#include "library/simplifier/simp_rule_set.h"
|
|
|
|
#include "library/simplifier/ceqv.h"
|
|
|
|
#include "library/app_builder.h"
|
|
|
|
#include "util/flet.h"
|
|
|
|
#include "util/pair.h"
|
|
|
|
#include "util/sexpr/option_declarations.h"
|
|
|
|
#include <array>
|
2015-11-06 22:44:04 +00:00
|
|
|
#include <map>
|
2015-11-05 05:53:12 +00:00
|
|
|
|
|
|
|
#ifndef LEAN_DEFAULT_SIMPLIFY_MAX_STEPS
|
2015-11-08 05:09:39 +00:00
|
|
|
#define LEAN_DEFAULT_SIMPLIFY_MAX_STEPS 1000
|
2015-11-05 05:53:12 +00:00
|
|
|
#endif
|
|
|
|
#ifndef LEAN_DEFAULT_SIMPLIFY_TOP_DOWN
|
|
|
|
#define LEAN_DEFAULT_SIMPLIFY_TOP_DOWN false
|
|
|
|
#endif
|
|
|
|
#ifndef LEAN_DEFAULT_SIMPLIFY_EXHAUSTIVE
|
|
|
|
#define LEAN_DEFAULT_SIMPLIFY_EXHAUSTIVE true
|
|
|
|
#endif
|
|
|
|
#ifndef LEAN_DEFAULT_SIMPLIFY_MEMOIZE
|
|
|
|
#define LEAN_DEFAULT_SIMPLIFY_MEMOIZE true
|
|
|
|
#endif
|
|
|
|
#ifndef LEAN_DEFAULT_SIMPLIFY_CONTEXTUAL
|
|
|
|
#define LEAN_DEFAULT_SIMPLIFY_CONTEXTUAL true
|
|
|
|
#endif
|
|
|
|
#ifndef LEAN_DEFAULT_SIMPLIFY_EXPAND_MACROS
|
|
|
|
#define LEAN_DEFAULT_SIMPLIFY_EXPAND_MACROS false
|
|
|
|
#endif
|
|
|
|
#ifndef LEAN_DEFAULT_SIMPLIFY_TRACE
|
|
|
|
#define LEAN_DEFAULT_SIMPLIFY_TRACE false
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
namespace blast {
|
|
|
|
|
|
|
|
using simp::result;
|
|
|
|
|
|
|
|
/* Options */
|
|
|
|
|
|
|
|
static name * g_simplify_max_steps = nullptr;
|
|
|
|
static name * g_simplify_top_down = nullptr;
|
|
|
|
static name * g_simplify_exhaustive = nullptr;
|
|
|
|
static name * g_simplify_memoize = nullptr;
|
|
|
|
static name * g_simplify_contextual = nullptr;
|
|
|
|
static name * g_simplify_expand_macros = nullptr;
|
|
|
|
static name * g_simplify_trace = nullptr;
|
|
|
|
|
|
|
|
unsigned get_simplify_max_steps() {
|
|
|
|
return ios().get_options().get_unsigned(*g_simplify_max_steps, LEAN_DEFAULT_SIMPLIFY_MAX_STEPS);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_simplify_top_down() {
|
|
|
|
return ios().get_options().get_bool(*g_simplify_top_down, LEAN_DEFAULT_SIMPLIFY_TOP_DOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_simplify_exhaustive() {
|
|
|
|
return ios().get_options().get_bool(*g_simplify_exhaustive, LEAN_DEFAULT_SIMPLIFY_EXHAUSTIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_simplify_memoize() {
|
|
|
|
return ios().get_options().get_bool(*g_simplify_memoize, LEAN_DEFAULT_SIMPLIFY_MEMOIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_simplify_contextual() {
|
|
|
|
return ios().get_options().get_bool(*g_simplify_contextual, LEAN_DEFAULT_SIMPLIFY_CONTEXTUAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_simplify_expand_macros() {
|
|
|
|
return ios().get_options().get_bool(*g_simplify_expand_macros, LEAN_DEFAULT_SIMPLIFY_EXPAND_MACROS);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_simplify_trace() {
|
|
|
|
return ios().get_options().get_bool(*g_simplify_trace, LEAN_DEFAULT_SIMPLIFY_TRACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Main simplifier class */
|
|
|
|
|
|
|
|
class simplifier {
|
|
|
|
blast_tmp_type_context m_tmp_tctx;
|
|
|
|
app_builder m_app_builder;
|
|
|
|
branch m_branch;
|
|
|
|
name m_rel;
|
|
|
|
|
2015-11-07 05:02:04 +00:00
|
|
|
simp_rule_sets m_ctx_srss;
|
2015-11-05 05:53:12 +00:00
|
|
|
|
|
|
|
/* Logging */
|
|
|
|
unsigned m_num_steps{0};
|
|
|
|
unsigned m_depth{0};
|
|
|
|
|
|
|
|
/* Options */
|
|
|
|
unsigned m_max_steps{get_simplify_max_steps()};
|
|
|
|
bool m_top_down{get_simplify_top_down()};
|
|
|
|
bool m_exhaustive{get_simplify_exhaustive()};
|
|
|
|
bool m_memoize{get_simplify_memoize()};
|
|
|
|
bool m_contextual{get_simplify_contextual()};
|
|
|
|
bool m_expand_macros{get_simplify_expand_macros()};
|
|
|
|
bool m_trace{get_simplify_trace()};
|
2015-11-07 23:28:18 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
/* Cache */
|
2015-11-07 23:28:18 +00:00
|
|
|
typedef expr_bi_struct_map<result> simplify_cache;
|
|
|
|
typedef std::map<name, simplify_cache, name_quick_cmp> simplify_caches;
|
|
|
|
simplify_caches m_simplify_caches;
|
2015-11-05 05:53:12 +00:00
|
|
|
|
2015-11-06 22:44:04 +00:00
|
|
|
optional<result> cache_lookup(expr const & e);
|
|
|
|
void cache_save(expr const & e, result const & r);
|
2015-11-05 05:53:12 +00:00
|
|
|
|
|
|
|
/* Basic helpers */
|
|
|
|
bool using_eq() { return m_rel == get_eq_name(); }
|
|
|
|
|
|
|
|
bool is_dependent_fn(expr const & f) {
|
|
|
|
expr f_type = m_tmp_tctx->whnf(m_tmp_tctx->infer(f));
|
|
|
|
lean_assert(is_pi(f_type));
|
|
|
|
return has_free_vars(binding_body(f_type));
|
|
|
|
}
|
|
|
|
|
2015-11-07 05:02:04 +00:00
|
|
|
simp_rule_sets add_to_srss(simp_rule_sets const & _srss, buffer<expr> & ls) {
|
|
|
|
simp_rule_sets srss = _srss;
|
|
|
|
for (unsigned i = 0; i < ls.size(); i++) {
|
|
|
|
expr & l = ls[i];
|
2015-11-07 23:28:18 +00:00
|
|
|
tmp_type_context tctx(env(), ios());
|
|
|
|
srss = add(tctx, srss, mlocal_name(l), tctx.infer(l), l);
|
2015-11-07 05:02:04 +00:00
|
|
|
}
|
|
|
|
return srss;
|
|
|
|
}
|
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
/* Results */
|
|
|
|
result lift_from_eq(expr const & x, result const & r);
|
2015-11-07 23:28:18 +00:00
|
|
|
result join(result const & r1, result const & r2);
|
2015-11-05 05:53:12 +00:00
|
|
|
result funext(result const & r, expr const & l);
|
|
|
|
result finalize(result const & r);
|
|
|
|
|
|
|
|
/* Simplification */
|
2015-11-07 23:28:18 +00:00
|
|
|
result simplify(expr const & e);
|
2015-11-05 05:53:12 +00:00
|
|
|
result simplify_lambda(expr const & e);
|
|
|
|
result simplify_pi(expr const & e);
|
|
|
|
result simplify_app(expr const & e);
|
|
|
|
result simplify_fun(expr const & e);
|
2015-11-07 23:28:18 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
/* Rewriting */
|
2015-11-07 05:02:04 +00:00
|
|
|
result rewrite(expr const & e);
|
2015-11-07 23:28:18 +00:00
|
|
|
result rewrite(expr const & e, simp_rule_sets const & srss);
|
2015-11-05 05:53:12 +00:00
|
|
|
result rewrite(expr const & e, simp_rule const & sr);
|
|
|
|
|
|
|
|
/* Congruence */
|
2015-11-07 23:28:18 +00:00
|
|
|
result congr(result const & r_f, result const & r_arg);
|
2015-11-05 05:53:12 +00:00
|
|
|
result congr_fun(result const & r_f, expr const & arg);
|
|
|
|
result congr_arg(expr const & f, result const & r_arg);
|
2015-11-07 23:28:18 +00:00
|
|
|
result congr_funs(result const & r_f, buffer<expr> const & args);
|
|
|
|
|
|
|
|
result try_congrs(expr const & e);
|
2015-11-05 05:53:12 +00:00
|
|
|
result try_congr(expr const & e, congr_rule const & cr);
|
|
|
|
|
|
|
|
public:
|
|
|
|
simplifier(branch const & b, name const & rel);
|
|
|
|
result operator()(expr const & e) { return simplify(e); }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Constructor */
|
|
|
|
|
|
|
|
simplifier::simplifier(branch const & b, name const & rel):
|
|
|
|
m_app_builder(*m_tmp_tctx), m_branch(b), m_rel(rel) { }
|
|
|
|
|
2015-11-06 22:44:04 +00:00
|
|
|
/* Cache */
|
2015-11-07 05:02:04 +00:00
|
|
|
|
2015-11-06 22:44:04 +00:00
|
|
|
optional<result> simplifier::cache_lookup(expr const & e) {
|
2015-11-07 01:18:31 +00:00
|
|
|
simplify_cache & cache = m_simplify_caches[m_rel];
|
2015-11-06 22:44:04 +00:00
|
|
|
auto it = cache.find(e);
|
|
|
|
if (it != cache.end()) return optional<result>(it->second);
|
|
|
|
return optional<result>();
|
|
|
|
}
|
|
|
|
void simplifier::cache_save(expr const & e, result const & r) {
|
2015-11-07 01:18:31 +00:00
|
|
|
simplify_cache & cache = m_simplify_caches[m_rel];
|
2015-11-07 23:28:18 +00:00
|
|
|
cache.insert(mk_pair(e, r));
|
2015-11-06 22:44:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
/* Results */
|
|
|
|
|
|
|
|
result simplifier::lift_from_eq(expr const & x, result const & r) {
|
|
|
|
lean_assert(!r.is_none());
|
|
|
|
expr l = m_tmp_tctx->mk_tmp_local(m_tmp_tctx->infer(x));
|
2015-11-08 21:05:41 +00:00
|
|
|
expr motive_local = m_app_builder.mk_app(m_rel, x, l);
|
|
|
|
expr motive = Fun(l, motive_local);
|
|
|
|
expr Rxx = m_app_builder.mk_refl(m_rel, x);
|
|
|
|
expr pf = m_app_builder.mk_eq_rec(motive, Rxx, r.get_proof());
|
2015-11-07 23:28:18 +00:00
|
|
|
return result(r.get_new(), pf);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::join(result const & r1, result const & r2) {
|
|
|
|
/* Assumes that both results are with respect to the same relation */
|
|
|
|
if (r1.is_none()) {
|
|
|
|
return r2;
|
2015-11-07 23:28:18 +00:00
|
|
|
} else if (r2.is_none()) {
|
2015-11-05 05:53:12 +00:00
|
|
|
return r1;
|
2015-11-07 23:28:18 +00:00
|
|
|
} else {
|
2015-11-08 21:05:41 +00:00
|
|
|
expr trans = m_app_builder.mk_trans(m_rel, r1.get_proof(), r2.get_proof());
|
|
|
|
return result(r2.get_new(), trans);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::funext(result const & r, expr const & l) {
|
|
|
|
// theorem funext {f₁ f₂ : Πx : A, B x} : (∀x, f₁ x = f₂ x) → f₁ = f₂ :=
|
|
|
|
lean_assert(!r.is_none());
|
2015-11-08 21:05:41 +00:00
|
|
|
expr e = Fun(l, r.get_new());
|
|
|
|
expr pf = m_app_builder.mk_app(get_funext_name(), Fun(l, r.get_proof()));
|
|
|
|
return result(e, pf);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::finalize(result const & r) {
|
|
|
|
if (!r.is_none()) return r;
|
2015-11-08 21:05:41 +00:00
|
|
|
expr pf = m_app_builder.mk_refl(m_rel, r.get_new());
|
|
|
|
return result(r.get_new(), pf);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Simplification */
|
|
|
|
|
|
|
|
result simplifier::simplify(expr const & e) {
|
|
|
|
m_num_steps++;
|
|
|
|
flet<unsigned> inc_depth(m_depth, m_depth+1);
|
|
|
|
|
|
|
|
if (m_trace) {
|
2015-11-07 20:54:01 +00:00
|
|
|
ios().get_diagnostic_channel() << m_depth << "." << m_rel << ": " << e << "\n";
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_num_steps > m_max_steps)
|
|
|
|
throw blast_exception("simplifier failed, maximum number of steps exceeded", e);
|
|
|
|
|
|
|
|
if (m_memoize) {
|
2015-11-06 22:44:04 +00:00
|
|
|
if (auto it = cache_lookup(e)) return *it;
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
2015-11-07 23:28:18 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
result r(e);
|
|
|
|
|
|
|
|
if (m_top_down) r = join(r, rewrite(whnf(r.get_new())));
|
|
|
|
|
|
|
|
r.update(whnf(r.get_new()));
|
|
|
|
|
|
|
|
switch (r.get_new().kind()) {
|
|
|
|
case expr_kind::Local:
|
|
|
|
case expr_kind::Meta:
|
|
|
|
case expr_kind::Sort:
|
|
|
|
case expr_kind::Constant:
|
|
|
|
// no-op
|
|
|
|
break;
|
|
|
|
case expr_kind::Var:
|
|
|
|
lean_unreachable();
|
|
|
|
case expr_kind::Macro:
|
|
|
|
if (m_expand_macros) {
|
2015-11-07 23:28:18 +00:00
|
|
|
if (auto m = m_tmp_tctx->expand_macro(e)) r = join(r, simplify(whnf(*m)));
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case expr_kind::Lambda:
|
2015-11-07 23:28:18 +00:00
|
|
|
if (using_eq()) r = join(r, simplify_lambda(r.get_new()));
|
2015-11-05 05:53:12 +00:00
|
|
|
break;
|
|
|
|
case expr_kind::Pi:
|
2015-11-07 23:28:18 +00:00
|
|
|
r = join(r, simplify_pi(r.get_new()));
|
2015-11-05 05:53:12 +00:00
|
|
|
break;
|
|
|
|
case expr_kind::App:
|
2015-11-07 23:28:18 +00:00
|
|
|
r = join(r, simplify_app(r.get_new()));
|
2015-11-05 05:53:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-11-07 23:28:18 +00:00
|
|
|
if (!m_top_down) r = join(r, rewrite(whnf(r.get_new())));
|
2015-11-05 05:53:12 +00:00
|
|
|
|
|
|
|
if (r.get_new() == e && !using_eq()) {
|
|
|
|
{
|
|
|
|
flet<name> use_eq(m_rel, get_eq_name());
|
|
|
|
r = simplify(r.get_new());
|
|
|
|
}
|
2015-11-07 23:28:18 +00:00
|
|
|
if (!r.is_none()) r = lift_from_eq(e, r);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
2015-11-07 23:28:18 +00:00
|
|
|
if (m_exhaustive && r.get_new() != e) r = join(r, simplify(r.get_new()));
|
2015-11-05 05:53:12 +00:00
|
|
|
|
2015-11-07 23:28:18 +00:00
|
|
|
if (m_memoize) cache_save(e, r);
|
2015-11-05 05:53:12 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2015-11-07 23:28:18 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
result simplifier::simplify_lambda(expr const & _e) {
|
|
|
|
lean_assert(is_lambda(_e));
|
|
|
|
expr e = _e;
|
2015-11-07 23:28:18 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
buffer<expr> ls;
|
|
|
|
while (is_lambda(e)) {
|
|
|
|
expr d = instantiate_rev(binding_domain(e), ls.size(), ls.data());
|
2015-11-07 23:28:18 +00:00
|
|
|
expr l = m_tmp_tctx->mk_tmp_local(d, binding_info(e));
|
2015-11-05 05:53:12 +00:00
|
|
|
ls.push_back(l);
|
2015-11-07 23:28:18 +00:00
|
|
|
e = instantiate(binding_body(e), l);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result r = simplify(e);
|
|
|
|
if (r.is_none()) { return result(_e); }
|
|
|
|
|
2015-11-07 23:28:18 +00:00
|
|
|
for (int i = ls.size() - 1; i >= 0; --i) r = funext(r, ls[i]);
|
2015-11-05 05:53:12 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::simplify_pi(expr const & e) {
|
|
|
|
lean_assert(is_pi(e));
|
|
|
|
return try_congrs(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::simplify_app(expr const & e) {
|
|
|
|
lean_assert(is_app(e));
|
2015-11-07 23:28:18 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
/* (1) Try user-defined congruences */
|
|
|
|
result r = try_congrs(e);
|
|
|
|
if (!r.is_none()) {
|
2015-11-07 23:28:18 +00:00
|
|
|
if (using_eq()) return join(r, simplify_fun(r.get_new()));
|
2015-11-05 05:53:12 +00:00
|
|
|
else return r;
|
|
|
|
}
|
2015-11-07 23:28:18 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
/* (2) Synthesize congruence lemma */
|
|
|
|
if (using_eq()) {
|
2015-11-08 05:09:39 +00:00
|
|
|
buffer<expr> args;
|
|
|
|
expr fn = get_app_args(e, args);
|
|
|
|
if (auto congr_lemma = mk_congr_lemma_for_simp(fn, args.size())) {
|
|
|
|
expr proof = congr_lemma->get_proof();
|
|
|
|
expr type = congr_lemma->get_type();
|
|
|
|
unsigned i = 0;
|
|
|
|
bool simplified = false;
|
|
|
|
buffer<expr> locals;
|
|
|
|
for_each(congr_lemma->get_arg_kinds(), [&](congr_arg_kind const & ckind) {
|
|
|
|
proof = mk_app(proof, args[i]);
|
|
|
|
type = instantiate(binding_body(type), args[i]);
|
|
|
|
|
|
|
|
if (ckind == congr_arg_kind::Eq) {
|
|
|
|
result r_arg = simplify(args[i]);
|
|
|
|
if (!r_arg.is_none()) simplified = true;
|
|
|
|
r_arg = finalize(r_arg);
|
|
|
|
proof = mk_app(proof, r_arg.get_new(), r_arg.get_proof());
|
|
|
|
type = instantiate(binding_body(type), r_arg.get_new());
|
|
|
|
type = instantiate(binding_body(type), r_arg.get_proof());
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
});
|
|
|
|
if (simplified) {
|
|
|
|
lean_assert(is_eq(type));
|
|
|
|
buffer<expr> type_args;
|
|
|
|
get_app_args(type, type_args);
|
|
|
|
expr & new_e = type_args[2];
|
|
|
|
return join(result(new_e, proof), simplify_fun(new_e));
|
|
|
|
} else {
|
|
|
|
return simplify_fun(e);
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Fall back on generic binary congruence */
|
|
|
|
if (using_eq()) {
|
|
|
|
expr const & f = app_fn(e);
|
|
|
|
expr const & arg = app_arg(e);
|
|
|
|
|
|
|
|
result r_f = simplify(f);
|
|
|
|
|
|
|
|
if (is_dependent_fn(f)) {
|
|
|
|
if (r_f.is_none()) return e;
|
2015-11-07 23:28:18 +00:00
|
|
|
else return congr_fun(r_f, arg);
|
|
|
|
} else {
|
2015-11-05 05:53:12 +00:00
|
|
|
result r_arg = simplify(arg);
|
|
|
|
if (r_f.is_none() && r_arg.is_none()) return e;
|
2015-11-07 23:28:18 +00:00
|
|
|
else if (r_f.is_none()) return congr_arg(f, r_arg);
|
|
|
|
else if (r_arg.is_none()) return congr_fun(r_f, arg);
|
|
|
|
else return congr(r_f, r_arg);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::simplify_fun(expr const & e) {
|
|
|
|
lean_assert(is_app(e));
|
|
|
|
buffer<expr> args;
|
|
|
|
expr const & f = get_app_args(e, args);
|
|
|
|
result r_f = simplify(f);
|
|
|
|
if (r_f.is_none()) return result(e);
|
2015-11-07 23:28:18 +00:00
|
|
|
else return congr_funs(simplify(f), args);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Rewriting */
|
|
|
|
|
|
|
|
result simplifier::rewrite(expr const & e) {
|
|
|
|
result r(e);
|
2015-11-07 05:02:04 +00:00
|
|
|
while (true) {
|
2015-11-07 23:28:18 +00:00
|
|
|
result r_ctx = rewrite(r.get_new(), m_ctx_srss);
|
|
|
|
result r_new = rewrite(r_ctx.get_new(), get_simp_rule_sets(env()));
|
2015-11-07 05:02:04 +00:00
|
|
|
if (r_ctx.is_none() && r_new.is_none()) break;
|
2015-11-07 23:28:18 +00:00
|
|
|
r = join(join(r, r_ctx), r_new);
|
2015-11-07 05:02:04 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2015-11-05 05:53:12 +00:00
|
|
|
|
2015-11-07 05:02:04 +00:00
|
|
|
result simplifier::rewrite(expr const & e, simp_rule_sets const & srss) {
|
|
|
|
result r(e);
|
2015-11-07 23:28:18 +00:00
|
|
|
|
2015-11-07 05:02:04 +00:00
|
|
|
simp_rule_set const * sr = srss.find(m_rel);
|
2015-11-05 05:53:12 +00:00
|
|
|
if (!sr) return r;
|
|
|
|
|
|
|
|
list<simp_rule> const * srs = sr->find_simp(e);
|
|
|
|
if (!srs) return r;
|
2015-11-06 05:29:47 +00:00
|
|
|
|
2015-11-07 23:28:18 +00:00
|
|
|
for_each(*srs, [&](simp_rule const & sr) {
|
|
|
|
result r_new = rewrite(r.get_new(), sr);
|
2015-11-07 05:02:04 +00:00
|
|
|
if (r_new.is_none()) return;
|
2015-11-07 23:28:18 +00:00
|
|
|
r = join(r, r_new);
|
2015-11-07 05:02:04 +00:00
|
|
|
});
|
2015-11-05 05:53:12 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::rewrite(expr const & e, simp_rule const & sr) {
|
2015-11-07 23:28:18 +00:00
|
|
|
blast_tmp_type_context tmp_tctx(sr.get_num_umeta(), sr.get_num_emeta());
|
2015-11-05 05:53:12 +00:00
|
|
|
|
2015-11-07 23:28:18 +00:00
|
|
|
if (!tmp_tctx->is_def_eq(e, sr.get_lhs())) return result(e);
|
2015-11-08 01:12:34 +00:00
|
|
|
|
2015-11-07 00:55:39 +00:00
|
|
|
if (m_trace) {
|
2015-11-07 05:02:04 +00:00
|
|
|
expr new_lhs = tmp_tctx->instantiate_uvars_mvars(sr.get_lhs());
|
|
|
|
expr new_rhs = tmp_tctx->instantiate_uvars_mvars(sr.get_rhs());
|
2015-11-07 20:54:01 +00:00
|
|
|
ios().get_diagnostic_channel()
|
|
|
|
<< "REW(" << sr.get_id() << ") "
|
|
|
|
<< "[" << new_lhs << " =?= " << new_rhs << "]\n";
|
2015-11-07 00:55:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
/* Traverse metavariables backwards */
|
|
|
|
for (int i = sr.get_num_emeta() - 1; i >= 0; --i) {
|
|
|
|
expr const & m = sr.get_emeta(i);
|
|
|
|
bool is_instance = sr.is_instance(i);
|
|
|
|
|
2015-11-07 23:54:18 +00:00
|
|
|
expr m_type = tmp_tctx->instantiate_uvars_mvars(tmp_tctx->infer(m));
|
|
|
|
lean_assert(!has_metavar(m_type));
|
2015-11-08 01:12:34 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
if (is_instance) {
|
2015-11-07 20:54:01 +00:00
|
|
|
if (auto v = tmp_tctx->mk_class_instance(m_type)) {
|
|
|
|
if (!tmp_tctx->force_assign(m, *v)) {
|
|
|
|
if (m_trace) {
|
|
|
|
ios().get_diagnostic_channel() << "unable to assign instance for: " << m_type << "\n";
|
|
|
|
}
|
2015-11-05 05:53:12 +00:00
|
|
|
return result(e);
|
2015-11-07 20:54:01 +00:00
|
|
|
}
|
2015-11-05 05:53:12 +00:00
|
|
|
} else {
|
2015-11-07 20:54:01 +00:00
|
|
|
if (m_trace) {
|
|
|
|
ios().get_diagnostic_channel() << "unable to synthesize instance for: " << m_type << "\n";
|
|
|
|
}
|
2015-11-05 05:53:12 +00:00
|
|
|
return result(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp_tctx->is_mvar_assigned(i)) continue;
|
|
|
|
|
2015-11-07 20:54:01 +00:00
|
|
|
if (tmp_tctx->is_prop(m_type)) {
|
2015-11-07 23:28:18 +00:00
|
|
|
flet<name> set_name(m_rel, get_iff_name());
|
2015-11-07 20:54:01 +00:00
|
|
|
result r_cond = simplify(m_type);
|
|
|
|
if (is_constant(r_cond.get_new()) && const_name(r_cond.get_new()) == get_true_name()) {
|
2015-11-08 21:05:41 +00:00
|
|
|
expr pf = m_app_builder.mk_app(name("iff", "elim_right"), finalize(r_cond).get_proof(), mk_constant(get_true_intro_name()));
|
2015-11-08 21:12:43 +00:00
|
|
|
lean_verify(tmp_tctx->is_def_eq(m, pf));
|
2015-11-07 20:54:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
2015-11-07 05:02:04 +00:00
|
|
|
if (m_trace) {
|
2015-11-07 20:54:01 +00:00
|
|
|
ios().get_diagnostic_channel() << "failed to assign: " << m << " : " << m_type << "\n";
|
2015-11-07 05:02:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
/* We fail if there is a meta variable that we still cannot assign */
|
|
|
|
return result(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < sr.get_num_umeta(); i++) {
|
|
|
|
if (!tmp_tctx->is_uvar_assigned(i)) return result(e);
|
|
|
|
}
|
|
|
|
|
2015-11-07 20:54:01 +00:00
|
|
|
expr new_lhs = tmp_tctx->instantiate_uvars_mvars(sr.get_lhs());
|
|
|
|
expr new_rhs = tmp_tctx->instantiate_uvars_mvars(sr.get_rhs());
|
|
|
|
|
|
|
|
if (sr.is_perm()) {
|
2015-11-07 23:28:18 +00:00
|
|
|
if (!is_lt(new_rhs, new_lhs, false))
|
2015-11-07 20:54:01 +00:00
|
|
|
return result(e);
|
|
|
|
}
|
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
expr pf = tmp_tctx->instantiate_uvars_mvars(sr.get_proof());
|
2015-11-07 23:28:18 +00:00
|
|
|
return result(result(new_rhs, pf));
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Congruence */
|
2015-11-07 20:54:01 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
result simplifier::congr(result const & r_f, result const & r_arg) {
|
|
|
|
lean_assert(!r_f.is_none() && !r_arg.is_none());
|
|
|
|
// theorem congr {A B : Type} {f₁ f₂ : A → B} {a₁ a₂ : A} (H₁ : f₁ = f₂) (H₂ : a₁ = a₂) : f₁ a₁ = f₂ a₂
|
2015-11-08 21:05:41 +00:00
|
|
|
expr e = mk_app(r_f.get_new(), r_arg.get_new());
|
2015-11-08 21:18:06 +00:00
|
|
|
expr pf = m_app_builder.mk_congr(r_f.get_proof(), r_arg.get_proof());
|
2015-11-08 21:05:41 +00:00
|
|
|
return result(e, pf);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::congr_fun(result const & r_f, expr const & arg) {
|
|
|
|
lean_assert(!r_f.is_none());
|
|
|
|
// theorem congr_fun {A : Type} {B : A → Type} {f g : Π x, B x} (H : f = g) (a : A) : f a = g a
|
2015-11-08 21:05:41 +00:00
|
|
|
expr e = mk_app(r_f.get_new(), arg);
|
2015-11-08 21:18:06 +00:00
|
|
|
expr pf = m_app_builder.mk_congr_fun(r_f.get_proof(), arg);
|
2015-11-08 21:05:41 +00:00
|
|
|
return result(e, pf);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::congr_arg(expr const & f, result const & r_arg) {
|
|
|
|
lean_assert(!r_arg.is_none());
|
|
|
|
// theorem congr_arg {A B : Type} {a₁ a₂ : A} (f : A → B) : a₁ = a₂ → f a₁ = f a₂
|
2015-11-08 21:05:41 +00:00
|
|
|
expr e = mk_app(f, r_arg.get_new());
|
2015-11-08 21:18:06 +00:00
|
|
|
expr pf = m_app_builder.mk_congr_arg(f, r_arg.get_proof());
|
2015-11-08 21:05:41 +00:00
|
|
|
return result(e, pf);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::congr_funs(result const & r_f, buffer<expr> const & args) {
|
|
|
|
lean_assert(!r_f.is_none());
|
|
|
|
// congr_fun : ∀ {A : Type} {B : A → Type} {f g : Π (x : A), B x}, f = g → (∀ (a : A), f a = g a)
|
|
|
|
expr e = r_f.get_new();
|
|
|
|
expr pf = r_f.get_proof();
|
|
|
|
for (unsigned i = 0; i < args.size(); ++i) {
|
2015-11-08 21:05:41 +00:00
|
|
|
e = mk_app(e, args[i]);
|
|
|
|
pf = m_app_builder.mk_app(get_congr_fun_name(), pf, args[i]);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
2015-11-07 23:28:18 +00:00
|
|
|
return result(e, pf);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::try_congrs(expr const & e) {
|
|
|
|
simp_rule_set const * sr = get_simp_rule_sets(env()).find(m_rel);
|
|
|
|
if (!sr) return result(e);
|
|
|
|
|
|
|
|
list<congr_rule> const * crs = sr->find_congr(e);
|
|
|
|
if (!crs) return result(e);
|
|
|
|
|
|
|
|
result r(e);
|
2015-11-07 23:28:18 +00:00
|
|
|
for_each(*crs, [&](congr_rule const & cr) {
|
2015-11-05 05:53:12 +00:00
|
|
|
if (!r.is_none()) return;
|
2015-11-07 23:28:18 +00:00
|
|
|
r = try_congr(e, cr);
|
|
|
|
});
|
2015-11-05 05:53:12 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
result simplifier::try_congr(expr const & e, congr_rule const & cr) {
|
2015-11-07 23:28:18 +00:00
|
|
|
blast_tmp_type_context tmp_tctx(cr.get_num_umeta(), cr.get_num_emeta());
|
2015-11-05 05:53:12 +00:00
|
|
|
|
2015-11-07 23:28:18 +00:00
|
|
|
if (!tmp_tctx->is_def_eq(e, cr.get_lhs())) return result(e);
|
2015-11-07 00:55:39 +00:00
|
|
|
|
|
|
|
if (m_trace) {
|
2015-11-07 23:28:18 +00:00
|
|
|
ios().get_diagnostic_channel() << "<" << cr.get_id() << "> "
|
2015-11-07 05:02:04 +00:00
|
|
|
<< e << " === " << cr.get_lhs() << "\n";
|
2015-11-07 00:55:39 +00:00
|
|
|
}
|
2015-11-07 23:28:18 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
/* First, iterate over the congruence hypotheses */
|
|
|
|
bool failed = false;
|
|
|
|
bool simplified = false;
|
|
|
|
list<expr> const & congr_hyps = cr.get_congr_hyps();
|
2015-11-07 23:28:18 +00:00
|
|
|
for_each(congr_hyps, [&](expr const & m) {
|
2015-11-05 05:53:12 +00:00
|
|
|
if (failed) return;
|
|
|
|
buffer<expr> ls;
|
2015-11-07 20:54:01 +00:00
|
|
|
expr m_type = tmp_tctx->instantiate_uvars_mvars(tmp_tctx->infer(m));
|
2015-11-05 05:53:12 +00:00
|
|
|
|
|
|
|
while (is_pi(m_type)) {
|
|
|
|
expr d = instantiate_rev(binding_domain(m_type), ls.size(), ls.data());
|
2015-11-07 23:28:18 +00:00
|
|
|
expr l = tmp_tctx->mk_tmp_local(d, binding_info(m_type));
|
2015-11-07 23:54:18 +00:00
|
|
|
lean_assert(!has_metavar(l));
|
2015-11-07 05:02:04 +00:00
|
|
|
ls.push_back(l);
|
2015-11-07 23:28:18 +00:00
|
|
|
m_type = instantiate(binding_body(m_type), l);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr h_rel, h_lhs, h_rhs;
|
2015-11-08 21:12:43 +00:00
|
|
|
lean_verify(is_simp_relation(env(), m_type, h_rel, h_lhs, h_rhs) && is_constant(h_rel));
|
2015-11-05 05:53:12 +00:00
|
|
|
{
|
2015-11-07 23:28:18 +00:00
|
|
|
simplify_caches fresh_caches;
|
|
|
|
flet<simplify_caches> set_simplify_caches(m_simplify_caches, fresh_caches);
|
|
|
|
flet<name> set_name(m_rel, const_name(h_rel));
|
|
|
|
|
|
|
|
flet<simp_rule_sets> set_ctx_srss(m_ctx_srss, add_to_srss(m_ctx_srss, ls));
|
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
h_lhs = tmp_tctx->instantiate_uvars_mvars(h_lhs);
|
2015-11-07 23:54:18 +00:00
|
|
|
lean_assert(!has_metavar(h_lhs));
|
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
result r_congr_hyp = simplify(h_lhs);
|
|
|
|
expr hyp;
|
|
|
|
if (r_congr_hyp.is_none()) {
|
|
|
|
hyp = finalize(r_congr_hyp).get_proof();
|
2015-11-07 23:28:18 +00:00
|
|
|
} else {
|
2015-11-05 05:53:12 +00:00
|
|
|
hyp = r_congr_hyp.get_proof();
|
|
|
|
simplified = true;
|
|
|
|
}
|
2015-11-07 05:02:04 +00:00
|
|
|
|
2015-11-07 23:28:18 +00:00
|
|
|
if (!tmp_tctx->is_def_eq(m, Fun(ls, hyp))) failed = true;
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
});
|
2015-11-07 23:28:18 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
if (failed || !simplified) return result(e);
|
2015-11-07 23:28:18 +00:00
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
/* Traverse metavariables backwards, proving or synthesizing the rest */
|
|
|
|
for (int i = cr.get_num_emeta() - 1; i >= 0; --i) {
|
|
|
|
expr const & m = cr.get_emeta(i);
|
|
|
|
bool is_instance = cr.is_instance(i);
|
|
|
|
|
|
|
|
if (is_instance) {
|
|
|
|
expr type = tmp_tctx->instantiate_uvars_mvars(tmp_tctx->infer(m));
|
|
|
|
if (auto v = tmp_tctx->mk_class_instance(type)) {
|
|
|
|
if (!tmp_tctx->force_assign(m, *v))
|
|
|
|
return result(e);
|
|
|
|
} else {
|
|
|
|
return result(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp_tctx->is_mvar_assigned(i)) continue;
|
|
|
|
|
|
|
|
if (tmp_tctx->is_prop(tmp_tctx->infer(m))) {
|
2015-11-07 23:28:18 +00:00
|
|
|
// TODO(dhs): should I try to prove?
|
2015-11-05 05:53:12 +00:00
|
|
|
return result(e);
|
|
|
|
}
|
|
|
|
|
2015-11-07 05:02:04 +00:00
|
|
|
if (m_trace) {
|
|
|
|
ios().get_diagnostic_channel() << "failed to assign: " << tmp_tctx->instantiate_uvars_mvars(m) << " : "
|
|
|
|
<< tmp_tctx->instantiate_uvars_mvars(tmp_tctx->infer(m)) << "\n";
|
|
|
|
}
|
|
|
|
|
2015-11-05 05:53:12 +00:00
|
|
|
/* We fail if there is a meta variable that we still cannot assign */
|
|
|
|
return result(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < cr.get_num_umeta(); i++) {
|
|
|
|
if (!tmp_tctx->is_uvar_assigned(i)) return result(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
expr e_s = tmp_tctx->instantiate_uvars_mvars(cr.get_rhs());
|
|
|
|
expr pf = tmp_tctx->instantiate_uvars_mvars(cr.get_proof());
|
2015-11-07 23:28:18 +00:00
|
|
|
return result(e_s, pf);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup and teardown */
|
|
|
|
|
|
|
|
void initialize_simplifier() {
|
|
|
|
g_simplify_max_steps = new name{"simplify", "max_steps"};
|
|
|
|
g_simplify_top_down = new name{"simplify", "top_down"};
|
|
|
|
g_simplify_exhaustive = new name{"simplify", "exhaustive"};
|
|
|
|
g_simplify_memoize = new name{"simplify", "memoize"};
|
|
|
|
g_simplify_contextual = new name{"simplify", "contextual"};
|
|
|
|
g_simplify_expand_macros = new name{"simplify", "expand_macros"};
|
2015-11-07 23:28:18 +00:00
|
|
|
g_simplify_trace = new name{"simplify", "trace"};
|
2015-11-05 05:53:12 +00:00
|
|
|
|
|
|
|
register_unsigned_option(*g_simplify_max_steps, LEAN_DEFAULT_SIMPLIFY_MAX_STEPS,
|
|
|
|
"(simplify) max allowed steps in simplification");
|
|
|
|
register_bool_option(*g_simplify_top_down, LEAN_DEFAULT_SIMPLIFY_TOP_DOWN,
|
|
|
|
"(simplify) use top-down rewriting instead of bottom-up");
|
|
|
|
register_bool_option(*g_simplify_exhaustive, LEAN_DEFAULT_SIMPLIFY_EXHAUSTIVE,
|
|
|
|
"(simplify) rewrite exhaustively");
|
|
|
|
register_bool_option(*g_simplify_memoize, LEAN_DEFAULT_SIMPLIFY_MEMOIZE,
|
|
|
|
"(simplify) memoize simplifications");
|
|
|
|
register_bool_option(*g_simplify_contextual, LEAN_DEFAULT_SIMPLIFY_CONTEXTUAL,
|
|
|
|
"(simplify) use contextual simplification");
|
|
|
|
register_bool_option(*g_simplify_expand_macros, LEAN_DEFAULT_SIMPLIFY_EXPAND_MACROS,
|
|
|
|
"(simplify) expand macros");
|
|
|
|
register_bool_option(*g_simplify_trace, LEAN_DEFAULT_SIMPLIFY_TRACE,
|
|
|
|
"(simplify) trace");
|
|
|
|
}
|
|
|
|
|
|
|
|
void finalize_simplifier() {
|
|
|
|
delete g_simplify_trace;
|
|
|
|
delete g_simplify_expand_macros;
|
|
|
|
delete g_simplify_contextual;
|
|
|
|
delete g_simplify_memoize;
|
|
|
|
delete g_simplify_exhaustive;
|
2015-11-07 23:28:18 +00:00
|
|
|
delete g_simplify_top_down;
|
2015-11-05 05:53:12 +00:00
|
|
|
delete g_simplify_max_steps;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Entry point */
|
|
|
|
|
|
|
|
result simplify(branch const & b, name const & rel, expr const & e) {
|
2015-11-07 23:28:18 +00:00
|
|
|
return simplifier(b, rel)(e);
|
2015-11-05 05:53:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}}
|