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-12-12 18:50:07 +00:00
|
|
|
#include <limits>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "kernel/free_vars.h"
|
2013-12-03 20:40:52 +00:00
|
|
|
#include "kernel/replace_fn.h"
|
2013-12-12 18:50:07 +00:00
|
|
|
#include "kernel/instantiate.h"
|
2013-07-24 07:32:01 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-05-12 23:38:53 +00:00
|
|
|
expr instantiate(expr const & a, unsigned s, unsigned n, expr const * subst) {
|
2014-04-24 19:46:19 +00:00
|
|
|
if (s >= get_free_var_range(a) || n == 0)
|
2014-04-17 20:12:49 +00:00
|
|
|
return a;
|
2014-03-01 00:57:25 +00:00
|
|
|
return replace(a, [=](expr const & m, unsigned offset) -> optional<expr> {
|
2014-04-17 20:12:49 +00:00
|
|
|
unsigned s1 = s + offset;
|
|
|
|
if (s1 < s)
|
|
|
|
return some_expr(m); // overflow, vidx can't be >= max unsigned
|
|
|
|
if (s1 >= get_free_var_range(m))
|
|
|
|
return some_expr(m); // expression m does not contain free variables with idx >= s1
|
2013-12-18 02:31:59 +00:00
|
|
|
if (is_var(m)) {
|
|
|
|
unsigned vidx = var_idx(m);
|
2014-04-17 20:12:49 +00:00
|
|
|
if (vidx >= s1) {
|
|
|
|
unsigned h = s1 + n;
|
|
|
|
if (h < s1 /* overflow, h is bigger than any vidx */ || vidx < h) {
|
|
|
|
return some_expr(lift_free_vars(subst[vidx - s1], offset));
|
2014-01-21 22:30:38 +00:00
|
|
|
} else {
|
2014-03-01 00:57:25 +00:00
|
|
|
return some_expr(mk_var(vidx - n));
|
2014-01-21 22:30:38 +00:00
|
|
|
}
|
2013-12-18 02:31:59 +00:00
|
|
|
}
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|
2014-03-01 00:57:25 +00:00
|
|
|
return none_expr();
|
2014-05-12 23:38:53 +00:00
|
|
|
});
|
2013-08-03 18:45:51 +00:00
|
|
|
}
|
2014-01-21 22:30:38 +00:00
|
|
|
|
2014-02-17 23:21:48 +00:00
|
|
|
expr instantiate(expr const & e, unsigned n, expr const * s) { return instantiate(e, 0, n, s); }
|
2013-12-13 01:47:11 +00:00
|
|
|
expr instantiate(expr const & e, std::initializer_list<expr> const & l) { return instantiate(e, l.size(), l.begin()); }
|
2014-02-17 23:21:48 +00:00
|
|
|
expr instantiate(expr const & e, unsigned i, expr const & s) { return instantiate(e, i, 1, &s); }
|
|
|
|
expr instantiate(expr const & e, expr const & s) { return instantiate(e, 0, s); }
|
2013-10-23 22:44:26 +00:00
|
|
|
|
2014-06-15 17:40:18 +00:00
|
|
|
expr instantiate_rev(expr const & a, unsigned n, expr const * subst) {
|
|
|
|
if (closed(a))
|
|
|
|
return a;
|
|
|
|
return replace(a, [=](expr const & m, unsigned offset) -> optional<expr> {
|
|
|
|
if (offset >= get_free_var_range(m))
|
|
|
|
return some_expr(m); // expression m does not contain free variables with idx >= offset
|
|
|
|
if (is_var(m)) {
|
|
|
|
unsigned vidx = var_idx(m);
|
|
|
|
if (vidx >= offset) {
|
|
|
|
unsigned h = offset + n;
|
|
|
|
if (h < offset /* overflow, h is bigger than any vidx */ || vidx < h) {
|
|
|
|
return some_expr(lift_free_vars(subst[n - (vidx - offset) - 1], offset));
|
|
|
|
} else {
|
|
|
|
return some_expr(mk_var(vidx - n));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none_expr();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-23 22:44:26 +00:00
|
|
|
bool is_head_beta(expr const & t) {
|
2014-02-17 23:21:48 +00:00
|
|
|
expr const * it = &t;
|
|
|
|
while (is_app(*it)) {
|
|
|
|
expr const & f = app_fn(*it);
|
|
|
|
if (is_lambda(f)) {
|
|
|
|
return true;
|
|
|
|
} else if (is_app(f)) {
|
|
|
|
it = &f;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2013-10-23 22:44:26 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 23:21:48 +00:00
|
|
|
expr apply_beta(expr f, unsigned num_args, expr const * args) {
|
2014-05-25 13:57:13 +00:00
|
|
|
if (num_args == 0) {
|
|
|
|
return f;
|
|
|
|
} else if (!is_lambda(f)) {
|
2014-02-22 19:44:57 +00:00
|
|
|
return mk_rev_app(f, num_args, args);
|
2014-01-14 22:36:51 +00:00
|
|
|
} else {
|
|
|
|
unsigned m = 1;
|
2014-05-16 18:13:50 +00:00
|
|
|
while (is_lambda(binding_body(f)) && m < num_args) {
|
|
|
|
f = binding_body(f);
|
2014-01-14 22:36:51 +00:00
|
|
|
m++;
|
|
|
|
}
|
|
|
|
lean_assert(m <= num_args);
|
2014-05-16 18:13:50 +00:00
|
|
|
return mk_rev_app(instantiate(binding_body(f), m, args + (num_args - m)), num_args - m, args);
|
2013-10-23 23:01:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 23:21:48 +00:00
|
|
|
expr head_beta_reduce(expr const & t) {
|
2013-10-23 22:44:26 +00:00
|
|
|
if (!is_head_beta(t)) {
|
|
|
|
return t;
|
|
|
|
} else {
|
2014-02-17 23:21:48 +00:00
|
|
|
buffer<expr> args;
|
|
|
|
expr const * it = &t;
|
|
|
|
while (true) {
|
|
|
|
lean_assert(is_app(*it));
|
2014-02-17 23:32:27 +00:00
|
|
|
expr const & f = app_fn(*it);
|
2014-02-17 23:21:48 +00:00
|
|
|
args.push_back(app_arg(*it));
|
|
|
|
if (is_lambda(f)) {
|
|
|
|
return apply_beta(f, args.size(), args.data());
|
|
|
|
} else {
|
|
|
|
lean_assert(is_app(f));
|
|
|
|
it = &f;
|
|
|
|
}
|
|
|
|
}
|
2013-10-23 22:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 23:21:48 +00:00
|
|
|
expr beta_reduce(expr t) {
|
2014-03-01 00:57:25 +00:00
|
|
|
auto f = [=](expr const & m, unsigned) -> optional<expr> {
|
2013-10-23 22:44:26 +00:00
|
|
|
if (is_head_beta(m))
|
2014-03-01 00:57:25 +00:00
|
|
|
return some_expr(head_beta_reduce(m));
|
2014-06-28 01:35:59 +00:00
|
|
|
else if (is_local(m) || is_metavar(m))
|
|
|
|
return some_expr(m); // do not simplify local constants and metavariables types.
|
2013-10-23 22:44:26 +00:00
|
|
|
else
|
2014-03-01 00:57:25 +00:00
|
|
|
return none_expr();
|
2013-10-23 22:44:26 +00:00
|
|
|
};
|
|
|
|
while (true) {
|
2014-02-17 23:21:48 +00:00
|
|
|
expr new_t = replace_fn(f)(t);
|
2013-10-23 22:44:26 +00:00
|
|
|
if (new_t == t)
|
|
|
|
return new_t;
|
|
|
|
else
|
|
|
|
t = new_t;
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 02:31:38 +00:00
|
|
|
|
2014-05-16 18:13:50 +00:00
|
|
|
expr instantiate_params(expr const & e, level_param_names const & ps, levels const & ls) {
|
2014-03-01 01:16:34 +00:00
|
|
|
if (!has_param_univ(e))
|
|
|
|
return e;
|
|
|
|
return replace(e, [&](expr const & e, unsigned) -> optional<expr> {
|
|
|
|
if (!has_param_univ(e))
|
|
|
|
return some_expr(e);
|
|
|
|
if (is_constant(e)) {
|
2014-05-16 18:13:50 +00:00
|
|
|
return some_expr(update_constant(e, map_reuse(const_levels(e),
|
2014-03-01 01:16:34 +00:00
|
|
|
[&](level const & l) { return instantiate(l, ps, ls); },
|
|
|
|
[](level const & l1, level const & l2) { return is_eqp(l1, l2); })));
|
|
|
|
} else if (is_sort(e)) {
|
|
|
|
return some_expr(update_sort(e, instantiate(sort_level(e), ps, ls)));
|
|
|
|
} else {
|
|
|
|
return none_expr();
|
|
|
|
}
|
|
|
|
});
|
2014-02-25 02:31:38 +00:00
|
|
|
}
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|