fix(library/module): do not store full path of imported modules
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
e59889d84f
commit
6891f48c67
4 changed files with 34 additions and 31 deletions
|
@ -1016,27 +1016,28 @@ static std::string g_lua_module_key("lua_module");
|
||||||
static void lua_module_reader(deserializer & d, module_idx, shared_environment &,
|
static void lua_module_reader(deserializer & d, module_idx, shared_environment &,
|
||||||
std::function<void(asynch_update_fn const &)> &,
|
std::function<void(asynch_update_fn const &)> &,
|
||||||
std::function<void(delayed_update_fn const &)> & add_delayed_update) {
|
std::function<void(delayed_update_fn const &)> & add_delayed_update) {
|
||||||
std::string fname;
|
name fname;
|
||||||
d >> fname;
|
d >> fname;
|
||||||
add_delayed_update([=](environment const & env, io_state const &) -> environment {
|
add_delayed_update([=](environment const & env, io_state const &) -> environment {
|
||||||
system_import(fname.c_str());
|
std::string rname = find_file(fname, {".lua"});
|
||||||
|
system_import(rname.c_str());
|
||||||
return env;
|
return env;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
register_module_object_reader_fn g_lua_module_reader(g_lua_module_key, lua_module_reader);
|
register_module_object_reader_fn g_lua_module_reader(g_lua_module_key, lua_module_reader);
|
||||||
|
|
||||||
void parser::parse_imports() {
|
void parser::parse_imports() {
|
||||||
buffer<std::string> olean_files;
|
buffer<name> olean_files;
|
||||||
buffer<std::string> lua_files;
|
buffer<name> lua_files;
|
||||||
while (curr_is_token(g_import)) {
|
while (curr_is_token(g_import)) {
|
||||||
m_last_cmd_pos = pos();
|
m_last_cmd_pos = pos();
|
||||||
next();
|
next();
|
||||||
while (curr_is_identifier()) {
|
while (curr_is_identifier()) {
|
||||||
name f = get_name_val();
|
name f = get_name_val();
|
||||||
if (auto it = find_file(f, ".lua")) {
|
if (auto it = find_file(f, ".lua")) {
|
||||||
lua_files.push_back(*it);
|
lua_files.push_back(f);
|
||||||
} else if (auto it = find_file(f, ".olean")) {
|
} else if (auto it = find_file(f, ".olean")) {
|
||||||
olean_files.push_back(*it);
|
olean_files.push_back(f);
|
||||||
} else {
|
} else {
|
||||||
throw parser_error(sstream() << "invalid import, unknow module '" << f << "'", pos());
|
throw parser_error(sstream() << "invalid import, unknow module '" << f << "'", pos());
|
||||||
}
|
}
|
||||||
|
@ -1045,7 +1046,8 @@ void parser::parse_imports() {
|
||||||
}
|
}
|
||||||
m_env = import_modules(m_env, olean_files.size(), olean_files.data(), m_num_threads, true, m_ios);
|
m_env = import_modules(m_env, olean_files.size(), olean_files.data(), m_num_threads, true, m_ios);
|
||||||
for (auto const & f : lua_files) {
|
for (auto const & f : lua_files) {
|
||||||
system_import(f.c_str());
|
std::string rname = find_file(f, {".lua"});
|
||||||
|
system_import(rname.c_str());
|
||||||
m_env = module::add(m_env, g_lua_module_key, [=](serializer & s) {
|
m_env = module::add(m_env, g_lua_module_key, [=](serializer & s) {
|
||||||
s << f;
|
s << f;
|
||||||
});
|
});
|
||||||
|
|
|
@ -1126,16 +1126,16 @@ static int environment_for_each_universe(lua_State * L) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void to_string_buffer(lua_State * L, int i, buffer<std::string> & r) {
|
static void to_name_buffer(lua_State * L, int i, buffer<name> & r) {
|
||||||
if (lua_isstring(L, i)) {
|
if (lua_isstring(L, i) || is_name(L, i)) {
|
||||||
r.push_back(lua_tostring(L, i));
|
r.push_back(to_name_ext(L, i));
|
||||||
} else {
|
} else {
|
||||||
luaL_checktype(L, i, LUA_TTABLE);
|
luaL_checktype(L, i, LUA_TTABLE);
|
||||||
lua_pushvalue(L, i);
|
lua_pushvalue(L, i);
|
||||||
int sz = objlen(L, -1);
|
int sz = objlen(L, -1);
|
||||||
for (int i = 1; i <= sz; i++) {
|
for (int i = 1; i <= sz; i++) {
|
||||||
lua_rawgeti(L, -1, i);
|
lua_rawgeti(L, -1, i);
|
||||||
r.push_back(lua_tostring(L, -1));
|
r.push_back(to_name_ext(L, -1));
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1143,8 +1143,8 @@ static void to_string_buffer(lua_State * L, int i, buffer<std::string> & r) {
|
||||||
|
|
||||||
static int import_modules(environment const & env, lua_State * L, int s) {
|
static int import_modules(environment const & env, lua_State * L, int s) {
|
||||||
int nargs = lua_gettop(L);
|
int nargs = lua_gettop(L);
|
||||||
buffer<std::string> mnames;
|
buffer<name> mnames;
|
||||||
to_string_buffer(L, s, mnames);
|
to_name_buffer(L, s, mnames);
|
||||||
unsigned num_threads = 1;
|
unsigned num_threads = 1;
|
||||||
bool keep_proofs = false;
|
bool keep_proofs = false;
|
||||||
if (nargs > s) {
|
if (nargs > s) {
|
||||||
|
|
|
@ -17,6 +17,7 @@ Author: Leonardo de Moura
|
||||||
#include "util/sstream.h"
|
#include "util/sstream.h"
|
||||||
#include "util/buffer.h"
|
#include "util/buffer.h"
|
||||||
#include "util/interrupt.h"
|
#include "util/interrupt.h"
|
||||||
|
#include "util/name_map.h"
|
||||||
#include "kernel/type_checker.h"
|
#include "kernel/type_checker.h"
|
||||||
#include "library/module.h"
|
#include "library/module.h"
|
||||||
#include "library/kernel_serializer.h"
|
#include "library/kernel_serializer.h"
|
||||||
|
@ -30,8 +31,8 @@ namespace lean {
|
||||||
typedef std::pair<std::string, std::function<void(serializer &)>> writer;
|
typedef std::pair<std::string, std::function<void(serializer &)>> writer;
|
||||||
|
|
||||||
struct module_ext : public environment_extension {
|
struct module_ext : public environment_extension {
|
||||||
list<std::string> m_direct_imports;
|
list<name> m_direct_imports;
|
||||||
list<writer> m_writers;
|
list<writer> m_writers;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct module_ext_reg {
|
struct module_ext_reg {
|
||||||
|
@ -52,7 +53,7 @@ static char const * g_olean_header = "oleanfile";
|
||||||
|
|
||||||
void export_module(std::ostream & out, environment const & env) {
|
void export_module(std::ostream & out, environment const & env) {
|
||||||
module_ext const & ext = get_extension(env);
|
module_ext const & ext = get_extension(env);
|
||||||
buffer<std::string> imports;
|
buffer<name> imports;
|
||||||
buffer<writer const *> writers;
|
buffer<writer const *> writers;
|
||||||
to_buffer(ext.m_direct_imports, imports);
|
to_buffer(ext.m_direct_imports, imports);
|
||||||
std::reverse(imports.begin(), imports.end());
|
std::reverse(imports.begin(), imports.end());
|
||||||
|
@ -169,7 +170,7 @@ struct import_modules_fn {
|
||||||
atomic<bool> m_all_modules_imported;
|
atomic<bool> m_all_modules_imported;
|
||||||
|
|
||||||
struct module_info {
|
struct module_info {
|
||||||
std::string m_name;
|
name m_name;
|
||||||
std::string m_fname;
|
std::string m_fname;
|
||||||
atomic<unsigned> m_counter; // number of dependencies to be processed
|
atomic<unsigned> m_counter; // number of dependencies to be processed
|
||||||
unsigned m_module_idx;
|
unsigned m_module_idx;
|
||||||
|
@ -178,7 +179,7 @@ struct import_modules_fn {
|
||||||
module_info():m_counter(0), m_module_idx(0) {}
|
module_info():m_counter(0), m_module_idx(0) {}
|
||||||
};
|
};
|
||||||
typedef std::shared_ptr<module_info> module_info_ptr;
|
typedef std::shared_ptr<module_info> module_info_ptr;
|
||||||
std::unordered_map<std::string, module_info_ptr> m_module_info;
|
name_map<module_info_ptr> m_module_info;
|
||||||
|
|
||||||
import_modules_fn(environment const & env, unsigned num_threads, bool keep_proofs, io_state const & ios):
|
import_modules_fn(environment const & env, unsigned num_threads, bool keep_proofs, io_state const & ios):
|
||||||
m_senv(env), m_num_threads(num_threads), m_keep_proofs(keep_proofs), m_ios(ios),
|
m_senv(env), m_num_threads(num_threads), m_keep_proofs(keep_proofs), m_ios(ios),
|
||||||
|
@ -195,10 +196,10 @@ struct import_modules_fn {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module_info_ptr load_module_file(std::string const & mname) {
|
module_info_ptr load_module_file(name const & mname) {
|
||||||
auto it = m_module_info.find(mname);
|
auto it = m_module_info.find(mname);
|
||||||
if (it != m_module_info.end())
|
if (it)
|
||||||
return it->second;
|
return *it;
|
||||||
std::string fname = find_file(mname, {".olean"});
|
std::string fname = find_file(mname, {".olean"});
|
||||||
std::ifstream in(fname, std::ifstream::binary);
|
std::ifstream in(fname, std::ifstream::binary);
|
||||||
if (!in.good())
|
if (!in.good())
|
||||||
|
@ -213,9 +214,9 @@ struct import_modules_fn {
|
||||||
// Enforce version?
|
// Enforce version?
|
||||||
|
|
||||||
unsigned num_imports = d1.read_unsigned();
|
unsigned num_imports = d1.read_unsigned();
|
||||||
buffer<std::string> imports;
|
buffer<name> imports;
|
||||||
for (unsigned i = 0; i < num_imports; i++)
|
for (unsigned i = 0; i < num_imports; i++)
|
||||||
imports.push_back(d1.read_string());
|
imports.push_back(read_name(d1));
|
||||||
|
|
||||||
unsigned code_size = d1.read_unsigned();
|
unsigned code_size = d1.read_unsigned();
|
||||||
std::vector<char> code(code_size);
|
std::vector<char> code(code_size);
|
||||||
|
@ -233,7 +234,7 @@ struct import_modules_fn {
|
||||||
r->m_module_idx = m_import_counter+1; // importate modules have idx > 0, we reserve idx 0 for new module
|
r->m_module_idx = m_import_counter+1; // importate modules have idx > 0, we reserve idx 0 for new module
|
||||||
m_import_counter++;
|
m_import_counter++;
|
||||||
std::swap(r->m_obj_code, code);
|
std::swap(r->m_obj_code, code);
|
||||||
m_module_info.insert(mk_pair(mname, r));
|
m_module_info.insert(mname, r);
|
||||||
|
|
||||||
for (auto i : imports) {
|
for (auto i : imports) {
|
||||||
auto d = load_module_file(i);
|
auto d = load_module_file(i);
|
||||||
|
@ -417,16 +418,16 @@ struct import_modules_fn {
|
||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
|
||||||
void store_direct_imports(unsigned num_modules, std::string const * modules) {
|
void store_direct_imports(unsigned num_modules, name const * modules) {
|
||||||
m_senv.update([&](environment const & env) -> environment {
|
m_senv.update([&](environment const & env) -> environment {
|
||||||
module_ext ext = get_extension(env);
|
module_ext ext = get_extension(env);
|
||||||
for (unsigned i = 0; i < num_modules; i++)
|
for (unsigned i = 0; i < num_modules; i++)
|
||||||
ext.m_direct_imports = list<std::string>(modules[i], ext.m_direct_imports);
|
ext.m_direct_imports = list<name>(modules[i], ext.m_direct_imports);
|
||||||
return update(env, ext);
|
return update(env, ext);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
environment operator()(unsigned num_modules, std::string const * modules) {
|
environment operator()(unsigned num_modules, name const * modules) {
|
||||||
store_direct_imports(num_modules, modules);
|
store_direct_imports(num_modules, modules);
|
||||||
for (unsigned i = 0; i < num_modules; i++)
|
for (unsigned i = 0; i < num_modules; i++)
|
||||||
load_module_file(modules[i]);
|
load_module_file(modules[i]);
|
||||||
|
@ -435,12 +436,12 @@ struct import_modules_fn {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
environment import_modules(environment const & env, unsigned num_modules, std::string const * modules,
|
environment import_modules(environment const & env, unsigned num_modules, name const * modules,
|
||||||
unsigned num_threads, bool keep_proofs, io_state const & ios) {
|
unsigned num_threads, bool keep_proofs, io_state const & ios) {
|
||||||
return import_modules_fn(env, num_threads, keep_proofs, ios)(num_modules, modules);
|
return import_modules_fn(env, num_threads, keep_proofs, ios)(num_modules, modules);
|
||||||
}
|
}
|
||||||
|
|
||||||
environment import_module(environment const & env, std::string const & module,
|
environment import_module(environment const & env, name const & module,
|
||||||
unsigned num_threads, bool keep_proofs, io_state const & ios) {
|
unsigned num_threads, bool keep_proofs, io_state const & ios) {
|
||||||
return import_modules(env, 1, &module, num_threads, keep_proofs, ios);
|
return import_modules(env, 1, &module, num_threads, keep_proofs, ios);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,9 +21,9 @@ namespace lean {
|
||||||
If \c keep_proofs is false, then the proof of the imported theorems is discarded after being
|
If \c keep_proofs is false, then the proof of the imported theorems is discarded after being
|
||||||
checked. The idea is to save memory.
|
checked. The idea is to save memory.
|
||||||
*/
|
*/
|
||||||
environment import_modules(environment const & env, unsigned num_modules, std::string const * modules,
|
environment import_modules(environment const & env, unsigned num_modules, name const * modules,
|
||||||
unsigned num_threads, bool keep_proofs, io_state const & ios);
|
unsigned num_threads, bool keep_proofs, io_state const & ios);
|
||||||
environment import_module(environment const & env, std::string const & module,
|
environment import_module(environment const & env, name const & module,
|
||||||
unsigned num_threads, bool keep_proofs, io_state const & ios);
|
unsigned num_threads, bool keep_proofs, io_state const & ios);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue