environment add_namespace(environment const & env, name const & ns) { scope_mng_ext ext = get_extension(env); if (!ext.m_namespace_set.contains(ns)) { ext.m_namespace_set.insert(ns); environment r = update(env, ext); r = module::add(r, *g_new_namespace_key, [=](environment const &, serializer & s) { s << ns; }); if (ns.is_atomic()) return r; else return add_namespace(r, ns.get_prefix()); } else { return env; } }
environment add_alias(parser & p, environment env, bool composite, name const & full_id, levels const & ctx_levels, buffer<expr> const & ctx_params) { name id; if (composite) id = name(name(full_id.get_prefix().get_string()), full_id.get_string()); else id = name(full_id.get_string()); if (!empty(ctx_levels) || !ctx_params.empty()) { expr r = mk_local_ref(full_id, ctx_levels, ctx_params); env = p.add_local_ref(env, id, r); } if (full_id != id) env = add_expr_alias_rec(env, id, full_id); return env; }
expr visit_cases_on(name const & fn, buffer<expr> & args) { name const & I_name = fn.get_prefix(); if (is_inductive_predicate(env(), I_name)) throw exception(sstream() << "code generation failed, inductive predicate '" << I_name << "' is not supported"); bool is_builtin = is_vm_builtin_function(fn); buffer<name> cnames; get_intro_rule_names(env(), I_name, cnames); lean_assert(args.size() >= cnames.size() + 1); if (args.size() > cnames.size() + 1) distribute_extra_args_over_minors(I_name, cnames, args); lean_assert(args.size() == cnames.size() + 1); /* Process major premise */ args[0] = visit(args[0]); unsigned num_reachable = 0; optional<expr> reachable_case; /* Process minor premises */ for (unsigned i = 0; i < cnames.size(); i++) { buffer<bool> rel_fields; get_constructor_info(cnames[i], rel_fields); auto p = visit_minor_premise(args[i+1], rel_fields); expr new_minor = p.first; if (i == 0 && has_trivial_structure(I_name, rel_fields)) { /* Optimization for an inductive datatype that has a single constructor with only one relevant field */ return beta_reduce(mk_app(new_minor, args[0])); } args[i+1] = new_minor; if (!p.second) { num_reachable++; reachable_case = p.first; } } if (num_reachable == 0) { return mk_unreachable_expr(); } else if (num_reachable == 1 && !is_builtin) { /* Use _cases.1 */ return mk_app(mk_cases(1), args[0], *reachable_case); } else if (is_builtin) { return mk_app(mk_constant(fn), args); } else { return mk_app(mk_cases(cnames.size()), args); } }
static bool is_explicit_placeholder(name const & n) { return !n.is_atomic() && n.get_prefix() == *g_explicit_placeholder_name; }
static bool is_placeholder(name const & n) { if (n.is_atomic()) return false; name const & p = n.get_prefix(); return p == *g_implicit_placeholder_name || p == *g_strict_placeholder_name || p == *g_explicit_placeholder_name; }