2014-06-24 21:55:06 +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 <utility>
|
|
|
|
#include <vector>
|
|
|
|
#include "util/flet.h"
|
|
|
|
#include "util/list_fn.h"
|
2014-06-25 15:30:09 +00:00
|
|
|
#include "util/lazy_list_fn.h"
|
2014-06-24 21:55:06 +00:00
|
|
|
#include "util/sstream.h"
|
2014-07-02 03:43:53 +00:00
|
|
|
#include "util/name_map.h"
|
2014-06-24 21:55:06 +00:00
|
|
|
#include "kernel/abstract.h"
|
|
|
|
#include "kernel/instantiate.h"
|
|
|
|
#include "kernel/type_checker.h"
|
2014-06-29 16:47:25 +00:00
|
|
|
#include "kernel/for_each_fn.h"
|
2014-07-06 23:46:34 +00:00
|
|
|
#include "kernel/replace_fn.h"
|
2014-06-24 21:55:06 +00:00
|
|
|
#include "kernel/kernel_exception.h"
|
|
|
|
#include "kernel/error_msgs.h"
|
|
|
|
#include "kernel/expr_maps.h"
|
|
|
|
#include "library/coercion.h"
|
|
|
|
#include "library/placeholder.h"
|
|
|
|
#include "library/choice.h"
|
|
|
|
#include "library/explicit.h"
|
|
|
|
#include "library/unifier.h"
|
2014-07-06 01:58:20 +00:00
|
|
|
#include "library/opaque_hints.h"
|
2014-07-06 23:46:34 +00:00
|
|
|
#include "library/locals.h"
|
2014-08-01 02:54:21 +00:00
|
|
|
#include "library/deep_copy.h"
|
2014-06-29 16:47:25 +00:00
|
|
|
#include "library/tactic/tactic.h"
|
2014-07-02 03:43:53 +00:00
|
|
|
#include "library/tactic/expr_to_tactic.h"
|
2014-06-29 16:47:25 +00:00
|
|
|
#include "library/error_handling/error_handling.h"
|
2014-07-06 23:46:34 +00:00
|
|
|
#include "frontends/lean/local_decls.h"
|
2014-07-04 22:45:50 +00:00
|
|
|
#include "frontends/lean/class.h"
|
2014-07-08 21:28:33 +00:00
|
|
|
#include "frontends/lean/tactic_hint.h"
|
2014-08-07 02:35:26 +00:00
|
|
|
#include "frontends/lean/info_manager.h"
|
2014-08-13 01:43:56 +00:00
|
|
|
#include "frontends/lean/elaborator.h"
|
2014-06-24 21:55:06 +00:00
|
|
|
|
2014-07-05 01:03:13 +00:00
|
|
|
#ifndef LEAN_DEFAULT_ELABORATOR_LOCAL_INSTANCES
|
|
|
|
#define LEAN_DEFAULT_ELABORATOR_LOCAL_INSTANCES true
|
|
|
|
#endif
|
|
|
|
|
2014-06-24 21:55:06 +00:00
|
|
|
namespace lean {
|
2014-07-05 01:03:13 +00:00
|
|
|
// ==========================================
|
|
|
|
// elaborator configuration options
|
2014-07-13 13:03:47 +00:00
|
|
|
static name g_elaborator_local_instances{"elaborator", "local_instances"};
|
2014-07-05 01:03:13 +00:00
|
|
|
RegisterBoolOption(g_elaborator_local_instances, LEAN_DEFAULT_ELABORATOR_LOCAL_INSTANCES, "(lean elaborator) use local declarates as class instances");
|
|
|
|
bool get_elaborator_local_instances(options const & opts) {
|
|
|
|
return opts.get_bool(g_elaborator_local_instances, LEAN_DEFAULT_ELABORATOR_LOCAL_INSTANCES);
|
|
|
|
}
|
|
|
|
// ==========================================
|
|
|
|
|
2014-07-06 23:46:34 +00:00
|
|
|
/** \brief Functional object for converting the universe metavariables in an expression in new universe parameters.
|
|
|
|
The substitution is updated with the mapping metavar -> new param.
|
|
|
|
The set of parameter names m_params and the buffer m_new_params are also updated.
|
|
|
|
*/
|
|
|
|
class univ_metavars_to_params_fn {
|
|
|
|
environment const & m_env;
|
|
|
|
local_decls<level> const & m_lls;
|
|
|
|
substitution & m_subst;
|
|
|
|
name_set & m_params;
|
|
|
|
buffer<name> & m_new_params;
|
|
|
|
unsigned m_next_idx;
|
|
|
|
|
|
|
|
/** \brief Create a new universe parameter s.t. the new name does not occur in \c m_params, nor it is
|
|
|
|
a global universe in \e m_env or in the local_decls<level> m_lls.
|
|
|
|
The new name is added to \c m_params, and the new level parameter is returned.
|
|
|
|
The name is of the form "l_i" where \c i >= m_next_idx.
|
|
|
|
*/
|
|
|
|
level mk_new_univ_param() {
|
|
|
|
name l("l");
|
|
|
|
name r = l.append_after(m_next_idx);
|
|
|
|
while (m_lls.contains(r) || m_params.contains(r) || m_env.is_universe(r)) {
|
|
|
|
m_next_idx++;
|
|
|
|
r = l.append_after(m_next_idx);
|
|
|
|
}
|
|
|
|
m_params.insert(r);
|
|
|
|
m_new_params.push_back(r);
|
|
|
|
return mk_param_univ(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
univ_metavars_to_params_fn(environment const & env, local_decls<level> const & lls, substitution & s, name_set & ps, buffer<name> & new_ps):
|
|
|
|
m_env(env), m_lls(lls), m_subst(s), m_params(ps), m_new_params(new_ps), m_next_idx(1) {}
|
|
|
|
|
|
|
|
level apply(level const & l) {
|
|
|
|
return replace(l, [&](level const & l) {
|
|
|
|
if (!has_meta(l))
|
|
|
|
return some_level(l);
|
|
|
|
if (is_meta(l)) {
|
|
|
|
if (auto it = m_subst.get_level(meta_id(l))) {
|
|
|
|
return some_level(*it);
|
|
|
|
} else {
|
|
|
|
level new_p = mk_new_univ_param();
|
2014-07-23 15:51:24 +00:00
|
|
|
m_subst.assign(l, new_p);
|
2014-07-06 23:46:34 +00:00
|
|
|
return some_level(new_p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none_level();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
expr apply(expr const & e) {
|
|
|
|
if (!has_univ_metavar(e)) {
|
|
|
|
return e;
|
|
|
|
} else {
|
2014-07-19 11:53:01 +00:00
|
|
|
return replace(e, [&](expr const & e) {
|
2014-07-06 23:46:34 +00:00
|
|
|
if (!has_univ_metavar(e)) {
|
|
|
|
return some_expr(e);
|
|
|
|
} else if (is_sort(e)) {
|
|
|
|
return some_expr(update_sort(e, apply(sort_level(e))));
|
|
|
|
} else if (is_constant(e)) {
|
|
|
|
levels ls = map(const_levels(e), [&](level const & l) { return apply(l); });
|
|
|
|
return some_expr(update_constant(e, ls));
|
|
|
|
} else {
|
|
|
|
return none_expr();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expr operator()(expr const & e) { return apply(e); }
|
|
|
|
};
|
|
|
|
|
2014-08-13 19:45:57 +00:00
|
|
|
elaborator_context::elaborator_context(environment const & env, io_state const & ios, local_decls<level> const & lls,
|
|
|
|
pos_info_provider const * pp, info_manager * info, bool check_unassigned):
|
2014-08-13 01:43:56 +00:00
|
|
|
m_env(env), m_ios(ios), m_lls(lls), m_pos_provider(pp), m_info_manager(info), m_check_unassigned(check_unassigned) {
|
|
|
|
m_use_local_instances = get_elaborator_local_instances(ios.get_options());
|
|
|
|
}
|
|
|
|
|
2014-08-05 23:25:54 +00:00
|
|
|
/** \brief Return a list of instances of the class \c cls_name that occur in \c ctx */
|
|
|
|
list<expr> get_local_instances(list<expr> const & ctx, name const & cls_name) {
|
|
|
|
buffer<expr> buffer;
|
|
|
|
for (auto const & l : ctx) {
|
|
|
|
if (!is_local(l))
|
|
|
|
continue;
|
|
|
|
expr inst_type = mlocal_type(l);
|
|
|
|
if (!is_constant(get_app_fn(inst_type)) || const_name(get_app_fn(inst_type)) != cls_name)
|
|
|
|
continue;
|
|
|
|
buffer.push_back(l);
|
|
|
|
}
|
|
|
|
return to_list(buffer.begin(), buffer.end());
|
|
|
|
}
|
|
|
|
|
2014-07-06 23:46:34 +00:00
|
|
|
/** \brief Helper class for implementing the \c elaborate functions. */
|
2014-06-24 21:55:06 +00:00
|
|
|
class elaborator {
|
2014-08-05 23:25:54 +00:00
|
|
|
typedef name_map<expr> mvar2meta;
|
|
|
|
|
|
|
|
/** \brief Auxiliary data-structure for storing the local context, and creating metavariables in the scope of the local context efficiently */
|
|
|
|
class context {
|
|
|
|
name_generator & m_ngen;
|
|
|
|
mvar2meta & m_mvar2meta;
|
|
|
|
list<expr> m_ctx; // current local context: a list of local constants
|
|
|
|
buffer<expr> m_ctx_buffer; // m_ctx as a buffer
|
|
|
|
buffer<expr> m_ctx_domain_buffer; // m_ctx_domain_buffer[i] == abstract_locals(m_ctx_buffer[i], i, m_ctx_buffer.beg
|
|
|
|
public:
|
|
|
|
context(name_generator & ngen, mvar2meta & m, list<expr> const & ctx):m_ngen(ngen), m_mvar2meta(m) { set_ctx(ctx); }
|
|
|
|
|
|
|
|
void set_ctx(list<expr> const & ctx) {
|
|
|
|
m_ctx = ctx;
|
|
|
|
m_ctx_buffer.clear();
|
|
|
|
m_ctx_domain_buffer.clear();
|
|
|
|
to_buffer(ctx, m_ctx_buffer);
|
|
|
|
std::reverse(m_ctx_buffer.begin(), m_ctx_buffer.end());
|
|
|
|
for (unsigned i = 0; i < m_ctx_buffer.size(); i++) {
|
|
|
|
m_ctx_domain_buffer.push_back(abstract_locals(m_ctx_buffer[i], i, m_ctx_buffer.data()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Given <tt>e[l_1, ..., l_n]</tt> and assuming \c m_ctx is
|
|
|
|
<tt>[l_n : A_n[l_1, ..., l_{n-1}], ..., l_1 : A_1 ]</tt>,
|
|
|
|
then the result is
|
|
|
|
<tt>(Pi (x_1 : A_1) ... (x_n : A_n[x_1, ..., x_{n-1}]), e[x_1, ... x_n])</tt>.
|
|
|
|
*/
|
|
|
|
expr pi_abstract_context(expr e, tag g) {
|
|
|
|
e = abstract_locals(e, m_ctx_buffer.size(), m_ctx_buffer.data());
|
|
|
|
unsigned i = m_ctx_domain_buffer.size();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
expr const & l = m_ctx_domain_buffer[i];
|
|
|
|
e = save_tag(mk_pi(local_pp_name(l), mlocal_type(l), e, local_info(l)), g);
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Assuming \c m_ctx is
|
|
|
|
<tt>[l_n : A_n[l_1, ..., l_{n-1}], ..., l_1 : A_1 ]</tt>,
|
|
|
|
return <tt>(f l_1 ... l_n)</tt>.
|
|
|
|
*/
|
|
|
|
expr apply_context(expr const & f, tag g) {
|
|
|
|
expr r = f;
|
|
|
|
for (unsigned i = 0; i < m_ctx_buffer.size(); i++)
|
|
|
|
r = save_tag(::lean::mk_app(r, m_ctx_buffer[i]), g);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Assuming \c m_ctx is
|
|
|
|
<tt>[l_n : A_n[l_1, ..., l_{n-1}], ..., l_1 : A_1 ]</tt>,
|
|
|
|
return a fresh metavariable \c ?m with type
|
|
|
|
<tt>(Pi (x_1 : A_1) ... (x_n : A_n[x_1, ..., x_{n-1}]), Type.{?u})</tt>,
|
|
|
|
where \c ?u is a fresh universe metavariable.
|
|
|
|
*/
|
|
|
|
expr mk_type_metavar(tag g) {
|
|
|
|
name n = m_ngen.next();
|
|
|
|
expr s = save_tag(mk_sort(mk_meta_univ(m_ngen.next())), g);
|
|
|
|
expr t = pi_abstract_context(s, g);
|
|
|
|
return save_tag(::lean::mk_metavar(n, t), g);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Assuming \c m_ctx is
|
|
|
|
<tt>[l_n : A_n[l_1, ..., l_{n-1}], ..., l_1 : A_1 ]</tt>,
|
|
|
|
return <tt>(?m l_1 ... l_n)</tt> where \c ?m is a fresh metavariable with type
|
|
|
|
<tt>(Pi (x_1 : A_1) ... (x_n : A_n[x_1, ..., x_{n-1}]), Type.{?u})</tt>,
|
|
|
|
and \c ?u is a fresh universe metavariable.
|
|
|
|
|
|
|
|
\remark The type of the resulting expression is <tt>Type.{?u}</tt>
|
|
|
|
*/
|
|
|
|
expr mk_type_meta(tag g) {
|
|
|
|
return apply_context(mk_type_metavar(g), g);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Given <tt>type[l_1, ..., l_n]</tt> and assuming \c m_ctx is
|
|
|
|
<tt>[l_n : A_n[l_1, ..., l_{n-1}], ..., l_1 : A_1 ]</tt>,
|
|
|
|
then the result is a fresh metavariable \c ?m with type
|
|
|
|
<tt>(Pi (x_1 : A_1) ... (x_n : A_n[x_1, ..., x_{n-1}]), type[x_1, ... x_n])</tt>.
|
|
|
|
If <tt>type</tt> is none, then the result is a fresh metavariable \c ?m1 with type
|
|
|
|
<tt>(Pi (x_1 : A_1) ... (x_n : A_n[x_1, ..., x_{n-1}]), ?m2 x1 .... xn)</tt>,
|
|
|
|
where ?m2 is another fresh metavariable with type
|
|
|
|
<tt>(Pi (x_1 : A_1) ... (x_n : A_n[x_1, ..., x_{n-1}]), Type.{?u})</tt>,
|
|
|
|
and \c ?u is a fresh universe metavariable.
|
|
|
|
*/
|
|
|
|
expr mk_metavar(optional<expr> const & type, tag g) {
|
|
|
|
name n = m_ngen.next();
|
|
|
|
expr r_type = type ? *type : mk_type_meta(g);
|
|
|
|
expr t = pi_abstract_context(r_type, g);
|
|
|
|
return save_tag(::lean::mk_metavar(n, t), g);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Given <tt>type[l_1, ..., l_n]</tt> and assuming \c m_ctx is
|
|
|
|
<tt>[l_n : A_n[l_1, ..., l_{n-1}], ..., l_1 : A_1 ]</tt>,
|
|
|
|
return (?m l_1 ... l_n), where ?m is a fresh metavariable
|
|
|
|
created using \c mk_metavar.
|
|
|
|
|
|
|
|
\see mk_metavar
|
|
|
|
*/
|
|
|
|
expr mk_meta(optional<expr> const & type, tag g) {
|
|
|
|
expr mvar = mk_metavar(type, g);
|
|
|
|
expr meta = apply_context(mvar, g);
|
|
|
|
m_mvar2meta.insert(mlocal_name(mvar), meta);
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_local(expr const & l) {
|
|
|
|
m_ctx = cons(l, m_ctx);
|
|
|
|
m_ctx_domain_buffer.push_back(abstract_locals(l, m_ctx_buffer.size(), m_ctx_buffer.data()));
|
|
|
|
m_ctx_buffer.push_back(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
list<expr> const & get_data() const {
|
|
|
|
return m_ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Scope object for restoring the content of the context */
|
|
|
|
class scope {
|
|
|
|
context & m_main;
|
|
|
|
list<expr> m_old_ctx;
|
|
|
|
unsigned m_old_sz;
|
|
|
|
public:
|
|
|
|
scope(context & main):m_main(main), m_old_ctx(main.m_ctx), m_old_sz(main.m_ctx_buffer.size()) {}
|
|
|
|
~scope() {
|
|
|
|
m_main.m_ctx = m_old_ctx;
|
|
|
|
m_main.m_ctx_buffer.shrink(m_old_sz);
|
|
|
|
m_main.m_ctx_domain_buffer.shrink(m_old_sz);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** \brief Scope object for temporarily replacing the content of the context */
|
|
|
|
class scope_replace {
|
|
|
|
context & m_main;
|
|
|
|
list<expr> m_saved;
|
|
|
|
public:
|
|
|
|
scope_replace(context & main, list<expr> const & new_ctx):m_main(main), m_saved(m_main.m_ctx) {
|
|
|
|
m_main.set_ctx(new_ctx);
|
|
|
|
}
|
|
|
|
~scope_replace() {
|
|
|
|
m_main.set_ctx(m_saved);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-07-04 19:47:33 +00:00
|
|
|
typedef std::vector<constraint> constraint_vect;
|
2014-07-08 21:28:33 +00:00
|
|
|
typedef name_map<expr> local_tactic_hints;
|
2014-07-06 01:58:20 +00:00
|
|
|
typedef std::unique_ptr<type_checker> type_checker_ptr;
|
2014-06-24 21:55:06 +00:00
|
|
|
|
2014-08-13 19:45:57 +00:00
|
|
|
elaborator_context & m_env;
|
|
|
|
name_generator m_ngen;
|
|
|
|
type_checker_ptr m_tc[2];
|
|
|
|
mvar2meta m_mvar2meta; // mapping from metavariable ?m to the (?m l_1 ... l_n) where [l_1 ... l_n] are the local constants
|
2014-08-05 23:25:54 +00:00
|
|
|
// representing the context where ?m was created.
|
2014-08-13 19:45:57 +00:00
|
|
|
context m_context; // current local context: a list of local constants
|
|
|
|
context m_full_context; // superset of m_context, it also contains non-contextual locals.
|
2014-08-05 23:25:54 +00:00
|
|
|
|
2014-08-13 19:45:57 +00:00
|
|
|
constraint_vect m_constraints; // constraints that must be solved for the elaborated term to be type correct.
|
|
|
|
local_tactic_hints m_local_tactic_hints; // mapping from metavariable name ?m to tactic expression that should be used to solve it.
|
2014-07-08 21:28:33 +00:00
|
|
|
// this mapping is populated by the 'by tactic-expr' expression.
|
2014-08-14 17:37:24 +00:00
|
|
|
name_set m_displayed_errors; // set of metavariables that we already reported unsolved/unassigned
|
|
|
|
bool m_relax_main_opaque; // if true, then treat opaque definitions from the main module as transparent
|
|
|
|
std::vector<std::unique_ptr<info_data>> m_pre_info_data;
|
2014-06-24 21:55:06 +00:00
|
|
|
|
2014-08-05 23:25:54 +00:00
|
|
|
struct scope_ctx {
|
2014-08-05 23:46:43 +00:00
|
|
|
context::scope m_scope1;
|
|
|
|
context::scope m_scope2;
|
|
|
|
scope_ctx(elaborator & e):m_scope1(e.m_context), m_scope2(e.m_full_context) {}
|
2014-08-05 23:25:54 +00:00
|
|
|
};
|
2014-07-12 16:49:14 +00:00
|
|
|
|
2014-08-05 23:46:43 +00:00
|
|
|
/** \brief Auxiliary object for creating backtracking points, and replacing the local scopes.
|
|
|
|
|
2014-08-12 22:48:16 +00:00
|
|
|
\remark A new scope can only be created when m_constraints is empty.
|
2014-06-24 21:55:06 +00:00
|
|
|
*/
|
2014-08-05 23:25:54 +00:00
|
|
|
struct new_scope {
|
|
|
|
elaborator & m_main;
|
2014-08-05 23:46:43 +00:00
|
|
|
context::scope_replace m_context_scope;
|
|
|
|
context::scope_replace m_full_context_scope;
|
2014-08-12 22:48:16 +00:00
|
|
|
new_scope(elaborator & e, list<expr> const & ctx, list<expr> const & full_ctx):
|
2014-08-05 23:46:43 +00:00
|
|
|
m_main(e), m_context_scope(e.m_context, ctx), m_full_context_scope(e.m_full_context, full_ctx) {
|
2014-06-24 21:55:06 +00:00
|
|
|
lean_assert(m_main.m_constraints.empty());
|
2014-07-27 19:01:06 +00:00
|
|
|
m_main.m_tc[0]->push();
|
|
|
|
m_main.m_tc[1]->push();
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
2014-08-05 23:25:54 +00:00
|
|
|
~new_scope() {
|
2014-07-27 19:01:06 +00:00
|
|
|
m_main.m_tc[0]->pop();
|
|
|
|
m_main.m_tc[1]->pop();
|
2014-06-24 21:55:06 +00:00
|
|
|
m_main.m_constraints.clear();
|
|
|
|
lean_assert(m_main.m_constraints.empty());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-06 23:46:34 +00:00
|
|
|
/* \brief Move all constraints generated by the type checker to the buffer m_constraints. */
|
2014-06-26 20:35:36 +00:00
|
|
|
void consume_tc_cnstrs() {
|
2014-07-27 19:01:06 +00:00
|
|
|
for (unsigned i = 0; i < 2; i++)
|
|
|
|
while (auto c = m_tc[i]->next_cnstr())
|
|
|
|
m_constraints.push_back(*c);
|
2014-06-26 20:35:36 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 15:30:09 +00:00
|
|
|
struct choice_elaborator {
|
2014-07-08 22:04:52 +00:00
|
|
|
bool m_ignore_failure;
|
|
|
|
choice_elaborator(bool ignore_failure = false):m_ignore_failure(ignore_failure) {}
|
2014-07-04 22:45:50 +00:00
|
|
|
virtual optional<constraints> next() = 0;
|
|
|
|
};
|
|
|
|
|
2014-07-06 23:46:34 +00:00
|
|
|
/** \brief 'Choice' expressions <tt>(choice e_1 ... e_n)</tt> are mapped into a metavariable \c ?m
|
|
|
|
and a choice constraints <tt>(?m in fn)</tt> where \c fn is a choice function.
|
|
|
|
The choice function produces a stream of alternatives. In this case, it produces a stream of
|
|
|
|
size \c n, one alternative for each \c e_i.
|
|
|
|
This is a helper class for implementing this choice functions.
|
|
|
|
*/
|
2014-07-04 22:45:50 +00:00
|
|
|
struct choice_expr_elaborator : public choice_elaborator {
|
2014-06-25 15:30:09 +00:00
|
|
|
elaborator & m_elab;
|
2014-07-04 19:47:33 +00:00
|
|
|
expr m_mvar;
|
2014-06-25 15:30:09 +00:00
|
|
|
expr m_choice;
|
2014-08-05 23:25:54 +00:00
|
|
|
list<expr> m_ctx;
|
2014-08-05 23:46:43 +00:00
|
|
|
list<expr> m_full_ctx;
|
2014-06-25 15:30:09 +00:00
|
|
|
unsigned m_idx;
|
2014-07-27 19:01:06 +00:00
|
|
|
bool m_relax_main_opaque;
|
2014-08-05 23:46:43 +00:00
|
|
|
choice_expr_elaborator(elaborator & elab, expr const & mvar, expr const & c,
|
|
|
|
list<expr> const & ctx, list<expr> const & full_ctx,
|
2014-08-12 22:48:16 +00:00
|
|
|
bool relax):
|
2014-08-05 23:46:43 +00:00
|
|
|
m_elab(elab), m_mvar(mvar), m_choice(c), m_ctx(ctx), m_full_ctx(full_ctx),
|
2014-08-12 22:48:16 +00:00
|
|
|
m_idx(0), m_relax_main_opaque(relax) {
|
2014-06-25 15:30:09 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 22:45:50 +00:00
|
|
|
virtual optional<constraints> next() {
|
2014-06-25 15:30:09 +00:00
|
|
|
while (m_idx < get_num_choices(m_choice)) {
|
|
|
|
expr const & c = get_choice(m_choice, m_idx);
|
|
|
|
m_idx++;
|
|
|
|
try {
|
2014-08-12 22:48:16 +00:00
|
|
|
new_scope s(m_elab, m_ctx, m_full_ctx);
|
2014-06-25 15:30:09 +00:00
|
|
|
expr r = m_elab.visit(c);
|
2014-06-26 20:35:36 +00:00
|
|
|
m_elab.consume_tc_cnstrs();
|
2014-06-25 15:30:09 +00:00
|
|
|
list<constraint> cs = to_list(m_elab.m_constraints.begin(), m_elab.m_constraints.end());
|
2014-08-12 22:36:26 +00:00
|
|
|
cs = cons(mk_eq_cnstr(m_mvar, r, justification(), m_relax_main_opaque), cs);
|
2014-07-04 19:47:33 +00:00
|
|
|
return optional<constraints>(cs);
|
2014-06-25 15:30:09 +00:00
|
|
|
} catch (exception &) {}
|
|
|
|
}
|
2014-07-04 19:47:33 +00:00
|
|
|
return optional<constraints>();
|
2014-06-25 15:30:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-06 23:46:34 +00:00
|
|
|
/** \brief Whenever the elaborator finds a placeholder '_' or introduces an implicit argument, it creates
|
2014-07-13 13:50:52 +00:00
|
|
|
a metavariable \c ?m. It also creates a delayed choice constraint (?m in fn).
|
|
|
|
|
|
|
|
The function \c fn produces a stream of alternative solutions for ?m.
|
2014-07-06 23:46:34 +00:00
|
|
|
In this case, \c fn will do the following:
|
2014-07-13 13:50:52 +00:00
|
|
|
1) if the elaborated type of ?m is a 'class' C, then the stream will start with
|
|
|
|
a) all local instances of class C (if elaborator.local_instances == true)
|
|
|
|
b) solutions produced by tactic_hints for class C
|
|
|
|
2) if the elaborated type of ?m is not a class, then the stream will only contain
|
|
|
|
the solutions produced by tactic_hints.
|
|
|
|
|
|
|
|
The unifier only process delayed choice constraints when there are no other kind of constraint to be
|
|
|
|
processed.
|
2014-07-06 23:46:34 +00:00
|
|
|
|
|
|
|
This is a helper class for implementing this choice function.
|
|
|
|
*/
|
2014-07-13 21:35:57 +00:00
|
|
|
struct placeholder_elaborator : public choice_elaborator {
|
2014-07-14 01:53:02 +00:00
|
|
|
elaborator & m_elab;
|
|
|
|
expr m_meta;
|
|
|
|
expr m_meta_type; // elaborated type of the metavariable
|
|
|
|
list<expr> m_local_instances; // local instances that should also be included in the class-instance resolution.
|
|
|
|
list<name> m_instances; // global declaration names that are class instances.
|
|
|
|
// This information is retrieved using #get_class_instances.
|
|
|
|
list<tactic_hint_entry> m_tactics;
|
|
|
|
proof_state_seq m_tactic_result; // result produce by last executed tactic.
|
|
|
|
buffer<expr> m_mvars_in_meta_type; // metavariables that occur in m_meta_type, the tactics may instantiate some of them
|
2014-08-05 23:25:54 +00:00
|
|
|
list<expr> m_ctx; // local context for m_meta
|
2014-08-05 23:46:43 +00:00
|
|
|
list<expr> m_full_ctx;
|
2014-07-14 01:53:02 +00:00
|
|
|
justification m_jst;
|
2014-07-27 19:01:06 +00:00
|
|
|
bool m_relax_main_opaque;
|
2014-07-14 01:53:02 +00:00
|
|
|
|
|
|
|
placeholder_elaborator(elaborator & elab, expr const & meta, expr const & meta_type,
|
|
|
|
list<expr> const & local_insts, list<name> const & instances, list<tactic_hint_entry> const & tacs,
|
2014-08-05 23:46:43 +00:00
|
|
|
list<expr> const & ctx, list<expr> const & full_ctx,
|
2014-08-12 22:48:16 +00:00
|
|
|
justification const & j, bool ignore_failure, bool relax):
|
2014-07-14 01:53:02 +00:00
|
|
|
choice_elaborator(ignore_failure),
|
|
|
|
m_elab(elab), m_meta(meta), m_meta_type(meta_type), m_local_instances(local_insts), m_instances(instances),
|
2014-08-12 22:48:16 +00:00
|
|
|
m_tactics(tacs), m_ctx(ctx), m_full_ctx(full_ctx), m_jst(j), m_relax_main_opaque(relax) {
|
2014-07-14 01:53:02 +00:00
|
|
|
collect_metavars(meta_type, m_mvars_in_meta_type);
|
2014-07-05 01:03:13 +00:00
|
|
|
}
|
2014-07-04 22:45:50 +00:00
|
|
|
|
2014-07-13 13:50:52 +00:00
|
|
|
optional<constraints> try_instance(name const & inst) {
|
2014-08-13 01:43:56 +00:00
|
|
|
auto decl = m_elab.env().find(inst);
|
2014-07-13 13:50:52 +00:00
|
|
|
if (!decl)
|
|
|
|
return optional<constraints>();
|
|
|
|
expr type = decl->get_type();
|
|
|
|
// create the term pre (inst _ ... _)
|
2014-07-14 01:53:02 +00:00
|
|
|
expr pre = copy_tag(m_meta, mk_explicit(copy_tag(m_meta, mk_constant(inst))));
|
2014-07-13 13:50:52 +00:00
|
|
|
while (is_pi(type)) {
|
|
|
|
type = binding_body(type);
|
2014-07-14 01:53:02 +00:00
|
|
|
pre = copy_tag(m_meta, ::lean::mk_app(pre, copy_tag(m_meta, mk_strict_expr_placeholder())));
|
2014-07-13 13:50:52 +00:00
|
|
|
}
|
|
|
|
try {
|
2014-08-12 22:48:16 +00:00
|
|
|
new_scope s(m_elab, m_ctx, m_full_ctx);
|
2014-07-13 13:50:52 +00:00
|
|
|
expr r = m_elab.visit(pre); // use elaborator to create metavariables, levels, etc.
|
|
|
|
m_elab.consume_tc_cnstrs();
|
2014-07-14 01:53:02 +00:00
|
|
|
for (auto & c : m_elab.m_constraints)
|
|
|
|
c = update_justification(c, mk_composite1(m_jst, c.get_justification()));
|
2014-07-13 13:50:52 +00:00
|
|
|
list<constraint> cs = to_list(m_elab.m_constraints.begin(), m_elab.m_constraints.end());
|
2014-08-12 22:36:26 +00:00
|
|
|
cs = cons(mk_eq_cnstr(m_meta, r, m_jst, m_relax_main_opaque), cs);
|
2014-07-13 13:50:52 +00:00
|
|
|
return optional<constraints>(cs);
|
|
|
|
} catch (exception &) {
|
|
|
|
return optional<constraints>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 01:53:02 +00:00
|
|
|
optional<constraints> get_next_tactic_result() {
|
|
|
|
while (auto next = m_tactic_result.pull()) {
|
|
|
|
m_tactic_result = next->second;
|
|
|
|
if (!empty(next->first.get_goals()))
|
|
|
|
continue; // has unsolved goals
|
|
|
|
substitution subst = next->first.get_subst();
|
|
|
|
buffer<constraint> cs;
|
|
|
|
expr const & mvar = get_app_fn(m_meta);
|
2014-07-27 19:01:06 +00:00
|
|
|
cs.push_back(mk_eq_cnstr(mvar, subst.instantiate(mvar), m_jst, m_relax_main_opaque));
|
2014-07-14 01:53:02 +00:00
|
|
|
for (auto const & mvar : m_mvars_in_meta_type)
|
2014-07-27 19:01:06 +00:00
|
|
|
cs.push_back(mk_eq_cnstr(mvar, subst.instantiate(mvar), m_jst, m_relax_main_opaque));
|
2014-07-14 01:53:02 +00:00
|
|
|
return optional<constraints>(to_list(cs.begin(), cs.end()));
|
|
|
|
}
|
|
|
|
return optional<constraints>();
|
|
|
|
}
|
|
|
|
|
2014-07-04 22:45:50 +00:00
|
|
|
virtual optional<constraints> next() {
|
2014-07-05 01:03:13 +00:00
|
|
|
while (!empty(m_local_instances)) {
|
|
|
|
expr inst = head(m_local_instances);
|
|
|
|
m_local_instances = tail(m_local_instances);
|
2014-07-27 19:01:06 +00:00
|
|
|
constraints cs(mk_eq_cnstr(m_meta, inst, m_jst, m_relax_main_opaque));
|
2014-07-05 01:03:13 +00:00
|
|
|
return optional<constraints>(cs);
|
|
|
|
}
|
2014-07-04 22:45:50 +00:00
|
|
|
while (!empty(m_instances)) {
|
|
|
|
name inst = head(m_instances);
|
|
|
|
m_instances = tail(m_instances);
|
2014-07-13 13:50:52 +00:00
|
|
|
if (auto cs = try_instance(inst))
|
|
|
|
return cs;
|
2014-07-04 22:45:50 +00:00
|
|
|
}
|
2014-07-14 01:53:02 +00:00
|
|
|
if (auto cs = get_next_tactic_result())
|
|
|
|
return cs;
|
|
|
|
while (!empty(m_tactics)) {
|
|
|
|
tactic const & tac = head(m_tactics).get_tactic();
|
|
|
|
m_tactics = tail(m_tactics);
|
2014-08-12 22:48:16 +00:00
|
|
|
proof_state ps(goals(goal(m_meta, m_meta_type)), substitution(), m_elab.m_ngen.mk_child());
|
2014-07-14 01:53:02 +00:00
|
|
|
try {
|
2014-08-13 01:43:56 +00:00
|
|
|
m_tactic_result = tac(m_elab.env(), m_elab.ios(), ps);
|
2014-07-14 01:53:02 +00:00
|
|
|
if (auto cs = get_next_tactic_result())
|
|
|
|
return cs;
|
2014-07-14 15:37:55 +00:00
|
|
|
} catch (exception &) {}
|
2014-07-14 01:53:02 +00:00
|
|
|
}
|
2014-07-04 22:45:50 +00:00
|
|
|
return optional<constraints>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-04 19:47:33 +00:00
|
|
|
lazy_list<constraints> choose(std::shared_ptr<choice_elaborator> c) {
|
|
|
|
return mk_lazy_list<constraints>([=]() {
|
2014-06-25 15:30:09 +00:00
|
|
|
auto s = c->next();
|
2014-07-08 22:04:52 +00:00
|
|
|
if (s) {
|
2014-06-25 15:30:09 +00:00
|
|
|
return some(mk_pair(*s, choose(c)));
|
2014-07-08 22:04:52 +00:00
|
|
|
} else if (c->m_ignore_failure) {
|
|
|
|
// return singleton empty list of constraints, and let tactic hints try to instantiate the metavariable.
|
|
|
|
return lazy_list<constraints>::maybe_pair(constraints(), lazy_list<constraints>());
|
|
|
|
} else {
|
2014-07-04 19:47:33 +00:00
|
|
|
return lazy_list<constraints>::maybe_pair();
|
2014-07-08 22:04:52 +00:00
|
|
|
}
|
2014-06-25 15:30:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2014-08-13 19:45:57 +00:00
|
|
|
elaborator(elaborator_context & env, list<expr> const & ctx, name_generator const & ngen):
|
2014-08-13 01:43:56 +00:00
|
|
|
m_env(env),
|
2014-07-27 19:01:06 +00:00
|
|
|
m_ngen(ngen),
|
2014-08-05 23:25:54 +00:00
|
|
|
m_context(m_ngen, m_mvar2meta, ctx),
|
2014-08-13 01:43:56 +00:00
|
|
|
m_full_context(m_ngen, m_mvar2meta, ctx) {
|
2014-07-27 19:01:06 +00:00
|
|
|
m_relax_main_opaque = false;
|
2014-08-13 01:43:56 +00:00
|
|
|
m_tc[0] = mk_type_checker_with_hints(env.m_env, m_ngen.mk_child(), false);
|
|
|
|
m_tc[1] = mk_type_checker_with_hints(env.m_env, m_ngen.mk_child(), true);
|
2014-06-25 15:30:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 01:43:56 +00:00
|
|
|
environment const & env() const { return m_env.m_env; }
|
|
|
|
io_state const & ios() const { return m_env.m_ios; }
|
|
|
|
local_decls<level> const & lls() const { return m_env.m_lls; }
|
|
|
|
bool use_local_instances() const { return m_env.m_use_local_instances; }
|
|
|
|
info_manager * infom() const { return m_env.m_info_manager; }
|
|
|
|
pos_info_provider const * pip() const { return m_env.m_pos_provider; }
|
|
|
|
bool check_unassigned() const { return m_env.m_check_unassigned; }
|
|
|
|
|
2014-06-30 16:14:55 +00:00
|
|
|
expr mk_local(name const & n, expr const & t, binder_info const & bi) {
|
|
|
|
return ::lean::mk_local(m_ngen.next(), n, t, bi);
|
2014-06-25 18:04:50 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 15:30:09 +00:00
|
|
|
expr infer_type(expr const & e) {
|
|
|
|
lean_assert(closed(e));
|
2014-07-27 19:01:06 +00:00
|
|
|
return m_tc[m_relax_main_opaque]->infer(e);
|
|
|
|
}
|
2014-06-25 18:04:50 +00:00
|
|
|
|
|
|
|
expr whnf(expr const & e) {
|
2014-07-27 19:01:06 +00:00
|
|
|
return m_tc[m_relax_main_opaque]->whnf(e);
|
2014-06-25 18:04:50 +00:00
|
|
|
}
|
2014-06-25 15:30:09 +00:00
|
|
|
|
2014-08-12 22:48:16 +00:00
|
|
|
/** \brief Clear constraint buffer \c m_constraints */
|
2014-06-25 15:30:09 +00:00
|
|
|
void clear_constraints() {
|
|
|
|
m_constraints.clear();
|
|
|
|
}
|
|
|
|
|
2014-08-12 22:36:26 +00:00
|
|
|
void add_cnstr(constraint const & c) {
|
2014-06-24 21:55:06 +00:00
|
|
|
m_constraints.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static expr save_tag(expr && e, tag g) {
|
|
|
|
e.set_tag(g);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2014-07-12 16:49:14 +00:00
|
|
|
expr mk_app(expr const & f, expr const & a, tag g) {
|
|
|
|
return save_tag(::lean::mk_app(f, a), g);
|
|
|
|
}
|
|
|
|
|
2014-07-04 22:45:50 +00:00
|
|
|
list<name> get_class_instances(expr const & type) {
|
|
|
|
if (is_constant(get_app_fn(type))) {
|
|
|
|
name const & c = const_name(get_app_fn(type));
|
2014-08-13 01:43:56 +00:00
|
|
|
return ::lean::get_class_instances(env(), c);
|
2014-07-04 22:45:50 +00:00
|
|
|
} else {
|
|
|
|
return list<name>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_class(expr const & type) {
|
2014-07-05 01:49:05 +00:00
|
|
|
expr f = get_app_fn(type);
|
2014-07-14 01:53:02 +00:00
|
|
|
if (!is_constant(f))
|
|
|
|
return false;
|
|
|
|
name const & cls_name = const_name(f);
|
2014-08-13 01:43:56 +00:00
|
|
|
return ::lean::is_class(env(), cls_name) || !empty(get_tactic_hints(env(), cls_name));
|
2014-07-04 22:45:50 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 15:51:24 +00:00
|
|
|
static expr instantiate_meta(expr const & meta, substitution & subst) {
|
2014-07-14 01:53:02 +00:00
|
|
|
buffer<expr> locals;
|
|
|
|
expr mvar = get_app_args(meta, locals);
|
2014-07-23 21:21:47 +00:00
|
|
|
mvar = update_mlocal(mvar, subst.instantiate_all(mlocal_type(mvar)));
|
|
|
|
for (auto & local : locals) local = subst.instantiate_all(local);
|
2014-07-14 01:53:02 +00:00
|
|
|
return ::lean::mk_app(mvar, locals);
|
2014-07-04 22:45:50 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 21:35:57 +00:00
|
|
|
/** \brief Return a 'failed to synthesize placholder' justification for the given
|
|
|
|
metavariable application \c m of the form (?m l_1 ... l_k) */
|
|
|
|
justification mk_failed_to_synthesize_jst(expr const & m) {
|
2014-08-13 01:43:56 +00:00
|
|
|
environment _env = env();
|
2014-07-13 21:35:57 +00:00
|
|
|
return mk_justification(m, [=](formatter const & fmt, substitution const & subst) {
|
2014-07-23 15:51:24 +00:00
|
|
|
substitution tmp(subst);
|
|
|
|
expr new_m = instantiate_meta(m, tmp);
|
2014-08-13 01:43:56 +00:00
|
|
|
expr new_type = type_checker(_env).infer(new_m);
|
2014-07-13 21:35:57 +00:00
|
|
|
proof_state ps(goals(goal(new_m, new_type)), substitution(), name_generator("dontcare"));
|
|
|
|
return format({format("failed to synthesize placeholder"), line(), ps.pp(fmt)});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-14 01:53:02 +00:00
|
|
|
/** \brief Create a metavariable, and attach choice constraint for generating
|
|
|
|
solutions using class-instances and tactic-hints.
|
2014-07-04 22:45:50 +00:00
|
|
|
*/
|
2014-07-14 01:53:02 +00:00
|
|
|
expr mk_placeholder_meta(optional<expr> const & type, tag g, bool is_strict = false) {
|
2014-08-05 23:46:43 +00:00
|
|
|
expr m = m_context.mk_meta(type, g);
|
|
|
|
list<expr> ctx = m_context.get_data();
|
|
|
|
list<expr> full_ctx = m_full_context.get_data();
|
|
|
|
justification j = mk_failed_to_synthesize_jst(m);
|
2014-07-14 01:53:02 +00:00
|
|
|
auto choice_fn = [=](expr const & meta, expr const & meta_type, substitution const & s, name_generator const & /* ngen */) {
|
|
|
|
expr const & mvar = get_app_fn(meta);
|
|
|
|
if (is_class(meta_type)) {
|
|
|
|
name const & cls_name = const_name(get_app_fn(meta_type));
|
2014-07-05 01:37:49 +00:00
|
|
|
list<expr> local_insts;
|
2014-08-13 01:43:56 +00:00
|
|
|
if (use_local_instances())
|
2014-07-14 01:53:02 +00:00
|
|
|
local_insts = get_local_instances(ctx, cls_name);
|
|
|
|
list<name> insts = get_class_instances(meta_type);
|
|
|
|
list<tactic_hint_entry> tacs;
|
|
|
|
if (!s.is_assigned(mvar))
|
2014-08-13 01:43:56 +00:00
|
|
|
tacs = get_tactic_hints(env(), cls_name);
|
2014-07-14 01:53:02 +00:00
|
|
|
if (empty(local_insts) && empty(insts) && empty(tacs))
|
|
|
|
return lazy_list<constraints>(); // nothing to be done
|
|
|
|
bool ignore_failure = false; // we are always strict with placeholders associated with classes
|
2014-08-05 23:46:43 +00:00
|
|
|
return choose(std::make_shared<placeholder_elaborator>(*this, meta, meta_type, local_insts, insts, tacs, ctx, full_ctx,
|
2014-08-12 22:48:16 +00:00
|
|
|
j, ignore_failure, m_relax_main_opaque));
|
2014-07-14 01:53:02 +00:00
|
|
|
} else if (s.is_assigned(mvar)) {
|
|
|
|
// if the metavariable is assigned and it is not a class, then we just ignore it, and return
|
|
|
|
// the an empty set of constraints.
|
|
|
|
return lazy_list<constraints>(constraints());
|
|
|
|
} else {
|
2014-08-13 01:43:56 +00:00
|
|
|
list<tactic_hint_entry> tacs = get_tactic_hints(env());
|
2014-07-14 01:53:02 +00:00
|
|
|
bool ignore_failure = !is_strict;
|
2014-08-05 23:46:43 +00:00
|
|
|
return choose(std::make_shared<placeholder_elaborator>(*this, meta, meta_type, list<expr>(), list<name>(), tacs, ctx, full_ctx,
|
2014-08-12 22:48:16 +00:00
|
|
|
j, ignore_failure, m_relax_main_opaque));
|
2014-07-14 01:53:02 +00:00
|
|
|
}
|
|
|
|
};
|
2014-08-04 20:30:28 +00:00
|
|
|
add_cnstr(mk_choice_cnstr(m, choice_fn, to_delay_factor(cnstr_group::DelayedChoice2), false, j, m_relax_main_opaque));
|
2014-07-04 22:45:50 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2014-07-06 23:46:34 +00:00
|
|
|
/** \brief Convert the metavariable to the metavariable application that captures
|
2014-07-02 03:43:53 +00:00
|
|
|
the context where it was defined.
|
|
|
|
*/
|
|
|
|
optional<expr> mvar_to_meta(expr mvar) {
|
|
|
|
if (auto it = m_mvar2meta.find(mlocal_name(mvar)))
|
|
|
|
return some_expr(*it);
|
|
|
|
else
|
|
|
|
return none_expr();
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 19:45:54 +00:00
|
|
|
void save_placeholder_info(expr const & e, expr const & r) {
|
|
|
|
if (is_explicit_placeholder(e)) {
|
|
|
|
save_info_data(e, r);
|
|
|
|
save_synth_data(e, r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 21:55:06 +00:00
|
|
|
expr visit_expecting_type(expr const & e) {
|
2014-08-14 15:49:43 +00:00
|
|
|
if (is_placeholder(e) && !placeholder_type(e)) {
|
|
|
|
expr r = m_context.mk_type_meta(e.get_tag());
|
2014-08-15 19:45:54 +00:00
|
|
|
save_placeholder_info(e, r);
|
2014-08-14 15:49:43 +00:00
|
|
|
return r;
|
|
|
|
} else {
|
2014-06-24 21:55:06 +00:00
|
|
|
return visit(e);
|
2014-08-14 15:49:43 +00:00
|
|
|
}
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr visit_expecting_type_of(expr const & e, expr const & t) {
|
2014-08-14 15:49:43 +00:00
|
|
|
if (is_placeholder(e) && !placeholder_type(e)) {
|
|
|
|
expr r = mk_placeholder_meta(some_expr(t), e.get_tag(), is_strict_placeholder(e));
|
2014-08-15 19:45:54 +00:00
|
|
|
save_placeholder_info(e, r);
|
2014-08-14 15:49:43 +00:00
|
|
|
return r;
|
|
|
|
} else if (is_choice(e)) {
|
2014-06-24 21:55:06 +00:00
|
|
|
return visit_choice(e, some_expr(t));
|
2014-08-14 15:49:43 +00:00
|
|
|
} else if (is_by(e)) {
|
2014-07-02 03:43:53 +00:00
|
|
|
return visit_by(e, some_expr(t));
|
2014-08-14 15:49:43 +00:00
|
|
|
} else {
|
2014-06-24 21:55:06 +00:00
|
|
|
return visit(e);
|
2014-08-14 15:49:43 +00:00
|
|
|
}
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr visit_choice(expr const & e, optional<expr> const & t) {
|
|
|
|
lean_assert(is_choice(e));
|
|
|
|
// Possible optimization: try to lookahead and discard some of the alternatives.
|
2014-08-05 23:46:43 +00:00
|
|
|
expr m = m_full_context.mk_meta(t, e.get_tag());
|
|
|
|
list<expr> ctx = m_context.get_data();
|
|
|
|
list<expr> full_ctx = m_full_context.get_data();
|
|
|
|
bool relax = m_relax_main_opaque;
|
2014-08-12 22:48:16 +00:00
|
|
|
auto fn = [=](expr const & mvar, expr const & /* type */, substitution const & /* s */, name_generator const & /* ngen */) {
|
|
|
|
return choose(std::make_shared<choice_expr_elaborator>(*this, mvar, e, ctx, full_ctx, relax));
|
2014-06-24 21:55:06 +00:00
|
|
|
};
|
2014-06-26 15:52:40 +00:00
|
|
|
justification j = mk_justification("none of the overloads is applicable", some_expr(e));
|
2014-07-30 00:32:55 +00:00
|
|
|
add_cnstr(mk_choice_cnstr(m, fn, to_delay_factor(cnstr_group::Basic), true, j, m_relax_main_opaque));
|
2014-06-24 21:55:06 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2014-07-02 03:43:53 +00:00
|
|
|
expr visit_by(expr const & e, optional<expr> const & t) {
|
|
|
|
lean_assert(is_by(e));
|
|
|
|
expr tac = visit(get_by_arg(e));
|
2014-08-05 23:25:54 +00:00
|
|
|
expr m = m_context.mk_meta(t, e.get_tag());
|
2014-07-08 21:28:33 +00:00
|
|
|
m_local_tactic_hints.insert(mlocal_name(get_app_fn(m)), tac);
|
2014-07-02 03:43:53 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2014-07-06 23:46:34 +00:00
|
|
|
/** \brief Make sure \c f is really a function, if it is not, try to apply coercions.
|
2014-06-24 21:55:06 +00:00
|
|
|
The result is a pair <tt>new_f, f_type</tt>, where new_f is the new value for \c f,
|
|
|
|
and \c f_type is its type (and a Pi-expression)
|
|
|
|
*/
|
|
|
|
std::pair<expr, expr> ensure_fun(expr f) {
|
|
|
|
expr f_type = infer_type(f);
|
|
|
|
if (!is_pi(f_type))
|
|
|
|
f_type = whnf(f_type);
|
|
|
|
if (!is_pi(f_type) && has_metavar(f_type)) {
|
2014-08-12 22:36:26 +00:00
|
|
|
f_type = whnf(f_type);
|
2014-06-24 21:55:06 +00:00
|
|
|
if (!is_pi(f_type) && is_meta(f_type)) {
|
|
|
|
// let type checker add constraint
|
2014-07-27 19:01:06 +00:00
|
|
|
f_type = m_tc[m_relax_main_opaque]->ensure_pi(f_type, f);
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is_pi(f_type)) {
|
|
|
|
// try coercion to function-class
|
2014-08-13 01:43:56 +00:00
|
|
|
optional<expr> c = get_coercion_to_fun(env(), f_type);
|
2014-06-24 21:55:06 +00:00
|
|
|
if (c) {
|
|
|
|
f = mk_app(*c, f, f.get_tag());
|
|
|
|
f_type = infer_type(f);
|
|
|
|
lean_assert(is_pi(f_type));
|
|
|
|
} else {
|
2014-08-13 01:43:56 +00:00
|
|
|
throw_kernel_exception(env(), f, [=](formatter const & fmt) { return pp_function_expected(fmt, f); });
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
lean_assert(is_pi(f_type));
|
|
|
|
return mk_pair(f, f_type);
|
|
|
|
}
|
|
|
|
|
2014-06-27 01:39:23 +00:00
|
|
|
bool has_coercions_from(expr const & a_type) {
|
|
|
|
expr const & a_cls = get_app_fn(whnf(a_type));
|
2014-08-13 01:43:56 +00:00
|
|
|
return is_constant(a_cls) && ::lean::has_coercions_from(env(), const_name(a_cls));
|
2014-06-27 01:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool has_coercions_to(expr const & d_type) {
|
|
|
|
expr const & d_cls = get_app_fn(whnf(d_type));
|
2014-08-13 01:43:56 +00:00
|
|
|
return is_constant(d_cls) && ::lean::has_coercions_to(env(), const_name(d_cls));
|
2014-06-27 01:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr apply_coercion(expr const & a, expr a_type, expr d_type) {
|
2014-06-24 21:55:06 +00:00
|
|
|
a_type = whnf(a_type);
|
|
|
|
d_type = whnf(d_type);
|
|
|
|
expr const & d_cls = get_app_fn(d_type);
|
2014-06-27 01:39:23 +00:00
|
|
|
if (is_constant(d_cls)) {
|
2014-08-13 01:43:56 +00:00
|
|
|
if (auto c = get_coercion(env(), a_type, const_name(d_cls)))
|
2014-06-27 01:39:23 +00:00
|
|
|
return mk_app(*c, a, a.get_tag());
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2014-08-05 23:46:43 +00:00
|
|
|
constraint mk_delayed_coercion_cnstr(expr const & m, expr const & a, expr const & a_type,
|
|
|
|
justification const & j, unsigned delay_factor) {
|
2014-07-27 19:01:06 +00:00
|
|
|
bool relax = m_relax_main_opaque;
|
2014-07-07 19:40:00 +00:00
|
|
|
auto choice_fn = [=](expr const & mvar, expr const & new_d_type, substitution const & /* s */, name_generator const & /* ngen */) {
|
|
|
|
// Remark: we want the coercions solved before we start discarding FlexFlex constraints. So, we use PreFlexFlex as a max cap
|
|
|
|
// for delaying coercions.
|
2014-08-04 20:30:28 +00:00
|
|
|
if (is_meta(new_d_type) && delay_factor < to_delay_factor(cnstr_group::DelayedChoice1)) {
|
2014-07-07 19:40:00 +00:00
|
|
|
// The type is still unknown, delay the constraint even more.
|
|
|
|
return lazy_list<constraints>(constraints(mk_delayed_coercion_cnstr(m, a, a_type, justification(), delay_factor+1)));
|
|
|
|
} else {
|
|
|
|
expr r = apply_coercion(a, a_type, new_d_type);
|
2014-07-27 19:01:06 +00:00
|
|
|
return lazy_list<constraints>(constraints(mk_eq_cnstr(mvar, r, justification(), relax)));
|
2014-07-07 19:40:00 +00:00
|
|
|
}
|
|
|
|
};
|
2014-07-30 00:32:55 +00:00
|
|
|
return mk_choice_cnstr(m, choice_fn, delay_factor, true, j, m_relax_main_opaque);
|
2014-07-07 19:40:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 20:55:39 +00:00
|
|
|
/** \brief Given a term <tt>a : a_type</tt>, and an expected type generate a metavariable with a delayed coercion. */
|
|
|
|
expr mk_delayed_coercion(expr const & a, expr const & a_type, expr const & expected_type, justification const & j) {
|
2014-08-05 23:46:43 +00:00
|
|
|
expr m = m_full_context.mk_meta(some_expr(expected_type), a.get_tag());
|
2014-07-07 19:40:00 +00:00
|
|
|
add_cnstr(mk_delayed_coercion_cnstr(m, a, a_type, j, to_delay_factor(cnstr_group::Basic)));
|
2014-07-29 20:55:39 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Given a term <tt>a : a_type</tt>, ensure it has type \c expected_type. Apply coercions if needed
|
|
|
|
|
|
|
|
\remark relax == true affects how opaque definitions in the main module are treated.
|
|
|
|
*/
|
|
|
|
expr ensure_type(expr const & a, expr const & a_type, expr const & expected_type, justification const & j, bool relax) {
|
|
|
|
if (is_meta(expected_type) && has_coercions_from(a_type)) {
|
|
|
|
return mk_delayed_coercion(a, a_type, expected_type, j);
|
|
|
|
} else if (is_meta(a_type) && has_coercions_to(expected_type)) {
|
|
|
|
return mk_delayed_coercion(a, a_type, expected_type, j);
|
|
|
|
} else if (m_tc[relax]->is_def_eq(a_type, expected_type, j)) {
|
|
|
|
return a;
|
|
|
|
} else {
|
|
|
|
expr new_a = apply_coercion(a, a_type, expected_type);
|
|
|
|
bool coercion_worked = false;
|
|
|
|
if (!is_eqp(a, new_a)) {
|
2014-08-12 22:36:26 +00:00
|
|
|
expr new_a_type = infer_type(new_a);
|
2014-07-29 20:55:39 +00:00
|
|
|
coercion_worked = m_tc[relax]->is_def_eq(new_a_type, expected_type, j);
|
|
|
|
}
|
|
|
|
if (coercion_worked) {
|
|
|
|
return new_a;
|
|
|
|
} else if (has_metavar(a_type) || has_metavar(expected_type)) {
|
|
|
|
// rely on unification hints to solve this constraint
|
|
|
|
add_cnstr(mk_eq_cnstr(a_type, expected_type, j, relax));
|
|
|
|
return a;
|
|
|
|
} else {
|
2014-08-12 22:48:16 +00:00
|
|
|
throw unifier_exception(j, substitution());
|
2014-07-29 20:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 06:43:40 +00:00
|
|
|
bool is_choice_app(expr const & e) {
|
|
|
|
expr const & f = get_app_fn(e);
|
|
|
|
return is_choice(f) || (is_explicit(f) && is_choice(get_explicit_arg(f)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Process ((choice f_1 ... f_n) a_1 ... a_k) as
|
|
|
|
(choice (f_1 a_1 ... a_k) ... (f_n a_1 ... a_k))
|
|
|
|
*/
|
|
|
|
expr visit_choice_app(expr const & e) {
|
|
|
|
buffer<expr> args;
|
|
|
|
expr f = get_app_rev_args(e, args);
|
|
|
|
bool expl = is_explicit(f);
|
|
|
|
if (expl)
|
|
|
|
f = get_explicit_arg(f);
|
|
|
|
lean_assert(is_choice(f));
|
|
|
|
buffer<expr> new_choices;
|
|
|
|
unsigned num = get_num_choices(f);
|
|
|
|
for (unsigned i = 0; i < num; i++) {
|
|
|
|
expr f_i = get_choice(f, i);
|
|
|
|
if (expl)
|
2014-08-05 22:42:31 +00:00
|
|
|
f_i = copy_tag(f_i, mk_explicit(f_i));
|
2014-07-25 06:43:40 +00:00
|
|
|
new_choices.push_back(mk_rev_app(f_i, args));
|
|
|
|
}
|
2014-08-05 22:42:31 +00:00
|
|
|
return visit_choice(copy_tag(e, mk_choice(new_choices.size(), new_choices.data())), none_expr());
|
2014-07-25 06:43:40 +00:00
|
|
|
}
|
|
|
|
|
2014-06-24 21:55:06 +00:00
|
|
|
expr visit_app(expr const & e) {
|
2014-07-25 06:43:40 +00:00
|
|
|
if (is_choice_app(e))
|
|
|
|
return visit_choice_app(e);
|
2014-06-28 14:30:36 +00:00
|
|
|
bool expl = is_explicit(get_app_fn(e));
|
2014-06-24 21:55:06 +00:00
|
|
|
expr f = visit(app_fn(e));
|
|
|
|
auto f_t = ensure_fun(f);
|
|
|
|
f = f_t.first;
|
2014-06-26 00:47:38 +00:00
|
|
|
expr f_type = f_t.second;
|
|
|
|
lean_assert(is_pi(f_type));
|
2014-06-28 14:30:36 +00:00
|
|
|
if (!expl) {
|
2014-08-01 17:58:20 +00:00
|
|
|
bool first = true;
|
|
|
|
while (binding_info(f_type).is_strict_implicit() || (!first && binding_info(f_type).is_implicit())) {
|
2014-06-28 14:30:36 +00:00
|
|
|
tag g = f.get_tag();
|
2014-07-04 22:45:50 +00:00
|
|
|
expr imp_arg = mk_placeholder_meta(some_expr(binding_domain(f_type)), g);
|
2014-06-28 14:30:36 +00:00
|
|
|
f = mk_app(f, imp_arg, g);
|
2014-07-18 22:47:53 +00:00
|
|
|
auto f_t = ensure_fun(f);
|
|
|
|
f = f_t.first;
|
|
|
|
f_type = f_t.second;
|
2014-08-01 17:58:20 +00:00
|
|
|
first = false;
|
2014-06-28 14:30:36 +00:00
|
|
|
}
|
2014-08-02 03:57:24 +00:00
|
|
|
if (!first) {
|
2014-08-07 02:35:26 +00:00
|
|
|
// we save the info data again for application of functions with strict implicit arguments
|
|
|
|
replace_info_data(get_app_fn(e), f);
|
2014-08-02 03:57:24 +00:00
|
|
|
}
|
2014-06-26 00:47:38 +00:00
|
|
|
}
|
|
|
|
expr d_type = binding_domain(f_type);
|
2014-06-24 21:55:06 +00:00
|
|
|
expr a = visit_expecting_type_of(app_arg(e), d_type);
|
2014-08-12 22:36:26 +00:00
|
|
|
expr a_type = infer_type(a);
|
2014-06-25 18:04:50 +00:00
|
|
|
expr r = mk_app(f, a, e.get_tag());
|
2014-06-27 01:39:23 +00:00
|
|
|
|
2014-07-29 21:24:12 +00:00
|
|
|
justification j = mk_app_justification(r, a, d_type, a_type);
|
2014-07-29 20:55:39 +00:00
|
|
|
expr new_a = ensure_type(a, a_type, d_type, j, m_relax_main_opaque);
|
|
|
|
return update_app(r, app_fn(r), new_a);
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr visit_placeholder(expr const & e) {
|
2014-08-14 15:49:43 +00:00
|
|
|
expr r = mk_placeholder_meta(placeholder_type(e), e.get_tag(), is_strict_placeholder(e));
|
2014-08-15 19:45:54 +00:00
|
|
|
save_placeholder_info(e, r);
|
2014-08-14 15:49:43 +00:00
|
|
|
return r;
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
level replace_univ_placeholder(level const & l) {
|
|
|
|
return replace(l, [&](level const & l) {
|
|
|
|
if (is_placeholder(l))
|
|
|
|
return some_level(mk_meta_univ(m_ngen.next()));
|
|
|
|
else
|
|
|
|
return none_level();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
expr visit_sort(expr const & e) {
|
2014-08-12 22:00:32 +00:00
|
|
|
return update_sort(e, replace_univ_placeholder(sort_level(e)));
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr visit_macro(expr const & e) {
|
2014-07-14 05:27:36 +00:00
|
|
|
if (is_as_is(e)) {
|
|
|
|
return get_as_is_arg(e);
|
|
|
|
} else {
|
|
|
|
// Remark: Macros are not meant to be used in the front end.
|
|
|
|
// Perhaps, we should throw error.
|
|
|
|
buffer<expr> args;
|
|
|
|
for (unsigned i = 0; i < macro_num_args(e); i++)
|
|
|
|
args.push_back(visit(macro_arg(e, i)));
|
|
|
|
return update_macro(e, args.size(), args.data());
|
|
|
|
}
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 02:35:26 +00:00
|
|
|
/** \brief Store the pair (pos(e), type(r)) in the info_data if m_info_manager is available. */
|
|
|
|
void save_info_data_core(expr const & e, expr const & r, bool replace) {
|
2014-08-13 01:43:56 +00:00
|
|
|
if (infom() && pip() && (is_constant(e) || is_local(e) || is_placeholder(e))) {
|
|
|
|
if (auto p = pip()->get_pos_info(e)) {
|
2014-08-02 03:17:26 +00:00
|
|
|
type_checker::scope scope(*m_tc[m_relax_main_opaque]);
|
|
|
|
expr t = m_tc[m_relax_main_opaque]->infer(r);
|
2014-08-02 04:15:02 +00:00
|
|
|
if (replace) {
|
2014-08-14 17:37:24 +00:00
|
|
|
while (!m_pre_info_data.empty() && m_pre_info_data.back()->eq_pos(p->first, p->second))
|
2014-08-07 04:23:37 +00:00
|
|
|
m_pre_info_data.pop_back();
|
2014-08-02 04:15:02 +00:00
|
|
|
}
|
2014-08-14 17:37:24 +00:00
|
|
|
m_pre_info_data.push_back(std::unique_ptr<info_data>(new type_info_data(p->first, p->second, t)));
|
2014-08-02 03:17:26 +00:00
|
|
|
}
|
2014-08-01 02:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-07 02:35:26 +00:00
|
|
|
void save_info_data(expr const & e, expr const & r) {
|
|
|
|
save_info_data_core(e, r, false);
|
2014-08-02 04:15:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 02:35:26 +00:00
|
|
|
void replace_info_data(expr const & e, expr const & r) {
|
|
|
|
save_info_data_core(e, r, true);
|
2014-08-02 04:15:02 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 17:37:24 +00:00
|
|
|
void save_synth_data(expr const & e, expr const & r) {
|
|
|
|
if (infom() && pip() && is_placeholder(e)) {
|
|
|
|
if (auto p = pip()->get_pos_info(e)) {
|
|
|
|
m_pre_info_data.push_back(std::unique_ptr<info_data>(new synth_info_data(p->first, p->second, r)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 21:55:06 +00:00
|
|
|
expr visit_constant(expr const & e) {
|
2014-08-13 01:43:56 +00:00
|
|
|
declaration d = env().get(const_name(e));
|
2014-08-12 22:00:32 +00:00
|
|
|
buffer<level> ls;
|
|
|
|
for (level const & l : const_levels(e))
|
|
|
|
ls.push_back(replace_univ_placeholder(l));
|
|
|
|
unsigned num_univ_params = length(d.get_univ_params());
|
|
|
|
if (num_univ_params < ls.size())
|
2014-08-13 01:43:56 +00:00
|
|
|
throw_kernel_exception(env(), sstream() << "incorrect number of universe levels parameters for '"
|
2014-08-12 22:00:32 +00:00
|
|
|
<< const_name(e) << "', #" << num_univ_params
|
|
|
|
<< " expected, #" << ls.size() << " provided");
|
|
|
|
// "fill" with meta universe parameters
|
|
|
|
for (unsigned i = ls.size(); i < num_univ_params; i++)
|
|
|
|
ls.push_back(mk_meta_univ(m_ngen.next()));
|
|
|
|
lean_assert(num_univ_params == ls.size());
|
|
|
|
return update_constant(e, to_list(ls.begin(), ls.end()));
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Make sure \c e is a type. If it is not, then try to apply coercions. */
|
|
|
|
expr ensure_type(expr const & e) {
|
|
|
|
expr t = infer_type(e);
|
|
|
|
if (is_sort(t))
|
|
|
|
return e;
|
|
|
|
t = whnf(t);
|
|
|
|
if (is_sort(t))
|
|
|
|
return e;
|
|
|
|
if (has_metavar(t)) {
|
2014-08-12 22:36:26 +00:00
|
|
|
t = whnf(t);
|
2014-06-24 21:55:06 +00:00
|
|
|
if (is_sort(t))
|
|
|
|
return e;
|
|
|
|
if (is_meta(t)) {
|
|
|
|
// let type checker add constraint
|
2014-07-27 19:01:06 +00:00
|
|
|
m_tc[m_relax_main_opaque]->ensure_sort(t, e);
|
2014-06-24 21:55:06 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
2014-08-13 01:43:56 +00:00
|
|
|
optional<expr> c = get_coercion_to_sort(env(), t);
|
2014-06-24 21:55:06 +00:00
|
|
|
if (c)
|
|
|
|
return mk_app(*c, e, e.get_tag());
|
2014-08-13 01:43:56 +00:00
|
|
|
throw_kernel_exception(env(), e, [=](formatter const & fmt) { return pp_type_expected(fmt, e); });
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
|
2014-08-01 02:54:21 +00:00
|
|
|
/** \brief Similar to instantiate_rev, but assumes that subst contains only local constants.
|
|
|
|
When replacing a variable with a local, we copy the local constant and inherit the tag
|
|
|
|
associated with the variable. This is a trick for getter better error messages */
|
|
|
|
expr instantiate_rev_locals(expr const & a, unsigned n, expr const * subst) {
|
|
|
|
if (closed(a))
|
|
|
|
return a;
|
|
|
|
return replace(a, [=](expr const & m, unsigned offset) -> optional<expr> {
|
|
|
|
if (offset >= get_free_var_range(m))
|
|
|
|
return some_expr(m); // expression m does not contain free variables with idx >= offset
|
|
|
|
if (is_var(m)) {
|
|
|
|
unsigned vidx = var_idx(m);
|
|
|
|
if (vidx >= offset) {
|
|
|
|
unsigned h = offset + n;
|
|
|
|
if (h < offset /* overflow, h is bigger than any vidx */ || vidx < h) {
|
|
|
|
expr local = subst[n - (vidx - offset) - 1];
|
|
|
|
lean_assert(is_local(local));
|
|
|
|
return some_expr(copy_tag(m, copy(local)));
|
|
|
|
} else {
|
|
|
|
return some_expr(copy_tag(m, mk_var(vidx - n)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none_expr();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-14 15:57:20 +00:00
|
|
|
expr visit_binding(expr e, expr_kind k) {
|
|
|
|
scope_ctx scope(*this);
|
|
|
|
buffer<expr> ds, ls, es;
|
|
|
|
while (e.kind() == k) {
|
|
|
|
es.push_back(e);
|
|
|
|
expr d = binding_domain(e);
|
2014-08-01 02:54:21 +00:00
|
|
|
d = instantiate_rev_locals(d, ls.size(), ls.data());
|
2014-07-14 15:57:20 +00:00
|
|
|
d = ensure_type(visit_expecting_type(d));
|
|
|
|
ds.push_back(d);
|
|
|
|
expr l = mk_local(binding_name(e), d, binding_info(e));
|
|
|
|
if (binding_info(e).is_contextual())
|
2014-08-05 23:25:54 +00:00
|
|
|
m_context.add_local(l);
|
2014-08-05 23:46:43 +00:00
|
|
|
m_full_context.add_local(l);
|
2014-07-14 15:57:20 +00:00
|
|
|
ls.push_back(l);
|
|
|
|
e = binding_body(e);
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
2014-07-14 15:57:20 +00:00
|
|
|
lean_assert(ls.size() == es.size() && ls.size() == ds.size());
|
2014-08-01 02:54:21 +00:00
|
|
|
e = instantiate_rev_locals(e, ls.size(), ls.data());
|
2014-07-14 15:57:20 +00:00
|
|
|
e = (k == expr_kind::Pi) ? ensure_type(visit_expecting_type(e)) : visit(e);
|
|
|
|
e = abstract_locals(e, ls.size(), ls.data());
|
|
|
|
unsigned i = ls.size();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
e = update_binding(es[i], abstract_locals(ds[i], i, ls.data()), e);
|
|
|
|
}
|
|
|
|
return e;
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
2014-07-14 15:57:20 +00:00
|
|
|
expr visit_pi(expr const & e) { return visit_binding(e, expr_kind::Pi); }
|
|
|
|
expr visit_lambda(expr const & e) { return visit_binding(e, expr_kind::Lambda); }
|
2014-06-24 21:55:06 +00:00
|
|
|
|
|
|
|
expr visit_core(expr const & e) {
|
|
|
|
if (is_placeholder(e)) {
|
|
|
|
return visit_placeholder(e);
|
|
|
|
} else if (is_choice(e)) {
|
|
|
|
return visit_choice(e, none_expr());
|
2014-07-02 03:43:53 +00:00
|
|
|
} else if (is_by(e)) {
|
|
|
|
return visit_by(e, none_expr());
|
2014-06-24 21:55:06 +00:00
|
|
|
} else {
|
|
|
|
switch (e.kind()) {
|
|
|
|
case expr_kind::Local: return e;
|
|
|
|
case expr_kind::Meta: return e;
|
|
|
|
case expr_kind::Sort: return visit_sort(e);
|
|
|
|
case expr_kind::Var: lean_unreachable(); // LCOV_EXCL_LINE
|
|
|
|
case expr_kind::Constant: return visit_constant(e);
|
|
|
|
case expr_kind::Macro: return visit_macro(e);
|
|
|
|
case expr_kind::Lambda: return visit_lambda(e);
|
|
|
|
case expr_kind::Pi: return visit_pi(e);
|
|
|
|
case expr_kind::App: return visit_app(e);
|
|
|
|
}
|
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expr visit(expr const & e) {
|
|
|
|
expr r;
|
2014-08-02 03:57:24 +00:00
|
|
|
expr b = e;
|
2014-06-24 21:55:06 +00:00
|
|
|
if (is_explicit(e)) {
|
2014-08-02 03:57:24 +00:00
|
|
|
b = get_explicit_arg(e);
|
2014-06-24 21:55:06 +00:00
|
|
|
r = visit_core(get_explicit_arg(e));
|
2014-06-28 14:30:36 +00:00
|
|
|
} else if (is_explicit(get_app_fn(e))) {
|
|
|
|
r = visit_core(e);
|
2014-06-24 21:55:06 +00:00
|
|
|
} else {
|
2014-07-19 08:55:34 +00:00
|
|
|
if (is_implicit(e)) {
|
|
|
|
r = get_implicit_arg(e);
|
|
|
|
if (is_explicit(r)) r = get_explicit_arg(r);
|
2014-08-02 03:57:24 +00:00
|
|
|
b = r;
|
2014-07-19 08:55:34 +00:00
|
|
|
r = visit_core(r);
|
|
|
|
} else {
|
|
|
|
r = visit_core(e);
|
|
|
|
}
|
2014-06-25 15:30:09 +00:00
|
|
|
if (!is_lambda(r)) {
|
|
|
|
tag g = e.get_tag();
|
|
|
|
expr r_type = whnf(infer_type(r));
|
|
|
|
expr imp_arg;
|
|
|
|
while (is_pi(r_type) && binding_info(r_type).is_implicit()) {
|
2014-07-04 22:45:50 +00:00
|
|
|
imp_arg = mk_placeholder_meta(some_expr(binding_domain(r_type)), g);
|
2014-06-25 15:30:09 +00:00
|
|
|
r = mk_app(r, imp_arg, g);
|
|
|
|
r_type = whnf(instantiate(binding_body(r_type), imp_arg));
|
|
|
|
}
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-07 02:35:26 +00:00
|
|
|
save_info_data(b, r);
|
2014-06-24 21:55:06 +00:00
|
|
|
return r;
|
|
|
|
}
|
2014-06-25 15:30:09 +00:00
|
|
|
|
|
|
|
lazy_list<substitution> solve() {
|
2014-06-26 20:35:36 +00:00
|
|
|
consume_tc_cnstrs();
|
2014-06-25 15:30:09 +00:00
|
|
|
buffer<constraint> cs;
|
|
|
|
cs.append(m_constraints);
|
|
|
|
m_constraints.clear();
|
2014-08-13 01:43:56 +00:00
|
|
|
return unify(env(), cs.size(), cs.data(), m_ngen.mk_child(), true, ios().get_options());
|
2014-06-25 15:30:09 +00:00
|
|
|
}
|
|
|
|
|
2014-07-14 01:53:02 +00:00
|
|
|
static void collect_metavars(expr const & e, buffer<expr> & mvars) {
|
2014-06-29 16:47:25 +00:00
|
|
|
for_each(e, [&](expr const & e, unsigned) {
|
2014-07-02 03:43:53 +00:00
|
|
|
if (is_metavar(e)) {
|
|
|
|
mvars.push_back(e);
|
|
|
|
return false; /* do not visit its type */
|
|
|
|
}
|
2014-06-29 16:47:25 +00:00
|
|
|
return has_metavar(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-03 00:32:13 +00:00
|
|
|
void display_unsolved_proof_state(expr const & mvar, proof_state const & ps, char const * msg) {
|
|
|
|
lean_assert(is_metavar(mvar));
|
|
|
|
if (!m_displayed_errors.contains(mlocal_name(mvar))) {
|
|
|
|
m_displayed_errors.insert(mlocal_name(mvar));
|
2014-08-13 01:43:56 +00:00
|
|
|
auto out = regular(env(), ios());
|
2014-08-01 00:41:39 +00:00
|
|
|
flycheck_error err(out);
|
2014-08-13 01:43:56 +00:00
|
|
|
display_error_pos(out, pip(), mvar);
|
2014-07-31 19:45:54 +00:00
|
|
|
out << " unsolved placeholder, " << msg << "\n" << ps << endl;
|
2014-07-03 00:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 01:58:32 +00:00
|
|
|
// For each occurrence of \c exact_tac in \c pre_tac, display its unassigned metavariables.
|
|
|
|
// This is a trick to improve the quality of the error messages.
|
|
|
|
void check_exact_tacs(expr const & pre_tac, substitution const & s) {
|
|
|
|
for_each(pre_tac, [&](expr const & e, unsigned) {
|
|
|
|
expr const & f = get_app_fn(e);
|
2014-07-03 03:45:10 +00:00
|
|
|
if (is_constant(f) && const_name(f) == const_name(get_exact_tac_fn())) {
|
2014-07-03 01:58:32 +00:00
|
|
|
display_unassigned_mvars(e, s);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-03 00:32:13 +00:00
|
|
|
optional<expr> get_pre_tactic_for(substitution & subst, expr const & mvar, name_set & visited) {
|
2014-07-08 21:28:33 +00:00
|
|
|
if (auto it = m_local_tactic_hints.find(mlocal_name(mvar))) {
|
2014-07-03 00:32:13 +00:00
|
|
|
expr pre_tac = subst.instantiate(*it);
|
|
|
|
pre_tac = solve_unassigned_mvars(subst, pre_tac, visited);
|
2014-07-03 01:58:32 +00:00
|
|
|
check_exact_tacs(pre_tac, subst);
|
2014-07-03 00:32:13 +00:00
|
|
|
return some_expr(pre_tac);
|
2014-06-29 16:47:25 +00:00
|
|
|
} else {
|
2014-07-03 00:32:13 +00:00
|
|
|
return none_expr();
|
2014-06-29 16:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 00:32:13 +00:00
|
|
|
optional<tactic> pre_tactic_to_tactic(expr const & pre_tac, expr const & mvar) {
|
|
|
|
try {
|
2014-08-13 01:43:56 +00:00
|
|
|
return optional<tactic>(expr_to_tactic(env(), pre_tac, pip()));
|
2014-07-03 00:32:13 +00:00
|
|
|
} catch (expr_to_tactic_exception & ex) {
|
2014-08-13 01:43:56 +00:00
|
|
|
auto out = regular(env(), ios());
|
|
|
|
display_error_pos(out, pip(), mvar);
|
2014-07-03 00:32:13 +00:00
|
|
|
out << " " << ex.what();
|
2014-07-10 17:32:00 +00:00
|
|
|
out << pp_indent_expr(out.get_formatter(), pre_tac) << endl << "failed at:"
|
|
|
|
<< pp_indent_expr(out.get_formatter(), ex.get_expr()) << endl;
|
2014-07-03 00:32:13 +00:00
|
|
|
return optional<tactic>();
|
2014-07-02 23:26:06 +00:00
|
|
|
}
|
2014-06-29 16:47:25 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 21:28:33 +00:00
|
|
|
optional<tactic> get_local_tactic_hint(substitution & subst, expr const & mvar, name_set & visited) {
|
|
|
|
if (auto pre_tac = get_pre_tactic_for(subst, mvar, visited)) {
|
|
|
|
return pre_tactic_to_tactic(*pre_tac, mvar);
|
|
|
|
} else {
|
|
|
|
return optional<tactic>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Try to instantiate meta-variable \c mvar (modulo its state ps) using the given tactic.
|
|
|
|
If it succeeds, then update subst with the solution.
|
|
|
|
Return true iff the metavariable \c mvar has been assigned.
|
|
|
|
*/
|
2014-07-14 01:53:02 +00:00
|
|
|
bool try_using(substitution & subst, expr const & mvar, proof_state const & ps, tactic const & tac) {
|
2014-07-08 21:28:33 +00:00
|
|
|
lean_assert(length(ps.get_goals()) == 1);
|
2014-07-14 01:53:02 +00:00
|
|
|
// make sure ps is a really a proof state for mvar.
|
|
|
|
lean_assert(mlocal_name(get_app_fn(head(ps.get_goals()).get_meta())) == mlocal_name(mvar));
|
2014-07-08 21:28:33 +00:00
|
|
|
try {
|
2014-08-13 01:43:56 +00:00
|
|
|
proof_state_seq seq = tac(env(), ios(), ps);
|
2014-07-08 21:28:33 +00:00
|
|
|
auto r = seq.pull();
|
|
|
|
if (!r) {
|
|
|
|
// tactic failed to produce any result
|
2014-07-14 01:53:02 +00:00
|
|
|
display_unsolved_proof_state(mvar, ps, "tactic failed");
|
2014-07-08 21:28:33 +00:00
|
|
|
return false;
|
|
|
|
} else if (!empty(r->first.get_goals())) {
|
|
|
|
// tactic contains unsolved subgoals
|
2014-07-14 01:53:02 +00:00
|
|
|
display_unsolved_proof_state(mvar, r->first, "unsolved subgoals");
|
2014-07-08 21:28:33 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
subst = r->first.get_subst();
|
|
|
|
expr v = subst.instantiate(mvar);
|
2014-07-23 15:51:24 +00:00
|
|
|
subst.assign(mlocal_name(mvar), v);
|
2014-07-08 21:28:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (tactic_exception & ex) {
|
2014-08-13 01:43:56 +00:00
|
|
|
auto out = regular(env(), ios());
|
|
|
|
display_error_pos(out, pip(), ex.get_expr());
|
2014-07-14 01:53:02 +00:00
|
|
|
out << " tactic failed: " << ex.what() << "\n";
|
2014-07-08 21:28:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 00:32:13 +00:00
|
|
|
void solve_unassigned_mvar(substitution & subst, expr mvar, name_set & visited) {
|
|
|
|
if (visited.contains(mlocal_name(mvar)))
|
|
|
|
return;
|
|
|
|
visited.insert(mlocal_name(mvar));
|
2014-07-08 21:28:33 +00:00
|
|
|
if (auto local_hint = get_local_tactic_hint(subst, mvar, visited)) {
|
2014-07-14 01:53:02 +00:00
|
|
|
auto meta = mvar_to_meta(mvar);
|
|
|
|
if (!meta)
|
|
|
|
return;
|
|
|
|
meta = instantiate_meta(*meta, subst);
|
2014-07-27 19:01:06 +00:00
|
|
|
expr type = m_tc[m_relax_main_opaque]->infer(*meta);
|
2014-07-14 01:53:02 +00:00
|
|
|
// first solve unassigned metavariables in type
|
|
|
|
type = solve_unassigned_mvars(subst, type, visited);
|
|
|
|
proof_state ps(goals(goal(*meta, type)), subst, m_ngen.mk_child());
|
|
|
|
try_using(subst, mvar, ps, *local_hint);
|
2014-07-03 00:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 22:02:33 +00:00
|
|
|
expr solve_unassigned_mvars(substitution & subst, expr e, name_set & visited) {
|
|
|
|
e = subst.instantiate(e);
|
2014-06-29 16:47:25 +00:00
|
|
|
buffer<expr> mvars;
|
|
|
|
collect_metavars(e, mvars);
|
2014-07-25 22:02:33 +00:00
|
|
|
if (mvars.empty())
|
|
|
|
return e;
|
2014-06-29 16:47:25 +00:00
|
|
|
for (auto mvar : mvars) {
|
2014-07-03 00:32:13 +00:00
|
|
|
check_interrupted();
|
|
|
|
solve_unassigned_mvar(subst, mvar, visited);
|
2014-06-29 16:47:25 +00:00
|
|
|
}
|
2014-07-02 22:39:25 +00:00
|
|
|
return subst.instantiate(e);
|
2014-06-29 16:47:25 +00:00
|
|
|
}
|
|
|
|
|
2014-07-03 00:32:13 +00:00
|
|
|
expr solve_unassigned_mvars(substitution & subst, expr const & e) {
|
|
|
|
name_set visited;
|
|
|
|
return solve_unassigned_mvars(subst, e, visited);
|
|
|
|
}
|
|
|
|
|
2014-07-03 01:07:11 +00:00
|
|
|
void display_unassigned_mvars(expr const & e, substitution const & s) {
|
2014-08-13 01:43:56 +00:00
|
|
|
if (check_unassigned() && has_metavar(e)) {
|
2014-07-23 15:51:24 +00:00
|
|
|
substitution tmp_s(s);
|
2014-07-02 23:26:06 +00:00
|
|
|
for_each(e, [&](expr const & e, unsigned) {
|
|
|
|
if (!is_metavar(e))
|
|
|
|
return has_metavar(e);
|
|
|
|
if (auto it = m_mvar2meta.find(mlocal_name(e))) {
|
2014-07-23 15:51:24 +00:00
|
|
|
expr meta = tmp_s.instantiate(*it);
|
2014-08-13 01:43:56 +00:00
|
|
|
expr meta_type = tmp_s.instantiate(type_checker(env()).infer(meta));
|
2014-07-02 23:26:06 +00:00
|
|
|
goal g(meta, meta_type);
|
|
|
|
display_unsolved_proof_state(e, proof_state(goals(g), substitution(), m_ngen),
|
|
|
|
"don't know how to synthesize it");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-29 16:47:25 +00:00
|
|
|
/** \brief Apply substitution and solve remaining metavariables using tactics. */
|
2014-07-06 23:46:34 +00:00
|
|
|
expr apply(substitution & s, expr const & e, name_set & univ_params, buffer<name> & new_params) {
|
2014-07-23 15:51:24 +00:00
|
|
|
expr r = s.instantiate(e);
|
2014-07-06 23:46:34 +00:00
|
|
|
if (has_univ_metavar(r))
|
2014-08-13 01:43:56 +00:00
|
|
|
r = univ_metavars_to_params_fn(env(), lls(), s, univ_params, new_params)(r);
|
2014-07-02 23:26:06 +00:00
|
|
|
r = solve_unassigned_mvars(s, r);
|
2014-07-03 01:07:11 +00:00
|
|
|
display_unassigned_mvars(r, s);
|
2014-07-02 23:26:06 +00:00
|
|
|
return r;
|
2014-06-29 16:47:25 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 23:46:34 +00:00
|
|
|
std::tuple<expr, level_param_names> apply(substitution & s, expr const & e) {
|
|
|
|
auto ps = collect_univ_params(e);
|
|
|
|
buffer<name> new_ps;
|
|
|
|
expr r = apply(s, e, ps, new_ps);
|
|
|
|
return std::make_tuple(r, to_list(new_ps.begin(), new_ps.end()));
|
|
|
|
}
|
|
|
|
|
2014-08-07 02:35:26 +00:00
|
|
|
void copy_info_to_manager(substitution s) {
|
2014-08-13 01:43:56 +00:00
|
|
|
if (!infom())
|
2014-08-07 02:35:26 +00:00
|
|
|
return;
|
2014-08-07 04:56:57 +00:00
|
|
|
for (auto & p : m_pre_info_data)
|
2014-08-14 17:37:24 +00:00
|
|
|
p->instantiate(s);
|
|
|
|
// TODO(Leo): implement smarter append
|
|
|
|
infom()->append(std::move(m_pre_info_data), false);
|
|
|
|
m_pre_info_data.clear();
|
2014-08-01 02:54:21 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 19:01:06 +00:00
|
|
|
std::tuple<expr, level_param_names> operator()(expr const & e, bool _ensure_type, bool relax_main_opaque) {
|
2014-08-13 01:43:56 +00:00
|
|
|
flet<bool> set_relax(m_relax_main_opaque, relax_main_opaque && !get_hide_main_opaque(env()));
|
2014-06-25 15:30:09 +00:00
|
|
|
expr r = visit(e);
|
2014-07-07 04:54:16 +00:00
|
|
|
if (_ensure_type)
|
|
|
|
r = ensure_type(r);
|
2014-06-25 15:30:09 +00:00
|
|
|
auto p = solve().pull();
|
|
|
|
lean_assert(p);
|
|
|
|
substitution s = p->first;
|
2014-08-01 02:54:21 +00:00
|
|
|
auto result = apply(s, r);
|
2014-08-07 02:35:26 +00:00
|
|
|
copy_info_to_manager(s);
|
2014-08-01 02:54:21 +00:00
|
|
|
return result;
|
2014-06-25 15:30:09 +00:00
|
|
|
}
|
|
|
|
|
2014-07-27 19:01:06 +00:00
|
|
|
std::tuple<expr, expr, level_param_names> operator()(expr const & t, expr const & v, name const & n, bool is_opaque) {
|
2014-07-14 04:04:01 +00:00
|
|
|
lean_assert(!has_local(t)); lean_assert(!has_local(v));
|
2014-07-07 04:54:16 +00:00
|
|
|
expr r_t = ensure_type(visit(t));
|
2014-07-27 19:01:06 +00:00
|
|
|
// Opaque definitions in the main module may treat other opaque definitions (in the main module) as transparent.
|
2014-08-13 01:43:56 +00:00
|
|
|
flet<bool> set_relax(m_relax_main_opaque, is_opaque && !get_hide_main_opaque(env()));
|
2014-06-25 15:30:09 +00:00
|
|
|
expr r_v = visit(v);
|
|
|
|
expr r_v_type = infer_type(r_v);
|
2014-07-29 21:24:12 +00:00
|
|
|
justification j = mk_justification(r_v, [=](formatter const & fmt, substitution const & subst) {
|
2014-07-23 15:51:24 +00:00
|
|
|
substitution s(subst);
|
|
|
|
return pp_def_type_mismatch(fmt, n, s.instantiate(r_t), s.instantiate(r_v_type));
|
2014-06-25 15:30:09 +00:00
|
|
|
});
|
2014-07-29 20:55:39 +00:00
|
|
|
r_v = ensure_type(r_v, r_v_type, r_t, j, is_opaque);
|
2014-06-25 15:30:09 +00:00
|
|
|
auto p = solve().pull();
|
|
|
|
lean_assert(p);
|
|
|
|
substitution s = p->first;
|
2014-07-06 23:46:34 +00:00
|
|
|
name_set univ_params = collect_univ_params(r_v, collect_univ_params(r_t));
|
|
|
|
buffer<name> new_params;
|
|
|
|
expr new_r_t = apply(s, r_t, univ_params, new_params);
|
|
|
|
expr new_r_v = apply(s, r_v, univ_params, new_params);
|
2014-08-07 02:35:26 +00:00
|
|
|
copy_info_to_manager(s);
|
2014-07-06 23:46:34 +00:00
|
|
|
return std::make_tuple(new_r_t, new_r_v, to_list(new_params.begin(), new_params.end()));
|
2014-06-25 15:30:09 +00:00
|
|
|
}
|
2014-06-24 21:55:06 +00:00
|
|
|
};
|
2014-06-25 15:30:09 +00:00
|
|
|
|
|
|
|
static name g_tmp_prefix = name::mk_internal_unique_name();
|
|
|
|
|
2014-08-13 19:45:57 +00:00
|
|
|
std::tuple<expr, level_param_names> elaborate(elaborator_context & env, list<expr> const & ctx, expr const & e,
|
2014-08-13 01:43:56 +00:00
|
|
|
bool relax_main_opaque, bool ensure_type) {
|
|
|
|
return elaborator(env, ctx, name_generator(g_tmp_prefix))(e, ensure_type, relax_main_opaque);
|
2014-06-25 15:30:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 19:45:57 +00:00
|
|
|
std::tuple<expr, expr, level_param_names> elaborate(elaborator_context & env, name const & n, expr const & t, expr const & v,
|
2014-08-13 01:43:56 +00:00
|
|
|
bool is_opaque) {
|
|
|
|
return elaborator(env, list<expr>(), name_generator(g_tmp_prefix))(t, v, n, is_opaque);
|
2014-06-30 07:51:11 +00:00
|
|
|
}
|
2014-06-24 21:55:06 +00:00
|
|
|
}
|