refactor(library): injectivity ==> inverse
This commit is contained in:
parent
75155c3824
commit
9c8e9e07ae
4 changed files with 52 additions and 50 deletions
|
|
@ -14,7 +14,7 @@ add_library(library OBJECT deep_copy.cpp expr_lt.cpp io_state.cpp
|
|||
local_context.cpp metavar_context.cpp type_context.cpp export_decl.cpp delayed_abstraction.cpp
|
||||
fun_info.cpp congr_lemma.cpp defeq_canonizer.cpp scope_pos_info_provider.cpp
|
||||
inductive.cpp mpq_macro.cpp arith_instance_manager.cpp replace_visitor_with_tc.cpp
|
||||
aux_definition.cpp injectivity.cpp
|
||||
aux_definition.cpp inverse.cpp
|
||||
|
||||
# Legacy -- The following files will be eventually deleted
|
||||
normalize.cpp justification.cpp constraint.cpp metavar.cpp choice.cpp locals.cpp
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ Author: Leonardo de Moura
|
|||
#include "library/inductive.h"
|
||||
#include "library/mpq_macro.h"
|
||||
#include "library/arith_instance_manager.h"
|
||||
#include "library/injectivity.h"
|
||||
#include "library/inverse.h"
|
||||
|
||||
// #include "library/congr_lemma_manager.h"
|
||||
// #include "library/light_lt_manager.h"
|
||||
|
|
@ -130,11 +130,11 @@ void initialize_library_module() {
|
|||
initialize_library_inductive();
|
||||
initialize_mpq_macro();
|
||||
initialize_arith_instance_manager();
|
||||
initialize_injectivity();
|
||||
initialize_inverse();
|
||||
}
|
||||
|
||||
void finalize_library_module() {
|
||||
finalize_injectivity();
|
||||
finalize_inverse();
|
||||
finalize_arith_instance_manager();
|
||||
finalize_mpq_macro();
|
||||
finalize_library_inductive();
|
||||
|
|
|
|||
|
|
@ -9,38 +9,38 @@ Author: Leonardo de Moura
|
|||
#include "library/util.h"
|
||||
#include "library/scoped_ext.h"
|
||||
#include "library/attribute_manager.h"
|
||||
#include "library/injectivity.h"
|
||||
#include "library/inverse.h"
|
||||
|
||||
namespace lean {
|
||||
struct injectivity_entry {
|
||||
name m_fn;
|
||||
injectivity_info m_info;
|
||||
injectivity_entry() {}
|
||||
injectivity_entry(name const & fn, injectivity_info const & info):
|
||||
struct inverse_entry {
|
||||
name m_fn;
|
||||
inverse_info m_info;
|
||||
inverse_entry() {}
|
||||
inverse_entry(name const & fn, inverse_info const & info):
|
||||
m_fn(fn), m_info(info) {}
|
||||
};
|
||||
|
||||
struct injectivity_state {
|
||||
name_map<injectivity_info> m_fn_info; /* mapping from function to its inverse and lemma */
|
||||
name_map<name> m_inv_info; /* mapping from inverse to function */
|
||||
struct inverse_state {
|
||||
name_map<inverse_info> m_fn_info; /* mapping from function to its inverse and lemma */
|
||||
name_map<name> m_inv_info; /* mapping from inverse to function */
|
||||
|
||||
void add(injectivity_entry const & e) {
|
||||
void add(inverse_entry const & e) {
|
||||
m_fn_info.insert(e.m_fn, e.m_info);
|
||||
m_inv_info.insert(e.m_info.m_inv, e.m_fn);
|
||||
}
|
||||
};
|
||||
|
||||
static name * g_injectivity_name = nullptr;
|
||||
static name * g_inverse_name = nullptr;
|
||||
static std::string * g_key = nullptr;
|
||||
|
||||
struct injectivity_config {
|
||||
typedef injectivity_state state;
|
||||
typedef injectivity_entry entry;
|
||||
struct inverse_config {
|
||||
typedef inverse_state state;
|
||||
typedef inverse_entry entry;
|
||||
static void add_entry(environment const &, io_state const &, state & s, entry const & e) {
|
||||
s.add(e);
|
||||
}
|
||||
static name const & get_class_name() {
|
||||
return *g_injectivity_name;
|
||||
return *g_inverse_name;
|
||||
}
|
||||
static std::string const & get_serialization_key() {
|
||||
return *g_key;
|
||||
|
|
@ -58,68 +58,69 @@ struct injectivity_config {
|
|||
}
|
||||
};
|
||||
|
||||
template class scoped_ext<injectivity_config>;
|
||||
typedef scoped_ext<injectivity_config> injectivity_ext;
|
||||
template class scoped_ext<inverse_config>;
|
||||
typedef scoped_ext<inverse_config> inverse_ext;
|
||||
|
||||
optional<injectivity_info> has_inverse(environment const & env, name const & fn) {
|
||||
if (auto r = injectivity_ext::get_state(env).m_fn_info.find(fn))
|
||||
return optional<injectivity_info>(*r);
|
||||
optional<inverse_info> has_inverse(environment const & env, name const & fn) {
|
||||
if (auto r = inverse_ext::get_state(env).m_fn_info.find(fn))
|
||||
return optional<inverse_info>(*r);
|
||||
else
|
||||
return optional<injectivity_info>();
|
||||
return optional<inverse_info>();
|
||||
}
|
||||
|
||||
optional<name> is_inverse(environment const & env, name const & inv) {
|
||||
if (auto r = injectivity_ext::get_state(env).m_inv_info.find(inv))
|
||||
if (auto r = inverse_ext::get_state(env).m_inv_info.find(inv))
|
||||
return optional<name>(*r);
|
||||
else
|
||||
return optional<name>();
|
||||
}
|
||||
|
||||
void throw_injectivity_error() {
|
||||
throw exception("invalid injectivity lemma, "
|
||||
void throw_inverse_error() {
|
||||
throw exception("invalid inverse lemma, "
|
||||
"it should be of the form: (f ... (g ... x)) = x");
|
||||
}
|
||||
|
||||
environment add_injectivity_lemma(environment const & env, name const & lemma, bool persistent) {
|
||||
environment add_inverse_lemma(environment const & env, name const & lemma, bool persistent) {
|
||||
type_checker tc(env);
|
||||
declaration d = env.get(lemma);
|
||||
buffer<expr> tele;
|
||||
expr type = to_telescope(tc, d.get_type(), tele);
|
||||
expr lhs, rhs;
|
||||
if (!is_eq(type, lhs, rhs) || !is_app(lhs) || !is_constant(get_app_fn(lhs)) || !is_local(rhs))
|
||||
throw_injectivity_error();
|
||||
injectivity_info info;
|
||||
throw_inverse_error();
|
||||
inverse_info info;
|
||||
buffer<expr> lhs_args;
|
||||
expr const & lhs_fn = get_app_args(lhs, lhs_args);
|
||||
info.m_inv = const_name(lhs_fn);
|
||||
info.m_lemma = lemma;
|
||||
info.m_inv = const_name(lhs_fn);
|
||||
info.m_inv_arity = lhs_args.size();
|
||||
info.m_lemma = lemma;
|
||||
expr const & last_arg = lhs_args.back();
|
||||
if (!is_app(last_arg) || !is_constant(get_app_fn(last_arg)))
|
||||
throw_injectivity_error();
|
||||
throw_inverse_error();
|
||||
buffer<expr> last_arg_args;
|
||||
expr const & fn = get_app_args(last_arg, last_arg_args);
|
||||
if (last_arg_args.back() != rhs)
|
||||
throw_injectivity_error();
|
||||
throw_inverse_error();
|
||||
info.m_arity = last_arg_args.size();
|
||||
return injectivity_ext::add_entry(env, get_dummy_ios(), injectivity_entry(const_name(fn), info), persistent);
|
||||
return inverse_ext::add_entry(env, get_dummy_ios(), inverse_entry(const_name(fn), info), persistent);
|
||||
}
|
||||
|
||||
void initialize_injectivity() {
|
||||
g_injectivity_name = new name("injectivity");
|
||||
g_key = new std::string("injectivity");
|
||||
injectivity_ext::initialize();
|
||||
void initialize_inverse() {
|
||||
g_inverse_name = new name("inverse");
|
||||
g_key = new std::string("inverse");
|
||||
inverse_ext::initialize();
|
||||
|
||||
register_system_attribute(basic_attribute("injectivity", "attribute for marking injectivity lemmas "
|
||||
register_system_attribute(basic_attribute("inverse", "attribute for marking inverse lemmas "
|
||||
"used by the equation compiler",
|
||||
[](environment const & env, io_state const &, name const & d, unsigned,
|
||||
bool persistent) {
|
||||
return add_injectivity_lemma(env, d, persistent);
|
||||
return add_inverse_lemma(env, d, persistent);
|
||||
}));
|
||||
}
|
||||
|
||||
void finalize_injectivity() {
|
||||
injectivity_ext::finalize();
|
||||
void finalize_inverse() {
|
||||
inverse_ext::finalize();
|
||||
delete g_key;
|
||||
delete g_injectivity_name;
|
||||
delete g_inverse_name;
|
||||
}
|
||||
}
|
||||
|
|
@ -8,15 +8,16 @@ Author: Leonardo de Moura
|
|||
#include "kernel/environment.h"
|
||||
|
||||
namespace lean {
|
||||
struct injectivity_info {
|
||||
struct inverse_info {
|
||||
unsigned m_arity;
|
||||
name m_inv;
|
||||
unsigned m_inv_arity;
|
||||
name m_lemma;
|
||||
};
|
||||
|
||||
optional<injectivity_info> has_inverse(environment const & env, name const & fn);
|
||||
optional<inverse_info> has_inverse(environment const & env, name const & fn);
|
||||
optional<name> is_inverse(environment const & env, name const & inv);
|
||||
environment add_injectivity_lemma(environment const & env, name const & lemma, bool persistent);
|
||||
void initialize_injectivity();
|
||||
void finalize_injectivity();
|
||||
environment add_inverse_lemma(environment const & env, name const & lemma, bool persistent);
|
||||
void initialize_inverse();
|
||||
void finalize_inverse();
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue