2013-11-21 01:02:41 +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
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
2013-11-21 18:44:53 +00:00
|
|
|
#include <memory>
|
2013-11-21 20:34:37 +00:00
|
|
|
#include <mutex>
|
2013-11-21 23:31:55 +00:00
|
|
|
#include "library/state.h"
|
2013-11-21 01:02:41 +00:00
|
|
|
#include "library/tactic/proof_state.h"
|
|
|
|
|
|
|
|
namespace lean {
|
2013-11-21 20:34:37 +00:00
|
|
|
typedef std::unique_ptr<proof_state> proof_state_ref;
|
|
|
|
class tactic_result;
|
|
|
|
typedef std::unique_ptr<tactic_result> tactic_result_ref;
|
|
|
|
|
2013-11-21 18:44:53 +00:00
|
|
|
class tactic_result {
|
2013-11-21 23:31:55 +00:00
|
|
|
std::mutex m_mutex;
|
|
|
|
std::atomic<bool> m_interrupted;
|
2013-11-21 20:34:37 +00:00
|
|
|
protected:
|
|
|
|
template<typename F>
|
|
|
|
void exclusive_update(F && f) {
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
virtual void interrupt();
|
|
|
|
void propagate_interrupt(tactic_result_ref & r) { if (r) r->interrupt(); }
|
2013-11-21 23:31:55 +00:00
|
|
|
virtual proof_state_ref next_core(environment const & env, state const & s) = 0;
|
2013-11-21 18:44:53 +00:00
|
|
|
public:
|
2013-11-21 23:31:55 +00:00
|
|
|
tactic_result():m_interrupted(false) {}
|
2013-11-21 20:34:37 +00:00
|
|
|
void request_interrupt();
|
2013-11-21 23:31:55 +00:00
|
|
|
proof_state_ref next(environment const & env, state const & s);
|
2013-11-21 18:44:53 +00:00
|
|
|
virtual ~tactic_result();
|
|
|
|
};
|
|
|
|
|
2013-11-21 01:02:41 +00:00
|
|
|
class tactic_cell {
|
|
|
|
void dealloc() { delete this; }
|
|
|
|
MK_LEAN_RC();
|
|
|
|
public:
|
2013-11-21 18:44:53 +00:00
|
|
|
virtual ~tactic_cell();
|
2013-11-21 23:31:55 +00:00
|
|
|
virtual tactic_result_ref operator()(proof_state const & s) = 0;
|
2013-11-21 01:02:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class tactic {
|
|
|
|
protected:
|
|
|
|
tactic_cell * m_ptr;
|
|
|
|
public:
|
|
|
|
explicit tactic(tactic_cell * ptr):m_ptr(ptr) { if (m_ptr) m_ptr->inc_ref(); }
|
|
|
|
tactic(tactic const & s):m_ptr(s.m_ptr) { if (m_ptr) m_ptr->inc_ref(); }
|
|
|
|
tactic(tactic && s):m_ptr(s.m_ptr) { s.m_ptr = nullptr; }
|
|
|
|
~tactic() { if (m_ptr) m_ptr->dec_ref(); }
|
|
|
|
friend void swap(tactic & a, tactic & b) { std::swap(a.m_ptr, b.m_ptr); }
|
2013-11-21 18:44:53 +00:00
|
|
|
tactic & operator=(tactic const & s);
|
|
|
|
tactic & operator=(tactic && s);
|
2013-11-21 01:02:41 +00:00
|
|
|
|
2013-11-21 23:31:55 +00:00
|
|
|
tactic_result_ref operator()(proof_state const & s) { return m_ptr->operator()(s); }
|
2013-11-21 01:02:41 +00:00
|
|
|
};
|
|
|
|
|
2013-11-21 23:31:55 +00:00
|
|
|
template<typename F>
|
|
|
|
class simple_tactic_cell : public tactic_cell {
|
|
|
|
F m_f;
|
|
|
|
public:
|
|
|
|
simple_tactic_cell(F && f):m_f(f) {}
|
|
|
|
|
|
|
|
class result : public tactic_result {
|
|
|
|
simple_tactic_cell * m_cell;
|
|
|
|
proof_state m_in;
|
|
|
|
public:
|
|
|
|
result(simple_tactic_cell * c, proof_state const & in):m_cell(c), m_in(in) {
|
|
|
|
lean_assert(m_cell);
|
|
|
|
m_cell->inc_ref();
|
|
|
|
}
|
|
|
|
virtual ~result() { if (m_cell) m_cell->dec_ref(); }
|
|
|
|
virtual proof_state_ref next_core(environment const & env, state const & io) {
|
|
|
|
if (m_cell) {
|
|
|
|
proof_state_ref r(new proof_state(m_cell->m_f(env, io, m_in)));
|
|
|
|
m_cell = nullptr;
|
|
|
|
return std::move(r);
|
|
|
|
} else {
|
|
|
|
return proof_state_ref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual tactic_result_ref operator()(proof_state const & s) {
|
|
|
|
return tactic_result_ref(new result(this, s));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
tactic mk_tactic(F && f) { return tactic(new simple_tactic_cell<F>(std::forward<F>(f))); }
|
|
|
|
|
|
|
|
tactic id_tactic();
|
|
|
|
tactic fail_tactic();
|
|
|
|
tactic now_tactic();
|
|
|
|
tactic assumption_tactic();
|
2013-11-21 20:34:37 +00:00
|
|
|
tactic then(tactic const & t1, tactic const & t2);
|
|
|
|
}
|