feat(library): add module for tracking noncomputable definitions

This commit is contained in:
Leonardo de Moura 2015-07-28 18:10:40 -07:00
parent 7e8a394caf
commit a009db2902
4 changed files with 126 additions and 1 deletions

View file

@ -15,6 +15,6 @@ add_library(library deep_copy.cpp expr_lt.cpp io_state.cpp occurs.cpp
unfold_macros.cpp app_builder.cpp projection.cpp abbreviation.cpp
relation_manager.cpp export.cpp user_recursors.cpp
class_instance_synth.cpp idx_metavar.cpp composition_manager.cpp
tc_multigraph.cpp)
tc_multigraph.cpp noncomputable.cpp)
target_link_libraries(library ${LEAN_LIBS})

View file

@ -40,6 +40,7 @@ Author: Leonardo de Moura
#include "library/user_recursors.h"
#include "library/class_instance_synth.h"
#include "library/composition_manager.h"
#include "library/noncomputable.h"
namespace lean {
void initialize_library_module() {
@ -79,9 +80,11 @@ void initialize_library_module() {
initialize_user_recursors();
initialize_class_instance_elaborator();
initialize_composition_manager();
initialize_noncomputable();
}
void finalize_library_module() {
finalize_noncomputable();
finalize_composition_manager();
finalize_class_instance_elaborator();
finalize_user_recursors();

View file

@ -0,0 +1,103 @@
/*
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 <utility>
#include <string>
#include "util/sstream.h"
#include "kernel/for_each_fn.h"
#include "kernel/type_checker.h"
#include "library/module.h"
#include "library/util.h"
#include "library/fingerprint.h"
namespace lean {
struct noncomputable_ext : public environment_extension {
name_set m_noncomputable;
noncomputable_ext() {}
};
struct noncomputable_ext_reg {
unsigned m_ext_id;
noncomputable_ext_reg() {
m_ext_id = environment::register_extension(std::make_shared<noncomputable_ext>());
}
};
static noncomputable_ext_reg * g_ext = nullptr;
static noncomputable_ext const & get_extension(environment const & env) {
return static_cast<noncomputable_ext const &>(env.get_extension(g_ext->m_ext_id));
}
static environment update(environment const & env, noncomputable_ext const & ext) {
return env.update(g_ext->m_ext_id, std::make_shared<noncomputable_ext>(ext));
}
static name * g_noncomputable = nullptr;
static std::string * g_key = nullptr;
static void noncomputable_reader(deserializer & d, shared_environment & senv,
std::function<void(asynch_update_fn const &)> &,
std::function<void(delayed_update_fn const &)> &) {
name n;
d >> n;
senv.update([=](environment const & env) -> environment {
noncomputable_ext ext = get_extension(env);
ext.m_noncomputable.insert(n);
return update(env, ext);
});
}
static bool is_noncomputable(type_checker & tc, noncomputable_ext const & ext, name const & n) {
if (ext.m_noncomputable.contains(n))
return true;
declaration const & d = tc.env().get(n);
return d.is_axiom() && !tc.is_prop(d.get_type()).first;
}
bool is_noncomputable(environment const & env, name const & n) {
type_checker tc(env);
auto ext = get_extension(env);
return is_noncomputable(tc, ext, n);
}
environment mark_noncomputable(environment const & env, name const & n) {
auto ext = get_extension(env);
ext.m_noncomputable.insert(n);
environment new_env = update(env, ext);
return module::add(new_env, *g_key, [=](environment const &, serializer & s) { s << n; });
}
void validate_computable_definition(environment const & env, name const & n) {
if (!is_standard(env))
return; // do nothing if it is not a standard kernel
type_checker tc(env);
declaration const & d = env.get(n);
if (!d.is_definition())
return;
if (tc.is_prop(d.get_type()).first)
return; // definition is a proposition, then do nothing
expr const & v = d.get_value();
auto ext = get_extension(env);
for_each(v, [&](expr const & e, unsigned) {
if (is_constant(e) && is_noncomputable(tc, ext, const_name(e))) {
throw exception(sstream() << "definition '" << n << "' is noncomputable, it depends on '" << const_name(e) << "'");
}
return true;
});
}
void initialize_noncomputable() {
g_ext = new noncomputable_ext_reg();
g_noncomputable = new name("noncomputable");
g_key = new std::string("ncomp");
register_module_object_reader(*g_key, noncomputable_reader);
}
void finalize_noncomputable() {
delete g_key;
delete g_noncomputable;
delete g_ext;
}
}

View file

@ -0,0 +1,19 @@
/*
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#pragma once
#include "kernel/environment.h"
namespace lean {
/** \brief Return true if \c n is noncomputable */
bool is_noncomputable(environment const & env, name const & n);
/** \brief Mark \c n as noncomputable */
environment mark_noncomputable(environment const & env, name const & n);
/** \brief Throw an exception if \c n depends on a noncomputable definition */
void validate_computable_definition(environment const & env, name const & n);
void initialize_noncomputable();
void finalize_noncomputable();
}