2013-07-24 07:32:01 +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-07-24 21:41:03 +00:00
|
|
|
#include "buffer.h"
|
2013-07-24 07:32:01 +00:00
|
|
|
#include "expr.h"
|
|
|
|
#include "maps.h"
|
|
|
|
namespace lean {
|
|
|
|
/**
|
2013-07-26 18:43:53 +00:00
|
|
|
\brief Functional for applying <tt>F</tt> to the subexpressions of a given expression.
|
2013-07-24 07:32:01 +00:00
|
|
|
|
2013-07-26 19:27:55 +00:00
|
|
|
The signature of \c F is
|
2013-07-24 07:32:01 +00:00
|
|
|
unsigned, expr -> expr
|
|
|
|
|
2013-07-26 19:27:55 +00:00
|
|
|
F is invoked for each subexpression \c s of the input expression e.
|
|
|
|
In a call <tt>F(n, s)</tt>, n is the scope level, i.e., the number of
|
|
|
|
bindings operators that enclosing \c s.
|
2013-07-24 07:32:01 +00:00
|
|
|
*/
|
|
|
|
template<typename F>
|
2013-07-24 07:45:38 +00:00
|
|
|
class replace_fn {
|
2013-07-24 07:32:01 +00:00
|
|
|
expr_cell_offset_map<expr> m_cache;
|
|
|
|
F m_f;
|
|
|
|
|
|
|
|
expr apply(expr const & e, unsigned offset) {
|
2013-07-30 03:59:15 +00:00
|
|
|
bool sh = false;
|
2013-07-24 07:32:01 +00:00
|
|
|
if (is_shared(e)) {
|
|
|
|
expr_cell_offset p(e.raw(), offset);
|
|
|
|
auto it = m_cache.find(p);
|
|
|
|
if (it != m_cache.end())
|
|
|
|
return it->second;
|
2013-07-30 03:59:15 +00:00
|
|
|
sh = true;
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr r = m_f(e, offset);
|
|
|
|
if (eqp(e, r)) {
|
|
|
|
switch (e.kind()) {
|
2013-07-30 02:49:34 +00:00
|
|
|
case expr_kind::Type: case expr_kind::Numeral: case expr_kind::Constant: case expr_kind::Var:
|
2013-07-24 07:32:01 +00:00
|
|
|
break;
|
2013-07-30 04:28:22 +00:00
|
|
|
case expr_kind::App:
|
|
|
|
r = update_app(e, [=](expr const & c) { return apply(c, offset); });
|
2013-07-24 07:32:01 +00:00
|
|
|
break;
|
|
|
|
case expr_kind::Lambda:
|
2013-07-30 04:28:22 +00:00
|
|
|
case expr_kind::Pi:
|
|
|
|
r = update_abst(e, [=](expr const & t, expr const & b) { return std::make_pair(apply(t, offset), apply(b, offset+1)); });
|
2013-07-24 07:32:01 +00:00
|
|
|
break;
|
2013-07-30 04:28:22 +00:00
|
|
|
}
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 03:59:15 +00:00
|
|
|
if (sh)
|
2013-07-24 07:32:01 +00:00
|
|
|
m_cache.insert(std::make_pair(expr_cell_offset(e.raw(), offset), r));
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2013-07-24 07:45:38 +00:00
|
|
|
replace_fn(F const & f):
|
2013-07-24 07:32:01 +00:00
|
|
|
m_f(f) {
|
|
|
|
}
|
|
|
|
|
|
|
|
expr operator()(expr const & e) {
|
|
|
|
return apply(e, 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|