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-09-13 01:25:38 +00:00
|
|
|
#include "kernel/metavar.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 free variables or not.
|
2013-07-23 18:31:31 +00:00
|
|
|
|
2013-09-13 01:25:38 +00:00
|
|
|
\remark We assume that a metavariable contains free variables.
|
|
|
|
This is an approximation, since we don't know how the metavariable will be instantiated.
|
|
|
|
*/
|
2013-07-26 19:27:55 +00:00
|
|
|
class has_free_vars_fn {
|
|
|
|
protected:
|
2013-08-11 18:19:59 +00:00
|
|
|
expr_cell_offset_set m_cached;
|
2013-07-26 19:27:55 +00:00
|
|
|
|
2013-12-08 07:21:07 +00:00
|
|
|
bool apply(optional<expr> const & e, unsigned offset) {
|
|
|
|
return e && apply(*e, offset);
|
|
|
|
}
|
|
|
|
|
2013-07-23 18:31:31 +00:00
|
|
|
bool apply(expr const & e, unsigned offset) {
|
|
|
|
// handle easy cases
|
|
|
|
switch (e.kind()) {
|
2013-11-19 19:21:45 +00:00
|
|
|
case expr_kind::Constant:
|
|
|
|
if (!const_type(e))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case expr_kind::Type: case expr_kind::Value:
|
2013-07-23 18:31:31 +00:00
|
|
|
return false;
|
2013-09-13 01:25:38 +00:00
|
|
|
case expr_kind::MetaVar:
|
|
|
|
return true;
|
2013-07-23 18:31:31 +00:00
|
|
|
case expr_kind::Var:
|
2013-08-11 18:19:59 +00:00
|
|
|
return var_idx(e) >= offset;
|
2014-01-16 23:07:51 +00:00
|
|
|
case expr_kind::App: 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;
|
|
|
|
|
2013-08-11 18:19:59 +00:00
|
|
|
if (offset == 0) {
|
|
|
|
return apply_core(e, 0);
|
|
|
|
} else {
|
|
|
|
// The apply_core(e, 0) may seem redundant, but it allows us to
|
|
|
|
// mark nested closed expressions.
|
|
|
|
return apply_core(e, 0) && apply_core(e, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool apply_core(expr const & e, unsigned offset) {
|
|
|
|
bool shared = false;
|
2013-07-23 18:31:31 +00:00
|
|
|
if (is_shared(e)) {
|
2013-08-11 18:19:59 +00:00
|
|
|
shared = true;
|
2013-07-23 18:31:31 +00:00
|
|
|
expr_cell_offset p(e.raw(), offset);
|
2013-08-11 18:19:59 +00:00
|
|
|
if (m_cached.find(p) != m_cached.end())
|
2013-07-23 18:31:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
switch (e.kind()) {
|
2013-11-19 19:21:45 +00:00
|
|
|
case expr_kind::Constant:
|
|
|
|
lean_assert(const_type(e));
|
|
|
|
result = apply(const_type(e), offset);
|
|
|
|
break;
|
|
|
|
case expr_kind::Type: case expr_kind::Value: case expr_kind::Var: case expr_kind::MetaVar:
|
2013-07-23 18:31:31 +00:00
|
|
|
// easy cases were already handled
|
2013-11-11 17:19:38 +00:00
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2013-07-23 18:31:31 +00:00
|
|
|
case expr_kind::App:
|
|
|
|
result = std::any_of(begin_args(e), end_args(e), [=](expr const & arg){ return apply(arg, offset); });
|
|
|
|
break;
|
|
|
|
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:
|
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-04 16:37:52 +00:00
|
|
|
break;
|
2013-07-23 18:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 18:19:59 +00:00
|
|
|
if (!result) {
|
|
|
|
if (offset == 0)
|
|
|
|
e.raw()->set_closed();
|
|
|
|
if (shared)
|
|
|
|
m_cached.insert(expr_cell_offset(e.raw(), offset));
|
|
|
|
}
|
2013-07-23 18:31:31 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2013-07-24 07:45:38 +00:00
|
|
|
public:
|
2013-08-11 18:19:59 +00:00
|
|
|
has_free_vars_fn() {}
|
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-12-08 07:21:07 +00:00
|
|
|
return has_free_vars_fn()(e);
|
2013-07-26 19:27:55 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 23:21:52 +00:00
|
|
|
/**
|
|
|
|
\brief Functional object for computing the range [0, R) of free variables occurring
|
|
|
|
in an expression.
|
|
|
|
*/
|
|
|
|
class free_var_range_fn {
|
|
|
|
expr_map<unsigned> m_cached;
|
2014-01-13 01:06:57 +00:00
|
|
|
optional<metavar_env> const & m_menv;
|
2013-12-11 23:21:52 +00:00
|
|
|
|
|
|
|
static unsigned dec(unsigned s) { return (s == 0) ? 0 : s - 1; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
\brief If a metavariable \c m was defined in a context \c ctx and <tt>ctx.size() == R</tt>,
|
|
|
|
then \c m can only contain free variables in the range <tt>[0, R)</tt>
|
|
|
|
|
|
|
|
So, if \c m does not have an associated local context, the answer is just \c R.
|
|
|
|
If \c m has an associated local context, we process it using the following rules
|
|
|
|
|
|
|
|
[inst:s v] R ===> if s >= R then R else max(R-1, range_of(v))
|
|
|
|
[lift:s:n] R ===> if s >= R then R else R + n
|
|
|
|
*/
|
|
|
|
unsigned process_metavar(expr const & m) {
|
|
|
|
lean_assert(is_metavar(m));
|
2014-01-13 01:06:57 +00:00
|
|
|
if (!m_menv)
|
|
|
|
return std::numeric_limits<unsigned>::max(); // metavariable environment is not available, assume the worst.
|
|
|
|
context ctx = (*m_menv)->get_context(metavar_name(m));
|
2013-12-11 23:21:52 +00:00
|
|
|
unsigned R = ctx.size();
|
|
|
|
if (has_local_context(m)) {
|
|
|
|
local_context lctx = metavar_lctx(m);
|
|
|
|
buffer<local_entry> lentries;
|
|
|
|
to_buffer(lctx, lentries);
|
|
|
|
unsigned i = lentries.size();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
local_entry const & entry = lentries[i];
|
|
|
|
if (entry.is_inst()) {
|
|
|
|
if (entry.s() < R) {
|
|
|
|
R = std::max(dec(R), apply(entry.v()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (entry.s() < R)
|
|
|
|
R += entry.n();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned apply(optional<expr> const & e) {
|
|
|
|
return e ? apply(*e) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned apply(expr const & e) {
|
|
|
|
// handle easy cases
|
|
|
|
switch (e.kind()) {
|
|
|
|
case expr_kind::Constant:
|
|
|
|
if (!const_type(e))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case expr_kind::Type: case expr_kind::Value:
|
|
|
|
return 0;
|
|
|
|
case expr_kind::Var:
|
|
|
|
return var_idx(e) + 1;
|
2014-01-16 23:07:51 +00:00
|
|
|
case expr_kind::MetaVar: case expr_kind::App:
|
2013-12-11 23:21:52 +00:00
|
|
|
case expr_kind::Lambda: case expr_kind::Pi: case expr_kind::Let:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.raw()->is_closed())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bool shared = false;
|
|
|
|
if (is_shared(e)) {
|
|
|
|
shared = true;
|
|
|
|
auto it = m_cached.find(e);
|
|
|
|
if (it != m_cached.end())
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned result = 0;
|
|
|
|
|
|
|
|
switch (e.kind()) {
|
|
|
|
case expr_kind::Constant:
|
|
|
|
lean_assert(const_type(e));
|
|
|
|
result = apply(const_type(e));
|
|
|
|
break;
|
|
|
|
case expr_kind::Type: case expr_kind::Value: case expr_kind::Var:
|
|
|
|
// easy cases were already handled
|
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
|
|
|
case expr_kind::MetaVar:
|
|
|
|
result = process_metavar(e);
|
|
|
|
break;
|
|
|
|
case expr_kind::App:
|
|
|
|
for (auto const & c : args(e))
|
|
|
|
result = std::max(result, apply(c));
|
|
|
|
break;
|
|
|
|
case expr_kind::Lambda:
|
|
|
|
case expr_kind::Pi:
|
|
|
|
result = std::max(apply(abst_domain(e)), dec(apply(abst_body(e))));
|
|
|
|
break;
|
|
|
|
case expr_kind::Let:
|
|
|
|
result = std::max({apply(let_type(e)), apply(let_value(e)), dec(apply(let_body(e)))});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shared)
|
|
|
|
m_cached.insert(mk_pair(e, result));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
public:
|
2014-01-13 01:06:57 +00:00
|
|
|
free_var_range_fn(optional<metavar_env> const & menv):m_menv(menv) {}
|
2013-12-11 23:21:52 +00:00
|
|
|
unsigned operator()(expr const & e) { return apply(e); }
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned free_var_range(expr const & e, metavar_env const & menv) {
|
2014-01-13 01:06:57 +00:00
|
|
|
return free_var_range_fn(some_menv(menv))(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned free_var_range(expr const & e) {
|
|
|
|
return free_var_range_fn(none_menv())(e);
|
2013-12-11 23:21:52 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
\remark We assume that a metavariable contains free variables.
|
|
|
|
This is an approximation, since we don't know how the metavariable will be instantiated.
|
|
|
|
*/
|
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;
|
|
|
|
std::unique_ptr<free_var_range_fn> m_range_fn;
|
2013-08-11 18:19:59 +00:00
|
|
|
|
2013-12-08 07:21:07 +00:00
|
|
|
bool apply(optional<expr> const & e, unsigned offset) {
|
|
|
|
return e && apply(*e, offset);
|
|
|
|
}
|
|
|
|
|
2014-01-14 22:36:14 +00:00
|
|
|
// Return true iff m_low + offset <= vidx
|
|
|
|
bool ge_lower(unsigned vidx, unsigned offset) const {
|
|
|
|
unsigned low1 = m_low + offset;
|
|
|
|
if (low1 < m_low)
|
|
|
|
return false; // overflow, vidx can't be >= max unsigned
|
|
|
|
return vidx >= low1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true iff vidx < m_high + offset
|
|
|
|
bool lt_upper(unsigned vidx, unsigned offset) const {
|
|
|
|
unsigned high1 = m_high + offset;
|
|
|
|
if (high1 < m_high)
|
|
|
|
return true; // overflow, vidx is always < max unsigned
|
|
|
|
return vidx < high1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true iff m_low + offset <= vidx < m_high + offset
|
|
|
|
bool in_interval(unsigned vidx, unsigned offset) const {
|
|
|
|
return ge_lower(vidx, offset) && lt_upper(vidx, offset);
|
|
|
|
}
|
|
|
|
|
2013-08-11 18:19:59 +00:00
|
|
|
bool apply(expr const & e, unsigned offset) {
|
|
|
|
// handle easy cases
|
|
|
|
switch (e.kind()) {
|
2013-11-19 19:21:45 +00:00
|
|
|
case expr_kind::Constant:
|
|
|
|
if (!const_type(e))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case expr_kind::Type: case expr_kind::Value:
|
2013-08-11 18:19:59 +00:00
|
|
|
return false;
|
2013-09-13 01:25:38 +00:00
|
|
|
case expr_kind::MetaVar:
|
2013-12-11 23:49:32 +00:00
|
|
|
if (m_range_fn)
|
|
|
|
break; // it is not cheap
|
|
|
|
else
|
|
|
|
return true; // assume that any free variable can occur in the metavariable
|
2013-08-11 18:19:59 +00:00
|
|
|
case expr_kind::Var:
|
2014-01-14 22:36:14 +00:00
|
|
|
return in_interval(var_idx(e), offset);
|
2014-01-16 23:07:51 +00:00
|
|
|
case expr_kind::App: case expr_kind::Lambda: case expr_kind::Pi: case expr_kind::Let:
|
2013-08-11 18:19:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.raw()->is_closed())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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()) {
|
2013-11-19 19:21:45 +00:00
|
|
|
case expr_kind::Constant:
|
|
|
|
lean_assert(const_type(e));
|
|
|
|
result = apply(const_type(e), offset);
|
|
|
|
break;
|
2013-12-11 23:49:32 +00:00
|
|
|
case expr_kind::Type: case expr_kind::Value: case expr_kind::Var:
|
2013-08-11 18:19:59 +00:00
|
|
|
// easy cases were already handled
|
2013-11-11 17:19:38 +00:00
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2013-12-11 23:49:32 +00:00
|
|
|
case expr_kind::MetaVar: {
|
|
|
|
lean_assert(m_range_fn);
|
|
|
|
unsigned R = (*m_range_fn)(e);
|
|
|
|
if (R > 0) {
|
|
|
|
unsigned max_fvar_idx = R - 1;
|
2014-01-14 22:36:14 +00:00
|
|
|
result = ge_lower(max_fvar_idx, offset);
|
2013-12-11 23:49:32 +00:00
|
|
|
// Remark: Variable #0 may occur in \c e.
|
|
|
|
// So, we don't have to check the upper bound offset + m_high;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-08-11 18:19:59 +00:00
|
|
|
case expr_kind::App:
|
|
|
|
result = std::any_of(begin_args(e), end_args(e), [=](expr const & arg){ return apply(arg, offset); });
|
|
|
|
break;
|
|
|
|
case expr_kind::Lambda:
|
|
|
|
case expr_kind::Pi:
|
|
|
|
result = apply(abst_domain(e), offset) || apply(abst_body(e), offset + 1);
|
|
|
|
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:
|
2013-12-13 01:47:11 +00:00
|
|
|
has_free_var_in_range_fn(unsigned low, unsigned high, optional<metavar_env> const & menv):
|
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-12-11 23:49:32 +00:00
|
|
|
if (menv)
|
2014-01-13 01:06:57 +00:00
|
|
|
m_range_fn.reset(new free_var_range_fn(menv));
|
2013-07-26 19:27:55 +00:00
|
|
|
}
|
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-01-13 01:44:28 +00:00
|
|
|
bool has_free_var(expr const & e, unsigned low, unsigned high, optional<metavar_env> const & menv) {
|
|
|
|
return high > low && has_free_var_in_range_fn(low, high, menv)(e);
|
|
|
|
}
|
2013-12-13 01:47:11 +00:00
|
|
|
bool has_free_var(expr const & e, unsigned low, unsigned high, metavar_env const & menv) { return has_free_var(e, low, high, some_menv(menv)); }
|
|
|
|
bool has_free_var(expr const & e, unsigned low, unsigned high) { return has_free_var(e, low, high, none_menv()); }
|
|
|
|
bool has_free_var(expr const & e, unsigned i, optional<metavar_env> const & menv) { return has_free_var(e, i, i+1, menv); }
|
|
|
|
bool has_free_var(expr const & e, unsigned i, metavar_env const & menv) { return has_free_var(e, i, i+1, menv); }
|
|
|
|
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-01-01 11:02:41 +00:00
|
|
|
bool has_free_var(context_entry const & e, unsigned low, unsigned high, metavar_env const & menv) {
|
2014-01-13 01:44:28 +00:00
|
|
|
if (high <= low)
|
|
|
|
return false;
|
2014-01-01 11:02:41 +00:00
|
|
|
auto d = e.get_domain();
|
|
|
|
auto b = e.get_body();
|
|
|
|
return (d && has_free_var(*d, low, high, menv)) || (b && has_free_var(*b, low, high, menv));
|
|
|
|
}
|
|
|
|
|
2014-01-14 22:36:14 +00:00
|
|
|
bool has_free_var_ge(expr const & e, unsigned low, metavar_env const & menv) { return has_free_var(e, low, std::numeric_limits<unsigned>::max(), menv); }
|
|
|
|
bool has_free_var_ge(expr const & e, unsigned low) { return has_free_var(e, low, std::numeric_limits<unsigned>::max()); }
|
|
|
|
|
2013-12-13 01:47:11 +00:00
|
|
|
expr lower_free_vars(expr const & e, unsigned s, unsigned d, optional<metavar_env> const & DEBUG_CODE(menv)) {
|
2014-01-13 01:44:28 +00:00
|
|
|
if (d == 0)
|
|
|
|
return e;
|
2013-09-17 02:21:40 +00:00
|
|
|
lean_assert(s >= d);
|
2013-12-11 23:49:32 +00:00
|
|
|
lean_assert(!has_free_var(e, s-d, s, menv));
|
2013-12-18 02:31:59 +00:00
|
|
|
return replace(e, [=](expr const & e, unsigned offset) -> expr {
|
|
|
|
if (is_var(e) && var_idx(e) >= s + offset) {
|
|
|
|
lean_assert(var_idx(e) >= offset + d);
|
|
|
|
return mk_var(var_idx(e) - d);
|
|
|
|
} else {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
});
|
2013-07-23 18:31:31 +00:00
|
|
|
}
|
2013-12-13 01:47:11 +00:00
|
|
|
expr lower_free_vars(expr const & e, unsigned s, unsigned d, metavar_env const & menv) { return lower_free_vars(e, s, d, some_menv(menv)); }
|
|
|
|
expr lower_free_vars(expr const & e, unsigned s, unsigned d) { return lower_free_vars(e, s, d, none_menv()); }
|
|
|
|
expr lower_free_vars(expr const & e, unsigned d, optional<metavar_env> const & menv) { return lower_free_vars(e, d, d, menv); }
|
|
|
|
expr lower_free_vars(expr const & e, unsigned d, metavar_env const & menv) { return lower_free_vars(e, d, d, menv); }
|
|
|
|
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-01-01 11:02:41 +00:00
|
|
|
context_entry lower_free_vars(context_entry const & e, unsigned s, unsigned d, metavar_env const & menv) {
|
|
|
|
auto domain = e.get_domain();
|
|
|
|
auto body = e.get_body();
|
|
|
|
if (domain && body)
|
|
|
|
return context_entry(e.get_name(), lower_free_vars(*domain, s, d, menv), lower_free_vars(*body, s, d, menv));
|
|
|
|
else if (domain)
|
|
|
|
return context_entry(e.get_name(), lower_free_vars(*domain, s, d, menv));
|
|
|
|
else
|
|
|
|
return context_entry(e.get_name(), none_expr(), lower_free_vars(*body, s, d, menv));
|
|
|
|
}
|
|
|
|
|
2013-12-13 01:47:11 +00:00
|
|
|
expr lift_free_vars(expr const & e, unsigned s, unsigned d, optional<metavar_env> const & menv) {
|
2013-07-30 08:39:29 +00:00
|
|
|
if (d == 0)
|
|
|
|
return e;
|
2013-12-18 02:31:59 +00:00
|
|
|
return replace(e, [=](expr const & e, unsigned offset) -> expr {
|
|
|
|
if (is_var(e) && var_idx(e) >= s + offset) {
|
|
|
|
return mk_var(var_idx(e) + d);
|
|
|
|
} else if (is_metavar(e)) {
|
|
|
|
return add_lift(e, s + offset, d, menv);
|
|
|
|
} else {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
});
|
2013-07-30 08:39:29 +00:00
|
|
|
}
|
2013-12-13 01:47:11 +00:00
|
|
|
expr lift_free_vars(expr const & e, unsigned s, unsigned d, metavar_env const & menv) { return lift_free_vars(e, s, d, some_menv(menv)); }
|
|
|
|
expr lift_free_vars(expr const & e, unsigned s, unsigned d) { return lift_free_vars(e, s, d, none_menv()); }
|
|
|
|
expr lift_free_vars(expr const & e, unsigned d, optional<metavar_env> const & menv) { return lift_free_vars(e, 0, d, menv); }
|
|
|
|
expr lift_free_vars(expr const & e, unsigned d) { return lift_free_vars(e, 0, d); }
|
|
|
|
expr lift_free_vars(expr const & e, unsigned d, metavar_env const & menv) { return lift_free_vars(e, 0, d, menv); }
|
2014-01-01 11:02:41 +00:00
|
|
|
|
|
|
|
context_entry lift_free_vars(context_entry const & e, unsigned s, unsigned d, metavar_env const & menv) {
|
|
|
|
auto domain = e.get_domain();
|
|
|
|
auto body = e.get_body();
|
|
|
|
if (domain && body)
|
|
|
|
return context_entry(e.get_name(), lift_free_vars(*domain, s, d, menv), lift_free_vars(*body, s, d, menv));
|
|
|
|
else if (domain)
|
|
|
|
return context_entry(e.get_name(), lift_free_vars(*domain, s, d, menv));
|
|
|
|
else
|
|
|
|
return context_entry(e.get_name(), none_expr(), lift_free_vars(*body, s, d, menv));
|
|
|
|
}
|
2013-07-23 18:31:31 +00:00
|
|
|
}
|