2013-07-24 00:32:01 -07:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include <algorithm>
|
2013-09-13 03:35:29 -07:00
|
|
|
#include <utility>
|
|
|
|
#include "kernel/abstract.h"
|
|
|
|
#include "kernel/free_vars.h"
|
2013-12-03 12:40:52 -08:00
|
|
|
#include "kernel/replace_fn.h"
|
2013-07-24 00:32:01 -07:00
|
|
|
|
|
|
|
namespace lean {
|
2014-05-16 11:55:43 -07:00
|
|
|
expr abstract(expr const & e, unsigned s, unsigned n, expr const * subst) {
|
|
|
|
lean_assert(std::all_of(subst, subst+n, closed));
|
2014-02-28 16:57:25 -08:00
|
|
|
return replace(e, [=](expr const & e, unsigned offset) -> optional<expr> {
|
2014-05-16 11:55:43 -07:00
|
|
|
if (closed(e)) {
|
|
|
|
unsigned i = n;
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
if (subst[i] == e)
|
|
|
|
return some_expr(mk_var(offset + s + n - i - 1));
|
|
|
|
}
|
2013-12-17 18:31:59 -08:00
|
|
|
}
|
2014-02-28 16:57:25 -08:00
|
|
|
return none_expr();
|
2013-12-17 18:31:59 -08:00
|
|
|
});
|
2013-07-24 00:32:01 -07:00
|
|
|
}
|
2014-05-16 11:55:43 -07:00
|
|
|
expr abstract(expr const & e, unsigned n, expr const * subst) { return abstract(e, 0, n, subst); }
|
2014-05-16 17:47:04 -07:00
|
|
|
expr abstract(expr const & e, expr const & s, unsigned i) { return abstract(e, i, 1, &s); }
|
2014-05-16 11:55:43 -07:00
|
|
|
|
2013-08-03 12:20:01 -07:00
|
|
|
expr abstract_p(expr const & e, unsigned n, expr const * s) {
|
2013-07-24 00:32:01 -07:00
|
|
|
lean_assert(std::all_of(s, s+n, closed));
|
2014-02-28 16:57:25 -08:00
|
|
|
return replace(e, [=](expr const & e, unsigned offset) -> optional<expr> {
|
2014-05-16 15:39:19 -07:00
|
|
|
if (closed(e)) {
|
2014-05-16 11:55:43 -07:00
|
|
|
unsigned i = n;
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
if (is_eqp(s[i], e))
|
|
|
|
return some_expr(mk_var(offset + n - i - 1));
|
|
|
|
}
|
2013-12-17 18:31:59 -08:00
|
|
|
}
|
2014-02-28 16:57:25 -08:00
|
|
|
return none_expr();
|
2013-12-17 18:31:59 -08:00
|
|
|
});
|
2013-07-24 00:32:01 -07:00
|
|
|
}
|
2013-08-14 22:30:12 -07:00
|
|
|
#define MkBinder(FName) \
|
|
|
|
expr FName(std::initializer_list<std::pair<expr const &, expr const &>> const & l, expr const & b) { \
|
|
|
|
expr r = b; \
|
|
|
|
auto it = l.end(); \
|
|
|
|
while (it != l.begin()) { \
|
|
|
|
--it; \
|
|
|
|
auto const & p = *it; \
|
|
|
|
r = FName(p.first, p.second, r); \
|
|
|
|
} \
|
|
|
|
return r; \
|
2013-08-06 11:27:14 -07:00
|
|
|
}
|
2013-08-14 22:30:12 -07:00
|
|
|
|
|
|
|
MkBinder(Fun);
|
|
|
|
MkBinder(Pi);
|
2013-07-24 00:32:01 -07:00
|
|
|
}
|