void test_import() { lean_exception ex; lean_env env = mk_env(); lean_name std = mk_name("standard"); lean_list_name ms = mk_unary_name_list(std); lean_options o; lean_ios ios; lean_env new_env; lean_decl nat_gcd_decl; lean_name nat = mk_name("nat"); lean_name nat_gcd; lean_expr nat_gcd_val; char const * s; check(lean_options_mk_empty(&o, &ex)); check(lean_ios_mk_std(o, &ios, &ex)); check(lean_env_import(env, ios, ms, &new_env, &ex)); check(lean_env_for_each_decl(new_env, print_decl_name_and_del, &ex)); check(lean_name_mk_str(nat, "gcd", &nat_gcd, &ex)); check(lean_env_get_decl(new_env, nat_gcd, &nat_gcd_decl, &ex)); check(lean_decl_get_value(nat_gcd_decl, &nat_gcd_val, &ex)); check(lean_expr_to_pp_string(new_env, ios, nat_gcd_val, &s, &ex)); printf("nat.gcd\n%s\n", s); lean_env_del(env); lean_name_del(std); lean_list_name_del(ms); lean_options_del(o); lean_ios_del(ios); lean_env_del(new_env); lean_name_del(nat); lean_name_del(nat_gcd); lean_decl_del(nat_gcd_decl); lean_expr_del(nat_gcd_val); lean_string_del(s); }
void anonymous_unique() { lean_exception ex; lean_name a1, a2; check(lean_name_mk_anonymous(&a1, &ex)); check(lean_name_mk_anonymous(&a2, &ex)); check(lean_name_eq(a1, a2)); lean_name_del(a1); lean_name_del(a2); }
void test_add_univ() { lean_exception ex; lean_name u = mk_name("u"); lean_name v = mk_name("v"); lean_env env = mk_env(); lean_env new_env; check(lean_env_add_univ(env, u, &new_env, &ex)); check(lean_env_contains_univ(new_env, u)); check(!lean_env_contains_univ(new_env, v)); check(lean_env_for_each_univ(new_env, print_univ_and_del, &ex)); lean_name_del(u); lean_name_del(v); lean_env_del(env); lean_env_del(new_env); }
lean_decl mk_def(char const * n, lean_list_name p, lean_expr t, lean_expr v) { lean_exception ex; lean_name name = mk_name(n); lean_decl r; check(lean_decl_mk_def(name, p, t, v, 0, lean_true, &r, &ex)); lean_name_del(name); return r; }
lean_expr mk_local(char const * n, lean_expr t) { lean_exception ex; lean_name _n = mk_name(n); lean_expr r; check(lean_expr_mk_local(_n, t, &r, &ex)); lean_name_del(_n); return r; }
void print_univ_and_del(lean_name n) { lean_exception ex; char const * s; check(lean_name_to_string(n, &s, &ex)); printf("universe: %s\n", s); lean_string_del(s); lean_name_del(n); }
lean_expr mk_pi(char const * n, lean_expr d, lean_expr c) { lean_exception ex; lean_name name = mk_name(n); lean_expr r; check(lean_expr_mk_pi(name, d, c, LEAN_BINDER_DEFAULT, &r, &ex)); lean_name_del(name); return r; }
lean_univ mk_uparam(char const * n) { lean_exception ex; lean_univ r; lean_name _n = mk_name(n); check(lean_univ_mk_param(_n, &r, &ex)); lean_name_del(_n); return r; }
lean_expr mk_constant(char const * n, lean_list_univ us) { lean_exception ex; lean_name _n = mk_name(n); lean_expr r; check(lean_expr_mk_const(_n, us, &r, &ex)); lean_name_del(_n); return r; }
lean_name mk_name(char const * n) { lean_exception ex; lean_name a, r; check(lean_name_mk_anonymous(&a, &ex)); check(lean_name_mk_str(a, n, &r, &ex)); lean_name_del(a); return r; }
void print_decl_name_and_del(lean_decl d) { lean_exception ex; lean_name n; char const * s; check(lean_decl_get_name(d, &n, &ex)); check(lean_name_to_string(n, &s, &ex)); printf("declaration name: %s\n", s); lean_name_del(n); lean_string_del(s); lean_decl_del(d); }
/** create a constant with a single universe parameter */ lean_expr mk_const(char const * n, lean_univ u) { lean_exception ex; lean_name _n = mk_name(n); lean_list_univ l1, l2; check(lean_list_univ_mk_nil(&l1, &ex)); check(lean_list_univ_mk_cons(u, l1, &l2, &ex)); lean_expr r; check(lean_expr_mk_const(_n, l2, &r, &ex)); lean_name_del(_n); lean_list_univ_del(l1); lean_list_univ_del(l2); return r; }
int main() { lean_exception ex; lean_name a, n1, n2, n3, n4, n5, n6; char const * s1; char const * s2; char const * s3; lean_list_name l1, l2, l3, l4; unsigned idx; printf("Started name test\n"); check(lean_name_mk_anonymous(&a, &ex)); check(lean_name_is_anonymous(a)); check(lean_name_mk_str(a, "foo", &n1, &ex)); check(lean_name_mk_str(n1, "bla", &n2, &ex)); check(lean_name_to_string(n2, &s1, &ex)); printf("Lean name: %s\n", s1); check(lean_name_is_str(n2)); check(!lean_name_is_anonymous(n2)); check(!lean_name_is_idx(n2)); check(lean_name_mk_idx(n2, 1, &n3, &ex)); check(lean_name_to_string(n3, &s2, &ex)); printf("Lean name: %s\n", s2); check(lean_name_is_idx(n3)); check(lean_name_get_prefix(n3, &n4, &ex)); check(lean_name_eq(n2, n4)); check(lean_name_get_idx(n3, &idx, &ex)); check(idx == 1); check(!lean_name_get_prefix(a, &n5, &ex)); s3 = lean_exception_get_message(ex); printf("Lean exception: %s\n", s3); check(lean_list_name_mk_nil(&l1, &ex)); check(!lean_list_name_is_cons(l1)); check(lean_list_name_mk_cons(n1, l1, &l2, &ex)); check(lean_list_name_is_cons(l2)); check(lean_list_name_mk_cons(n2, l2, &l3, &ex)); check(lean_list_name_head(l3, &n6, &ex)); check(lean_name_eq(n6, n2)); check(lean_list_name_tail(l3, &l4, &ex)); check(lean_list_name_eq(l4, l2)); anonymous_unique(); lean_name_del(a); lean_name_del(n1); lean_name_del(n2); lean_name_del(n3); lean_name_del(n4); lean_name_del(n6); lean_list_name_del(l1); lean_list_name_del(l2); lean_list_name_del(l3); lean_list_name_del(l4); lean_string_del(s1); lean_string_del(s2); lean_string_del(s3); lean_exception_del(ex); return 0; }
void test_import() { lean_exception ex; lean_env env = mk_env(); lean_name std = mk_name("standard"); lean_list_name ms = mk_unary_name_list(std); lean_options o; lean_ios ios; lean_env new_env; check(lean_options_mk_empty(&o, &ex)); check(lean_ios_mk_std(o, &ios, &ex)); check(lean_env_import(env, ios, ms, &new_env, &ex)); lean_env_del(env); lean_env_del(new_env); lean_name_del(std); lean_list_name_del(ms); lean_options_del(o); lean_ios_del(ios); }
void print_decl_and_del(lean_decl d) { lean_exception ex; lean_name n; lean_expr t; char const * s; check(lean_decl_get_name(d, &n, &ex)); check(lean_name_to_string(n, &s, &ex)); printf("declaration name: %s\n", s); check(lean_decl_get_type(d, &t, &ex)); printf(" type: "); print_expr(t); if (lean_decl_get_kind(d) == LEAN_DECL_DEF || lean_decl_get_kind(d) == LEAN_DECL_THM) { lean_expr v; check(lean_decl_get_value(d, &v, &ex)); printf(" value: "); print_expr(v); lean_expr_del(v); } lean_expr_del(t); lean_name_del(n); lean_string_del(s); lean_decl_del(d); }
void test_id() { lean_exception ex; lean_univ l = mk_uparam("l"); lean_env env = mk_env(); lean_expr v0 = mk_var(0); lean_expr v1 = mk_var(1); lean_expr AA = mk_pi("a", v0, v1); lean_expr Type = mk_sort(l); lean_expr id_type = mk_pi("A", Type, AA); lean_expr f = mk_lambda("a", v0, v0); lean_expr id_val = mk_lambda("A", Type, f); lean_name l_name = mk_name("l"); lean_list_name id_ps = mk_unary_name_list(l_name); lean_decl id_def = mk_def("id", id_ps, id_type, id_val); lean_name id_name = mk_name("id"); lean_cert_decl id_cert_def; lean_env new_env; lean_univ zero, one; lean_bool is_lt; check(lean_expr_lt(f, id_val, &is_lt, &ex) && is_lt); check(lean_expr_lt(id_val, f, &is_lt, &ex) && !is_lt); printf("id type: "); print_expr(id_type); printf("id value: "); print_expr(id_val); printf("-------\n"); /* type check definition */ check(lean_decl_check(env, id_def, &id_cert_def, &ex)); /* add certified definition to environment */ check(lean_env_add(env, id_cert_def, &new_env, &ex)); check(!lean_env_contains_decl(env, id_name)); check(lean_env_contains_decl(new_env, id_name)); check(lean_env_for_each_decl(new_env, print_decl_and_del, &ex)); check(lean_univ_mk_zero(&zero, &ex)); check(lean_univ_mk_succ(zero, &one, &ex)); check(lean_univ_lt(zero, one, &is_lt, &ex) && is_lt); check(lean_univ_lt(one, zero, &is_lt, &ex) && !is_lt); { lean_type_checker tc; lean_expr T0 = mk_sort(zero); lean_expr T1 = mk_sort(one); lean_expr id1 = mk_const("id", one); lean_expr id1T1, id1T1T0; lean_expr n1; lean_cnstr_seq s1; check(lean_expr_mk_app(id1, T1, &id1T1, &ex)); check(lean_expr_mk_app(id1T1, T0, &id1T1T0, &ex)); check(lean_type_checker_mk(new_env, &tc, &ex)); printf("WHNF test\n"); print_expr(id1T1T0); check(lean_type_checker_whnf(tc, id1T1T0, &n1, &s1, &ex)); printf("=====>\n"); print_expr(n1); lean_expr_del(n1); lean_cnstr_seq_del(s1); printf("Infer type test\n"); print_expr(id1T1); check(lean_type_checker_infer(tc, id1T1, &n1, &s1, &ex)); printf("=====>\n"); print_expr(n1); lean_expr_del(n1); lean_cnstr_seq_del(s1); lean_type_checker_del(tc); lean_expr_del(T0); lean_expr_del(T1); lean_expr_del(id1); lean_expr_del(id1T1); lean_expr_del(id1T1T0); } lean_univ_del(l); lean_env_del(env); lean_expr_del(v0); lean_expr_del(v1); lean_expr_del(Type); lean_expr_del(AA); lean_expr_del(id_type); lean_expr_del(f); lean_expr_del(id_val); lean_decl_del(id_def); lean_list_name_del(id_ps); lean_name_del(l_name); lean_cert_decl_del(id_cert_def); lean_env_del(new_env); lean_name_del(id_name); lean_univ_del(zero); lean_univ_del(one); }
void test_inductive() { // declare list type lean_exception ex = 0; lean_env env = mk_env(); lean_name l_name = mk_name("l"); lean_univ l = mk_uparam("l"); lean_univ one = mk_one(); lean_univ m1l = mk_max(one, l); lean_expr Typel = mk_sort(l); lean_expr Typem1l = mk_sort(m1l); lean_expr list_type = mk_pi("A", Typel, Typem1l); lean_name list_name = mk_name("list"); lean_expr list = mk_const("list", l); lean_expr v0 = mk_var(0); // nil : Pi (A : Type.{l}), list.{l} A lean_expr list_v0 = mk_app(list, v0); lean_expr nil_type = mk_pi("A", Typel, list_v0); lean_expr nil = mk_local("nil", nil_type); // cons : Pi (A : Type.{l}), A -> list.{l} A -> list.{l} A lean_expr v1 = mk_var(1); lean_expr v2 = mk_var(2); lean_expr list_v2 = mk_app(list, v2); lean_expr list_v1 = mk_app(list, v1); lean_expr cons_type1 = mk_pi("tail", list_v1, list_v2); lean_expr cons_type2 = mk_pi("head", v0, cons_type1); lean_expr cons_type = mk_pi("A", Typel, cons_type2); lean_expr cons = mk_local("cons", cons_type); // lean_list_expr cs1, cs2, list_cs; lean_inductive_type list_ind_type; lean_list_inductive_type li1, list_ind_types; lean_list_name ls1, ls; lean_inductive_decl list_decl; lean_env new_env; check(lean_list_name_mk_nil(&ls1, &ex)); check(lean_list_name_mk_cons(l_name, ls1, &ls, &ex)); check(lean_list_expr_mk_nil(&cs1, &ex)); check(lean_list_expr_mk_cons(nil, cs1, &cs2, &ex)); check(lean_list_expr_mk_cons(cons, cs2, &list_cs, &ex)); check(lean_inductive_type_mk(list_name, list_type, list_cs, &list_ind_type, &ex)); check(lean_list_inductive_type_mk_nil(&li1, &ex)); check(lean_list_inductive_type_mk_cons(list_ind_type, li1, &list_ind_types, &ex)); check(lean_inductive_decl_mk(ls, 1, list_ind_types, &list_decl, &ex)); check(lean_env_add_inductive(env, list_decl, &new_env, &ex)); { unsigned n; lean_inductive_decl d; lean_name cons_name = mk_name("cons"); lean_name r_name; lean_list_inductive_type types; check(lean_env_get_inductive_type_num_indices(new_env, list_name, &n, &ex) && n == 0); check(lean_env_get_inductive_type_num_minor_premises(new_env, list_name, &n, &ex) && n == 2); check(!lean_env_is_inductive_type(env, list_name, &d, &ex)); check(lean_env_is_inductive_type(new_env, list_name, &d, &ex)); check(lean_inductive_decl_get_num_params(d, &n, &ex) && n == 1); check(lean_inductive_decl_get_types(d, &types, &ex)); check(lean_list_inductive_type_is_cons(types)); check(lean_env_is_constructor(new_env, cons_name, &r_name, &ex) && lean_name_eq(list_name, r_name)); lean_inductive_decl_del(d); lean_name_del(cons_name); lean_name_del(r_name); } lean_env_del(env); lean_name_del(list_name); lean_name_del(l_name); lean_univ_del(l); lean_univ_del(one); lean_univ_del(m1l); lean_expr_del(Typel); lean_expr_del(Typem1l); lean_expr_del(list_type); lean_expr_del(list); lean_expr_del(v0); lean_expr_del(list_v0); lean_expr_del(nil_type); lean_expr_del(nil); lean_expr_del(v1); lean_expr_del(v2); lean_expr_del(list_v2); lean_expr_del(list_v1); lean_expr_del(cons_type1); lean_expr_del(cons_type2); lean_expr_del(cons_type); lean_expr_del(cons); lean_list_expr_del(cs1); lean_list_expr_del(cs2); lean_list_expr_del(list_cs); lean_inductive_type_del(list_ind_type); lean_list_inductive_type_del(li1); lean_list_inductive_type_del(list_ind_types); lean_list_name_del(ls1); lean_list_name_del(ls); lean_inductive_decl_del(list_decl); lean_env_del(new_env); }
int main() { lean_exception ex; lean_name a, l, U, pp, pp_unicode, rn; lean_options o1, o2; lean_univ zero, one, p1, g1, m1, u, n, i, ru; lean_list_name ln1, ln2; lean_list_univ lu1, lu2; char const * s1; lean_bool r; check(lean_name_mk_anonymous(&a, &ex)); check(lean_name_mk_str(a, "l_1", &l, &ex)); check(lean_name_mk_str(a, "U", &U, &ex)); check(lean_name_mk_str(a, "pp", &pp, &ex)); check(lean_name_mk_str(pp, "unicode", &pp_unicode, &ex)); check(lean_options_mk_empty(&o1, &ex)); check(lean_options_set_bool(o1, pp_unicode, lean_false, &o2, &ex)); check(lean_univ_mk_zero(&zero, &ex)); check(lean_univ_mk_succ(zero, &one, &ex)); check(lean_univ_mk_param(l, &p1, &ex)); check(lean_univ_mk_global(U, &g1, &ex)); check(lean_univ_mk_max(p1, one, &m1, &ex)); check(lean_univ_mk_succ(m1, &u, &ex)); check(lean_univ_normalize(u, &n, &ex)); check(lean_univ_to_string(n, &s1, &ex)); printf("universe: %s\n", s1); check(lean_univ_geq(one, zero, &r, &ex) && r); /* replace l_1 with one in u */ check(lean_list_name_mk_nil(&ln1, &ex)); check(lean_list_name_mk_cons(l, ln1, &ln2, &ex)); check(lean_list_univ_mk_nil(&lu1, &ex)); check(lean_list_univ_mk_cons(one, lu1, &lu2, &ex)); check(lean_univ_instantiate(u, ln2, lu2, &i, &ex)); lean_list_name_del(ln1); lean_list_name_del(ln2); lean_list_univ_del(lu1); lean_list_univ_del(lu2); lean_string_del(s1); check(lean_univ_to_string_using(i, o2, &s1, &ex)); printf("universe: %s\n", s1); check(lean_univ_get_kind(zero) == LEAN_UNIV_ZERO); check(lean_univ_get_kind(one) == LEAN_UNIV_SUCC); check(lean_univ_get_kind(n) == LEAN_UNIV_MAX); check(lean_univ_get_name(g1, &rn, &ex) && lean_name_eq(rn, U)); check(lean_univ_get_max_lhs(m1, &ru, &ex) && lean_univ_eq(ru, p1, &r, &ex) && r); lean_univ_del(ru); check(lean_univ_get_max_rhs(m1, &ru, &ex) && lean_univ_eq(ru, one, &r, &ex) && r); lean_univ_del(ru); check(lean_univ_get_pred(one, &ru, &ex) && lean_univ_eq(ru, zero, &r, &ex) && r); lean_univ_del(ru); lean_name_del(a); lean_name_del(l); lean_name_del(U); lean_name_del(pp); lean_name_del(pp_unicode); lean_name_del(rn); lean_options_del(o1); lean_options_del(o2); lean_univ_del(zero); lean_univ_del(one); lean_univ_del(p1); lean_univ_del(g1); lean_univ_del(m1); lean_univ_del(u); lean_univ_del(n); lean_univ_del(i); lean_string_del(s1); return 0; }