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-09-13 03:04:10 +00:00
|
|
|
#include "kernel/free_vars.h"
|
|
|
|
#include "kernel/replace.h"
|
2013-09-13 01:25:38 +00:00
|
|
|
#include "kernel/metavar.h"
|
2013-07-24 07:32:01 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-09-13 01:25:38 +00:00
|
|
|
expr instantiate_with_closed(expr const & a, unsigned n, expr const * s) {
|
2013-07-24 07:32:01 +00:00
|
|
|
lean_assert(std::all_of(s, s+n, closed));
|
|
|
|
|
2013-09-13 01:25:38 +00:00
|
|
|
auto f = [=](expr const & m, unsigned offset) -> expr {
|
|
|
|
if (is_var(m)) {
|
|
|
|
unsigned vidx = var_idx(m);
|
2013-07-24 07:32:01 +00:00
|
|
|
if (vidx >= offset) {
|
|
|
|
if (vidx < offset + n)
|
|
|
|
return s[n - (vidx - offset) - 1];
|
|
|
|
else
|
2013-08-06 18:27:14 +00:00
|
|
|
return mk_var(vidx - n);
|
2013-09-13 01:25:38 +00:00
|
|
|
} else {
|
|
|
|
return m;
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|
2013-09-13 01:25:38 +00:00
|
|
|
} else if (is_metavar(m)) {
|
|
|
|
expr r = m;
|
|
|
|
for (unsigned i = 0; i < n; i++)
|
|
|
|
r = add_subst(r, offset + i, s[n - i - 1]);
|
|
|
|
return add_lower(r, offset + n, n);
|
|
|
|
} else {
|
|
|
|
return m;
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|
|
|
|
};
|
2013-09-13 01:25:38 +00:00
|
|
|
return replace_fn<decltype(f)>(f)(a);
|
2013-08-03 18:45:51 +00:00
|
|
|
}
|
2013-09-13 01:25:38 +00:00
|
|
|
expr instantiate(expr const & a, unsigned n, expr const * s) {
|
|
|
|
auto f = [=](expr const & m, unsigned offset) -> expr {
|
|
|
|
if (is_var(m)) {
|
|
|
|
unsigned vidx = var_idx(m);
|
2013-08-03 18:45:51 +00:00
|
|
|
if (vidx >= offset) {
|
|
|
|
if (vidx < offset + n)
|
|
|
|
return lift_free_vars(s[n - (vidx - offset) - 1], offset);
|
|
|
|
else
|
2013-08-06 18:27:14 +00:00
|
|
|
return mk_var(vidx - n);
|
2013-09-13 01:25:38 +00:00
|
|
|
} else {
|
|
|
|
return m;
|
2013-08-03 18:45:51 +00:00
|
|
|
}
|
2013-09-13 01:25:38 +00:00
|
|
|
} else if (is_metavar(m)) {
|
|
|
|
expr r = m;
|
|
|
|
for (unsigned i = 0; i < n; i++)
|
|
|
|
r = add_subst(r, offset + i, lift_free_vars(s[n - i - 1], offset + n));
|
|
|
|
return add_lower(r, offset + n, n);
|
|
|
|
} else {
|
|
|
|
return m;
|
2013-08-03 18:45:51 +00:00
|
|
|
}
|
|
|
|
};
|
2013-09-13 01:25:38 +00:00
|
|
|
return replace_fn<decltype(f)>(f)(a);
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|
|
|
|
}
|