bool NodeclStaticInfo::is_constant_access( const Nodecl::NodeclBase& n ) const { bool result = true; if( n.is<Nodecl::ArraySubscript>( ) ) { Nodecl::ArraySubscript array = n.as<Nodecl::ArraySubscript>( ); //Check subscripted if ( !is_constant( array.get_subscripted( ) )) { return false; } // Check subscrips Nodecl::List subscript = array.get_subscripts( ).as<Nodecl::List>( ); Nodecl::List::iterator it = subscript.begin( ); for( ; it != subscript.end( ); ++it ) { // All dimensions must be constant if( !is_constant( *it ) ) { result = false; break; } } } return result; }
optional<expr> unfold_step(type_context & ctx, expr const & e, name_set const & to_unfold, bool unfold_reducible) { if (!unfold_reducible && to_unfold.empty()) return none_expr(); if (!is_app(e) && !is_constant(e)) return none_expr(); expr const & fn = get_app_fn(e); if (!is_constant(fn)) return none_expr(); name const & fn_name = const_name(fn); bool in_to_unfold = to_unfold.contains(const_name(fn)); if (!in_to_unfold && !unfold_reducible) return none_expr(); if (is_projection(ctx.env(), const_name(fn))) { if (in_to_unfold) { type_context::transparency_scope scope(ctx, transparency_mode::Instances); return ctx.reduce_projection(e); } else { return none_expr(); } } else if (in_to_unfold) { return unfold_term(ctx.env(), e); } else if (unfold_reducible && is_reducible(ctx.env(), fn_name)) { type_context::transparency_scope scope(ctx, transparency_mode::Reducible); return unfold_term(ctx.env(), e); } else { return none_expr(); } }
proof_scheme *eql_of_cst_scheme::factory(predicated_real const &real) { if (real.pred() != PRED_EQL || !is_constant(real.real()) || !is_constant(real.real2())) return NULL; preal_vect hyps; hyps.push_back(predicated_real(real.real(), PRED_BND)); hyps.push_back(predicated_real(real.real2(), PRED_BND)); return new eql_of_cst_scheme(real, hyps); }
edge(expr const & e, bool fn) { m_fn = fn; lean_assert(is_constant(e) || is_local(e)); if (is_constant(e)) { m_kind = edge_kind::Constant; m_name = const_name(e); } else { lean_assert(is_local(e)); m_kind = edge_kind::Local; m_name = mlocal_name(e); } }
static void process_op(FILE *fout, Term t, int prec, Term type_term, Term symb_term) { if (ARITY(symb_term) != 0) { fwrite_term_nl(fout, t); fwrite_term_nl(stderr, t); fatal_error("symbols in op command must have no arguments"); } else { Parsetype pt = NOTHING_SPECIAL; if (is_constant(type_term, "infix")) pt = INFIX; else if (is_constant(type_term, "infix_left")) pt = INFIX_LEFT; else if (is_constant(type_term, "infix_right")) pt = INFIX_RIGHT; else if (is_constant(type_term, "prefix")) pt = PREFIX; else if (is_constant(type_term, "prefix_paren")) pt = PREFIX_PAREN; else if (is_constant(type_term, "postfix")) pt = POSTFIX; else if (is_constant(type_term, "postfix_paren")) pt = POSTFIX_PAREN; else if (is_constant(type_term, "clear")) pt = NOTHING_SPECIAL; else { fwrite_term_nl(fout, t); fwrite_term_nl(stderr, t); fatal_error("bad parse-type in op command"); } set_parse_type(sn_to_str(SYMNUM(symb_term)), prec, pt); } } /* process_op */
environment coercion_cmd(parser & p) { auto pos = p.pos(); expr f = p.parse_expr(); if (!is_constant(f)) throw parser_error("invalid 'coercion' command, constant expected", pos); if (p.curr_is_token(g_colon)) { p.next(); pos = p.pos(); expr C = p.parse_expr(); if (!is_constant(C)) throw parser_error("invalid 'coercion' command, constant expected", pos); return add_coercion(p.env(), const_name(f), const_name(C), p.ios()); } else { return add_coercion(p.env(), const_name(f), p.ios()); } }
void LIRItem::load_nonconstant() { if (is_constant()) { dont_load_item(); } else { load_item(); } }
/* Try to reduce cases_on (and nonrecursive recursor) application if major became a constructor */ expr visit_cases_on_app(expr const & e_0) { expr e = default_visit_app(e_0); buffer<expr> args; expr const & fn = get_app_args(e, args); lean_assert(is_constant(fn)); bool is_cases_on = is_cases_on_recursor(env(), const_name(fn)); name const & rec_name = const_name(fn); name const & I_name = rec_name.get_prefix(); unsigned nparams = *inductive::get_num_params(env(), I_name); unsigned nindices = *inductive::get_num_indices(env(), I_name); unsigned major_idx; if (is_cases_on) { major_idx = nparams + 1 + nindices; } else { major_idx = *inductive::get_elim_major_idx(env(), rec_name); } expr major = beta_reduce(args[major_idx]); if (is_constructor_app(env(), major)) { /* Major premise became a constructor. So, we should reduce. */ expr new_e = e; if (is_cases_on) { /* unfold cases_on */ if (auto r = unfold_term(env(), new_e)) new_e = *r; else return e; } /* reduce */ if (auto r = ctx().norm_ext(new_e)) return compiler_step_visitor::visit(beta_reduce(*r)); } return e; }
inline bool has_no_local_control(CDFGNode *node) { return is_constant(node) || node->ntype == hls::Multiplexer || node->ntype == hls::Accept; }
KOKKOS_INLINE_FUNCTION /*volatile*/ PCE<Storage>& PCE<Storage>:: operator=(const volatile PCE<Storage>& x) volatile { if (this != &x) { if (!s_.is_view()) const_cast<my_cijk_type&>(cijk_) = const_cast<const my_cijk_type&>(x.cijk_); if (!s_.is_view() && is_constant(x)) { s_.resize(1); s_[0] = x.s_[0]; } else s_ = x.s_; // For DyamicStorage as a view (is_owned=false), we need to set // the trailing entries when assigning a constant vector (because // the copy constructor in this case doesn't reset the size of this) if (s_.size() > x.s_.size()) for (ordinal_type i=x.s_.size(); i<s_.size(); i++) s_[i] = value_type(0); } return const_cast<PCE&>(*this); }
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); } }
optional<pair<expr, constraint_seq>> projection_converter::reduce_projection(expr const & t) { projection_info const * info = is_projection(t); if (!info) return optional<pair<expr, constraint_seq>>(); buffer<expr> args; get_app_args(t, args); if (args.size() <= info->m_nparams) { return optional<pair<expr, constraint_seq>>(); } unsigned mkidx = info->m_nparams; expr const & mk = args[mkidx]; pair<expr, constraint_seq> new_mk_cs = whnf(mk); expr new_mk = new_mk_cs.first; expr const & new_mk_fn = get_app_fn(new_mk); if (!is_constant(new_mk_fn) || const_name(new_mk_fn) != info->m_constructor) { return optional<pair<expr, constraint_seq>>(); } buffer<expr> mk_args; get_app_args(new_mk, mk_args); unsigned i = info->m_nparams + info->m_i; if (i >= mk_args.size()) { return optional<pair<expr, constraint_seq>>(); } expr r = mk_args[i]; r = mk_app(r, args.size() - mkidx - 1, args.data() + mkidx + 1); return optional<pair<expr, constraint_seq>>(r, new_mk_cs.second); }
void add_enum (symbol_t *enm, symbol_t *name, expr_t *val) { symbol_t *sym; type_t *enum_type = enm->type; symtab_t *enum_tab; int value; if (name->table == current_symtab) error (0, "%s redefined", name->name); if (name->table) name = new_symbol (name->name); name->sy_type = sy_const; name->type = enum_type; enum_tab = enum_type->t.symtab; value = 0; if (enum_tab->symbols) value = ((symbol_t *)(enum_tab->symtail))->s.value->v.integer_val + 1; if (val) { if (!is_constant (val)) error (val, "non-constant initializer"); else if (!is_integer_val (val)) error (val, "invalid initializer type"); else value = expr_integer (val); } name->s.value = new_integer_val (value); symtab_addsymbol (enum_tab, name); sym = new_symbol_type (name->name, name->type); sym->sy_type = sy_const; sym->s.value = name->s.value; symtab_addsymbol (enum_tab->parent, sym); }
projection_info const * projection_converter::is_projection(expr const & e) const { expr const & f = get_app_fn(e); if (is_constant(f)) return m_proj_info.find(const_name(f)); else return nullptr; }
bool Expression::is_expr_primary() const { return is_constant() || is_string_literal() || is_identifier(); }
bool is_recursive_rec_app(environment const & env, expr const & e) { buffer<expr> args; name_generator ngen; expr const & f = get_app_args(e, args); if (!is_constant(f)) return false; auto I_name = inductive::is_elim_rule(env, const_name(f)); if (!I_name || !is_recursive_datatype(env, *I_name) || is_inductive_predicate(env, *I_name)) return false; unsigned nparams = *inductive::get_num_params(env, *I_name); unsigned nminors = *inductive::get_num_minor_premises(env, *I_name); unsigned ntypeformers = *inductive::get_num_type_formers(env, *I_name); buffer<buffer<bool>> is_rec_arg; get_rec_args(env, *I_name, is_rec_arg); for (unsigned i = nparams + ntypeformers, j = 0; i < nparams + ntypeformers + nminors; i++, j++) { buffer<bool> const & minor_is_rec_arg = is_rec_arg[j]; expr minor = args[i]; buffer<expr> minor_ctx; expr minor_body = fun_to_telescope(ngen, minor, minor_ctx, optional<binder_info>()); unsigned sz = std::min(minor_is_rec_arg.size(), minor_ctx.size()); if (find(minor_body, [&](expr const & e, unsigned) { if (!is_local(e)) return false; for (unsigned k = 0; k < sz; k++) { if (minor_is_rec_arg[k] && mlocal_name(e) == mlocal_name(minor_ctx[k])) return true; } return false; })) return false; } return true; }
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(); }); }
// Apply lazy delta-reduction and then normalizer extensions lbool projection_converter::reduce_def_eq(expr & t_n, expr & s_n, constraint_seq & cs) { while (true) { // first, keep applying lazy delta-reduction while applicable lbool r = lazy_delta_reduction(t_n, s_n, cs); if (r != l_undef) return r; auto p_t = reduce_projection(t_n); auto p_s = reduce_projection(s_n); if (p_t && p_s) { t_n = whnf_core(p_t->first); s_n = whnf_core(p_s->first); cs += p_t->second; cs += p_s->second; } else if (p_t || p_s) { expr const & f_t = get_app_fn(t_n); expr const & f_s = get_app_fn(s_n); if (is_constant(f_t) && is_constant(f_s) && const_name(f_t) == const_name(f_s) && (p_t || is_stuck(t_n, *m_tc)) && (p_s || is_stuck(s_n, *m_tc))) { // treat it as a delta-delta step return l_undef; } if (p_t) { t_n = whnf_core(p_t->first); cs += p_t->second; } else if (p_s) { s_n = whnf_core(p_s->first); cs += p_s->second; } else { lean_unreachable(); } } else { auto new_t_n = d_norm_ext(t_n, cs); auto new_s_n = d_norm_ext(s_n, cs); if (!new_t_n && !new_s_n) return l_undef; if (new_t_n) { t_n = whnf_core(*new_t_n); } if (new_s_n) { s_n = whnf_core(*new_s_n); } } r = quick_is_def_eq(t_n, s_n, cs); if (r != l_undef) return r; } }
String english_plural(const String& str) { if (str.size() > 2) { Char a = str.GetChar(str.size() - 2); Char b = str.GetChar(str.size() - 1); if (b == _('y') && is_constant(a)) { return str.substr(0, str.size() - 1) + _("ies"); } else if (b == _('o') && is_constant(a)) { return str + _("es"); } else if ((a == _('s') || a == _('c')) && b == _('h')) { return str + _("es"); } else if (b == _('s')) { return str + _("es"); } else { return str + _("s"); } } return str + _("s"); }
static optional<head_index> to_head_index(expr type) { // TODO(dhs): we will want to filter this set, // because some constants are treated specially by this module // (e.g. [eq] and [not]) expr const & f = get_app_fn(type); if (is_constant(f) || is_local(f)) return optional<head_index>(head_index(f)); else return optional<head_index>(); }
linear_expression& linear_expression::operator/= (const linear_expression& x) { if (is_constant()) return *this = x / constant(); else if (!x.is_constant()) throw nonlinear_expression(); return operator/=(x.constant()); }
void close(hid_t id, H5I_type_t type) { if((id == -1) || (is_constant(type, id))) return; if(!H5Iis_valid(id)) H5CPP_THROW("attempted to close bad hdf5 id: " << id); _close(id, type); }
static optional<unsigned> is_internal_symbol(expr const & e, name const & prefix) { if (!is_constant(e)) return optional<unsigned>(); name const & n = const_name(e); if (n.is_atomic() || !n.is_numeral()) return optional<unsigned>(); if (n.get_prefix() == prefix) return optional<unsigned>(n.get_numeral()); else return optional<unsigned>(); }
action_result no_confusion_action(hypothesis_idx hidx) { try { state & s = curr_state(); app_builder & b = get_app_builder(); hypothesis const & h = s.get_hypothesis_decl(hidx); expr type = h.get_type(); expr lhs, rhs; if (!is_eq(type, lhs, rhs)) return action_result::failed(); lhs = whnf(lhs); rhs = whnf(rhs); optional<name> c1 = is_constructor_app(env(), lhs); optional<name> c2 = is_constructor_app(env(), rhs); if (!c1 || !c2) return action_result::failed(); expr A = whnf(infer_type(lhs)); expr I = get_app_fn(A); if (!is_constant(I) || !inductive::is_inductive_decl(env(), const_name(I))) return action_result::failed(); name nct_name(const_name(I), "no_confusion_type"); if (!env().find(nct_name)) return action_result::failed(); expr target = s.get_target(); expr nct = whnf(b.mk_app(nct_name, target, lhs, rhs)); if (c1 == c2) { if (!is_pi(nct)) return action_result::failed(); if (s.has_target_forward_deps(hidx)) { // TODO(Leo): we currently do not handle this case. // To avoid non-termination we remove the given hypothesis, if there // forward dependencies, we would also have to remove them. // Remark: this is a low priority refinement since it will not happen // very often in practice. return action_result::failed(); } unsigned num_params = *inductive::get_num_params(env(), const_name(I)); unsigned cnstr_arity = get_arity(env().get(*c1).get_type()); lean_assert(cnstr_arity >= num_params); unsigned num_new_eqs = cnstr_arity - num_params; s.push_proof_step(new no_confusion_proof_step_cell(const_name(I), target, h.get_self(), num_new_eqs)); s.set_target(binding_domain(nct)); s.del_hypothesis(hidx); trace_action("no_confusion"); return action_result::new_branch(); } else { name nc_name(const_name(I), "no_confusion"); expr pr = b.mk_app(nc_name, {target, lhs, rhs, h.get_self()}); trace_action("no_confusion"); return action_result::solved(pr); } } catch (app_builder_exception &) { return action_result::failed(); } }
virtual optional<expr> expand(expr const & m, abstract_type_context & ctx) const { check_macro(m); expr const & s = macro_arg(m, 0); expr new_s = ctx.whnf(s); buffer<expr> c_args; expr const & c = get_app_args(new_s, c_args); if (is_constant(c) && const_name(c) == m_constructor_name && m_idx < c_args.size()) { return some_expr(c_args[m_idx]); } else { // expand into recursor expr s_type = ctx.whnf(ctx.infer(s)); buffer<expr> args; expr const & I = get_app_args(s_type, args); if (!is_constant(I) || length(m_ps) != length(const_levels(I))) return none_expr(); expr r = instantiate_univ_params(m_val, m_ps, const_levels(I)); args.push_back(new_s); return some(instantiate_rev(r, args.size(), args.data())); } }
void close(hid_t id) { if((id == -1) || (is_constant(id))) return; if(!H5Iis_valid(id)) H5CPP_THROW("attempted to close bad hdf5 id: " << id); H5I_type_t t = H5CPP_ERR_ON_NEG(H5Iget_type(id)); _close(id, t); }
bool binspector_parser_t::is_named_statement() { return is_invariant() || is_constant() || is_skip() || is_slot() || is_signal() || is_field(); // field should be last because atoms only // require an expression which most everything // falls into; the more explicit stuff should // come first. }
bool is_simp_relation(environment const & env, expr const & e, expr & rel, expr & lhs, expr & rhs) { buffer<expr> args; rel = get_app_args(e, args); if (!is_constant(rel) || !is_simp_relation(env, const_name(rel))) return false; relation_info const * rel_info = get_relation_info(env, const_name(rel)); if (!rel_info || rel_info->get_lhs_pos() >= args.size() || rel_info->get_rhs_pos() >= args.size()) return false; lhs = args[rel_info->get_lhs_pos()]; rhs = args[rel_info->get_rhs_pos()]; return true; }
bool has_placeholder(expr const & e) { return (bool) find(e, [](expr const & e, unsigned) { // NOLINT if (is_placeholder(e)) return true; else if (is_sort(e)) return has_placeholder(sort_level(e)); else if (is_constant(e)) return std::any_of(const_levels(e).begin(), const_levels(e).end(), [](level const & l) { return has_placeholder(l); }); else return false; }); }
static inline bool is_doing_stupid_math(const bh_instruction& instr) { return bh_type_is_integer(instr.constant.type) and is_constant(instr) and ( is_multiplying_by_one(instr) or is_dividing_by_one(instr) or is_adding_zero(instr) or is_subtracting_zero(instr) ) and is_entire_view(instr); }