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
|
|
|
|
*/
|
|
|
|
#include <unordered_set>
|
2013-07-24 21:41:03 +00:00
|
|
|
#include "buffer.h"
|
2013-07-24 19:24:49 +00:00
|
|
|
#include "max_sharing.h"
|
2013-07-23 02:31:27 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-07-23 15:59:39 +00:00
|
|
|
|
2013-07-24 19:24:49 +00:00
|
|
|
struct max_sharing_fn::imp {
|
2013-07-23 15:59:39 +00:00
|
|
|
struct expr_struct_eq { unsigned operator()(expr const & e1, expr const & e2) const { return e1 == e2; }};
|
|
|
|
typedef typename std::unordered_set<expr, expr_hash, expr_struct_eq> expr_cache;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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-07-30 02:49:34 +00:00
|
|
|
case expr_kind::Var: case expr_kind::Constant: case expr_kind::Type: case expr_kind::Numeral:
|
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-07-24 21:41:03 +00:00
|
|
|
buffer<expr> new_args;
|
2013-07-23 15:59:39 +00:00
|
|
|
bool modified = false;
|
2013-07-23 18:47:36 +00:00
|
|
|
for (expr const & old_arg : args(a)) {
|
2013-07-23 15:59:39 +00:00
|
|
|
new_args.push_back(apply(old_arg));
|
|
|
|
if (!eqp(old_arg, new_args.back()))
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
if (!modified) {
|
|
|
|
cache(a);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
expr r = app(new_args.size(), new_args.data());
|
|
|
|
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-23 18:47:36 +00:00
|
|
|
expr const & old_t = abst_type(a);
|
2013-07-24 04:49:19 +00:00
|
|
|
expr const & old_b = abst_body(a);
|
2013-07-23 15:59:39 +00:00
|
|
|
expr t = apply(old_t);
|
2013-07-24 04:49:19 +00:00
|
|
|
expr b = apply(old_b);
|
|
|
|
if (!eqp(t, old_t) || !eqp(b, old_b)) {
|
2013-07-23 18:47:36 +00:00
|
|
|
name const & n = abst_name(a);
|
2013-07-24 04:49:19 +00:00
|
|
|
expr r = is_pi(a) ? pi(n, t, b) : lambda(n, t, b);
|
2013-07-23 15:59:39 +00:00
|
|
|
cache(r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cache(a);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
lean_unreachable();
|
|
|
|
return a;
|
|
|
|
}
|
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-07-24 19:24:49 +00:00
|
|
|
|
|
|
|
max_sharing_fn::max_sharing_fn():m_imp(new imp) {}
|
|
|
|
max_sharing_fn::~max_sharing_fn() {}
|
|
|
|
expr max_sharing_fn::operator()(expr const & a) { return (*m_imp)(a); }
|
|
|
|
void max_sharing_fn::clear() { m_imp->m_cache.clear(); }
|
|
|
|
|
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
|
|
|
}
|
2013-07-23 15:59:39 +00:00
|
|
|
|
2013-07-23 02:31:27 +00:00
|
|
|
} // namespace lean
|