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
|
|
|
|
*/
|
|
|
|
#include "environment.h"
|
|
|
|
#include "type_check.h"
|
2013-08-06 03:06:07 +00:00
|
|
|
#include "toplevel.h"
|
2013-08-04 23:07:37 +00:00
|
|
|
#include "builtin.h"
|
|
|
|
#include "arith.h"
|
|
|
|
#include "normalize.h"
|
|
|
|
#include "abstract.h"
|
|
|
|
#include "exception.h"
|
2013-08-05 17:33:05 +00:00
|
|
|
#include "trace.h"
|
2013-08-04 23:07:37 +00:00
|
|
|
#include "test.h"
|
|
|
|
using namespace lean;
|
|
|
|
|
|
|
|
static void tst1() {
|
|
|
|
environment env;
|
2013-08-09 22:33:34 +00:00
|
|
|
level u = env.add_uvar("u", level() + 1);
|
|
|
|
level w = env.add_uvar("w", u + 1);
|
2013-08-04 23:07:37 +00:00
|
|
|
lean_assert(!env.has_children());
|
|
|
|
lean_assert(!env.has_parent());
|
|
|
|
{
|
|
|
|
environment child = env.mk_child();
|
|
|
|
lean_assert(child.is_ge(w, u));
|
|
|
|
lean_assert(child.is_ge(w, level() + 2));
|
|
|
|
lean_assert(env.is_ge(w, level() + 2));
|
|
|
|
lean_assert(env.has_children());
|
|
|
|
lean_assert(child.has_parent());
|
|
|
|
lean_assert(!env.has_parent());
|
|
|
|
try {
|
2013-08-09 22:33:34 +00:00
|
|
|
level o = env.add_uvar("o", w + 1);
|
2013-08-04 23:07:37 +00:00
|
|
|
lean_unreachable();
|
2013-08-05 03:52:14 +00:00
|
|
|
} catch (exception const & ex) {
|
2013-08-04 23:07:37 +00:00
|
|
|
std::cout << "expected error: " << ex.what() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cout << "tst1 checkpoint" << std::endl;
|
2013-08-09 22:33:34 +00:00
|
|
|
level o = env.add_uvar("o", w + 1);
|
2013-08-04 23:07:37 +00:00
|
|
|
lean_assert(!env.has_children());
|
|
|
|
env.display_uvars(std::cout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static environment mk_child() {
|
|
|
|
environment env;
|
2013-08-09 22:33:34 +00:00
|
|
|
level u = env.add_uvar("u", level() + 1);
|
2013-08-04 23:07:37 +00:00
|
|
|
return env.mk_child();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst2() {
|
|
|
|
environment child = mk_child();
|
|
|
|
lean_assert(child.has_parent());
|
|
|
|
lean_assert(!child.has_children());
|
|
|
|
environment parent = child.parent();
|
|
|
|
parent.display_uvars(std::cout);
|
|
|
|
lean_assert(parent.has_children());
|
2013-08-05 00:47:54 +00:00
|
|
|
std::cout << "uvar: " << child.get_uvar("u") << "\n";
|
2013-08-04 23:07:37 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 03:52:14 +00:00
|
|
|
static void tst3() {
|
|
|
|
environment env;
|
|
|
|
try {
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_definition("a", Int, Const("a"));
|
2013-08-05 03:52:14 +00:00
|
|
|
lean_unreachable();
|
2013-08-05 22:47:58 +00:00
|
|
|
} catch (exception const & ex) {
|
2013-08-05 03:52:14 +00:00
|
|
|
std::cout << "expected error: " << ex.what() << "\n";
|
|
|
|
}
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_definition("a", Int, iAdd(iVal(1), iVal(2)));
|
|
|
|
expr t = iAdd(Const("a"), iVal(1));
|
2013-08-05 03:52:14 +00:00
|
|
|
std::cout << t << " --> " << normalize(t, env) << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
lean_assert(normalize(t, env) == iVal(4));
|
|
|
|
env.add_definition("b", Int, iMul(iVal(2), Const("a")));
|
|
|
|
std::cout << "b --> " << normalize(Const("b"), env) << "\n";
|
|
|
|
lean_assert(normalize(Const("b"), env) == iVal(6));
|
2013-08-05 03:52:14 +00:00
|
|
|
try {
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_definition("c", arrow(Int, Int), Const("a"));
|
2013-08-05 03:52:14 +00:00
|
|
|
lean_unreachable();
|
2013-08-05 22:47:58 +00:00
|
|
|
} catch (exception const & ex) {
|
2013-08-05 03:52:14 +00:00
|
|
|
std::cout << "expected error: " << ex.what() << "\n";
|
|
|
|
}
|
|
|
|
try {
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_definition("a", Int, iVal(10));
|
2013-08-05 03:52:14 +00:00
|
|
|
lean_unreachable();
|
2013-08-05 22:47:58 +00:00
|
|
|
} catch (exception const & ex) {
|
2013-08-05 03:52:14 +00:00
|
|
|
std::cout << "expected error: " << ex.what() << "\n";
|
|
|
|
}
|
|
|
|
environment c_env = env.mk_child();
|
|
|
|
try {
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_definition("c", Int, Const("a"));
|
2013-08-05 03:52:14 +00:00
|
|
|
lean_unreachable();
|
2013-08-05 22:47:58 +00:00
|
|
|
} catch (exception const & ex) {
|
2013-08-05 03:52:14 +00:00
|
|
|
std::cout << "expected error: " << ex.what() << "\n";
|
|
|
|
}
|
2013-08-06 18:27:14 +00:00
|
|
|
lean_assert(normalize(Const("b"), env) == iVal(6));
|
|
|
|
lean_assert(normalize(Const("b"), c_env) == iVal(6));
|
|
|
|
c_env.add_definition("c", Int, Const("a"));
|
|
|
|
lean_assert(normalize(Const("c"), c_env) == iVal(3));
|
2013-08-05 03:52:14 +00:00
|
|
|
try {
|
2013-08-06 18:27:14 +00:00
|
|
|
expr r = normalize(Const("c"), env);
|
|
|
|
lean_assert(r == iVal(3));
|
2013-08-05 03:52:14 +00:00
|
|
|
lean_unreachable();
|
2013-08-05 22:47:58 +00:00
|
|
|
} catch (exception const & ex) {
|
|
|
|
std::cout << "expected error: " << ex.what() << std::endl;
|
2013-08-05 03:52:14 +00:00
|
|
|
}
|
2013-08-05 22:47:58 +00:00
|
|
|
std::cout << "end tst3" << std::endl;
|
2013-08-05 03:52:14 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 17:33:05 +00:00
|
|
|
static void tst4() {
|
|
|
|
environment env;
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_definition("a", Int, iVal(1), true); // add opaque definition
|
|
|
|
expr t = iAdd(Const("a"), iVal(1));
|
2013-08-05 17:33:05 +00:00
|
|
|
std::cout << t << " --> " << normalize(t, env) << "\n";
|
|
|
|
lean_assert(normalize(t, env) == t);
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_definition("b", Int, iAdd(Const("a"), iVal(1)));
|
|
|
|
expr t2 = iSub(Const("b"), iVal(9));
|
2013-08-05 17:33:05 +00:00
|
|
|
std::cout << t2 << " --> " << normalize(t2, env) << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
lean_assert(normalize(t2, env) == iSub(iAdd(Const("a"), iVal(1)), iVal(9)));
|
2013-08-05 17:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst5() {
|
|
|
|
environment env;
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_definition("a", Int, iVal(1), true); // add opaque definition
|
2013-08-05 17:33:05 +00:00
|
|
|
try {
|
2013-08-06 18:27:14 +00:00
|
|
|
std::cout << infer_type(iAdd(Const("a"), Int), env) << "\n";
|
2013-08-05 17:33:05 +00:00
|
|
|
lean_unreachable();
|
2013-08-05 22:47:58 +00:00
|
|
|
} catch (exception const & ex) {
|
2013-08-05 17:33:05 +00:00
|
|
|
std::cout << "expected error: " << ex.what() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:16:20 +00:00
|
|
|
static void tst6() {
|
|
|
|
environment env;
|
2013-08-09 22:33:34 +00:00
|
|
|
level u = env.add_uvar("u", level() + 1);
|
|
|
|
level w = env.add_uvar("w", u + 1);
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_var("f", arrow(Type(u), Type(u)));
|
|
|
|
expr t = Const("f")(Int);
|
2013-08-05 20:16:20 +00:00
|
|
|
std::cout << "type of " << t << " is " << infer_type(t, env) << "\n";
|
|
|
|
try {
|
2013-08-06 18:27:14 +00:00
|
|
|
infer_type(Const("f")(Type(w)), env);
|
2013-08-05 20:16:20 +00:00
|
|
|
lean_unreachable();
|
2013-08-05 22:47:58 +00:00
|
|
|
} catch (exception const & ex) {
|
2013-08-05 20:16:20 +00:00
|
|
|
std::cout << "expected error: " << ex.what() << "\n";
|
|
|
|
}
|
|
|
|
try {
|
2013-08-06 18:27:14 +00:00
|
|
|
infer_type(Const("f")(Type(u)), env);
|
2013-08-05 20:16:20 +00:00
|
|
|
lean_unreachable();
|
2013-08-05 22:47:58 +00:00
|
|
|
} catch (exception const & ex) {
|
2013-08-05 20:16:20 +00:00
|
|
|
std::cout << "expected error: " << ex.what() << "\n";
|
|
|
|
}
|
2013-08-06 18:27:14 +00:00
|
|
|
t = Const("f")(Type());
|
2013-08-05 20:16:20 +00:00
|
|
|
std::cout << "type of " << t << " is " << infer_type(t, env) << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
std::cout << infer_type(arrow(Type(u), Type(w)), env) << "\n";
|
|
|
|
lean_assert(infer_type(arrow(Type(u), Type(w)), env) == Type(max(u+1, w+1)));
|
|
|
|
std::cout << infer_type(arrow(Int, Int), env) << "\n";
|
|
|
|
lean_assert(infer_type(arrow(Int, Int), env) == Type());
|
2013-08-05 20:16:20 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 03:06:07 +00:00
|
|
|
static void tst7() {
|
|
|
|
environment env = mk_toplevel();
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_var("a", Int);
|
|
|
|
env.add_var("b", Int);
|
|
|
|
expr t = If(Int, True, Const("a"), Const("b"));
|
2013-08-06 03:06:07 +00:00
|
|
|
std::cout << t << " --> " << normalize(t, env) << "\n";
|
|
|
|
std::cout << infer_type(t, env) << "\n";
|
|
|
|
std::cout << "Environment\n" << env;
|
|
|
|
}
|
|
|
|
|
2013-08-04 23:07:37 +00:00
|
|
|
int main() {
|
2013-08-05 17:33:05 +00:00
|
|
|
enable_trace("is_convertible");
|
2013-08-04 23:07:37 +00:00
|
|
|
continue_on_violation(true);
|
|
|
|
tst1();
|
|
|
|
tst2();
|
2013-08-05 03:52:14 +00:00
|
|
|
tst3();
|
2013-08-05 17:33:05 +00:00
|
|
|
tst4();
|
|
|
|
tst5();
|
2013-08-05 20:16:20 +00:00
|
|
|
tst6();
|
2013-08-06 03:06:07 +00:00
|
|
|
tst7();
|
2013-08-04 23:07:37 +00:00
|
|
|
return has_violations() ? 1 : 0;
|
|
|
|
}
|