format pp(level l, bool unicode, unsigned indent) { if (is_explicit(l)) { lean_assert(get_depth(l) > 0); return format(get_depth(l) - 1); } else { switch (kind(l)) { case level_kind::Zero: lean_unreachable(); // LCOV_EXCL_LINE case level_kind::Param: case level_kind::Global: return format(to_param_core(l).m_id); case level_kind::Meta: return format("?") + format(meta_id(l)); case level_kind::Succ: { auto p = to_offset(l); return pp_child(p.first, unicode, indent) + format("+") + format(p.second); } case level_kind::Max: case level_kind::IMax: { format r = format(is_max(l) ? "max" : "imax"); r += nest(indent, compose(line(), pp_child(to_max_core(l).m_lhs, unicode, indent))); // max and imax are right associative while (kind(to_max_core(l).m_rhs) == kind(l)) { l = to_max_core(l).m_rhs; r += nest(indent, compose(line(), pp_child(to_max_core(l).m_lhs, unicode, indent))); } r += nest(indent, compose(line(), pp_child(to_max_core(l).m_rhs, unicode, indent))); return group(r); }} lean_unreachable(); // LCOV_EXCL_LINE } }
static void print(std::ostream & out, level l) { if (is_explicit(l)) { lean_assert(get_depth(l) > 0); out << get_depth(l) - 1; } else { switch (kind(l)) { case level_kind::Zero: lean_unreachable(); // LCOV_EXCL_LINE case level_kind::Param: case level_kind::Global: out << to_param_core(l).m_id; break; case level_kind::Meta: out << "?" << meta_id(l); break; case level_kind::Succ: out << "succ "; print_child(out, succ_of(l)); break; case level_kind::Max: case level_kind::IMax: if (is_max(l)) out << "max "; else out << "imax "; print_child(out, to_max_core(l).m_lhs); // max and imax are right associative while (kind(to_max_core(l).m_rhs) == kind(l)) { l = to_max_core(l).m_rhs; out << " "; print_child(out, to_max_core(l).m_lhs); } out << " "; print_child(out, to_max_core(l).m_rhs); break; } } }
void push_max_args(level const & l, buffer<level> & r) { if (is_max(l)) { push_max_args(max_lhs(l), r); push_max_args(max_rhs(l), r); } else { r.push_back(l); } }
level update_max(level const & l, level const & new_lhs, level const & new_rhs) { if (is_eqp(to_max_core(l).m_lhs, new_lhs) && is_eqp(to_max_core(l).m_rhs, new_rhs)) return l; else if (is_max(l)) return mk_max(new_lhs, new_rhs); else return mk_imax(new_lhs, new_rhs); }
bool is_geq_core(level l1, level l2) { if (l1 == l2 || is_zero(l2)) return true; if (is_max(l2)) return is_geq(l1, max_lhs(l2)) && is_geq(l1, max_rhs(l2)); if (is_max(l1) && (is_geq(max_lhs(l1), l2) || is_geq(max_rhs(l1), l2))) return true; if (is_imax(l2)) return is_geq(l1, imax_lhs(l2)) && is_geq(l1, imax_rhs(l2)); if (is_imax(l1)) return is_geq(imax_rhs(l1), l2); auto p1 = to_offset(l1); auto p2 = to_offset(l2); if (p1.first == p2.first || is_zero(p2.first)) return p1.second >= p2.second; if (p1.second == p2.second && p1.second > 0) return is_geq(p1.first, p2.first); return false; }
level mk_max(level const & l1, level const & l2) { if (is_explicit(l1) && is_explicit(l2)) { return get_depth(l1) >= get_depth(l2) ? l1 : l2; } else if (l1 == l2) { return l1; } else if (is_zero(l1)) { return l2; } else if (is_zero(l2)) { return l1; } else if (is_max(l2) && (max_lhs(l2) == l1 || max_rhs(l2) == l1)) { return l2; // if l2 == (max l1 l'), then max l1 l2 == l2 } else if (is_max(l1) && (max_lhs(l1) == l2 || max_rhs(l1) == l2)) { return l1; // if l1 == (max l2 l'), then max l1 l2 == l1 } else { auto p1 = to_offset(l1); auto p2 = to_offset(l2); if (p1.first == p2.first) { lean_assert(p1.second != p2.second); return p1.second > p2.second ? l1 : l2; } else { return cache(level(new level_max_core(false, l1, l2))); } } }
int main() { short val; int n; int run = 0; char skip[256]; assert(sizeof(val) == 2); n = read(0, skip, 44); if (n != 44) { perror("READ"); exit(EXIT_FAILURE); } while (1) { n = read(0, &val, sizeof(val)); if (n == 0) break; if (n == -1) { perror("read()"); exit(EXIT_FAILURE); } shift(val); if (is_max()) { printf("%d\n", run); run = 0; } else { run++; } } exit(EXIT_SUCCESS); }
static environment mk_below(environment const & env, name const & n, bool ibelow) { if (!is_recursive_datatype(env, n)) return env; if (is_inductive_predicate(env, n)) return env; inductive::inductive_decls decls = *inductive::is_inductive_decl(env, n); type_checker tc(env); name_generator ngen; unsigned nparams = std::get<1>(decls); declaration ind_decl = env.get(n); declaration rec_decl = env.get(inductive::get_elim_name(n)); unsigned nindices = *inductive::get_num_indices(env, n); unsigned nminors = *inductive::get_num_minor_premises(env, n); unsigned ntypeformers = length(std::get<2>(decls)); level_param_names lps = rec_decl.get_univ_params(); bool is_reflexive = is_reflexive_datatype(tc, n); level lvl = mk_param_univ(head(lps)); levels lvls = param_names_to_levels(tail(lps)); level_param_names blvls; // universe level parameters of ibelow/below level rlvl; // universe level of the resultant type // The arguments of below (ibelow) are the ones in the recursor - minor premises. // The universe we map to is also different (l+1 for below of reflexive types) and (0 fo ibelow). expr ref_type; expr Type_result; if (ibelow) { // we are eliminating to Prop blvls = tail(lps); rlvl = mk_level_zero(); ref_type = instantiate_univ_param(rec_decl.get_type(), param_id(lvl), mk_level_zero()); } else if (is_reflexive) { blvls = lps; rlvl = get_datatype_level(ind_decl.get_type()); // if rlvl is of the form (max 1 l), then rlvl <- l if (is_max(rlvl) && is_one(max_lhs(rlvl))) rlvl = max_rhs(rlvl); rlvl = mk_max(mk_succ(lvl), rlvl); ref_type = instantiate_univ_param(rec_decl.get_type(), param_id(lvl), mk_succ(lvl)); } else { // we can simplify the universe levels for non-reflexive datatypes blvls = lps; rlvl = mk_max(mk_level_one(), lvl); ref_type = rec_decl.get_type(); } Type_result = mk_sort(rlvl); buffer<expr> ref_args; to_telescope(ngen, ref_type, ref_args); if (ref_args.size() != nparams + ntypeformers + nminors + nindices + 1) throw_corrupted(n); // args contains the below/ibelow arguments buffer<expr> args; buffer<name> typeformer_names; // add parameters and typeformers for (unsigned i = 0; i < nparams; i++) args.push_back(ref_args[i]); for (unsigned i = nparams; i < nparams + ntypeformers; i++) { args.push_back(ref_args[i]); typeformer_names.push_back(mlocal_name(ref_args[i])); } // we ignore minor premises in below/ibelow for (unsigned i = nparams + ntypeformers + nminors; i < ref_args.size(); i++) args.push_back(ref_args[i]); // We define below/ibelow using the recursor for this type levels rec_lvls = cons(mk_succ(rlvl), lvls); expr rec = mk_constant(rec_decl.get_name(), rec_lvls); for (unsigned i = 0; i < nparams; i++) rec = mk_app(rec, args[i]); // add type formers for (unsigned i = nparams; i < nparams + ntypeformers; i++) { buffer<expr> targs; to_telescope(ngen, mlocal_type(args[i]), targs); rec = mk_app(rec, Fun(targs, Type_result)); } // add minor premises for (unsigned i = nparams + ntypeformers; i < nparams + ntypeformers + nminors; i++) { expr minor = ref_args[i]; expr minor_type = mlocal_type(minor); buffer<expr> minor_args; minor_type = to_telescope(ngen, minor_type, minor_args); buffer<expr> prod_pairs; 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); if (is_typeformer_app(typeformer_names, minor_arg_type)) { expr fst = mlocal_type(minor_arg); minor_arg = update_mlocal(minor_arg, Pi(minor_arg_args, Type_result)); expr snd = Pi(minor_arg_args, mk_app(minor_arg, minor_arg_args)); prod_pairs.push_back(mk_prod(tc, fst, snd, ibelow)); } } expr new_arg = foldr([&](expr const & a, expr const & b) { return mk_prod(tc, a, b, ibelow); }, [&]() { return mk_unit(rlvl, ibelow); }, prod_pairs.size(), prod_pairs.data()); rec = mk_app(rec, Fun(minor_args, new_arg)); } // add indices and major premise for (unsigned i = nparams + ntypeformers; i < args.size(); i++) { rec = mk_app(rec, args[i]); } name below_name = ibelow ? name{n, "ibelow"} : name{n, "below"}; expr below_type = Pi(args, Type_result); expr below_value = Fun(args, rec); bool use_conv_opt = true; declaration new_d = mk_definition(env, below_name, blvls, below_type, below_value, use_conv_opt); environment new_env = module::add(env, check(env, new_d)); new_env = set_reducible(new_env, below_name, reducible_status::Reducible); if (!ibelow) new_env = add_unfold_hint(new_env, below_name, nparams + nindices + ntypeformers); return add_protected(new_env, below_name); }
static environment mk_brec_on(environment const & env, name const & n, bool ind) { if (!is_recursive_datatype(env, n)) return env; if (is_inductive_predicate(env, n)) return env; inductive::inductive_decls decls = *inductive::is_inductive_decl(env, n); type_checker tc(env); name_generator ngen; unsigned nparams = std::get<1>(decls); declaration ind_decl = env.get(n); declaration rec_decl = env.get(inductive::get_elim_name(n)); // declaration below_decl = env.get(name(n, ind ? "ibelow" : "below")); unsigned nindices = *inductive::get_num_indices(env, n); unsigned nminors = *inductive::get_num_minor_premises(env, n); unsigned ntypeformers = length(std::get<2>(decls)); level_param_names lps = rec_decl.get_univ_params(); bool is_reflexive = is_reflexive_datatype(tc, n); level lvl = mk_param_univ(head(lps)); levels lvls = param_names_to_levels(tail(lps)); level rlvl; level_param_names blps; levels blvls; // universe level parameters of brec_on/binduction_on // The arguments of brec_on (binduction_on) are the ones in the recursor - minor premises. // The universe we map to is also different (l+1 for below of reflexive types) and (0 fo ibelow). expr ref_type; if (ind) { // we are eliminating to Prop blps = tail(lps); blvls = lvls; rlvl = mk_level_zero(); ref_type = instantiate_univ_param(rec_decl.get_type(), param_id(lvl), mk_level_zero()); } else if (is_reflexive) { blps = lps; blvls = cons(lvl, lvls); rlvl = get_datatype_level(ind_decl.get_type()); // if rlvl is of the form (max 1 l), then rlvl <- l if (is_max(rlvl) && is_one(max_lhs(rlvl))) rlvl = max_rhs(rlvl); rlvl = mk_max(mk_succ(lvl), rlvl); // inner_prod, inner_prod_intro, pr1, pr2 do not use the same universe levels for // reflective datatypes. ref_type = instantiate_univ_param(rec_decl.get_type(), param_id(lvl), mk_succ(lvl)); } else { // we can simplify the universe levels for non-reflexive datatypes blps = lps; blvls = cons(lvl, lvls); rlvl = mk_max(mk_level_one(), lvl); ref_type = rec_decl.get_type(); } buffer<expr> ref_args; to_telescope(ngen, ref_type, ref_args); if (ref_args.size() != nparams + ntypeformers + nminors + nindices + 1) throw_corrupted(n); // args contains the brec_on/binduction_on arguments buffer<expr> args; buffer<name> typeformer_names; // add parameters and typeformers for (unsigned i = 0; i < nparams; i++) args.push_back(ref_args[i]); for (unsigned i = nparams; i < nparams + ntypeformers; i++) { args.push_back(ref_args[i]); typeformer_names.push_back(mlocal_name(ref_args[i])); } // add indices and major premise for (unsigned i = nparams + ntypeformers + nminors; i < ref_args.size(); i++) args.push_back(ref_args[i]); // create below terms (one per datatype) // (below.{lvls} params type-formers) // Remark: it also creates the result type buffer<expr> belows; expr result_type; unsigned k = 0; for (auto const & decl : std::get<2>(decls)) { name const & n1 = inductive::inductive_decl_name(decl); if (n1 == n) { result_type = ref_args[nparams + k]; for (unsigned i = nparams + ntypeformers + nminors; i < ref_args.size(); i++) result_type = mk_app(result_type, ref_args[i]); } k++; name bname = name(n1, ind ? "ibelow" : "below"); expr below = mk_constant(bname, blvls); for (unsigned i = 0; i < nparams; i++) below = mk_app(below, ref_args[i]); for (unsigned i = nparams; i < nparams + ntypeformers; i++) below = mk_app(below, ref_args[i]); belows.push_back(below); } // create functionals (one for each type former) // Pi idxs t, below idxs t -> C idxs t buffer<expr> Fs; name F_name("F"); for (unsigned i = nparams, j = 0; i < nparams + ntypeformers; i++, j++) { expr const & C = ref_args[i]; buffer<expr> F_args; to_telescope(ngen, mlocal_type(C), F_args); expr F_result = mk_app(C, F_args); expr F_below = mk_app(belows[j], F_args); F_args.push_back(mk_local(ngen.next(), "f", F_below, binder_info())); expr F_type = Pi(F_args, F_result); expr F = mk_local(ngen.next(), F_name.append_after(j+1), F_type, binder_info()); Fs.push_back(F); args.push_back(F); } // We define brec_on/binduction_on using the recursor for this type levels rec_lvls = cons(rlvl, lvls); expr rec = mk_constant(rec_decl.get_name(), rec_lvls); // add parameters to rec for (unsigned i = 0; i < nparams; i++) rec = mk_app(rec, ref_args[i]); // add type formers to rec // Pi indices t, prod (C ... t) (below ... t) for (unsigned i = nparams, j = 0; i < nparams + ntypeformers; i++, j++) { expr const & C = ref_args[i]; buffer<expr> C_args; to_telescope(ngen, mlocal_type(C), C_args); expr C_t = mk_app(C, C_args); expr below_t = mk_app(belows[j], C_args); expr prod = mk_prod(tc, C_t, below_t, ind); rec = mk_app(rec, Fun(C_args, prod)); } // add minor premises to rec for (unsigned i = nparams + ntypeformers, j = 0; i < nparams + ntypeformers + nminors; i++, j++) { expr minor = ref_args[i]; expr minor_type = mlocal_type(minor); buffer<expr> minor_args; minor_type = to_telescope(ngen, minor_type, minor_args); buffer<expr> pairs; 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); if (auto k = is_typeformer_app(typeformer_names, minor_arg_type)) { buffer<expr> C_args; get_app_args(minor_arg_type, C_args); expr new_minor_arg_type = mk_prod(tc, minor_arg_type, mk_app(belows[*k], C_args), ind); minor_arg = update_mlocal(minor_arg, Pi(minor_arg_args, new_minor_arg_type)); if (minor_arg_args.empty()) { pairs.push_back(minor_arg); } else { expr r = mk_app(minor_arg, minor_arg_args); expr r_1 = Fun(minor_arg_args, mk_pr1(tc, r, ind)); expr r_2 = Fun(minor_arg_args, mk_pr2(tc, r, ind)); pairs.push_back(mk_pair(tc, r_1, r_2, ind)); } } } expr b = foldr([&](expr const & a, expr const & b) { return mk_pair(tc, a, b, ind); }, [&]() { return mk_unit_mk(rlvl, ind); }, pairs.size(), pairs.data()); unsigned F_idx = *is_typeformer_app(typeformer_names, minor_type); expr F = Fs[F_idx]; buffer<expr> F_args; get_app_args(minor_type, F_args); F_args.push_back(b); expr new_arg = mk_pair(tc, mk_app(F, F_args), b, ind); rec = mk_app(rec, Fun(minor_args, new_arg)); } // add indices and major to rec for (unsigned i = nparams + ntypeformers + nminors; i < ref_args.size(); i++) rec = mk_app(rec, ref_args[i]); name brec_on_name = name(n, ind ? "binduction_on" : "brec_on"); expr brec_on_type = Pi(args, result_type); expr brec_on_value = Fun(args, mk_pr1(tc, rec, ind)); bool use_conv_opt = true; declaration new_d = mk_definition(env, brec_on_name, blps, brec_on_type, brec_on_value, use_conv_opt); environment new_env = module::add(env, check(env, new_d)); new_env = set_reducible(new_env, brec_on_name, reducible_status::Reducible); if (!ind) new_env = add_unfold_hint(new_env, brec_on_name, nparams + nindices + ntypeformers); return add_protected(new_env, brec_on_name); }
int main(int argc, char* argv[]){ double rho = 100; int loop = 1, i, j, k, count = 0;//loop stands for still looping(not every result is correct) FILE *fp; double x[P][N+1]; //all patterns(N for elements of vector--pattern and P for numbers of pattern), value is the class. N+1--the last is for class double w[C][N]; //all power(N for element of vector--power and C for numbers of power), value is the power double g[C]; //value of the result of each culculation /* set the original power*/ /*for(i = 0; i < C; i++) { for(j = 0; j<N; j++) { w[i][j] = 10; } }*/ w[0][0] = 6; w[0][1] = 2; w[0][2] = 1; w[1][0] = 2; w[1][1] = 1; w[1][2] = 5; w[2][0] = 1; w[2][1] = 6; w[2][2] = 1; /* read the data in*/ if(argc != 2) { printf("Usage: ./a.out pattern.dat\n"); return 1; } else { if((fp = fopen(argv[1], "r")) != NULL) { for(i = 0; i<P; i++) { for(j = 0; j<N+1; j++) { if(j == 0) { x[i][j] = 1; } else fscanf(fp, "%lf ", &x[i][j]); } } fclose(fp); } else { printf("Data file can't be opened\n"); return 1; } } /* check */ /*for(i = 0; i<P; i++) { for(j = 0; j<N+1; j++) printf("%lf ", x[i][j]); printf("\n"); }*/ /* learn the power */ while(loop) { count++; loop = 0; /* learn from every pattern */ for(i = 0; i<P; i++) { //printf("pattern number:%d\n", i+1); /* culculate the product of pattern and power */ for(j = 0; j < C; j++) { g[j] = multi(x[i], w[j], N); // printf("g%d(x):%lf\n", j+1, g[j]); } /* compare and revise the power */ for(j = 0; j < C; j++){ /* if x belongs to class j+1 but g(j) is not the biggest */ if(!is_max(j, g, C) && x[i][N] == (j+1)){ // printf("g%d(x) is not the max, but it should be\n", j+1); /* made the power w[j] +tho */ rev(w[j], x[i], rho, N); /* there is a revise, so loop is continuing*/ loop = 1; } else if(is_max(j, g, C) && x[i][N] != (j+1)){ // printf("g%d(x) is the max, but it shouldn't be\n", j+1); rev(w[j], x[i], -rho, N); loop = 1; } } /*printf("Power now is: \n"); for(j = 0; j < C; j++) { printf("class %d:", j+1); for(k = 0; k<N; k++) { printf("%lf ", w[j][k]); } printf("\n"); } printf("\n");*/ } } printf("number of loop is %d\n", count); /* 重みwをファイルに出力 */ if((fp = fopen("power.dat", "w")) != NULL) { for(i = 0; i<C; i++) { for(j = 0; j<N; j++) { fprintf(fp, "%lf ", w[i][j]); printf("%lf ", w[i][j]); } fprintf(fp, "\n"); printf("\n"); } fclose(fp); } else { printf("Power file can't be written\n"); return 1; } return 0; }
level const & max_rhs(level const & l) { lean_assert(is_max(l)); return to_max_core(l).m_rhs; }
static level_max_core const & to_max_core(level const & l) { lean_assert(is_max(l) || is_imax(l)); return static_cast<level_max_core const &>(to_cell(l)); }