2014-02-22 01:55:59 +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-02-22 16:41:45 +00:00
|
|
|
#include <utility>
|
|
|
|
#include "kernel/metavar.h"
|
2014-02-22 19:44:57 +00:00
|
|
|
#include "kernel/free_vars.h"
|
|
|
|
#include "kernel/replace_visitor.h"
|
|
|
|
#include "kernel/justification.h"
|
|
|
|
#include "kernel/instantiate.h"
|
2014-02-23 00:12:06 +00:00
|
|
|
#include "kernel/find_fn.h"
|
2014-02-25 02:31:38 +00:00
|
|
|
#include "kernel/level.h"
|
2014-02-22 01:55:59 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-04-29 17:22:36 +00:00
|
|
|
substitution::substitution() {}
|
|
|
|
|
2014-02-25 02:31:38 +00:00
|
|
|
bool substitution::is_expr_assigned(name const & m) const {
|
|
|
|
return m_expr_subst.contains(m);
|
2014-02-22 16:41:45 +00:00
|
|
|
}
|
|
|
|
|
2014-07-03 21:01:22 +00:00
|
|
|
auto substitution::get_expr_assignment(name const & m) const -> opt_expr_jst {
|
2014-02-25 02:31:38 +00:00
|
|
|
auto it = m_expr_subst.find(m);
|
2014-02-22 16:41:45 +00:00
|
|
|
if (it)
|
2014-07-03 21:01:22 +00:00
|
|
|
return opt_expr_jst(mk_pair(*it, get_expr_jst(m)));
|
2014-02-22 16:41:45 +00:00
|
|
|
else
|
2014-07-03 21:01:22 +00:00
|
|
|
return opt_expr_jst();
|
2014-02-22 16:41:45 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 02:31:38 +00:00
|
|
|
bool substitution::is_level_assigned(name const & m) const {
|
|
|
|
return m_level_subst.contains(m);
|
|
|
|
}
|
|
|
|
|
2014-07-03 21:01:22 +00:00
|
|
|
auto substitution::get_level_assignment(name const & m) const -> opt_level_jst {
|
2014-02-25 02:31:38 +00:00
|
|
|
auto it = m_level_subst.find(m);
|
|
|
|
if (it)
|
2014-07-03 21:01:22 +00:00
|
|
|
return opt_level_jst(mk_pair(*it, get_level_jst(m)));
|
2014-02-25 02:31:38 +00:00
|
|
|
else
|
|
|
|
return opt_level_jst();
|
|
|
|
}
|
|
|
|
|
2014-02-22 16:41:45 +00:00
|
|
|
optional<expr> substitution::get_expr(name const & m) const {
|
2014-02-25 02:31:38 +00:00
|
|
|
auto it = m_expr_subst.find(m);
|
2014-07-03 21:01:22 +00:00
|
|
|
return it ? some_expr(*it) : none_expr();
|
2014-02-22 16:41:45 +00:00
|
|
|
}
|
2014-02-22 01:55:59 +00:00
|
|
|
|
2014-02-25 02:31:38 +00:00
|
|
|
optional<level> substitution::get_level(name const & m) const {
|
|
|
|
auto it = m_level_subst.find(m);
|
2014-07-03 21:01:22 +00:00
|
|
|
return it ? some_level(*it) : none_level();
|
2014-02-25 02:31:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 15:51:24 +00:00
|
|
|
void substitution::assign(name const & m, expr const & t, justification const & j) {
|
2014-02-22 19:44:57 +00:00
|
|
|
lean_assert(closed(t));
|
2014-07-03 21:01:22 +00:00
|
|
|
m_expr_subst.insert(m, t);
|
|
|
|
if (!j.is_none())
|
|
|
|
m_expr_jsts.insert(m, j);
|
2014-02-22 16:41:45 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 15:51:24 +00:00
|
|
|
void substitution::assign(name const & m, level const & l, justification const & j) {
|
2014-07-03 21:01:22 +00:00
|
|
|
m_level_subst.insert(m, l);
|
|
|
|
if (!j.is_none())
|
|
|
|
m_level_jsts.insert(m, j);
|
2014-02-25 02:31:38 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 15:51:24 +00:00
|
|
|
std::pair<level, justification> substitution::instantiate_metavars(level const & l, bool use_jst) {
|
2014-05-01 22:28:34 +00:00
|
|
|
if (!has_meta(l))
|
2014-02-25 02:31:38 +00:00
|
|
|
return mk_pair(l, justification());
|
|
|
|
justification j;
|
|
|
|
auto save_jst = [&](justification const & j2) { j = mk_composite1(j, j2); };
|
|
|
|
level r = replace(l, [&](level const & l) {
|
|
|
|
if (!has_meta(l)) {
|
|
|
|
return some_level(l);
|
|
|
|
} else if (is_meta(l)) {
|
2014-04-29 17:22:36 +00:00
|
|
|
auto p1 = get_assignment(l);
|
2014-02-25 02:31:38 +00:00
|
|
|
if (p1) {
|
2014-07-23 15:51:24 +00:00
|
|
|
auto p2 = instantiate_metavars(p1->first, use_jst);
|
2014-02-25 02:31:38 +00:00
|
|
|
if (use_jst) {
|
|
|
|
justification new_jst = mk_composite1(p1->second, p2.second);
|
2014-07-23 15:51:24 +00:00
|
|
|
assign(meta_id(l), p2.first, new_jst);
|
2014-02-25 02:31:38 +00:00
|
|
|
save_jst(new_jst);
|
2014-07-23 15:51:24 +00:00
|
|
|
} else {
|
|
|
|
assign(meta_id(l), p2.first);
|
2014-02-25 02:31:38 +00:00
|
|
|
}
|
|
|
|
return some_level(p2.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none_level();
|
|
|
|
});
|
|
|
|
return mk_pair(r, j);
|
|
|
|
}
|
|
|
|
|
2014-02-22 19:44:57 +00:00
|
|
|
class instantiate_metavars_fn : public replace_visitor {
|
|
|
|
protected:
|
|
|
|
substitution & m_subst;
|
|
|
|
justification m_jst;
|
|
|
|
bool m_use_jst;
|
|
|
|
|
|
|
|
void save_jst(justification const & j) { m_jst = mk_composite1(m_jst, j); }
|
|
|
|
|
2014-02-25 02:31:38 +00:00
|
|
|
level visit_level(level const & l) {
|
2014-07-23 15:51:24 +00:00
|
|
|
auto p1 = m_subst.instantiate_metavars(l, m_use_jst);
|
2014-02-25 02:31:38 +00:00
|
|
|
if (m_use_jst)
|
|
|
|
save_jst(p1.second);
|
|
|
|
return p1.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
levels visit_levels(levels const & ls) {
|
|
|
|
return map_reuse(ls, [&](level const & l) { return visit_level(l); }, [](level const & l1, level const & l2) { return is_eqp(l1, l2); });
|
|
|
|
}
|
|
|
|
|
2014-05-14 22:53:09 +00:00
|
|
|
virtual expr visit_sort(expr const & s) {
|
2014-02-25 02:31:38 +00:00
|
|
|
return update_sort(s, visit_level(sort_level(s)));
|
|
|
|
}
|
|
|
|
|
2014-05-14 22:53:09 +00:00
|
|
|
virtual expr visit_constant(expr const & c) {
|
2014-05-16 18:13:50 +00:00
|
|
|
return update_constant(c, visit_levels(const_levels(c)));
|
2014-02-25 02:31:38 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 22:53:09 +00:00
|
|
|
virtual expr visit_meta(expr const & m) {
|
2014-02-22 19:44:57 +00:00
|
|
|
name const & m_name = mlocal_name(m);
|
2014-02-25 02:31:38 +00:00
|
|
|
auto p1 = m_subst.get_expr_assignment(m_name);
|
2014-02-22 19:44:57 +00:00
|
|
|
if (p1) {
|
|
|
|
if (!has_metavar(p1->first)) {
|
|
|
|
if (m_use_jst)
|
|
|
|
save_jst(p1->second);
|
|
|
|
return p1->first;
|
|
|
|
} else if (m_use_jst) {
|
2014-07-23 15:51:24 +00:00
|
|
|
auto p2 = m_subst.instantiate_metavars(p1->first);
|
|
|
|
justification new_jst = mk_composite1(p1->second, p2.second);
|
|
|
|
m_subst.assign(m_name, p2.first, new_jst);
|
|
|
|
save_jst(new_jst);
|
|
|
|
return p2.first;
|
2014-02-22 19:44:57 +00:00
|
|
|
} else {
|
2014-07-23 15:51:24 +00:00
|
|
|
auto p2 = m_subst.instantiate_metavars(p1->first);
|
|
|
|
m_subst.assign(m_name, p2.first, mk_composite1(p1->second, p2.second));
|
|
|
|
return p2.first;
|
2014-02-22 19:44:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 22:53:09 +00:00
|
|
|
virtual expr visit_app(expr const & e) {
|
2014-02-22 19:44:57 +00:00
|
|
|
buffer<expr> args;
|
|
|
|
expr const * it = &e;
|
|
|
|
while (is_app(*it)) {
|
2014-05-14 22:53:09 +00:00
|
|
|
args.push_back(visit(app_arg(*it)));
|
2014-02-22 19:44:57 +00:00
|
|
|
it = &app_fn(*it);
|
|
|
|
}
|
|
|
|
expr const & f = *it;
|
2014-02-25 02:31:38 +00:00
|
|
|
if (is_metavar(f) && m_subst.is_expr_assigned(mlocal_name(f))) {
|
2014-05-14 22:53:09 +00:00
|
|
|
expr new_f = visit_meta(f);
|
2014-02-22 19:44:57 +00:00
|
|
|
return apply_beta(new_f, args.size(), args.data());
|
|
|
|
} else {
|
2014-05-14 22:53:09 +00:00
|
|
|
args.push_back(visit(f));
|
2014-02-22 19:44:57 +00:00
|
|
|
return update_rev_app(e, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 22:53:09 +00:00
|
|
|
virtual expr visit(expr const & e) {
|
2014-07-23 15:51:24 +00:00
|
|
|
if (!has_metavar(e)) {
|
2014-02-25 02:31:38 +00:00
|
|
|
return e;
|
|
|
|
} else {
|
2014-05-14 22:53:09 +00:00
|
|
|
return replace_visitor::visit(e);
|
2014-02-25 02:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 19:44:57 +00:00
|
|
|
public:
|
2014-07-23 15:51:24 +00:00
|
|
|
instantiate_metavars_fn(substitution & s, bool use_jst):
|
|
|
|
m_subst(s), m_use_jst(use_jst) {}
|
2014-02-22 19:44:57 +00:00
|
|
|
justification const & get_justification() const { return m_jst; }
|
|
|
|
};
|
|
|
|
|
2014-07-23 15:51:24 +00:00
|
|
|
std::pair<expr, justification> substitution::instantiate_metavars(expr const & e) {
|
|
|
|
if (!has_metavar(e)) {
|
2014-07-12 04:54:05 +00:00
|
|
|
return mk_pair(e, justification());
|
|
|
|
} else {
|
2014-07-23 15:51:24 +00:00
|
|
|
instantiate_metavars_fn fn(*this, true);
|
2014-07-12 04:54:05 +00:00
|
|
|
expr r = fn(e);
|
|
|
|
return mk_pair(r, fn.get_justification());
|
|
|
|
}
|
2014-02-22 19:44:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 15:51:24 +00:00
|
|
|
expr substitution::instantiate_metavars_wo_jst(expr const & e) {
|
|
|
|
return instantiate_metavars_fn(*this, false)(e);
|
2014-04-25 00:44:43 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 20:12:29 +00:00
|
|
|
|
|
|
|
bool substitution::occurs_expr_core(name const & m, expr const & e, name_set & visited) const {
|
2014-07-15 00:57:17 +00:00
|
|
|
bool found = false;
|
2014-07-23 20:12:29 +00:00
|
|
|
for_each(e, [&](expr const & e, unsigned) {
|
2014-07-15 00:57:17 +00:00
|
|
|
if (found || !has_expr_metavar(e)) return false;
|
2014-02-23 00:12:06 +00:00
|
|
|
if (is_metavar(e)) {
|
2014-07-23 20:12:29 +00:00
|
|
|
name const & n = mlocal_name(e);
|
|
|
|
if (n == m)
|
|
|
|
found = true;
|
|
|
|
auto s = get_expr(e);
|
|
|
|
if (!s || visited.contains(n))
|
|
|
|
return false; // do not visit type
|
|
|
|
visited.insert(n);
|
|
|
|
if (s && occurs_expr_core(m, *s, visited))
|
2014-07-15 00:57:17 +00:00
|
|
|
found = true;
|
|
|
|
return false; // do not visit type
|
2014-02-23 00:12:06 +00:00
|
|
|
}
|
2014-07-15 00:57:17 +00:00
|
|
|
if (is_local(e)) return false; // do not visit type
|
|
|
|
return true;
|
2014-02-23 00:12:06 +00:00
|
|
|
});
|
2014-07-15 00:57:17 +00:00
|
|
|
return found;
|
2014-02-22 19:44:57 +00:00
|
|
|
}
|
2014-07-23 20:12:29 +00:00
|
|
|
|
|
|
|
bool substitution::occurs_expr(name const & m, expr const & e) const {
|
|
|
|
if (!has_expr_metavar(e))
|
|
|
|
return false;
|
|
|
|
name_set visited;
|
|
|
|
return occurs_expr_core(m, e, visited);
|
|
|
|
}
|
2014-02-22 01:55:59 +00:00
|
|
|
}
|