expr apply(expr const & a) { check_system("max_sharing"); auto r = m_expr_cache.find(a); if (r != m_expr_cache.end()) return *r; expr res; switch (a.kind()) { case expr_kind::Var: res = a; break; case expr_kind::Constant: res = update_constant(a, map(const_levels(a), [&](level const & l) { return apply(l); })); break; case expr_kind::Sort: res = update_sort(a, apply(sort_level(a))); break; case expr_kind::App: res = update_app(a, apply(app_fn(a)), apply(app_arg(a))); break; case expr_kind::Lambda: case expr_kind::Pi: res = update_binding(a, apply(binding_domain(a)), apply(binding_body(a))); break; case expr_kind::Meta: case expr_kind::Local: res = update_mlocal(a, apply(mlocal_type(a))); break; 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; }} m_expr_cache.insert(res); return res; }
expr apply(expr const & a) { 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; } switch (a.kind()) { case expr_kind::Var: case expr_kind::Constant: case expr_kind::Type: case expr_kind::Value: cache(a); return a; case expr_kind::App: { expr r = update_app(a, [=](expr const & c){ return apply(c); }); cache(r); return r; } case expr_kind::Eq : { expr r = update_eq(a, [=](expr const & l, expr const & r){ return std::make_pair(apply(l), apply(r)); }); cache(r); return r; } case expr_kind::Lambda: case expr_kind::Pi: { expr r = update_abst(a, [=](expr const & t, expr const & b) { return std::make_pair(apply(t), apply(b)); }); cache(r); return r; } case expr_kind::Let: { expr r = update_let(a, [=](expr const & t, expr const & v, expr const & b) { expr new_t = t ? apply(t) : expr(); return std::make_tuple(new_t, apply(v), apply(b)); }); cache(r); return r; } case expr_kind::MetaVar: { expr r = update_metavar(a, [=](meta_entry const & e) -> meta_entry { if (e.is_inst()) return mk_inst(e.s(), apply(e.v())); else return e; }); cache(r); return r; }} lean_unreachable(); }
bool already_processed(expr const & a) const { auto r = m_expr_cache.find(a); return r != m_expr_cache.end() && is_eqp(*r, a); }