// If restricted is true, we don't use (e <-> true) rewrite list<expr_pair> apply(expr const & e, expr const & H, bool restrited) { expr c, Hdec, A, arg1, arg2; if (is_relation(e)) { return mk_singleton(e, H); } else if (is_standard(m_env) && is_not(m_env, e, arg1)) { expr new_e = mk_iff(arg1, mk_false()); expr new_H = mk_app(mk_constant(get_iff_false_intro_name()), arg1, H); return mk_singleton(new_e, new_H); } else if (is_standard(m_env) && is_and(e, arg1, arg2)) { // TODO(Leo): we can extend this trick to any type that has only one constructor expr H1 = mk_app(mk_constant(get_and_elim_left_name()), arg1, arg2, H); expr H2 = mk_app(mk_constant(get_and_elim_right_name()), arg1, arg2, H); auto r1 = apply(arg1, H1, restrited); auto r2 = apply(arg2, H2, restrited); return append(r1, r2); } else if (is_pi(e)) { // TODO(dhs): keep name? expr local = m_tctx.mk_tmp_local(binding_domain(e), binding_info(e)); expr new_e = instantiate(binding_body(e), local); expr new_H = mk_app(H, local); auto r = apply(new_e, new_H, restrited); unsigned len = length(r); if (len == 0) { return r; } else if (len == 1 && head(r).first == new_e && head(r).second == new_H) { return mk_singleton(e, H); } else { return lift(local, r); } } else if (is_standard(m_env) && is_ite(e, c, Hdec, A, arg1, arg2) && is_prop(e)) { // TODO(Leo): support HoTT mode if users request expr not_c = mk_app(mk_constant(get_not_name()), c); expr Hc = m_tctx.mk_tmp_local(c); expr Hnc = m_tctx.mk_tmp_local(not_c); expr H1 = mk_app({mk_constant(get_implies_of_if_pos_name()), c, arg1, arg2, Hdec, e, Hc}); expr H2 = mk_app({mk_constant(get_implies_of_if_neg_name()), c, arg1, arg2, Hdec, e, Hnc}); auto r1 = lift(Hc, apply(arg1, H1, restrited)); auto r2 = lift(Hnc, apply(arg2, H2, restrited)); return append(r1, r2); } else if (!restrited) { expr new_e = m_tctx.whnf(e); if (new_e != e) { if (auto r = apply(new_e, H, true)) return r; } if (is_standard(m_env) && is_prop(e)) { expr new_e = mk_iff(e, mk_true()); expr new_H = mk_app(mk_constant(get_iff_true_intro_name()), e, H); return mk_singleton(new_e, new_H); } else { return list<expr_pair>(); } } else { return list<expr_pair>(); } }
size_t ft_stmrxlen(const char *s, char *c) { size_t i; i = 0; while (s[i] && is_not(s[i], c)) ++i; return (i); }
action_result by_contradiction_action() { state & s = curr_state(); expr target = whnf(s.get_target()); if (!is_prop(target)) return action_result::failed(); if (blast::is_false(target)) return action_result::failed(); expr not_target; if (is_not(target, not_target)) { s.set_target(mk_arrow(not_target, mk_constant(get_false_name()))); return intros_action(1); } blast_tmp_type_context tmp_tctx; optional<expr> target_decidable = tmp_tctx->mk_class_instance(mk_app(mk_constant(get_decidable_name()), target)); if (!target_decidable) return action_result::failed(); expr href = s.mk_hypothesis(get_app_builder().mk_not(target)); auto pcell = new by_contradiction_proof_step_cell(href); s.push_proof_step(pcell); s.set_target(mk_constant(get_false_name())); trace_action("by_contradiction"); return action_result::new_branch(); }
size_t rewire_sparse_ex(size_t *from, size_t *to,size_t *degree,size_t ncol, size_t nrow,size_t max_iter, size_t e,size_t verbose,size_t MAXITER,unsigned int seed) { set_rand(seed); size_t i,n,rand1,rand2,t=0; //copy of the original incidence matrix size_t a,b,c,d; size_t ad,cb,ac,bd; unsigned short *adj; size_t *cumdeg; do cumdeg=(size_t *)calloc(nrow,sizeof(size_t)); while(cumdeg==NULL); cumdeg[0]=0; for(i=1;i<nrow;i++) { cumdeg[i]=cumdeg[i-1]+degree[i-1]+1; } adj=(unsigned short *)calloc(cumdeg[nrow-1]+degree[nrow-1]+1,sizeof(unsigned short)); while(adj==NULL); for(i=0;i<nrow;i++) adj[cumdeg[i]+degree[i]]= degree[i]; for(i=0;i<e;i++) { a=from[i]; b=to[i]; adj[cumdeg[a]+degree[a]-adj[cumdeg[a]+degree[a]]]=b; adj[cumdeg[a]+degree[a]]--; adj[cumdeg[b]+degree[b]-adj[cumdeg[b]+degree[b]]]=a; adj[cumdeg[b]+degree[b]]--; } //GetRNGstate(); time_t tin,tfin; tin = time (NULL); for(n=0;n<max_iter;t++) { if(verbose==1) loadBar( n, max_iter, 100, 50); //random rewiring rand1=(size_t) (unif_rand()*e); do rand2=(size_t) (unif_rand()*e); while (rand1==rand2); a=from[rand1]; c=from[rand2]; b=to[rand1]; d=to[rand2]; ad=is_not(a,d,degree,adj,cumdeg); cb=is_not(c,b,degree,adj,cumdeg); ac=is_not(c,a,degree,adj,cumdeg); bd=is_not(b,d,degree,adj,cumdeg); if(t>MAXITER) { tfin = time (NULL); if(verbose==1) printf("DONE in %d seconds \n",-(tin-tfin)); //PutRNGstate(); return (-1); } // printf("%d %d %d %d %d %d %d %d %d %d \n ",rand1, rand2,a+1,b+1,c+1,d+1,ad,cb,ac,bd); if( a!=c && b!=d & a!=d && c!=b && (( ad==1 && cb==1 )|| (ac==1 && bd==1) )) { if(ad==1 && cb==1 && ac==1 && bd==1 ) { if(unif_rand()>=0.5) { to[rand1]=d; to[rand2]=b; sub(a,b,c,d,degree,adj,cumdeg); n++; } else { sub2(a,b,c,d,degree,adj,cumdeg); // from[rand1]=d; to[rand1]=c; from[rand2]=b; to[rand2]=d; n++; } } else if(ad==1 && cb==1 ) { to[rand1]=d; to[rand2]=b; sub(a,b,c,d,degree,adj,cumdeg); n++; } else { sub2(a,b,c,d,degree,adj,cumdeg); to[rand1]=c; from[rand2]=b; to[rand2]=d; n++; } } } tfin = time (NULL); if(verbose==1) printf("DONE in %d seconds \n",-(tin-tfin)); //PutRNGstate(); return 0; }
tactic contradiction_tactic() { auto fn = [=](environment const & env, io_state const & ios, proof_state const & s) { goals const & gs = s.get_goals(); if (empty(gs)) { throw_no_goal_if_enabled(s); return optional<proof_state>(); } goal const & g = head(gs); expr const & t = g.get_type(); substitution subst = s.get_subst(); auto tc = mk_type_checker(env); auto conserv_tc = mk_type_checker(env, UnfoldReducible); buffer<expr> hyps; g.get_hyps(hyps); for (expr const & h : hyps) { expr h_type = mlocal_type(h); h_type = tc->whnf(h_type).first; expr lhs, rhs, arg; if (is_false(env, h_type)) { assign(subst, g, mk_false_rec(*tc, h, t)); return some_proof_state(proof_state(s, tail(gs), subst)); } else if (is_not(env, h_type, arg)) { optional<expr> h_pos; for (expr const & h_prime : hyps) { constraint_seq cs; if (conserv_tc->is_def_eq(arg, mlocal_type(h_prime), justification(), cs) && !cs) { h_pos = h_prime; break; } } if (h_pos) { assign(subst, g, mk_absurd(*tc, t, *h_pos, h)); return some_proof_state(proof_state(s, tail(gs), subst)); } } else if (is_eq(h_type, lhs, rhs)) { lhs = tc->whnf(lhs).first; rhs = tc->whnf(rhs).first; optional<name> lhs_c = is_constructor_app(env, lhs); optional<name> rhs_c = is_constructor_app(env, rhs); if (lhs_c && rhs_c && *lhs_c != *rhs_c) { if (optional<name> I_name = inductive::is_intro_rule(env, *lhs_c)) { name no_confusion(*I_name, "no_confusion"); try { expr I = tc->whnf(tc->infer(lhs).first).first; buffer<expr> args; expr I_fn = get_app_args(I, args); if (is_constant(I_fn)) { level t_lvl = sort_level(tc->ensure_type(t).first); expr V = mk_app(mk_app(mk_constant(no_confusion, cons(t_lvl, const_levels(I_fn))), args), t, lhs, rhs, h); if (auto r = lift_down_if_hott(*tc, V)) { check_term(*tc, *r); assign(subst, g, *r); return some_proof_state(proof_state(s, tail(gs), subst)); } } } catch (kernel_exception & ex) { regular(env, ios) << ex << "\n"; } } } } } return none_proof_state(); }; return tactic01(fn); }
bool is_not(expr const & e) { expr not_e; return is_not(e, not_e); }