2013-09-25 22:38:16 +00:00
|
|
|
/*
|
2013-11-30 07:14:51 +00:00
|
|
|
Copyright (c) 2013 Microsoft Corporation.
|
|
|
|
Copyright (c) 2013 Carnegie Mellon University.
|
|
|
|
All rights reserved.
|
2013-09-25 22:38:16 +00:00
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Soonho Kong
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <utility>
|
|
|
|
#include <algorithm>
|
|
|
|
#include "util/exception.h"
|
|
|
|
#include "kernel/environment.h"
|
|
|
|
|
2013-11-30 07:14:51 +00:00
|
|
|
// TODO(soonhok)
|
2013-09-25 22:38:16 +00:00
|
|
|
// FORALL
|
|
|
|
// FAIL_IF
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
|
|
|
|
class rewriter_exception : public exception {
|
2013-11-27 20:19:54 +00:00
|
|
|
public:
|
|
|
|
virtual exception * clone() const { return new rewriter_exception(); }
|
|
|
|
virtual void rethrow() const { throw *this; }
|
2013-09-25 22:38:16 +00:00
|
|
|
};
|
|
|
|
|
2013-10-01 07:22:58 +00:00
|
|
|
enum class rewriter_kind { Theorem, OrElse, Then, Try, App,
|
|
|
|
LambdaType, LambdaBody, Lambda,
|
|
|
|
PiType, PiBody, Pi,
|
|
|
|
LetType, LetValue, LetBody, Let,
|
2013-11-30 07:14:51 +00:00
|
|
|
Fail, Success, Repeat, Depth };
|
2013-09-25 22:38:16 +00:00
|
|
|
|
|
|
|
class rewriter;
|
|
|
|
|
|
|
|
class rewriter_cell {
|
|
|
|
protected:
|
|
|
|
rewriter_kind m_kind;
|
|
|
|
MK_LEAN_RC();
|
|
|
|
void dealloc();
|
2013-10-01 07:22:58 +00:00
|
|
|
virtual std::ostream & display(std::ostream & out) const = 0;
|
2013-09-25 22:38:16 +00:00
|
|
|
public:
|
|
|
|
rewriter_cell(rewriter_kind k);
|
|
|
|
virtual ~rewriter_cell();
|
|
|
|
rewriter_kind kind() const { return m_kind; }
|
|
|
|
// unsigned hash() const { return m_hash; }
|
|
|
|
virtual std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception) = 0;
|
2013-10-01 07:22:58 +00:00
|
|
|
friend std::ostream & operator<<(std::ostream & out, rewriter_cell const & rw);
|
2013-09-25 22:38:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class rewriter {
|
|
|
|
private:
|
|
|
|
rewriter_cell * m_ptr;
|
|
|
|
public:
|
|
|
|
explicit rewriter(rewriter_cell * ptr):m_ptr(ptr) {}
|
|
|
|
rewriter():m_ptr(nullptr) {}
|
|
|
|
rewriter(rewriter const & r):m_ptr(r.m_ptr) {
|
|
|
|
if (m_ptr) m_ptr->inc_ref();
|
|
|
|
}
|
|
|
|
rewriter(rewriter && r):m_ptr(r.m_ptr) { r.m_ptr = nullptr; }
|
|
|
|
~rewriter() { if (m_ptr) m_ptr->dec_ref(); }
|
|
|
|
void release() { if (m_ptr) m_ptr->dec_ref(); m_ptr = nullptr; }
|
|
|
|
friend void swap(rewriter & a, rewriter & b) { std::swap(a.m_ptr, b.m_ptr); }
|
|
|
|
rewriter_kind kind() const { return m_ptr->kind(); }
|
|
|
|
rewriter & operator=(rewriter const & s) { LEAN_COPY_REF(rewriter, s); }
|
|
|
|
rewriter & operator=(rewriter && s) { LEAN_MOVE_REF(rewriter, s); }
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const {
|
|
|
|
return (*m_ptr)(env, ctx, v);
|
|
|
|
}
|
2013-10-01 07:22:58 +00:00
|
|
|
friend std::ostream & operator<<(std::ostream & out, rewriter const & rw);
|
2013-09-25 22:38:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class theorem_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
expr const & m_type;
|
|
|
|
expr const & m_body;
|
|
|
|
expr m_pattern;
|
|
|
|
expr m_rhs;
|
|
|
|
unsigned m_num_args;
|
2013-10-01 07:22:58 +00:00
|
|
|
std::ostream & display(std::ostream & out) const;
|
2013-09-25 22:38:16 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
theorem_rewriter_cell(expr const & type, expr const & body);
|
|
|
|
~theorem_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
|
|
|
class orelse_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
2013-09-25 23:46:39 +00:00
|
|
|
list<rewriter> m_rwlist;
|
2013-10-01 07:22:58 +00:00
|
|
|
std::ostream & display(std::ostream & out) const;
|
2013-09-25 22:38:16 +00:00
|
|
|
public:
|
|
|
|
orelse_rewriter_cell(rewriter const & rw1, rewriter const & rw2);
|
2013-09-25 23:46:39 +00:00
|
|
|
orelse_rewriter_cell(std::initializer_list<rewriter> const & l);
|
2013-09-25 22:38:16 +00:00
|
|
|
~orelse_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
|
|
|
class then_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
2013-09-25 23:46:39 +00:00
|
|
|
list<rewriter> m_rwlist;
|
2013-10-01 07:22:58 +00:00
|
|
|
std::ostream & display(std::ostream & out) const;
|
2013-09-25 22:38:16 +00:00
|
|
|
public:
|
|
|
|
then_rewriter_cell(rewriter const & rw1, rewriter const & rw2);
|
2013-09-25 23:46:39 +00:00
|
|
|
then_rewriter_cell(std::initializer_list<rewriter> const & l);
|
2013-09-25 22:38:16 +00:00
|
|
|
~then_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
2013-10-01 07:22:58 +00:00
|
|
|
class try_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
list<rewriter> m_rwlist;
|
|
|
|
std::ostream & display(std::ostream & out) const;
|
|
|
|
public:
|
|
|
|
try_rewriter_cell(rewriter const & rw1, rewriter const & rw2);
|
|
|
|
try_rewriter_cell(std::initializer_list<rewriter> const & l);
|
|
|
|
~try_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
2013-09-25 22:38:16 +00:00
|
|
|
class app_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
2013-10-01 07:22:58 +00:00
|
|
|
std::ostream & display(std::ostream & out) const;
|
2013-09-25 22:38:16 +00:00
|
|
|
public:
|
|
|
|
app_rewriter_cell(rewriter const & rw);
|
|
|
|
~app_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
2013-10-01 07:22:58 +00:00
|
|
|
class lambda_type_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
|
|
|
std::ostream & display(std::ostream & out) const;
|
|
|
|
public:
|
|
|
|
lambda_type_rewriter_cell(rewriter const & rw);
|
|
|
|
~lambda_type_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
|
|
|
class lambda_body_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
|
|
|
std::ostream & display(std::ostream & out) const;
|
|
|
|
public:
|
|
|
|
lambda_body_rewriter_cell(rewriter const & rw);
|
|
|
|
~lambda_body_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
2013-09-25 22:38:16 +00:00
|
|
|
class lambda_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
2013-10-01 07:22:58 +00:00
|
|
|
std::ostream & display(std::ostream & out) const;
|
2013-09-25 22:38:16 +00:00
|
|
|
public:
|
|
|
|
lambda_rewriter_cell(rewriter const & rw);
|
|
|
|
~lambda_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
2013-10-01 07:22:58 +00:00
|
|
|
class pi_type_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
|
|
|
std::ostream & display(std::ostream & out) const;
|
|
|
|
public:
|
|
|
|
pi_type_rewriter_cell(rewriter const & rw);
|
|
|
|
~pi_type_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
|
|
|
class pi_body_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
|
|
|
std::ostream & display(std::ostream & out) const;
|
|
|
|
public:
|
|
|
|
pi_body_rewriter_cell(rewriter const & rw);
|
|
|
|
~pi_body_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
2013-09-25 22:38:16 +00:00
|
|
|
class pi_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
2013-10-01 07:22:58 +00:00
|
|
|
std::ostream & display(std::ostream & out) const;
|
2013-09-25 22:38:16 +00:00
|
|
|
public:
|
|
|
|
pi_rewriter_cell(rewriter const & rw);
|
|
|
|
~pi_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
2013-10-01 07:22:58 +00:00
|
|
|
|
|
|
|
class let_type_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
|
|
|
std::ostream & display(std::ostream & out) const;
|
|
|
|
public:
|
|
|
|
let_type_rewriter_cell(rewriter const & rw);
|
|
|
|
~let_type_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
|
|
|
class let_value_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
|
|
|
std::ostream & display(std::ostream & out) const;
|
|
|
|
public:
|
|
|
|
let_value_rewriter_cell(rewriter const & rw);
|
|
|
|
~let_value_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
|
|
|
class let_body_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
|
|
|
std::ostream & display(std::ostream & out) const;
|
|
|
|
public:
|
|
|
|
let_body_rewriter_cell(rewriter const & rw);
|
|
|
|
~let_body_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
2013-09-25 22:38:16 +00:00
|
|
|
class let_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
2013-10-01 07:22:58 +00:00
|
|
|
std::ostream & display(std::ostream & out) const;
|
2013-09-25 22:38:16 +00:00
|
|
|
public:
|
|
|
|
let_rewriter_cell(rewriter const & rw);
|
|
|
|
~let_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
|
|
|
class fail_rewriter_cell : public rewriter_cell {
|
2013-10-01 07:22:58 +00:00
|
|
|
private:
|
|
|
|
std::ostream & display(std::ostream & out) const;
|
2013-09-25 22:38:16 +00:00
|
|
|
public:
|
|
|
|
fail_rewriter_cell();
|
|
|
|
~fail_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
|
|
|
class success_rewriter_cell : public rewriter_cell {
|
2013-10-01 07:22:58 +00:00
|
|
|
private:
|
|
|
|
std::ostream & display(std::ostream & out) const;
|
2013-09-25 22:38:16 +00:00
|
|
|
public:
|
|
|
|
success_rewriter_cell();
|
|
|
|
~success_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
|
|
|
class repeat_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
2013-10-01 07:22:58 +00:00
|
|
|
std::ostream & display(std::ostream & out) const;
|
2013-09-25 22:38:16 +00:00
|
|
|
public:
|
|
|
|
repeat_rewriter_cell(rewriter const & rw);
|
|
|
|
~repeat_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
2013-11-30 07:14:51 +00:00
|
|
|
class depth_rewriter_cell : public rewriter_cell {
|
|
|
|
private:
|
|
|
|
rewriter m_rw;
|
|
|
|
std::ostream & display(std::ostream & out) const;
|
|
|
|
public:
|
|
|
|
depth_rewriter_cell(rewriter const & rw);
|
|
|
|
~depth_rewriter_cell();
|
|
|
|
std::pair<expr, expr> operator()(environment const & env, context & ctx, expr const & v) const throw(rewriter_exception);
|
|
|
|
};
|
|
|
|
|
2013-10-01 07:22:58 +00:00
|
|
|
/** \brief (For debugging) Display the content of this rewriter */
|
|
|
|
inline std::ostream & operator<<(std::ostream & out, rewriter_cell const & rc) { rc.display(out); return out; }
|
|
|
|
inline std::ostream & operator<<(std::ostream & out, rewriter const & rw) { out << *(rw.m_ptr); return out; }
|
|
|
|
|
2013-09-25 22:38:16 +00:00
|
|
|
rewriter mk_theorem_rewriter(expr const & type, expr const & body);
|
|
|
|
rewriter mk_then_rewriter(rewriter const & rw1, rewriter const & rw2);
|
2013-09-25 23:46:39 +00:00
|
|
|
rewriter mk_then_rewriter(std::initializer_list<rewriter> const & l);
|
2013-10-01 07:22:58 +00:00
|
|
|
rewriter mk_try_rewriter(rewriter const & rw);
|
|
|
|
rewriter mk_try_rewriter(rewriter const & rw1, rewriter const & rw2);
|
|
|
|
rewriter mk_try_rewriter(std::initializer_list<rewriter> const & l);
|
2013-09-25 22:38:16 +00:00
|
|
|
rewriter mk_orelse_rewriter(rewriter const & rw1, rewriter const & rw2);
|
2013-09-25 23:46:39 +00:00
|
|
|
rewriter mk_orelse_rewriter(std::initializer_list<rewriter> const & l);
|
2013-09-25 22:38:16 +00:00
|
|
|
rewriter mk_app_rewriter(rewriter const & rw);
|
2013-10-01 07:22:58 +00:00
|
|
|
rewriter mk_lambda_type_rewriter(rewriter const & rw);
|
|
|
|
rewriter mk_lambda_body_rewriter(rewriter const & rw);
|
2013-09-25 22:38:16 +00:00
|
|
|
rewriter mk_lambda_rewriter(rewriter const & rw);
|
2013-10-01 07:22:58 +00:00
|
|
|
rewriter mk_pi_type_rewriter(rewriter const & rw);
|
|
|
|
rewriter mk_pi_body_rewriter(rewriter const & rw);
|
2013-09-25 22:38:16 +00:00
|
|
|
rewriter mk_pi_rewriter(rewriter const & rw);
|
2013-10-01 07:22:58 +00:00
|
|
|
rewriter mk_let_type_rewriter(rewriter const & rw);
|
|
|
|
rewriter mk_let_value_rewriter(rewriter const & rw);
|
|
|
|
rewriter mk_let_body_rewriter(rewriter const & rw);
|
2013-09-25 22:38:16 +00:00
|
|
|
rewriter mk_let_rewriter(rewriter const & rw);
|
|
|
|
rewriter mk_fail_rewriter();
|
|
|
|
rewriter mk_success_rewriter();
|
|
|
|
rewriter mk_repeat_rewriter(rewriter const & rw);
|
2013-11-30 07:14:51 +00:00
|
|
|
rewriter mk_depth_rewriter(rewriter const & rw);
|
|
|
|
|
|
|
|
std::pair<expr, expr> rewrite_lambda_type(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_ty);
|
|
|
|
std::pair<expr, expr> rewrite_lambda_body(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_body);
|
|
|
|
std::pair<expr, expr> rewrite_lambda(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_ty, std::pair<expr, expr> const & result_body);
|
|
|
|
std::pair<expr, expr> rewrite_pi_type(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_ty);
|
|
|
|
std::pair<expr, expr> rewrite_pi_body(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_body);
|
|
|
|
std::pair<expr, expr> rewrite_pi(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_ty, std::pair<expr, expr> const & result_body);
|
|
|
|
std::pair<expr, expr> rewrite_eq_lhs(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_lhs);
|
|
|
|
std::pair<expr, expr> rewrite_eq_rhs(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_rhs);
|
|
|
|
std::pair<expr, expr> rewrite_eq(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_lhs, std::pair<expr, expr> const & result_rhs);
|
|
|
|
std::pair<expr, expr> rewrite_let_type(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_ty);
|
|
|
|
std::pair<expr, expr> rewrite_let_value(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_value);
|
|
|
|
std::pair<expr, expr> rewrite_let_body(environment const & env, context & ctx, expr const & v, std::pair<expr, expr> const & result_body);
|
|
|
|
std::pair<expr, expr> rewrite_app(environment const & env, context & ctx, expr const & v, buffer<std::pair<expr, expr>> const & results );
|
2013-09-25 22:38:16 +00:00
|
|
|
}
|