feat(library/simplifier): initialize simplification set.

This commit is contained in:
Leonardo de Moura 2015-07-27 12:57:28 -07:00
parent 4131bb3dec
commit 966e0109ff
5 changed files with 120 additions and 24 deletions

View file

@ -392,30 +392,16 @@ static void print_simp_rules(parser & p) {
} else {
s = get_simp_rule_sets(p.env());
}
name prev_eqv;
s.for_each_simp([&](name const & eqv, simp_rule const & rw) {
if (prev_eqv != eqv) {
out << "simplification rules for " << eqv;
format header;
if (!ns.is_anonymous())
out << " at namespace '" << ns << "'";
out << "\n";
prev_eqv = eqv;
}
out << rw.pp(out.get_formatter()) << "\n";
});
header = format(" at namespace '") + format(ns) + format("'");
out << s.pp_simp(out.get_formatter(), header);
}
static void print_congr_rules(parser & p) {
io_state_stream out = p.regular_stream();
simp_rule_sets s = get_simp_rule_sets(p.env());
name prev_eqv;
s.for_each_congr([&](name const & eqv, congr_rule const & cr) {
if (prev_eqv != eqv) {
out << "congruencec rules for " << eqv << "\n";
prev_eqv = eqv;
}
out << cr.pp(out.get_formatter()) << "\n";
});
out << s.pp_congr(out.get_formatter());
}
environment print_cmd(parser & p) {

View file

@ -51,11 +51,7 @@ expr parse_simp_tactic(parser & p) {
p.next();
p.check_token_next(get_lbracket_tk(), "invalid 'simp' tactic, '[' expected");
while (true) {
auto id_pos = p.pos();
name id = p.check_constant_next("invalid 'simp' tactic, constant expected");
if (!is_simp_rule(p.env(), id))
throw parser_error(sstream() << "invalid 'simp' tactic, '" << id
<< "' is not an active simplification rule", id_pos);
hiding.push_back(id);
if (!p.curr_is_token(get_comma_tk()))
break;

View file

@ -99,6 +99,24 @@ void simp_rule_set::for_each_congr(std::function<void(congr_rule const &)> const
m_congr_set.for_each_entry([&](head_index const &, congr_rule const & r) { fn(r); });
}
void simp_rule_set::erase_simp(name_set const & ids) {
// This method is not very smart and doesn't use any indexing or caching.
// So, it may be a bottleneck in the future
buffer<simp_rule> to_delete;
for_each_simp([&](simp_rule const & r) {
if (ids.contains(r.get_id())) {
to_delete.push_back(r);
}
});
for (simp_rule const & r : to_delete) {
erase(r);
}
}
void simp_rule_set::erase_simp(buffer<name> const & ids) {
erase_simp(to_name_set(ids));
}
template<typename R>
void simp_rule_sets::insert_core(name const & eqv, R const & r) {
simp_rule_set s(eqv);
@ -143,6 +161,20 @@ void simp_rule_sets::get_relations(buffer<name> & rs) const {
});
}
void simp_rule_sets::erase_simp(name_set const & ids) {
name_map<simp_rule_set> new_sets;
m_sets.for_each([&](name const & n, simp_rule_set const & s) {
simp_rule_set new_s = s;
new_s.erase_simp(ids);
new_sets.insert(n, new_s);
});
m_sets = new_sets;
}
void simp_rule_sets::erase_simp(buffer<name> const & ids) {
erase_simp(to_name_set(ids));
}
simp_rule_set const * simp_rule_sets::find(name const & eqv) const {
return m_sets.find(eqv);
}
@ -175,6 +207,50 @@ void simp_rule_sets::for_each_congr(std::function<void(name const &, congr_rule
});
}
format simp_rule_sets::pp(formatter const & fmt, format const & header, bool simp, bool congr) const {
format r;
if (simp) {
name prev_eqv;
for_each_simp([&](name const & eqv, simp_rule const & rw) {
if (prev_eqv != eqv) {
r += format("simplification rules for ") + format(eqv);
r += header;
r += line();
prev_eqv = eqv;
}
r += rw.pp(fmt) + line();
});
}
if (congr) {
name prev_eqv;
for_each_congr([&](name const & eqv, congr_rule const & cr) {
if (prev_eqv != eqv) {
r += format("congruencec rules for ") + format(eqv) + line();
prev_eqv = eqv;
}
r += cr.pp(fmt) + line();
});
}
return r;
}
format simp_rule_sets::pp_simp(formatter const & fmt, format const & header) const {
return pp(fmt, header, true, false);
}
format simp_rule_sets::pp_simp(formatter const & fmt) const {
return pp(fmt, format(), true, false);
}
format simp_rule_sets::pp_congr(formatter const & fmt) const {
return pp(fmt, format(), false, true);
}
format simp_rule_sets::pp(formatter const & fmt) const {
return pp(fmt, format(), true, true);
}
static name * g_prefix = nullptr;
simp_rule_sets add_core(type_checker & tc, simp_rule_sets const & s,
@ -458,6 +534,12 @@ simp_rule_sets get_simp_rule_sets(environment const & env, name const & ns) {
return set;
}
io_state_stream const & operator<<(io_state_stream const & out, simp_rule_sets const & s) {
options const & opts = out.get_options();
out.get_stream() << mk_pair(s.pp(out.get_formatter()), opts);
return out;
}
void initialize_simp_rule_set() {
g_prefix = new name(name::mk_internal_unique_name());
g_class_name = new name("rrs");

View file

@ -7,6 +7,7 @@ Author: Leonardo de Moura
#pragma once
#include "kernel/type_checker.h"
#include "library/head_map.h"
#include "library/io_state_stream.h"
namespace lean {
class simp_rule_sets;
@ -75,6 +76,8 @@ public:
void erase(simp_rule const & r);
void insert(congr_rule const & r);
void erase(congr_rule const & r);
void erase_simp(name_set const & ids);
void erase_simp(buffer<name> const & ids);
list<simp_rule> const * find_simp(head_index const & h) const;
void for_each_simp(std::function<void(simp_rule const &)> const & fn) const;
list<congr_rule> const * find_congr(head_index const & h) const;
@ -90,12 +93,19 @@ public:
void erase(name const & eqv, simp_rule const & r);
void insert(name const & eqv, congr_rule const & r);
void erase(name const & eqv, congr_rule const & r);
void erase_simp(name_set const & ids);
void erase_simp(buffer<name> const & ids);
void get_relations(buffer<name> & rs) const;
simp_rule_set const * find(name const & eqv) const;
list<simp_rule> const * find_simp(name const & eqv, head_index const & h) const;
list<congr_rule> const * find_congr(name const & eqv, head_index const & h) const;
void for_each_simp(std::function<void(name const &, simp_rule const &)> const & fn) const;
void for_each_congr(std::function<void(name const &, congr_rule const &)> const & fn) const;
format pp(formatter const & fmt, format const & header, bool simp, bool congr) const;
format pp_simp(formatter const & fmt, format const & header) const;
format pp_simp(formatter const & fmt) const;
format pp_congr(formatter const & fmt) const;
format pp(formatter const & fmt) const;
};
simp_rule_sets add(type_checker & tc, simp_rule_sets const & s, name const & id, expr const & e, expr const & h);
@ -112,6 +122,9 @@ bool is_congr_rule(environment const & env, name const & n);
simp_rule_sets get_simp_rule_sets(environment const & env);
/** \brief Get simplification rule sets in the given namespace. */
simp_rule_sets get_simp_rule_sets(environment const & env, name const & ns);
io_state_stream const & operator<<(io_state_stream const & out, simp_rule_sets const & s);
void initialize_simp_rule_set();
void finalize_simp_rule_set();
}

View file

@ -9,6 +9,7 @@ Author: Leonardo de Moura
#include "library/util.h"
#include "library/tactic/expr_to_tactic.h"
#include "library/simplifier/simp_tactic.h"
#include "library/simplifier/simp_rule_set.h"
#ifndef LEAN_DEFAULT_SIMP_SINGLE_PASS
#define LEAN_DEFAULT_SIMP_SINGLE_PASS false
@ -135,6 +136,8 @@ private:
goal m_g;
substitution m_subst;
simp_rule_sets m_simp_set;
// configuration options
bool m_single_pass;
bool m_bottom_up;
@ -148,6 +151,8 @@ private:
bool m_propext;
bool m_standard;
io_state_stream out() const { return regular(m_env, m_ios); }
void set_options(environment const & env, options const & o) {
m_single_pass = get_simp_single_pass(o);
m_bottom_up = get_simp_bottom_up(o);
@ -179,11 +184,25 @@ private:
return false;
}
// Initialize m_simp_set with information that is context independent
void init_simp_set(buffer<name> const & ns, buffer<name> const & ex) {
m_simp_set = get_simp_rule_sets(m_env);
for (name const & n : ns) {
simp_rule_sets tmp_simp_set = get_simp_rule_sets(m_env, n);
m_simp_set = join(m_simp_set, tmp_simp_set);
}
m_simp_set.erase_simp(ex);
if (m_trace) {
out() << m_simp_set;
}
}
public:
simp_tactic_fn(environment const & env, io_state const & ios, name_generator && ngen,
buffer<expr> const & /* ls */, buffer<name> const & /* ns */, buffer<name> const & /* ex */,
buffer<expr> const & /* ls */, buffer<name> const & ns, buffer<name> const & ex,
optional<tactic> const & tac):m_env(env), m_ios(ios), m_ngen(ngen), m_tactic(tac) {
set_options(env, m_ios.get_options());
init_simp_set(ns, ex);
}
std::tuple<res_kind, goal, substitution> operator()(goal const & g, location const & loc, substitution const & s) {