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>
|
|
|
|
#include "free_vars.h"
|
|
|
|
#include "sets.h"
|
2013-07-26 19:27:55 +00:00
|
|
|
#include "replace.h"
|
2013-07-23 18:31:31 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
|
2013-07-26 19:27:55 +00:00
|
|
|
/** \brief Functional object for checking whether a kernel expression has free variables or not. */
|
|
|
|
class has_free_vars_fn {
|
|
|
|
protected:
|
2013-07-23 18:31:31 +00:00
|
|
|
expr_cell_offset_set m_visited;
|
2013-08-06 03:06:07 +00:00
|
|
|
bool m_set_closed_flag;
|
2013-07-24 07:45:38 +00:00
|
|
|
|
2013-07-26 19:27:55 +00:00
|
|
|
virtual bool process_var(expr const & x, unsigned offset) {
|
|
|
|
return var_idx(x) >= offset;
|
|
|
|
}
|
|
|
|
|
2013-07-23 18:31:31 +00:00
|
|
|
bool apply(expr const & e, unsigned offset) {
|
|
|
|
// handle easy cases
|
|
|
|
switch (e.kind()) {
|
2013-08-03 23:09:21 +00:00
|
|
|
case expr_kind::Constant: case expr_kind::Type: case expr_kind::Value:
|
2013-07-23 18:31:31 +00:00
|
|
|
return false;
|
|
|
|
case expr_kind::Var:
|
2013-07-26 19:27:55 +00:00
|
|
|
return process_var(e, offset);
|
2013-08-04 16:37:52 +00:00
|
|
|
case expr_kind::App: case expr_kind::Eq: case expr_kind::Lambda: case expr_kind::Pi: case expr_kind::Let:
|
2013-07-23 18:31:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.raw()->is_closed())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (is_shared(e)) {
|
|
|
|
expr_cell_offset p(e.raw(), offset);
|
|
|
|
if (m_visited.find(p) != m_visited.end())
|
|
|
|
return false;
|
|
|
|
m_visited.insert(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
switch (e.kind()) {
|
2013-08-03 23:09:21 +00:00
|
|
|
case expr_kind::Constant: case expr_kind::Type: case expr_kind::Value: case expr_kind::Var:
|
2013-07-23 18:31:31 +00:00
|
|
|
// easy cases were already handled
|
|
|
|
lean_unreachable(); return false;
|
|
|
|
case expr_kind::App:
|
|
|
|
result = std::any_of(begin_args(e), end_args(e), [=](expr const & arg){ return apply(arg, offset); });
|
|
|
|
break;
|
2013-08-04 16:37:52 +00:00
|
|
|
case expr_kind::Eq:
|
|
|
|
result = apply(eq_lhs(e), offset) || apply(eq_rhs(e), offset);
|
|
|
|
break;
|
2013-07-23 18:31:31 +00:00
|
|
|
case expr_kind::Lambda:
|
|
|
|
case expr_kind::Pi:
|
2013-08-03 18:31:42 +00:00
|
|
|
result = apply(abst_domain(e), offset) || apply(abst_body(e), offset + 1);
|
2013-07-23 18:31:31 +00:00
|
|
|
break;
|
2013-08-04 16:37:52 +00:00
|
|
|
case expr_kind::Let:
|
|
|
|
result = apply(let_value(e), offset) || apply(let_body(e), offset + 1);
|
|
|
|
break;
|
2013-07-23 18:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 03:06:07 +00:00
|
|
|
if (m_set_closed_flag && !result)
|
2013-07-23 18:31:31 +00:00
|
|
|
e.raw()->set_closed();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2013-07-24 07:45:38 +00:00
|
|
|
public:
|
2013-08-06 03:06:07 +00:00
|
|
|
has_free_vars_fn(bool s):m_set_closed_flag(s) {}
|
2013-07-24 07:45:38 +00:00
|
|
|
bool operator()(expr const & e) { return apply(e, 0); }
|
2013-07-23 18:31:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool has_free_vars(expr const & e) {
|
2013-08-06 03:06:07 +00:00
|
|
|
return has_free_vars_fn(true)(e);
|
2013-07-26 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Functional object for checking whether a kernel expression has a free variable in the range <tt>[low, high)</tt> or not. */
|
|
|
|
class has_free_var_in_range_fn : public has_free_vars_fn {
|
|
|
|
unsigned m_low;
|
|
|
|
unsigned m_high;
|
|
|
|
virtual bool process_var(expr const & x, unsigned offset) {
|
|
|
|
return var_idx(x) >= offset + m_low && var_idx(x) < offset + m_high;
|
|
|
|
}
|
|
|
|
public:
|
2013-08-06 03:06:07 +00:00
|
|
|
has_free_var_in_range_fn(unsigned low, unsigned high):
|
|
|
|
has_free_vars_fn(false /* We should not set the closed flag since we are only considering a range of free variables */),
|
|
|
|
m_low(low),
|
|
|
|
m_high(high) {
|
2013-07-26 19:27:55 +00:00
|
|
|
lean_assert(low < high);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool has_free_var(expr const & e, unsigned vidx) {
|
|
|
|
return has_free_var_in_range_fn(vidx, vidx+1)(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_free_var(expr const & e, unsigned low, unsigned high) {
|
|
|
|
return has_free_var_in_range_fn(low, high)(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
expr lower_free_vars(expr const & e, unsigned d) {
|
|
|
|
lean_assert(d > 0);
|
|
|
|
lean_assert(!has_free_var(e, 0, d));
|
|
|
|
auto f = [=](expr const & e, unsigned offset) -> expr {
|
|
|
|
if (is_var(e) && var_idx(e) >= offset) {
|
|
|
|
lean_assert(var_idx(e) >= offset + d);
|
|
|
|
return var(var_idx(e) - d);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return replace_fn<decltype(f)>(f)(e);
|
2013-07-23 18:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 08:39:29 +00:00
|
|
|
expr lift_free_vars(expr const & e, unsigned d) {
|
|
|
|
if (d == 0)
|
|
|
|
return e;
|
|
|
|
auto f = [=](expr const & e, unsigned offset) -> expr {
|
|
|
|
if (is_var(e) && var_idx(e) >= offset) {
|
|
|
|
return var(var_idx(e) + d);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return replace_fn<decltype(f)>(f)(e);
|
|
|
|
}
|
|
|
|
|
2013-07-23 18:31:31 +00:00
|
|
|
}
|