2014-06-13 22:13:32 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include "util/name_set.h"
|
|
|
|
#include "kernel/expr.h"
|
|
|
|
#include "kernel/for_each_fn.h"
|
2014-11-26 21:09:42 +00:00
|
|
|
#include "kernel/find_fn.h"
|
2014-11-27 01:08:14 +00:00
|
|
|
#include "library/locals.h"
|
2014-06-13 22:13:32 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
void collect_univ_params_core(level const & l, name_set & r) {
|
|
|
|
for_each(l, [&](level const & l) {
|
|
|
|
if (!has_param(l))
|
|
|
|
return false;
|
|
|
|
if (is_param(l))
|
|
|
|
r.insert(param_id(l));
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
name_set collect_univ_params(expr const & e, name_set const & ls) {
|
|
|
|
if (!has_param_univ(e)) {
|
|
|
|
return ls;
|
|
|
|
} else {
|
|
|
|
name_set r = ls;
|
|
|
|
for_each(e, [&](expr const & e, unsigned) {
|
|
|
|
if (!has_param_univ(e)) {
|
|
|
|
return false;
|
|
|
|
} else if (is_sort(e)) {
|
|
|
|
collect_univ_params_core(sort_level(e), r);
|
|
|
|
} else if (is_constant(e)) {
|
|
|
|
for (auto const & l : const_levels(e))
|
|
|
|
collect_univ_params_core(l, r);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
level_param_names to_level_param_names(name_set const & ls) {
|
|
|
|
level_param_names r;
|
|
|
|
ls.for_each([&](name const & n) {
|
|
|
|
r = level_param_names(n, r);
|
|
|
|
});
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-06-06 00:29:36 +00:00
|
|
|
void collected_locals::insert(expr const & l) {
|
|
|
|
if (m_local_names.contains(mlocal_name(l)))
|
|
|
|
return;
|
|
|
|
m_local_names.insert(mlocal_name(l));
|
|
|
|
m_locals.push_back(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
void collect_locals(expr const & e, collected_locals & ls, bool restricted) {
|
2014-07-14 03:12:58 +00:00
|
|
|
if (!has_local(e))
|
|
|
|
return;
|
|
|
|
for_each(e, [&](expr const & e, unsigned) {
|
|
|
|
if (!has_local(e))
|
|
|
|
return false;
|
2015-05-02 02:45:23 +00:00
|
|
|
if (is_local(e)) {
|
2014-07-14 03:12:58 +00:00
|
|
|
ls.insert(e);
|
2015-05-02 02:45:23 +00:00
|
|
|
return !restricted; // search type if not restricted
|
|
|
|
}
|
|
|
|
if (is_meta(e))
|
|
|
|
return !restricted; // search type if not restricted
|
2014-07-14 03:12:58 +00:00
|
|
|
return true;
|
|
|
|
});
|
2014-06-13 22:13:32 +00:00
|
|
|
}
|
2014-11-26 21:09:42 +00:00
|
|
|
|
|
|
|
bool contains_local(expr const & e, name const & n) {
|
|
|
|
if (!has_local(e))
|
|
|
|
return false;
|
|
|
|
bool result = false;
|
|
|
|
for_each(e, [&](expr const & e, unsigned) {
|
|
|
|
if (result || !has_local(e)) {
|
|
|
|
return false;
|
|
|
|
} else if (is_local(e) && mlocal_name(e) == n) {
|
|
|
|
result = true;
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2014-11-27 01:08:14 +00:00
|
|
|
|
2015-02-17 02:18:37 +00:00
|
|
|
optional<expr> depends_on(unsigned sz, expr const * es, expr const & h) {
|
2014-11-27 01:08:14 +00:00
|
|
|
for (unsigned i = 0; i < sz; i++)
|
|
|
|
if (depends_on(es[i], h))
|
2015-02-17 02:18:37 +00:00
|
|
|
return some_expr(es[i]);
|
|
|
|
return none_expr();
|
2014-11-27 01:08:14 +00:00
|
|
|
}
|
2015-05-15 22:56:18 +00:00
|
|
|
|
|
|
|
bool depends_on_any(expr const & e, unsigned hs_sz, expr const * hs) {
|
|
|
|
return std::any_of(hs, hs+hs_sz, [&](expr const & h) { return depends_on(e, h); });
|
|
|
|
}
|
2014-06-13 22:13:32 +00:00
|
|
|
}
|