2013-11-21 20:34:37 +00:00
|
|
|
/*
|
2014-06-27 21:49:48 +00:00
|
|
|
Copyright (c) 2013-2014 Microsoft Corporation. All rights reserved.
|
2013-11-21 20:34:37 +00:00
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2013-12-23 00:37:38 +00:00
|
|
|
#include <utility>
|
2013-11-21 20:34:37 +00:00
|
|
|
#include "util/sstream.h"
|
2014-06-27 21:49:48 +00:00
|
|
|
#include "util/interrupt.h"
|
2013-12-23 00:37:38 +00:00
|
|
|
#include "kernel/type_checker.h"
|
2014-06-27 21:49:48 +00:00
|
|
|
#include "kernel/instantiate.h"
|
|
|
|
#include "kernel/abstract.h"
|
2014-01-02 21:14:21 +00:00
|
|
|
#include "library/io_state_stream.h"
|
2013-11-27 03:15:49 +00:00
|
|
|
#include "library/kernel_bindings.h"
|
2013-11-21 20:34:37 +00:00
|
|
|
#include "library/tactic/proof_state.h"
|
|
|
|
|
2013-12-06 05:18:22 +00:00
|
|
|
#ifndef LEAN_PROOF_STATE_GOAL_NAMES
|
|
|
|
#define LEAN_PROOF_STATE_GOAL_NAMES false
|
|
|
|
#endif
|
|
|
|
|
2014-06-27 21:49:48 +00:00
|
|
|
#ifndef LEAN_PROOF_STATE_MINIMIZE_CONTEXTUAL
|
|
|
|
#define LEAN_PROOF_STATE_MINIMIZE_CONTEXTUAL true
|
|
|
|
#endif
|
|
|
|
|
2013-11-21 20:34:37 +00:00
|
|
|
namespace lean {
|
2014-06-27 21:49:48 +00:00
|
|
|
static name g_proof_state_goal_names {"tactic", "goal_names"};
|
2013-12-06 05:18:22 +00:00
|
|
|
RegisterBoolOption(g_proof_state_goal_names, LEAN_PROOF_STATE_GOAL_NAMES, "(tactic) display goal names when pretty printing proof state");
|
2014-06-27 21:49:48 +00:00
|
|
|
bool get_proof_state_goal_names(options const & opts) { return opts.get_bool(g_proof_state_goal_names, LEAN_PROOF_STATE_GOAL_NAMES); }
|
2013-12-05 12:41:08 +00:00
|
|
|
|
2014-06-27 21:49:48 +00:00
|
|
|
format proof_state::pp(environment const & env, formatter const & fmt, options const & opts) const {
|
2013-12-06 05:18:22 +00:00
|
|
|
bool show_goal_names = get_proof_state_goal_names(opts);
|
|
|
|
unsigned indent = get_pp_indent(opts);
|
2013-11-22 00:44:31 +00:00
|
|
|
format r;
|
|
|
|
bool first = true;
|
2014-07-01 23:11:19 +00:00
|
|
|
for (auto const & g : get_goals()) {
|
2014-06-27 21:49:48 +00:00
|
|
|
if (first) first = false; else r += line();
|
2013-12-06 05:18:22 +00:00
|
|
|
if (show_goal_names) {
|
2014-07-01 23:11:19 +00:00
|
|
|
r += group(format{format(g.get_name()), colon(), nest(indent, compose(line(), g.pp(env, fmt, opts)))});
|
2013-12-06 05:18:22 +00:00
|
|
|
} else {
|
2014-07-01 23:11:19 +00:00
|
|
|
r += g.pp(env, fmt, opts);
|
2013-12-06 05:18:22 +00:00
|
|
|
}
|
2013-11-22 00:44:31 +00:00
|
|
|
}
|
2014-06-27 21:49:48 +00:00
|
|
|
if (first) r = format("no goals");
|
2013-11-22 00:44:31 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-07-01 23:11:19 +00:00
|
|
|
goals map_goals(proof_state const & s, std::function<optional<goal>(goal const & g)> f) {
|
|
|
|
return map_filter(s.get_goals(), [=](goal const & in, goal & out) -> bool {
|
2014-06-27 21:49:48 +00:00
|
|
|
check_interrupted();
|
2014-07-01 23:11:19 +00:00
|
|
|
optional<goal> new_goal = f(in);
|
2014-06-27 21:49:48 +00:00
|
|
|
if (new_goal) {
|
2014-07-01 23:11:19 +00:00
|
|
|
out = *new_goal;
|
2014-06-27 21:49:48 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2013-11-25 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 23:11:19 +00:00
|
|
|
proof_state to_proof_state(expr const & mvar, name_generator ngen) {
|
2014-06-27 21:49:48 +00:00
|
|
|
if (!is_metavar(mvar))
|
|
|
|
throw exception("invalid 'to_proof_state', argument is not a metavariable");
|
|
|
|
expr t = mlocal_type(mvar);
|
2014-07-01 23:11:19 +00:00
|
|
|
buffer<expr> ls;
|
2013-12-23 00:37:38 +00:00
|
|
|
while (is_pi(t)) {
|
2014-06-30 16:14:55 +00:00
|
|
|
expr l = mk_local(ngen.next(), binding_name(t), binding_domain(t), binding_info(t));
|
2014-07-01 23:11:19 +00:00
|
|
|
ls.push_back(l);
|
2014-06-27 21:49:48 +00:00
|
|
|
t = instantiate(binding_body(t), l);
|
2013-12-23 00:37:38 +00:00
|
|
|
}
|
2014-07-01 23:11:19 +00:00
|
|
|
expr meta = mk_app(mvar, ls);
|
|
|
|
goals gs(goal(meta, t));
|
|
|
|
return proof_state(gs, substitution(), ngen, meta);
|
2014-06-27 21:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static name g_tmp_prefix = name::mk_internal_unique_name();
|
2014-07-01 23:11:19 +00:00
|
|
|
proof_state to_proof_state(expr const & mvar) {
|
|
|
|
return to_proof_state(mvar, name_generator(g_tmp_prefix));
|
2013-11-21 20:34:37 +00:00
|
|
|
}
|
2013-11-27 03:15:49 +00:00
|
|
|
|
2014-06-28 01:35:59 +00:00
|
|
|
io_state_stream const & operator<<(io_state_stream const & out, proof_state const & s) {
|
2013-12-10 21:09:35 +00:00
|
|
|
options const & opts = out.get_options();
|
2014-06-27 21:49:48 +00:00
|
|
|
out.get_stream() << mk_pair(s.pp(out.get_environment(), out.get_formatter(), opts), opts);
|
2013-11-29 05:08:12 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2013-11-27 03:15:49 +00:00
|
|
|
DECL_UDATA(goals)
|
|
|
|
|
|
|
|
static int mk_goals(lua_State * L) {
|
2014-06-27 21:49:48 +00:00
|
|
|
int i = lua_gettop(L);
|
|
|
|
goals r;
|
|
|
|
if (i > 0 && is_goals(L, i)) {
|
|
|
|
r = to_goals(L, i);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
while (i > 0) {
|
2014-07-01 23:11:19 +00:00
|
|
|
r = goals(to_goal(L, i), r);
|
2014-06-27 21:49:48 +00:00
|
|
|
i--;
|
2013-11-27 03:15:49 +00:00
|
|
|
}
|
2014-06-27 21:49:48 +00:00
|
|
|
return push_goals(L, r);
|
2013-11-27 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int goals_is_nil(lua_State * L) {
|
|
|
|
lua_pushboolean(L, !to_goals(L, 1));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int goals_head(lua_State * L) {
|
|
|
|
goals const & hs = to_goals(L, 1);
|
|
|
|
if (!hs)
|
|
|
|
throw exception("head method expects a non-empty goal list");
|
2014-07-01 23:11:19 +00:00
|
|
|
return push_goal(L, head(hs));
|
2013-11-27 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int goals_tail(lua_State * L) {
|
|
|
|
goals const & hs = to_goals(L, 1);
|
|
|
|
if (!hs)
|
|
|
|
throw exception("tail method expects a non-empty goal list");
|
|
|
|
push_goals(L, tail(hs));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int goals_next(lua_State * L) {
|
|
|
|
goals & hs = to_goals(L, lua_upvalueindex(1));
|
|
|
|
if (hs) {
|
|
|
|
push_goals(L, tail(hs));
|
|
|
|
lua_replace(L, lua_upvalueindex(1));
|
2014-07-01 23:11:19 +00:00
|
|
|
return push_goal(L, head(hs));
|
2013-11-27 03:15:49 +00:00
|
|
|
} else {
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int goals_items(lua_State * L) {
|
|
|
|
goals & hs = to_goals(L, 1);
|
|
|
|
push_goals(L, hs); // upvalue(1): goals
|
|
|
|
lua_pushcclosure(L, &safe_function<goals_next>, 1); // create closure with 1 upvalue
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int goals_len(lua_State * L) {
|
2014-07-01 23:11:19 +00:00
|
|
|
return push_integer(L, length(to_goals(L, 1)));
|
2013-11-27 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct luaL_Reg goals_m[] = {
|
|
|
|
{"__gc", goals_gc}, // never throws
|
|
|
|
{"__len", safe_function<goals_len>},
|
|
|
|
{"size", safe_function<goals_len>},
|
|
|
|
{"pairs", safe_function<goals_items>},
|
|
|
|
{"is_nil", safe_function<goals_is_nil>},
|
|
|
|
{"empty", safe_function<goals_is_nil>},
|
|
|
|
{"head", safe_function<goals_head>},
|
|
|
|
{"tail", safe_function<goals_tail>},
|
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
DECL_UDATA(proof_state)
|
|
|
|
|
|
|
|
static int mk_proof_state(lua_State * L) {
|
|
|
|
int nargs = lua_gettop(L);
|
2014-06-27 21:49:48 +00:00
|
|
|
if (nargs == 2) {
|
|
|
|
return push_proof_state(L, proof_state(to_proof_state(L, 1), to_goals(L, 2)));
|
2013-11-27 03:15:49 +00:00
|
|
|
} else if (nargs == 3) {
|
2014-07-01 23:11:19 +00:00
|
|
|
return push_proof_state(L, proof_state(to_proof_state(L, 1), to_goals(L, 2), to_substitution(L, 3)));
|
2014-06-27 21:49:48 +00:00
|
|
|
} else if (nargs == 4) {
|
2014-07-01 23:11:19 +00:00
|
|
|
return push_proof_state(L, proof_state(to_proof_state(L, 1), to_goals(L, 2), to_substitution(L, 3), to_name_generator(L, 4)));
|
2013-11-27 03:15:49 +00:00
|
|
|
} else {
|
2014-06-27 21:49:48 +00:00
|
|
|
throw exception("proof_state invalid number of arguments");
|
2013-11-27 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int to_proof_state(lua_State * L) {
|
2014-06-27 21:49:48 +00:00
|
|
|
int nargs = lua_gettop(L);
|
|
|
|
if (nargs == 1)
|
|
|
|
return push_proof_state(L, to_proof_state(to_expr(L, 1)));
|
|
|
|
else
|
2014-07-01 23:11:19 +00:00
|
|
|
return push_proof_state(L, to_proof_state(to_expr(L, 1), to_name_generator(L, 2)));
|
2013-11-27 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int proof_state_tostring(lua_State * L) {
|
|
|
|
std::ostringstream out;
|
|
|
|
proof_state & s = to_proof_state(L, 1);
|
2014-06-27 21:49:48 +00:00
|
|
|
environment env = get_global_environment(L);
|
|
|
|
formatter fmt = get_global_formatter(L);
|
|
|
|
options opts = get_global_options(L);
|
|
|
|
out << mk_pair(s.pp(env, fmt, opts), opts);
|
2013-11-27 03:15:49 +00:00
|
|
|
lua_pushstring(L, out.str().c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-27 21:49:48 +00:00
|
|
|
static int proof_state_get_goals(lua_State * L) { return push_goals(L, to_proof_state(L, 1).get_goals()); }
|
2014-07-01 23:11:19 +00:00
|
|
|
static int proof_state_get_ngen(lua_State * L) { return push_name_generator(L, to_proof_state(L, 1).get_ngen()); }
|
|
|
|
static int proof_state_get_subst(lua_State * L) { return push_substitution(L, to_proof_state(L, 1).get_subst()); }
|
|
|
|
static int proof_state_get_initial(lua_State * L) { return push_list_expr(L, to_proof_state(L, 1).get_initial()); }
|
2014-06-30 15:48:01 +00:00
|
|
|
static int proof_state_is_final_state(lua_State * L) { return push_boolean(L, to_proof_state(L, 1).is_final_state()); }
|
2013-11-27 03:15:49 +00:00
|
|
|
static int proof_state_pp(lua_State * L) {
|
|
|
|
int nargs = lua_gettop(L);
|
|
|
|
proof_state & s = to_proof_state(L, 1);
|
2014-06-27 21:49:48 +00:00
|
|
|
if (nargs == 1)
|
|
|
|
return push_format(L, s.pp(get_global_environment(L), get_global_formatter(L), get_global_options(L)));
|
|
|
|
else if (nargs == 2)
|
|
|
|
return push_format(L, s.pp(to_environment(L, 2), get_global_formatter(L), get_global_options(L)));
|
|
|
|
else if (nargs == 3)
|
|
|
|
return push_format(L, s.pp(to_environment(L, 2), to_formatter(L, 3), get_global_options(L)));
|
|
|
|
else
|
|
|
|
return push_format(L, s.pp(to_environment(L, 2), to_formatter(L, 3), to_options(L, 4)));
|
2013-11-27 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct luaL_Reg proof_state_m[] = {
|
|
|
|
{"__gc", proof_state_gc}, // never throws
|
|
|
|
{"__tostring", safe_function<proof_state_tostring>},
|
|
|
|
{"pp", safe_function<proof_state_pp>},
|
|
|
|
{"goals", safe_function<proof_state_get_goals>},
|
2014-07-01 23:11:19 +00:00
|
|
|
{"subst", safe_function<proof_state_get_subst>},
|
|
|
|
{"initial", safe_function<proof_state_get_initial>},
|
|
|
|
{"ngen", safe_function<proof_state_get_ngen>},
|
2014-06-30 15:48:01 +00:00
|
|
|
{"is_final_state", safe_function<proof_state_is_final_state>},
|
2013-11-27 03:15:49 +00:00
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
void open_proof_state(lua_State * L) {
|
|
|
|
luaL_newmetatable(L, goals_mt);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
setfuncs(L, goals_m, 0);
|
|
|
|
|
|
|
|
SET_GLOBAL_FUN(goals_pred, "is_goals");
|
|
|
|
SET_GLOBAL_FUN(mk_goals, "goals");
|
|
|
|
|
|
|
|
luaL_newmetatable(L, proof_state_mt);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
setfuncs(L, proof_state_m, 0);
|
|
|
|
|
|
|
|
SET_GLOBAL_FUN(proof_state_pred, "is_proof_state");
|
|
|
|
SET_GLOBAL_FUN(mk_proof_state, "proof_state");
|
|
|
|
SET_GLOBAL_FUN(to_proof_state, "to_proof_state");
|
|
|
|
}
|
2013-11-21 20:34:37 +00:00
|
|
|
}
|