2014-07-05 17:59:53 +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
|
|
|
|
*/
|
|
|
|
#include "util/lazy_list_fn.h"
|
2015-06-27 00:15:53 +00:00
|
|
|
#include "kernel/instantiate.h"
|
2014-07-05 17:59:53 +00:00
|
|
|
#include "kernel/inductive/inductive.h"
|
|
|
|
#include "library/unifier_plugin.h"
|
|
|
|
#include "library/unifier.h"
|
2015-05-27 21:41:12 +00:00
|
|
|
#include "library/util.h"
|
2014-07-05 17:59:53 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
class inductive_unifier_plugin_cell : public unifier_plugin_cell {
|
2014-07-06 23:46:34 +00:00
|
|
|
/** \brief Return true iff the lhs or rhs of the constraint c is of the form (elim ... (?m ...) ...) */
|
2014-07-05 17:59:53 +00:00
|
|
|
bool is_elim_meta_cnstr(type_checker & tc, constraint const & c) const {
|
2014-07-06 23:46:34 +00:00
|
|
|
return is_eq_cnstr(c) && (inductive::is_elim_meta_app(tc, cnstr_lhs_expr(c)) ||
|
|
|
|
inductive::is_elim_meta_app(tc, cnstr_rhs_expr(c)));
|
2014-07-05 17:59:53 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 23:46:34 +00:00
|
|
|
/** \brief Return true iff \c e is of the form (?m ... (intro ...) ...) */
|
2014-07-05 17:59:53 +00:00
|
|
|
bool is_meta_intro_app(type_checker & tc, expr const & e) const {
|
|
|
|
if (!is_app(e) || !is_meta(e))
|
|
|
|
return false;
|
2014-07-06 23:46:34 +00:00
|
|
|
buffer<expr> args;
|
|
|
|
get_app_args(e, args);
|
|
|
|
for (expr const & a : args) {
|
|
|
|
expr arg = get_app_fn(a);
|
|
|
|
if (!is_constant(arg))
|
|
|
|
continue;
|
|
|
|
if (inductive::is_intro_rule(tc.env(), const_name(arg)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-07-05 17:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Return true iff the lhs or rhs of the constraint c is of the form (?m ... (intro ...)) */
|
|
|
|
bool is_meta_intro_cnstr(type_checker & tc, constraint const & c) const {
|
|
|
|
return is_eq_cnstr(c) && (is_meta_intro_app(tc, cnstr_lhs_expr(c)) || is_meta_intro_app(tc, cnstr_rhs_expr(c)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Given (elim args) =?= t, where elim is the eliminator/recursor for the inductive declaration \c decl,
|
2014-07-06 23:46:34 +00:00
|
|
|
and the major premise is of the form (?m ...), we create a case split where we try to assign (?m ...)
|
2014-07-05 17:59:53 +00:00
|
|
|
to the different constructors of decl.
|
|
|
|
*/
|
2015-05-21 21:32:36 +00:00
|
|
|
lazy_list<constraints> add_elim_meta_cnstrs(type_checker & tc, name_generator & ngen, inductive::inductive_decl const & decl,
|
2014-07-25 23:00:38 +00:00
|
|
|
expr const & elim, buffer<expr> & args, expr const & t, justification const & j,
|
2015-05-08 21:36:38 +00:00
|
|
|
constraint_seq cs) const {
|
2014-07-05 17:59:53 +00:00
|
|
|
lean_assert(is_constant(elim));
|
2014-07-06 23:46:34 +00:00
|
|
|
environment const & env = tc.env();
|
2014-07-05 17:59:53 +00:00
|
|
|
levels elim_lvls = const_levels(elim);
|
|
|
|
unsigned elim_num_lvls = length(elim_lvls);
|
2014-07-06 23:46:34 +00:00
|
|
|
unsigned major_idx = *inductive::get_elim_major_idx(env, const_name(elim));
|
|
|
|
expr meta = args[major_idx]; // save this argument, we will update it
|
2014-07-27 02:57:54 +00:00
|
|
|
lean_assert(has_expr_metavar_strict(meta));
|
2014-07-05 17:59:53 +00:00
|
|
|
buffer<expr> margs;
|
|
|
|
expr const & m = get_app_args(meta, margs);
|
2014-08-20 05:31:26 +00:00
|
|
|
expr mtype = tc.infer(m, cs);
|
2014-07-05 17:59:53 +00:00
|
|
|
buffer<constraints> alts;
|
|
|
|
for (auto const & intro : inductive::inductive_decl_intros(decl)) {
|
2014-08-20 05:31:26 +00:00
|
|
|
constraint_seq cs_intro = cs;
|
2014-07-05 17:59:53 +00:00
|
|
|
name const & intro_name = inductive::intro_rule_name(intro);
|
|
|
|
declaration intro_decl = env.get(intro_name);
|
|
|
|
levels intro_lvls;
|
2015-01-29 01:22:18 +00:00
|
|
|
if (intro_decl.get_num_univ_params() == elim_num_lvls) {
|
2014-07-05 17:59:53 +00:00
|
|
|
intro_lvls = elim_lvls;
|
|
|
|
} else {
|
2015-01-29 01:22:18 +00:00
|
|
|
lean_assert(intro_decl.get_num_univ_params() == elim_num_lvls - 1);
|
2014-07-05 17:59:53 +00:00
|
|
|
intro_lvls = tail(elim_lvls);
|
|
|
|
}
|
|
|
|
expr intro_fn = mk_constant(inductive::intro_rule_name(intro), intro_lvls);
|
|
|
|
expr hint = intro_fn;
|
2014-08-20 05:31:26 +00:00
|
|
|
expr intro_type = tc.whnf(inductive::intro_rule_type(intro), cs_intro);
|
2014-07-05 17:59:53 +00:00
|
|
|
while (is_pi(intro_type)) {
|
2015-06-27 00:15:53 +00:00
|
|
|
expr new_arg = mk_app(mk_aux_metavar_for(ngen, mtype), margs);
|
|
|
|
hint = mk_app(hint, new_arg);
|
|
|
|
intro_type = tc.whnf(instantiate(binding_body(intro_type), new_arg), cs_intro);
|
2014-07-05 17:59:53 +00:00
|
|
|
}
|
2015-05-08 21:36:38 +00:00
|
|
|
constraint c1 = mk_eq_cnstr(meta, hint, j);
|
2014-07-06 23:46:34 +00:00
|
|
|
args[major_idx] = hint;
|
2014-08-20 05:31:26 +00:00
|
|
|
expr reduce_elim = tc.whnf(mk_app(elim, args), cs_intro);
|
2015-05-08 21:36:38 +00:00
|
|
|
constraint c2 = mk_eq_cnstr(reduce_elim, t, j);
|
2014-08-20 05:31:26 +00:00
|
|
|
cs_intro = constraint_seq(c1) + constraint_seq(c2) + cs_intro;
|
|
|
|
buffer<constraint> cs_buffer;
|
|
|
|
cs_intro.linearize(cs_buffer);
|
|
|
|
alts.push_back(to_list(cs_buffer.begin(), cs_buffer.end()));
|
2014-07-05 17:59:53 +00:00
|
|
|
}
|
|
|
|
return to_lazy(to_list(alts.begin(), alts.end()));
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:32:36 +00:00
|
|
|
lazy_list<constraints> process_elim_meta_core(type_checker & tc, name_generator & ngen,
|
2015-05-08 21:36:38 +00:00
|
|
|
expr const & lhs, expr const & rhs, justification const & j) const {
|
2014-07-06 23:46:34 +00:00
|
|
|
lean_assert(inductive::is_elim_meta_app(tc, lhs));
|
2014-08-20 05:31:26 +00:00
|
|
|
auto dcs = tc.is_def_eq_types(lhs, rhs, j);
|
|
|
|
if (!dcs.first)
|
2014-07-25 23:00:38 +00:00
|
|
|
return lazy_list<constraints>();
|
2014-08-20 05:31:26 +00:00
|
|
|
constraint_seq cs = dcs.second;
|
2014-07-05 17:59:53 +00:00
|
|
|
buffer<expr> args;
|
|
|
|
expr const & elim = get_app_args(lhs, args);
|
|
|
|
environment const & env = tc.env();
|
|
|
|
auto it_name = *inductive::is_elim_rule(env, const_name(elim));
|
2015-05-27 21:41:12 +00:00
|
|
|
if (is_recursive_datatype(env, it_name))
|
|
|
|
return lazy_list<constraints>();
|
2014-07-25 23:00:38 +00:00
|
|
|
auto decls = *inductive::is_inductive_decl(env, it_name);
|
2014-07-05 17:59:53 +00:00
|
|
|
for (auto const & d : std::get<2>(decls)) {
|
|
|
|
if (inductive::inductive_decl_name(d) == it_name)
|
2015-05-08 21:36:38 +00:00
|
|
|
return add_elim_meta_cnstrs(tc, ngen, d, elim, args, rhs, j, cs);
|
2014-07-05 17:59:53 +00:00
|
|
|
}
|
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
\brief Try to solve constraint of the form (elim ... (?m ...)) =?= t, by assigning (?m ...) to the introduction rules
|
|
|
|
associated with the eliminator \c elim.
|
|
|
|
*/
|
2015-05-21 21:32:36 +00:00
|
|
|
virtual lazy_list<constraints> solve(type_checker & tc, constraint const & c, name_generator && ngen) const {
|
2014-07-08 23:39:39 +00:00
|
|
|
if (!is_eq_cnstr(c))
|
|
|
|
return lazy_list<constraints>();
|
2014-07-05 17:59:53 +00:00
|
|
|
expr const & lhs = cnstr_lhs_expr(c);
|
|
|
|
expr const & rhs = cnstr_rhs_expr(c);
|
|
|
|
justification const & j = c.get_justification();
|
2014-07-06 23:46:34 +00:00
|
|
|
if (inductive::is_elim_meta_app(tc, lhs))
|
2015-05-08 21:36:38 +00:00
|
|
|
return process_elim_meta_core(tc, ngen, lhs, rhs, j);
|
2014-07-06 23:46:34 +00:00
|
|
|
else if (inductive::is_elim_meta_app(tc, rhs))
|
2015-05-08 21:36:38 +00:00
|
|
|
return process_elim_meta_core(tc, ngen, rhs, lhs, j);
|
2014-07-05 17:59:53 +00:00
|
|
|
else
|
|
|
|
return lazy_list<constraints>();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool delay_constraint(type_checker & tc, constraint const & c) const {
|
|
|
|
return is_elim_meta_cnstr(tc, c) || is_meta_intro_cnstr(tc, c);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
unifier_plugin mk_inductive_unifier_plugin() {
|
|
|
|
return std::make_shared<inductive_unifier_plugin_cell>();
|
|
|
|
}
|
|
|
|
}
|