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;
|
2013-07-23 15:59:39 +00:00
|
|
|
|
|
|
|
expr_cache m_cache;
|
|
|
|
|
|
|
|
expr apply(expr const & a) {
|
2014-01-21 22:33:32 +00:00
|
|
|
check_system("max_sharing");
|
2013-07-23 15:59:39 +00:00
|
|
|
auto r = m_cache.find(a);
|
2014-04-22 22:38:18 +00:00
|
|
|
if (r != m_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()) {
|
2014-02-18 05:32:24 +00:00
|
|
|
case expr_kind::Constant: case expr_kind::Var:
|
2014-04-25 22:02:52 +00:00
|
|
|
case expr_kind::Sort:
|
2014-02-04 00:52:49 +00:00
|
|
|
res = a;
|
|
|
|
break;
|
2014-04-17 17:52:07 +00:00
|
|
|
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;
|
|
|
|
}}
|
2014-04-24 22:55:48 +00:00
|
|
|
m_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 {
|
|
|
|
auto r = m_cache.find(a);
|
|
|
|
return r != m_cache.end() && is_eqp(*r, a);
|
|
|
|
}
|
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); }
|
|
|
|
void max_sharing_fn::clear() { m_ptr->m_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
|
|
|
}
|