2013-08-04 16:37:52 +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
|
|
|
|
|
*/
|
|
|
|
|
#include "builtin.h"
|
2013-08-05 21:23:43 +00:00
|
|
|
|
#include "environment.h"
|
2013-08-06 03:06:07 +00:00
|
|
|
|
#include "abstract.h"
|
|
|
|
|
|
|
|
|
|
#ifndef LEAN_DEFAULT_LEVEL_SEPARATION
|
|
|
|
|
#define LEAN_DEFAULT_LEVEL_SEPARATION 512
|
|
|
|
|
#endif
|
2013-08-04 16:37:52 +00:00
|
|
|
|
|
|
|
|
|
namespace lean {
|
2013-08-16 16:04:59 +00:00
|
|
|
|
expr mk_bin_rop(expr const & op, expr const & unit, unsigned num_args, expr const * args) {
|
2013-08-06 03:06:07 +00:00
|
|
|
|
if (num_args == 0) {
|
|
|
|
|
return unit;
|
|
|
|
|
} else {
|
|
|
|
|
expr r = args[num_args - 1];
|
|
|
|
|
unsigned i = num_args - 1;
|
|
|
|
|
while (i > 0) {
|
|
|
|
|
--i;
|
2013-08-06 18:27:14 +00:00
|
|
|
|
r = mk_app({op, args[i], r});
|
2013-08-06 03:06:07 +00:00
|
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-08-16 16:04:59 +00:00
|
|
|
|
expr mk_bin_rop(expr const & op, expr const & unit, std::initializer_list<expr> const & l) {
|
|
|
|
|
return mk_bin_rop(op, unit, l.size(), l.begin());
|
|
|
|
|
}
|
2013-08-06 03:06:07 +00:00
|
|
|
|
|
2013-08-16 16:04:59 +00:00
|
|
|
|
expr mk_bin_lop(expr const & op, expr const & unit, unsigned num_args, expr const * args) {
|
|
|
|
|
if (num_args == 0) {
|
|
|
|
|
return unit;
|
|
|
|
|
} else {
|
|
|
|
|
expr r = args[0];
|
|
|
|
|
for (unsigned i = 1; i < num_args; i++) {
|
|
|
|
|
r = mk_app({op, r, args[i]});
|
|
|
|
|
}
|
|
|
|
|
return r;
|
|
|
|
|
}
|
2013-08-06 03:06:07 +00:00
|
|
|
|
}
|
2013-08-16 16:04:59 +00:00
|
|
|
|
expr mk_bin_lop(expr const & op, expr const & unit, std::initializer_list<expr> const & l) {
|
|
|
|
|
return mk_bin_lop(op, unit, l.size(), l.begin());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =======================================
|
|
|
|
|
// Bultin universe variables m and u
|
2013-09-02 19:24:29 +00:00
|
|
|
|
static level m_lvl(name("M"));
|
|
|
|
|
static level u_lvl(name("U"));
|
2013-09-02 16:11:53 +00:00
|
|
|
|
expr const TypeM = Type(m_lvl);
|
|
|
|
|
expr const TypeU = Type(u_lvl);
|
2013-08-16 16:04:59 +00:00
|
|
|
|
// =======================================
|
|
|
|
|
|
|
|
|
|
// =======================================
|
|
|
|
|
// Boolean Type
|
|
|
|
|
static char const * g_Bool_str = "Bool";
|
2013-09-02 19:24:29 +00:00
|
|
|
|
static name g_Bool_name(g_Bool_str);
|
2013-08-16 16:04:59 +00:00
|
|
|
|
static format g_Bool_fmt(g_Bool_str);
|
2013-08-04 16:37:52 +00:00
|
|
|
|
class bool_type_value : public value {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~bool_type_value() {}
|
2013-08-06 18:27:14 +00:00
|
|
|
|
virtual expr get_type() const { return Type(); }
|
2013-09-04 06:15:37 +00:00
|
|
|
|
virtual name get_name() const { return g_Bool_name; }
|
2013-08-04 16:37:52 +00:00
|
|
|
|
};
|
2013-09-02 16:11:53 +00:00
|
|
|
|
expr const Bool = mk_value(*(new bool_type_value()));
|
|
|
|
|
expr mk_bool_type() { return Bool; }
|
2013-08-16 16:04:59 +00:00
|
|
|
|
// =======================================
|
|
|
|
|
|
|
|
|
|
// =======================================
|
|
|
|
|
// Boolean values True and False
|
2013-09-04 06:15:37 +00:00
|
|
|
|
static name g_true_name("true");
|
|
|
|
|
static name g_false_name("false");
|
2013-09-04 15:53:00 +00:00
|
|
|
|
static name g_true_u_name("\u22A4"); // ⊤
|
|
|
|
|
static name g_false_u_name("\u22A5"); // ⊥
|
2013-08-04 16:37:52 +00:00
|
|
|
|
class bool_value_value : public value {
|
|
|
|
|
bool m_val;
|
|
|
|
|
public:
|
|
|
|
|
bool_value_value(bool v):m_val(v) {}
|
|
|
|
|
virtual ~bool_value_value() {}
|
2013-08-06 18:27:14 +00:00
|
|
|
|
virtual expr get_type() const { return Bool; }
|
2013-09-04 06:15:37 +00:00
|
|
|
|
virtual name get_name() const { return m_val ? g_true_name : g_false_name; }
|
2013-09-04 15:53:00 +00:00
|
|
|
|
virtual name get_unicode_name() const { return m_val ? g_true_u_name : g_false_u_name; }
|
2013-08-04 16:37:52 +00:00
|
|
|
|
virtual bool operator==(value const & other) const {
|
2013-09-02 16:35:10 +00:00
|
|
|
|
bool_value_value const * _other = dynamic_cast<bool_value_value const*>(&other);
|
|
|
|
|
return _other && _other->m_val == m_val;
|
2013-08-04 16:37:52 +00:00
|
|
|
|
}
|
|
|
|
|
bool get_val() const { return m_val; }
|
|
|
|
|
};
|
2013-09-02 16:11:53 +00:00
|
|
|
|
expr const True = mk_value(*(new bool_value_value(true)));
|
|
|
|
|
expr const False = mk_value(*(new bool_value_value(false)));
|
2013-08-06 18:27:14 +00:00
|
|
|
|
expr mk_bool_value(bool v) {
|
2013-09-02 16:11:53 +00:00
|
|
|
|
return v ? True : False;
|
2013-08-04 16:37:52 +00:00
|
|
|
|
}
|
|
|
|
|
bool is_bool_value(expr const & e) {
|
2013-09-02 16:35:10 +00:00
|
|
|
|
return is_value(e) && dynamic_cast<bool_value_value const *>(&to_value(e)) != nullptr;
|
2013-08-04 16:37:52 +00:00
|
|
|
|
}
|
|
|
|
|
bool to_bool(expr const & e) {
|
|
|
|
|
lean_assert(is_bool_value(e));
|
|
|
|
|
return static_cast<bool_value_value const &>(to_value(e)).get_val();
|
|
|
|
|
}
|
|
|
|
|
bool is_true(expr const & e) {
|
|
|
|
|
return is_bool_value(e) && to_bool(e);
|
|
|
|
|
}
|
|
|
|
|
bool is_false(expr const & e) {
|
|
|
|
|
return is_bool_value(e) && !to_bool(e);
|
|
|
|
|
}
|
2013-08-16 16:04:59 +00:00
|
|
|
|
// =======================================
|
2013-08-04 16:37:52 +00:00
|
|
|
|
|
2013-08-16 16:04:59 +00:00
|
|
|
|
// =======================================
|
|
|
|
|
// If-then-else builtin operator
|
2013-09-04 15:30:04 +00:00
|
|
|
|
static name g_if_name("if");
|
|
|
|
|
static format g_if_fmt(g_if_name);
|
|
|
|
|
class if_fn_value : public value {
|
2013-08-06 03:06:07 +00:00
|
|
|
|
expr m_type;
|
|
|
|
|
public:
|
2013-09-04 15:30:04 +00:00
|
|
|
|
if_fn_value() {
|
2013-08-06 18:27:14 +00:00
|
|
|
|
expr A = Const("A");
|
2013-08-06 03:06:07 +00:00
|
|
|
|
// Pi (A: Type), bool -> A -> A -> A
|
2013-08-06 18:27:14 +00:00
|
|
|
|
m_type = Pi({A, TypeU}, Bool >> (A >> (A >> A)));
|
2013-08-06 03:06:07 +00:00
|
|
|
|
}
|
2013-09-04 15:30:04 +00:00
|
|
|
|
virtual ~if_fn_value() {}
|
2013-08-06 03:06:07 +00:00
|
|
|
|
virtual expr get_type() const { return m_type; }
|
2013-09-04 15:30:04 +00:00
|
|
|
|
virtual name get_name() const { return g_if_name; }
|
2013-08-06 03:06:07 +00:00
|
|
|
|
virtual bool normalize(unsigned num_args, expr const * args, expr & r) const {
|
|
|
|
|
if (num_args == 5 && is_bool_value(args[2])) {
|
|
|
|
|
if (to_bool(args[2]))
|
|
|
|
|
r = args[3]; // if A true a b --> a
|
|
|
|
|
else
|
|
|
|
|
r = args[4]; // if A false a b --> b
|
|
|
|
|
return true;
|
2013-08-07 03:21:43 +00:00
|
|
|
|
} if (num_args == 5 && args[3] == args[4]) {
|
|
|
|
|
r = args[3]; // if A c a a --> a
|
|
|
|
|
return true;
|
2013-08-06 03:06:07 +00:00
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-09-04 15:30:04 +00:00
|
|
|
|
MK_BUILTIN(if_fn, if_fn_value);
|
2013-08-16 16:04:59 +00:00
|
|
|
|
// =======================================
|
|
|
|
|
|
|
|
|
|
MK_CONSTANT(implies_fn, name("implies"));
|
2013-08-18 22:03:58 +00:00
|
|
|
|
MK_CONSTANT(iff_fn, name("iff"));
|
2013-08-16 16:04:59 +00:00
|
|
|
|
MK_CONSTANT(and_fn, name("and"));
|
|
|
|
|
MK_CONSTANT(or_fn, name("or"));
|
|
|
|
|
MK_CONSTANT(not_fn, name("not"));
|
|
|
|
|
MK_CONSTANT(forall_fn, name("forall"));
|
|
|
|
|
MK_CONSTANT(exists_fn, name("exists"));
|
2013-08-30 21:26:12 +00:00
|
|
|
|
MK_CONSTANT(homo_eq_fn, name("heq"));
|
2013-09-07 01:31:47 +00:00
|
|
|
|
MK_CONSTANT(cast_fn, name("cast"));
|
2013-08-06 03:06:07 +00:00
|
|
|
|
|
2013-08-07 15:29:20 +00:00
|
|
|
|
// Axioms
|
|
|
|
|
MK_CONSTANT(mp_fn, name("MP"));
|
|
|
|
|
MK_CONSTANT(discharge_fn, name("Discharge"));
|
|
|
|
|
MK_CONSTANT(refl_fn, name("Refl"));
|
|
|
|
|
MK_CONSTANT(case_fn, name("Case"));
|
|
|
|
|
MK_CONSTANT(subst_fn, name("Subst"));
|
|
|
|
|
MK_CONSTANT(eta_fn, name("Eta"));
|
|
|
|
|
MK_CONSTANT(imp_antisym_fn, name("ImpAntisym"));
|
2013-09-07 01:31:47 +00:00
|
|
|
|
MK_CONSTANT(dom_inj_fn, name("DomInj"));
|
|
|
|
|
MK_CONSTANT(ran_inj_fn, name("RanInj"));
|
2013-08-07 02:10:38 +00:00
|
|
|
|
|
2013-08-05 21:23:43 +00:00
|
|
|
|
void add_basic_theory(environment & env) {
|
2013-08-09 22:33:34 +00:00
|
|
|
|
env.add_uvar(uvar_name(m_lvl), level() + LEAN_DEFAULT_LEVEL_SEPARATION);
|
|
|
|
|
env.add_uvar(uvar_name(u_lvl), m_lvl + LEAN_DEFAULT_LEVEL_SEPARATION);
|
2013-09-04 15:30:04 +00:00
|
|
|
|
|
2013-08-07 08:16:37 +00:00
|
|
|
|
expr p1 = Bool >> Bool;
|
|
|
|
|
expr p2 = Bool >> p1;
|
|
|
|
|
expr f = Const("f");
|
|
|
|
|
expr a = Const("a");
|
|
|
|
|
expr b = Const("b");
|
|
|
|
|
expr x = Const("x");
|
|
|
|
|
expr y = Const("y");
|
|
|
|
|
expr A = Const("A");
|
2013-09-07 01:31:47 +00:00
|
|
|
|
expr Ap = Const("A'");
|
2013-08-07 08:16:37 +00:00
|
|
|
|
expr A_pred = A >> Bool;
|
|
|
|
|
expr B = Const("B");
|
2013-09-07 01:31:47 +00:00
|
|
|
|
expr Bp = Const("B'");
|
2013-08-07 08:16:37 +00:00
|
|
|
|
expr q_type = Pi({A, TypeU}, A_pred >> Bool);
|
|
|
|
|
expr piABx = Pi({x, A}, B(x));
|
2013-09-07 01:31:47 +00:00
|
|
|
|
expr piApBpx = Pi({x, Ap}, Bp(x));
|
2013-08-06 21:47:13 +00:00
|
|
|
|
expr A_arrow_u = A >> TypeU;
|
2013-08-07 08:16:37 +00:00
|
|
|
|
expr P = Const("P");
|
|
|
|
|
expr H = Const("H");
|
|
|
|
|
expr H1 = Const("H1");
|
|
|
|
|
expr H2 = Const("H2");
|
2013-08-16 16:04:59 +00:00
|
|
|
|
|
2013-09-04 15:30:04 +00:00
|
|
|
|
env.add_builtin(mk_bool_type());
|
|
|
|
|
env.add_builtin(mk_bool_value(true));
|
|
|
|
|
env.add_builtin(mk_bool_value(false));
|
|
|
|
|
env.add_builtin(mk_if_fn());
|
2013-08-16 16:04:59 +00:00
|
|
|
|
|
2013-09-04 15:30:04 +00:00
|
|
|
|
// implies(x, y) := if x y True
|
2013-08-16 16:04:59 +00:00
|
|
|
|
env.add_definition(implies_fn_name, p2, Fun({{x, Bool}, {y, Bool}}, bIf(x, y, True)));
|
2013-08-06 21:47:13 +00:00
|
|
|
|
|
2013-08-18 22:03:58 +00:00
|
|
|
|
// iff(x, y) := x = y
|
|
|
|
|
env.add_definition(iff_fn_name, p2, Fun({{x, Bool}, {y, Bool}}, Eq(x, y)));
|
|
|
|
|
|
2013-09-04 15:30:04 +00:00
|
|
|
|
// not(x) := if x False True
|
2013-08-16 16:04:59 +00:00
|
|
|
|
env.add_definition(not_fn_name, p1, Fun({x, Bool}, bIf(x, False, True)));
|
2013-08-07 03:21:43 +00:00
|
|
|
|
|
2013-08-16 16:04:59 +00:00
|
|
|
|
// or(x, y) := Not(x) => y
|
2013-08-07 03:21:43 +00:00
|
|
|
|
env.add_definition(or_fn_name, p2, Fun({{x, Bool}, {y, Bool}}, Implies(Not(x), y)));
|
2013-08-07 02:10:38 +00:00
|
|
|
|
|
2013-08-16 16:04:59 +00:00
|
|
|
|
// and(x, y) := Not(x => Not(y))
|
2013-08-07 03:21:43 +00:00
|
|
|
|
env.add_definition(and_fn_name, p2, Fun({{x, Bool}, {y, Bool}}, Not(Implies(x, Not(y)))));
|
2013-08-06 03:06:07 +00:00
|
|
|
|
|
|
|
|
|
// forall : Pi (A : Type u), (A -> Bool) -> Bool
|
2013-08-06 23:44:58 +00:00
|
|
|
|
env.add_definition(forall_fn_name, q_type, Fun({{A, TypeU}, {P, A_pred}}, Eq(P, Fun({x, A}, True))));
|
2013-08-07 03:21:43 +00:00
|
|
|
|
// TODO: introduce epsilon
|
2013-08-06 18:27:14 +00:00
|
|
|
|
env.add_definition(exists_fn_name, q_type, Fun({{A,TypeU}, {P, A_pred}}, Not(Forall(A, Fun({x, A}, Not(P(x)))))));
|
2013-08-06 03:06:07 +00:00
|
|
|
|
|
2013-08-30 21:26:12 +00:00
|
|
|
|
// homogeneous equality
|
2013-09-03 00:01:50 +00:00
|
|
|
|
env.add_definition(homo_eq_fn_name, Pi({{A,TypeU},{x,A},{y,A}}, Bool), Fun({{A,TypeU}, {x,A}, {y,A}}, Eq(x, y)));
|
2013-08-30 21:26:12 +00:00
|
|
|
|
|
2013-09-07 01:31:47 +00:00
|
|
|
|
// Cast: Pi (A : Type u) (B : Type u) (H : A = B) (a : A), B
|
|
|
|
|
env.add_var(cast_fn_name, Pi({{A, TypeU}, {B, TypeU}, {H, Eq(A,B)}, {a, A}}, B));
|
|
|
|
|
|
2013-08-07 03:21:43 +00:00
|
|
|
|
// MP : Pi (a b : Bool) (H1 : a => b) (H2 : a), b
|
|
|
|
|
env.add_axiom(mp_fn_name, Pi({{a, Bool}, {b, Bool}, {H1, Implies(a, b)}, {H2, a}}, b));
|
|
|
|
|
|
|
|
|
|
// Discharge : Pi (a b : Bool) (H : a -> b), a => b
|
|
|
|
|
env.add_axiom(discharge_fn_name, Pi({{a, Bool}, {b, Bool}, {H, a >> b}}, Implies(a, b)));
|
|
|
|
|
|
2013-08-07 02:10:38 +00:00
|
|
|
|
// Refl : Pi (A : Type u) (a : A), a = a
|
2013-08-06 18:27:14 +00:00
|
|
|
|
env.add_axiom(refl_fn_name, Pi({{A, TypeU}, {a, A}}, Eq(a, a)));
|
2013-08-06 21:37:30 +00:00
|
|
|
|
|
2013-08-07 02:10:38 +00:00
|
|
|
|
// Case : Pi (P : Bool -> Bool) (H1 : P True) (H2 : P False) (a : Bool), P a
|
|
|
|
|
env.add_axiom(case_fn_name, Pi({{P, Bool >> Bool}, {H1, P(True)}, {H2, P(False)}, {a, Bool}}, P(a)));
|
|
|
|
|
|
2013-08-08 04:47:58 +00:00
|
|
|
|
// Subst : Pi (A : Type u) (a b : A) (P : A -> bool) (H1 : P a) (H2 : a = b), P b
|
|
|
|
|
env.add_axiom(subst_fn_name, Pi({{A, TypeU}, {a, A}, {b, A}, {P, A_pred}, {H1, P(a)}, {H2, Eq(a,b)}}, P(b)));
|
2013-08-07 03:21:43 +00:00
|
|
|
|
|
|
|
|
|
// Eta : Pi (A : Type u) (B : A -> Type u), f : (Pi x : A, B x), (Fun x : A => f x) = f
|
|
|
|
|
env.add_axiom(eta_fn_name, Pi({{A, TypeU}, {B, A_arrow_u}, {f, piABx}}, Eq(Fun({x, A}, f(x)), f)));
|
2013-08-07 15:29:20 +00:00
|
|
|
|
|
|
|
|
|
// ImpliesAntisym : Pi (a b : Bool) (H1 : a => b) (H2 : b => a), a = b
|
|
|
|
|
env.add_axiom(imp_antisym_fn_name, Pi({{a, Bool}, {b, Bool}, {H1, Implies(a, b)}, {H2, Implies(b, a)}}, Eq(a, b)));
|
2013-09-07 01:31:47 +00:00
|
|
|
|
|
|
|
|
|
// DomInj : Pi (A A': Type u) (B : A -> Type u) (B' : A' -> Type u) (H : (Pi x : A, B x) = (Pi x : A', B' x)), A = A'
|
|
|
|
|
env.add_axiom(dom_inj_fn_name, Pi({{A, TypeU}, {Ap, TypeU}, {B, A >> TypeU}, {Bp, Ap >> TypeU}, {H, Eq(piABx, piApBpx)}}, Eq(A, Ap)));
|
|
|
|
|
|
|
|
|
|
// RanInj : Pi (A A': Type u) (B : A -> Type u) (B' : A' -> Type u) (H : (Pi x : A, B x) = (Pi x : A', B' x)) (a : A),
|
|
|
|
|
// B a = B' (cast A A' (DomInj A A' B B' H) a)
|
|
|
|
|
env.add_axiom(ran_inj_fn_name, Pi({{A, TypeU}, {Ap, TypeU}, {B, A >> TypeU}, {Bp, Ap >> TypeU}, {H, Eq(piABx, piApBpx)}, {a, A}},
|
|
|
|
|
Eq(B(a), Bp(Cast(A, Ap, DomInj(A, Ap, B, Bp, H), a)))));
|
2013-08-05 21:23:43 +00:00
|
|
|
|
}
|
2013-08-04 16:37:52 +00:00
|
|
|
|
}
|