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"
|
2014-02-16 18:15:00 +00:00
|
|
|
#include "kernel/expr.h"
|
2013-11-19 21:03:46 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
class find_fn {
|
2014-02-16 19:23:25 +00:00
|
|
|
template<typename P>
|
2013-11-19 21:03:46 +00:00
|
|
|
struct pred_fn {
|
2013-12-08 07:21:07 +00:00
|
|
|
optional<expr> & m_result;
|
2014-02-16 19:23:25 +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) {}
|
2014-02-16 19:23:25 +00:00
|
|
|
bool operator()(expr const & e, unsigned offset) {
|
2013-11-19 21:03:46 +00:00
|
|
|
if (m_result) {
|
|
|
|
return false; // already found result, stop the search
|
2014-02-16 19:23:25 +00:00
|
|
|
} else if (m_p(e, offset)) {
|
2013-11-19 21:03:46 +00:00
|
|
|
m_result = e;
|
|
|
|
return false; // stop the search
|
|
|
|
} else {
|
|
|
|
return true; // continue the search
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-02-16 19:23:25 +00:00
|
|
|
optional<expr> m_result;
|
|
|
|
for_each_fn m_proc;
|
2013-11-19 21:03:46 +00:00
|
|
|
public:
|
2014-02-16 19:23:25 +00:00
|
|
|
template<typename P> find_fn(P const & p):m_proc(pred_fn<P>(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.
|
|
|
|
*/
|
2014-02-16 19:23:25 +00:00
|
|
|
template<typename P> optional<expr> find(expr const & e, P p) {
|
|
|
|
return find_fn(p)(e);
|
2013-11-19 21:03:46 +00:00
|
|
|
}
|
|
|
|
}
|