2013-07-22 20:04:27 +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-23 18:31:31 +00:00
|
|
|
#include <algorithm>
|
2013-07-22 20:04:27 +00:00
|
|
|
#include "expr.h"
|
2013-07-23 17:09:04 +00:00
|
|
|
#include "sets.h"
|
2013-07-23 16:01:54 +00:00
|
|
|
#include "max_sharing.h"
|
2013-07-23 18:31:31 +00:00
|
|
|
#include "free_vars.h"
|
2013-07-22 20:04:27 +00:00
|
|
|
#include "test.h"
|
|
|
|
using namespace lean;
|
|
|
|
|
2013-07-23 02:02:11 +00:00
|
|
|
void tst1() {
|
2013-07-22 20:04:27 +00:00
|
|
|
expr a;
|
|
|
|
a = numeral(mpz(10));
|
|
|
|
expr f;
|
|
|
|
f = var(0);
|
2013-07-23 18:56:15 +00:00
|
|
|
expr fa = f(a);
|
2013-07-22 20:04:27 +00:00
|
|
|
std::cout << fa << "\n";
|
2013-07-23 18:56:15 +00:00
|
|
|
std::cout << fa(a) << "\n";
|
2013-07-23 18:47:36 +00:00
|
|
|
lean_assert(eqp(arg(fa, 0), f));
|
|
|
|
lean_assert(eqp(arg(fa, 1), a));
|
2013-07-23 18:56:15 +00:00
|
|
|
lean_assert(!eqp(fa, f(a)));
|
|
|
|
lean_assert(app(fa, a) == f(a, a));
|
|
|
|
std::cout << fa(fa, fa) << "\n";
|
2013-07-23 16:37:32 +00:00
|
|
|
std::cout << lambda("x", prop(), var(0)) << "\n";
|
2013-07-23 18:56:15 +00:00
|
|
|
lean_assert(f(a)(a) == f(a, a));
|
|
|
|
lean_assert(f(a(a)) != f(a, a));
|
2013-07-24 02:27:13 +00:00
|
|
|
lean_assert(lambda("x", prop(), var(0)) == lambda("y", prop(), var(0)));
|
|
|
|
std::cout << pi("x", prop(), var(0)) << "\n";
|
2013-07-22 20:04:27 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 18:31:31 +00:00
|
|
|
expr mk_dag(unsigned depth, bool _closed = false) {
|
2013-07-23 16:37:32 +00:00
|
|
|
expr f = constant("f");
|
2013-07-23 18:31:31 +00:00
|
|
|
expr a = _closed ? constant("a") : var(0);
|
2013-07-22 20:04:27 +00:00
|
|
|
while (depth > 0) {
|
|
|
|
depth--;
|
2013-07-23 18:56:15 +00:00
|
|
|
a = f(a, a);
|
2013-07-22 20:04:27 +00:00
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-07-22 23:40:17 +00:00
|
|
|
unsigned depth1(expr const & e) {
|
|
|
|
switch (e.kind()) {
|
|
|
|
case expr_kind::Var: case expr_kind::Constant: case expr_kind::Prop: case expr_kind::Type: case expr_kind::Numeral:
|
|
|
|
return 1;
|
|
|
|
case expr_kind::App: {
|
|
|
|
unsigned m = 0;
|
2013-07-23 18:47:36 +00:00
|
|
|
for (expr const & a : args(e))
|
2013-07-22 23:40:17 +00:00
|
|
|
m = std::max(m, depth1(a));
|
|
|
|
return m + 1;
|
|
|
|
}
|
|
|
|
case expr_kind::Lambda: case expr_kind::Pi:
|
2013-07-23 18:47:36 +00:00
|
|
|
return std::max(depth1(abst_type(e)), depth1(abst_expr(e))) + 1;
|
2013-07-22 23:40:17 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the fastest depth implementation in this file.
|
|
|
|
unsigned depth2(expr const & e) {
|
|
|
|
switch (e.kind()) {
|
|
|
|
case expr_kind::Var: case expr_kind::Constant: case expr_kind::Prop: case expr_kind::Type: case expr_kind::Numeral:
|
|
|
|
return 1;
|
|
|
|
case expr_kind::App:
|
|
|
|
return
|
|
|
|
std::accumulate(begin_args(e), end_args(e), 0,
|
|
|
|
[](unsigned m, expr const & arg){ return std::max(depth2(arg), m); })
|
|
|
|
+ 1;
|
|
|
|
case expr_kind::Lambda: case expr_kind::Pi:
|
2013-07-23 18:47:36 +00:00
|
|
|
return std::max(depth2(abst_type(e)), depth2(abst_expr(e))) + 1;
|
2013-07-22 23:40:17 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the slowest depth implementation in this file.
|
|
|
|
unsigned depth3(expr const & e) {
|
|
|
|
static std::vector<std::pair<expr const *, unsigned>> todo;
|
|
|
|
unsigned m = 0;
|
|
|
|
todo.push_back(std::make_pair(&e, 0));
|
|
|
|
while (!todo.empty()) {
|
|
|
|
auto const & p = todo.back();
|
|
|
|
expr const & e = *(p.first);
|
|
|
|
unsigned c = p.second + 1;
|
|
|
|
todo.pop_back();
|
|
|
|
switch (e.kind()) {
|
|
|
|
case expr_kind::Var: case expr_kind::Constant: case expr_kind::Prop: case expr_kind::Type: case expr_kind::Numeral:
|
|
|
|
m = std::max(c, m);
|
|
|
|
break;
|
|
|
|
case expr_kind::App: {
|
2013-07-23 18:47:36 +00:00
|
|
|
unsigned num = num_args(e);
|
2013-07-22 23:40:17 +00:00
|
|
|
for (unsigned i = 0; i < num; i++)
|
2013-07-23 18:47:36 +00:00
|
|
|
todo.push_back(std::make_pair(&arg(e, i), c));
|
2013-07-22 23:40:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case expr_kind::Lambda: case expr_kind::Pi:
|
2013-07-23 18:47:36 +00:00
|
|
|
todo.push_back(std::make_pair(&abst_type(e), c));
|
|
|
|
todo.push_back(std::make_pair(&abst_expr(e), c));
|
2013-07-22 23:40:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2013-07-23 02:02:11 +00:00
|
|
|
void tst2() {
|
2013-07-22 23:40:17 +00:00
|
|
|
expr r1 = mk_dag(20);
|
|
|
|
expr r2 = mk_dag(20);
|
|
|
|
lean_verify(r1 == r2);
|
|
|
|
std::cout << depth2(r1) << "\n";
|
|
|
|
lean_verify(depth2(r1) == 21);
|
|
|
|
}
|
|
|
|
|
|
|
|
expr mk_big(expr f, unsigned depth, unsigned val) {
|
|
|
|
if (depth == 1)
|
2013-07-23 18:31:31 +00:00
|
|
|
return constant(name(val));
|
2013-07-22 23:40:17 +00:00
|
|
|
else
|
2013-07-23 18:56:15 +00:00
|
|
|
return f(mk_big(f, depth - 1, val << 1), mk_big(f, depth - 1, (val << 1) + 1));
|
2013-07-22 23:40:17 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 02:02:11 +00:00
|
|
|
void tst3() {
|
2013-07-23 16:37:32 +00:00
|
|
|
expr f = constant("f");
|
2013-07-22 23:40:17 +00:00
|
|
|
expr r1 = mk_big(f, 18, 0);
|
|
|
|
expr r2 = mk_big(f, 18, 0);
|
2013-07-22 20:04:27 +00:00
|
|
|
lean_verify(r1 == r2);
|
|
|
|
}
|
|
|
|
|
2013-07-23 02:02:11 +00:00
|
|
|
void tst4() {
|
2013-07-23 16:37:32 +00:00
|
|
|
expr f = constant("f");
|
2013-07-23 00:58:28 +00:00
|
|
|
expr a = var(0);
|
|
|
|
for (unsigned i = 0; i < 10000; i++) {
|
2013-07-23 18:56:15 +00:00
|
|
|
a = f(a);
|
2013-07-23 00:58:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 02:02:11 +00:00
|
|
|
expr mk_redundant_dag(expr f, unsigned depth) {
|
|
|
|
if (depth == 0)
|
|
|
|
return var(0);
|
|
|
|
else
|
2013-07-23 18:56:15 +00:00
|
|
|
return f(mk_redundant_dag(f, depth - 1), mk_redundant_dag(f, depth - 1));
|
2013-07-23 02:02:11 +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()) {
|
|
|
|
case expr_kind::Var: case expr_kind::Constant: case expr_kind::Prop: case expr_kind::Type: case expr_kind::Numeral:
|
|
|
|
return 1;
|
|
|
|
case expr_kind::App:
|
|
|
|
return std::accumulate(begin_args(a), end_args(a), 1,
|
|
|
|
[&](unsigned sum, expr const & arg){ return sum + count_core(arg, s); });
|
|
|
|
case expr_kind::Lambda: case expr_kind::Pi:
|
2013-07-23 18:47:36 +00:00
|
|
|
return count_core(abst_type(a), s) + count_core(abst_expr(a), s) + 1;
|
2013-07-23 02:02:11 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned count(expr const & a) {
|
|
|
|
expr_set s;
|
|
|
|
return count_core(a, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst5() {
|
2013-07-23 16:37:32 +00:00
|
|
|
expr f = constant("f");
|
2013-07-23 02:31:27 +00:00
|
|
|
{
|
|
|
|
expr r1 = mk_redundant_dag(f, 5);
|
2013-07-23 15:59:39 +00:00
|
|
|
expr r2 = max_sharing(r1);
|
2013-07-23 02:31:27 +00:00
|
|
|
std::cout << "count(r1): " << count(r1) << "\n";
|
|
|
|
std::cout << "count(r2): " << count(r2) << "\n";
|
|
|
|
lean_assert(r1 == r2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
expr r1 = mk_redundant_dag(f, 16);
|
2013-07-23 15:59:39 +00:00
|
|
|
expr r2 = max_sharing(r1);
|
2013-07-23 02:31:27 +00:00
|
|
|
lean_assert(r1 == r2);
|
|
|
|
}
|
2013-07-23 02:02:11 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 15:59:39 +00:00
|
|
|
void tst6() {
|
2013-07-23 16:37:32 +00:00
|
|
|
expr f = constant("f");
|
2013-07-23 15:59:39 +00:00
|
|
|
expr r = mk_redundant_dag(f, 12);
|
|
|
|
for (unsigned i = 0; i < 1000; i++) {
|
|
|
|
r = max_sharing(r);
|
|
|
|
}
|
|
|
|
r = mk_big(f, 16, 0);
|
|
|
|
for (unsigned i = 0; i < 1000000; i++) {
|
|
|
|
r = max_sharing(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 16:18:18 +00:00
|
|
|
void tst7() {
|
2013-07-23 16:37:32 +00:00
|
|
|
expr f = constant("f");
|
2013-07-23 16:18:18 +00:00
|
|
|
expr v = var(0);
|
2013-07-23 18:56:15 +00:00
|
|
|
expr a1 = max_sharing(f(v,v));
|
|
|
|
expr a2 = max_sharing(f(v,v));
|
2013-07-23 16:18:18 +00:00
|
|
|
lean_assert(!eqp(a1, a2));
|
2013-07-23 18:56:15 +00:00
|
|
|
expr b = max_sharing(f(a1, a2));
|
2013-07-23 18:47:36 +00:00
|
|
|
lean_assert(eqp(arg(b, 1), arg(b, 2)));
|
2013-07-23 16:18:18 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 18:31:31 +00:00
|
|
|
void tst8() {
|
|
|
|
expr f = constant("f");
|
|
|
|
expr x = var(0);
|
|
|
|
expr a = constant("a");
|
|
|
|
expr n = numeral(mpz(10));
|
|
|
|
expr p = prop();
|
|
|
|
expr y = var(1);
|
|
|
|
lean_assert(closed(a));
|
|
|
|
lean_assert(!closed(x));
|
|
|
|
lean_assert(closed(f));
|
2013-07-23 18:56:15 +00:00
|
|
|
lean_assert(!closed(f(x)));
|
2013-07-23 18:31:31 +00:00
|
|
|
lean_assert(closed(lambda("x", p, x)));
|
|
|
|
lean_assert(!closed(lambda("x", x, x)));
|
|
|
|
lean_assert(!closed(lambda("x", p, y)));
|
2013-07-23 18:56:15 +00:00
|
|
|
lean_assert(closed(f(f(f(a)))));
|
|
|
|
lean_assert(closed(lambda("x", p, f(f(f(a))))));
|
2013-07-23 18:31:31 +00:00
|
|
|
lean_assert(closed(pi("x", p, x)));
|
|
|
|
lean_assert(!closed(pi("x", x, x)));
|
|
|
|
lean_assert(!closed(pi("x", p, y)));
|
2013-07-23 18:56:15 +00:00
|
|
|
lean_assert(closed(pi("x", p, f(f(f(a))))));
|
2013-07-23 18:31:31 +00:00
|
|
|
lean_assert(closed(lambda("y", p, lambda("x", p, y))));
|
|
|
|
lean_assert(closed(lambda("y", p, app(lambda("x", p, y), var(0)))));
|
|
|
|
expr r = lambda("y", p, app(lambda("x", p, y), var(0)));
|
|
|
|
lean_assert(closed(r));
|
|
|
|
lean_assert(closed(r));
|
|
|
|
r = lambda("y", p, app(lambda("x", p, y), var(1)));
|
|
|
|
lean_assert(!closed(r));
|
|
|
|
r = lambda("y", p, app(lambda("x", p, var(0)), var(1)));
|
|
|
|
lean_assert(!closed(r));
|
|
|
|
lean_assert(closed(lambda("z", p, r)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst9() {
|
|
|
|
expr r = mk_dag(20, true);
|
|
|
|
lean_assert(closed(r));
|
|
|
|
r = mk_dag(20, false);
|
|
|
|
lean_assert(!closed(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst10() {
|
|
|
|
expr f = constant("f");
|
|
|
|
expr r = mk_big(f, 16, 0);
|
|
|
|
for (unsigned i = 0; i < 1000; i++) {
|
|
|
|
lean_assert(closed(r));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-24 02:27:13 +00:00
|
|
|
void tst11() {
|
|
|
|
expr f = constant("f");
|
|
|
|
expr a = constant("a");
|
|
|
|
expr y = var(0);
|
|
|
|
expr x = var(0);
|
|
|
|
lean_assert(closed(lambda("bla", prop(), lambda("x", prop(), f(f(a, a, x), y)))));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-22 20:04:27 +00:00
|
|
|
int main() {
|
2013-07-23 15:59:39 +00:00
|
|
|
continue_on_violation(true);
|
2013-07-22 20:04:27 +00:00
|
|
|
std::cout << "sizeof(expr): " << sizeof(expr) << "\n";
|
|
|
|
std::cout << "sizeof(expr_app): " << sizeof(expr_app) << "\n";
|
2013-07-23 02:31:27 +00:00
|
|
|
tst1();
|
|
|
|
tst2();
|
|
|
|
tst3();
|
|
|
|
tst4();
|
2013-07-23 02:02:11 +00:00
|
|
|
tst5();
|
2013-07-23 15:59:39 +00:00
|
|
|
tst6();
|
2013-07-23 16:18:18 +00:00
|
|
|
tst7();
|
2013-07-23 18:47:36 +00:00
|
|
|
tst8();
|
|
|
|
tst9();
|
|
|
|
tst10();
|
2013-07-24 02:27:13 +00:00
|
|
|
tst11();
|
2013-07-23 00:58:28 +00:00
|
|
|
std::cout << "done" << "\n";
|
2013-07-22 20:04:27 +00:00
|
|
|
return has_violations() ? 1 : 0;
|
|
|
|
}
|