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>
|
|
|
|
#include "free_vars.h"
|
|
|
|
#include "replace.h"
|
|
|
|
|
|
|
|
namespace lean {
|
2013-08-03 19:20:01 +00:00
|
|
|
expr instantiate_with_closed(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
|
|
|
if (is_var(e)) {
|
|
|
|
unsigned vidx = var_idx(e);
|
|
|
|
if (vidx >= offset) {
|
|
|
|
if (vidx < offset + n)
|
|
|
|
return s[n - (vidx - offset) - 1];
|
|
|
|
else
|
|
|
|
return var(vidx - n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
};
|
2013-08-03 18:45:51 +00:00
|
|
|
return replace_fn<decltype(f)>(f)(e);
|
|
|
|
}
|
2013-08-03 19:20:01 +00:00
|
|
|
expr instantiate(expr const & e, unsigned n, expr const * s) {
|
2013-08-03 18:45:51 +00:00
|
|
|
auto f = [=](expr const & e, unsigned offset) -> expr {
|
|
|
|
if (is_var(e)) {
|
|
|
|
unsigned vidx = var_idx(e);
|
|
|
|
if (vidx >= offset) {
|
|
|
|
if (vidx < offset + n)
|
|
|
|
return lift_free_vars(s[n - (vidx - offset) - 1], offset);
|
|
|
|
else
|
|
|
|
return var(vidx - n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|