void get_rec_args(environment const & env, name const & n, buffer<buffer<bool>> & r) { lean_assert(inductive::is_inductive_decl(env, n)); type_checker tc(env); name_generator ngen; declaration ind_decl = env.get(n); declaration rec_decl = env.get(inductive::get_elim_name(n)); unsigned nparams = *inductive::get_num_params(env, n); unsigned nminors = *inductive::get_num_minor_premises(env, n); unsigned ntypeformers = *inductive::get_num_type_formers(env, n); buffer<expr> rec_args; to_telescope(ngen, rec_decl.get_type(), rec_args); buffer<name> typeformer_names; for (unsigned i = nparams; i < nparams + ntypeformers; i++) { typeformer_names.push_back(mlocal_name(rec_args[i])); } lean_assert(typeformer_names.size() == ntypeformers); r.clear(); // add minor premises for (unsigned i = nparams + ntypeformers; i < nparams + ntypeformers + nminors; i++) { r.push_back(buffer<bool>()); buffer<bool> & bv = r.back(); expr minor_type = mlocal_type(rec_args[i]); buffer<expr> minor_args; to_telescope(ngen, minor_type, minor_args); for (expr & minor_arg : minor_args) { buffer<expr> minor_arg_args; expr minor_arg_type = to_telescope(tc, mlocal_type(minor_arg), minor_arg_args); bv.push_back(is_typeformer_app(typeformer_names, minor_arg_type)); } } }
/** * Converts an environment to a string used by CreateProcess(). * * Converts the environment's contents to the format used by the * CreateProcess() system call. The returned char* string is * allocated in dynamic memory; the caller must free it when not * used any more. This is enforced by the use of a shared pointer. * * \return A dynamically allocated char* string that represents * the environment's content. This string is of the form * var1=value1\\0var2=value2\\0\\0. */ inline boost::shared_array<char> environment_to_win32_strings(const environment &env) { boost::shared_array<char> envp; if (env.empty()) { envp.reset(new char[2]); ::ZeroMemory(envp.get(), 2); } else { std::string s; for (environment::const_iterator it = env.begin(); it != env.end(); ++it) { s += it->first + "=" + it->second; s.push_back(0); } envp.reset(new char[s.size() + 1]); #if defined(__CYGWIN__) || defined(_SCL_SECURE_NO_DEPRECATE) ::memcpy(envp.get(), s.c_str(), s.size() + 1); #else ::memcpy_s(envp.get(), s.size() + 1, s.c_str(), s.size() + 1); #endif } return envp; }
void write_field(environment e, jobject obj, const char* name, T value) { std::string descriptor; jvb::detail::descriptors::descriptor<T> (e, std::back_inserter(descriptor)); jclass cls = e.raw()->GetObjectClass(obj); jfieldID fid = e.raw()->GetFieldID(cls, name, descriptor.c_str()); assert(fid != 0); jvb::detail::set_field(e.raw(), obj, fid, value); }
void write_static_field(environment e, jvb::class_ class_, const char* name, T value) { std::string descriptor; jvb::detail::descriptors::descriptor<T> (e, std::back_inserter(descriptor)); jclass cls = class_.raw(); jfieldID fid = e.raw()->GetStaticFieldID(cls, name, descriptor.c_str()); assert(fid != 0); jvb::detail::set_static_field(e.raw(), cls, fid, value); }
result_type operator()(const bind &b) { assert(detail::isinstance<name>(b.lhs())); const name& lhs = boost::get<const name&>(b.lhs()); if (m_declared.exists(lhs.id())) { m_modified.insert(lhs.id()); } else { m_declared.insert(lhs.id()); } return b.ptr(); }
T read_static_field(environment e, jvb::class_ class_, const char* name) { std::string descriptor; jvb::detail::descriptors::descriptor<T> (e, std::back_inserter(descriptor)); jclass cls = class_.raw(); std::cout << "Searching for " << name << " with descriptor " << descriptor << std::endl; jfieldID fid = e.raw()->GetStaticFieldID(cls, name, descriptor.c_str()); assert(fid != 0); return jvb::detail::get_static_field(e.raw(), cls, fid, jvb::detail::tag<T>()); }
void print_axioms(expr const & ex) { for_each(ex, [&] (expr const & e, unsigned) { if (is_constant(e) && !m_already_printed.count(const_name(e))) { auto decl = m_env.get(const_name(e)); m_already_printed.insert(decl.get_name()); print_axioms(decl); if (decl.is_constant_assumption() && !m_env.is_builtin(decl.get_name())) print_decl(decl); } return true; }); }
/** * Converts an environment to a char** table as used by execve(). * * Converts the environment's contents to the format used by the * execve() system call. The returned char** array is allocated * in dynamic memory; the caller must free it when not used any * more. Each entry is also allocated in dynamic memory and is a * NULL-terminated string of the form var=value; these must also be * released by the caller. * * This operation is only available on POSIX systems. * * \return The first argument of the pair is an integer that indicates * how many strings are stored in the second argument. The * second argument is a NULL-terminated, dynamically allocated * array of dynamically allocated strings representing the * enviroment's content. Each array entry is a NULL-terminated * string of the form var=value. The caller is responsible for * freeing them. */ inline std::pair<std::size_t, char **> environment_to_envp(const environment &env) { std::size_t nargs = env.size(); char **envp = new char *[nargs + 1]; environment::size_type i = 0; for (environment::const_iterator it = env.begin(); it != env.end(); ++it) { std::string s = it->first + "=" + it->second; envp[i] = new char[s.size() + 1]; std::strncpy(envp[i], s.c_str(), s.size() + 1); ++i; } envp[i] = 0; return std::pair<std::size_t, char **>(nargs, envp); } // environment_to_envp
void npc::action_unit(environment ¤t) { person::action_unit(current); point dest = current.player()->position(); if (m_ai_state == ai_state::idle) { if (current.distance(dest, position()) <= alert_distance || !health().full()) { m_ai_state = ai_state::alert; current.broadcast({ "!", 0xff0000, position(), 1.0 }); } } else if (m_ai_state == ai_state::alert) { auto scene = current.scene(); auto path = path::find(position(), dest, 50, [&](const point &p) { return scene->traversable(p); }); if (path) { auto step = path->begin(); if (step != path->end()) { auto blocking = scene->blocking(*step); if (blocking && !blocking->invincible()) { for (auto &skill : m_skills) { if (skill.targeted() && skill.useable(this, blocking)) { skill.use(this, blocking); break; } else if (skill.useable(this, dest)) { skill.use(this, dest); break; } } } else { scene->move(*this, *step); } } } } }
json serialize_decl(name const & short_name, name const & long_name, environment const & env, options const & o) { declaration const & d = env.get(long_name); type_context_old tc(env); auto fmter = mk_pretty_formatter_factory()(env, o, tc); expr type = d.get_type(); if (LEAN_COMPLETE_CONSUME_IMPLICIT) { while (true) { if (!is_pi(type)) break; if (!binding_info(type).is_implicit() && !binding_info(type).is_inst_implicit()) break; std::string q("?"); q += binding_name(type).to_string(); expr m = mk_constant(name(q.c_str())); type = instantiate(binding_body(type), m); } } json completion; completion["text"] = short_name.to_string(); interactive_report_type(env, o, type, completion); add_source_info(env, long_name, completion); if (auto doc = get_doc_string(env, long_name)) completion["doc"] = *doc; return completion; }
inline jstring unwrap(environment e, const char* c) { assert(c != 0); jstring str = e.raw()->NewStringUTF(c); assert(str != 0); return str; }
static optional<pair<expr, expr>> mk_op(environment const & env, old_local_context & ctx, type_checker_ptr & tc, name const & op, unsigned nunivs, unsigned nargs, std::initializer_list<expr> const & explicit_args, constraint_seq & cs, tag g) { levels lvls; for (unsigned i = 0; i < nunivs; i++) lvls = levels(mk_meta_univ(mk_fresh_name()), lvls); expr c = mk_constant(op, lvls); expr op_type = instantiate_type_univ_params(env.get(op), lvls); buffer<expr> args; for (unsigned i = 0; i < nargs; i++) { if (!is_pi(op_type)) return optional<pair<expr, expr>>(); expr arg = ctx.mk_meta(some_expr(binding_domain(op_type)), g); args.push_back(arg); op_type = instantiate(binding_body(op_type), arg); } expr r = mk_app(c, args, g); for (expr const & explicit_arg : explicit_args) { if (!is_pi(op_type)) return optional<pair<expr, expr>>(); r = mk_app(r, explicit_arg); expr type = tc->infer(explicit_arg, cs); justification j = mk_app_justification(r, op_type, explicit_arg, type); if (!tc->is_def_eq(binding_domain(op_type), type, j, cs)) return optional<pair<expr, expr>>(); op_type = instantiate(binding_body(op_type), explicit_arg); } return some(mk_pair(r, op_type)); }
optional<name> get_noncomputable_reason(environment const & env, name const & n) { declaration const & d = env.get(n); if (!d.is_definition()) return optional<name>(); type_checker tc(env); if (tc.is_prop(d.get_type())) return optional<name>(); // definition is a proposition, then do nothing expr const & v = d.get_value(); auto ext = get_extension(env); bool ok = true; /* quick check */ for_each(v, [&](expr const & e, unsigned) { if (!ok) return false; // stop the search if (is_constant(e) && is_noncomputable(tc, ext, const_name(e))) { ok = false; } return true; }); if (ok) { return optional<name>(); } /* expensive check */ try { get_noncomputable_reason_fn proc(tc); proc(v); return optional<name>(); } catch (get_noncomputable_reason_fn::found & r) { return optional<name>(r.m_reason); } }
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; }
environment decl_attributes::apply(environment env, io_state const & ios, name const & d) const { if (m_is_instance) { if (m_priority) { #if defined(__GNUC__) && !defined(__CLANG__) #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" #endif env = add_instance(env, d, *m_priority, m_persistent); } else { env = add_instance(env, d, m_persistent); } } if (m_is_trans_instance) { if (m_priority) { #if defined(__GNUC__) && !defined(__CLANG__) #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" #endif env = add_trans_instance(env, d, *m_priority, m_persistent); } else { env = add_trans_instance(env, d, m_persistent); } } if (m_is_coercion) env = add_coercion(env, ios, d, m_persistent); auto decl = env.find(d); if (decl && decl->is_definition()) { if (m_is_reducible) env = set_reducible(env, d, reducible_status::Reducible, m_persistent); if (m_is_irreducible) env = set_reducible(env, d, reducible_status::Irreducible, m_persistent); if (m_is_semireducible) env = set_reducible(env, d, reducible_status::Semireducible, m_persistent); if (m_is_quasireducible) env = set_reducible(env, d, reducible_status::Quasireducible, m_persistent); if (m_unfold_hint) env = add_unfold_hint(env, d, m_unfold_hint, m_persistent); if (m_unfold_full_hint) env = add_unfold_full_hint(env, d, m_persistent); } if (m_constructor_hint) env = add_constructor_hint(env, d, m_persistent); if (m_symm) env = add_symm(env, d, m_persistent); if (m_refl) env = add_refl(env, d, m_persistent); if (m_trans) env = add_trans(env, d, m_persistent); if (m_subst) env = add_subst(env, d, m_persistent); if (m_recursor) env = add_user_recursor(env, d, m_recursor_major_pos, m_persistent); if (m_is_class) env = add_class(env, d, m_persistent); if (m_rewrite) env = add_rewrite_rule(env, d, m_persistent); if (m_has_multiple_instances) env = mark_multiple_instances(env, d, m_persistent); return env; }
static pair<expr, unsigned> extract_arg_types_core(environment const & env, name const & f, buffer<expr> & arg_types) { declaration d = env.get(f); expr f_type = d.get_type(); while (is_pi(f_type)) { arg_types.push_back(binding_domain(f_type)); f_type = binding_body(f_type); } return mk_pair(f_type, length(d.get_univ_params())); }
name mk_fresh_name(environment const & env, name const & prefix, char const * suffix, unsigned & idx) { while (true) { name curr(prefix, suffix); curr = curr.append_after(idx); idx++; if (!env.find(curr) && !is_vm_function(env, curr)) return curr; } }
unsigned get_constructor_arity(environment const & env, name const & n) { declaration d = env.get(n); expr e = d.get_type(); unsigned r = 0; while (is_pi(e)) { r++; e = binding_body(e); } return r; }
static jmethodID find_id(environment e, Class cls) { std::string descriptor; jvb::detail::descriptors::descriptor_function <void, typename boost::function_types::parameter_types<F>::type> (e, std::back_inserter(descriptor)); jmethodID id = e.raw()->GetMethodID(cls.raw(), "<init>", descriptor.c_str()); assert(id != 0); return id; }
result_type operator()(const procedure &p) { for(auto i = p.args().begin(); i != p.args().end(); i++) { assert(detail::isinstance<name>(*i)); const name& arg_name = boost::get<const name&>(*i); m_declared.insert(arg_name.id()); } return rewriter<protector>::operator()(p); }
optional<name> get_undef_global(level const & l, environment const & env) { optional<name> r; for_each(l, [&](level const & l) { if (!has_global(l) || r) return false; if (is_global(l) && !env.is_universe(global_id(l))) r = global_id(l); return true; }); return r; }
static name mk_fresh_name(environment const & env, buffer<name> const & names, name const & s) { unsigned i = 1; name c = s; while (true) { if (!env.find(c) && std::find(names.begin(), names.end(), c) == names.end()) return c; c = s.append_after(i); i++; } }
static jmethodID find_id(environment e, jclass cls, const char* name) { typedef typename boost::function_types::result_type<F>::type return_type; typedef typename boost::function_types::parameter_types<F>::type parameter_types; std::string type; detail::descriptors::descriptor_function<return_type, parameter_types> (e, std::back_inserter<std::string>(type)); assert(cls != 0); jmethodID id = e.raw()->GetStaticMethodID(cls, name, type.c_str()); assert(id != 0); return id; }
static unsigned get_max_height(environment const & env, expr const & v) { unsigned h = 0; for_each(v, [&](expr const & e, unsigned) { if (is_constant(e)) { auto d = env.find(const_name(e)); if (d && d->get_height() > h) h = d->get_height(); } return true; }); return h; }
name mk_name_for(expr const & e) { lean_assert(is_nested_declaration(e)); if (auto n = get_nested_declaration_name(e)) { return *n; } else { name ns = get_namespace(m_env); while (true) { name aux = m_dname.append_after(m_idx); m_idx++; if (!m_env.find(ns + aux)) return aux; } } }
static void parse(environment const & env, io_state const & ios, char const * str) { environment child = env->mk_child(); io_state ios_copy = ios; std::istringstream in(str); if (parse_commands(child, ios_copy, in, "[string]")) { formatter fmt = mk_pp_formatter(env); std::for_each(child->begin_local_objects(), child->end_local_objects(), [&](object const & obj) { std::cout << fmt(obj) << "\n"; std::cout << obj << "\n"; }); } }
void definition_cache::collect_dependencies(environment const & env, expr const & e, dependencies & deps) { for_each(e, [&](expr const & e, unsigned) { if (!is_constant(e)) return true; name const & n = const_name(e); if (deps.contains(n)) return true; auto d = env.find(n); if (!d) return true; deps.insert(n, hash_bi(d->get_type())); return true; }); }
/** \brief Return true iff the type of all declarations in deps still have the same hashcode stored in deps. */ bool definition_cache::check_dependencies(environment const & env, dependencies const & deps) { bool ok = true; deps.for_each([&](name const & n, unsigned h) { if (ok) { if (auto d = env.find(n)) { if (h != hash_bi(d->get_type())) ok = false; } else { ok = false; } } }); return ok; }
environment mk_rec_on(environment const & env, name const & n) { if (!inductive::is_inductive_decl(env, n)) throw exception(sstream() << "error in 'rec_on' generation, '" << n << "' is not an inductive datatype"); name rec_on_name(n, "rec_on"); name_generator ngen; declaration rec_decl = env.get(inductive::get_elim_name(n)); buffer<expr> locals; expr rec_type = rec_decl.get_type(); while (is_pi(rec_type)) { expr local = mk_local(ngen.next(), binding_name(rec_type), binding_domain(rec_type), binding_info(rec_type)); rec_type = instantiate(binding_body(rec_type), local); locals.push_back(local); } // locals order // A C minor_premises indices major-premise // new_locals order // A C indices major-premise minor-premises buffer<expr> new_locals; unsigned idx_major_sz = *inductive::get_num_indices(env, n) + 1; unsigned minor_sz = *inductive::get_num_minor_premises(env, n); unsigned AC_sz = locals.size() - minor_sz - idx_major_sz; for (unsigned i = 0; i < AC_sz; i++) new_locals.push_back(locals[i]); for (unsigned i = 0; i < idx_major_sz; i++) new_locals.push_back(locals[AC_sz + minor_sz + i]); unsigned rec_on_major_idx = new_locals.size() - 1; for (unsigned i = 0; i < minor_sz; i++) new_locals.push_back(locals[AC_sz + i]); expr rec_on_type = Pi(new_locals, rec_type); levels ls = param_names_to_levels(rec_decl.get_univ_params()); expr rec = mk_constant(rec_decl.get_name(), ls); expr rec_on_val = Fun(new_locals, mk_app(rec, locals)); bool use_conv_opt = true; environment new_env = module::add(env, check(env, mk_definition(env, rec_on_name, rec_decl.get_univ_params(), rec_on_type, rec_on_val, use_conv_opt))); new_env = set_reducible(new_env, rec_on_name, reducible_status::Reducible); new_env = add_unfold_hint(new_env, rec_on_name, rec_on_major_idx); new_env = add_aux_recursor(new_env, rec_on_name); return add_protected(new_env, rec_on_name); }
bool is_tactic_namespace_open(environment const & env) { for (name const & a : get_expr_aliases(env, "apply")) { if (a == get_tactic_apply_name()) { // make sure the type is the expected one if (auto d = env.find(a)) { expr t = d->get_type(); if (is_pi(t)) { expr b = binding_body(t); if (!is_constant(b) || const_name(b) != get_tactic_name()) throw exception("tactic namespace declarations have been modified, " "function 'tactic.apply' is expected to return a 'tactic'"); } } return true; } } return false; }