feat(kernel): add telescope abstract procedure

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2014-05-16 11:55:43 -07:00
parent 9a689ab0c3
commit 91a1b62b9e
2 changed files with 39 additions and 12 deletions

View file

@ -11,27 +11,46 @@ Author: Leonardo de Moura
#include "kernel/replace_fn.h"
namespace lean {
expr abstract(expr const & e, unsigned n, expr const * s) {
lean_assert(std::all_of(s, s+n, closed));
expr abstract(expr const & e, unsigned s, unsigned n, expr const * subst) {
lean_assert(std::all_of(subst, subst+n, closed));
return replace(e, [=](expr const & e, unsigned offset) -> optional<expr> {
if (closed(e)) {
unsigned i = n;
while (i > 0) {
--i;
if (s[i] == e)
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();
});
}
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) {
lean_assert(std::all_of(s, s+n, closed));
return replace(e, [=](expr const & e, unsigned offset) -> optional<expr> {
if (closed) {
unsigned i = n;
while (i > 0) {
--i;
if (is_eqp(s[i], e))
return some_expr(mk_var(offset + n - i - 1));
}
}
return none_expr();
});
}

View file

@ -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, 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).