feat(library/kernel_bindings): expose environment::for_each method in the Lua API

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2014-05-20 10:16:19 -07:00
parent 34a9c8304a
commit 00b1a84051
2 changed files with 24 additions and 0 deletions

View file

@ -1122,6 +1122,16 @@ static int environment_whnf(lua_State * L) { return push_expr(L, type_checker(to
static int environment_normalize(lua_State * L) { return push_expr(L, normalize(to_environment(L, 1), to_expr(L, 2))); }
static int environment_infer_type(lua_State * L) { return push_expr(L, type_checker(to_environment(L, 1)).infer(to_expr(L, 2))); }
static int environment_type_check(lua_State * L) { return push_expr(L, type_checker(to_environment(L, 1)).check(to_expr(L, 2))); }
static int environment_for_each(lua_State * L) {
environment const & env = to_environment(L, 1);
luaL_checktype(L, 2, LUA_TFUNCTION); // user-fun
env.for_each([&](definition const & d) {
lua_pushvalue(L, 2); // push user-fun
push_definition(L, d);
pcall(L, 1, 0, 0);
});
return 0;
}
static const struct luaL_Reg environment_m[] = {
{"__gc", environment_gc}, // never throws
@ -1143,6 +1153,7 @@ static const struct luaL_Reg environment_m[] = {
{"normalize", safe_function<environment_normalize>},
{"infer_type", safe_function<environment_infer_type>},
{"type_check", safe_function<environment_type_check>},
{"for_each", safe_function<environment_for_each>},
{0, 0}
};

13
tests/lua/env8.lua Normal file
View file

@ -0,0 +1,13 @@
local env = environment()
local nat = Const("nat")
env = add_inductive(env,
"nat", Type,
"zero", nat,
"succ", mk_arrow(nat, nat))
-- Display all declarations in the environment
env:for_each(function(d)
print(tostring(d:name()) .. " : " .. tostring(d:type()))
end
)