2014-07-08 21:28:33 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include <string>
|
|
|
|
#include "util/sstream.h"
|
|
|
|
#include "kernel/type_checker.h"
|
|
|
|
#include "library/scoped_ext.h"
|
2015-01-24 00:50:32 +00:00
|
|
|
#include "library/constants.h"
|
2014-07-08 21:28:33 +00:00
|
|
|
#include "library/kernel_serializer.h"
|
|
|
|
#include "library/tactic/tactic.h"
|
|
|
|
#include "frontends/lean/tactic_hint.h"
|
|
|
|
#include "frontends/lean/cmd_table.h"
|
|
|
|
#include "frontends/lean/parser.h"
|
2014-09-23 00:30:29 +00:00
|
|
|
#include "frontends/lean/tokens.h"
|
2014-07-08 21:28:33 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-10-07 18:34:58 +00:00
|
|
|
typedef list<expr> tactic_hints;
|
2014-07-08 21:28:33 +00:00
|
|
|
|
2014-09-23 00:30:29 +00:00
|
|
|
static name * g_class_name = nullptr;
|
|
|
|
static std::string * g_key = nullptr;
|
|
|
|
|
2014-07-08 21:28:33 +00:00
|
|
|
struct tactic_hint_config {
|
2014-10-07 18:34:58 +00:00
|
|
|
typedef tactic_hints state;
|
|
|
|
typedef expr entry;
|
2014-07-08 21:28:33 +00:00
|
|
|
|
2014-10-07 18:34:58 +00:00
|
|
|
static void add_entry(environment const &, io_state const &, state & s, entry const & e) {
|
|
|
|
s = cons(e, filter(s, [&](expr const & e1) { return e1 != e; }));
|
2014-07-08 21:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static name const & get_class_name() {
|
2014-09-23 00:30:29 +00:00
|
|
|
return *g_class_name;
|
2014-07-08 21:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string const & get_serialization_key() {
|
2014-09-23 00:30:29 +00:00
|
|
|
return *g_key;
|
2014-07-08 21:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void write_entry(serializer & s, entry const & e) {
|
2014-10-07 18:34:58 +00:00
|
|
|
s << e;
|
2014-07-08 21:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static entry read_entry(deserializer & d) {
|
|
|
|
entry e;
|
2014-10-07 18:34:58 +00:00
|
|
|
d >> e;
|
2014-07-08 21:28:33 +00:00
|
|
|
return e;
|
|
|
|
}
|
2014-10-07 18:34:58 +00:00
|
|
|
|
2014-09-30 01:26:53 +00:00
|
|
|
static optional<unsigned> get_fingerprint(entry const & e) {
|
2014-10-07 18:34:58 +00:00
|
|
|
return some(e.hash());
|
2014-09-30 01:26:53 +00:00
|
|
|
}
|
2014-07-08 21:28:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template class scoped_ext<tactic_hint_config>;
|
|
|
|
typedef scoped_ext<tactic_hint_config> tactic_hint_ext;
|
|
|
|
|
2014-09-23 00:30:29 +00:00
|
|
|
void initialize_tactic_hint() {
|
2015-02-11 18:35:04 +00:00
|
|
|
g_class_name = new name("tactic-hints");
|
2014-09-23 00:30:29 +00:00
|
|
|
g_key = new std::string("tachint");
|
|
|
|
tactic_hint_ext::initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void finalize_tactic_hint() {
|
|
|
|
tactic_hint_ext::finalize();
|
|
|
|
delete g_key;
|
|
|
|
delete g_class_name;
|
|
|
|
}
|
|
|
|
|
2014-10-07 18:34:58 +00:00
|
|
|
list<expr> const & get_tactic_hints(environment const & env) {
|
|
|
|
return tactic_hint_ext::get_state(env);
|
2014-07-08 21:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr parse_tactic_name(parser & p) {
|
2014-07-15 00:19:47 +00:00
|
|
|
auto pos = p.pos();
|
|
|
|
name pre_tac = p.check_constant_next("invalid tactic name, constant expected");
|
2014-09-17 21:34:41 +00:00
|
|
|
auto decl = p.env().get(pre_tac);
|
|
|
|
expr pre_tac_type = decl.get_type();
|
2015-01-24 00:50:32 +00:00
|
|
|
if (!is_constant(pre_tac_type) || const_name(pre_tac_type) != get_tactic_name())
|
2014-07-15 00:19:47 +00:00
|
|
|
throw parser_error(sstream() << "invalid tactic name, '" << pre_tac << "' is not a tactic", pos);
|
2014-09-17 21:34:41 +00:00
|
|
|
buffer<level> ls;
|
|
|
|
for (auto const & n : decl.get_univ_params())
|
|
|
|
ls.push_back(mk_meta_univ(n));
|
|
|
|
return mk_constant(pre_tac, to_list(ls.begin(), ls.end()));
|
2014-07-08 21:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
environment tactic_hint_cmd(parser & p) {
|
|
|
|
expr pre_tac = parse_tactic_name(p);
|
2014-10-07 18:34:58 +00:00
|
|
|
return tactic_hint_ext::add_entry(p.env(), get_dummy_ios(), pre_tac);
|
2014-07-08 21:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void register_tactic_hint_cmd(cmd_table & r) {
|
|
|
|
add_cmd(r, cmd_info("tactic_hint", "add a new tactic hint", tactic_hint_cmd));
|
|
|
|
}
|
|
|
|
}
|