feat(frontends/lean): add 'print inductive' command

This commit is contained in:
Leonardo de Moura 2015-04-29 15:22:10 -07:00
parent 018f768555
commit 1a28a3c36f
7 changed files with 86 additions and 23 deletions

View file

@ -100,10 +100,8 @@ static void print_prefix(parser & p) {
p.regular_stream() << "no declaration starting with prefix '" << prefix << "'" << endl;
}
static void print_fields(parser & p) {
auto pos = p.pos();
static void print_fields(parser & p, name const & S, pos_info const & pos) {
environment const & env = p.env();
name S = p.check_constant_next("invalid 'print fields' command, constant expected");
if (!is_structure(env, S))
throw parser_error(sstream() << "invalid 'print fields' command, '" << S << "' is not a structure", pos);
buffer<name> field_names;
@ -168,6 +166,48 @@ static void print_metaclasses(parser & p) {
p.regular_stream() << "[" << n << "]" << endl;
}
static void print_definition(parser const & p, name const & n, pos_info const & pos) {
environment const & env = p.env();
declaration d = env.get(n);
io_state_stream out = p.regular_stream();
options opts = out.get_options();
opts = opts.update_if_undef(get_pp_beta_name(), false);
io_state_stream new_out = out.update_options(opts);
if (!d.is_definition())
throw parser_error(sstream() << "invalid 'print definition', '" << n << "' is not a definition", pos);
new_out << d.get_value() << endl;
}
static void print_inductive(parser & p, name const & n, pos_info const & pos) {
environment const & env = p.env();
io_state_stream out = p.regular_stream();
if (auto idecls = inductive::is_inductive_decl(env, n)) {
level_param_names ls; unsigned nparams; list<inductive::inductive_decl> dlist;
std::tie(ls, nparams, dlist) = *idecls;
if (is_structure(env, n))
out << "structure";
else
out << "inductive";
out << " " << n;
if (is_class(env, n))
out << " [class]";
out << " : " << env.get(n).get_type() << "\n";
if (is_structure(env, n)) {
out << "fields:\n";
print_fields(p, n, pos);
} else {
out << "constructors:\n";
buffer<name> constructors;
get_intro_rule_names(env, n, constructors);
for (name const & c : constructors) {
out << c << " : " << env.get(c).get_type() << "\n";
}
}
} else {
throw parser_error(sstream() << "invalid 'print inductive', '" << n << "' is not an inductive declaration", pos);
}
}
environment print_cmd(parser & p) {
flycheck_information info(p.regular_stream());
if (info.enabled()) {
@ -194,15 +234,7 @@ environment print_cmd(parser & p) {
p.next();
auto pos = p.pos();
name c = p.check_constant_next("invalid 'print definition', constant expected");
environment const & env = p.env();
declaration d = env.get(c);
io_state_stream out = p.regular_stream();
options opts = out.get_options();
opts = opts.update_if_undef(get_pp_beta_name(), false);
io_state_stream new_out = out.update_options(opts);
if (!d.is_definition())
throw parser_error(sstream() << "invalid 'print definition', '" << c << "' is not a definition", pos);
new_out << d.get_value() << endl;
print_definition(p, c, pos);
} else if (p.curr_is_token_or_id(get_instances_tk())) {
p.next();
name c = p.check_constant_next("invalid 'print instances', constant expected");
@ -236,10 +268,17 @@ environment print_cmd(parser & p) {
print_axioms(p);
} else if (p.curr_is_token_or_id(get_fields_tk())) {
p.next();
print_fields(p);
auto pos = p.pos();
name S = p.check_constant_next("invalid 'print fields' command, constant expected");
print_fields(p, S, pos);
} else if (p.curr_is_token_or_id(get_notation_tk())) {
p.next();
print_notation(p);
} else if (p.curr_is_token_or_id(get_inductive_tk())) {
p.next();
auto pos = p.pos();
name c = p.check_constant_next("invalid 'print inductive', constant expected");
print_inductive(p, c, pos);
} else {
throw parser_error("invalid print command", p.pos());
}

View file

@ -30,6 +30,7 @@ Author: Leonardo de Moura
#include "library/constants.h"
#include "library/replace_visitor.h"
#include "frontends/lean/pp.h"
#include "frontends/lean/util.h"
#include "frontends/lean/token_table.h"
#include "frontends/lean/builtin_exprs.h"
#include "frontends/lean/parser_config.h"
@ -541,21 +542,13 @@ auto pretty_fn::pp_app(expr const & e) -> result {
format pretty_fn::pp_binder_block(buffer<name> const & names, expr const & type, binder_info const & bi) {
format r;
if (bi.is_implicit()) r += format("{");
else if (bi.is_inst_implicit()) r += format("[");
else if (bi.is_strict_implicit() && m_unicode) r += format("");
else if (bi.is_strict_implicit() && !m_unicode) r += format("{{");
else r += format("(");
r += format(open_binder_string(bi, m_unicode));
for (name const & n : names) {
r += format(n);
r += space();
}
r += compose(colon(), nest(m_indent, compose(line(), pp_child(type, 0).fmt())));
if (bi.is_implicit()) r += format("}");
else if (bi.is_inst_implicit()) r += format("]");
else if (bi.is_strict_implicit() && m_unicode) r += format("");
else if (bi.is_strict_implicit() && !m_unicode) r += format("}}");
else r += format(")");
r += format(close_binder_string(bi, m_unicode));
return group(r);
}

View file

@ -133,6 +133,7 @@ static name * g_root = nullptr;
static name * g_fields = nullptr;
static name * g_trust = nullptr;
static name * g_metaclasses = nullptr;
static name * g_inductive = nullptr;
void initialize_tokens() {
g_period = new name(".");
@ -261,9 +262,11 @@ void initialize_tokens() {
g_fields = new name("fields");
g_trust = new name("trust");
g_metaclasses = new name("metaclasses");
g_inductive = new name("inductive");
}
void finalize_tokens() {
delete g_inductive;
delete g_metaclasses;
delete g_persistent;
delete g_root;
@ -392,6 +395,7 @@ void finalize_tokens() {
delete g_period;
}
name const & get_inductive_tk() { return *g_inductive; }
name const & get_metaclasses_tk() { return *g_metaclasses; }
name const & get_period_tk() { return *g_period; }
name const & get_placeholder_tk() { return *g_placeholder; }

View file

@ -135,4 +135,5 @@ name const & get_root_tk();
name const & get_fields_tk();
name const & get_trust_tk();
name const & get_metaclasses_tk();
name const & get_inductive_tk();
}

View file

@ -444,4 +444,20 @@ name get_priority_namespace() {
environment open_priority_aliases(environment const & env) {
return overwrite_aliases(env, get_priority_namespace(), name());
}
char const * open_binder_string(binder_info const & bi, bool unicode) {
if (bi.is_implicit()) return "{";
else if (bi.is_inst_implicit()) return "[";
else if (bi.is_strict_implicit() && unicode) return "";
else if (bi.is_strict_implicit() && !unicode) return "{{";
else return "(";
}
char const * close_binder_string(binder_info const & bi, bool unicode) {
if (bi.is_implicit()) return "}";
else if (bi.is_inst_implicit()) return "]";
else if (bi.is_strict_implicit() && unicode) return "";
else if (bi.is_strict_implicit() && !unicode) return "}}";
else return ")";
}
}

View file

@ -111,4 +111,7 @@ environment open_prec_aliases(environment const & env);
/** \brief Open 'std.priority' aliases */
environment open_priority_aliases(environment const & env);
name get_priority_namespace();
char const * open_binder_string(binder_info const & bi, bool unicode);
char const * close_binder_string(binder_info const & bi, bool unicode);
}

View file

@ -0,0 +1,7 @@
import data.list algebra.group
print inductive nat
print inductive algebra.group
print inductive list