2015-02-03 00:03:06 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include "library/kernel_serializer.h"
|
|
|
|
#include "library/tactic/rewrite_tactic.h"
|
|
|
|
#include "library/tactic/expr_to_tactic.h"
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
rewrite_element::rewrite_element():m_symm(false), m_unfold(true), m_multiplicity(rewrite_multiplicity::Once) {}
|
|
|
|
|
|
|
|
rewrite_element::rewrite_element(name const & l, bool symm, bool unfold, rewrite_multiplicity m,
|
2015-02-03 01:37:11 +00:00
|
|
|
optional<unsigned> const & n, location const & loc):
|
|
|
|
m_lemma(l), m_symm(symm), m_unfold(unfold), m_multiplicity(m), m_num(n), m_location(loc) {
|
2015-02-03 00:03:06 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 01:37:11 +00:00
|
|
|
rewrite_element rewrite_element::mk_unfold(name const & l, location const & loc) {
|
|
|
|
return rewrite_element(l, false, true, rewrite_multiplicity::Once, optional<unsigned>(), loc);
|
2015-02-03 00:03:06 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 01:37:11 +00:00
|
|
|
rewrite_element rewrite_element::mk_once(name const & l, bool symm, location const & loc) {
|
|
|
|
return rewrite_element(l, symm, false, rewrite_multiplicity::Once, optional<unsigned>(), loc);
|
2015-02-03 00:03:06 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 01:37:11 +00:00
|
|
|
rewrite_element rewrite_element::mk_at_most_n(name const & l, unsigned n, bool symm, location const & loc) {
|
|
|
|
return rewrite_element(l, symm, false, rewrite_multiplicity::AtMostN, optional<unsigned>(n), loc);
|
2015-02-03 00:03:06 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 01:37:11 +00:00
|
|
|
rewrite_element rewrite_element::mk_exactly_n(name const & l, unsigned n, bool symm, location const & loc) {
|
|
|
|
return rewrite_element(l, symm, false, rewrite_multiplicity::ExactlyN, optional<unsigned>(n), loc);
|
2015-02-03 00:03:06 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 01:37:11 +00:00
|
|
|
rewrite_element rewrite_element::mk_zero_or_more(name const & l, bool symm, location const & loc) {
|
|
|
|
return rewrite_element(l, symm, false, rewrite_multiplicity::ZeroOrMore, optional<unsigned>(), loc);
|
2015-02-03 00:03:06 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 01:37:11 +00:00
|
|
|
rewrite_element rewrite_element::mk_one_or_more(name const & l, bool symm, location const & loc) {
|
|
|
|
return rewrite_element(l, symm, false, rewrite_multiplicity::ZeroOrMore, optional<unsigned>(), loc);
|
2015-02-03 00:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serializer & operator<<(serializer & s, rewrite_element const & e) {
|
2015-02-03 01:37:11 +00:00
|
|
|
s << e.m_lemma << e.m_symm << e.m_unfold << static_cast<char>(e.m_multiplicity) << e.m_location;
|
2015-02-03 00:03:06 +00:00
|
|
|
if (e.has_num())
|
|
|
|
s << e.num();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer & operator>>(deserializer & d, rewrite_element & e) {
|
|
|
|
char multp;
|
2015-02-03 01:37:11 +00:00
|
|
|
d >> e.m_lemma >> e.m_symm >> e.m_unfold >> multp >> e.m_location;
|
2015-02-03 00:03:06 +00:00
|
|
|
e.m_multiplicity = static_cast<rewrite_multiplicity>(multp);
|
|
|
|
if (e.has_num())
|
|
|
|
e.m_num = d.read_unsigned();
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
static expr * g_rewrite_tac = nullptr;
|
|
|
|
static name * g_rewrite_elems_name = nullptr;
|
|
|
|
static std::string * g_rewrite_elems_opcode = nullptr;
|
|
|
|
|
|
|
|
[[ noreturn ]] static void throw_re_ex() { throw exception("unexpected occurrence of 'rewrite elements' expression"); }
|
|
|
|
|
|
|
|
class rewrite_elements_macro_cell : public macro_definition_cell {
|
|
|
|
list<rewrite_element> m_elems;
|
|
|
|
public:
|
2015-02-03 01:37:11 +00:00
|
|
|
rewrite_elements_macro_cell(list<rewrite_element> const & elems):m_elems(elems) {}
|
2015-02-03 00:03:06 +00:00
|
|
|
virtual name get_name() const { return *g_rewrite_elems_name; }
|
|
|
|
virtual pair<expr, constraint_seq> get_type(expr const &, extension_context &) const { throw_re_ex(); }
|
|
|
|
virtual optional<expr> expand(expr const &, extension_context &) const { throw_re_ex(); }
|
|
|
|
virtual void write(serializer & s) const {
|
2015-02-03 01:37:11 +00:00
|
|
|
s << *g_rewrite_elems_opcode;
|
2015-02-03 00:03:06 +00:00
|
|
|
write_list<rewrite_element>(s, m_elems);
|
|
|
|
|
|
|
|
}
|
|
|
|
list<rewrite_element> const & get_elems() const { return m_elems; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/** \brief Create a macro expression to encapsulate a list of rewrite elements */
|
2015-02-03 01:37:11 +00:00
|
|
|
expr mk_rewrite_elements(list<rewrite_element> const & e) {
|
|
|
|
macro_definition def(new rewrite_elements_macro_cell(e));
|
2015-02-03 00:03:06 +00:00
|
|
|
return mk_macro(def);
|
|
|
|
}
|
|
|
|
|
2015-02-03 01:37:11 +00:00
|
|
|
expr mk_rewrite_elements(buffer<rewrite_element> const & e) {
|
|
|
|
return mk_rewrite_elements(to_list(e));
|
2015-02-03 00:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Return true iff \c e is a "macro" that encapsulates a list of rewrite_elements */
|
|
|
|
bool is_rewrite_elements(expr const & e) {
|
|
|
|
return is_macro(e) && macro_def(e).get_name() == *g_rewrite_elems_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** \brief Copy the rewrite_elements stored in \c e into result.
|
|
|
|
\pre is_rewrite_elements(e)
|
|
|
|
*/
|
|
|
|
void get_rewrite_elements(expr const & e, buffer<rewrite_element> & result) {
|
|
|
|
lean_assert(is_rewrite_elements(e));
|
|
|
|
list<rewrite_element> const & l = static_cast<rewrite_elements_macro_cell const*>(macro_def(e).raw())->get_elems();
|
|
|
|
to_buffer(l, result);
|
|
|
|
}
|
|
|
|
|
2015-02-03 01:37:11 +00:00
|
|
|
expr mk_rewrite_tactic_expr(buffer<rewrite_element> const & elems) {
|
|
|
|
return mk_app(*g_rewrite_tac, mk_rewrite_elements(elems));
|
2015-02-03 01:02:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-03 01:37:11 +00:00
|
|
|
tactic mk_rewrite_tactic(buffer<rewrite_element> const & elems) {
|
2015-02-03 00:03:06 +00:00
|
|
|
// TODO(Leo)
|
|
|
|
for (auto const & e : elems)
|
|
|
|
std::cout << ">> " << e.get_name() << "\n";
|
|
|
|
return id_tactic();
|
|
|
|
}
|
|
|
|
|
|
|
|
void initialize_rewrite_tactic() {
|
|
|
|
name rewrite_tac_name{"tactic", "rewrite_tac"};
|
|
|
|
g_rewrite_tac = new expr(Const(rewrite_tac_name));
|
|
|
|
g_rewrite_elems_name = new name("rewrite_elements");
|
|
|
|
g_rewrite_elems_opcode = new std::string("RWE");
|
|
|
|
register_macro_deserializer(*g_rewrite_elems_opcode,
|
|
|
|
[](deserializer & d, unsigned num, expr const *) {
|
|
|
|
if (num != 0)
|
|
|
|
throw corrupted_stream_exception();
|
|
|
|
list<rewrite_element> elems = read_list<rewrite_element>(d);
|
2015-02-03 01:37:11 +00:00
|
|
|
return mk_rewrite_elements(elems);
|
2015-02-03 00:03:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
register_tac(rewrite_tac_name,
|
|
|
|
[](type_checker &, elaborate_fn const &, expr const & e, pos_info_provider const *) {
|
2015-02-03 01:02:14 +00:00
|
|
|
check_tactic_expr(app_arg(e), "invalid 'rewrite' tactic, invalid argument");
|
|
|
|
expr arg = get_tactic_expr_expr(app_arg(e));
|
|
|
|
if (!is_rewrite_elements(arg))
|
|
|
|
throw expr_to_tactic_exception(e, "invalid 'rewrite' tactic, invalid argument");
|
2015-02-03 00:03:06 +00:00
|
|
|
buffer<rewrite_element> elems;
|
2015-02-03 01:02:14 +00:00
|
|
|
get_rewrite_elements(arg, elems);
|
2015-02-03 01:37:11 +00:00
|
|
|
return mk_rewrite_tactic(elems);
|
2015-02-03 00:03:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void finalize_rewrite_tactic() {
|
|
|
|
delete g_rewrite_tac;
|
|
|
|
delete g_rewrite_elems_name;
|
|
|
|
delete g_rewrite_elems_opcode;
|
|
|
|
}
|
|
|
|
}
|