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-03 04:03:42 +00:00
|
|
|
#include "kernel/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 {
|
2013-11-14 10:31:54 +00:00
|
|
|
typedef typename std::unordered_set<expr, expr_hash, std::equal_to<expr>> expr_cache;
|
2013-07-23 15:59:39 +00:00
|
|
|
|
|
|
|
expr_cache m_cache;
|
|
|
|
|
|
|
|
void cache(expr const & a) {
|
|
|
|
a.raw()->set_max_shared();
|
|
|
|
m_cache.insert(a);
|
2013-07-23 02:31:27 +00:00
|
|
|
}
|
2013-07-23 15:59:39 +00:00
|
|
|
|
2013-12-08 07:21:07 +00:00
|
|
|
optional<expr> apply(optional<expr> const & a) {
|
|
|
|
if (a)
|
2013-12-08 18:34:38 +00:00
|
|
|
return some_expr(apply(*a));
|
2013-12-08 07:21:07 +00:00
|
|
|
else
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-07-23 15:59:39 +00:00
|
|
|
expr apply(expr const & a) {
|
|
|
|
auto r = m_cache.find(a);
|
|
|
|
if (r != m_cache.end()) {
|
|
|
|
lean_assert((*r).raw()->max_shared());
|
|
|
|
return *r;
|
2013-07-23 02:31:27 +00:00
|
|
|
}
|
2013-07-23 16:18:18 +00:00
|
|
|
if (a.raw()->max_shared()) {
|
|
|
|
m_cache.insert(a);
|
|
|
|
return a;
|
|
|
|
}
|
2013-07-23 15:59:39 +00:00
|
|
|
switch (a.kind()) {
|
2013-12-08 07:21:07 +00:00
|
|
|
case expr_kind::Constant: {
|
|
|
|
expr r = update_const(a, apply(const_type(a)));
|
|
|
|
cache(r);
|
|
|
|
return r;
|
|
|
|
}
|
2013-11-19 19:21:45 +00:00
|
|
|
case expr_kind::Var: case expr_kind::Type: case expr_kind::Value:
|
2013-07-23 15:59:39 +00:00
|
|
|
cache(a);
|
2013-07-23 02:31:27 +00:00
|
|
|
return a;
|
2013-07-23 15:59:39 +00:00
|
|
|
case expr_kind::App: {
|
2013-12-08 07:21:07 +00:00
|
|
|
expr r = update_app(a, [=](expr const & c) { return apply(c); });
|
2013-07-30 04:28:22 +00:00
|
|
|
cache(r);
|
|
|
|
return r;
|
2013-07-23 02:31:27 +00:00
|
|
|
}
|
2013-07-23 15:59:39 +00:00
|
|
|
case expr_kind::Lambda:
|
|
|
|
case expr_kind::Pi: {
|
2013-07-30 04:28:22 +00:00
|
|
|
expr r = update_abst(a, [=](expr const & t, expr const & b) { return std::make_pair(apply(t), apply(b)); });
|
|
|
|
cache(r);
|
|
|
|
return r;
|
2013-08-04 16:37:52 +00:00
|
|
|
}
|
|
|
|
case expr_kind::Let: {
|
2013-12-08 07:21:07 +00:00
|
|
|
expr r = update_let(a, [=](optional<expr> const & t, expr const & v, expr const & b) {
|
|
|
|
return std::make_tuple(apply(t), apply(v), apply(b));
|
2013-09-06 17:06:26 +00:00
|
|
|
});
|
2013-08-04 16:37:52 +00:00
|
|
|
cache(r);
|
|
|
|
return r;
|
2013-09-13 01:25:38 +00:00
|
|
|
}
|
|
|
|
case expr_kind::MetaVar: {
|
2013-09-27 01:24:45 +00:00
|
|
|
expr r = update_metavar(a, [=](local_entry const & e) -> local_entry {
|
2013-09-17 02:21:40 +00:00
|
|
|
if (e.is_inst())
|
|
|
|
return mk_inst(e.s(), apply(e.v()));
|
2013-09-13 01:25:38 +00:00
|
|
|
else
|
|
|
|
return e;
|
|
|
|
});
|
|
|
|
cache(r);
|
|
|
|
return r;
|
2013-07-23 15:59:39 +00:00
|
|
|
}}
|
2013-11-11 17:19:38 +00:00
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2013-07-23 15:59:39 +00:00
|
|
|
}
|
2013-07-24 07:45:38 +00:00
|
|
|
expr operator()(expr const & a) { return apply(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(); }
|
2013-07-24 19:24:49 +00:00
|
|
|
|
2013-07-23 15:59:39 +00:00
|
|
|
expr max_sharing(expr const & a) {
|
2013-07-24 07:45:38 +00:00
|
|
|
if (a.raw()->max_shared())
|
2013-07-23 15:59:39 +00:00
|
|
|
return a;
|
2013-07-24 07:45:38 +00:00
|
|
|
else
|
2013-07-24 19:24:49 +00:00
|
|
|
return max_sharing_fn::imp()(a);
|
2013-07-23 02:31:27 +00:00
|
|
|
}
|
|
|
|
} // namespace lean
|