2015-05-05 01:05:00 +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 <unordered_map>
|
|
|
|
#include "kernel/expr_maps.h"
|
2015-07-28 22:51:22 +00:00
|
|
|
#include "kernel/for_each_fn.h"
|
2015-05-05 01:05:00 +00:00
|
|
|
#include "kernel/inductive/inductive.h"
|
|
|
|
#include "library/max_sharing.h"
|
|
|
|
#include "library/module.h"
|
|
|
|
#include "library/unfold_macros.h"
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
template<typename T>
|
|
|
|
using level_map = typename std::unordered_map<level, T, level_hash, level_eq>;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
using name_hmap = typename std::unordered_map<name, T, name_hash, name_eq>;
|
|
|
|
|
|
|
|
|
|
|
|
class exporter {
|
2015-05-06 21:00:57 +00:00
|
|
|
std::ostream & m_out;
|
|
|
|
environment m_env;
|
2015-07-28 22:51:22 +00:00
|
|
|
bool m_all;
|
|
|
|
name_set m_exported;
|
2015-05-06 21:00:57 +00:00
|
|
|
max_sharing_fn m_max_sharing;
|
|
|
|
name_hmap<unsigned> m_name2idx;
|
|
|
|
level_map<unsigned> m_level2idx;
|
|
|
|
expr_map<unsigned> m_expr2idx;
|
2015-05-05 01:05:00 +00:00
|
|
|
|
2015-07-28 22:51:22 +00:00
|
|
|
void mark(name const & n) {
|
|
|
|
m_exported.insert(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool already_exported(name const & n) {
|
|
|
|
return m_exported.contains(n);
|
|
|
|
}
|
|
|
|
|
2015-05-05 01:05:00 +00:00
|
|
|
unsigned export_name(name const & n) {
|
|
|
|
auto it = m_name2idx.find(n);
|
|
|
|
if (it != m_name2idx.end())
|
|
|
|
return it->second;
|
|
|
|
unsigned i;
|
|
|
|
if (n.is_anonymous()) {
|
2015-05-05 01:22:12 +00:00
|
|
|
lean_unreachable();
|
2015-05-05 01:05:00 +00:00
|
|
|
} else if (n.is_string()) {
|
|
|
|
unsigned p = export_name(n.get_prefix());
|
|
|
|
i = m_name2idx.size();
|
2015-07-27 20:47:18 +00:00
|
|
|
m_out << i << " #NS " << p << " " << n.get_string() << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
} else {
|
|
|
|
unsigned p = export_name(n.get_prefix());
|
|
|
|
i = m_name2idx.size();
|
2015-07-27 20:47:18 +00:00
|
|
|
m_out << i << " #NI " << p << " " << n.get_numeral() << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
}
|
|
|
|
m_name2idx.insert(mk_pair(n, i));
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned export_level(level const & l) {
|
|
|
|
auto it = m_level2idx.find(l);
|
|
|
|
if (it != m_level2idx.end())
|
|
|
|
return it->second;
|
|
|
|
unsigned i = 0;
|
|
|
|
unsigned l1, l2, n;
|
|
|
|
switch (l.kind()) {
|
|
|
|
case level_kind::Zero:
|
2015-05-05 01:22:12 +00:00
|
|
|
lean_unreachable();
|
2015-05-05 01:05:00 +00:00
|
|
|
break;
|
|
|
|
case level_kind::Succ:
|
|
|
|
l1 = export_level(succ_of(l));
|
|
|
|
i = m_level2idx.size();
|
2015-07-27 17:24:00 +00:00
|
|
|
m_out << i << " #US " << l1 << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
break;
|
|
|
|
case level_kind::Max:
|
|
|
|
l1 = export_level(max_lhs(l));
|
|
|
|
l2 = export_level(max_rhs(l));
|
|
|
|
i = m_level2idx.size();
|
2015-07-27 17:24:00 +00:00
|
|
|
m_out << i << " #UM " << l1 << " " << l2 << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
break;
|
|
|
|
case level_kind::IMax:
|
|
|
|
l1 = export_level(imax_lhs(l));
|
|
|
|
l2 = export_level(imax_rhs(l));
|
|
|
|
i = m_level2idx.size();
|
2015-07-27 17:24:00 +00:00
|
|
|
m_out << i << " #UIM " << l1 << " " << l2 << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
break;
|
|
|
|
case level_kind::Param:
|
|
|
|
n = export_name(param_id(l));
|
|
|
|
i = m_level2idx.size();
|
2015-07-27 17:24:00 +00:00
|
|
|
m_out << i << " #UP " << n << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
break;
|
|
|
|
case level_kind::Global:
|
|
|
|
n = export_name(global_id(l));
|
|
|
|
i = m_level2idx.size();
|
2015-07-27 17:24:00 +00:00
|
|
|
m_out << i << " #UG " << n << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
break;
|
|
|
|
case level_kind::Meta:
|
2015-08-19 00:34:26 +00:00
|
|
|
throw exception("invalid 'export', universe meta-variables cannot be exported");
|
2015-05-05 01:05:00 +00:00
|
|
|
}
|
|
|
|
m_level2idx.insert(mk_pair(l, i));
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void display_binder_info(binder_info const & bi) {
|
|
|
|
if (bi.is_implicit())
|
2015-07-27 20:47:18 +00:00
|
|
|
m_out << "#BI";
|
2015-05-05 01:05:00 +00:00
|
|
|
else if (bi.is_strict_implicit())
|
2015-07-27 20:47:18 +00:00
|
|
|
m_out << "#BS";
|
2015-05-05 01:05:00 +00:00
|
|
|
else if (bi.is_inst_implicit())
|
2015-07-27 20:47:18 +00:00
|
|
|
m_out << "#BC";
|
2015-05-05 01:05:00 +00:00
|
|
|
else
|
2015-07-27 20:47:18 +00:00
|
|
|
m_out << "#BD";
|
2015-05-05 01:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned export_binding(expr const & e, char const * k) {
|
|
|
|
unsigned n = export_name(binding_name(e));
|
|
|
|
unsigned e1 = export_expr(binding_domain(e));
|
|
|
|
unsigned e2 = export_expr(binding_body(e));
|
|
|
|
unsigned i = m_expr2idx.size();
|
|
|
|
m_out << i << " " << k << " ";
|
|
|
|
display_binder_info(binding_info(e));
|
|
|
|
m_out << " " << n << " " << e1 << " " << e2 << "\n";
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned export_const(expr const & e) {
|
|
|
|
buffer<unsigned> ls;
|
|
|
|
unsigned n = export_name(const_name(e));
|
|
|
|
for (level const & l : const_levels(e))
|
|
|
|
ls.push_back(export_level(l));
|
|
|
|
unsigned i = m_expr2idx.size();
|
2015-07-27 20:47:18 +00:00
|
|
|
m_out << i << " #EC " << n;
|
2015-05-05 01:05:00 +00:00
|
|
|
for (unsigned l : ls)
|
|
|
|
m_out << " " << l;
|
|
|
|
m_out << "\n";
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned export_expr(expr const & e) {
|
|
|
|
auto it = m_expr2idx.find(e);
|
|
|
|
if (it != m_expr2idx.end())
|
|
|
|
return it->second;
|
|
|
|
unsigned i = 0;
|
|
|
|
unsigned l, e1, e2;
|
|
|
|
switch (e.kind()) {
|
|
|
|
case expr_kind::Var:
|
|
|
|
i = m_expr2idx.size();
|
2015-07-27 20:47:18 +00:00
|
|
|
m_out << i << " #EV " << var_idx(e) << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
break;
|
|
|
|
case expr_kind::Sort:
|
|
|
|
l = export_level(sort_level(e));
|
|
|
|
i = m_expr2idx.size();
|
2015-07-27 20:47:18 +00:00
|
|
|
m_out << i << " #ES " << l << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
break;
|
|
|
|
case expr_kind::Constant:
|
|
|
|
i = export_const(e);
|
|
|
|
break;
|
|
|
|
case expr_kind::App:
|
|
|
|
e1 = export_expr(app_fn(e));
|
|
|
|
e2 = export_expr(app_arg(e));
|
|
|
|
i = m_expr2idx.size();
|
2015-07-27 20:47:18 +00:00
|
|
|
m_out << i << " #EA " << e1 << " " << e2 << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
break;
|
|
|
|
case expr_kind::Lambda:
|
2015-07-27 20:47:18 +00:00
|
|
|
i = export_binding(e, "#EL");
|
2015-05-05 01:05:00 +00:00
|
|
|
break;
|
|
|
|
case expr_kind::Pi:
|
2015-07-27 20:47:18 +00:00
|
|
|
i = export_binding(e, "#EP");
|
2015-05-05 01:05:00 +00:00
|
|
|
break;
|
|
|
|
case expr_kind::Meta:
|
2015-08-19 00:34:26 +00:00
|
|
|
throw exception("invalid 'export', meta-variables cannot be exported");
|
2015-05-05 01:05:00 +00:00
|
|
|
case expr_kind::Local:
|
2015-08-19 00:34:26 +00:00
|
|
|
throw exception("invalid 'export', local constants cannot be exported");
|
2015-05-05 01:05:00 +00:00
|
|
|
case expr_kind::Macro:
|
2015-08-19 00:34:26 +00:00
|
|
|
throw exception("invalid 'export', macros cannot be exported");
|
2015-05-05 01:05:00 +00:00
|
|
|
}
|
|
|
|
m_expr2idx.insert(mk_pair(e, i));
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned export_root_expr(expr const & e) {
|
|
|
|
return export_expr(m_max_sharing(unfold_all_macros(m_env, e)));
|
|
|
|
}
|
|
|
|
|
2015-07-28 22:51:22 +00:00
|
|
|
void export_dependencies(expr const & e) {
|
|
|
|
for_each(e, [&](expr const & e, unsigned) {
|
|
|
|
if (is_constant(e)) {
|
|
|
|
name const & n = const_name(e);
|
2015-07-30 21:59:57 +00:00
|
|
|
export_declaration(n);
|
2015-07-28 22:51:22 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-05 01:05:00 +00:00
|
|
|
void export_definition(declaration const & d) {
|
2015-07-28 22:51:22 +00:00
|
|
|
if (already_exported(d.get_name()))
|
|
|
|
return;
|
|
|
|
mark(d.get_name());
|
2015-05-05 01:05:00 +00:00
|
|
|
unsigned n = export_name(d.get_name());
|
|
|
|
buffer<unsigned> ps;
|
2015-07-28 22:51:22 +00:00
|
|
|
if (m_all) {
|
|
|
|
export_dependencies(d.get_type());
|
|
|
|
export_dependencies(d.get_value());
|
|
|
|
}
|
2015-05-05 01:05:00 +00:00
|
|
|
for (name const & p : d.get_univ_params())
|
|
|
|
ps.push_back(export_name(p));
|
|
|
|
unsigned t = export_root_expr(d.get_type());
|
|
|
|
unsigned v = export_root_expr(d.get_value());
|
2015-07-27 17:24:00 +00:00
|
|
|
m_out << "#DEF " << n;
|
2015-05-05 01:05:00 +00:00
|
|
|
for (unsigned p : ps)
|
|
|
|
m_out << " " << p;
|
|
|
|
m_out << " | " << t << " " << v << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void export_axiom(declaration const & d) {
|
2015-07-31 00:23:38 +00:00
|
|
|
if (inductive::is_intro_rule(m_env, d.get_name()) || inductive::is_elim_rule(m_env, d.get_name()))
|
|
|
|
return;
|
2015-07-28 22:51:22 +00:00
|
|
|
if (already_exported(d.get_name()))
|
|
|
|
return;
|
|
|
|
mark(d.get_name());
|
2015-05-05 01:05:00 +00:00
|
|
|
unsigned n = export_name(d.get_name());
|
|
|
|
buffer<unsigned> ps;
|
2015-07-28 22:51:22 +00:00
|
|
|
if (m_all)
|
|
|
|
export_dependencies(d.get_type());
|
2015-05-05 01:05:00 +00:00
|
|
|
for (name const & p : d.get_univ_params())
|
|
|
|
ps.push_back(export_name(p));
|
|
|
|
unsigned t = export_root_expr(d.get_type());
|
2015-07-27 17:24:00 +00:00
|
|
|
m_out << "#AX " << n;
|
2015-05-05 01:05:00 +00:00
|
|
|
for (unsigned p : ps)
|
|
|
|
m_out << " " << p;
|
|
|
|
m_out << " | " << t << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void export_inductive(name const & n) {
|
2015-07-28 22:51:22 +00:00
|
|
|
if (already_exported(n))
|
|
|
|
return;
|
|
|
|
mark(n);
|
2015-05-05 01:05:00 +00:00
|
|
|
std::tuple<level_param_names, unsigned, list<inductive::inductive_decl>> decls =
|
|
|
|
*inductive::is_inductive_decl(m_env, n);
|
2015-07-28 22:51:22 +00:00
|
|
|
if (m_all) {
|
|
|
|
for (inductive::inductive_decl const & d : std::get<2>(decls)) {
|
|
|
|
export_dependencies(inductive::inductive_decl_type(d));
|
|
|
|
for (inductive::intro_rule const & c : inductive::inductive_decl_intros(d)) {
|
|
|
|
export_dependencies(inductive::intro_rule_type(c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 01:05:00 +00:00
|
|
|
for (name const & p : std::get<0>(decls))
|
|
|
|
export_name(p);
|
|
|
|
for (inductive::inductive_decl const & d : std::get<2>(decls)) {
|
|
|
|
export_name(inductive::inductive_decl_name(d));
|
2015-05-06 21:00:57 +00:00
|
|
|
export_root_expr(inductive::inductive_decl_type(d));
|
2015-05-05 01:05:00 +00:00
|
|
|
for (inductive::intro_rule const & c : inductive::inductive_decl_intros(d)) {
|
|
|
|
export_name(inductive::intro_rule_name(c));
|
2015-05-06 21:00:57 +00:00
|
|
|
export_root_expr(inductive::intro_rule_type(c));
|
2015-05-05 01:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-27 17:24:00 +00:00
|
|
|
m_out << "#BIND " << std::get<1>(decls) << " " << length(std::get<2>(decls));
|
2015-05-05 01:05:00 +00:00
|
|
|
for (name const & p : std::get<0>(decls))
|
|
|
|
m_out << " " << export_name(p);
|
|
|
|
m_out << "\n";
|
|
|
|
for (inductive::inductive_decl const & d : std::get<2>(decls)) {
|
2015-07-27 17:24:00 +00:00
|
|
|
m_out << "#IND " << export_name(inductive::inductive_decl_name(d)) << " "
|
2015-05-06 21:00:57 +00:00
|
|
|
<< export_root_expr(inductive::inductive_decl_type(d)) << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
for (inductive::intro_rule const & c : inductive::inductive_decl_intros(d)) {
|
2015-07-27 17:24:00 +00:00
|
|
|
m_out << "#INTRO " << export_name(inductive::intro_rule_name(c)) << " "
|
2015-05-06 21:00:57 +00:00
|
|
|
<< export_root_expr(inductive::intro_rule_type(c)) << "\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-27 17:24:00 +00:00
|
|
|
m_out << "#EIND\n";
|
2015-05-05 01:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void export_declaration(name const & n) {
|
|
|
|
if (inductive::is_inductive_decl(m_env, n)) {
|
|
|
|
export_inductive(n);
|
|
|
|
} else {
|
|
|
|
declaration const & d = m_env.get(n);
|
|
|
|
if (d.is_definition())
|
|
|
|
export_definition(d);
|
|
|
|
else
|
|
|
|
export_axiom(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void export_declarations() {
|
2015-07-30 21:59:57 +00:00
|
|
|
if (m_all) {
|
|
|
|
m_env.for_each_declaration([&](declaration const & d) {
|
|
|
|
export_declaration(d.get_name());
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
buffer<name> ns;
|
|
|
|
to_buffer(get_curr_module_decl_names(m_env), ns);
|
|
|
|
std::reverse(ns.begin(), ns.end());
|
|
|
|
for (name const & n : ns) {
|
|
|
|
export_declaration(n);
|
|
|
|
}
|
2015-07-28 22:51:22 +00:00
|
|
|
}
|
2015-05-05 01:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void export_direct_imports() {
|
2015-07-30 21:59:57 +00:00
|
|
|
if (!m_all) {
|
|
|
|
buffer<module_name> imports;
|
|
|
|
to_buffer(get_curr_module_imports(m_env), imports);
|
|
|
|
std::reverse(imports.begin(), imports.end());
|
|
|
|
for (module_name const & m : imports) {
|
|
|
|
unsigned n = export_name(m.get_name());
|
|
|
|
if (m.is_relative()) {
|
|
|
|
m_out << "#RI " << *m.get_k() << " " << n << "\n";
|
|
|
|
} else {
|
|
|
|
m_out << "#DI " << n << "\n";
|
|
|
|
}
|
2015-05-05 01:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void export_global_universes() {
|
2015-07-30 21:59:57 +00:00
|
|
|
if (m_all) {
|
|
|
|
m_env.for_each_universe([&](name const & u) {
|
|
|
|
unsigned n = export_name(u);
|
|
|
|
m_out << "#UNI " << n << "\n";
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
buffer<name> ns;
|
|
|
|
to_buffer(get_curr_module_univ_names(m_env), ns);
|
|
|
|
std::reverse(ns.begin(), ns.end());
|
|
|
|
for (name const & u : ns) {
|
|
|
|
unsigned n = export_name(u);
|
|
|
|
m_out << "#UNI " << n << "\n";
|
|
|
|
}
|
2015-05-05 01:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2015-07-28 22:51:22 +00:00
|
|
|
exporter(std::ostream & out, environment const & env, bool all):m_out(out), m_env(env), m_all(all) {}
|
2015-05-05 01:05:00 +00:00
|
|
|
|
|
|
|
void operator()() {
|
2015-05-05 01:22:12 +00:00
|
|
|
m_name2idx.insert(mk_pair(name(), 0));
|
|
|
|
m_level2idx.insert(mk_pair(level(), 0));
|
2015-07-30 21:59:57 +00:00
|
|
|
export_direct_imports();
|
2015-05-05 01:05:00 +00:00
|
|
|
export_global_universes();
|
|
|
|
export_declarations();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void export_module_as_lowtext(std::ostream & out, environment const & env) {
|
2015-07-28 22:51:22 +00:00
|
|
|
exporter(out, env, false)();
|
|
|
|
}
|
|
|
|
|
|
|
|
void export_all_as_lowtext(std::ostream & out, environment const & env) {
|
|
|
|
exporter(out, env, true)();
|
2015-05-05 01:05:00 +00:00
|
|
|
}
|
|
|
|
}
|