lean2/src/library/expr_lt.cpp

107 lines
4.1 KiB
C++
Raw Normal View History

/*
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 "kernel/expr.h"
namespace lean {
bool is_lt(expr const & a, expr const & b, bool use_hash);
static bool is_lt(optional<expr> const & a, optional<expr> const & b, bool use_hash) {
if (is_eqp(a, b)) return false;
else if (!a && b) return true;
else if (a && !b) return false;
else return is_lt(*a, *b, use_hash);
}
bool is_lt(expr const & a, expr const & b, bool use_hash) {
if (is_eqp(a, b)) return false;
unsigned da = get_depth(a);
unsigned db = get_depth(b);
if (da < db) return true;
if (da > db) return false;
if (a.kind() != b.kind()) return a.kind() < b.kind();
if (use_hash) {
if (a.hash() < b.hash()) return true;
if (a.hash() > b.hash()) return false;
}
if (a == b) return false;
if (is_var(a)) return var_idx(a) < var_idx(b);
switch (a.kind()) {
case expr_kind::Var:
lean_unreachable(); // LCOV_EXCL_LINE
case expr_kind::Constant:
return const_name(a) < const_name(b);
case expr_kind::App:
if (num_args(a) != num_args(b))
return num_args(a) < num_args(b);
for (unsigned i = 0; i < num_args(a); i++) {
if (arg(a, i) != arg(b, i))
return is_lt(arg(a, i), arg(b, i), use_hash);
}
lean_unreachable(); // LCOV_EXCL_LINE
refactor(kernel): add heterogeneous equality back to expr The main motivation is that we will be able to move equalities between universes. For example, suppose we have A : (Type i) B : (Type i) H : @eq (Type j) A B where j > i We didn't find any trick for deducing (@eq (Type i) A B) from H. Before this commit, heterogeneous equality as a constant with type heq : {A B : (Type U)} : A -> B -> Bool So, from H, we would only be able to deduce (@heq (Type j) (Type j) A B) Not being able to move the equality back to a smaller universe is problematic in several cases. I list some instances in the end of the commit message. With this commit, Heterogeneous equality is a special kind of expression. It is not a constant anymore. From H, we can deduce H1 : A == B That is, we are essentially "erasing" the universes when we move to heterogeneous equality. Now, since A and B have (Type i), we can deduce (@eq (Type i) A B) from H1. The proof term is (to_eq (Type i) A B (to_heq (Type j) A B H)) : (@eq (Type i) A B) So, it remains to explain why we need this feature. For example, suppose we want to state the Pi extensionality axiom. axiom hpiext {A A' : (Type U)} {B : A → (Type U)} {B' : A' → (Type U)} : A = A' → (∀ x x', x == x' → B x == B' x') → (∀ x, B x) == (∀ x, B' x) This axiom produces an "inflated" equality at (Type U) when we treat heterogeneous equality as a constant. The conclusion (∀ x, B x) == (∀ x, B' x) is syntax sugar for (@heq (Type U) (Type U) (∀ x : A, B x) (∀ x : A', B' x)) Even if A, A', B, B' live in a much smaller universe. As I described above, it doesn't seem to be a way to move this equality back to a smaller universe. So, if we wanted to keep the heterogeneous equality as a constant, it seems we would have to support axiom schemas. That is, hpiext would be parametrized by the universes where A, A', B and B'. Another possibility would be to have universe polymorphism like Agda. None of the solutions seem attractive. So, we decided to have heterogeneous equality as a special kind of expression. And use the trick above to move equalities back to the right universe. BTW, the parser is not creating the new heterogeneous equalities yet. Moreover, kernel.lean still contains a constant name heq2 that is the heterogeneous equality as a constant. Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
2014-02-07 18:07:08 +00:00
case expr_kind::HEq:
if (heq_lhs(a) != heq_lhs(b))
return is_lt(heq_lhs(a), heq_lhs(b), use_hash);
else
return is_lt(heq_rhs(a), heq_rhs(b), use_hash);
case expr_kind::Pair:
if (pair_first(a) != pair_first(b))
return is_lt(pair_first(a), pair_first(b), use_hash);
else if (pair_second(a) != pair_second(b))
return is_lt(pair_second(a), pair_second(b), use_hash);
else
return is_lt(pair_type(a), pair_type(b), use_hash);
case expr_kind::Proj:
if (proj_first(a) != proj_first(b))
return proj_first(a) && !proj_first(b); // first projection is smaller than the second one.
else
return is_lt(proj_arg(a), proj_arg(b), use_hash);
case expr_kind::Sigma:
case expr_kind::Lambda:
case expr_kind::Pi: // Remark: we ignore get_abs_name because we want alpha-equivalence
if (abst_domain(a) != abst_domain(b))
return is_lt(abst_domain(a), abst_domain(b), use_hash);
else
return is_lt(abst_body(a), abst_body(b), use_hash);
case expr_kind::Type:
return ty_level(a) < ty_level(b);
case expr_kind::Value:
return to_value(a) < to_value(b);
case expr_kind::Let:
if (let_type(a) != let_type(b)) {
return is_lt(let_type(a), let_type(b), use_hash);
} else if (let_value(a) != let_value(b)){
return is_lt(let_value(a), let_value(b), use_hash);
} else {
return is_lt(let_body(a), let_body(b), use_hash);
}
case expr_kind::MetaVar:
if (metavar_name(a) != metavar_name(b)) {
return metavar_name(a) < metavar_name(b);
} else {
auto it1 = metavar_lctx(a).begin();
auto it2 = metavar_lctx(b).begin();
auto end1 = metavar_lctx(a).end();
auto end2 = metavar_lctx(b).end();
for (; it1 != end1 && it2 != end2; ++it1, ++it2) {
if (it1->kind() != it2->kind()) {
return it1->kind() < it2->kind();
} else if (it1->s() != it2->s()) {
return it1->s() < it2->s();
} else if (it1->is_inst()) {
if (it1->v() != it2->v())
return is_lt(it1->v(), it2->v(), use_hash);
} else {
if (it1->n() != it2->n())
return it1->n() < it2->n();
}
}
return it1 == end1 && it2 != end2;
}
}
lean_unreachable(); // LCOV_EXCL_LINE
}
}