2013-11-21 01:02:41 +00:00
|
|
|
/*
|
2014-06-27 13:59:17 +00:00
|
|
|
Copyright (c) 2013-2014 Microsoft Corporation. All rights reserved.
|
2013-11-21 01:02:41 +00:00
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include <utility>
|
|
|
|
#include "util/buffer.h"
|
2014-06-27 18:11:12 +00:00
|
|
|
#include "util/sstream.h"
|
|
|
|
#include "kernel/replace_fn.h"
|
|
|
|
#include "kernel/for_each_fn.h"
|
|
|
|
#include "kernel/abstract.h"
|
2014-07-01 23:11:19 +00:00
|
|
|
#include "kernel/type_checker.h"
|
2014-10-15 00:37:20 +00:00
|
|
|
#include "kernel/metavar.h"
|
2013-11-27 03:15:49 +00:00
|
|
|
#include "library/kernel_bindings.h"
|
2013-11-21 01:02:41 +00:00
|
|
|
#include "library/tactic/goal.h"
|
|
|
|
|
|
|
|
namespace lean {
|
2014-07-10 17:32:00 +00:00
|
|
|
format goal::pp(formatter const & fmt) const {
|
2014-10-15 00:37:20 +00:00
|
|
|
return pp(fmt, substitution());
|
|
|
|
}
|
|
|
|
|
|
|
|
format goal::pp(formatter const & fmt, substitution const & s) const {
|
|
|
|
substitution tmp_subst(s);
|
2014-07-10 17:32:00 +00:00
|
|
|
options const & opts = fmt.get_options();
|
2013-11-22 00:44:31 +00:00
|
|
|
unsigned indent = get_pp_indent(opts);
|
|
|
|
bool unicode = get_pp_unicode(opts);
|
|
|
|
format turnstile = unicode ? format("\u22A2") /* ⊢ */ : format("|-");
|
2014-07-01 23:11:19 +00:00
|
|
|
expr conclusion = m_type;
|
|
|
|
buffer<expr> tmp;
|
|
|
|
get_app_args(m_meta, tmp);
|
2013-11-22 00:44:31 +00:00
|
|
|
bool first = true;
|
|
|
|
format r;
|
2014-06-27 18:11:12 +00:00
|
|
|
auto end = tmp.end();
|
|
|
|
for (auto it = tmp.begin(); it != end; ++it) {
|
|
|
|
if (first) first = false; else r += compose(comma(), line());
|
2014-07-01 23:11:19 +00:00
|
|
|
expr l = *it;
|
2014-10-15 00:37:20 +00:00
|
|
|
expr t = tmp_subst.instantiate(mlocal_type(l));
|
|
|
|
r += fmt(l) + space() + colon() + nest(indent, line() + fmt(t));
|
2013-11-22 00:44:31 +00:00
|
|
|
}
|
|
|
|
r = group(r);
|
2014-10-15 00:37:20 +00:00
|
|
|
r += line() + turnstile + space() + nest(indent, fmt(tmp_subst.instantiate(conclusion)));
|
2013-11-22 00:44:31 +00:00
|
|
|
return group(r);
|
|
|
|
}
|
|
|
|
|
2014-07-01 23:11:19 +00:00
|
|
|
expr goal::abstract(expr const & v) const {
|
|
|
|
buffer<expr> locals;
|
|
|
|
get_app_args(m_meta, locals);
|
|
|
|
return Fun(locals, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
expr goal::mk_meta(name const & n, expr const & type, bool only_contextual) const {
|
|
|
|
buffer<expr> locals;
|
2014-07-02 21:47:18 +00:00
|
|
|
expr this_mvar = get_app_args(m_meta, locals);
|
2014-07-01 23:11:19 +00:00
|
|
|
if (only_contextual) {
|
|
|
|
auto new_end = std::remove_if(locals.begin(), locals.end(),
|
|
|
|
[](expr const & l) { return !local_info(l).is_contextual(); });
|
|
|
|
locals.shrink(locals.size() - (locals.end() - new_end));
|
2014-06-27 18:11:12 +00:00
|
|
|
}
|
2014-07-02 21:47:18 +00:00
|
|
|
expr mvar = copy_tag(this_mvar, mk_metavar(n, Pi(locals, type)));
|
|
|
|
return copy_tag(m_meta, mk_app(mvar, locals));
|
2014-06-27 18:11:12 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 23:11:19 +00:00
|
|
|
static bool validate_locals(expr const & r, unsigned num_locals, expr const * locals) {
|
2014-06-27 18:11:12 +00:00
|
|
|
bool failed = false;
|
|
|
|
for_each(r, [&](expr const & e, unsigned) {
|
|
|
|
if (!has_local(e) || failed) return false;
|
|
|
|
if (is_local(e) &&
|
2014-07-01 23:11:19 +00:00
|
|
|
!std::any_of(locals, locals + num_locals,
|
|
|
|
[&](expr const & l) { return mlocal_name(l) == mlocal_name(e); })) {
|
2014-06-27 18:11:12 +00:00
|
|
|
failed = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return !failed;
|
|
|
|
}
|
|
|
|
|
2014-07-01 23:11:19 +00:00
|
|
|
bool goal::validate_locals() const {
|
|
|
|
buffer<expr> locals;
|
|
|
|
get_app_args(m_meta, locals);
|
|
|
|
if (!::lean::validate_locals(m_type, locals.size(), locals.data()))
|
2014-06-27 18:11:12 +00:00
|
|
|
return false;
|
2014-07-01 23:11:19 +00:00
|
|
|
unsigned i = locals.size();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
if (!::lean::validate_locals(mlocal_type(locals[i]), i, locals.data()))
|
2014-06-27 18:11:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-01 23:11:19 +00:00
|
|
|
bool goal::validate(environment const & env) const {
|
|
|
|
if (validate_locals()) {
|
|
|
|
type_checker tc(env);
|
2014-08-20 05:31:26 +00:00
|
|
|
return tc.is_def_eq(tc.check(m_meta).first, m_type).first;
|
2013-11-27 03:15:49 +00:00
|
|
|
} else {
|
2014-07-01 23:11:19 +00:00
|
|
|
return false;
|
2013-11-27 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 00:12:57 +00:00
|
|
|
list<expr> goal::to_context() const {
|
|
|
|
buffer<expr> locals;
|
|
|
|
get_app_rev_args(m_meta, locals);
|
|
|
|
return to_list(locals.begin(), locals.end());
|
|
|
|
}
|
|
|
|
|
2013-11-27 03:15:49 +00:00
|
|
|
DECL_UDATA(goal)
|
|
|
|
|
2014-07-01 23:11:19 +00:00
|
|
|
static int mk_goal(lua_State * L) { return push_goal(L, goal(to_expr(L, 1), to_expr(L, 2))); }
|
|
|
|
static int goal_meta(lua_State * L) { return push_expr(L, to_goal(L, 1).get_meta()); }
|
|
|
|
static int goal_type(lua_State * L) { return push_expr(L, to_goal(L, 1).get_type()); }
|
2013-11-27 03:15:49 +00:00
|
|
|
static int goal_tostring(lua_State * L) {
|
|
|
|
std::ostringstream out;
|
|
|
|
goal & g = to_goal(L, 1);
|
2014-07-10 17:32:00 +00:00
|
|
|
formatter fmt = mk_formatter(L);
|
2014-06-27 13:59:17 +00:00
|
|
|
options opts = get_global_options(L);
|
2014-07-10 17:32:00 +00:00
|
|
|
out << mk_pair(g.pp(fmt), opts);
|
2013-11-27 03:15:49 +00:00
|
|
|
lua_pushstring(L, out.str().c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
2014-07-01 23:11:19 +00:00
|
|
|
static int goal_mk_meta(lua_State * L) {
|
|
|
|
int nargs = lua_gettop(L);
|
|
|
|
return push_expr(L, to_goal(L, 1).mk_meta(to_name_ext(L, 2), to_expr(L, 3), nargs == 4 ? lua_toboolean(L, 4) : true));
|
|
|
|
}
|
|
|
|
|
2013-11-27 03:15:49 +00:00
|
|
|
static int goal_pp(lua_State * L) {
|
|
|
|
int nargs = lua_gettop(L);
|
|
|
|
goal & g = to_goal(L, 1);
|
2013-12-08 07:21:07 +00:00
|
|
|
if (nargs == 1) {
|
2014-07-10 17:32:00 +00:00
|
|
|
return push_format(L, g.pp(mk_formatter(L)));
|
2013-11-27 03:15:49 +00:00
|
|
|
} else {
|
2014-07-10 17:32:00 +00:00
|
|
|
return push_format(L, g.pp(to_formatter(L, 2)));
|
2013-11-27 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-01 23:11:19 +00:00
|
|
|
static int goal_validate_locals(lua_State * L) { return push_boolean(L, to_goal(L, 1).validate_locals()); }
|
|
|
|
static int goal_validate(lua_State * L) { return push_boolean(L, to_goal(L, 1).validate(to_environment(L, 2))); }
|
|
|
|
static int goal_abstract(lua_State * L) { return push_expr(L, to_goal(L, 1).abstract(to_expr(L, 2))); }
|
|
|
|
static int goal_name(lua_State * L) { return push_name(L, to_goal(L, 1).get_name()); }
|
2013-11-27 03:15:49 +00:00
|
|
|
|
|
|
|
static const struct luaL_Reg goal_m[] = {
|
|
|
|
{"__gc", goal_gc}, // never throws
|
|
|
|
{"__tostring", safe_function<goal_tostring>},
|
2014-07-01 23:11:19 +00:00
|
|
|
{"abstract", safe_function<goal_abstract>},
|
2014-06-27 18:11:12 +00:00
|
|
|
{"mk_meta", safe_function<goal_mk_meta>},
|
2013-11-27 03:15:49 +00:00
|
|
|
{"pp", safe_function<goal_pp>},
|
2014-06-27 18:11:12 +00:00
|
|
|
{"validate", safe_function<goal_validate>},
|
2014-07-01 23:11:19 +00:00
|
|
|
{"validate_locals", safe_function<goal_validate_locals>},
|
|
|
|
{"meta", safe_function<goal_meta>},
|
|
|
|
{"type", safe_function<goal_type>},
|
|
|
|
{"name", safe_function<goal_name>},
|
2013-11-27 03:15:49 +00:00
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
void open_goal(lua_State * L) {
|
|
|
|
luaL_newmetatable(L, goal_mt);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
setfuncs(L, goal_m, 0);
|
|
|
|
|
|
|
|
SET_GLOBAL_FUN(goal_pred, "is_goal");
|
|
|
|
SET_GLOBAL_FUN(mk_goal, "goal");
|
|
|
|
}
|
2013-11-21 01:02:41 +00:00
|
|
|
}
|