lean2/src/kernel/max_sharing.cpp
Leonardo de Moura 4842ae4fc7 refactor(kernel): store macro arguments in the macro_expr
Before this commit, we "stored" macro arguments using applications.
This representation had some issues. Suppose we use [m a] to denote a macro
application. In the old representation, ([m a] b) and [m a b] would have
the same representation. Another problem is that some procedures (e.g., type inference)
would not have a clean implementation.

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
2014-04-25 15:02:56 -07:00

75 lines
2.4 KiB
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
*/
#include <tuple>
#include <unordered_set>
#include <functional>
#include "util/buffer.h"
#include "util/interrupt.h"
#include "kernel/max_sharing.h"
namespace lean {
/**
\brief Implementation of the functional object for creating expressions with maximally
shared sub-expressions.
*/
struct max_sharing_fn::imp {
typedef typename std::unordered_set<expr, expr_hash, std::equal_to<expr>> expr_cache;
expr_cache m_cache;
expr apply(expr const & a) {
check_system("max_sharing");
auto r = m_cache.find(a);
if (r != m_cache.end())
return *r;
expr res;
switch (a.kind()) {
case expr_kind::Constant: case expr_kind::Var:
case expr_kind::Sort:
res = a;
break;
case expr_kind::App:
res = update_app(a, apply(app_fn(a)), apply(app_arg(a)));
break;
case expr_kind::Lambda: case expr_kind::Pi:
res = update_binder(a, apply(binder_domain(a)), apply(binder_body(a)));
break;
case expr_kind::Let:
res = update_let(a, apply(let_type(a)), apply(let_value(a)), apply(let_body(a)));
break;
case expr_kind::Meta: case expr_kind::Local:
res = update_mlocal(a, apply(mlocal_type(a)));
break;
case expr_kind::Macro: {
buffer<expr> new_args;
for (unsigned i = 0; i < macro_num_args(a); i++)
new_args.push_back(macro_arg(a, i));
res = update_macro(a, new_args.size(), new_args.data());
break;
}}
m_cache.insert(res);
return res;
}
expr operator()(expr const & a) { return apply(a); }
bool already_processed(expr const & a) const {
auto r = m_cache.find(a);
return r != m_cache.end() && is_eqp(*r, a);
}
};
max_sharing_fn::max_sharing_fn():m_ptr(new imp) {}
max_sharing_fn::~max_sharing_fn() {}
expr max_sharing_fn::operator()(expr const & a) { return (*m_ptr)(a); }
void max_sharing_fn::clear() { m_ptr->m_cache.clear(); }
bool max_sharing_fn::already_processed(expr const & a) const { return m_ptr->already_processed(a); }
expr max_sharing(expr const & a) {
return max_sharing_fn::imp()(a);
}
}