2013-08-10 01:00:05 +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 23:14:24 +00:00
|
|
|
#include <vector>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/test.h"
|
|
|
|
#include "kernel/environment.h"
|
|
|
|
#include "kernel/kernel_exception.h"
|
|
|
|
#include "kernel/builtin.h"
|
|
|
|
#include "kernel/abstract.h"
|
2013-10-01 01:16:13 +00:00
|
|
|
#include "kernel/printer.h"
|
2013-09-13 03:09:35 +00:00
|
|
|
#include "frontends/lean/frontend.h"
|
|
|
|
#include "frontends/lean/operator_info.h"
|
|
|
|
#include "frontends/lean/pp.h"
|
2013-08-10 01:00:05 +00:00
|
|
|
using namespace lean;
|
|
|
|
|
|
|
|
static void tst1() {
|
2013-12-18 22:37:55 +00:00
|
|
|
environment env; io_state ios; init_frontend(env, ios);
|
|
|
|
env->add_uvar("tst");
|
|
|
|
environment c = env->mk_child();
|
|
|
|
lean_assert(c->get_uvar("tst") == env->get_uvar("tst"));
|
|
|
|
lean_assert(env->has_children());
|
2013-08-10 01:00:05 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 01:06:37 +00:00
|
|
|
static void tst2() {
|
|
|
|
operator_info op1 = infixl(name("and"), 10);
|
|
|
|
operator_info op2 = infixr(name("implies"), 20);
|
|
|
|
lean_assert(op1.get_precedence() == 10);
|
2013-08-15 15:52:10 +00:00
|
|
|
lean_assert(op1.get_fixity() == fixity::Infixl);
|
2013-08-20 00:25:15 +00:00
|
|
|
op1.add_expr(mk_and_fn());
|
2013-08-12 01:06:37 +00:00
|
|
|
operator_info op3 = infixl(name("+"), 10);
|
2013-08-20 00:25:15 +00:00
|
|
|
op3.add_expr(Const(name{"Int", "plus"}));
|
|
|
|
op3.add_expr(Const(name{"Real", "plus"}));
|
2013-08-27 16:49:48 +00:00
|
|
|
std::cout << op3.get_denotations() << "\n";
|
|
|
|
lean_assert(length(op3.get_denotations()) == 2);
|
2013-08-12 01:06:37 +00:00
|
|
|
operator_info op4 = op3.copy();
|
2013-08-20 00:25:15 +00:00
|
|
|
op4.add_expr(Const(name{"Complex", "plus"}));
|
2013-08-27 16:49:48 +00:00
|
|
|
std::cout << op4.get_denotations() << "\n";
|
|
|
|
lean_assert(length(op3.get_denotations()) == 2);
|
|
|
|
lean_assert(length(op4.get_denotations()) == 3);
|
2013-08-15 15:52:10 +00:00
|
|
|
lean_assert(op4.get_fixity() == fixity::Infixl);
|
2013-08-12 01:06:37 +00:00
|
|
|
lean_assert(op4.get_op_name() == op3.get_op_name());
|
|
|
|
lean_assert(prefix("tst", 20).get_fixity() == fixity::Prefix);
|
|
|
|
lean_assert(postfix("!", 20).get_fixity() == fixity::Postfix);
|
|
|
|
lean_assert(length(mixfixl({"if", "then", "else"}, 10).get_op_name_parts()) == 3);
|
|
|
|
lean_assert(mixfixc({"if", "then", "else", "endif"}, 10).get_fixity() == fixity::Mixfixc);
|
|
|
|
std::cout << mixfixc({"if", "then", "else", "endif"}, 10).get_op_name_parts() << "\n";
|
|
|
|
std::cout << op4 << "\n";
|
|
|
|
std::cout << op2 << "\n";
|
|
|
|
std::cout << mixfixc({"if", "then", "else", "endif"}, 10) << "\n";
|
|
|
|
}
|
|
|
|
|
2013-08-15 15:52:10 +00:00
|
|
|
static void tst3() {
|
2013-12-18 22:37:55 +00:00
|
|
|
environment env;
|
2013-08-16 16:30:08 +00:00
|
|
|
std::cout << "====================\n";
|
2013-12-18 22:37:55 +00:00
|
|
|
std::cout << env << "\n";
|
2013-08-17 03:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst4() {
|
2013-12-18 22:37:55 +00:00
|
|
|
environment env; io_state ios; init_frontend(env, ios);
|
|
|
|
formatter fmt = mk_pp_formatter(env);
|
2013-08-17 03:39:24 +00:00
|
|
|
context c;
|
|
|
|
c = extend(c, "x", Bool);
|
|
|
|
c = extend(c, "y", Bool);
|
|
|
|
c = extend(c, "x", Bool, Var(1));
|
|
|
|
c = extend(c, "x", Bool, Var(2));
|
|
|
|
c = extend(c, "z", Bool, Var(1));
|
|
|
|
c = extend(c, "x", Bool, Var(4));
|
|
|
|
std::cout << fmt(c) << "\n";
|
2013-08-15 15:52:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 17:55:42 +00:00
|
|
|
static void tst5() {
|
|
|
|
std::cout << "=================\n";
|
2013-12-18 22:37:55 +00:00
|
|
|
environment env; io_state ios; init_frontend(env, ios);
|
|
|
|
formatter fmt = mk_pp_formatter(env);
|
|
|
|
env->add_var("A", Type());
|
|
|
|
env->add_var("x", Const("A"));
|
|
|
|
optional<object> obj = env->find_object("x");
|
2013-08-17 17:55:42 +00:00
|
|
|
lean_assert(obj);
|
2013-12-08 22:37:38 +00:00
|
|
|
lean_assert(obj->get_name() == "x");
|
|
|
|
std::cout << fmt(*obj) << "\n";
|
2013-12-18 22:37:55 +00:00
|
|
|
optional<object> obj2 = env->find_object("y");
|
2013-08-17 17:55:42 +00:00
|
|
|
lean_assert(!obj2);
|
|
|
|
try {
|
2013-12-18 22:37:55 +00:00
|
|
|
env->get_object("y");
|
2013-08-17 17:55:42 +00:00
|
|
|
lean_unreachable();
|
|
|
|
} catch (unknown_name_exception & ex) {
|
|
|
|
std::cout << ex.what() << " " << ex.get_name() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-17 18:04:22 +00:00
|
|
|
class alien_cell : public neutral_object_cell {
|
|
|
|
unsigned m_data[128];
|
|
|
|
public:
|
|
|
|
alien_cell() {}
|
|
|
|
unsigned & get_data(unsigned i) { return m_data[i]; }
|
|
|
|
virtual char const * keyword() const { return "alien"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void tst6() {
|
|
|
|
std::cout << "=================\n";
|
2013-12-18 22:37:55 +00:00
|
|
|
environment env; io_state ios; init_frontend(env, ios);
|
2013-12-13 00:33:31 +00:00
|
|
|
env->add_neutral_object(new alien_cell());
|
2013-12-18 22:37:55 +00:00
|
|
|
formatter fmt = mk_pp_formatter(env);
|
2013-08-17 18:04:22 +00:00
|
|
|
std::cout << fmt(env) << "\n";
|
|
|
|
}
|
|
|
|
|
2013-08-17 19:33:19 +00:00
|
|
|
static void tst7() {
|
2013-12-18 22:37:55 +00:00
|
|
|
environment env; io_state ios; init_frontend(env, ios);
|
|
|
|
formatter fmt = mk_pp_formatter(env);
|
2013-08-17 19:33:19 +00:00
|
|
|
std::cout << fmt(And(Const("x"), Const("y"))) << "\n";
|
|
|
|
}
|
|
|
|
|
2013-08-17 19:38:32 +00:00
|
|
|
static void tst8() {
|
2013-12-18 22:37:55 +00:00
|
|
|
environment env; io_state ios; init_frontend(env, ios);
|
|
|
|
formatter fmt = mk_pp_formatter(env);
|
|
|
|
add_infixl(env, ios, "<-$->", 10, mk_refl_fn());
|
|
|
|
std::cout << fmt(*(env->find_object("Trivial"))) << "\n";
|
2013-08-17 19:38:32 +00:00
|
|
|
}
|
|
|
|
|
2013-08-18 07:28:26 +00:00
|
|
|
static void tst9() {
|
2013-12-18 22:37:55 +00:00
|
|
|
environment env; io_state ios; init_frontend(env, ios);
|
|
|
|
lean_assert(!env->has_children());
|
2013-08-18 07:28:26 +00:00
|
|
|
{
|
2013-12-18 22:37:55 +00:00
|
|
|
environment c = env->mk_child();
|
|
|
|
lean_assert(env->has_children());
|
2013-08-18 07:28:26 +00:00
|
|
|
}
|
2013-12-18 22:37:55 +00:00
|
|
|
lean_assert(!env->has_children());
|
|
|
|
env->add_uvar("l", level()+1);
|
|
|
|
lean_assert(env->get_uvar("l") == level("l"));
|
|
|
|
try { env->get_uvar("l2"); lean_unreachable(); }
|
2013-08-18 07:28:26 +00:00
|
|
|
catch (exception &) {}
|
2013-12-18 22:37:55 +00:00
|
|
|
env->add_definition("x", Bool, True);
|
|
|
|
object const & obj = env->get_object("x");
|
2013-08-18 07:28:26 +00:00
|
|
|
lean_assert(obj.get_name() == "x");
|
2013-11-26 19:30:18 +00:00
|
|
|
lean_assert(is_bool(obj.get_type()));
|
2013-08-18 07:28:26 +00:00
|
|
|
lean_assert(obj.get_value() == True);
|
2013-12-18 22:37:55 +00:00
|
|
|
try { env->get_object("y"); lean_unreachable(); }
|
2013-08-18 07:28:26 +00:00
|
|
|
catch (exception &) {}
|
2013-12-18 22:37:55 +00:00
|
|
|
lean_assert(!env->find_object("y"));
|
|
|
|
env->add_definition("y", False);
|
|
|
|
lean_assert(is_bool(env->find_object("y")->get_type()));
|
|
|
|
lean_assert(env->has_object("y"));
|
|
|
|
lean_assert(!env->has_object("z"));
|
2013-08-18 07:28:26 +00:00
|
|
|
bool found = false;
|
2013-12-18 22:37:55 +00:00
|
|
|
std::for_each(env->begin_objects(), env->end_objects(), [&](object const & obj) { if (obj.has_name() && obj.get_name() == "y") found = true; });
|
2013-08-18 07:28:26 +00:00
|
|
|
lean_assert(found);
|
2013-12-18 22:37:55 +00:00
|
|
|
add_postfix(env, ios, "!", 10, Const("factorial"));
|
2013-08-18 07:28:26 +00:00
|
|
|
name parts[] = {"if", "then", "else"};
|
2013-12-18 22:37:55 +00:00
|
|
|
add_mixfixl(env, ios, 3, parts, 10, Const("if"));
|
2013-08-18 07:28:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-18 21:29:32 +00:00
|
|
|
static void tst10() {
|
2013-12-18 22:37:55 +00:00
|
|
|
environment env; io_state ios; init_frontend(env, ios);
|
|
|
|
formatter fmt = mk_pp_formatter(env);
|
2013-08-18 21:29:32 +00:00
|
|
|
expr x = Const("xxxxxxxxxxxx");
|
|
|
|
expr y = Const("y");
|
|
|
|
expr z = Const("foo");
|
2013-09-13 19:49:03 +00:00
|
|
|
expr e = Let({{x, True}, {y, And({x, x, x, x, x, x, x, x})}, {z, And(x, y)}}, Or({x, x, x, x, x, x, x, x, x, z, z, z, z, z, z, z}));
|
2013-08-18 21:29:32 +00:00
|
|
|
std::cout << e << "\n";
|
|
|
|
std::cout << fmt(e) << "\n";
|
|
|
|
}
|
|
|
|
|
2013-08-26 16:04:17 +00:00
|
|
|
static void tst11() {
|
2013-12-18 22:37:55 +00:00
|
|
|
environment env; io_state ios; init_frontend(env, ios);
|
2013-08-26 16:04:17 +00:00
|
|
|
expr A = Const("A");
|
2013-12-18 22:37:55 +00:00
|
|
|
env->add_var("g", Pi({A, Type()}, A >> (A >> A)));
|
|
|
|
lean_assert(!has_implicit_arguments(env, "g"));
|
|
|
|
mark_implicit_arguments(env, "g", {true, false, false});
|
|
|
|
lean_assert(has_implicit_arguments(env, "g"))
|
|
|
|
name gexp = get_explicit_version(env, "g");
|
|
|
|
lean_assert(env->find_object(gexp));
|
|
|
|
lean_assert(env->find_object("g")->get_type() == env->find_object(gexp)->get_type());
|
|
|
|
lean_assert(get_implicit_arguments(env, "g") == std::vector<bool>({true})); // the trailing "false" marks are removed
|
2013-08-26 16:04:17 +00:00
|
|
|
try {
|
2013-12-18 22:37:55 +00:00
|
|
|
mark_implicit_arguments(env, "g", {true, false, false});
|
2013-08-26 16:04:17 +00:00
|
|
|
lean_unreachable();
|
|
|
|
} catch (exception & ex) {
|
|
|
|
std::cout << "Expected error: " << ex.what() << std::endl;
|
|
|
|
}
|
2013-12-18 22:37:55 +00:00
|
|
|
env->add_var("s", Pi({A, Type()}, A >> (A >> A)));
|
2013-08-26 16:04:17 +00:00
|
|
|
try {
|
2013-12-18 22:37:55 +00:00
|
|
|
mark_implicit_arguments(env, "s", {true, true, true, true});
|
2013-08-26 16:04:17 +00:00
|
|
|
lean_unreachable();
|
|
|
|
} catch (exception & ex) {
|
|
|
|
std::cout << "Expected error: " << ex.what() << std::endl;
|
|
|
|
}
|
2013-12-22 01:02:16 +00:00
|
|
|
env->add_var(name("@h"), Pi({A, Type()}, A >> (A >> A)));
|
2013-12-18 22:37:55 +00:00
|
|
|
env->add_var("h", Pi({A, Type()}, A >> (A >> A)));
|
2013-08-26 16:04:17 +00:00
|
|
|
try {
|
2013-12-18 22:37:55 +00:00
|
|
|
mark_implicit_arguments(env, "h", {true, false, false});
|
2013-08-26 16:04:17 +00:00
|
|
|
lean_unreachable();
|
|
|
|
} catch (exception & ex) {
|
|
|
|
std::cout << "Expected error: " << ex.what() << std::endl;
|
|
|
|
}
|
|
|
|
try {
|
2013-12-18 22:37:55 +00:00
|
|
|
mark_implicit_arguments(env, "u", {true, false});
|
2013-08-26 16:04:17 +00:00
|
|
|
lean_unreachable();
|
|
|
|
} catch (exception & ex) {
|
|
|
|
std::cout << "Expected error: " << ex.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-10 01:00:05 +00:00
|
|
|
int main() {
|
2013-12-01 20:42:32 +00:00
|
|
|
save_stack_info();
|
2013-08-10 01:00:05 +00:00
|
|
|
tst1();
|
2013-08-12 01:06:37 +00:00
|
|
|
tst2();
|
2013-08-15 15:52:10 +00:00
|
|
|
tst3();
|
2013-08-17 03:51:11 +00:00
|
|
|
tst4();
|
2013-08-17 17:55:42 +00:00
|
|
|
tst5();
|
2013-08-17 18:04:22 +00:00
|
|
|
tst6();
|
2013-08-17 19:33:19 +00:00
|
|
|
tst7();
|
2013-08-17 19:38:32 +00:00
|
|
|
tst8();
|
2013-08-18 07:28:26 +00:00
|
|
|
tst9();
|
2013-08-18 21:29:32 +00:00
|
|
|
tst10();
|
2013-08-26 16:04:17 +00:00
|
|
|
tst11();
|
2013-08-10 01:00:05 +00:00
|
|
|
return has_violations() ? 1 : 0;
|
|
|
|
}
|