expr collect(expr const & e) { return replace(e, [&](expr const & e, unsigned) { if (is_metavar(e)) { name const & id = mlocal_name(e); if (auto r = m_meta_to_param.find(id)) { return some_expr(*r); } else { expr type = m_ctx.infer(e); expr x = m_ctx.push_local("_x", type); m_meta_to_param.insert(id, x); m_meta_to_param_inv.insert(mlocal_name(x), e); m_params.push_back(x); return some_expr(x); } } else if (is_local(e)) { name const & id = mlocal_name(e); if (!m_found_local.contains(id)) { m_found_local.insert(id); m_params.push_back(e); } } else if (is_sort(e)) { return some_expr(update_sort(e, collect(sort_level(e)))); } else if (is_constant(e)) { return some_expr(update_constant(e, collect(const_levels(e)))); } return none_expr(); }); }
// Collect all universe global levels occurring in l into ls static void collect_global_levels(level const & l, name_set & ls) { for_each(l, [&](level const & l) { if (is_global(l)) ls.insert(global_id(l)); return true; }); }
level collect(level const & l) { return replace(l, [&](level const & l) { if (is_meta(l)) { name const & id = meta_id(l); if (auto r = m_univ_meta_to_param.find(id)) { return some_level(*r); } else { name n = m_prefix.append_after(m_next_idx); m_next_idx++; level new_r = mk_param_univ(n); m_univ_meta_to_param.insert(id, new_r); m_univ_meta_to_param_inv.insert(n, l); m_level_params.push_back(n); return some_level(new_r); } } else if (is_param(l)) { name const & id = param_id(l); if (!m_found_univ_params.contains(id)) { m_found_univ_params.insert(id); m_level_params.push_back(id); } } return none_level(); }); }
void collect_univ_params_core(level const & l, name_set & r) { for_each(l, [&](level const & l) { if (!has_param(l)) return false; if (is_param(l)) r.insert(param_id(l)); return true; }); }
// Return true iff lhs is of the form (B (x : ?m1), ?m2) or (B (x : ?m1), ?m2 x), // where B is lambda or Pi static bool is_valid_congr_rule_binding_lhs(expr const & lhs, name_set & found_mvars) { lean_assert(is_binding(lhs)); expr const & d = binding_domain(lhs); expr const & b = binding_body(lhs); if (!is_metavar(d)) return false; if (is_metavar(b) && b != d) { found_mvars.insert(mlocal_name(b)); found_mvars.insert(mlocal_name(d)); return true; } if (is_app(b) && is_metavar(app_fn(b)) && is_var(app_arg(b), 0) && app_fn(b) != d) { found_mvars.insert(mlocal_name(app_fn(b))); found_mvars.insert(mlocal_name(d)); return true; } return false; }
// Check whether rhs is of the form (mvar l_1 ... l_n) where mvar is a metavariable, // and l_i's are local constants, and mvar does not occur in found_mvars. // If it is return true and update found_mvars static bool is_valid_congr_hyp_rhs(expr const & rhs, name_set & found_mvars) { buffer<expr> rhs_args; expr const & rhs_fn = get_app_args(rhs, rhs_args); if (!is_metavar(rhs_fn) || found_mvars.contains(mlocal_name(rhs_fn))) return false; for (expr const & arg : rhs_args) if (!is_local(arg)) return false; found_mvars.insert(mlocal_name(rhs_fn)); return true; }
name_set operator()() { name_set A; name_set Fs = m_relevant; // unsigned i = 1; while (true) { // std::cout << "#" << i << ", p: " << m_p << "\n"; name_set Rel; Fs.for_each([&](name const & F) { name_set used_by = get_used_by_set(m_env, F); used_by.for_each([&](name const & T) { declaration const & T_decl = m_env.get(T); if (A.contains(T)) return; // T is already in the result set if (!T_decl.is_theorem() && !T_decl.is_axiom()) return; // we only care about axioms and theorems if (ignore_T(T)) return; // we ignore private decls double M = get_thm_score(T); // std::cout << T << " : " << M << "\n"; if (M < m_p) return; // score is to low Rel.insert(T); A.insert(T); }); }); if (Rel.empty()) break; // include symbols of new theorems in m_relevant Fs = name_set(); // reset Fs Rel.for_each([&](name const & T) { name_set uses = get_use_set(m_env, T); uses.for_each([&](name const & F) { declaration const & F_decl = m_env.get(F); if (F_decl.is_theorem() || F_decl.is_axiom()) return; // we ignore theorems occurring in types if (ignore_F(F)) return; // if (!m_relevant.contains(F)) // std::cout << "new relevant: " << F << "\n"; m_relevant.insert(F); Fs.insert(F); }); }); m_p = m_p + (1.0 - m_p) / m_c; } return A; }
bool substitution::occurs_expr_core(name const & m, expr const & e, name_set & visited) const { bool found = false; for_each(e, [&](expr const & e, unsigned) { if (found || !has_expr_metavar(e)) return false; if (is_metavar(e)) { name const & n = mlocal_name(e); if (n == m) found = true; auto s = get_expr(e); if (!s || visited.contains(n)) return false; // do not visit type visited.insert(n); if (s && occurs_expr_core(m, *s, visited)) found = true; return false; // do not visit type } if (is_local(e)) return false; // do not visit type return true; }); return found; }
void add_congr(environment const & env, name const & n) { add_congr_core(env, m_sets, n); m_congr_names.insert(n); }
void add_simp(environment const & env, name const & cname) { type_checker tc(env); m_sets = add_core(tc, m_sets, cname); m_simp_names.insert(cname); }