2013-08-04 23:07:37 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2014-05-28 05:25:54 +00:00
|
|
|
#include <vector>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/test.h"
|
|
|
|
#include "util/exception.h"
|
|
|
|
#include "util/trace.h"
|
|
|
|
#include "kernel/environment.h"
|
|
|
|
#include "kernel/type_checker.h"
|
|
|
|
#include "kernel/abstract.h"
|
2014-04-28 21:04:05 +00:00
|
|
|
#include "kernel/kernel_exception.h"
|
2013-08-04 23:07:37 +00:00
|
|
|
using namespace lean;
|
|
|
|
|
2014-05-20 17:41:36 +00:00
|
|
|
static environment add_decl(environment const & env, declaration const & d) {
|
2014-05-09 01:08:32 +00:00
|
|
|
auto cd = check(env, d, name_generator("test"));
|
2014-04-28 21:04:05 +00:00
|
|
|
return env.add(cd);
|
2013-08-04 23:07:37 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 21:04:05 +00:00
|
|
|
static void tst1() {
|
|
|
|
environment env1;
|
2014-05-20 17:41:36 +00:00
|
|
|
auto env2 = add_decl(env1, mk_definition("Bool", level_param_names(), mk_Type(), mk_Bool()));
|
2014-04-28 21:04:05 +00:00
|
|
|
lean_assert(!env1.find("Bool"));
|
|
|
|
lean_assert(env2.find("Bool"));
|
|
|
|
lean_assert(env2.find("Bool")->get_value() == mk_Bool());
|
2013-08-05 03:52:14 +00:00
|
|
|
try {
|
2014-05-20 17:41:36 +00:00
|
|
|
auto env3 = add_decl(env2, mk_definition("Bool", level_param_names(), mk_Type(), mk_Bool()));
|
2013-08-05 03:52:14 +00:00
|
|
|
lean_unreachable();
|
2014-04-28 21:04:05 +00:00
|
|
|
} catch (kernel_exception & ex) {
|
|
|
|
std::cout << "expected error: " << ex.pp(mk_simple_formatter(), options()) << "\n";
|
2013-08-05 03:52:14 +00:00
|
|
|
}
|
|
|
|
try {
|
2014-05-20 17:41:36 +00:00
|
|
|
auto env4 = add_decl(env2, mk_definition("BuggyBool", level_param_names(), mk_Bool(), mk_Bool()));
|
2013-08-05 03:52:14 +00:00
|
|
|
lean_unreachable();
|
2014-04-28 21:04:05 +00:00
|
|
|
} catch (kernel_exception & ex) {
|
|
|
|
std::cout << "expected error: " << ex.pp(mk_simple_formatter(), options()) << "\n";
|
2013-08-05 03:52:14 +00:00
|
|
|
}
|
|
|
|
try {
|
2014-05-20 17:41:36 +00:00
|
|
|
auto env5 = add_decl(env2, mk_definition("Type1", level_param_names(), mk_metavar("T", mk_sort(mk_meta_univ("l"))), mk_Type()));
|
2013-08-05 03:52:14 +00:00
|
|
|
lean_unreachable();
|
2014-04-28 21:04:05 +00:00
|
|
|
} catch (kernel_exception & ex) {
|
|
|
|
std::cout << "expected error: " << ex.pp(mk_simple_formatter(), options()) << "\n";
|
2013-08-05 03:52:14 +00:00
|
|
|
}
|
2013-08-05 17:33:05 +00:00
|
|
|
try {
|
2014-05-20 17:41:36 +00:00
|
|
|
auto env6 = add_decl(env2, mk_definition("Type1", level_param_names(), mk_Type(), mk_metavar("T", mk_sort(mk_meta_univ("l")))));
|
2013-08-05 17:33:05 +00:00
|
|
|
lean_unreachable();
|
2014-04-28 21:04:05 +00:00
|
|
|
} catch (kernel_exception & ex) {
|
|
|
|
std::cout << "expected error: " << ex.pp(mk_simple_formatter(), options()) << "\n";
|
2013-08-05 20:16:20 +00:00
|
|
|
}
|
|
|
|
try {
|
2014-05-20 17:41:36 +00:00
|
|
|
auto env7 = add_decl(env2, mk_definition("foo", level_param_names(), mk_Type() >> mk_Type(), mk_Bool()));
|
2013-08-05 20:16:20 +00:00
|
|
|
lean_unreachable();
|
2014-04-28 21:04:05 +00:00
|
|
|
} catch (kernel_exception & ex) {
|
|
|
|
std::cout << "expected error: " << ex.pp(mk_simple_formatter(), options()) << "\n";
|
2013-08-05 20:16:20 +00:00
|
|
|
}
|
2014-04-28 21:04:05 +00:00
|
|
|
expr A = Const("A");
|
|
|
|
expr x = Const("x");
|
2014-05-20 17:41:36 +00:00
|
|
|
auto env3 = add_decl(env2, mk_definition("id", level_param_names(),
|
|
|
|
Pi(A, mk_Type(), A >> A),
|
|
|
|
Fun({{A, mk_Type()}, {x, A}}, x)));
|
2014-06-16 21:27:26 +00:00
|
|
|
expr c = mk_local("c", Bool);
|
2014-04-28 21:04:05 +00:00
|
|
|
expr id = Const("id");
|
|
|
|
type_checker checker(env3, name_generator("tmp"));
|
|
|
|
lean_assert(checker.check(id(Bool)) == Bool >> Bool);
|
|
|
|
lean_assert(checker.whnf(id(Bool, c)) == c);
|
|
|
|
lean_assert(checker.whnf(id(Bool, id(Bool, id(Bool, c)))) == c);
|
|
|
|
|
|
|
|
type_checker checker2(env2, name_generator("tmp"));
|
|
|
|
lean_assert(checker2.whnf(id(Bool, id(Bool, id(Bool, c)))) == id(Bool, id(Bool, id(Bool, c))));
|
2013-08-16 19:51:12 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 21:04:05 +00:00
|
|
|
static void tst2() {
|
2013-10-15 21:50:08 +00:00
|
|
|
environment env;
|
2014-04-28 21:04:05 +00:00
|
|
|
name base("base");
|
2014-05-20 17:41:36 +00:00
|
|
|
env = add_decl(env, mk_var_decl(name(base, 0u), level_param_names(), Bool >> (Bool >> Bool)));
|
2014-04-28 21:04:05 +00:00
|
|
|
expr x = Const("x");
|
|
|
|
expr y = Const("y");
|
|
|
|
for (unsigned i = 1; i <= 100; i++) {
|
|
|
|
expr prev = Const(name(base, i-1));
|
2014-05-20 17:41:36 +00:00
|
|
|
env = add_decl(env, mk_definition(env, name(base, i), level_param_names(), Bool >> (Bool >> Bool),
|
|
|
|
Fun({{x, Bool}, {y, Bool}}, prev(prev(x, y), prev(y, x)))));
|
2014-04-28 21:04:05 +00:00
|
|
|
}
|
|
|
|
expr A = Const("A");
|
2014-05-20 17:41:36 +00:00
|
|
|
env = add_decl(env, mk_definition("id", level_param_names(),
|
|
|
|
Pi(A, mk_Type(), A >> A),
|
|
|
|
Fun({{A, mk_Type()}, {x, A}}, x)));
|
2014-04-28 21:04:05 +00:00
|
|
|
type_checker checker(env, name_generator("tmp"));
|
|
|
|
expr f96 = Const(name(base, 96));
|
|
|
|
expr f97 = Const(name(base, 97));
|
|
|
|
expr f98 = Const(name(base, 98));
|
|
|
|
expr f3 = Const(name(base, 3));
|
2014-06-16 21:27:26 +00:00
|
|
|
expr c1 = mk_local("c1", Bool);
|
|
|
|
expr c2 = mk_local("c2", Bool);
|
2014-04-28 21:04:05 +00:00
|
|
|
expr id = Const("id");
|
|
|
|
std::cout << checker.whnf(f3(c1, c2)) << "\n";
|
|
|
|
lean_assert_eq(env.find(name(base, 98))->get_weight(), 98);
|
2014-05-10 03:29:35 +00:00
|
|
|
lean_assert(checker.is_def_eq(f98(c1, c2), f97(f97(c1, c2), f97(c2, c1))));
|
|
|
|
lean_assert(checker.is_def_eq(f98(c1, id(Bool, id(Bool, c2))), f97(f97(c1, id(Bool, c2)), f97(c2, c1))));
|
2014-04-28 21:04:05 +00:00
|
|
|
name_set s;
|
|
|
|
s.insert(name(base, 96));
|
2014-05-01 01:42:01 +00:00
|
|
|
type_checker checker2(env, name_generator("tmp"), mk_default_converter(env, optional<module_idx>(), true, s));
|
2014-04-28 21:04:05 +00:00
|
|
|
lean_assert_eq(checker2.whnf(f98(c1, c2)),
|
|
|
|
f96(f96(f97(c1, c2), f97(c2, c1)), f96(f97(c2, c1), f97(c1, c2))));
|
2013-10-15 21:50:08 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 21:04:05 +00:00
|
|
|
class normalizer_extension_tst : public normalizer_extension {
|
|
|
|
public:
|
|
|
|
virtual optional<expr> operator()(expr const & e, extension_context & ctx) const {
|
|
|
|
if (!is_app(e))
|
|
|
|
return none_expr();
|
|
|
|
expr const & f = app_fn(e);
|
|
|
|
expr const & a = app_arg(e);
|
|
|
|
if (!is_constant(f) || const_name(f) != name("proj1"))
|
|
|
|
return none_expr();
|
|
|
|
expr a_n = ctx.whnf(a);
|
|
|
|
if (!is_app(a_n) || !is_app(app_fn(a_n)) || !is_constant(app_fn(app_fn(a_n))))
|
|
|
|
return none_expr();
|
|
|
|
expr const & mk = app_fn(app_fn(a_n));
|
|
|
|
if (const_name(mk) != name("mk"))
|
|
|
|
return none_expr();
|
|
|
|
// In a real implementation, we must check if proj1 and mk were defined in the environment.
|
|
|
|
return some_expr(app_arg(app_fn(a_n)));
|
2013-11-07 03:21:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-28 21:04:05 +00:00
|
|
|
static void tst3() {
|
2014-05-16 22:04:34 +00:00
|
|
|
environment env(0, true, true, true, list<name>(), std::unique_ptr<normalizer_extension>(new normalizer_extension_tst()));
|
2014-04-28 21:04:05 +00:00
|
|
|
expr A = Const("A");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr id = Const("id");
|
2014-05-20 17:41:36 +00:00
|
|
|
env = add_decl(env, mk_definition("id", level_param_names(),
|
|
|
|
Pi(A, mk_Type(), A >> A),
|
|
|
|
Fun({{A, mk_Type()}, {x, A}}, x)));
|
2014-04-28 21:04:05 +00:00
|
|
|
expr mk = Const("mk");
|
|
|
|
expr proj1 = Const("proj1");
|
|
|
|
expr a = Const("a");
|
|
|
|
expr b = Const("b");
|
|
|
|
type_checker checker(env, name_generator("tmp"));
|
|
|
|
lean_assert_eq(checker.whnf(proj1(proj1(mk(id(A, mk(a, b)), b)))), a);
|
2013-11-17 02:35:17 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 19:42:29 +00:00
|
|
|
class dummy_ext : public environment_extension {};
|
|
|
|
|
|
|
|
static void tst4() {
|
|
|
|
environment env;
|
|
|
|
try {
|
|
|
|
env.get_extension(10000);
|
|
|
|
lean_unreachable();
|
|
|
|
} catch (kernel_exception & ex) {
|
|
|
|
std::cout << "expected error: " << ex.what() << "\n";
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
env.update(10000, std::make_shared<dummy_ext>());
|
|
|
|
lean_unreachable();
|
|
|
|
} catch (kernel_exception & ex) {
|
|
|
|
std::cout << "expected error: " << ex.what() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 05:25:54 +00:00
|
|
|
namespace lean {
|
|
|
|
class environment_id_tester {
|
|
|
|
public:
|
|
|
|
static void tst1() {
|
|
|
|
environment_id id1;
|
|
|
|
environment_id id2 = environment_id::mk_descendant(id1);
|
|
|
|
environment_id id3 = environment_id::mk_descendant(id2);
|
|
|
|
environment_id id4 = environment_id::mk_descendant(id1);
|
|
|
|
environment_id id5 = environment_id::mk_descendant(id3);
|
|
|
|
environment_id id6 = environment_id::mk_descendant(id4);
|
|
|
|
environment_id id7 = environment_id::mk_descendant(id3);
|
|
|
|
environment_id id8 = environment_id::mk_descendant(id7);
|
|
|
|
lean_assert(id1.is_descendant(id1));
|
|
|
|
lean_assert(id2.is_descendant(id1));
|
|
|
|
lean_assert(!id1.is_descendant(id2));
|
|
|
|
lean_assert(id3.is_descendant(id1));
|
|
|
|
lean_assert(id3.is_descendant(id2));
|
|
|
|
lean_assert(id4.is_descendant(id1));
|
|
|
|
lean_assert(!id4.is_descendant(id2));
|
|
|
|
lean_assert(!id4.is_descendant(id3));
|
|
|
|
lean_assert(id5.is_descendant(id3));
|
|
|
|
lean_assert(!id5.is_descendant(id4));
|
|
|
|
lean_assert(id6.is_descendant(id4));
|
|
|
|
lean_assert(!id6.is_descendant(id5));
|
|
|
|
lean_assert(id5.is_descendant(id1));
|
|
|
|
lean_assert(id6.is_descendant(id1));
|
|
|
|
lean_assert(id7.is_descendant(id3));
|
|
|
|
lean_assert(id7.is_descendant(id2));
|
|
|
|
lean_assert(id7.is_descendant(id1));
|
|
|
|
lean_assert(!id7.is_descendant(id4));
|
|
|
|
lean_assert(!id7.is_descendant(id5));
|
|
|
|
lean_assert(!id7.is_descendant(id6));
|
|
|
|
lean_assert(id8.is_descendant(id7));
|
|
|
|
lean_assert(id8.is_descendant(id3));
|
|
|
|
lean_assert(id8.is_descendant(id2));
|
|
|
|
lean_assert(id8.is_descendant(id1));
|
|
|
|
lean_assert(!id8.is_descendant(id4));
|
|
|
|
lean_assert(!id8.is_descendant(id5));
|
|
|
|
lean_assert(!id8.is_descendant(id6));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst2() {
|
|
|
|
constexpr unsigned num_paths = 50;
|
|
|
|
constexpr unsigned path_len = 100;
|
|
|
|
std::vector<environment_id> ids[num_paths];
|
|
|
|
for (unsigned i = 0; i < num_paths; i++) {
|
|
|
|
if (i == 0)
|
|
|
|
ids[i].push_back(environment_id());
|
|
|
|
else
|
|
|
|
ids[i].push_back(environment_id::mk_descendant(ids[i-1][1]));
|
|
|
|
for (unsigned j = 0; j < path_len; j++) {
|
|
|
|
ids[i].push_back(environment_id::mk_descendant(ids[i].back()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < num_paths; i++) {
|
|
|
|
for (unsigned j = 0; j < path_len; j++) {
|
|
|
|
for (unsigned k = 0; k < i; k++) {
|
|
|
|
lean_assert(ids[i][j].is_descendant(ids[k][1]));
|
|
|
|
lean_assert(ids[i][j].is_descendant(ids[k][0]));
|
|
|
|
lean_assert(!ids[k][1].is_descendant(ids[i][j]));
|
|
|
|
lean_assert(!ids[k][0].is_descendant(ids[i][j]));
|
|
|
|
for (unsigned s = 2; s < path_len; s++) {
|
|
|
|
lean_assert(!ids[i][j].is_descendant(ids[k][s]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned k = 0; k < j; k++) {
|
|
|
|
lean_assert(ids[i][j].is_descendant(ids[i][k]));
|
|
|
|
lean_assert(!ids[i][k].is_descendant(ids[i][j]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-08-04 23:07:37 +00:00
|
|
|
int main() {
|
2013-12-01 20:42:32 +00:00
|
|
|
save_stack_info();
|
2013-08-04 23:07:37 +00:00
|
|
|
tst1();
|
|
|
|
tst2();
|
2013-08-05 03:52:14 +00:00
|
|
|
tst3();
|
2014-05-21 19:42:29 +00:00
|
|
|
tst4();
|
2014-05-28 05:25:54 +00:00
|
|
|
environment_id_tester::tst1();
|
|
|
|
environment_id_tester::tst2();
|
2013-08-04 23:07:37 +00:00
|
|
|
return has_violations() ? 1 : 0;
|
|
|
|
}
|