feat(kernel): add telescope abstract procedure
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
9a689ab0c3
commit
91a1b62b9e
2 changed files with 39 additions and 12 deletions
|
@ -11,26 +11,45 @@ Author: Leonardo de Moura
|
||||||
#include "kernel/replace_fn.h"
|
#include "kernel/replace_fn.h"
|
||||||
|
|
||||||
namespace lean {
|
namespace lean {
|
||||||
expr abstract(expr const & e, unsigned n, expr const * s) {
|
expr abstract(expr const & e, unsigned s, unsigned n, expr const * subst) {
|
||||||
lean_assert(std::all_of(s, s+n, closed));
|
lean_assert(std::all_of(subst, subst+n, closed));
|
||||||
return replace(e, [=](expr const & e, unsigned offset) -> optional<expr> {
|
return replace(e, [=](expr const & e, unsigned offset) -> optional<expr> {
|
||||||
unsigned i = n;
|
if (closed(e)) {
|
||||||
while (i > 0) {
|
unsigned i = n;
|
||||||
--i;
|
while (i > 0) {
|
||||||
if (s[i] == e)
|
--i;
|
||||||
return some_expr(mk_var(offset + n - i - 1));
|
if (subst[i] == e)
|
||||||
|
return some_expr(mk_var(offset + s + n - i - 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return none_expr();
|
return none_expr();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
expr abstract(expr const & e, unsigned n, expr const * subst) { return abstract(e, 0, n, subst); }
|
||||||
|
|
||||||
|
telescope abstract(telescope const & t, expr const & s, unsigned i) {
|
||||||
|
if (is_nil(t)) {
|
||||||
|
return t;
|
||||||
|
} else {
|
||||||
|
binder const & b = head(t);
|
||||||
|
return telescope(b.update_type(abstract(b.get_type(), i, 1, &s)),
|
||||||
|
abstract(head(t), s, i+1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
telescope abstract(telescope const & t, expr const & s) { return abstract(t, s, 0); }
|
||||||
|
|
||||||
|
|
||||||
expr abstract_p(expr const & e, unsigned n, expr const * s) {
|
expr abstract_p(expr const & e, unsigned n, expr const * s) {
|
||||||
lean_assert(std::all_of(s, s+n, closed));
|
lean_assert(std::all_of(s, s+n, closed));
|
||||||
return replace(e, [=](expr const & e, unsigned offset) -> optional<expr> {
|
return replace(e, [=](expr const & e, unsigned offset) -> optional<expr> {
|
||||||
unsigned i = n;
|
if (closed) {
|
||||||
while (i > 0) {
|
unsigned i = n;
|
||||||
--i;
|
while (i > 0) {
|
||||||
if (is_eqp(s[i], e))
|
--i;
|
||||||
return some_expr(mk_var(offset + n - i - 1));
|
if (is_eqp(s[i], e))
|
||||||
|
return some_expr(mk_var(offset + n - i - 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return none_expr();
|
return none_expr();
|
||||||
});
|
});
|
||||||
|
|
|
@ -20,6 +20,14 @@ expr abstract(expr const & e, unsigned n, expr const * s);
|
||||||
inline expr abstract(expr const & e, expr const & s) { return abstract(e, 1, &s); }
|
inline expr abstract(expr const & e, expr const & s) { return abstract(e, 1, &s); }
|
||||||
inline expr abstract(expr const & e, std::initializer_list<expr> const & l) { return abstract(e, l.size(), l.begin()); }
|
inline expr abstract(expr const & e, std::initializer_list<expr> const & l) { return abstract(e, l.size(), l.begin()); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
\brief Replace s with variable #i in the given telescope.
|
||||||
|
\pre s is closed
|
||||||
|
*/
|
||||||
|
telescope abstract(telescope const & t, expr const & s, unsigned i);
|
||||||
|
/** \brief Replace s with variable #0 in the given telescope. */
|
||||||
|
telescope abstract(telescope const & t, expr const & s);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
\brief Replace the expressions s[0], ..., s[n-1] in e with var(n-1), ..., var(0).
|
\brief Replace the expressions s[0], ..., s[n-1] in e with var(n-1), ..., var(0).
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue