2013-07-23 18:31:31 +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>
|
2014-01-13 01:06:57 +00:00
|
|
|
#include <limits>
|
2013-09-13 10:35:29 +00:00
|
|
|
#include "kernel/free_vars.h"
|
|
|
|
#include "kernel/expr_sets.h"
|
2013-12-03 20:40:52 +00:00
|
|
|
#include "kernel/replace_fn.h"
|
2014-01-13 00:45:34 +00:00
|
|
|
#include "kernel/for_each_fn.h"
|
2013-07-23 18:31:31 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-09-13 01:25:38 +00:00
|
|
|
/**
|
|
|
|
\brief Functional object for checking whether a kernel expression has a free variable in the range <tt>[low, high)</tt> or not.
|
|
|
|
*/
|
2013-08-11 18:19:59 +00:00
|
|
|
class has_free_var_in_range_fn {
|
|
|
|
protected:
|
2013-12-11 23:49:32 +00:00
|
|
|
unsigned m_low;
|
|
|
|
unsigned m_high;
|
|
|
|
expr_cell_offset_set m_cached;
|
2013-08-11 18:19:59 +00:00
|
|
|
|
2014-04-17 19:18:19 +00:00
|
|
|
bool apply(expr const & e, unsigned offset) {
|
|
|
|
unsigned range = get_free_var_range(e);
|
|
|
|
if (range == 0) {
|
|
|
|
lean_assert(closed(e));
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-14 22:36:14 +00:00
|
|
|
unsigned low1 = m_low + offset;
|
|
|
|
if (low1 < m_low)
|
|
|
|
return false; // overflow, vidx can't be >= max unsigned
|
2014-04-17 19:18:19 +00:00
|
|
|
if (range <= low1) {
|
2013-08-11 18:19:59 +00:00
|
|
|
return false;
|
2014-04-17 19:18:19 +00:00
|
|
|
} else {
|
|
|
|
lean_assert(range > low1);
|
|
|
|
unsigned high1 = m_high + offset;
|
|
|
|
if (high1 < m_high)
|
|
|
|
return true; // overflow, vidx is always < max unsigned
|
|
|
|
if (range <= high1)
|
|
|
|
return true;
|
|
|
|
// At this point, e contains a free variables in the range [0, range),
|
|
|
|
// and it definitely contains Var(range-1).
|
|
|
|
// Moreover [low1, high1) is a proper subset of [0, range), i.e., range > high1
|
|
|
|
if (is_var(e))
|
|
|
|
return var_idx(e) < high1;
|
2013-08-11 18:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool shared = false;
|
|
|
|
if (is_shared(e)) {
|
|
|
|
shared = true;
|
|
|
|
expr_cell_offset p(e.raw(), offset);
|
|
|
|
if (m_cached.find(p) != m_cached.end())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
switch (e.kind()) {
|
2014-02-16 21:36:46 +00:00
|
|
|
case expr_kind::Constant: case expr_kind::Sort: case expr_kind::Macro:
|
|
|
|
case expr_kind::Var:
|
2013-11-11 17:19:38 +00:00
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2014-02-16 21:36:46 +00:00
|
|
|
case expr_kind::Meta: case expr_kind::Local:
|
|
|
|
result = apply(mlocal_type(e), offset);
|
2014-02-23 00:12:06 +00:00
|
|
|
break;
|
2013-08-11 18:19:59 +00:00
|
|
|
case expr_kind::App:
|
2014-02-16 21:36:46 +00:00
|
|
|
result = apply(app_fn(e), offset) || apply(app_arg(e), offset);
|
2013-08-11 18:19:59 +00:00
|
|
|
break;
|
2014-04-17 17:52:07 +00:00
|
|
|
case expr_kind::Lambda: case expr_kind::Pi:
|
2014-02-16 21:36:46 +00:00
|
|
|
result = apply(binder_domain(e), offset) || apply(binder_body(e), offset + 1);
|
2013-08-11 18:19:59 +00:00
|
|
|
break;
|
|
|
|
case expr_kind::Let:
|
2013-12-08 07:21:07 +00:00
|
|
|
result = apply(let_type(e), offset) || apply(let_value(e), offset) || apply(let_body(e), offset + 1);
|
2013-08-11 18:19:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result && shared) {
|
|
|
|
m_cached.insert(expr_cell_offset(e.raw(), offset));
|
|
|
|
}
|
|
|
|
return result;
|
2013-07-26 19:27:55 +00:00
|
|
|
}
|
|
|
|
public:
|
2014-02-16 21:36:46 +00:00
|
|
|
has_free_var_in_range_fn(unsigned low, unsigned high):
|
2013-08-06 03:06:07 +00:00
|
|
|
m_low(low),
|
|
|
|
m_high(high) {
|
2013-07-26 19:27:55 +00:00
|
|
|
lean_assert(low < high);
|
|
|
|
}
|
2013-08-11 18:19:59 +00:00
|
|
|
bool operator()(expr const & e) { return apply(e, 0); }
|
2013-07-26 19:27:55 +00:00
|
|
|
};
|
|
|
|
|
2014-02-16 21:36:46 +00:00
|
|
|
bool has_free_var(expr const & e, unsigned low, unsigned high) {
|
2014-04-17 19:18:19 +00:00
|
|
|
unsigned range = get_free_var_range(e);
|
|
|
|
if (high <= low || range <= low)
|
|
|
|
return false;
|
|
|
|
if (range <= high)
|
|
|
|
return true;
|
|
|
|
return has_free_var_in_range_fn(low, high)(e);
|
2014-01-13 01:44:28 +00:00
|
|
|
}
|
2013-12-13 01:47:11 +00:00
|
|
|
bool has_free_var(expr const & e, unsigned i) { return has_free_var(e, i, i+1); }
|
2013-07-26 19:27:55 +00:00
|
|
|
|
2014-02-16 21:36:46 +00:00
|
|
|
expr lower_free_vars(expr const & e, unsigned s, unsigned d) {
|
2014-04-17 19:41:06 +00:00
|
|
|
if (d == 0 || s >= get_free_var_range(e))
|
2014-01-13 01:44:28 +00:00
|
|
|
return e;
|
2013-09-17 02:21:40 +00:00
|
|
|
lean_assert(s >= d);
|
2014-02-16 21:36:46 +00:00
|
|
|
lean_assert(!has_free_var(e, s-d, s));
|
2014-03-01 00:57:25 +00:00
|
|
|
return replace(e, [=](expr const & e, unsigned offset) -> optional<expr> {
|
2014-04-17 19:41:06 +00:00
|
|
|
unsigned s1 = s + offset;
|
|
|
|
if (s1 < s)
|
|
|
|
return some_expr(e); // overflow, vidx can't be >= max unsigned
|
|
|
|
if (s1 >= get_free_var_range(e))
|
|
|
|
return some_expr(e); // expression e does not contain free variables with idx >= s1
|
|
|
|
if (is_var(e) && var_idx(e) >= s1) {
|
2013-12-18 02:31:59 +00:00
|
|
|
lean_assert(var_idx(e) >= offset + d);
|
2014-03-01 00:57:25 +00:00
|
|
|
return some_expr(mk_var(var_idx(e) - d));
|
2013-12-18 02:31:59 +00:00
|
|
|
} else {
|
2014-03-01 00:57:25 +00:00
|
|
|
return none_expr();
|
2013-12-18 02:31:59 +00:00
|
|
|
}
|
|
|
|
});
|
2013-07-23 18:31:31 +00:00
|
|
|
}
|
2013-12-13 01:47:11 +00:00
|
|
|
expr lower_free_vars(expr const & e, unsigned d) { return lower_free_vars(e, d, d); }
|
2013-07-23 18:31:31 +00:00
|
|
|
|
2014-02-16 21:36:46 +00:00
|
|
|
expr lift_free_vars(expr const & e, unsigned s, unsigned d) {
|
2014-04-17 19:41:06 +00:00
|
|
|
if (d == 0 || s >= get_free_var_range(e))
|
2013-07-30 08:39:29 +00:00
|
|
|
return e;
|
2014-03-01 00:57:25 +00:00
|
|
|
return replace(e, [=](expr const & e, unsigned offset) -> optional<expr> {
|
2014-04-17 19:41:06 +00:00
|
|
|
unsigned s1 = s + offset;
|
|
|
|
if (s1 < s)
|
|
|
|
return some_expr(e); // overflow, vidx can't be >= max unsigned
|
|
|
|
if (s1 >= get_free_var_range(e))
|
|
|
|
return some_expr(e); // expression e does not contain free variables with idx >= s1
|
2013-12-18 02:31:59 +00:00
|
|
|
if (is_var(e) && var_idx(e) >= s + offset) {
|
2014-04-17 19:41:06 +00:00
|
|
|
unsigned new_idx = var_idx(e) + d;
|
|
|
|
if (new_idx < var_idx(e))
|
|
|
|
throw exception("invalid lift_free_vars operation, index overflow");
|
|
|
|
return some_expr(mk_var(new_idx));
|
2013-12-18 02:31:59 +00:00
|
|
|
} else {
|
2014-03-01 00:57:25 +00:00
|
|
|
return none_expr();
|
2013-12-18 02:31:59 +00:00
|
|
|
}
|
|
|
|
});
|
2013-07-30 08:39:29 +00:00
|
|
|
}
|
2013-12-13 01:47:11 +00:00
|
|
|
expr lift_free_vars(expr const & e, unsigned d) { return lift_free_vars(e, 0, d); }
|
2013-07-23 18:31:31 +00:00
|
|
|
}
|