feat(kernel/expr): maximize sharing before serializing

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2014-01-02 20:03:42 -08:00
parent 2d5800ace4
commit 92c7145d7f
14 changed files with 22 additions and 16 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -3,6 +3,6 @@ add_library(kernel expr.cpp free_vars.cpp abstract.cpp instantiate.cpp
type_checker.cpp builtin.cpp occurs.cpp metavar.cpp type_checker.cpp builtin.cpp occurs.cpp metavar.cpp
justification.cpp unification_constraint.cpp kernel_exception.cpp justification.cpp unification_constraint.cpp kernel_exception.cpp
type_checker_justification.cpp pos_info_provider.cpp type_checker_justification.cpp pos_info_provider.cpp
replace_visitor.cpp update_expr.cpp io_state.cpp) replace_visitor.cpp update_expr.cpp io_state.cpp max_sharing.cpp)
target_link_libraries(kernel ${LEAN_LIBS}) target_link_libraries(kernel ${LEAN_LIBS})

View file

@ -15,6 +15,7 @@ Author: Leonardo de Moura
#include "kernel/free_vars.h" #include "kernel/free_vars.h"
#include "kernel/expr_eq.h" #include "kernel/expr_eq.h"
#include "kernel/metavar.h" #include "kernel/metavar.h"
#include "kernel/max_sharing.h"
namespace lean { namespace lean {
static expr g_dummy(mk_var(0)); static expr g_dummy(mk_var(0));
@ -345,18 +346,19 @@ static_assert(is_small(expr_kind::Var) && is_small(expr_kind::Constant) && is_sm
class expr_serializer : public object_serializer<expr, expr_hash_alloc, expr_eqp> { class expr_serializer : public object_serializer<expr, expr_hash_alloc, expr_eqp> {
typedef object_serializer<expr, expr_hash_alloc, expr_eqp> super; typedef object_serializer<expr, expr_hash_alloc, expr_eqp> super;
public: max_sharing_fn m_max_sharing_fn;
void write(optional<expr> const & a) {
void write_core(optional<expr> const & a) {
serializer & s = get_owner(); serializer & s = get_owner();
if (a) { if (a) {
s << true; s << true;
write(*a); write_core(*a);
} else { } else {
s << false; s << false;
} }
} }
void write(expr const & a) { void write_core(expr const & a) {
auto k = a.kind(); auto k = a.kind();
char kc; char kc;
if (k == expr_kind::App && num_args(a) < g_small_app_num_args) { if (k == expr_kind::App && num_args(a) < g_small_app_num_args) {
@ -369,27 +371,31 @@ public:
if (kc >= static_cast<char>(g_first_app_size_kind)) { if (kc >= static_cast<char>(g_first_app_size_kind)) {
// compressed application // compressed application
for (unsigned i = 0; i < num_args(a); i++) for (unsigned i = 0; i < num_args(a); i++)
write(arg(a, i)); write_core(arg(a, i));
return; return;
} }
switch (k) { switch (k) {
case expr_kind::Var: s << var_idx(a); break; case expr_kind::Var: s << var_idx(a); break;
case expr_kind::Constant: s << const_name(a); write(const_type(a)); break; case expr_kind::Constant: s << const_name(a); write_core(const_type(a)); break;
case expr_kind::Type: s << ty_level(a); break; case expr_kind::Type: s << ty_level(a); break;
case expr_kind::Value: to_value(a).write(s); break; case expr_kind::Value: to_value(a).write(s); break;
case expr_kind::App: case expr_kind::App:
s << num_args(a); s << num_args(a);
for (unsigned i = 0; i < num_args(a); i++) for (unsigned i = 0; i < num_args(a); i++)
write(arg(a, i)); write_core(arg(a, i));
break; break;
case expr_kind::Eq: write(eq_lhs(a)); write(eq_rhs(a)); break; case expr_kind::Eq: write_core(eq_lhs(a)); write_core(eq_rhs(a)); break;
case expr_kind::Lambda: case expr_kind::Lambda:
case expr_kind::Pi: s << abst_name(a); write(abst_domain(a)); write(abst_body(a)); break; case expr_kind::Pi: s << abst_name(a); write_core(abst_domain(a)); write_core(abst_body(a)); break;
case expr_kind::Let: s << let_name(a); write(let_type(a)); write(let_value(a)); write(let_body(a)); break; case expr_kind::Let: s << let_name(a); write_core(let_type(a)); write_core(let_value(a)); write_core(let_body(a)); break;
case expr_kind::MetaVar: s << metavar_name(a) << metavar_lctx(a); break; case expr_kind::MetaVar: s << metavar_name(a) << metavar_lctx(a); break;
} }
}); });
} }
public:
void write(expr const & a) {
write_core(m_max_sharing_fn(a));
}
}; };
class expr_deserializer : public object_deserializer<expr> { class expr_deserializer : public object_deserializer<expr> {

View file

@ -8,7 +8,7 @@ Author: Leonardo de Moura
#include <unordered_set> #include <unordered_set>
#include <functional> #include <functional>
#include "util/buffer.h" #include "util/buffer.h"
#include "library/max_sharing.h" #include "kernel/max_sharing.h"
namespace lean { namespace lean {
/** /**

View file

@ -1,4 +1,4 @@
add_library(library kernel_bindings.cpp deep_copy.cpp max_sharing.cpp add_library(library kernel_bindings.cpp deep_copy.cpp
context_to_lambda.cpp placeholder.cpp expr_lt.cpp substitution.cpp context_to_lambda.cpp placeholder.cpp expr_lt.cpp substitution.cpp
fo_unify.cpp bin_op.cpp eq_heq.cpp io_state_stream.cpp printer.cpp) fo_unify.cpp bin_op.cpp eq_heq.cpp io_state_stream.cpp printer.cpp)

View file

@ -14,8 +14,8 @@ Author: Leonardo de Moura
#include "kernel/free_vars.h" #include "kernel/free_vars.h"
#include "kernel/abstract.h" #include "kernel/abstract.h"
#include "kernel/instantiate.h" #include "kernel/instantiate.h"
#include "kernel/max_sharing.h"
#include "library/printer.h" #include "library/printer.h"
#include "library/max_sharing.h"
#include "library/bin_op.h" #include "library/bin_op.h"
#include "library/arith/arith.h" #include "library/arith/arith.h"
using namespace lean; using namespace lean;

View file

@ -11,8 +11,8 @@ Author: Leonardo de Moura
#include "kernel/free_vars.h" #include "kernel/free_vars.h"
#include "kernel/abstract.h" #include "kernel/abstract.h"
#include "kernel/normalizer.h" #include "kernel/normalizer.h"
#include "kernel/max_sharing.h"
#include "library/io_state_stream.h" #include "library/io_state_stream.h"
#include "library/max_sharing.h"
#include "library/deep_copy.h" #include "library/deep_copy.h"
#include "library/arith/arith.h" #include "library/arith/arith.h"
#include "frontends/lean/frontend.h" #include "frontends/lean/frontend.h"

View file

@ -7,8 +7,8 @@ Author: Leonardo de Moura
#include <iostream> #include <iostream>
#include "util/test.h" #include "util/test.h"
#include "kernel/abstract.h" #include "kernel/abstract.h"
#include "kernel/max_sharing.h"
#include "library/printer.h" #include "library/printer.h"
#include "library/max_sharing.h"
using namespace lean; using namespace lean;
static void tst1() { static void tst1() {