2013-07-23 02:31:27 +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
|
|
|
|
*/
|
2013-10-26 18:07:06 +00:00
|
|
|
#include <tuple>
|
2013-07-23 02:31:27 +00:00
|
|
|
#include <unordered_set>
|
2013-11-14 10:31:54 +00:00
|
|
|
#include <functional>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/buffer.h"
|
2014-01-21 22:33:32 +00:00
|
|
|
#include "util/interrupt.h"
|
2014-05-17 18:15:08 +00:00
|
|
|
#include "library/max_sharing.h"
|
2013-07-23 02:31:27 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-08-22 22:41:03 +00:00
|
|
|
/**
|
|
|
|
\brief Implementation of the functional object for creating expressions with maximally
|
|
|
|
shared sub-expressions.
|
|
|
|
*/
|
2013-07-24 19:24:49 +00:00
|
|
|
struct max_sharing_fn::imp {
|
2014-06-28 15:31:09 +00:00
|
|
|
typedef typename std::unordered_set<expr, expr_hash, is_bi_equal_proc> expr_cache;
|
2015-05-06 21:26:12 +00:00
|
|
|
typedef typename std::unordered_set<level, level_hash> level_cache;
|
|
|
|
expr_cache m_expr_cache;
|
|
|
|
level_cache m_lvl_cache;
|
2013-07-23 15:59:39 +00:00
|
|
|
|
2015-05-06 21:26:12 +00:00
|
|
|
level apply(level const & l) {
|
|
|
|
auto r = m_lvl_cache.find(l);
|
|
|
|
if (r != m_lvl_cache.end())
|
|
|
|
return *r;
|
|
|
|
level res;
|
|
|
|
switch (l.kind()) {
|
|
|
|
case level_kind::Zero: case level_kind::Param:
|
|
|
|
case level_kind::Global: case level_kind::Meta:
|
|
|
|
res = l;
|
|
|
|
break;
|
|
|
|
case level_kind::Succ:
|
|
|
|
res = update_succ(l, apply(succ_of(l)));
|
|
|
|
break;
|
|
|
|
case level_kind::Max:
|
|
|
|
res = update_max(l, apply(max_lhs(l)), apply(max_rhs(l)));
|
|
|
|
break;
|
|
|
|
case level_kind::IMax:
|
|
|
|
res = update_max(l, apply(imax_lhs(l)), apply(imax_rhs(l)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_lvl_cache.insert(res);
|
|
|
|
return res;
|
|
|
|
}
|
2013-07-23 15:59:39 +00:00
|
|
|
|
|
|
|
expr apply(expr const & a) {
|
2014-01-21 22:33:32 +00:00
|
|
|
check_system("max_sharing");
|
2015-05-06 21:26:12 +00:00
|
|
|
auto r = m_expr_cache.find(a);
|
|
|
|
if (r != m_expr_cache.end())
|
2013-07-23 15:59:39 +00:00
|
|
|
return *r;
|
2014-02-04 00:52:49 +00:00
|
|
|
expr res;
|
2013-07-23 15:59:39 +00:00
|
|
|
switch (a.kind()) {
|
2015-05-06 21:26:12 +00:00
|
|
|
case expr_kind::Var:
|
2014-02-04 00:52:49 +00:00
|
|
|
res = a;
|
|
|
|
break;
|
2015-05-06 21:26:12 +00:00
|
|
|
case expr_kind::Constant:
|
|
|
|
res = update_constant(a, map(const_levels(a), [&](level const & l) { return apply(l); }));
|
|
|
|
break;
|
|
|
|
case expr_kind::Sort:
|
|
|
|
res = update_sort(a, apply(sort_level(a)));
|
|
|
|
break;
|
|
|
|
case expr_kind::App:
|
2014-02-18 05:32:24 +00:00
|
|
|
res = update_app(a, apply(app_fn(a)), apply(app_arg(a)));
|
2014-02-04 00:52:49 +00:00
|
|
|
break;
|
2014-04-17 17:52:07 +00:00
|
|
|
case expr_kind::Lambda: case expr_kind::Pi:
|
2014-05-17 18:37:27 +00:00
|
|
|
res = update_binding(a, apply(binding_domain(a)), apply(binding_body(a)));
|
2014-02-04 00:52:49 +00:00
|
|
|
break;
|
2014-02-18 05:32:24 +00:00
|
|
|
case expr_kind::Meta: case expr_kind::Local:
|
|
|
|
res = update_mlocal(a, apply(mlocal_type(a)));
|
2014-02-04 00:52:49 +00:00
|
|
|
break;
|
2014-04-25 22:02:52 +00:00
|
|
|
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;
|
|
|
|
}}
|
2015-05-06 21:26:12 +00:00
|
|
|
m_expr_cache.insert(res);
|
2014-02-04 00:52:49 +00:00
|
|
|
return res;
|
2013-07-23 15:59:39 +00:00
|
|
|
}
|
2014-04-23 01:30:07 +00:00
|
|
|
|
2015-05-06 20:57:36 +00:00
|
|
|
expr operator()(expr const & a) {
|
|
|
|
// we must disable approximate/opportunistic hash-consing used in the kernel
|
|
|
|
scoped_expr_caching disable(false);
|
|
|
|
return apply(a);
|
|
|
|
}
|
2014-04-23 01:30:07 +00:00
|
|
|
|
|
|
|
bool already_processed(expr const & a) const {
|
2015-05-06 21:26:12 +00:00
|
|
|
auto r = m_expr_cache.find(a);
|
|
|
|
return r != m_expr_cache.end() && is_eqp(*r, a);
|
2014-04-23 01:30:07 +00:00
|
|
|
}
|
2013-07-23 15:59:39 +00:00
|
|
|
};
|
|
|
|
|
2013-09-17 21:42:44 +00:00
|
|
|
max_sharing_fn::max_sharing_fn():m_ptr(new imp) {}
|
2013-07-24 19:24:49 +00:00
|
|
|
max_sharing_fn::~max_sharing_fn() {}
|
2013-09-17 21:42:44 +00:00
|
|
|
expr max_sharing_fn::operator()(expr const & a) { return (*m_ptr)(a); }
|
2015-05-06 21:26:12 +00:00
|
|
|
void max_sharing_fn::clear() { m_ptr->m_expr_cache.clear(); }
|
2014-04-23 01:30:07 +00:00
|
|
|
bool max_sharing_fn::already_processed(expr const & a) const { return m_ptr->already_processed(a); }
|
2013-07-24 19:24:49 +00:00
|
|
|
|
2013-07-23 15:59:39 +00:00
|
|
|
expr max_sharing(expr const & a) {
|
2014-04-22 22:38:18 +00:00
|
|
|
return max_sharing_fn::imp()(a);
|
2013-07-23 02:31:27 +00:00
|
|
|
}
|
2014-02-18 05:32:24 +00:00
|
|
|
}
|