2013-11-21 18:44:53 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2013-11-21 23:31:55 +00:00
|
|
|
#include <utility>
|
2013-11-22 02:39:33 +00:00
|
|
|
#include <chrono>
|
2013-11-23 23:33:25 +00:00
|
|
|
#include "util/sstream.h"
|
2013-11-21 23:31:55 +00:00
|
|
|
#include "util/interrupt.h"
|
2013-11-22 02:39:33 +00:00
|
|
|
#include "util/lazy_list_fn.h"
|
2013-11-21 23:31:55 +00:00
|
|
|
#include "library/tactic/tactic_exception.h"
|
2013-11-21 18:44:53 +00:00
|
|
|
#include "library/tactic/tactic.h"
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
tactic & tactic::operator=(tactic const & s) {
|
|
|
|
LEAN_COPY_REF(tactic, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
tactic & tactic::operator=(tactic && s) {
|
|
|
|
LEAN_MOVE_REF(tactic, s);
|
|
|
|
}
|
2013-11-21 20:34:37 +00:00
|
|
|
|
2013-11-22 00:44:31 +00:00
|
|
|
expr tactic::solve(environment const & env, io_state const & io, proof_state const & s) {
|
2013-11-22 02:39:33 +00:00
|
|
|
proof_state_seq r = operator()(env, io, s);
|
2013-11-23 23:33:25 +00:00
|
|
|
auto p = r.pull();
|
|
|
|
if (!p)
|
2013-11-22 02:39:33 +00:00
|
|
|
throw exception("tactic failed to solve input");
|
2013-11-23 23:33:25 +00:00
|
|
|
proof_state final = p->first;
|
2013-11-22 02:39:33 +00:00
|
|
|
assignment a(final.get_menv());
|
2013-11-22 00:44:31 +00:00
|
|
|
proof_map m;
|
2013-11-22 02:39:33 +00:00
|
|
|
return final.get_proof_builder()(m, env, a);
|
2013-11-22 00:44:31 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 01:25:19 +00:00
|
|
|
expr tactic::solve(environment const & env, io_state const & io, context const & ctx, expr const & t) {
|
|
|
|
proof_state s = to_proof_state(env, ctx, t);
|
|
|
|
return solve(env, io, s);
|
|
|
|
}
|
|
|
|
|
2013-11-22 02:39:33 +00:00
|
|
|
tactic id_tactic() {
|
|
|
|
return mk_tactic([](environment const &, io_state const &, proof_state const & s) -> proof_state_seq {
|
2013-11-23 23:33:25 +00:00
|
|
|
return to_proof_state_seq(s);
|
2013-11-22 02:39:33 +00:00
|
|
|
});
|
|
|
|
}
|
2013-11-22 01:25:19 +00:00
|
|
|
|
2013-11-22 02:39:33 +00:00
|
|
|
tactic fail_tactic() {
|
|
|
|
return mk_tactic([](environment const &, io_state const &, proof_state const &) -> proof_state_seq {
|
|
|
|
return proof_state_seq();
|
|
|
|
});
|
|
|
|
}
|
2013-11-22 01:25:19 +00:00
|
|
|
|
|
|
|
tactic now_tactic() {
|
2013-11-22 02:39:33 +00:00
|
|
|
return mk_tactic([](environment const &, io_state const &, proof_state const & s) -> proof_state_seq {
|
2013-11-22 01:25:19 +00:00
|
|
|
if (!empty(s.get_goals()))
|
2013-11-22 02:39:33 +00:00
|
|
|
return proof_state_seq();
|
|
|
|
else
|
2013-11-23 23:33:25 +00:00
|
|
|
return to_proof_state_seq(s);
|
2013-11-22 01:25:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-23 23:33:25 +00:00
|
|
|
tactic trace_tactic(std::string const & msg) {
|
|
|
|
return mk_tactic([=](environment const &, io_state const & io, proof_state const & s) -> proof_state_seq {
|
|
|
|
return proof_state_seq([=]() {
|
|
|
|
io.get_diagnostic_channel() << msg << "\n";
|
|
|
|
io.get_diagnostic_channel().get_stream().flush();
|
|
|
|
return some(mk_pair(s, proof_state_seq()));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tactic trace_tactic(sstream const & msg) {
|
|
|
|
return trace_tactic(msg.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
tactic trace_tactic(char const * msg) {
|
|
|
|
return trace_tactic(std::string(msg));
|
|
|
|
}
|
|
|
|
|
2013-11-22 01:25:19 +00:00
|
|
|
tactic assumption_tactic() {
|
2013-11-23 23:53:45 +00:00
|
|
|
return mk_simple_tactic([](environment const &, io_state const &, proof_state const & s) -> proof_state {
|
2013-11-22 01:25:19 +00:00
|
|
|
list<std::pair<name, expr>> proofs;
|
|
|
|
goals new_goals = map_goals(s, [&](name const & ng, goal const & g) -> goal {
|
|
|
|
expr const & c = g.get_conclusion();
|
|
|
|
expr pr;
|
|
|
|
for (auto const & p : g.get_hypotheses()) {
|
|
|
|
check_interrupted();
|
|
|
|
if (p.second == c) {
|
|
|
|
pr = mk_constant(p.first);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pr) {
|
|
|
|
proofs.emplace_front(ng, pr);
|
|
|
|
return goal();
|
|
|
|
} else {
|
|
|
|
return g;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
proof_builder p = s.get_proof_builder();
|
|
|
|
proof_builder new_p = mk_proof_builder([=](proof_map const & m, environment const & env, assignment const & a) -> expr {
|
|
|
|
proof_map new_m(m);
|
|
|
|
for (auto const & np : proofs) {
|
|
|
|
new_m.insert(np.first, np.second);
|
|
|
|
}
|
|
|
|
return p(new_m, env, a);
|
|
|
|
});
|
2013-11-23 23:53:45 +00:00
|
|
|
return proof_state(s, new_goals, new_p);
|
2013-11-22 01:25:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-11-22 02:39:33 +00:00
|
|
|
tactic then(tactic t1, tactic t2) {
|
|
|
|
return mk_tactic([=](environment const & env, io_state const & io, proof_state const & s1) -> proof_state_seq {
|
|
|
|
tactic _t1(t1);
|
|
|
|
return map_append(_t1(env, io, s1), [=](proof_state const & s2) {
|
|
|
|
check_interrupted();
|
|
|
|
tactic _t2(t2);
|
|
|
|
return _t2(env, io, s2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2013-11-21 20:34:37 +00:00
|
|
|
|
2013-11-22 02:39:33 +00:00
|
|
|
tactic orelse(tactic t1, tactic t2) {
|
|
|
|
return mk_tactic([=](environment const & env, io_state const & io, proof_state const & s) -> proof_state_seq {
|
|
|
|
tactic _t1(t1);
|
2013-11-23 23:33:25 +00:00
|
|
|
tactic _t2(t2);
|
|
|
|
return orelse(_t1(env, io, s), _t2(env, io, s));
|
2013-11-22 02:39:33 +00:00
|
|
|
});
|
|
|
|
}
|
2013-11-21 20:34:37 +00:00
|
|
|
|
2013-11-22 02:39:33 +00:00
|
|
|
tactic try_for(tactic t, unsigned ms, unsigned check_ms) {
|
|
|
|
if (check_ms == 0)
|
|
|
|
check_ms = 1;
|
|
|
|
return mk_tactic([=](environment const & env, io_state const & io, proof_state const & s) -> proof_state_seq {
|
|
|
|
tactic _t(t);
|
|
|
|
proof_state_seq r;
|
|
|
|
std::atomic<bool> done(false);
|
|
|
|
interruptible_thread th([&]() {
|
|
|
|
try {
|
|
|
|
r = _t(env, io, s);
|
|
|
|
} catch (...) {
|
|
|
|
r = proof_state_seq();
|
|
|
|
}
|
|
|
|
done = true;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
auto start = std::chrono::steady_clock::now();
|
|
|
|
std::chrono::milliseconds d(ms);
|
2013-11-22 23:51:17 +00:00
|
|
|
std::chrono::milliseconds small(check_ms);
|
2013-11-22 02:39:33 +00:00
|
|
|
while (!done) {
|
|
|
|
auto curr = std::chrono::steady_clock::now();
|
|
|
|
if (std::chrono::duration_cast<std::chrono::milliseconds>(curr - start) > d)
|
|
|
|
break;
|
|
|
|
check_interrupted();
|
2013-11-22 23:51:17 +00:00
|
|
|
std::this_thread::sleep_for(small);
|
2013-11-22 02:39:33 +00:00
|
|
|
}
|
|
|
|
th.request_interrupt();
|
|
|
|
th.join();
|
|
|
|
return r;
|
|
|
|
} catch (...) {
|
|
|
|
th.request_interrupt();
|
|
|
|
th.join();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-11-23 00:15:03 +00:00
|
|
|
|
|
|
|
tactic append(tactic t1, tactic t2) {
|
|
|
|
return mk_tactic([=](environment const & env, io_state const & io, proof_state const & s) -> proof_state_seq {
|
|
|
|
tactic _t1(t1);
|
|
|
|
tactic _t2(t2);
|
|
|
|
return append(_t1(env, io, s), _t2(env, io, s));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tactic interleave(tactic t1, tactic t2) {
|
|
|
|
return mk_tactic([=](environment const & env, io_state const & io, proof_state const & s) -> proof_state_seq {
|
|
|
|
tactic _t1(t1);
|
|
|
|
tactic _t2(t2);
|
|
|
|
return interleave(_t1(env, io, s), _t2(env, io, s));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tactic par(tactic t1, tactic t2, unsigned check_ms) {
|
|
|
|
return mk_tactic([=](environment const & env, io_state const & io, proof_state const & s) -> proof_state_seq {
|
|
|
|
tactic _t1(t1);
|
|
|
|
tactic _t2(t2);
|
|
|
|
proof_state_seq r1;
|
|
|
|
proof_state_seq r2;
|
|
|
|
std::atomic<bool> done1(false);
|
|
|
|
std::atomic<bool> done2(false);
|
|
|
|
interruptible_thread th1([&]() {
|
|
|
|
try {
|
|
|
|
r1 = _t1(env, io, s);
|
|
|
|
} catch (...) {
|
|
|
|
r1 = proof_state_seq();
|
|
|
|
}
|
|
|
|
done1 = true;
|
|
|
|
});
|
|
|
|
interruptible_thread th2([&]() {
|
|
|
|
try {
|
|
|
|
r2 = _t2(env, io, s);
|
|
|
|
} catch (...) {
|
|
|
|
r2 = proof_state_seq();
|
|
|
|
}
|
|
|
|
done2 = true;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
std::chrono::milliseconds small(check_ms);
|
|
|
|
while (!done1 && !done2) {
|
|
|
|
check_interrupted();
|
|
|
|
std::this_thread::sleep_for(small);
|
|
|
|
}
|
|
|
|
th1.request_interrupt();
|
|
|
|
th2.request_interrupt();
|
|
|
|
th1.join();
|
|
|
|
th2.join();
|
2013-11-23 23:33:25 +00:00
|
|
|
return orelse(r1, r2);
|
2013-11-23 00:15:03 +00:00
|
|
|
} catch (...) {
|
|
|
|
th1.request_interrupt();
|
|
|
|
th2.request_interrupt();
|
|
|
|
th1.join();
|
|
|
|
th2.join();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-11-23 00:39:25 +00:00
|
|
|
|
|
|
|
tactic repeat(tactic t) {
|
|
|
|
return mk_tactic([=](environment const & env, io_state const & io, proof_state const & s1) -> proof_state_seq {
|
|
|
|
tactic t1(t);
|
|
|
|
proof_state_seq r = t1(env, io, s1);
|
2013-11-23 23:33:25 +00:00
|
|
|
auto p = r.pull();
|
|
|
|
if (!p) {
|
|
|
|
return to_proof_state_seq(s1);
|
2013-11-23 00:39:25 +00:00
|
|
|
} else {
|
2013-11-23 23:33:25 +00:00
|
|
|
return map_append(to_proof_state_seq(p), [=](proof_state const & s2) {
|
2013-11-23 00:39:25 +00:00
|
|
|
check_interrupted();
|
|
|
|
tactic t2 = repeat(t1);
|
|
|
|
return t2(env, io, s2);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tactic repeat_at_most(tactic t, unsigned k) {
|
|
|
|
return mk_tactic([=](environment const & env, io_state const & io, proof_state const & s1) -> proof_state_seq {
|
|
|
|
if (k == 0)
|
2013-11-23 23:33:25 +00:00
|
|
|
return to_proof_state_seq(s1);
|
2013-11-23 00:39:25 +00:00
|
|
|
tactic t1(t);
|
|
|
|
proof_state_seq r = t1(env, io, s1);
|
2013-11-23 23:33:25 +00:00
|
|
|
auto p = r.pull();
|
|
|
|
if (!p) {
|
|
|
|
return to_proof_state_seq(s1);
|
2013-11-23 00:39:25 +00:00
|
|
|
} else {
|
2013-11-23 23:33:25 +00:00
|
|
|
return map_append(to_proof_state_seq(p), [=](proof_state const & s2) {
|
2013-11-23 00:39:25 +00:00
|
|
|
check_interrupted();
|
|
|
|
tactic t2 = repeat_at_most(t1, k - 1);
|
|
|
|
return t2(env, io, s2);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-11-23 01:05:18 +00:00
|
|
|
|
|
|
|
tactic take(tactic t, unsigned k) {
|
|
|
|
return mk_tactic([=](environment const & env, io_state const & io, proof_state const & s) -> proof_state_seq {
|
|
|
|
tactic _t(t);
|
|
|
|
return take(k, _t(env, io, s));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
tactic force(tactic t) {
|
|
|
|
return mk_tactic([=](environment const & env, io_state const & io, proof_state const & s) -> proof_state_seq {
|
|
|
|
tactic _t(t);
|
|
|
|
proof_state_seq r = _t(env, io, s);
|
|
|
|
buffer<proof_state> buf;
|
2013-11-23 23:33:25 +00:00
|
|
|
for_each(r, [&](proof_state const & s2) {
|
|
|
|
buf.push_back(s2);
|
|
|
|
check_interrupted();
|
|
|
|
});
|
2013-11-23 01:05:18 +00:00
|
|
|
return to_lazy(to_list(buf.begin(), buf.end()));
|
|
|
|
});
|
|
|
|
}
|
2013-11-21 18:44:53 +00:00
|
|
|
}
|