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>
|
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"
|
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-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;
|
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;
|
|
|
|
|
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;
|
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:
|
2013-09-06 17:06:26 +00:00
|
|
|
result = (let_type(e) && 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-07 00:01:57 +00:00
|
|
|
return e && has_free_vars_fn()(e);
|
2013-07-26 19:27:55 +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:
|
|
|
|
unsigned m_low;
|
|
|
|
unsigned m_high;
|
|
|
|
expr_cell_offset_set m_cached;
|
|
|
|
|
|
|
|
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:
|
|
|
|
return true;
|
2013-08-11 18:19:59 +00:00
|
|
|
case expr_kind::Var:
|
|
|
|
return var_idx(e) >= offset + m_low && var_idx(e) < offset + m_high;
|
|
|
|
case expr_kind::App: case expr_kind::Eq: case expr_kind::Lambda: case expr_kind::Pi: case expr_kind::Let:
|
|
|
|
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;
|
|
|
|
case expr_kind::Type: case expr_kind::Value: case expr_kind::Var: case expr_kind::MetaVar:
|
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-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::Eq:
|
|
|
|
result = apply(eq_lhs(e), offset) || apply(eq_rhs(e), 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-09-06 17:06:26 +00:00
|
|
|
result = (let_type(e) && 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-08-06 03:06:07 +00:00
|
|
|
has_free_var_in_range_fn(unsigned low, unsigned high):
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-09-13 01:25:38 +00:00
|
|
|
expr lower_free_vars(expr const & e, unsigned s, unsigned d) {
|
2013-09-17 02:21:40 +00:00
|
|
|
lean_assert(s >= d);
|
|
|
|
lean_assert(!has_free_var(e, s-d, s));
|
2013-07-26 19:27:55 +00:00
|
|
|
auto f = [=](expr const & e, unsigned offset) -> expr {
|
2013-09-13 01:25:38 +00:00
|
|
|
if (is_var(e) && var_idx(e) >= s + offset) {
|
2013-07-26 19:27:55 +00:00
|
|
|
lean_assert(var_idx(e) >= offset + d);
|
2013-08-06 18:27:14 +00:00
|
|
|
return mk_var(var_idx(e) - d);
|
2013-09-13 01:25:38 +00:00
|
|
|
} else if (is_metavar(e)) {
|
2013-11-11 17:19:38 +00:00
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2013-08-07 15:17:33 +00:00
|
|
|
} else {
|
2013-07-26 19:27:55 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return replace_fn<decltype(f)>(f)(e);
|
2013-07-23 18:31:31 +00:00
|
|
|
}
|
|
|
|
|
2013-09-13 01:25:38 +00:00
|
|
|
expr lift_free_vars(expr const & e, unsigned s, unsigned d) {
|
2013-07-30 08:39:29 +00:00
|
|
|
if (d == 0)
|
|
|
|
return e;
|
|
|
|
auto f = [=](expr const & e, unsigned offset) -> expr {
|
2013-09-13 01:25:38 +00:00
|
|
|
if (is_var(e) && var_idx(e) >= s + offset) {
|
2013-08-06 18:27:14 +00:00
|
|
|
return mk_var(var_idx(e) + d);
|
2013-09-13 01:25:38 +00:00
|
|
|
} else if (is_metavar(e)) {
|
|
|
|
return add_lift(e, s + offset, d);
|
2013-08-07 15:17:33 +00:00
|
|
|
} else {
|
2013-07-30 08:39:29 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return replace_fn<decltype(f)>(f)(e);
|
|
|
|
}
|
2013-07-23 18:31:31 +00:00
|
|
|
}
|