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
|
|
|
|
*/
|
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-04-28 21:04:05 +00:00
|
|
|
static environment add_def(environment const & env, definition 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-16 18:13:50 +00:00
|
|
|
auto env2 = add_def(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-16 18:13:50 +00:00
|
|
|
auto env3 = add_def(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-16 18:13:50 +00:00
|
|
|
auto env4 = add_def(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-16 18:13:50 +00:00
|
|
|
auto env5 = add_def(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-16 18:13:50 +00:00
|
|
|
auto env6 = add_def(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-16 18:13:50 +00:00
|
|
|
auto env7 = add_def(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-16 18:13:50 +00:00
|
|
|
auto env3 = add_def(env2, mk_definition("id", level_param_names(),
|
2014-04-28 21:04:05 +00:00
|
|
|
Pi(A, mk_Type(), A >> A),
|
|
|
|
Fun({{A, mk_Type()}, {x, A}}, x)));
|
|
|
|
expr c = mk_local("c", Bool);
|
|
|
|
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-16 18:13:50 +00:00
|
|
|
env = add_def(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-16 18:13:50 +00:00
|
|
|
env = add_def(env, mk_definition(env, name(base, i), level_param_names(), Bool >> (Bool >> Bool),
|
2014-04-28 21:04:05 +00:00
|
|
|
Fun({{x, Bool}, {y, Bool}}, prev(prev(x, y), prev(y, x)))));
|
|
|
|
}
|
|
|
|
expr A = Const("A");
|
2014-05-16 18:13:50 +00:00
|
|
|
env = add_def(env, mk_definition("id", level_param_names(),
|
2014-04-28 21:04:05 +00:00
|
|
|
Pi(A, mk_Type(), A >> A),
|
|
|
|
Fun({{A, mk_Type()}, {x, A}}, x)));
|
|
|
|
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));
|
|
|
|
expr c1 = mk_local("c1", Bool);
|
|
|
|
expr c2 = mk_local("c2", Bool);
|
|
|
|
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-05 21:08:36 +00:00
|
|
|
environment env(0, true, true, true, 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-16 18:13:50 +00:00
|
|
|
env = add_def(env, mk_definition("id", level_param_names(),
|
2014-04-28 21:04:05 +00:00
|
|
|
Pi(A, mk_Type(), A >> A),
|
|
|
|
Fun({{A, mk_Type()}, {x, A}}, x)));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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();
|
2013-08-04 23:07:37 +00:00
|
|
|
return has_violations() ? 1 : 0;
|
|
|
|
}
|