2013-08-08 02:10:12 +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
|
2013-10-25 21:52:08 +00:00
|
|
|
#include <memory>
|
2013-09-13 10:35:29 +00:00
|
|
|
#include "kernel/expr.h"
|
|
|
|
#include "kernel/expr_sets.h"
|
2013-08-08 02:10:12 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-09-13 20:46:22 +00:00
|
|
|
/**
|
|
|
|
\brief Template for implementing expression visitors.
|
|
|
|
The argument \c F must be a function object containing the method
|
|
|
|
<code>
|
|
|
|
void operator()(expr const & e, unsigned offset)
|
|
|
|
</code>
|
|
|
|
The \c offset is the number of binders under which \c e occurs.
|
|
|
|
*/
|
2013-08-08 02:10:12 +00:00
|
|
|
template<typename F>
|
|
|
|
class for_each_fn {
|
2013-10-25 21:52:08 +00:00
|
|
|
std::unique_ptr<expr_cell_offset_set> m_visited;
|
|
|
|
F m_f;
|
2013-10-25 21:58:02 +00:00
|
|
|
static_assert(std::is_same<typename std::result_of<F(expr const &, unsigned)>::type, bool>::value,
|
|
|
|
"for_each_fn: return type of m_f is not bool");
|
2013-08-08 02:10:12 +00:00
|
|
|
|
|
|
|
void apply(expr const & e, unsigned offset) {
|
|
|
|
if (is_shared(e)) {
|
|
|
|
expr_cell_offset p(e.raw(), offset);
|
2013-10-25 21:52:08 +00:00
|
|
|
if (!m_visited)
|
|
|
|
m_visited.reset(new expr_cell_offset_set());
|
|
|
|
if (m_visited->find(p) != m_visited->end())
|
2013-08-08 02:10:12 +00:00
|
|
|
return;
|
2013-10-25 21:52:08 +00:00
|
|
|
m_visited->insert(p);
|
2013-08-08 02:10:12 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 21:58:02 +00:00
|
|
|
if (!m_f(e, offset))
|
|
|
|
return;
|
2013-08-08 02:10:12 +00:00
|
|
|
|
|
|
|
switch (e.kind()) {
|
2013-09-13 01:25:38 +00:00
|
|
|
case expr_kind::Constant: case expr_kind::Type: case expr_kind::Value:
|
|
|
|
case expr_kind::Var: case expr_kind::MetaVar:
|
2013-08-08 02:10:12 +00:00
|
|
|
return;
|
|
|
|
case expr_kind::App:
|
|
|
|
std::for_each(begin_args(e), end_args(e), [=](expr const & arg){ return apply(arg, offset); });
|
|
|
|
return;
|
|
|
|
case expr_kind::Eq:
|
|
|
|
apply(eq_lhs(e), offset);
|
|
|
|
apply(eq_rhs(e), offset);
|
|
|
|
return;
|
|
|
|
case expr_kind::Lambda:
|
|
|
|
case expr_kind::Pi:
|
|
|
|
apply(abst_domain(e), offset);
|
|
|
|
apply(abst_body(e), offset + 1);
|
|
|
|
return;
|
|
|
|
case expr_kind::Let:
|
2013-09-06 17:06:26 +00:00
|
|
|
if (let_type(e))
|
|
|
|
apply(let_type(e), offset);
|
2013-08-08 02:10:12 +00:00
|
|
|
apply(let_value(e), offset);
|
|
|
|
apply(let_body(e), offset + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
for_each_fn(F const & f):m_f(f) {}
|
|
|
|
void operator()(expr const & e) { apply(e, 0); }
|
|
|
|
};
|
|
|
|
}
|