2015-11-05 23:29:30 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2015-11-05 23:59:40 +00:00
|
|
|
#include <algorithm>
|
2015-11-05 23:29:30 +00:00
|
|
|
#include "kernel/for_each_fn.h"
|
|
|
|
#include "kernel/instantiate.h"
|
2015-11-06 04:58:19 +00:00
|
|
|
#include "kernel/abstract.h"
|
|
|
|
#include "library/replace_visitor.h"
|
2015-11-05 23:29:30 +00:00
|
|
|
#include "library/fun_info_manager.h"
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
fun_info_manager::fun_info_manager(type_context & ctx):
|
|
|
|
m_ctx(ctx) {
|
|
|
|
}
|
|
|
|
|
2015-11-07 19:10:38 +00:00
|
|
|
list<unsigned> fun_info_manager::collect_deps(expr const & type, buffer<expr> const & locals) {
|
2015-11-07 19:02:32 +00:00
|
|
|
buffer<unsigned> deps;
|
2015-11-07 19:10:38 +00:00
|
|
|
for_each(type, [&](expr const & e, unsigned) {
|
2015-11-07 19:02:32 +00:00
|
|
|
if (m_ctx.is_tmp_local(e)) {
|
|
|
|
unsigned idx;
|
|
|
|
for (idx = 0; idx < locals.size(); idx++)
|
|
|
|
if (locals[idx] == e)
|
|
|
|
break;
|
|
|
|
if (idx < locals.size() && std::find(deps.begin(), deps.end(), idx) == deps.end())
|
|
|
|
deps.push_back(idx);
|
|
|
|
}
|
|
|
|
return has_local(e); // continue the search only if e has locals
|
|
|
|
});
|
|
|
|
std::sort(deps.begin(), deps.end());
|
|
|
|
return to_list(deps);
|
|
|
|
}
|
|
|
|
|
2016-01-07 01:28:52 +00:00
|
|
|
fun_info fun_info_manager::get(expr const & e) {
|
2015-11-05 23:29:30 +00:00
|
|
|
if (auto r = m_fun_info.find(e))
|
|
|
|
return *r;
|
2015-12-06 03:26:06 +00:00
|
|
|
expr type = m_ctx.relaxed_try_to_pi(m_ctx.infer(e));
|
2015-11-06 04:58:19 +00:00
|
|
|
buffer<param_info> info;
|
2015-11-05 23:29:30 +00:00
|
|
|
buffer<expr> locals;
|
|
|
|
while (is_pi(type)) {
|
|
|
|
expr local = m_ctx.mk_tmp_local_from_binding(type);
|
|
|
|
expr local_type = m_ctx.infer(local);
|
2015-12-06 03:26:06 +00:00
|
|
|
expr new_type = m_ctx.relaxed_try_to_pi(instantiate(binding_body(type), local));
|
2016-01-07 01:28:52 +00:00
|
|
|
bool spec = false;
|
2015-11-05 23:29:30 +00:00
|
|
|
bool is_prop = m_ctx.is_prop(local_type);
|
|
|
|
bool is_sub = is_prop;
|
2015-11-07 19:02:32 +00:00
|
|
|
bool is_dep = !closed(binding_body(type));
|
2015-11-07 19:10:38 +00:00
|
|
|
if (!is_sub) {
|
|
|
|
// TODO(Leo): check if the following line is a performance bottleneck.
|
2015-11-05 23:29:30 +00:00
|
|
|
is_sub = static_cast<bool>(m_ctx.mk_subsingleton_instance(local_type));
|
2015-11-07 19:10:38 +00:00
|
|
|
}
|
2016-01-07 01:28:52 +00:00
|
|
|
info.emplace_back(spec,
|
|
|
|
binding_info(type).is_implicit(),
|
2015-11-05 23:29:30 +00:00
|
|
|
binding_info(type).is_inst_implicit(),
|
2015-11-07 19:02:32 +00:00
|
|
|
is_prop, is_sub, is_dep, collect_deps(local_type, locals));
|
2015-11-07 19:10:38 +00:00
|
|
|
locals.push_back(local);
|
2015-11-07 19:02:32 +00:00
|
|
|
type = new_type;
|
2015-11-05 23:29:30 +00:00
|
|
|
}
|
2015-11-07 19:02:32 +00:00
|
|
|
fun_info r(info.size(), to_list(info), collect_deps(type, locals));
|
2015-11-05 23:29:30 +00:00
|
|
|
m_fun_info.insert(e, r);
|
|
|
|
return r;
|
|
|
|
}
|
2015-11-06 04:58:19 +00:00
|
|
|
|
2016-01-07 01:28:52 +00:00
|
|
|
fun_info fun_info_manager::get(expr const & e, unsigned nargs) {
|
2015-11-07 19:29:36 +00:00
|
|
|
auto r = get(e);
|
|
|
|
lean_assert(nargs <= r.get_arity());
|
|
|
|
if (nargs == r.get_arity()) {
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
buffer<param_info> pinfos;
|
|
|
|
to_buffer(r.get_params_info(), pinfos);
|
|
|
|
buffer<unsigned> rdeps;
|
2016-01-07 01:28:52 +00:00
|
|
|
to_buffer(r.get_result_dependencies(), rdeps);
|
2015-11-07 19:29:36 +00:00
|
|
|
for (unsigned i = nargs; i < pinfos.size(); i++) {
|
|
|
|
for (auto d : pinfos[i].get_dependencies()) {
|
|
|
|
if (std::find(rdeps.begin(), rdeps.end(), d) == rdeps.end())
|
|
|
|
rdeps.push_back(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pinfos.shrink(nargs);
|
|
|
|
return fun_info(nargs, to_list(pinfos), to_list(rdeps));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 01:28:52 +00:00
|
|
|
fun_info fun_info_manager::get_specialization(expr const &) {
|
|
|
|
// TODO(Leo)
|
|
|
|
lean_unreachable();
|
|
|
|
}
|
2015-11-05 23:29:30 +00:00
|
|
|
}
|