2014-11-27 05:28:00 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2014-12-31 05:22:50 +00:00
|
|
|
#include <utility>
|
2014-11-29 05:56:13 +00:00
|
|
|
#include "util/sstream.h"
|
2014-11-27 05:28:00 +00:00
|
|
|
#include "kernel/abstract.h"
|
|
|
|
#include "kernel/instantiate.h"
|
|
|
|
#include "kernel/inductive/inductive.h"
|
2014-12-19 05:29:13 +00:00
|
|
|
#include "library/io_state_stream.h"
|
2014-11-27 05:28:00 +00:00
|
|
|
#include "library/locals.h"
|
2014-12-19 05:29:13 +00:00
|
|
|
#include "library/util.h"
|
2015-01-24 00:50:32 +00:00
|
|
|
#include "library/constants.h"
|
2014-11-27 05:28:00 +00:00
|
|
|
#include "library/reducible.h"
|
2014-12-19 05:29:13 +00:00
|
|
|
#include "library/tactic/tactic.h"
|
2014-11-27 05:28:00 +00:00
|
|
|
#include "library/tactic/expr_to_tactic.h"
|
2014-12-19 05:29:13 +00:00
|
|
|
#include "library/tactic/class_instance_synth.h"
|
2014-12-31 05:22:50 +00:00
|
|
|
#include "library/tactic/inversion_tactic.h"
|
2014-11-27 05:28:00 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-12-31 05:22:50 +00:00
|
|
|
namespace inversion {
|
2015-01-03 06:00:01 +00:00
|
|
|
result::result(list<goal> const & gs, list<list<expr>> const & args, list<implementation_list> const & imps,
|
2015-01-01 02:55:18 +00:00
|
|
|
list<rename_map> const & rs, name_generator const & ngen, substitution const & subst):
|
|
|
|
m_goals(gs), m_args(args), m_implementation_lists(imps),
|
|
|
|
m_renames(rs), m_ngen(ngen), m_subst(subst) {
|
|
|
|
lean_assert_eq(length(m_goals), length(m_args));
|
2014-12-31 05:22:50 +00:00
|
|
|
lean_assert_eq(length(m_goals), length(m_implementation_lists));
|
2015-01-01 02:55:18 +00:00
|
|
|
lean_assert_eq(length(m_goals), length(m_renames));
|
2014-12-31 05:22:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 05:29:13 +00:00
|
|
|
/**
|
|
|
|
\brief Given eq_rec of the form <tt>@eq.rec.{l₂ l₁} A a (λ (a' : A) (h : a = a'), B a') b a p</tt>,
|
|
|
|
apply the eq_rec_eq definition to produce the equality
|
|
|
|
|
|
|
|
b = @eq.rec.{l₂ l₁} A a (λ (a' : A) (h : a = a'), B a') b a p
|
|
|
|
|
|
|
|
The eq_rec_eq definition is of the form
|
|
|
|
|
|
|
|
definition eq_rec_eq.{l₁ l₂} {A : Type.{l₁}} {B : A → Type.{l₂}} [h : is_hset A] {a : A} (b : B a) (p : a = a) :
|
|
|
|
b = @eq.rec.{l₂ l₁} A a (λ (a' : A) (h : a = a'), B a') b a p :=
|
|
|
|
...
|
|
|
|
*/
|
|
|
|
optional<expr> apply_eq_rec_eq(type_checker & tc, io_state const & ios, list<expr> const & ctx, expr const & eq_rec) {
|
|
|
|
buffer<expr> args;
|
|
|
|
expr const & eq_rec_fn = get_app_args(eq_rec, args);
|
|
|
|
if (args.size() != 6)
|
|
|
|
return none_expr();
|
|
|
|
expr const & p = args[5];
|
2014-12-22 17:40:15 +00:00
|
|
|
if (!is_local(p) || !is_eq_a_a(tc, mlocal_type(p)))
|
2014-12-19 05:29:13 +00:00
|
|
|
return none_expr();
|
|
|
|
expr const & A = args[0];
|
|
|
|
auto is_hset_A = mk_hset_instance(tc, ios, ctx, A);
|
|
|
|
if (!is_hset_A)
|
|
|
|
return none_expr();
|
|
|
|
levels ls = const_levels(eq_rec_fn);
|
|
|
|
level l2 = head(ls);
|
|
|
|
level l1 = head(tail(ls));
|
|
|
|
expr C = tc.whnf(args[2]).first;
|
|
|
|
if (!is_lambda(C))
|
|
|
|
return none_expr();
|
|
|
|
expr a1 = mk_local(tc.mk_fresh_name(), binding_domain(C));
|
|
|
|
C = tc.whnf(instantiate(binding_body(C), a1)).first;
|
|
|
|
if (!is_lambda(C))
|
|
|
|
return none_expr();
|
|
|
|
C = binding_body(C);
|
|
|
|
if (!closed(C))
|
|
|
|
return none_expr();
|
|
|
|
expr B = Fun(a1, C);
|
|
|
|
expr a = args[1];
|
|
|
|
expr b = args[3];
|
2015-01-24 00:50:32 +00:00
|
|
|
expr r = mk_constant(get_eq_rec_eq_name(), {l1, l2});
|
2014-12-19 05:29:13 +00:00
|
|
|
return some_expr(mk_app({r, A, B, *is_hset_A, a, b, p}));
|
|
|
|
}
|
|
|
|
|
2014-12-31 05:22:50 +00:00
|
|
|
typedef inversion::implementation_ptr implementation_ptr;
|
|
|
|
typedef inversion::implementation_list implementation_list;
|
|
|
|
|
2015-01-05 03:56:10 +00:00
|
|
|
static void replace(implementation_list const & imps, unsigned sz, expr const * from, expr const * to) {
|
|
|
|
lean_assert(std::all_of(from, from+sz, is_local));
|
2014-12-31 05:22:50 +00:00
|
|
|
for (implementation_ptr const & imp : imps) {
|
2015-01-05 03:56:10 +00:00
|
|
|
imp->update_exprs([&](expr const & e) {
|
|
|
|
return instantiate_rev(abstract_locals(e, sz, from), sz, to);
|
|
|
|
});
|
2014-12-31 05:22:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:56:10 +00:00
|
|
|
static void replace(implementation_list const & imps, buffer<expr> const & from, buffer<expr> const & to) {
|
|
|
|
lean_assert(from.size() == to.size());
|
|
|
|
return replace(imps, from.size(), from.data(), to.data());
|
2014-12-31 05:22:50 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 03:56:10 +00:00
|
|
|
static void replace(implementation_list const & imps, expr const & from, expr const & to) {
|
|
|
|
return replace(imps, 1, &from, &to);
|
2014-12-31 05:22:50 +00:00
|
|
|
}
|
|
|
|
|
2014-11-27 08:06:26 +00:00
|
|
|
class inversion_tac {
|
|
|
|
environment const & m_env;
|
2014-12-19 05:29:13 +00:00
|
|
|
io_state const & m_ios;
|
2014-12-31 05:22:50 +00:00
|
|
|
type_checker & m_tc;
|
2014-11-29 06:25:37 +00:00
|
|
|
list<name> m_ids;
|
2014-11-27 08:06:26 +00:00
|
|
|
name_generator m_ngen;
|
|
|
|
substitution m_subst;
|
|
|
|
|
2014-11-27 18:37:22 +00:00
|
|
|
bool m_dep_elim;
|
2014-12-19 05:29:13 +00:00
|
|
|
bool m_proof_irrel;
|
2014-11-27 08:06:26 +00:00
|
|
|
unsigned m_nparams;
|
|
|
|
unsigned m_nindices;
|
|
|
|
unsigned m_nminors;
|
|
|
|
declaration m_I_decl;
|
|
|
|
declaration m_cases_on_decl;
|
2014-11-27 05:28:00 +00:00
|
|
|
|
2015-02-01 18:47:32 +00:00
|
|
|
expr whnf(expr const & e) { return m_tc.whnf(e).first; }
|
|
|
|
expr infer_type(expr const & e) { return m_tc.infer(e).first; }
|
|
|
|
|
2014-11-27 08:06:26 +00:00
|
|
|
void init_inductive_info(name const & n) {
|
2014-12-19 05:29:13 +00:00
|
|
|
m_dep_elim = inductive::has_dep_elim(m_env, n);
|
|
|
|
m_nindices = *inductive::get_num_indices(m_env, n);
|
|
|
|
m_nparams = *inductive::get_num_params(m_env, n);
|
2014-12-02 02:32:38 +00:00
|
|
|
// This tactic is bases on cases_on construction which only has
|
|
|
|
// minor premises for the introduction rules of this datatype.
|
|
|
|
// For non-mutually recursive datatypes inductive::get_num_intro_rules == inductive::get_num_minor_premises
|
2014-12-19 05:29:13 +00:00
|
|
|
m_nminors = *inductive::get_num_intro_rules(m_env, n);
|
|
|
|
m_I_decl = m_env.get(n);
|
|
|
|
m_cases_on_decl = m_env.get({n, "cases_on"});
|
2014-11-27 05:28:00 +00:00
|
|
|
}
|
|
|
|
|
2014-11-27 08:06:26 +00:00
|
|
|
bool is_inversion_applicable(expr const & t) {
|
|
|
|
buffer<expr> args;
|
|
|
|
expr const & fn = get_app_args(t, args);
|
|
|
|
if (!is_constant(fn))
|
|
|
|
return false;
|
|
|
|
if (!inductive::is_inductive_decl(m_env, const_name(fn)))
|
|
|
|
return false;
|
2015-01-24 00:50:32 +00:00
|
|
|
if (!m_env.find(name{const_name(fn), "cases_on"}) || !m_env.find(get_eq_name()))
|
2014-12-19 05:29:13 +00:00
|
|
|
return false;
|
2015-01-24 00:50:32 +00:00
|
|
|
if (m_proof_irrel && !m_env.find(get_heq_name()))
|
2014-11-27 08:06:26 +00:00
|
|
|
return false;
|
|
|
|
init_inductive_info(const_name(fn));
|
|
|
|
if (args.size() != m_nindices + m_nparams)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pair<expr, expr> mk_eq(expr const & lhs, expr const & rhs) {
|
2015-02-01 18:47:32 +00:00
|
|
|
expr lhs_type = infer_type(lhs);
|
|
|
|
expr rhs_type = infer_type(rhs);
|
2014-12-31 05:22:50 +00:00
|
|
|
level l = sort_level(m_tc.ensure_type(lhs_type).first);
|
2014-11-27 08:06:26 +00:00
|
|
|
constraint_seq cs;
|
2014-12-31 05:22:50 +00:00
|
|
|
if (m_tc.is_def_eq(lhs_type, rhs_type, justification(), cs) && !cs) {
|
2015-01-24 00:50:32 +00:00
|
|
|
return mk_pair(mk_app(mk_constant(get_eq_name(), to_list(l)), lhs_type, lhs, rhs),
|
|
|
|
mk_app(mk_constant(get_eq_refl_name(), to_list(l)), rhs_type, rhs));
|
2014-11-27 08:06:26 +00:00
|
|
|
} else {
|
2015-01-24 00:50:32 +00:00
|
|
|
return mk_pair(mk_app(mk_constant(get_heq_name(), to_list(l)), lhs_type, lhs, rhs_type, rhs),
|
|
|
|
mk_app(mk_constant(get_heq_refl_name(), to_list(l)), rhs_type, rhs));
|
2014-11-27 08:06:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-01 06:35:32 +00:00
|
|
|
void assign(name const & n, expr const & val) {
|
|
|
|
m_subst.assign(n, val);
|
|
|
|
}
|
|
|
|
|
2015-01-02 01:37:17 +00:00
|
|
|
/** \brief We say h has independent indices IF
|
|
|
|
1- it is *not* an indexed inductive family, OR
|
|
|
|
2- it is an indexed inductive family, but all indices are distinct local constants,
|
|
|
|
and all hypotheses of g different from h and indices, do not depend on the indices.
|
|
|
|
*/
|
|
|
|
bool has_indep_indices(goal const & g, expr const & h, expr const & h_type) {
|
|
|
|
if (m_nindices == 0)
|
|
|
|
return true;
|
|
|
|
buffer<expr> args;
|
|
|
|
get_app_args(h_type, args);
|
|
|
|
lean_assert(m_nindices <= args.size());
|
|
|
|
unsigned fidx = args.size() - m_nindices;
|
|
|
|
for (unsigned i = fidx; i < args.size(); i++) {
|
|
|
|
if (!is_local(args[i]))
|
|
|
|
return false; // the indices must be local constants
|
|
|
|
for (unsigned j = fidx; j < i; j++) {
|
|
|
|
if (mlocal_name(args[j]) == mlocal_name(args[i]))
|
|
|
|
return false; // the indices must be distinct local constants
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer<expr> hyps;
|
|
|
|
g.get_hyps(hyps);
|
|
|
|
for (expr const & h1 : hyps) {
|
|
|
|
if (mlocal_name(h1) == mlocal_name(h))
|
|
|
|
continue;
|
|
|
|
if (std::any_of(args.end() - m_nindices, args.end(), [&](expr const & arg) { return mlocal_name(arg) == mlocal_name(h1); }))
|
|
|
|
continue;
|
|
|
|
// h1 is not h nor any of the indices
|
|
|
|
// Thus, it must not depend on the indices
|
|
|
|
if (std::any_of(args.end() - m_nindices, args.end(), [&](expr const & arg) { return depends_on(h1, arg); }))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:13:45 +00:00
|
|
|
/** \brief Split hyps into two "telescopes".
|
|
|
|
- non_deps : hypotheses that do not depend on H
|
|
|
|
- deps : hypotheses that depend on H (directly or indirectly)
|
|
|
|
*/
|
|
|
|
void split_deps(buffer<expr> const & hyps, expr const & H, buffer<expr> & non_deps, buffer<expr> & deps) {
|
|
|
|
for (expr const & hyp : hyps) {
|
|
|
|
expr const & hyp_type = mlocal_type(hyp);
|
|
|
|
if (depends_on(hyp_type, H) || std::any_of(deps.begin(), deps.end(), [&](expr const & dep) { return depends_on(hyp_type, dep); })) {
|
|
|
|
deps.push_back(hyp);
|
|
|
|
} else {
|
|
|
|
non_deps.push_back(hyp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Given a goal of the form
|
|
|
|
|
|
|
|
Ctx, h : I A j, D |- T
|
|
|
|
|
|
|
|
where the type of h is the inductive datatype (I A j) where A are parameters,
|
|
|
|
and j the indices. Generate the goal
|
|
|
|
|
|
|
|
Ctx, h : I A j, D, j' : J, h' : I A j' |- j == j' -> h == h' -> T
|
|
|
|
|
|
|
|
Remark: (j == j' -> h == h') is a "telescopic" equality. If the environment
|
|
|
|
is proof irrelevant, it is built using homogenous and heterogeneous equalities.
|
|
|
|
If the environment is proof relevant, it is built using homogeneous equality
|
|
|
|
and the eq.rec (this construction is much more complex than the proof irrelevant
|
|
|
|
one).
|
|
|
|
|
|
|
|
Remark: j is sequence of terms, and j' a sequence of local constants.
|
|
|
|
|
|
|
|
The original goal is solved if we can solve the produced goal.
|
|
|
|
|
|
|
|
\remark h_type is mlocal_type(h) after normalization
|
|
|
|
*/
|
2014-11-27 08:06:26 +00:00
|
|
|
goal generalize_indices(goal const & g, expr const & h, expr const & h_type) {
|
2014-11-27 05:28:00 +00:00
|
|
|
buffer<expr> hyps;
|
|
|
|
g.get_hyps(hyps);
|
|
|
|
expr m = g.get_meta();
|
|
|
|
expr m_type = g.get_type();
|
2014-11-29 05:56:13 +00:00
|
|
|
name h_new_name = get_unused_name(local_pp_name(h), hyps);
|
2014-11-27 05:28:00 +00:00
|
|
|
buffer<expr> I_args;
|
|
|
|
expr const & I = get_app_args(h_type, I_args);
|
2014-11-27 18:37:22 +00:00
|
|
|
expr h_new_type = mk_app(I, I_args.size() - m_nindices, I_args.data());
|
2015-02-01 18:47:32 +00:00
|
|
|
expr d = whnf(infer_type(h_new_type));
|
2014-11-27 18:37:22 +00:00
|
|
|
name t_prefix("t");
|
|
|
|
unsigned nidx = 1;
|
2014-12-19 05:29:13 +00:00
|
|
|
if (m_proof_irrel) {
|
|
|
|
unsigned eq_idx = 1;
|
|
|
|
name eq_prefix("H");
|
|
|
|
buffer<expr> ts;
|
|
|
|
buffer<expr> eqs;
|
|
|
|
buffer<expr> refls;
|
|
|
|
auto add_eq = [&](expr const & lhs, expr const & rhs) {
|
|
|
|
pair<expr, expr> p = mk_eq(lhs, rhs);
|
|
|
|
expr new_eq = p.first;
|
|
|
|
expr new_refl = p.second;
|
|
|
|
eqs.push_back(mk_local(m_ngen.next(), g.get_unused_name(eq_prefix, eq_idx), new_eq, binder_info()));
|
|
|
|
refls.push_back(new_refl);
|
|
|
|
};
|
|
|
|
for (unsigned i = I_args.size() - m_nindices; i < I_args.size(); i++) {
|
|
|
|
expr t_type = binding_domain(d);
|
|
|
|
expr t = mk_local(m_ngen.next(), g.get_unused_name(t_prefix, nidx), t_type, binder_info());
|
|
|
|
expr const & index = I_args[i];
|
|
|
|
add_eq(t, index);
|
|
|
|
h_new_type = mk_app(h_new_type, t);
|
|
|
|
hyps.push_back(t);
|
|
|
|
ts.push_back(t);
|
|
|
|
d = instantiate(binding_body(d), t);
|
|
|
|
}
|
|
|
|
expr h_new = mk_local(m_ngen.next(), h_new_name, h_new_type, local_info(h));
|
|
|
|
if (m_dep_elim)
|
|
|
|
add_eq(h_new, h);
|
|
|
|
hyps.push_back(h_new);
|
|
|
|
expr new_type = Pi(eqs, g.get_type());
|
|
|
|
expr new_meta = mk_app(mk_metavar(m_ngen.next(), Pi(hyps, new_type)), hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
|
|
|
expr val = g.abstract(mk_app(mk_app(mk_app(Fun(ts, Fun(h_new, new_meta)), m_nindices, I_args.end() - m_nindices), h),
|
|
|
|
refls));
|
|
|
|
assign(g.get_name(), val);
|
|
|
|
return new_g;
|
|
|
|
} else {
|
|
|
|
// proof relevant version
|
|
|
|
buffer<expr> ss;
|
|
|
|
buffer<expr> ts;
|
|
|
|
buffer<expr> refls;
|
|
|
|
for (unsigned i = I_args.size() - m_nindices; i < I_args.size(); i++) {
|
|
|
|
expr t_type = binding_domain(d);
|
|
|
|
expr t = mk_local(m_ngen.next(), g.get_unused_name(t_prefix, nidx), t_type, binder_info());
|
|
|
|
h_new_type = mk_app(h_new_type, t);
|
|
|
|
ss.push_back(I_args[i]);
|
2014-12-31 05:22:50 +00:00
|
|
|
refls.push_back(mk_refl(m_tc, I_args[i]));
|
2014-12-19 05:29:13 +00:00
|
|
|
hyps.push_back(t);
|
|
|
|
ts.push_back(t);
|
|
|
|
d = instantiate(binding_body(d), t);
|
|
|
|
}
|
|
|
|
expr h_new = mk_local(m_ngen.next(), h_new_name, h_new_type, local_info(h));
|
|
|
|
ts.push_back(h_new);
|
|
|
|
ss.push_back(h);
|
2014-12-31 05:22:50 +00:00
|
|
|
refls.push_back(mk_refl(m_tc, h));
|
2014-12-19 05:29:13 +00:00
|
|
|
hyps.push_back(h_new);
|
|
|
|
buffer<expr> eqs;
|
2014-12-31 05:22:50 +00:00
|
|
|
mk_telescopic_eq(m_tc, ss, ts, eqs);
|
2014-12-19 05:29:13 +00:00
|
|
|
ts.pop_back();
|
|
|
|
expr new_type = Pi(eqs, g.get_type());
|
|
|
|
expr new_meta = mk_app(mk_metavar(m_ngen.next(), Pi(hyps, new_type)), hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
|
|
|
expr val = g.abstract(mk_app(mk_app(mk_app(Fun(ts, Fun(h_new, new_meta)), m_nindices, I_args.end() - m_nindices), h),
|
|
|
|
refls));
|
|
|
|
assign(g.get_name(), val);
|
|
|
|
return new_g;
|
2014-11-27 05:28:00 +00:00
|
|
|
}
|
2014-11-27 08:06:26 +00:00
|
|
|
}
|
2014-11-27 05:28:00 +00:00
|
|
|
|
2015-01-01 23:13:45 +00:00
|
|
|
/** \brief Generalize h dependencies.
|
|
|
|
|
2015-01-02 01:37:17 +00:00
|
|
|
This tactic uses this method only if has_indep_indices(hyps, h, h_type) returns true.
|
2015-01-01 23:13:45 +00:00
|
|
|
|
|
|
|
The hypotheses that depend on h are stored in deps.
|
|
|
|
*/
|
|
|
|
goal generalize_dependecies(goal const & g, expr const & h, buffer<expr> & deps) {
|
|
|
|
buffer<expr> hyps;
|
|
|
|
g.get_hyps(hyps);
|
|
|
|
hyps.erase_elem(h);
|
|
|
|
buffer<expr> non_deps;
|
|
|
|
split_deps(hyps, h, non_deps, deps);
|
|
|
|
buffer<expr> & new_hyps = non_deps;
|
|
|
|
new_hyps.push_back(h);
|
|
|
|
expr new_type = Pi(deps, g.get_type());
|
|
|
|
expr new_meta = mk_app(mk_metavar(m_ngen.next(), Pi(new_hyps, new_type)), new_hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
|
|
|
expr val = g.abstract(mk_app(new_meta, deps));
|
|
|
|
assign(g.get_name(), val);
|
|
|
|
return new_g;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Given a goal of the form
|
|
|
|
|
|
|
|
Ctx, h : I A j |- T[h]
|
|
|
|
|
|
|
|
produce the subgoals
|
|
|
|
|
|
|
|
Ctx, h : I A j |- b_1 : B_1 -> T [C_1 A b_1]
|
|
|
|
...
|
|
|
|
Ctx, h : I A j |- b_n : B_n -> T [C_n A b_n]
|
|
|
|
|
|
|
|
where the C_i's are the constructors (aka introduction rules) of the inductive datatype h.
|
|
|
|
|
|
|
|
The hypothesis h must be the last hypothesis in the input goal. Consequently, no other
|
|
|
|
hypothesis depends on it.
|
|
|
|
|
|
|
|
The implementation list is external auxiliary data associated with constructors.
|
|
|
|
For example, we use it to compile recursive equations. Each implementation corresponds to one
|
|
|
|
equation. Each equation is tagged with a constructor C_i. The constructor is retrieved using
|
|
|
|
the virtual method get_constructor_name. We split the list imps in n lists. One for each
|
|
|
|
subgoal. An implementation associated with the constructor i is in the i-th resulting list.
|
|
|
|
|
|
|
|
\remark This method assumes the indices j are local constants, and only h and j may depend on j.
|
|
|
|
*/
|
2014-12-31 05:22:50 +00:00
|
|
|
std::pair<list<goal>, list<implementation_list>> apply_cases_on(goal const & g, implementation_list const & imps) {
|
2014-11-27 08:06:26 +00:00
|
|
|
buffer<expr> hyps;
|
|
|
|
g.get_hyps(hyps);
|
|
|
|
expr const & h = hyps.back();
|
2015-02-01 18:47:32 +00:00
|
|
|
expr const & h_type = whnf(mlocal_type(h));
|
2014-11-27 08:06:26 +00:00
|
|
|
buffer<expr> I_args;
|
|
|
|
expr const & I = get_app_args(h_type, I_args);
|
2014-12-31 05:22:50 +00:00
|
|
|
name const & I_name = const_name(I);
|
2014-11-27 08:06:26 +00:00
|
|
|
expr g_type = g.get_type();
|
|
|
|
expr cases_on;
|
2015-01-29 01:22:18 +00:00
|
|
|
if (m_cases_on_decl.get_num_univ_params() != m_I_decl.get_num_univ_params()) {
|
2014-12-31 05:22:50 +00:00
|
|
|
level g_lvl = sort_level(m_tc.ensure_type(g_type).first);
|
|
|
|
cases_on = mk_constant({I_name, "cases_on"}, cons(g_lvl, const_levels(I)));
|
2014-11-27 08:06:26 +00:00
|
|
|
} else {
|
2014-12-31 05:22:50 +00:00
|
|
|
cases_on = mk_constant({I_name, "cases_on"}, const_levels(I));
|
2014-11-27 08:06:26 +00:00
|
|
|
}
|
|
|
|
// add params
|
|
|
|
cases_on = mk_app(cases_on, m_nparams, I_args.data());
|
|
|
|
// add type former
|
2014-11-27 18:37:22 +00:00
|
|
|
expr type_former = g_type;
|
|
|
|
if (m_dep_elim)
|
|
|
|
type_former = Fun(h, type_former);
|
|
|
|
type_former = Fun(m_nindices, I_args.end() - m_nindices, type_former);
|
2014-11-27 08:06:26 +00:00
|
|
|
cases_on = mk_app(cases_on, type_former);
|
|
|
|
// add indices
|
|
|
|
cases_on = mk_app(cases_on, m_nindices, I_args.end() - m_nindices);
|
|
|
|
// add h
|
|
|
|
cases_on = mk_app(cases_on, h);
|
2014-12-31 05:22:50 +00:00
|
|
|
buffer<name> intro_names;
|
|
|
|
get_intro_rule_names(m_env, I_name, intro_names);
|
|
|
|
lean_assert(m_nminors == intro_names.size());
|
2014-11-27 08:06:26 +00:00
|
|
|
buffer<expr> new_hyps;
|
|
|
|
new_hyps.append(hyps.size() - m_nindices - 1, hyps.data());
|
|
|
|
// add a subgoal for each minor premise of cases_on
|
2015-02-01 18:47:32 +00:00
|
|
|
expr cases_on_type = whnf(infer_type(cases_on));
|
2014-11-27 08:06:26 +00:00
|
|
|
buffer<goal> new_goals;
|
2014-12-31 05:22:50 +00:00
|
|
|
buffer<implementation_list> new_imps;
|
2014-11-27 08:06:26 +00:00
|
|
|
for (unsigned i = 0; i < m_nminors; i++) {
|
|
|
|
expr new_type = binding_domain(cases_on_type);
|
|
|
|
expr new_meta = mk_app(mk_metavar(m_ngen.next(), Pi(new_hyps, new_type)), new_hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
|
|
|
new_goals.push_back(new_g);
|
|
|
|
cases_on = mk_app(cases_on, new_meta);
|
2015-02-01 18:47:32 +00:00
|
|
|
cases_on_type = whnf(binding_body(cases_on_type)); // the minor premises do not depend on each other
|
2014-12-31 05:22:50 +00:00
|
|
|
name const & intro_name = intro_names[i];
|
|
|
|
new_imps.push_back(filter(imps, [&](implementation_ptr const & imp) { return imp->get_constructor_name() == intro_name; }));
|
2014-11-27 08:06:26 +00:00
|
|
|
}
|
|
|
|
expr val = g.abstract(cases_on);
|
2014-12-01 06:35:32 +00:00
|
|
|
assign(g.get_name(), val);
|
2014-12-31 05:22:50 +00:00
|
|
|
return mk_pair(to_list(new_goals), to_list(new_imps));
|
2014-11-27 08:06:26 +00:00
|
|
|
}
|
2014-11-27 05:28:00 +00:00
|
|
|
|
2014-11-27 08:06:26 +00:00
|
|
|
// Store in \c r the number of arguments for each cases_on minor.
|
|
|
|
void get_minors_nargs(buffer<unsigned> & r) {
|
|
|
|
expr cases_on_type = m_cases_on_decl.get_type();
|
|
|
|
for (unsigned i = 0; i < m_nparams + 1 + m_nindices + 1; i++)
|
|
|
|
cases_on_type = binding_body(cases_on_type);
|
|
|
|
for (unsigned i = 0; i < m_nminors; i++) {
|
|
|
|
expr minor_type = binding_domain(cases_on_type);
|
|
|
|
unsigned nargs = 0;
|
|
|
|
while (is_pi(minor_type)) {
|
|
|
|
nargs++;
|
|
|
|
minor_type = binding_body(minor_type);
|
|
|
|
}
|
|
|
|
r.push_back(nargs);
|
|
|
|
cases_on_type = binding_body(cases_on_type);
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 05:28:00 +00:00
|
|
|
|
2015-01-01 23:13:45 +00:00
|
|
|
/**
|
|
|
|
The method apply_cases_on produces subgoals of the form
|
|
|
|
|
|
|
|
Ctx, h : I A j |- b_i : B_i -> T [C_i A b_i]
|
|
|
|
|
|
|
|
The b_i are the arguments of the constructor C_i.
|
|
|
|
This method moves the b_i's to the hypotheses list.
|
|
|
|
*/
|
2015-01-03 06:00:01 +00:00
|
|
|
std::pair<list<goal>, list<list<expr>>> intros_minors_args(list<goal> gs) {
|
2014-11-27 08:06:26 +00:00
|
|
|
buffer<unsigned> minors_nargs;
|
|
|
|
get_minors_nargs(minors_nargs);
|
|
|
|
lean_assert(length(gs) == minors_nargs.size());
|
|
|
|
buffer<goal> new_gs;
|
2015-01-03 06:00:01 +00:00
|
|
|
buffer<list<expr>> new_args;
|
2014-11-27 08:06:26 +00:00
|
|
|
for (unsigned i = 0; i < minors_nargs.size(); i++) {
|
|
|
|
goal const & g = head(gs);
|
|
|
|
unsigned nargs = minors_nargs[i];
|
|
|
|
buffer<expr> hyps;
|
|
|
|
g.get_hyps(hyps);
|
|
|
|
buffer<expr> new_hyps;
|
|
|
|
new_hyps.append(hyps);
|
|
|
|
expr g_type = g.get_type();
|
2015-01-03 06:00:01 +00:00
|
|
|
buffer<expr> curr_new_args;
|
2014-11-27 08:06:26 +00:00
|
|
|
for (unsigned i = 0; i < nargs; i++) {
|
|
|
|
expr type = binding_domain(g_type);
|
2014-11-29 06:25:37 +00:00
|
|
|
name new_h_name;
|
|
|
|
if (m_ids) {
|
|
|
|
new_h_name = head(m_ids);
|
|
|
|
m_ids = tail(m_ids);
|
|
|
|
} else {
|
|
|
|
new_h_name = binding_name(g_type);
|
|
|
|
}
|
|
|
|
expr new_h = mk_local(m_ngen.next(), get_unused_name(new_h_name, new_hyps), type, binder_info());
|
2015-01-03 06:00:01 +00:00
|
|
|
curr_new_args.push_back(new_h);
|
2014-11-27 08:06:26 +00:00
|
|
|
new_hyps.push_back(new_h);
|
|
|
|
g_type = instantiate(binding_body(g_type), new_h);
|
|
|
|
}
|
2015-01-01 02:55:18 +00:00
|
|
|
new_args.push_back(to_list(curr_new_args));
|
2014-11-27 08:06:26 +00:00
|
|
|
g_type = head_beta_reduce(g_type);
|
|
|
|
expr new_meta = mk_app(mk_metavar(m_ngen.next(), Pi(new_hyps, g_type)), new_hyps);
|
|
|
|
goal new_g(new_meta, g_type);
|
|
|
|
new_gs.push_back(new_g);
|
|
|
|
expr val = g.abstract(Fun(nargs, new_hyps.end() - nargs, new_meta));
|
2014-12-01 06:35:32 +00:00
|
|
|
assign(g.get_name(), val);
|
2014-11-27 08:06:26 +00:00
|
|
|
gs = tail(gs);
|
|
|
|
}
|
2015-01-01 02:55:18 +00:00
|
|
|
return mk_pair(to_list(new_gs), to_list(new_args));
|
2014-11-27 08:06:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-29 05:56:13 +00:00
|
|
|
struct inversion_exception : public exception {
|
|
|
|
inversion_exception(char const * msg):exception(msg) {}
|
|
|
|
inversion_exception(sstream const & strm):exception(strm) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
[[ noreturn ]] void throw_ill_formed_goal() {
|
|
|
|
throw inversion_exception("ill-formed goal");
|
|
|
|
}
|
|
|
|
|
|
|
|
[[ noreturn ]] void throw_ill_typed_goal() {
|
|
|
|
throw inversion_exception("ill-typed goal");
|
|
|
|
}
|
|
|
|
|
2014-12-19 05:29:13 +00:00
|
|
|
void throw_unification_eq_rec_failure() {
|
|
|
|
throw inversion_exception("unification failed to eliminate eq.rec in homogeneous equality");
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:13:45 +00:00
|
|
|
/** \brief Process goal of the form: Pi (H : eq.rec A s C a s p = b), R
|
|
|
|
The idea is to reduce it to
|
|
|
|
Pi (H : a = b), R
|
|
|
|
when A is a hset
|
|
|
|
and then invoke intro_next_eq recursively.
|
|
|
|
|
|
|
|
\remark \c type is the type of \c g after some normalization
|
|
|
|
*/
|
2014-12-19 05:29:13 +00:00
|
|
|
goal intro_next_eq_rec(goal const & g, expr const & type) {
|
|
|
|
lean_assert(is_pi(type));
|
|
|
|
buffer<expr> hyps;
|
|
|
|
g.get_hyps(hyps);
|
|
|
|
expr const & eq = binding_domain(type);
|
|
|
|
expr const & lhs = app_arg(app_fn(eq));
|
|
|
|
expr const & rhs = app_arg(eq);
|
|
|
|
lean_assert(is_eq_rec(lhs));
|
|
|
|
// lhs is of the form (eq.rec A s C a s p)
|
|
|
|
// aux_eq is a term of type ((eq.rec A s C a s p) = a)
|
2014-12-31 05:22:50 +00:00
|
|
|
auto aux_eq = apply_eq_rec_eq(m_tc, m_ios, to_list(hyps), lhs);
|
2014-12-19 05:29:13 +00:00
|
|
|
if (!aux_eq)
|
|
|
|
throw_unification_eq_rec_failure();
|
|
|
|
buffer<expr> lhs_args;
|
|
|
|
get_app_args(lhs, lhs_args);
|
|
|
|
expr const & reduced_lhs = lhs_args[3];
|
2014-12-31 05:22:50 +00:00
|
|
|
expr new_eq = ::lean::mk_eq(m_tc, reduced_lhs, rhs);
|
2014-12-19 05:29:13 +00:00
|
|
|
expr new_type = update_binding(type, new_eq, binding_body(type));
|
|
|
|
expr new_meta = mk_app(mk_metavar(m_ngen.next(), Pi(hyps, new_type)), hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
|
|
|
// create assignment for g
|
2015-02-01 18:47:32 +00:00
|
|
|
expr A = infer_type(lhs);
|
2014-12-31 05:22:50 +00:00
|
|
|
level lvl = sort_level(m_tc.ensure_type(A).first);
|
2014-12-19 05:29:13 +00:00
|
|
|
// old_eq : eq.rec A s C a s p = b
|
|
|
|
expr old_eq = mk_local(m_ngen.next(), binding_name(type), eq, binder_info());
|
|
|
|
// aux_eq : a = eq.rec A s C a s p
|
2015-01-24 00:50:32 +00:00
|
|
|
expr trans_eq = mk_app({mk_constant(get_eq_trans_name(), {lvl}), A, reduced_lhs, lhs, rhs, *aux_eq, old_eq});
|
2014-12-19 05:29:13 +00:00
|
|
|
// trans_eq : a = b
|
|
|
|
expr val = g.abstract(Fun(old_eq, mk_app(new_meta, trans_eq)));
|
|
|
|
assign(g.get_name(), val);
|
|
|
|
return intro_next_eq(new_g);
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:13:45 +00:00
|
|
|
/**
|
|
|
|
\brief Process goal of the form: Ctx |- Pi (H : a == b), R when a and b have the same type
|
|
|
|
The idea is to reduce it to
|
|
|
|
Ctx, H : a = b |- R
|
|
|
|
This method is only used if the environment has a proof irrelevant Prop.
|
|
|
|
*/
|
2014-12-21 23:19:25 +00:00
|
|
|
goal intro_next_heq(goal const & g) {
|
2014-12-19 05:29:13 +00:00
|
|
|
lean_assert(m_proof_irrel);
|
2014-12-21 23:19:25 +00:00
|
|
|
expr const & type = g.get_type();
|
2014-12-19 05:29:13 +00:00
|
|
|
expr eq = binding_domain(type);
|
2015-01-24 00:50:32 +00:00
|
|
|
lean_assert(const_name(get_app_fn(eq)) == get_heq_name());
|
2014-12-19 05:29:13 +00:00
|
|
|
buffer<expr> args;
|
|
|
|
expr const & heq_fn = get_app_args(eq, args);
|
|
|
|
constraint_seq cs;
|
2014-12-31 05:22:50 +00:00
|
|
|
if (m_tc.is_def_eq(args[0], args[2], justification(), cs) && !cs) {
|
2014-12-19 05:29:13 +00:00
|
|
|
buffer<expr> hyps;
|
|
|
|
g.get_hyps(hyps);
|
2015-01-24 00:50:32 +00:00
|
|
|
expr new_eq = mk_app(mk_constant(get_eq_name(), const_levels(heq_fn)), args[0], args[1], args[3]);
|
2014-12-19 05:29:13 +00:00
|
|
|
expr new_hyp = mk_local(m_ngen.next(), g.get_unused_name(binding_name(type)), new_eq, binder_info());
|
|
|
|
expr new_type = instantiate(binding_body(type), new_hyp);
|
|
|
|
hyps.push_back(new_hyp);
|
|
|
|
expr new_mvar = mk_metavar(m_ngen.next(), Pi(hyps, new_type));
|
|
|
|
expr new_meta = mk_app(new_mvar, hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
|
|
|
hyps.pop_back();
|
|
|
|
expr H = mk_local(m_ngen.next(), g.get_unused_name(binding_name(type)), binding_domain(type), binder_info());
|
2015-01-24 00:50:32 +00:00
|
|
|
expr to_eq = mk_app(mk_constant(get_heq_to_eq_name(), const_levels(heq_fn)), args[0], args[1], args[3], H);
|
2014-12-19 05:29:13 +00:00
|
|
|
expr val = g.abstract(Fun(H, mk_app(mk_app(new_mvar, hyps), to_eq)));
|
|
|
|
assign(g.get_name(), val);
|
|
|
|
return new_g;
|
|
|
|
} else {
|
|
|
|
throw inversion_exception("unification failed to reduce heterogeneous equality into homogeneous one");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:13:45 +00:00
|
|
|
/** \brief Process goal of the form: Ctx |- Pi (H : a = b), R
|
|
|
|
The idea is to reduce it to
|
|
|
|
Ctx, H : a = b |- R
|
|
|
|
|
|
|
|
\remark \c type is the type of \c g after some normalization
|
|
|
|
*/
|
2014-12-19 05:29:13 +00:00
|
|
|
goal intro_next_eq_simple(goal const & g, expr const & type) {
|
|
|
|
expr eq = binding_domain(type);
|
2015-01-24 00:50:32 +00:00
|
|
|
lean_assert(const_name(get_app_fn(eq)) == get_eq_name());
|
2014-12-19 05:29:13 +00:00
|
|
|
buffer<expr> hyps;
|
|
|
|
g.get_hyps(hyps);
|
|
|
|
expr new_hyp = mk_local(m_ngen.next(), g.get_unused_name(binding_name(type)), binding_domain(type), binder_info());
|
|
|
|
expr new_type = instantiate(binding_body(type), new_hyp);
|
|
|
|
hyps.push_back(new_hyp);
|
|
|
|
expr new_meta = mk_app(mk_metavar(m_ngen.next(), Pi(hyps, new_type)), hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
|
|
|
expr val = g.abstract(Fun(new_hyp, new_meta));
|
|
|
|
assign(g.get_name(), val);
|
|
|
|
return new_g;
|
|
|
|
}
|
|
|
|
|
2014-11-29 05:56:13 +00:00
|
|
|
goal intro_next_eq(goal const & g) {
|
2014-12-21 23:19:25 +00:00
|
|
|
expr type = g.get_type();
|
2014-11-29 05:56:13 +00:00
|
|
|
if (!is_pi(type))
|
|
|
|
throw_ill_formed_goal();
|
|
|
|
expr eq = binding_domain(type);
|
|
|
|
expr const & eq_fn = get_app_fn(eq);
|
|
|
|
if (!is_constant(eq_fn))
|
|
|
|
throw_ill_formed_goal();
|
2015-01-24 00:50:32 +00:00
|
|
|
if (const_name(eq_fn) == get_eq_name()) {
|
2014-12-19 05:29:13 +00:00
|
|
|
expr const & lhs = app_arg(app_fn(eq));
|
2014-12-21 23:19:25 +00:00
|
|
|
expr const & rhs = app_arg(eq);
|
2015-02-01 18:47:32 +00:00
|
|
|
expr new_lhs = whnf(lhs);
|
|
|
|
expr new_rhs = whnf(rhs);
|
2014-12-21 23:19:25 +00:00
|
|
|
if (lhs != new_lhs || rhs != new_rhs) {
|
|
|
|
eq = mk_app(app_fn(app_fn(eq)), new_lhs, new_rhs);
|
|
|
|
type = update_binding(type, eq, binding_body(type));
|
|
|
|
}
|
|
|
|
if (!m_proof_irrel && is_eq_rec(new_lhs)) {
|
2014-12-19 05:29:13 +00:00
|
|
|
return intro_next_eq_rec(g, type);
|
2014-11-29 05:56:13 +00:00
|
|
|
} else {
|
2014-12-19 05:29:13 +00:00
|
|
|
return intro_next_eq_simple(g, type);
|
2014-11-29 05:56:13 +00:00
|
|
|
}
|
2015-01-24 00:50:32 +00:00
|
|
|
} else if (m_proof_irrel && const_name(eq_fn) == get_heq_name()) {
|
2014-12-21 23:19:25 +00:00
|
|
|
return intro_next_heq(g);
|
2014-11-29 05:56:13 +00:00
|
|
|
} else {
|
|
|
|
throw_ill_formed_goal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:13:45 +00:00
|
|
|
/**
|
|
|
|
\brief The no_confusion constructions uses lifts in the proof relevant version.
|
|
|
|
We must apply lift.down to eliminate the auxiliary lift.
|
|
|
|
*/
|
2014-12-19 05:29:13 +00:00
|
|
|
expr lift_down(expr const & v) {
|
|
|
|
if (!m_proof_irrel) {
|
2015-02-01 18:47:32 +00:00
|
|
|
expr v_type = whnf(infer_type(v));
|
2014-12-19 05:29:13 +00:00
|
|
|
if (!is_app(v_type))
|
|
|
|
throw_unification_eq_rec_failure();
|
|
|
|
expr const & lift = app_fn(v_type);
|
2015-01-24 00:50:32 +00:00
|
|
|
if (!is_constant(lift) || const_name(lift) != get_lift_name())
|
2014-12-19 05:29:13 +00:00
|
|
|
throw_unification_eq_rec_failure();
|
2015-01-24 00:50:32 +00:00
|
|
|
return mk_app(mk_constant(get_lift_down_name(), const_levels(lift)), app_arg(v_type), v);
|
2014-12-19 05:29:13 +00:00
|
|
|
} else {
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 06:00:01 +00:00
|
|
|
buffer<expr> m_c_args; // current intro/constructor args that may be renamed by unify_eqs
|
2015-01-01 02:55:18 +00:00
|
|
|
rename_map m_renames;
|
|
|
|
implementation_list m_imps;
|
|
|
|
|
2015-01-03 06:00:01 +00:00
|
|
|
void store_rename(expr const & old_hyp, expr const & new_hyp) {
|
|
|
|
for (expr & c_arg : m_c_args) {
|
|
|
|
if (is_local(c_arg) && mlocal_name(old_hyp) == mlocal_name(c_arg))
|
|
|
|
c_arg = new_hyp;
|
|
|
|
}
|
|
|
|
if (is_local(new_hyp))
|
|
|
|
m_renames.insert(mlocal_name(old_hyp), mlocal_name(new_hyp));
|
|
|
|
}
|
|
|
|
|
2015-01-01 23:13:45 +00:00
|
|
|
/** \brief Update m_renames with old_hyps --> new_hyps. */
|
2015-01-01 02:55:18 +00:00
|
|
|
void store_renames(buffer<expr> const & old_hyps, buffer<expr> const & new_hyps) {
|
|
|
|
lean_assert(old_hyps.size() == new_hyps.size());
|
|
|
|
for (unsigned i = 0; i < old_hyps.size(); i++) {
|
2015-01-03 06:00:01 +00:00
|
|
|
store_rename(old_hyps[i], new_hyps[i]);
|
2015-01-01 02:55:18 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-31 05:22:50 +00:00
|
|
|
|
2015-01-01 02:55:18 +00:00
|
|
|
// Remark: it also updates m_renames and m_imps
|
|
|
|
optional<goal> unify_eqs(goal g, unsigned neqs) {
|
2014-11-29 05:56:13 +00:00
|
|
|
if (neqs == 0)
|
2015-01-01 02:55:18 +00:00
|
|
|
return optional<goal>(g); // done
|
2014-11-29 05:56:13 +00:00
|
|
|
g = intro_next_eq(g);
|
|
|
|
buffer<expr> hyps;
|
|
|
|
g.get_hyps(hyps);
|
|
|
|
lean_assert(!hyps.empty());
|
2014-12-02 03:08:56 +00:00
|
|
|
expr eq = hyps.back();
|
2014-11-29 05:56:13 +00:00
|
|
|
buffer<expr> eq_args;
|
|
|
|
get_app_args(mlocal_type(eq), eq_args);
|
2015-02-01 18:47:32 +00:00
|
|
|
expr const & A = whnf(eq_args[0]);
|
|
|
|
expr lhs = whnf(eq_args[1]);
|
|
|
|
expr rhs = whnf(eq_args[2]);
|
2014-11-29 05:56:13 +00:00
|
|
|
constraint_seq cs;
|
2014-12-31 05:22:50 +00:00
|
|
|
if (m_proof_irrel && m_tc.is_def_eq(lhs, rhs, justification(), cs) && !cs) {
|
2014-11-29 05:56:13 +00:00
|
|
|
// deletion transition: t == t
|
2014-11-29 17:36:41 +00:00
|
|
|
hyps.pop_back(); // remove t == t equality
|
|
|
|
expr new_type = g.get_type();
|
|
|
|
expr new_meta = mk_app(mk_metavar(m_ngen.next(), Pi(hyps, new_type)), hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
|
|
|
expr val = g.abstract(new_meta);
|
2014-12-01 06:35:32 +00:00
|
|
|
assign(g.get_name(), val);
|
2015-01-01 02:55:18 +00:00
|
|
|
return unify_eqs(new_g, neqs-1);
|
2014-11-29 05:56:13 +00:00
|
|
|
}
|
|
|
|
buffer<expr> lhs_args, rhs_args;
|
|
|
|
expr const & lhs_fn = get_app_args(lhs, lhs_args);
|
|
|
|
expr const & rhs_fn = get_app_args(rhs, rhs_args);
|
|
|
|
expr const & g_type = g.get_type();
|
2014-12-31 05:22:50 +00:00
|
|
|
level const & g_lvl = sort_level(m_tc.ensure_type(g_type).first);
|
2014-11-29 05:56:13 +00:00
|
|
|
if (is_constant(lhs_fn) &&
|
|
|
|
is_constant(rhs_fn) &&
|
|
|
|
inductive::is_intro_rule(m_env, const_name(lhs_fn)) &&
|
|
|
|
inductive::is_intro_rule(m_env, const_name(rhs_fn))) {
|
|
|
|
buffer<expr> A_args;
|
|
|
|
expr const & A_fn = get_app_args(A, A_args);
|
|
|
|
if (!is_constant(A_fn) || !inductive::is_inductive_decl(m_env, const_name(A_fn)))
|
|
|
|
throw_ill_typed_goal();
|
|
|
|
name no_confusion_name(const_name(A_fn), "no_confusion");
|
|
|
|
if (!m_env.find(no_confusion_name))
|
|
|
|
throw inversion_exception(sstream() << "construction '" << no_confusion_name << "' is not available in the environment");
|
|
|
|
expr no_confusion = mk_app(mk_app(mk_constant(no_confusion_name, cons(g_lvl, const_levels(A_fn))), A_args), g_type, lhs, rhs, eq);
|
|
|
|
if (const_name(lhs_fn) == const_name(rhs_fn)) {
|
|
|
|
// injectivity transition
|
2015-02-01 18:47:32 +00:00
|
|
|
expr new_type = binding_domain(whnf(infer_type(no_confusion)));
|
2014-12-19 05:29:13 +00:00
|
|
|
if (m_proof_irrel)
|
|
|
|
hyps.pop_back(); // remove processed equality
|
2014-11-29 05:56:13 +00:00
|
|
|
expr new_mvar = mk_metavar(m_ngen.next(), Pi(hyps, new_type));
|
|
|
|
expr new_meta = mk_app(new_mvar, hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
2014-12-19 05:29:13 +00:00
|
|
|
expr val = g.abstract(lift_down(mk_app(no_confusion, new_meta)));
|
2014-12-01 06:35:32 +00:00
|
|
|
assign(g.get_name(), val);
|
2014-11-29 06:07:35 +00:00
|
|
|
unsigned A_nparams = *inductive::get_num_params(m_env, const_name(A_fn));
|
|
|
|
lean_assert(lhs_args.size() >= A_nparams);
|
2015-01-01 02:55:18 +00:00
|
|
|
return unify_eqs(new_g, neqs - 1 + lhs_args.size() - A_nparams);
|
2014-11-29 05:56:13 +00:00
|
|
|
} else {
|
|
|
|
// conflict transition, eq is of the form c_1 ... = c_2 ..., where c_1 and c_2 are different constructors/intro rules.
|
2014-12-19 05:29:13 +00:00
|
|
|
expr val = g.abstract(lift_down(no_confusion));
|
2014-12-01 06:35:32 +00:00
|
|
|
assign(g.get_name(), val);
|
2015-01-01 02:55:18 +00:00
|
|
|
return optional<goal>(); // goal has been solved
|
2014-11-29 05:56:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_local(rhs)) {
|
|
|
|
// solution transition, eq is of the form t = y, where y is a local constant
|
|
|
|
|
|
|
|
// assume the current goal is of the form
|
|
|
|
//
|
|
|
|
// non_deps, deps[rhs], H : lhs = rhs |- C[rhs]
|
|
|
|
//
|
|
|
|
// We use non_deps to denote hypotheses that do not depend on rhs,
|
|
|
|
// and deps[rhs] to denote hypotheses that depend on it.
|
|
|
|
//
|
|
|
|
// The resultant goal is of the form
|
|
|
|
//
|
|
|
|
// non_deps, deps[lhs] |- C[lhs]
|
|
|
|
//
|
|
|
|
// Now, assume ?m1 is a solution for the resultant goal.
|
|
|
|
// Then,
|
|
|
|
//
|
|
|
|
// @eq.rec A lhs (fun rhs, Pi(deps[rhs], C[rhs])) (?m1 non_deps) rhs H deps[rhs]
|
|
|
|
//
|
|
|
|
// is a solution for the original goal.
|
|
|
|
// Remark: A is the type of lhs and rhs
|
|
|
|
hyps.pop_back(); // remove processed equality
|
|
|
|
buffer<expr> non_deps, deps;
|
|
|
|
split_deps(hyps, rhs, non_deps, deps);
|
2015-01-01 03:37:26 +00:00
|
|
|
if (deps.empty() && !depends_on(g_type, rhs)) {
|
|
|
|
// eq.rec is not necessary
|
|
|
|
buffer<expr> & new_hyps = non_deps;
|
|
|
|
expr new_type = g_type;
|
|
|
|
expr new_mvar = mk_metavar(m_ngen.next(), Pi(new_hyps, new_type));
|
|
|
|
expr new_meta = mk_app(new_mvar, new_hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
|
|
|
expr val = g.abstract(new_meta);
|
|
|
|
assign(g.get_name(), val);
|
|
|
|
return unify_eqs(new_g, neqs-1);
|
|
|
|
} else {
|
|
|
|
expr deps_g_type = Pi(deps, g_type);
|
|
|
|
level eq_rec_lvl1 = sort_level(m_tc.ensure_type(deps_g_type).first);
|
|
|
|
level eq_rec_lvl2 = sort_level(m_tc.ensure_type(A).first);
|
|
|
|
expr tformer;
|
|
|
|
if (m_proof_irrel)
|
|
|
|
tformer = Fun(rhs, deps_g_type);
|
|
|
|
else
|
|
|
|
tformer = Fun(rhs, Fun(eq, deps_g_type));
|
2015-01-24 00:50:32 +00:00
|
|
|
expr eq_rec = mk_constant(get_eq_rec_name(), {eq_rec_lvl1, eq_rec_lvl2});
|
2015-01-01 03:37:26 +00:00
|
|
|
eq_rec = mk_app(eq_rec, A, lhs, tformer);
|
|
|
|
buffer<expr> new_hyps;
|
|
|
|
new_hyps.append(non_deps);
|
|
|
|
expr new_type = instantiate(abstract_local(deps_g_type, rhs), lhs);
|
2015-01-03 06:00:01 +00:00
|
|
|
store_rename(rhs, lhs);
|
2015-01-05 03:56:10 +00:00
|
|
|
replace(m_imps, rhs, lhs);
|
2015-01-01 03:37:26 +00:00
|
|
|
if (!m_proof_irrel) {
|
|
|
|
new_type = abstract_local(new_type, eq);
|
|
|
|
new_type = instantiate(new_type, mk_refl(m_tc, lhs));
|
|
|
|
}
|
|
|
|
buffer<expr> new_deps;
|
|
|
|
for (unsigned i = 0; i < deps.size(); i++) {
|
|
|
|
expr new_hyp = mk_local(m_ngen.next(), binding_name(new_type), binding_domain(new_type),
|
|
|
|
binding_info(new_type));
|
|
|
|
new_hyps.push_back(new_hyp);
|
|
|
|
new_deps.push_back(new_hyp);
|
|
|
|
new_type = instantiate(binding_body(new_type), new_hyp);
|
|
|
|
}
|
2015-01-05 03:56:10 +00:00
|
|
|
replace(m_imps, deps, new_deps);
|
2015-01-01 03:37:26 +00:00
|
|
|
lean_assert(deps.size() == new_deps.size());
|
|
|
|
store_renames(deps, new_deps);
|
|
|
|
expr new_mvar = mk_metavar(m_ngen.next(), Pi(new_hyps, new_type));
|
|
|
|
expr new_meta = mk_app(new_mvar, new_hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
|
|
|
expr eq_rec_minor = mk_app(new_mvar, non_deps);
|
|
|
|
eq_rec = mk_app(eq_rec, eq_rec_minor, rhs, eq);
|
|
|
|
expr val = g.abstract(mk_app(eq_rec, deps));
|
|
|
|
assign(g.get_name(), val);
|
|
|
|
return unify_eqs(new_g, neqs-1);
|
2014-11-29 05:56:13 +00:00
|
|
|
}
|
2014-12-02 03:08:56 +00:00
|
|
|
} else if (is_local(lhs)) {
|
|
|
|
// flip equation and reduce to previous case
|
2014-12-19 05:29:13 +00:00
|
|
|
if (m_proof_irrel)
|
|
|
|
hyps.pop_back(); // remove processed equality
|
|
|
|
expr symm_eq = mk_eq(rhs, lhs).first;
|
|
|
|
expr new_type = mk_arrow(symm_eq, g_type);
|
2014-12-02 03:08:56 +00:00
|
|
|
expr new_mvar = mk_metavar(m_ngen.next(), Pi(hyps, new_type));
|
|
|
|
expr new_meta = mk_app(new_mvar, hyps);
|
|
|
|
goal new_g(new_meta, new_type);
|
2014-12-31 05:22:50 +00:00
|
|
|
level eq_symm_lvl = sort_level(m_tc.ensure_type(A).first);
|
2015-01-24 00:50:32 +00:00
|
|
|
expr symm_pr = mk_constant(get_eq_symm_name(), {eq_symm_lvl});
|
2014-12-02 03:08:56 +00:00
|
|
|
symm_pr = mk_app(symm_pr, A, lhs, rhs, eq);
|
|
|
|
expr val = g.abstract(mk_app(new_meta, symm_pr));
|
|
|
|
assign(g.get_name(), val);
|
2015-01-01 02:55:18 +00:00
|
|
|
return unify_eqs(new_g, neqs);
|
2014-11-29 05:56:13 +00:00
|
|
|
}
|
2014-12-31 05:22:50 +00:00
|
|
|
throw inversion_exception("unification failed");
|
2014-11-29 05:56:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 06:00:01 +00:00
|
|
|
auto unify_eqs(list<goal> const & gs, list<list<expr>> args, list<implementation_list> imps) ->
|
|
|
|
std::tuple<list<goal>, list<list<expr>>, list<implementation_list>, list<rename_map>> {
|
2014-12-31 05:22:50 +00:00
|
|
|
lean_assert(length(gs) == length(imps));
|
2014-11-29 05:56:13 +00:00
|
|
|
unsigned neqs = m_nindices + (m_dep_elim ? 1 : 0);
|
2014-12-31 05:22:50 +00:00
|
|
|
buffer<goal> new_goals;
|
2015-01-03 06:00:01 +00:00
|
|
|
buffer<list<expr>> new_args;
|
2014-12-31 05:22:50 +00:00
|
|
|
buffer<implementation_list> new_imps;
|
2015-01-01 02:55:18 +00:00
|
|
|
buffer<rename_map> new_renames;
|
2014-11-29 05:56:13 +00:00
|
|
|
for (goal const & g : gs) {
|
2015-01-01 02:55:18 +00:00
|
|
|
flet<rename_map> set1(m_renames, rename_map());
|
|
|
|
flet<implementation_list> set2(m_imps, head(imps));
|
2015-01-03 06:00:01 +00:00
|
|
|
m_c_args.clear();
|
|
|
|
to_buffer(head(args), m_c_args);
|
2015-01-01 02:55:18 +00:00
|
|
|
if (optional<goal> new_g = unify_eqs(g, neqs)) {
|
|
|
|
new_goals.push_back(*new_g);
|
2015-01-03 06:00:01 +00:00
|
|
|
list<expr> new_as = to_list(m_c_args);
|
2015-01-01 02:55:18 +00:00
|
|
|
new_args.push_back(new_as);
|
|
|
|
new_imps.push_back(m_imps);
|
|
|
|
new_renames.push_back(m_renames);
|
2014-12-31 05:22:50 +00:00
|
|
|
}
|
|
|
|
imps = tail(imps);
|
2015-01-01 02:55:18 +00:00
|
|
|
args = tail(args);
|
2014-11-29 05:56:13 +00:00
|
|
|
}
|
2015-01-01 02:55:18 +00:00
|
|
|
return std::make_tuple(to_list(new_goals), to_list(new_args), to_list(new_imps), to_list(new_renames));
|
2014-11-29 05:56:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 23:13:45 +00:00
|
|
|
std::pair<goal, rename_map> intro_deps(goal const & g, buffer<expr> const & deps) {
|
|
|
|
buffer<expr> hyps;
|
|
|
|
g.get_hyps(hyps);
|
|
|
|
buffer<expr> new_hyps;
|
|
|
|
new_hyps.append(hyps);
|
|
|
|
rename_map rs;
|
|
|
|
expr g_type = g.get_type();
|
|
|
|
for (expr const & d : deps) {
|
|
|
|
expr type = binding_domain(g_type);
|
|
|
|
expr new_d = mk_local(m_ngen.next(), get_unused_name(local_pp_name(d), new_hyps), type, local_info(d));
|
|
|
|
rs.insert(mlocal_name(d), mlocal_name(new_d));
|
|
|
|
new_hyps.push_back(new_d);
|
|
|
|
g_type = instantiate(binding_body(g_type), new_d);
|
|
|
|
}
|
|
|
|
expr new_meta = mk_app(mk_metavar(m_ngen.next(), Pi(new_hyps, g_type)), new_hyps);
|
|
|
|
goal new_g(new_meta, g_type);
|
|
|
|
unsigned ndeps = deps.size();
|
|
|
|
expr val = g.abstract(Fun(ndeps, new_hyps.end() - ndeps, new_meta));
|
|
|
|
assign(g.get_name(), val);
|
|
|
|
return mk_pair(new_g, rs);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<list<goal>, list<rename_map>> intro_deps(list<goal> const & gs, buffer<expr> const & deps) {
|
|
|
|
buffer<goal> new_goals;
|
|
|
|
buffer<rename_map> new_rs;
|
|
|
|
for (goal const & g : gs) {
|
|
|
|
auto p = intro_deps(g, deps);
|
|
|
|
new_goals.push_back(p.first);
|
|
|
|
new_rs.push_back(p.second);
|
|
|
|
}
|
|
|
|
return mk_pair(to_list(new_goals), to_list(new_rs));
|
|
|
|
}
|
|
|
|
|
2014-11-27 08:06:26 +00:00
|
|
|
public:
|
2014-12-31 05:22:50 +00:00
|
|
|
inversion_tac(environment const & env, io_state const & ios, name_generator const & ngen,
|
|
|
|
type_checker & tc, substitution const & subst, list<name> const & ids):
|
|
|
|
m_env(env), m_ios(ios), m_tc(tc), m_ids(ids),
|
|
|
|
m_ngen(ngen), m_subst(subst) {
|
2014-12-19 05:29:13 +00:00
|
|
|
m_proof_irrel = m_env.prop_proof_irrel();
|
2014-11-27 08:06:26 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 05:22:50 +00:00
|
|
|
inversion_tac(environment const & env, io_state const & ios, type_checker & tc):
|
|
|
|
inversion_tac(env, ios, tc.mk_ngen(), tc, substitution(), list<name>()) {}
|
|
|
|
|
|
|
|
typedef inversion::result result;
|
|
|
|
|
2015-01-01 02:55:18 +00:00
|
|
|
optional<result> execute(goal const & g, expr const & h, implementation_list const & imps) {
|
2014-11-29 05:56:13 +00:00
|
|
|
try {
|
2015-02-01 18:47:32 +00:00
|
|
|
expr h_type = whnf(mlocal_type(h));
|
2014-11-29 05:56:13 +00:00
|
|
|
if (!is_inversion_applicable(h_type))
|
2014-12-31 05:22:50 +00:00
|
|
|
return optional<result>();
|
2015-01-02 01:37:17 +00:00
|
|
|
if (has_indep_indices(g, h, h_type)) {
|
2015-01-01 23:13:45 +00:00
|
|
|
buffer<expr> deps;
|
|
|
|
goal g1 = generalize_dependecies(g, h, deps);
|
|
|
|
auto gs_imps_pair = apply_cases_on(g1, imps);
|
|
|
|
list<goal> gs2 = gs_imps_pair.first;
|
|
|
|
list<implementation_list> new_imps = gs_imps_pair.second;
|
|
|
|
auto gs_args_pair = intros_minors_args(gs2);
|
|
|
|
list<goal> gs3 = gs_args_pair.first;
|
2015-01-03 06:00:01 +00:00
|
|
|
list<list<expr>> args = gs_args_pair.second;
|
2015-01-01 23:13:45 +00:00
|
|
|
auto gs_rs_pair = intro_deps(gs3, deps);
|
|
|
|
list<goal> gs4 = gs_rs_pair.first;
|
|
|
|
list<rename_map> rs = gs_rs_pair.second;
|
|
|
|
return optional<result>(result(gs4, args, new_imps, rs, m_ngen, m_subst));
|
|
|
|
} else {
|
|
|
|
goal g1 = generalize_indices(g, h, h_type);
|
|
|
|
auto gs_imps_pair = apply_cases_on(g1, imps);
|
|
|
|
list<goal> gs2 = gs_imps_pair.first;
|
|
|
|
list<implementation_list> new_imps = gs_imps_pair.second;
|
|
|
|
auto gs_args_pair = intros_minors_args(gs2);
|
|
|
|
list<goal> gs3 = gs_args_pair.first;
|
2015-01-03 06:00:01 +00:00
|
|
|
list<list<expr>> args = gs_args_pair.second;
|
2015-01-01 23:13:45 +00:00
|
|
|
list<goal> gs4;
|
|
|
|
list<rename_map> rs;
|
|
|
|
std::tie(gs4, args, new_imps, rs) = unify_eqs(gs3, args, new_imps);
|
|
|
|
return optional<result>(result(gs4, args, new_imps, rs, m_ngen, m_subst));
|
|
|
|
}
|
2014-11-29 05:56:13 +00:00
|
|
|
} catch (inversion_exception & ex) {
|
2014-12-31 05:22:50 +00:00
|
|
|
return optional<result>();
|
2014-11-29 05:56:13 +00:00
|
|
|
}
|
2014-11-27 08:06:26 +00:00
|
|
|
}
|
2015-01-01 02:55:18 +00:00
|
|
|
|
|
|
|
optional<result> execute(goal const & g, name const & n, implementation_list const & imps) {
|
|
|
|
auto p = g.find_hyp(n);
|
|
|
|
if (!p)
|
|
|
|
return optional<result>();
|
|
|
|
expr const & h = p->first;
|
|
|
|
return execute(g, h, imps);
|
|
|
|
}
|
2014-11-27 08:06:26 +00:00
|
|
|
};
|
2014-11-27 05:28:00 +00:00
|
|
|
|
2014-12-31 05:22:50 +00:00
|
|
|
namespace inversion {
|
|
|
|
optional<result> apply(environment const & env, io_state const & ios, type_checker & tc,
|
2015-01-01 02:55:18 +00:00
|
|
|
goal const & g, expr const & h, implementation_list const & imps) {
|
|
|
|
return inversion_tac(env, ios, tc).execute(g, h, imps);
|
2014-12-31 05:22:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-29 06:25:37 +00:00
|
|
|
tactic inversion_tactic(name const & n, list<name> const & ids) {
|
2014-12-19 05:29:13 +00:00
|
|
|
auto fn = [=](environment const & env, io_state const & ios, proof_state const & ps) -> optional<proof_state> {
|
2014-12-31 05:22:50 +00:00
|
|
|
goals const & gs = ps.get_goals();
|
|
|
|
if (empty(gs))
|
|
|
|
return none_proof_state();
|
|
|
|
goal g = head(gs);
|
|
|
|
goals tail_gs = tail(gs);
|
|
|
|
name_generator ngen = ps.get_ngen();
|
|
|
|
std::unique_ptr<type_checker> tc = mk_type_checker(env, ngen.mk_child(), ps.relax_main_opaque());
|
|
|
|
inversion_tac tac(env, ios, ngen, *tc, ps.get_subst(), ids);
|
|
|
|
if (auto res = tac.execute(g, n, implementation_list())) {
|
|
|
|
proof_state new_s(ps, append(res->m_goals, tail_gs), res->m_subst, res->m_ngen);
|
|
|
|
return some_proof_state(new_s);
|
|
|
|
} else {
|
|
|
|
return none_proof_state();
|
|
|
|
}
|
2014-11-27 08:06:26 +00:00
|
|
|
};
|
|
|
|
return tactic01(fn);
|
2014-11-27 05:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initialize_inversion_tactic() {
|
2015-01-24 00:50:32 +00:00
|
|
|
register_tac(get_tactic_inversion_name(),
|
2014-11-27 05:28:00 +00:00
|
|
|
[](type_checker &, elaborate_fn const &, expr const & e, pos_info_provider const *) {
|
2014-11-29 06:25:37 +00:00
|
|
|
name n = tactic_expr_to_id(app_arg(e), "invalid 'inversion/cases' tactic, argument must be an identifier");
|
|
|
|
return inversion_tactic(n, list<name>());
|
|
|
|
});
|
2015-01-24 00:50:32 +00:00
|
|
|
register_tac(get_tactic_inversion_with_name(),
|
2014-11-29 06:25:37 +00:00
|
|
|
[](type_checker &, elaborate_fn const &, expr const & e, pos_info_provider const *) {
|
|
|
|
name n = tactic_expr_to_id(app_arg(app_fn(e)), "invalid 'cases-with' tactic, argument must be an identifier");
|
|
|
|
buffer<name> ids;
|
|
|
|
get_tactic_id_list_elements(app_arg(e), ids, "invalid 'cases-with' tactic, list of identifiers expected");
|
|
|
|
return inversion_tactic(n, to_list(ids.begin(), ids.end()));
|
2014-11-27 05:28:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
void finalize_inversion_tactic() {}
|
|
|
|
}
|