2013-11-19 21:03:46 +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
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "kernel/for_each_fn.h"
|
|
|
|
#include "kernel/context.h"
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
template<typename P>
|
|
|
|
class find_fn {
|
|
|
|
struct pred_fn {
|
2013-12-08 07:21:07 +00:00
|
|
|
optional<expr> & m_result;
|
2013-11-19 21:03:46 +00:00
|
|
|
P m_p;
|
2013-12-08 07:21:07 +00:00
|
|
|
pred_fn(optional<expr> & result, P const & p):m_result(result), m_p(p) {}
|
2013-11-19 21:03:46 +00:00
|
|
|
bool operator()(expr const & e, unsigned) {
|
|
|
|
if (m_result) {
|
|
|
|
return false; // already found result, stop the search
|
|
|
|
} else if (m_p(e)) {
|
|
|
|
m_result = e;
|
|
|
|
return false; // stop the search
|
|
|
|
} else {
|
|
|
|
return true; // continue the search
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-12-08 07:21:07 +00:00
|
|
|
optional<expr> m_result;
|
2013-11-19 21:03:46 +00:00
|
|
|
for_each_fn<pred_fn> m_proc;
|
|
|
|
public:
|
|
|
|
find_fn(P const & p):m_proc(pred_fn(m_result, p)) {}
|
2013-12-08 07:21:07 +00:00
|
|
|
optional<expr> operator()(expr const & e) { m_proc(e); return m_result; }
|
2013-11-19 21:03:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return a subexpression of \c e that satisfies the predicate \c p.
|
|
|
|
*/
|
|
|
|
template<typename P>
|
2013-12-08 07:21:07 +00:00
|
|
|
optional<expr> find(expr const & e, P p) {
|
2013-11-19 21:03:46 +00:00
|
|
|
return find_fn<P>(p)(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return an expression \c e that satisfies \c p and occurs in \c c or \c es.
|
|
|
|
*/
|
|
|
|
template<typename P>
|
2013-12-08 07:21:07 +00:00
|
|
|
optional<expr> find(context const * c, unsigned sz, expr const * es, P p) {
|
2013-11-19 21:03:46 +00:00
|
|
|
find_fn<P> finder(p);
|
|
|
|
if (c) {
|
|
|
|
for (auto const & e : *c) {
|
2013-12-08 07:21:07 +00:00
|
|
|
auto const & d = e.get_domain();
|
|
|
|
if (d) {
|
|
|
|
if (optional<expr> r = finder(*d))
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
auto const & b = e.get_body();
|
|
|
|
if (b) {
|
|
|
|
if (optional<expr> r = finder(*b))
|
2013-11-19 21:03:46 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < sz; i++) {
|
2013-12-08 07:21:07 +00:00
|
|
|
if (optional<expr> r = finder(es[i]))
|
2013-11-19 21:03:46 +00:00
|
|
|
return r;
|
|
|
|
}
|
2013-12-08 18:34:38 +00:00
|
|
|
return none_expr();
|
2013-11-19 21:03:46 +00:00
|
|
|
}
|
|
|
|
}
|