lean2/src/kernel/find_fn.h
Leonardo de Moura 42867d6fcd refactor(kernel/find_fn): simplify find_fn module
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
2014-07-23 18:09:58 -07:00

27 lines
731 B
C++

/*
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/expr.h"
namespace lean {
/** \brief Return a subexpression of \c e that satisfies the predicate \c p. */
template<typename P> optional<expr> find(expr const & e, P p) {
optional<expr> result;
for_each(e, [&](expr const & e, unsigned offset) {
if (result) {
return false;
} else if (p(e, offset)) {
result = e;
return false;
} else {
return true;
}
});
return result;
}
}