lean2/src/kernel/max_sharing.cpp

106 lines
3.2 KiB
C++
Raw Normal View History

/*
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>
#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;
void cache(expr const & a) {
a.raw()->set_max_shared();
m_cache.insert(a);
}
optional<expr> apply(optional<expr> const & a) {
if (a)
return some_expr(apply(*a));
else
return a;
}
expr apply(expr const & a) {
check_system("max_sharing");
auto r = m_cache.find(a);
if (r != m_cache.end()) {
lean_assert((*r).raw()->max_shared());
return *r;
}
if (a.raw()->max_shared()) {
m_cache.insert(a);
return a;
}
expr res;
switch (a.kind()) {
case expr_kind::Constant:
res = update_const(a, apply(const_type(a)));
break;
case expr_kind::Var: case expr_kind::Type: case expr_kind::Value:
res = a;
break;
refactor(kernel): add heterogeneous equality back to expr The main motivation is that we will be able to move equalities between universes. For example, suppose we have A : (Type i) B : (Type i) H : @eq (Type j) A B where j > i We didn't find any trick for deducing (@eq (Type i) A B) from H. Before this commit, heterogeneous equality as a constant with type heq : {A B : (Type U)} : A -> B -> Bool So, from H, we would only be able to deduce (@heq (Type j) (Type j) A B) Not being able to move the equality back to a smaller universe is problematic in several cases. I list some instances in the end of the commit message. With this commit, Heterogeneous equality is a special kind of expression. It is not a constant anymore. From H, we can deduce H1 : A == B That is, we are essentially "erasing" the universes when we move to heterogeneous equality. Now, since A and B have (Type i), we can deduce (@eq (Type i) A B) from H1. The proof term is (to_eq (Type i) A B (to_heq (Type j) A B H)) : (@eq (Type i) A B) So, it remains to explain why we need this feature. For example, suppose we want to state the Pi extensionality axiom. axiom hpiext {A A' : (Type U)} {B : A → (Type U)} {B' : A' → (Type U)} : A = A' → (∀ x x', x == x' → B x == B' x') → (∀ x, B x) == (∀ x, B' x) This axiom produces an "inflated" equality at (Type U) when we treat heterogeneous equality as a constant. The conclusion (∀ x, B x) == (∀ x, B' x) is syntax sugar for (@heq (Type U) (Type U) (∀ x : A, B x) (∀ x : A', B' x)) Even if A, A', B, B' live in a much smaller universe. As I described above, it doesn't seem to be a way to move this equality back to a smaller universe. So, if we wanted to keep the heterogeneous equality as a constant, it seems we would have to support axiom schemas. That is, hpiext would be parametrized by the universes where A, A', B and B'. Another possibility would be to have universe polymorphism like Agda. None of the solutions seem attractive. So, we decided to have heterogeneous equality as a special kind of expression. And use the trick above to move equalities back to the right universe. BTW, the parser is not creating the new heterogeneous equalities yet. Moreover, kernel.lean still contains a constant name heq2 that is the heterogeneous equality as a constant. Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
2014-02-07 18:07:08 +00:00
case expr_kind::HEq:
res = update_heq(a, apply(heq_lhs(a)), apply(heq_rhs(a)));
break;
case expr_kind::Pair:
res = update_pair(a, [=](expr const & f, expr const & s, expr const & t) {
return std::make_tuple(apply(f), apply(s), apply(t));
});
break;
case expr_kind::Proj:
res = update_proj(a, apply(proj_arg(a)));
break;
case expr_kind::App:
res = update_app(a, [=](expr const & c) { return apply(c); });
break;
case expr_kind::Sigma:
case expr_kind::Lambda:
case expr_kind::Pi:
res = update_abst(a, [=](expr const & t, expr const & b) { return std::make_pair(apply(t), apply(b)); });
break;
case expr_kind::Let:
res = update_let(a, [=](optional<expr> const & t, expr const & v, expr const & b) {
return std::make_tuple(apply(t), apply(v), apply(b));
});
break;
case expr_kind::MetaVar: {
res = update_metavar(a, [=](local_entry const & e) -> local_entry {
if (e.is_inst())
return mk_inst(e.s(), apply(e.v()));
else
return e;
});
break;
}}
cache(res);
return res;
}
expr operator()(expr const & a) { return apply(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(); }
expr max_sharing(expr const & a) {
if (a.raw()->max_shared())
return a;
else
return max_sharing_fn::imp()(a);
}
} // namespace lean