Add Repeat/Success/Fail to rewrite (skeleton)

This commit is contained in:
Soonho Kong 2013-09-24 20:04:08 -07:00
parent ac0eafa1b6
commit 8e9bd9ee67
2 changed files with 75 additions and 7 deletions

View file

@ -158,6 +158,7 @@ pair<expr, expr> lambda_rewrite_cell::operator()(context & ctx, expr const & v,
throw rewrite_exception(); throw rewrite_exception();
} }
// Pi Rewrite
pi_rewrite_cell::pi_rewrite_cell(rewrite const & rw) pi_rewrite_cell::pi_rewrite_cell(rewrite const & rw)
:rewrite_cell(rewrite_kind::Pi), m_rw(rw) { } :rewrite_cell(rewrite_kind::Pi), m_rw(rw) { }
pi_rewrite_cell::~pi_rewrite_cell() { } pi_rewrite_cell::~pi_rewrite_cell() { }
@ -175,10 +176,10 @@ pair<expr, expr> pi_rewrite_cell::operator()(context & ctx, expr const & v, envi
throw rewrite_exception(); throw rewrite_exception();
} }
// Let rewrite
let_rewrite_cell::let_rewrite_cell(rewrite const & rw) let_rewrite_cell::let_rewrite_cell(rewrite const & rw)
:rewrite_cell(rewrite_kind::Let), m_rw(rw) { } :rewrite_cell(rewrite_kind::Let), m_rw(rw) { }
let_rewrite_cell::~let_rewrite_cell() { } let_rewrite_cell::~let_rewrite_cell() { }
pair<expr, expr> let_rewrite_cell::operator()(context & ctx, expr const & v, environment const & env) const throw(rewrite_exception) { pair<expr, expr> let_rewrite_cell::operator()(context & ctx, expr const & v, environment const & env) const throw(rewrite_exception) {
if (!is_let(v)) if (!is_let(v))
throw rewrite_exception(); throw rewrite_exception();
@ -195,6 +196,36 @@ pair<expr, expr> let_rewrite_cell::operator()(context & ctx, expr const & v, env
throw rewrite_exception(); throw rewrite_exception();
} }
// Fail rewrite
fail_rewrite_cell::fail_rewrite_cell():rewrite_cell(rewrite_kind::Fail) { }
fail_rewrite_cell::~fail_rewrite_cell() { }
pair<expr, expr> fail_rewrite_cell::operator()(context &, expr const &, environment const &) const throw(rewrite_exception) {
throw rewrite_exception();
}
// Success rewrite (trivial)
success_rewrite_cell::success_rewrite_cell():rewrite_cell(rewrite_kind::Success) { }
success_rewrite_cell::~success_rewrite_cell() { }
pair<expr, expr> success_rewrite_cell::operator()(context &, expr const & v, environment const &) const throw(rewrite_exception) {
return make_pair(v, mk_app(Const("Refl"), v));
}
// Repeat rewrite
repeat_rewrite_cell::repeat_rewrite_cell(rewrite const & rw):rewrite_cell(rewrite_kind::Repeat), m_rw(rw) { }
repeat_rewrite_cell::~repeat_rewrite_cell() { }
pair<expr, expr> repeat_rewrite_cell::operator()(context & ctx, expr const & v, environment const & env) const throw(rewrite_exception) {
pair<expr, expr> result = mk_success_rewrite()(ctx, v, env);
// TODO(soonhok) fix
try {
while (true) {
result = m_rw(ctx, result.first, env);
}
}
catch (rewrite_exception & ) {
return result;
}
}
rewrite mk_theorem_rewrite(expr const & type, expr const & body) { rewrite mk_theorem_rewrite(expr const & type, expr const & body) {
return rewrite(new theorem_rewrite_cell(type, body)); return rewrite(new theorem_rewrite_cell(type, body));
} }
@ -204,5 +235,25 @@ rewrite mk_then_rewrite(rewrite const & rw1, rewrite const & rw2) {
rewrite mk_orelse_rewrite(rewrite const & rw1, rewrite const & rw2) { rewrite mk_orelse_rewrite(rewrite const & rw1, rewrite const & rw2) {
return rewrite(new orelse_rewrite_cell(rw1, rw2)); return rewrite(new orelse_rewrite_cell(rw1, rw2));
} }
rewrite mk_app_rewrite(rewrite const & rw) {
return rewrite(new app_rewrite_cell(rw));
}
rewrite mk_lambda_rewrite(rewrite const & rw) {
return rewrite(new lambda_rewrite_cell(rw));
}
rewrite mk_pi_rewrite(rewrite const & rw) {
return rewrite(new pi_rewrite_cell(rw));
}
rewrite mk_let_rewrite(rewrite const & rw) {
return rewrite(new let_rewrite_cell(rw));
}
rewrite mk_fail_rewrite() {
return rewrite(new fail_rewrite_cell());
}
rewrite mk_success_rewrite() {
return rewrite(new success_rewrite_cell());
}
rewrite mk_repeat_rewrite(rewrite const & rw) {
return rewrite(new repeat_rewrite_cell(rw));
}
} }

View file

@ -26,7 +26,7 @@ namespace lean {
class rewrite_exception : public exception { class rewrite_exception : public exception {
}; };
enum class rewrite_kind { Theorem, OrElse, Then, App, Lambda, Pi, Let }; enum class rewrite_kind {Theorem, OrElse, Then, App, Lambda, Pi, Let, Fail, Success, Repeat};
class rewrite; class rewrite;
@ -136,10 +136,25 @@ public:
class fail_rewrite_cell : public rewrite_cell { class fail_rewrite_cell : public rewrite_cell {
public: public:
fail_rewrite_cell(rewrite const & rw1, rewrite const & rw2); fail_rewrite_cell();
std::pair<expr, expr> operator()(context &, expr const &) const throw(rewrite_exception) { ~fail_rewrite_cell();
throw rewrite_exception(); std::pair<expr, expr> operator()(context &, expr const &, environment const &) const throw(rewrite_exception);
} };
class success_rewrite_cell : public rewrite_cell {
public:
success_rewrite_cell();
~success_rewrite_cell();
std::pair<expr, expr> operator()(context &, expr const &, environment const & env) const throw(rewrite_exception);
};
class repeat_rewrite_cell : public rewrite_cell {
private:
rewrite m_rw;
public:
repeat_rewrite_cell(rewrite const & rw);
~repeat_rewrite_cell();
std::pair<expr, expr> operator()(context &, expr const &, environment const & env) const throw(rewrite_exception);
}; };
rewrite mk_theorem_rewrite(expr const & type, expr const & body); rewrite mk_theorem_rewrite(expr const & type, expr const & body);
@ -150,4 +165,6 @@ rewrite mk_lambda_rewrite(rewrite const & rw);
rewrite mk_pi_rewrite(rewrite const & rw); rewrite mk_pi_rewrite(rewrite const & rw);
rewrite mk_let_rewrite(rewrite const & rw); rewrite mk_let_rewrite(rewrite const & rw);
rewrite mk_fail_rewrite(); rewrite mk_fail_rewrite();
rewrite mk_success_rewrite();
rewrite mk_repeat_rewrite(rewrite const & rw);
} }