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-10-26 18:07:06 +00:00
|
|
|
#include <tuple>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/buffer.h"
|
2013-12-07 22:59:21 +00:00
|
|
|
#include "util/interrupt.h"
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "kernel/expr.h"
|
|
|
|
#include "kernel/expr_maps.h"
|
|
|
|
|
2013-07-24 07:32:01 +00:00
|
|
|
namespace lean {
|
2013-08-28 17:47:19 +00:00
|
|
|
/**
|
|
|
|
\brief Default replace_fn postprocessor functional object. It is a
|
|
|
|
do-nothing object.
|
|
|
|
*/
|
|
|
|
class default_replace_postprocessor {
|
|
|
|
public:
|
2013-09-20 04:26:01 +00:00
|
|
|
void operator()(expr const &, expr const &) {}
|
2013-08-28 17:47:19 +00:00
|
|
|
};
|
|
|
|
|
2013-07-24 07:32:01 +00:00
|
|
|
/**
|
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
|
2014-04-17 19:41:06 +00:00
|
|
|
expr const &, unsigned -> optional(expr)
|
2013-07-24 07:32:01 +00:00
|
|
|
|
2013-07-26 19:27:55 +00:00
|
|
|
F is invoked for each subexpression \c s of the input expression e.
|
2013-09-17 18:09:59 +00:00
|
|
|
In a call <tt>F(s, n)</tt>, n is the scope level, i.e., the number of
|
2014-04-17 19:41:06 +00:00
|
|
|
bindings operators that enclosing \c s. The replaces only visits children of \c e
|
|
|
|
if F return none_expr
|
2013-08-28 17:47:19 +00:00
|
|
|
|
|
|
|
P is a "post-processing" functional object that is applied to each
|
|
|
|
pair (old, new)
|
2013-07-24 07:32:01 +00:00
|
|
|
*/
|
2013-07-24 07:45:38 +00:00
|
|
|
class replace_fn {
|
2013-12-18 00:35:39 +00:00
|
|
|
struct frame {
|
|
|
|
expr m_expr;
|
|
|
|
unsigned m_offset;
|
|
|
|
bool m_shared;
|
|
|
|
unsigned m_index;
|
|
|
|
frame(expr const & e, unsigned o, bool s):m_expr(e), m_offset(o), m_shared(s), m_index(0) {}
|
|
|
|
};
|
|
|
|
typedef buffer<frame> frame_stack;
|
|
|
|
typedef buffer<expr> result_stack;
|
|
|
|
|
2014-03-01 00:57:25 +00:00
|
|
|
expr_cell_offset_map<expr> m_cache;
|
|
|
|
std::function<optional<expr>(expr const &, unsigned)> m_f;
|
|
|
|
std::function<void(expr const &, expr const &)> m_post;
|
|
|
|
frame_stack m_fs;
|
|
|
|
result_stack m_rs;
|
2013-07-24 07:32:01 +00:00
|
|
|
|
2014-02-16 19:23:25 +00:00
|
|
|
void save_result(expr const & e, expr const & r, unsigned offset, bool shared);
|
|
|
|
bool visit(expr const & e, unsigned offset);
|
|
|
|
bool check_index(frame & f, unsigned idx);
|
|
|
|
expr const & rs(int i);
|
|
|
|
void pop_rs(unsigned num);
|
2013-07-24 07:32:01 +00:00
|
|
|
|
|
|
|
public:
|
2014-02-16 19:23:25 +00:00
|
|
|
template<typename F, typename P = default_replace_postprocessor>
|
2013-08-28 17:47:19 +00:00
|
|
|
replace_fn(F const & f, P const & p = P()):
|
2014-02-16 19:23:25 +00:00
|
|
|
m_f(f), m_post(p) {}
|
|
|
|
expr operator()(expr const & e);
|
|
|
|
void clear();
|
2013-07-24 07:32:01 +00:00
|
|
|
};
|
2013-12-18 02:31:59 +00:00
|
|
|
|
2014-02-16 19:23:25 +00:00
|
|
|
template<typename F> expr replace(expr const & e, F const & f) {
|
|
|
|
return replace_fn(f)(e);
|
2013-12-18 02:31:59 +00:00
|
|
|
}
|
|
|
|
|
2014-02-16 19:23:25 +00:00
|
|
|
template<typename F, typename P> expr replace(expr const & e, F const & f, P const & p) {
|
|
|
|
return replace_fn(f, p)(e);
|
2013-12-18 02:31:59 +00:00
|
|
|
}
|
2013-07-24 07:32:01 +00:00
|
|
|
}
|