expr mk_codomain(expr const & codomain, expr p, buffer<expr> const & locals, unsigned n) { buffer<expr> terms; for (unsigned i = 0; i < n - 1; i++) { terms.push_back(mk_app(m_ctx, get_psigma_fst_name(), p)); p = mk_app(m_ctx, get_psigma_snd_name(), p); } terms.push_back(p); return replace_locals(codomain, locals, terms); }
pair<environment, expr> operator()(name const & c, expr const & type, expr const & value, bool is_lemma, optional<bool> const & is_meta) { lean_assert(!is_lemma || is_meta); lean_assert(!is_lemma || *is_meta == false); expr new_type = collect(m_ctx.instantiate_mvars(type)); expr new_value = collect(m_ctx.instantiate_mvars(value)); buffer<expr> norm_params; collect_and_normalize_dependencies(norm_params); new_type = replace_locals(new_type, m_params, norm_params); new_value = replace_locals(new_value, m_params, norm_params); expr def_type = m_ctx.mk_pi(norm_params, new_type); expr def_value = m_ctx.mk_lambda(norm_params, new_value); environment const & env = m_ctx.env(); declaration d; if (is_lemma) { d = mk_theorem(c, to_list(m_level_params), def_type, def_value); } else if (is_meta) { bool use_self_opt = true; d = mk_definition(env, c, to_list(m_level_params), def_type, def_value, use_self_opt, !*is_meta); } else { bool use_self_opt = true; d = mk_definition_inferring_trusted(env, c, to_list(m_level_params), def_type, def_value, use_self_opt); } environment new_env = module::add(env, check(env, d, true)); buffer<level> ls; for (name const & n : m_level_params) { if (level const * l = m_univ_meta_to_param_inv.find(n)) ls.push_back(*l); else ls.push_back(mk_param_univ(n)); } buffer<expr> ps; for (expr const & x : m_params) { if (expr const * m = m_meta_to_param_inv.find(mlocal_name(x))) ps.push_back(*m); else ps.push_back(x); } expr r = mk_app(mk_constant(c, to_list(ls)), ps); return mk_pair(new_env, r); }
/* Collect (and sort) dependencies of collected parameters */ void collect_and_normalize_dependencies(buffer<expr> & norm_params) { name_map<expr> new_types; for (unsigned i = 0; i < m_params.size(); i++) { expr x = m_params[i]; expr new_type = collect(m_ctx.instantiate_mvars(m_ctx.infer(x))); new_types.insert(mlocal_name(x), new_type); } local_context const & lctx = m_ctx.lctx(); std::sort(m_params.begin(), m_params.end(), [&](expr const & l1, expr const & l2) { return lctx.get_local_decl(l1)->get_idx() < lctx.get_local_decl(l2)->get_idx(); }); for (unsigned i = 0; i < m_params.size(); i++) { expr x = m_params[i]; expr type = *new_types.find(mlocal_name(x)); expr new_type = replace_locals(type, i, m_params.data(), norm_params.data()); expr new_param = m_ctx.push_local(local_pp_name(x), new_type, local_info(x)); norm_params.push_back(new_param); } }
expr replace_locals(expr const & e, buffer<expr> const & locals, buffer<expr> const & terms) { lean_assert(locals.size() == terms.size()); lean_assert(std::all_of(locals.begin(), locals.end(), is_local)); return replace_locals(e, locals.size(), locals.data(), terms.data()); }