refactor(library/local_context): avoid hack in local_context

This commit is contained in:
Leonardo de Moura 2015-06-18 15:41:00 -07:00
parent 694eef7f6a
commit 70fc05294b
5 changed files with 33 additions and 22 deletions

View file

@ -177,15 +177,15 @@ finset.induction_on s
take a₁ a₂, assume H3 H4 H5,
H1 (!mem_insert_of_mem H3) (!mem_insert_of_mem H4) H5,
assert H6 : card ( (x : A) ∈ s', f x) = ∑ (x : A) ∈ s', card (f x), from IH H2,
have H7 : ∀ x, x ∈ s' → f a ∩ f x = ∅, from
assert H7 : ∀ x, x ∈ s' → f a ∩ f x = ∅, from
take x, assume xs',
have anex : a ≠ x, from assume aex, (eq.subst aex H) xs',
H1 !mem_insert (!mem_insert_of_mem xs') anex,
assert H8 : f a ∩ ( (x : A) ∈ s', f x) = ∅, from
calc
f a ∩ ( (x : A) ∈ s', f x) = ( (x : A) ∈ s', f a ∩ f x) : inter_Union
... = ( (x : A) ∈ s', ∅) : Union_ext H7
... = ∅ : Union_empty',
f a ∩ ( (x : A) ∈ s', f x) = ( (x : A) ∈ s', f a ∩ f x) : by rewrite inter_Union
... = ( (x : A) ∈ s', ∅) : by rewrite [Union_ext H7]
... = ∅ : by rewrite Union_empty',
by rewrite [Union_insert, Sum_insert_of_not_mem _ H,
card_union_of_disjoint H8, H6])
end deceqB

View file

@ -43,6 +43,7 @@ expr abstract_locals(expr const & e, unsigned n, expr const * subst) {
if (mlocal_name(subst[i]) == mlocal_name(m))
return some_expr(mk_var(offset + n - i - 1, m.get_tag()));
}
return some_expr(m);
}
return none_expr();
});

View file

@ -448,15 +448,18 @@ class equation_compiler_fn {
// are in local_ctx or m_global_context
bool check_ctx(expr const & e, list<expr> const & context, list<expr> const & local_context) const {
for_each(e, [&](expr const & e, unsigned) {
if (is_local(e) &&
!(contains_local(e, local_context) ||
contains_local(e, context) ||
contains_local(e, m_additional_context) ||
contains_local(e, m_global_context) ||
contains_local(e, m_fns))) {
lean_unreachable();
return false;
if (is_local(e)) {
if (!(contains_local(e, local_context) ||
contains_local(e, context) ||
contains_local(e, m_additional_context) ||
contains_local(e, m_global_context) ||
contains_local(e, m_fns))) {
lean_unreachable();
}
return false; // do not visit type
}
if (is_metavar(e))
return false; // do not visit type
return true;
});
return true;

View file

@ -17,7 +17,7 @@ namespace lean {
return
t[#n, ..., #0]
*/
expr abstract_locals(expr const & e, list<expr> const & locals) {
expr local_context::abstract_locals(expr const & e, list<expr> const & locals) {
lean_assert(std::all_of(locals.begin(), locals.end(), [](expr const & e) { return closed(e) && is_local(e); }));
if (!has_local(e))
return e;
@ -31,11 +31,16 @@ expr abstract_locals(expr const & e, list<expr> const & locals) {
return some_expr(copy_tag(m, mk_var(offset + i)));
i++;
}
return some_expr(m);
}
return none_expr();
});
}
auto local_context::to_local_decl(expr const & l, list<expr> const & ctx) -> local_decl {
return local_decl(local_pp_name(l), abstract_locals(mlocal_type(l), ctx), local_info(l));
}
local_context::local_context() {}
local_context::local_context(list<expr> const & ctx) {
set_ctx(ctx);
@ -43,20 +48,19 @@ local_context::local_context(list<expr> const & ctx) {
void local_context::set_ctx(list<expr> const & ctx) {
m_ctx = ctx;
buffer<expr> tmp;
buffer<local_decl> tmp;
list<expr> it = ctx;
while (it) {
tmp.push_back(abstract_locals(head(it), tail(it)));
tmp.push_back(to_local_decl(head(it), tail(it)));
it = tail(it);
}
m_ctx_abstracted = to_list(tmp.begin(), tmp.end());
lean_assert(std::all_of(m_ctx_abstracted.begin(), m_ctx_abstracted.end(), [](expr const & e) { return is_local(e); }));
}
expr local_context::pi_abstract_context(expr e, tag g) const {
e = abstract_locals(e, m_ctx);
for (expr const & l : m_ctx_abstracted)
e = mk_pi(local_pp_name(l), mlocal_type(l), e, local_info(l), g);
for (local_decl const & l : m_ctx_abstracted)
e = mk_pi(std::get<0>(l), std::get<1>(l), e, std::get<2>(l), g);
return e;
}
@ -99,10 +103,9 @@ expr local_context::mk_meta(name_generator & ngen, optional<name> const & suffix
void local_context::add_local(expr const & l) {
lean_assert(is_local(l));
m_ctx_abstracted = cons(abstract_locals(l, m_ctx), m_ctx_abstracted);
m_ctx_abstracted = cons(to_local_decl(l, m_ctx), m_ctx_abstracted);
m_ctx = cons(l, m_ctx);
lean_assert(length(m_ctx) == length(m_ctx_abstracted));
lean_assert(is_local(head(m_ctx_abstracted)));
}
list<expr> const & local_context::get_data() const {

View file

@ -13,8 +13,12 @@ namespace lean {
and creating metavariables in the scope of the local context efficiently
*/
class local_context {
list<expr> m_ctx; // current local context: a list of local constants
list<expr> m_ctx_abstracted; // m_ctx where elements have been abstracted
list<expr> m_ctx; // current local context: a list of local constants
typedef std::tuple<name, expr, binder_info> local_decl;
list<local_decl> m_ctx_abstracted; // m_ctx where elements have been abstracted
static expr abstract_locals(expr const & e, list<expr> const & locals);
// convert a local constant into a local_decl
static local_decl to_local_decl(expr const & l, list<expr> const & ctx);
public:
local_context();
local_context(list<expr> const & ctx);