2013-07-25 02:36:54 +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-07-26 02:13:45 +00:00
|
|
|
#include <algorithm>
|
2013-08-20 14:23:59 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/test.h"
|
|
|
|
#include "util/trace.h"
|
|
|
|
#include "util/exception.h"
|
|
|
|
#include "kernel/normalizer.h"
|
|
|
|
#include "kernel/builtin.h"
|
|
|
|
#include "kernel/expr_sets.h"
|
|
|
|
#include "kernel/abstract.h"
|
|
|
|
#include "kernel/kernel_exception.h"
|
|
|
|
#include "library/printer.h"
|
|
|
|
#include "library/import_all/import_all.h"
|
2013-07-25 02:36:54 +00:00
|
|
|
using namespace lean;
|
|
|
|
|
2013-07-30 07:25:19 +00:00
|
|
|
expr normalize(expr const & e) {
|
|
|
|
environment env;
|
|
|
|
return normalize(e, env);
|
|
|
|
}
|
|
|
|
|
2013-08-05 03:52:14 +00:00
|
|
|
static void eval(expr const & e, environment & env) { std::cout << e << " --> " << normalize(e, env) << "\n"; }
|
2013-08-06 18:27:14 +00:00
|
|
|
static expr t() { return Const("t"); }
|
|
|
|
static expr lam(expr const & e) { return mk_lambda("_", t(), e); }
|
|
|
|
static expr lam(expr const & t, expr const & e) { return mk_lambda("_", t, e); }
|
|
|
|
static expr v(unsigned i) { return Var(i); }
|
2013-07-26 02:13:45 +00:00
|
|
|
static expr zero() {
|
|
|
|
// fun (t : T) (s : t -> t) (z : t) z
|
2013-08-18 22:44:39 +00:00
|
|
|
return lam(t(), lam(mk_arrow(v(0), v(0)), lam(v(1), v(0))));
|
2013-07-26 02:13:45 +00:00
|
|
|
}
|
|
|
|
static expr one() {
|
|
|
|
// fun (t : T) (s : t -> t) s
|
2013-08-18 22:44:39 +00:00
|
|
|
return lam(t(), lam(mk_arrow(v(0), v(0)), v(0)));
|
2013-07-26 02:13:45 +00:00
|
|
|
}
|
2013-08-06 18:27:14 +00:00
|
|
|
static expr num() { return Const("num"); }
|
2013-07-26 02:13:45 +00:00
|
|
|
static expr plus() {
|
|
|
|
// fun (m n : numeral) (A : Type 0) (f : A -> A) (x : A) => m A f (n A f x).
|
|
|
|
expr x = v(0), f = v(1), A = v(2), n = v(3), m = v(4);
|
|
|
|
expr body = m(A, f, n(A, f, x));
|
2013-08-18 22:44:39 +00:00
|
|
|
return lam(num(), lam(num(), lam(t(), lam(mk_arrow(v(0), v(0)), lam(v(1), body)))));
|
2013-07-26 02:13:45 +00:00
|
|
|
}
|
2013-08-06 18:27:14 +00:00
|
|
|
static expr two() { return mk_app({plus(), one(), one()}); }
|
|
|
|
static expr three() { return mk_app({plus(), two(), one()}); }
|
|
|
|
static expr four() { return mk_app({plus(), two(), two()}); }
|
2013-07-26 02:13:45 +00:00
|
|
|
static expr times() {
|
|
|
|
// fun (m n : numeral) (A : Type 0) (f : A -> A) (x : A) => m A (n A f) x.
|
|
|
|
expr x = v(0), f = v(1), A = v(2), n = v(3), m = v(4);
|
|
|
|
expr body = m(A, n(A, f), x);
|
2013-08-18 22:44:39 +00:00
|
|
|
return lam(num(), lam(num(), lam(t(), lam(mk_arrow(v(0), v(0)), lam(v(1), body)))));
|
2013-07-26 02:13:45 +00:00
|
|
|
}
|
|
|
|
static expr power() {
|
|
|
|
// fun (m n : numeral) (A : Type 0) => m (A -> A) (n A).
|
|
|
|
expr A = v(0), n = v(1), m = v(2);
|
2013-08-18 22:44:39 +00:00
|
|
|
expr body = n(mk_arrow(A, A), m(A));
|
|
|
|
return lam(num(), lam(num(), lam(mk_arrow(v(0), v(0)), body)));
|
2013-07-26 02:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned count_core(expr const & a, expr_set & s) {
|
|
|
|
if (s.find(a) != s.end())
|
|
|
|
return 0;
|
|
|
|
s.insert(a);
|
|
|
|
switch (a.kind()) {
|
2013-08-03 23:09:21 +00:00
|
|
|
case expr_kind::Var: case expr_kind::Constant: case expr_kind::Type: case expr_kind::Value:
|
2013-07-26 02:13:45 +00:00
|
|
|
return 1;
|
|
|
|
case expr_kind::App:
|
|
|
|
return std::accumulate(begin_args(a), end_args(a), 1,
|
2013-07-26 21:16:29 +00:00
|
|
|
[&](unsigned sum, expr const & arg){ return sum + count_core(arg, s); });
|
2013-08-04 16:37:52 +00:00
|
|
|
case expr_kind::Eq:
|
|
|
|
return count_core(eq_lhs(a), s) + count_core(eq_rhs(a), s) + 1;
|
2013-07-26 02:13:45 +00:00
|
|
|
case expr_kind::Lambda: case expr_kind::Pi:
|
2013-08-03 18:31:42 +00:00
|
|
|
return count_core(abst_domain(a), s) + count_core(abst_body(a), s) + 1;
|
2013-08-04 16:37:52 +00:00
|
|
|
case expr_kind::Let:
|
|
|
|
return count_core(let_value(a), s) + count_core(let_body(a), s) + 1;
|
2013-07-26 02:13:45 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned count(expr const & a) {
|
|
|
|
expr_set s;
|
|
|
|
return count_core(a, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst_church_numbers() {
|
2013-08-05 03:52:14 +00:00
|
|
|
environment env;
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_var("t", Type());
|
|
|
|
env.add_var("N", Type());
|
|
|
|
env.add_var("z", Const("N"));
|
|
|
|
env.add_var("s", Const("N"));
|
|
|
|
expr N = Const("N");
|
|
|
|
expr z = Const("z");
|
|
|
|
expr s = Const("s");
|
|
|
|
std::cout << normalize(mk_app(zero(), N, s, z), env) << "\n";
|
|
|
|
std::cout << normalize(mk_app(one(), N, s, z), env) << "\n";
|
|
|
|
std::cout << normalize(mk_app(two(), N, s, z), env) << "\n";
|
|
|
|
std::cout << normalize(mk_app(four(), N, s, z), env) << "\n";
|
|
|
|
std::cout << count(normalize(mk_app(four(), N, s, z), env)) << "\n";
|
|
|
|
lean_assert(count(normalize(mk_app(four(), N, s, z), env)) == 4 + 2);
|
|
|
|
std::cout << normalize(mk_app(mk_app(times(), four(), four()), N, s, z), env) << "\n";
|
|
|
|
std::cout << normalize(mk_app(mk_app(power(), two(), four()), N, s, z), env) << "\n";
|
|
|
|
lean_assert(count(normalize(mk_app(mk_app(power(), two(), four()), N, s, z), env)) == 16 + 2);
|
|
|
|
std::cout << normalize(mk_app(mk_app(times(), two(), mk_app(power(), two(), four())), N, s, z), env) << "\n";
|
|
|
|
std::cout << count(normalize(mk_app(mk_app(times(), two(), mk_app(power(), two(), four())), N, s, z), env)) << "\n";
|
|
|
|
std::cout << count(normalize(mk_app(mk_app(times(), four(), mk_app(power(), two(), four())), N, s, z), env)) << "\n";
|
|
|
|
lean_assert(count(normalize(mk_app(mk_app(times(), four(), mk_app(power(), two(), four())), N, s, z), env)) == 64 + 2);
|
|
|
|
expr big = normalize(mk_app(mk_app(power(), two(), mk_app(power(), two(), three())), N, s, z), env);
|
2013-07-26 05:05:09 +00:00
|
|
|
std::cout << count(big) << "\n";
|
|
|
|
lean_assert(count(big) == 256 + 2);
|
2013-08-06 18:27:14 +00:00
|
|
|
expr three = mk_app(plus(), two(), one());
|
|
|
|
lean_assert(count(normalize(mk_app(mk_app(power(), three, three), N, s, z), env)) == 27 + 2);
|
|
|
|
// expr big2 = normalize(mk_app(mk_app(power(), two(), mk_app(times(), mk_app(plus(), four(), one()), four())), N, s, z), env);
|
2013-07-30 08:39:29 +00:00
|
|
|
// std::cout << count(big2) << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
std::cout << normalize(lam(lam(mk_app(mk_app(times(), four(), four()), N, Var(0), z))), env) << "\n";
|
2013-07-26 02:13:45 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 02:36:54 +00:00
|
|
|
static void tst1() {
|
2013-08-05 03:52:14 +00:00
|
|
|
environment env;
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_var("t", Type());
|
|
|
|
expr t = Type();
|
2013-08-18 22:44:39 +00:00
|
|
|
env.add_var("f", mk_arrow(t, t));
|
2013-08-06 18:27:14 +00:00
|
|
|
expr f = Const("f");
|
2013-08-06 03:06:07 +00:00
|
|
|
env.add_var("a", t);
|
2013-08-06 18:27:14 +00:00
|
|
|
expr a = Const("a");
|
2013-08-06 03:06:07 +00:00
|
|
|
env.add_var("b", t);
|
2013-08-06 18:27:14 +00:00
|
|
|
expr b = Const("b");
|
|
|
|
expr x = Var(0);
|
|
|
|
expr y = Var(1);
|
|
|
|
eval(mk_app(mk_lambda("x", t, x), a), env);
|
|
|
|
eval(mk_app(mk_lambda("x", t, x), a, b), env);
|
|
|
|
eval(mk_lambda("x", t, f(x)), env);
|
|
|
|
eval(mk_lambda("y", t, mk_lambda("x", t, f(y, x))), env);
|
|
|
|
eval(mk_app(mk_lambda("x", t,
|
|
|
|
mk_app(mk_lambda("f", t,
|
|
|
|
mk_app(Var(0), b)),
|
|
|
|
mk_lambda("g", t, f(Var(1))))),
|
2013-08-05 03:52:14 +00:00
|
|
|
a), env);
|
2013-07-26 02:13:45 +00:00
|
|
|
expr l01 = lam(v(0)(v(1)));
|
|
|
|
expr l12 = lam(lam(v(1)(v(2))));
|
2013-08-05 03:52:14 +00:00
|
|
|
eval(lam(l12(l01)), env);
|
|
|
|
lean_assert(normalize(lam(l12(l01)), env) == lam(lam(v(1)(v(1)))));
|
2013-07-25 02:36:54 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 07:25:19 +00:00
|
|
|
static void tst2() {
|
|
|
|
environment env;
|
2013-08-06 18:27:14 +00:00
|
|
|
expr t = Type();
|
2013-08-18 22:44:39 +00:00
|
|
|
env.add_var("f", mk_arrow(t, t));
|
2013-08-06 18:27:14 +00:00
|
|
|
expr f = Const("f");
|
2013-08-06 03:06:07 +00:00
|
|
|
env.add_var("a", t);
|
2013-08-06 18:27:14 +00:00
|
|
|
expr a = Const("a");
|
2013-08-06 03:06:07 +00:00
|
|
|
env.add_var("b", t);
|
2013-08-06 18:27:14 +00:00
|
|
|
expr b = Const("b");
|
2013-08-18 22:44:39 +00:00
|
|
|
env.add_var("h", mk_arrow(t, t));
|
2013-08-06 18:27:14 +00:00
|
|
|
expr h = Const("h");
|
|
|
|
expr x = Var(0);
|
|
|
|
expr y = Var(1);
|
2013-07-30 08:39:29 +00:00
|
|
|
lean_assert(normalize(f(x,x), env, extend(context(), name("f"), t, f(a))) == f(f(a), f(a)));
|
|
|
|
context c1 = extend(extend(context(), name("f"), t, f(a)), name("h"), t, h(x));
|
|
|
|
expr F1 = normalize(f(x,f(x)), env, c1);
|
|
|
|
lean_assert(F1 == f(h(f(a)), f(h(f(a)))));
|
|
|
|
std::cout << F1 << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
expr F2 = normalize(mk_lambda("x", t, f(x, f(y))), env, c1);
|
2013-07-30 08:39:29 +00:00
|
|
|
std::cout << F2 << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
lean_assert(F2 == mk_lambda("x", t, f(x, f(h(f(a))))));
|
|
|
|
expr F3 = normalize(mk_lambda("y", t, mk_lambda("x", t, f(x, f(y)))), env, c1);
|
2013-07-30 08:39:29 +00:00
|
|
|
std::cout << F3 << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
lean_assert(F3 == mk_lambda("y", t, mk_lambda("x", t, f(x, f(y)))));
|
|
|
|
context c2 = extend(extend(context(), name("foo"), t, mk_lambda("x", t, f(x, a))), name("bla"), t, mk_lambda("z", t, h(x,y)));
|
|
|
|
expr F4 = normalize(mk_lambda("x", t, f(x, f(y))), env, c2);
|
2013-07-30 08:39:29 +00:00
|
|
|
std::cout << F4 << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
lean_assert(F4 == mk_lambda("x", t, f(x, f(mk_lambda("z", t, h(x,mk_lambda("x", t, f(x, a))))))));
|
2013-07-30 08:39:29 +00:00
|
|
|
context c3 = extend(context(), name("x"), t);
|
2013-08-06 18:27:14 +00:00
|
|
|
expr f5 = mk_app(mk_lambda("f", t, mk_lambda("z", t, Var(1))), mk_lambda("y", t, Var(1)));
|
2013-07-30 08:39:29 +00:00
|
|
|
expr F5 = normalize(f5, env, c3);
|
|
|
|
std::cout << f5 << "\n---->\n";
|
|
|
|
std::cout << F5 << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
lean_assert(F5 == mk_lambda("z", t, mk_lambda("y", t, Var(2))));
|
2013-07-30 08:39:29 +00:00
|
|
|
context c4 = extend(extend(context(), name("x"), t), name("x2"), t);
|
2013-08-06 18:27:14 +00:00
|
|
|
expr F6 = normalize(mk_app(mk_lambda("f", t, mk_lambda("z1", t, mk_lambda("z2", t, mk_app(Var(2), Const("a"))))),
|
|
|
|
mk_lambda("y", t, mk_app(Var(1), Var(2), Var(0)))), env, c4);
|
2013-07-30 08:39:29 +00:00
|
|
|
std::cout << F6 << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
lean_assert(F6 == mk_lambda("z1", t, mk_lambda("z2", t, mk_app(Var(2), Var(3), Const("a")))));
|
2013-07-30 07:25:19 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 16:37:52 +00:00
|
|
|
static void tst3() {
|
2013-09-04 15:30:04 +00:00
|
|
|
environment env = mk_toplevel();
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_var("a", Bool);
|
|
|
|
expr t1 = Const("a");
|
|
|
|
expr t2 = Const("a");
|
|
|
|
expr e = Eq(t1, t2);
|
2013-08-04 16:37:52 +00:00
|
|
|
std::cout << e << " --> " << normalize(e, env) << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
lean_assert(normalize(e, env) == True);
|
2013-08-04 16:37:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst4() {
|
|
|
|
environment env;
|
2013-08-06 18:27:14 +00:00
|
|
|
env.add_var("b", Type());
|
2013-09-06 17:06:26 +00:00
|
|
|
expr t1 = mk_let("a", expr(), Const("b"), mk_lambda("c", Type(), Var(1)(Var(0))));
|
2013-08-04 16:37:52 +00:00
|
|
|
std::cout << t1 << " --> " << normalize(t1, env) << "\n";
|
2013-08-06 18:27:14 +00:00
|
|
|
lean_assert(normalize(t1, env) == mk_lambda("c", Type(), Const("b")(Var(0))));
|
2013-08-04 16:37:52 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 14:23:59 +00:00
|
|
|
static expr mk_big(unsigned depth) {
|
|
|
|
if (depth == 0)
|
|
|
|
return Const("a");
|
|
|
|
else
|
|
|
|
return Const("f")(mk_big(depth - 1), mk_big(depth - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst5() {
|
|
|
|
#ifndef __APPLE__
|
|
|
|
expr t = mk_big(18);
|
2013-09-04 15:30:04 +00:00
|
|
|
environment env = mk_toplevel();
|
2013-08-20 14:23:59 +00:00
|
|
|
env.add_var("f", Bool >> (Bool >> Bool));
|
|
|
|
env.add_var("a", Bool);
|
|
|
|
normalizer proc(env);
|
|
|
|
std::chrono::milliseconds dura(50);
|
|
|
|
std::thread thread([&]() {
|
|
|
|
try {
|
|
|
|
proc(t);
|
|
|
|
// Remark: if the following code is reached, we
|
|
|
|
// should decrease dura.
|
|
|
|
lean_unreachable();
|
|
|
|
} catch (interrupted & it) {
|
|
|
|
std::cout << "interrupted\n";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
std::this_thread::sleep_for(dura);
|
|
|
|
proc.interrupt();
|
|
|
|
thread.join();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-09-03 21:51:34 +00:00
|
|
|
static void tst6() {
|
2013-08-23 16:16:54 +00:00
|
|
|
environment env;
|
|
|
|
expr x = Const("x");
|
|
|
|
expr t = Fun({x, Type()}, mk_app(x, x));
|
|
|
|
expr omega = mk_app(t, t);
|
|
|
|
normalizer proc(env, 512);
|
|
|
|
try {
|
|
|
|
proc(omega);
|
|
|
|
} catch (kernel_exception & ex) {
|
|
|
|
std::cout << ex.what() << "\n";
|
|
|
|
}
|
|
|
|
}
|
2013-08-20 14:23:59 +00:00
|
|
|
|
2013-07-25 02:36:54 +00:00
|
|
|
int main() {
|
2013-07-26 02:13:45 +00:00
|
|
|
tst_church_numbers();
|
2013-07-30 07:25:19 +00:00
|
|
|
tst1();
|
|
|
|
tst2();
|
2013-08-04 16:37:52 +00:00
|
|
|
tst3();
|
|
|
|
tst4();
|
2013-08-20 14:23:59 +00:00
|
|
|
tst5();
|
2013-09-03 21:51:34 +00:00
|
|
|
tst6();
|
2013-07-25 02:36:54 +00:00
|
|
|
return has_violations() ? 1 : 0;
|
|
|
|
}
|