2013-09-13 01:25:38 +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-17 18:09:59 +00:00
|
|
|
#include <iostream>
|
2013-09-13 01:25:38 +00:00
|
|
|
#include <algorithm>
|
2013-09-16 02:37:59 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <utility>
|
2013-09-13 01:25:38 +00:00
|
|
|
#include "util/test.h"
|
|
|
|
#include "kernel/metavar.h"
|
|
|
|
#include "kernel/instantiate.h"
|
|
|
|
#include "kernel/abstract.h"
|
|
|
|
#include "kernel/free_vars.h"
|
2013-09-16 04:23:13 +00:00
|
|
|
#include "kernel/normalizer.h"
|
|
|
|
#include "kernel/environment.h"
|
2013-09-17 18:09:59 +00:00
|
|
|
#include "kernel/type_checker.h"
|
2013-09-13 01:25:38 +00:00
|
|
|
#include "library/printer.h"
|
2013-09-17 18:09:59 +00:00
|
|
|
#include "library/placeholder.h"
|
2013-09-30 15:33:30 +00:00
|
|
|
#include "library/arith/arith.h"
|
|
|
|
#include "library/all/all.h"
|
2013-09-13 01:25:38 +00:00
|
|
|
using namespace lean;
|
|
|
|
|
2013-09-27 01:24:45 +00:00
|
|
|
class unification_constraints_dbg : public unification_constraints {
|
2013-09-17 18:09:59 +00:00
|
|
|
typedef std::tuple<context, expr, expr> constraint;
|
|
|
|
typedef std::vector<constraint> constraints;
|
|
|
|
constraints m_eqs;
|
|
|
|
constraints m_type_of_eqs;
|
2013-09-16 02:37:59 +00:00
|
|
|
public:
|
2013-09-27 01:24:45 +00:00
|
|
|
unification_constraints_dbg() {}
|
|
|
|
virtual ~unification_constraints_dbg() {}
|
|
|
|
virtual void add(context const & ctx, expr const & lhs, expr const & rhs) { m_eqs.push_back(constraint(ctx, lhs, rhs)); }
|
|
|
|
virtual void add_type_of(context const & ctx, expr const & n, expr const & t) { m_type_of_eqs.push_back(constraint(ctx, n, t)); }
|
2013-09-17 18:09:59 +00:00
|
|
|
constraints const & eqs() const { return m_eqs; }
|
|
|
|
constraints const & type_of_eqs() const { return m_type_of_eqs; }
|
2013-09-27 01:24:45 +00:00
|
|
|
friend std::ostream & operator<<(std::ostream & out, unification_constraints_dbg const & uc) {
|
|
|
|
for (auto c : uc.m_eqs)
|
2013-09-17 18:09:59 +00:00
|
|
|
std::cout << std::get<0>(c) << " |- " << std::get<1>(c) << " == " << std::get<2>(c) << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
for (auto c : uc.m_type_of_eqs)
|
2013-09-23 02:12:19 +00:00
|
|
|
std::cout << std::get<0>(c) << " |- " << std::get<1>(c) << " : " << std::get<2>(c) << "\n";
|
2013-09-17 18:09:59 +00:00
|
|
|
return out;
|
|
|
|
}
|
2013-09-16 02:37:59 +00:00
|
|
|
};
|
|
|
|
|
2013-09-27 01:24:45 +00:00
|
|
|
std::ostream & operator<<(std::ostream & out, substitution const & env) {
|
2013-09-23 02:12:19 +00:00
|
|
|
bool first = true;
|
2013-09-27 01:24:45 +00:00
|
|
|
env.for_each([&](name const & n, expr const & v) {
|
2013-09-23 02:12:19 +00:00
|
|
|
if (first) first = false; else out << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
out << "?M" << n << " <- " << v;
|
|
|
|
});
|
2013-09-23 02:12:19 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2013-09-13 01:25:38 +00:00
|
|
|
static void tst1() {
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
|
|
|
lean_assert(!subst.is_assigned(m1));
|
|
|
|
expr t1 = metavar_type(m1);
|
2013-09-13 01:25:38 +00:00
|
|
|
lean_assert(is_metavar(t1));
|
2013-09-27 01:24:45 +00:00
|
|
|
lean_assert(is_eqp(metavar_type(m1), t1));
|
|
|
|
lean_assert(is_eqp(metavar_type(m1), t1));
|
|
|
|
lean_assert(!subst.is_assigned(m1));
|
|
|
|
expr m2 = gen.mk();
|
|
|
|
lean_assert(!subst.is_assigned(m1));
|
|
|
|
expr t2 = metavar_type(m2);
|
2013-09-13 01:25:38 +00:00
|
|
|
lean_assert(is_metavar(m2));
|
|
|
|
lean_assert(!is_eqp(t1, t2));
|
|
|
|
lean_assert(t1 != t2);
|
|
|
|
expr f = Const("f");
|
|
|
|
expr a = Const("a");
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, f(a));
|
|
|
|
lean_assert(subst.is_assigned(m1));
|
|
|
|
lean_assert(!subst.is_assigned(m2));
|
|
|
|
lean_assert(subst.get_subst(m1) == f(a));
|
2013-09-13 01:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst2() {
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
2013-09-13 01:25:38 +00:00
|
|
|
expr f = Const("f");
|
|
|
|
expr g = Const("g");
|
|
|
|
expr h = Const("h");
|
|
|
|
expr a = Const("a");
|
2013-09-27 01:24:45 +00:00
|
|
|
expr m1 = gen.mk();
|
|
|
|
expr m2 = gen.mk();
|
2013-09-13 01:25:38 +00:00
|
|
|
// move m1 to a different context, and store new metavariable + context in m11
|
2013-09-17 02:21:40 +00:00
|
|
|
std::cout << "---------------------\n";
|
|
|
|
expr m11 = add_inst(m1, 0, f(a, m2));
|
2013-09-13 01:25:38 +00:00
|
|
|
std::cout << m11 << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, f(Var(0)));
|
|
|
|
std::cout << instantiate_metavars(m11, subst) << "\n";
|
|
|
|
subst.assign(m2, g(a, Var(1)));
|
|
|
|
std::cout << instantiate_metavars(h(m11), subst) << "\n";
|
|
|
|
lean_assert(instantiate_metavars(h(m11), subst) == h(f(f(a, g(a, Var(1))))));
|
2013-09-13 01:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst3() {
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
2013-09-13 01:25:38 +00:00
|
|
|
expr f = Const("f");
|
|
|
|
expr g = Const("g");
|
|
|
|
expr h = Const("h");
|
|
|
|
expr a = Const("a");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr T = Const("T");
|
2013-09-27 01:24:45 +00:00
|
|
|
expr m1 = gen.mk();
|
2013-09-13 01:25:38 +00:00
|
|
|
expr F = Fun({x, T}, f(m1, x));
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, h(Var(0), Var(2)));
|
2013-09-13 01:25:38 +00:00
|
|
|
std::cout << instantiate(abst_body(F), g(a)) << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
std::cout << instantiate_metavars(instantiate(abst_body(F), g(a)), subst) << "\n";
|
|
|
|
lean_assert(instantiate_metavars(instantiate(abst_body(F), g(a)), subst) == f(h(g(a), Var(1)), g(a)));
|
|
|
|
std::cout << instantiate(instantiate_metavars(abst_body(F), subst), g(a)) << "\n";
|
|
|
|
lean_assert(instantiate(instantiate_metavars(abst_body(F), subst), g(a)) ==
|
|
|
|
instantiate_metavars(instantiate(abst_body(F), g(a)), subst));
|
2013-09-13 01:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst4() {
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
2013-09-13 01:25:38 +00:00
|
|
|
expr f = Const("f");
|
|
|
|
expr g = Const("g");
|
|
|
|
expr h = Const("h");
|
|
|
|
expr a = Const("a");
|
2013-09-27 01:24:45 +00:00
|
|
|
expr m1 = gen.mk();
|
2013-09-13 01:25:38 +00:00
|
|
|
expr F = f(m1, Var(2));
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, h(Var(1)));
|
2013-09-13 01:25:38 +00:00
|
|
|
std::cout << instantiate(F, {g(Var(0)), h(a)}) << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
std::cout << instantiate_metavars(instantiate(F, {g(Var(0)), h(a)}), subst) << "\n";
|
2013-09-13 01:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst5() {
|
2013-09-17 02:21:40 +00:00
|
|
|
return;
|
2013-09-13 01:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst6() {
|
|
|
|
expr N = Const("N");
|
|
|
|
expr f = Const("f");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr y = Const("y");
|
|
|
|
expr a = Const("a");
|
|
|
|
expr g = Const("g");
|
|
|
|
expr h = Const("h");
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
|
|
|
expr m2 = gen.mk();
|
2013-09-13 01:25:38 +00:00
|
|
|
expr t = f(Var(0), Fun({x, N}, f(Var(1), x, Fun({y, N}, f(Var(2), x, y)))));
|
|
|
|
expr r = instantiate(t, g(m1, m2));
|
|
|
|
std::cout << r << std::endl;
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m2, Var(2));
|
|
|
|
r = instantiate_metavars(r, subst);
|
2013-09-13 01:25:38 +00:00
|
|
|
std::cout << r << std::endl;
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, h(Var(3)));
|
|
|
|
r = instantiate_metavars(r, subst);
|
2013-09-13 01:25:38 +00:00
|
|
|
std::cout << r << std::endl;
|
|
|
|
lean_assert(r == f(g(h(Var(3)), Var(2)), Fun({x, N}, f(g(h(Var(4)), Var(3)), x, Fun({y, N}, f(g(h(Var(5)), Var(4)), x, y))))));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst7() {
|
|
|
|
expr f = Const("f");
|
|
|
|
expr g = Const("g");
|
|
|
|
expr a = Const("a");
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
2013-09-13 01:25:38 +00:00
|
|
|
expr t = f(m1, Var(0));
|
|
|
|
expr r = instantiate(t, a);
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, g(Var(0)));
|
|
|
|
r = instantiate_metavars(r, subst);
|
2013-09-13 01:25:38 +00:00
|
|
|
std::cout << r << std::endl;
|
|
|
|
lean_assert(r == f(g(a), a));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst8() {
|
|
|
|
expr f = Const("f");
|
|
|
|
expr g = Const("g");
|
|
|
|
expr a = Const("a");
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
2013-09-13 01:25:38 +00:00
|
|
|
expr t = f(m1, Var(0), Var(2));
|
|
|
|
expr r = instantiate(t, a);
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, g(Var(0), Var(1)));
|
|
|
|
r = instantiate_metavars(r, subst);
|
2013-09-13 01:25:38 +00:00
|
|
|
std::cout << r << std::endl;
|
|
|
|
lean_assert(r == f(g(a, Var(0)), a, Var(1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst9() {
|
|
|
|
expr f = Const("f");
|
|
|
|
expr g = Const("g");
|
|
|
|
expr a = Const("a");
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
2013-09-13 01:25:38 +00:00
|
|
|
expr t = f(m1, Var(1), Var(2));
|
|
|
|
expr r = lift_free_vars(t, 1, 2);
|
|
|
|
std::cout << r << std::endl;
|
|
|
|
r = instantiate(r, a);
|
|
|
|
std::cout << r << std::endl;
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, g(Var(0), Var(1)));
|
|
|
|
r = instantiate_metavars(r, subst);
|
2013-09-13 01:25:38 +00:00
|
|
|
std::cout << r << std::endl;
|
|
|
|
lean_assert(r == f(g(a, Var(2)), Var(2), Var(3)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst10() {
|
|
|
|
expr N = Const("N");
|
|
|
|
expr f = Const("f");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr y = Const("y");
|
|
|
|
expr a = Const("a");
|
|
|
|
expr g = Const("g");
|
|
|
|
expr h = Const("h");
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
|
|
|
expr m2 = gen.mk();
|
2013-09-13 01:25:38 +00:00
|
|
|
expr t = f(Var(0), Fun({x, N}, f(Var(1), Var(2), x, Fun({y, N}, f(Var(2), x, y)))));
|
|
|
|
expr r = instantiate(t, g(m1));
|
|
|
|
std::cout << r << std::endl;
|
|
|
|
r = instantiate(r, h(m2));
|
|
|
|
std::cout << r << std::endl;
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, f(Var(0)));
|
|
|
|
subst.assign(m2, Var(2));
|
|
|
|
r = instantiate_metavars(r, subst);
|
2013-09-13 01:25:38 +00:00
|
|
|
std::cout << r << std::endl;
|
|
|
|
lean_assert(r == f(g(f(h(Var(2)))), Fun({x, N}, f(g(f(h(Var(3)))), h(Var(3)), x, Fun({y, N}, f(g(f(h(Var(4)))), x, y))))));
|
|
|
|
}
|
|
|
|
|
2013-09-16 02:50:48 +00:00
|
|
|
static void tst11() {
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
unsigned t1 = subst.get_timestamp();
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m = gen.mk();
|
|
|
|
unsigned t2 = subst.get_timestamp();
|
|
|
|
lean_assert(t2 == t1);
|
|
|
|
lean_assert(!subst.is_assigned(m));
|
|
|
|
lean_assert(subst.get_timestamp() == t2);
|
|
|
|
subst.assign(m, Const("a"));
|
|
|
|
lean_assert(subst.get_timestamp() > t2);
|
2013-09-16 02:50:48 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 04:23:13 +00:00
|
|
|
static void tst12() {
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m = gen.mk();
|
2013-09-16 17:58:56 +00:00
|
|
|
expr f = Const("f");
|
|
|
|
std::cout << instantiate(f(m), {Var(0), Var(1)}) << "\n";
|
|
|
|
std::cout << instantiate(f(m), {Var(1), Var(0)}) << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst13() {
|
2013-09-16 04:23:13 +00:00
|
|
|
environment env;
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m = gen.mk();
|
2013-09-16 04:23:13 +00:00
|
|
|
env.add_var("N", Type());
|
|
|
|
expr N = Const("N");
|
|
|
|
env.add_var("f", N >> N);
|
|
|
|
expr f = Const("f");
|
|
|
|
env.add_var("a", N);
|
|
|
|
expr a = Const("a");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr F = Fun({x, N}, f(m))(a);
|
|
|
|
normalizer norm(env);
|
|
|
|
std::cout << norm(F) << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m, Var(0));
|
|
|
|
std::cout << norm(instantiate_metavars(F, subst)) << "\n";
|
|
|
|
lean_assert(norm(instantiate_metavars(F, subst)) ==
|
|
|
|
instantiate_metavars(norm(F), subst));
|
2013-09-16 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst14() {
|
|
|
|
environment env;
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
|
|
|
expr m2 = gen.mk();
|
2013-09-16 17:58:56 +00:00
|
|
|
expr N = Const("N");
|
|
|
|
expr f = Const("f");
|
|
|
|
expr h = Const("h");
|
|
|
|
expr a = Const("a");
|
|
|
|
expr b = Const("b");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr y = Const("y");
|
|
|
|
env.add_var("h", Pi({N, Type()}, N >> (N >> N)));
|
|
|
|
expr F1 = Fun({{N, Type()}, {a, N}, {f, N >> N}},
|
|
|
|
(Fun({{x, N}, {y, N}}, Eq(f(m1), y)))(a));
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst2 = subst;
|
|
|
|
subst2.assign(m1, h(Var(4), Var(1), Var(3)));
|
2013-09-16 17:58:56 +00:00
|
|
|
normalizer norm(env);
|
|
|
|
env.add_var("M", Type());
|
|
|
|
expr M = Const("M");
|
|
|
|
std::cout << norm(F1) << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
std::cout << instantiate_metavars(norm(F1), subst2) << "\n";
|
|
|
|
std::cout << instantiate_metavars(F1, subst2) << "\n";
|
|
|
|
std::cout << norm(instantiate_metavars(F1, subst2)) << "\n";
|
|
|
|
lean_assert(instantiate_metavars(norm(F1), subst2) ==
|
|
|
|
norm(instantiate_metavars(F1, subst2)));
|
2013-09-16 17:58:56 +00:00
|
|
|
expr F2 = (Fun({{N, Type()}, {f, N >> N}, {a, N}, {b, N}},
|
|
|
|
(Fun({{x, N}, {y, N}}, Eq(f(m1), y)))(a, m2)))(M);
|
|
|
|
std::cout << norm(F2) << "\n";
|
|
|
|
expr F3 = (Fun({{N, Type()}, {f, N >> N}, {a, N}, {b, N}},
|
|
|
|
(Fun({{x, N}, {y, N}}, Eq(f(m1), y)))(b, m2)))(M);
|
|
|
|
std::cout << norm(F3) << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst15() {
|
|
|
|
environment env;
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
2013-09-16 17:58:56 +00:00
|
|
|
normalizer norm(env);
|
2013-09-27 01:24:45 +00:00
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
2013-09-16 17:58:56 +00:00
|
|
|
expr f = Const("f");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr y = Const("y");
|
|
|
|
expr z = Const("z");
|
|
|
|
expr N = Const("N");
|
|
|
|
env.add_var("N", Type());
|
|
|
|
env.add_var("f", Type() >> Type());
|
|
|
|
expr F = Fun({z, Type()}, Fun({{x, Type()}, {y, Type()}}, f(m1))(N, N));
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, Var(2));
|
2013-09-16 17:58:56 +00:00
|
|
|
std::cout << norm(F) << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
std::cout << instantiate_metavars(norm(F), subst) << "\n";
|
|
|
|
std::cout << norm(instantiate_metavars(F, subst)) << "\n";
|
|
|
|
lean_assert(instantiate_metavars(norm(F), subst) ==
|
|
|
|
norm(instantiate_metavars(F, subst)));
|
2013-09-16 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst16() {
|
|
|
|
environment env;
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
2013-09-16 17:58:56 +00:00
|
|
|
normalizer norm(env);
|
|
|
|
context ctx;
|
|
|
|
ctx = extend(ctx, "w", Type());
|
2013-09-27 01:24:45 +00:00
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
2013-09-16 17:58:56 +00:00
|
|
|
expr f = Const("f");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr y = Const("y");
|
|
|
|
expr z = Const("z");
|
|
|
|
expr N = Const("N");
|
|
|
|
env.add_var("N", Type());
|
|
|
|
expr F = Fun({z, Type()}, Fun({{x, Type()}, {y, Type()}}, m1)(N, N));
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, Var(3));
|
2013-09-16 17:58:56 +00:00
|
|
|
std::cout << norm(F, ctx) << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
std::cout << instantiate_metavars(norm(F, ctx), subst) << "\n";
|
|
|
|
std::cout << norm(instantiate_metavars(F, subst), ctx) << "\n";
|
2013-09-16 17:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst17() {
|
|
|
|
environment env;
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
2013-09-16 17:58:56 +00:00
|
|
|
normalizer norm(env);
|
|
|
|
context ctx;
|
|
|
|
ctx = extend(ctx, "w1", Type());
|
|
|
|
ctx = extend(ctx, "w2", Type());
|
|
|
|
ctx = extend(ctx, "w3", Type());
|
|
|
|
ctx = extend(ctx, "w4", Type());
|
2013-09-27 01:24:45 +00:00
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
2013-09-16 17:58:56 +00:00
|
|
|
expr f = Const("f");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr y = Const("y");
|
|
|
|
expr z = Const("z");
|
|
|
|
expr N = Const("N");
|
|
|
|
env.add_var("N", Type());
|
|
|
|
expr F = Fun({z, Type()}, Fun({{x, Type()}, {y, Type()}}, m1)(N, N));
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst2 = subst;
|
|
|
|
subst.assign(m1, Var(3));
|
2013-09-16 17:58:56 +00:00
|
|
|
std::cout << norm(F, ctx) << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
std::cout << instantiate_metavars(norm(F, ctx), subst) << "\n";
|
|
|
|
std::cout << norm(instantiate_metavars(F, subst), ctx) << "\n";
|
2013-09-16 17:58:56 +00:00
|
|
|
F = Fun({z, Type()}, Fun({{x, Type()}, {y, Type()}, {x, Type()}, {y, Type()}, {x, Type()}}, m1)(N, N, N, N, N));
|
2013-09-27 01:24:45 +00:00
|
|
|
lean_assert(instantiate_metavars(norm(F, ctx), subst) ==
|
|
|
|
norm(instantiate_metavars(F, subst), ctx));
|
2013-09-16 17:58:56 +00:00
|
|
|
std::cout << "----------------------\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
subst2.assign(m1, Var(8));
|
2013-09-16 17:58:56 +00:00
|
|
|
std::cout << norm(F, ctx) << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
std::cout << instantiate_metavars(norm(F, ctx), subst2) << "\n";
|
|
|
|
std::cout << norm(instantiate_metavars(F, subst2), ctx) << "\n";
|
|
|
|
lean_assert(instantiate_metavars(norm(F, ctx), subst2) ==
|
|
|
|
norm(instantiate_metavars(F, subst2), ctx));
|
2013-09-16 04:23:13 +00:00
|
|
|
}
|
2013-09-17 08:57:21 +00:00
|
|
|
|
|
|
|
static void tst18() {
|
|
|
|
environment env;
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
2013-09-17 08:57:21 +00:00
|
|
|
normalizer norm(env);
|
|
|
|
context ctx;
|
|
|
|
ctx = extend(ctx, "w1", Type());
|
|
|
|
ctx = extend(ctx, "w2", Type());
|
2013-09-27 01:24:45 +00:00
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
|
|
|
expr m2 = gen.mk();
|
2013-09-17 08:57:21 +00:00
|
|
|
expr f = Const("f");
|
|
|
|
expr g = Const("g");
|
|
|
|
expr h = Const("h");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr y = Const("y");
|
|
|
|
expr z = Const("z");
|
|
|
|
expr N = Const("N");
|
|
|
|
expr a = Const("a");
|
|
|
|
env.add_var("N", Type());
|
|
|
|
env.add_var("a", N);
|
|
|
|
env.add_var("g", N >> N);
|
|
|
|
env.add_var("h", N >> (N >> N));
|
|
|
|
expr F = Fun({z, Type()}, Fun({{f, N >> N}, {y, Type()}}, m1)(Fun({x, N}, g(z, x, m2)), N));
|
|
|
|
std::cout << norm(F, ctx) << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst2 = subst;
|
|
|
|
subst2.assign(m1, Var(1));
|
|
|
|
subst2.assign(m2, h(Var(2), Var(1)));
|
|
|
|
std::cout << instantiate_metavars(norm(F, ctx), subst2) << "\n";
|
|
|
|
std::cout << instantiate_metavars(F, subst2) << "\n";
|
|
|
|
lean_assert(instantiate_metavars(norm(F, ctx), subst2) ==
|
|
|
|
norm(instantiate_metavars(F, subst2), ctx));
|
|
|
|
lean_assert(instantiate_metavars(norm(F, ctx), subst2) ==
|
2013-09-17 08:57:21 +00:00
|
|
|
Fun({{z, Type()}, {x, N}}, g(z, x, h(Var(2), z))));
|
|
|
|
}
|
2013-09-16 04:23:13 +00:00
|
|
|
|
2013-09-17 09:09:45 +00:00
|
|
|
static void tst19() {
|
|
|
|
environment env;
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
2013-09-17 09:09:45 +00:00
|
|
|
normalizer norm(env);
|
|
|
|
context ctx;
|
|
|
|
ctx = extend(ctx, "w1", Type());
|
|
|
|
ctx = extend(ctx, "w2", Type());
|
2013-09-27 01:24:45 +00:00
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
2013-09-17 09:09:45 +00:00
|
|
|
expr x = Const("x");
|
|
|
|
expr y = Const("y");
|
|
|
|
expr N = Const("N");
|
|
|
|
expr F = Fun({{N, Type()}, {x, N}, {y, N}}, m1);
|
|
|
|
std::cout << norm(F) << "\n";
|
|
|
|
std::cout << norm(F, ctx) << "\n";
|
2013-09-17 09:57:28 +00:00
|
|
|
lean_assert(norm(F) == F);
|
|
|
|
lean_assert(norm(F, ctx) == F);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst20() {
|
|
|
|
environment env;
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
2013-09-17 09:57:28 +00:00
|
|
|
normalizer norm(env);
|
|
|
|
context ctx;
|
|
|
|
ctx = extend(ctx, "w1", Type());
|
|
|
|
ctx = extend(ctx, "w2", Type());
|
2013-09-27 01:24:45 +00:00
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
2013-09-17 09:57:28 +00:00
|
|
|
expr x = Const("x");
|
|
|
|
expr y = Const("y");
|
|
|
|
expr z = Const("z");
|
|
|
|
expr N = Const("N");
|
|
|
|
expr a = Const("a");
|
|
|
|
expr b = Const("b");
|
|
|
|
env.add_var("N", Type());
|
|
|
|
env.add_var("a", N);
|
|
|
|
env.add_var("b", N);
|
|
|
|
expr F = Fun({{x, N}, {y, N}, {z, N}}, Fun({{x, N}, {y, N}}, m1)(a, b));
|
|
|
|
std::cout << norm(F) << "\n";
|
|
|
|
std::cout << norm(F, ctx) << "\n";
|
2013-09-17 09:09:45 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 10:14:02 +00:00
|
|
|
static void tst21() {
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
|
|
|
expr l = add_lift(add_lift(m1, 0, 1), 1, 1);
|
|
|
|
expr r = add_lift(m1, 0, 2);
|
|
|
|
std::cout << metavar_type(l) << " " << metavar_type(r) << "\n";
|
|
|
|
lean_assert_eq(l, r);
|
|
|
|
lean_assert_eq(add_lift(add_lift(m1, 1, 2), 3, 4),
|
|
|
|
add_lift(m1, 1, 6));
|
|
|
|
lean_assert_ne(add_lift(add_lift(m1, 1, 3), 3, 4),
|
|
|
|
add_lift(m1, 1, 7));
|
2013-09-17 10:14:02 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 18:09:59 +00:00
|
|
|
#define _ mk_placholder()
|
|
|
|
|
|
|
|
static void tst22() {
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator mgen;
|
2013-09-17 18:09:59 +00:00
|
|
|
expr f = Const("f");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr N = Const("N");
|
|
|
|
expr F = f(Fun({x, N}, f(_, x)), _);
|
|
|
|
std::cout << F << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
std::cout << replace_placeholders_with_metavars(F, mgen) << "\n";
|
2013-09-17 18:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tst23() {
|
|
|
|
environment env;
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
unification_constraints_dbg up;
|
|
|
|
metavar_generator mgen;
|
2013-09-17 18:09:59 +00:00
|
|
|
type_checker checker(env);
|
|
|
|
expr N = Const("N");
|
|
|
|
expr f = Const("f");
|
|
|
|
expr a = Const("a");
|
|
|
|
env.add_var("N", Type());
|
|
|
|
env.add_var("f", N >> (N >> N));
|
|
|
|
env.add_var("a", N);
|
|
|
|
expr x = Const("x");
|
|
|
|
expr F0 = f(Fun({x, N}, f(_, x))(a), _);
|
2013-09-27 01:24:45 +00:00
|
|
|
expr F1 = replace_placeholders_with_metavars(F0, mgen);
|
2013-09-17 18:09:59 +00:00
|
|
|
std::cout << F1 << "\n";
|
2013-09-27 01:24:45 +00:00
|
|
|
std::cout << checker.infer_type(F1, context(), &subst, &mgen, &up) << "\n";
|
2013-09-17 18:09:59 +00:00
|
|
|
std::cout << up << "\n";
|
|
|
|
}
|
|
|
|
|
2013-09-19 03:46:00 +00:00
|
|
|
static void tst24() {
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
metavar_generator gen;
|
|
|
|
expr m1 = gen.mk();
|
|
|
|
expr m2 = gen.mk();
|
2013-09-19 03:46:00 +00:00
|
|
|
expr f = Const("f");
|
|
|
|
expr a = Const("a");
|
2013-09-27 01:24:45 +00:00
|
|
|
subst.assign(m1, f(m2));
|
|
|
|
subst.assign(m2, a);
|
|
|
|
lean_assert(instantiate_metavars(f(m1), subst) == f(f(a)));
|
|
|
|
std::cout << instantiate_metavars(f(m1), subst) << "\n";
|
2013-09-19 03:46:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-23 02:12:19 +00:00
|
|
|
static void tst25() {
|
|
|
|
environment env;
|
2013-09-27 01:24:45 +00:00
|
|
|
substitution subst;
|
|
|
|
unification_constraints_dbg up;
|
|
|
|
metavar_generator gen;
|
2013-09-23 02:12:19 +00:00
|
|
|
type_checker checker(env);
|
|
|
|
expr N = Const("N");
|
|
|
|
expr a = Const("a");
|
|
|
|
expr b = Const("b");
|
|
|
|
env.add_var("N", Type());
|
|
|
|
env.add_var("a", N);
|
|
|
|
env.add_var("b", N);
|
2013-09-27 01:24:45 +00:00
|
|
|
expr m = gen.mk();
|
2013-09-23 02:12:19 +00:00
|
|
|
expr F = m(a, b);
|
2013-09-27 01:24:45 +00:00
|
|
|
std::cout << checker.infer_type(F, context(), &subst, &gen, &up) << "\n";
|
|
|
|
std::cout << subst << "\n";
|
2013-09-23 02:12:19 +00:00
|
|
|
std::cout << up << "\n";
|
|
|
|
}
|
|
|
|
|
2013-09-30 15:33:30 +00:00
|
|
|
static void tst26() {
|
|
|
|
/*
|
|
|
|
Encoding the following problem
|
|
|
|
|
|
|
|
Variable list : Type -> Type
|
|
|
|
Variable nil {A : Type} : list A
|
|
|
|
Variable cons {A : Type} (head : A) (tail : list A) : list A
|
|
|
|
Variables a b : Int
|
|
|
|
Variables n m : Nat
|
|
|
|
Definition l2 : list Int := cons a (cons n (cons b nil))
|
|
|
|
*/
|
|
|
|
std::cout << "\ntst26\n";
|
|
|
|
environment env;
|
|
|
|
import_all(env);
|
|
|
|
substitution subst;
|
|
|
|
unification_constraints_dbg up;
|
|
|
|
metavar_generator gen;
|
|
|
|
type_checker checker(env);
|
|
|
|
expr list = Const("list");
|
|
|
|
expr nil = Const("nil");
|
|
|
|
expr cons = Const("cons");
|
|
|
|
expr A = Const("A");
|
|
|
|
env.add_var("list", Type() >> Type());
|
|
|
|
env.add_var("nil", Pi({A, Type()}, list(A)));
|
|
|
|
env.add_var("cons", Pi({A, Type()}, A >> (list(A) >> list(A))));
|
|
|
|
env.add_var("a", Int);
|
|
|
|
env.add_var("b", Int);
|
|
|
|
env.add_var("n", Nat);
|
|
|
|
env.add_var("m", Nat);
|
|
|
|
expr a = Const("a");
|
|
|
|
expr b = Const("b");
|
|
|
|
expr n = Const("n");
|
|
|
|
expr m = Const("m");
|
|
|
|
expr m1 = gen.mk();
|
|
|
|
expr m2 = gen.mk();
|
|
|
|
expr m3 = gen.mk();
|
|
|
|
expr A1 = gen.mk();
|
|
|
|
expr A2 = gen.mk();
|
|
|
|
expr A3 = gen.mk();
|
|
|
|
expr A4 = gen.mk();
|
|
|
|
expr F = cons(A1, m1(a), cons(A2, m2(n), cons(A3, m3(b), nil(A4))));
|
|
|
|
std::cout << F << "\n";
|
|
|
|
std::cout << checker.infer_type(F, context(), &subst, &gen, &up) << "\n";
|
|
|
|
std::cout << subst << "\n";
|
|
|
|
std::cout << up << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tst27() {
|
|
|
|
/*
|
|
|
|
Variable f {A : Type} (a b : A) : Bool
|
|
|
|
Variable a : Int
|
|
|
|
Variable b : Real
|
|
|
|
Definition tst : Bool := (fun x y, f x y) a b
|
|
|
|
*/
|
|
|
|
std::cout << "\ntst27\n";
|
|
|
|
environment env;
|
|
|
|
import_all(env);
|
|
|
|
substitution subst;
|
|
|
|
unification_constraints_dbg up;
|
|
|
|
metavar_generator gen;
|
|
|
|
type_checker checker(env);
|
|
|
|
expr A = Const("A");
|
|
|
|
expr f = Const("f");
|
|
|
|
expr a = Const("a");
|
|
|
|
expr b = Const("b");
|
|
|
|
expr x = Const("x");
|
|
|
|
expr y = Const("y");
|
|
|
|
env.add_var("f", Pi({A, Type()}, A >> (A >> Bool)));
|
|
|
|
env.add_var("a", Int);
|
|
|
|
env.add_var("b", Real);
|
|
|
|
expr T1 = gen.mk();
|
|
|
|
expr T2 = gen.mk();
|
|
|
|
expr A1 = gen.mk();
|
|
|
|
expr m1 = gen.mk();
|
|
|
|
expr m2 = gen.mk();
|
|
|
|
expr F = Fun({{x, T1}, {y, T2}}, f(A1, x, y))(m1(a), m2(b));
|
|
|
|
std::cout << F << "\n";
|
|
|
|
std::cout << checker.infer_type(F, context(), &subst, &gen, &up) << "\n";
|
|
|
|
std::cout << subst << "\n";
|
|
|
|
std::cout << up << "\n";
|
|
|
|
}
|
|
|
|
|
2013-09-13 01:25:38 +00:00
|
|
|
int main() {
|
|
|
|
tst1();
|
|
|
|
tst2();
|
|
|
|
tst3();
|
|
|
|
tst4();
|
|
|
|
tst5();
|
|
|
|
tst6();
|
|
|
|
tst7();
|
|
|
|
tst8();
|
|
|
|
tst9();
|
|
|
|
tst10();
|
2013-09-16 02:50:48 +00:00
|
|
|
tst11();
|
2013-09-16 04:23:13 +00:00
|
|
|
tst12();
|
2013-09-17 08:57:21 +00:00
|
|
|
tst13();
|
|
|
|
tst14();
|
|
|
|
tst15();
|
|
|
|
tst16();
|
|
|
|
tst17();
|
|
|
|
tst18();
|
2013-09-17 09:09:45 +00:00
|
|
|
tst19();
|
2013-09-17 09:57:28 +00:00
|
|
|
tst20();
|
2013-09-17 10:14:02 +00:00
|
|
|
tst21();
|
2013-09-17 18:09:59 +00:00
|
|
|
tst22();
|
|
|
|
tst23();
|
2013-09-19 03:46:00 +00:00
|
|
|
tst24();
|
2013-09-23 02:12:19 +00:00
|
|
|
tst25();
|
2013-09-30 15:33:30 +00:00
|
|
|
tst26();
|
|
|
|
tst27();
|
2013-09-13 01:25:38 +00:00
|
|
|
return has_violations() ? 1 : 0;
|
|
|
|
}
|