2013-07-24 07:32:01 +00: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-08-06 18:27:14 +00:00
|
|
|
#include "abstract.h"
|
2013-07-24 07:32:01 +00:00
|
|
|
#include "free_vars.h"
|
|
|
|
#include "replace.h"
|
|
|
|
|
|
|
|
namespace lean {
|
2013-08-03 19:20:01 +00:00
|
|
|
expr abstract(expr const & e, unsigned n, expr const * s) {
|
2013-07-24 07:32:01 +00:00
|
|
|
lean_assert(std::all_of(s, s+n, closed));
|
|
|
|
|
2013-07-24 07:50:58 +00:00
|
|
|
auto f = [=](expr const & e, unsigned offset) -> expr {
|
2013-07-24 07:32:01 +00:00
|
|
|
unsigned i = n;
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
if (s[i] == e)
|
2013-08-06 18:27:14 +00:00
|
|
|
return mk_var(offset + n - i - 1);
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
};
|
|
|
|
|
2013-07-24 07:45:38 +00:00
|
|
|
return replace_fn<decltype(f)>(f)(e);
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|
2013-08-03 19:20:01 +00:00
|
|
|
expr abstract_p(expr const & e, unsigned n, expr const * s) {
|
2013-07-24 07:32:01 +00:00
|
|
|
lean_assert(std::all_of(s, s+n, closed));
|
|
|
|
|
2013-07-24 07:50:58 +00:00
|
|
|
auto f = [=](expr const & e, unsigned offset) -> expr {
|
2013-07-24 07:32:01 +00:00
|
|
|
unsigned i = n;
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
2013-08-04 16:41:49 +00:00
|
|
|
if (is_eqp(s[i], e))
|
2013-08-06 18:27:14 +00:00
|
|
|
return mk_var(offset + n - i - 1);
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
};
|
|
|
|
|
2013-07-24 07:45:38 +00:00
|
|
|
return replace_fn<decltype(f)>(f)(e);
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|
2013-08-15 05:30:12 +00: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 18:27:14 +00:00
|
|
|
}
|
2013-08-15 05:30:12 +00:00
|
|
|
|
|
|
|
MkBinder(Fun);
|
|
|
|
MkBinder(Pi);
|
|
|
|
MkBinder(Let);
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|