2013-09-24 19:16:32 +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 "kernel/expr.h"
|
2014-02-23 07:09:12 +00:00
|
|
|
#include "library/expr_lt.h"
|
2013-09-24 19:16:32 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-09-26 04:59:58 +00:00
|
|
|
bool is_lt(expr const & a, expr const & b, bool use_hash) {
|
|
|
|
if (is_eqp(a, b)) return false;
|
2014-08-04 22:48:24 +00:00
|
|
|
unsigned wa = get_weight(a);
|
|
|
|
unsigned wb = get_weight(b);
|
|
|
|
if (wa < wb) return true;
|
|
|
|
if (wa > wb) return false;
|
2013-09-26 04:59:58 +00:00
|
|
|
if (a.kind() != b.kind()) return a.kind() < b.kind();
|
2013-11-14 21:18:33 +00:00
|
|
|
if (use_hash) {
|
2014-01-20 21:06:30 +00:00
|
|
|
if (a.hash() < b.hash()) return true;
|
|
|
|
if (a.hash() > b.hash()) return false;
|
2013-11-14 21:18:33 +00:00
|
|
|
}
|
2013-09-26 04:59:58 +00:00
|
|
|
if (a == b) return false;
|
2013-09-24 19:16:32 +00:00
|
|
|
switch (a.kind()) {
|
|
|
|
case expr_kind::Var:
|
2014-02-23 07:09:12 +00:00
|
|
|
return var_idx(a) < var_idx(b);
|
2013-09-24 19:16:32 +00:00
|
|
|
case expr_kind::Constant:
|
2014-02-23 07:09:12 +00:00
|
|
|
if (const_name(a) != const_name(b))
|
|
|
|
return const_name(a) < const_name(b);
|
|
|
|
else
|
2014-05-16 18:13:50 +00:00
|
|
|
return is_lt(const_levels(a), const_levels(b), use_hash);
|
2013-09-24 19:16:32 +00:00
|
|
|
case expr_kind::App:
|
2014-02-23 07:09:12 +00:00
|
|
|
if (app_fn(a) != app_fn(b))
|
|
|
|
return is_lt(app_fn(a), app_fn(b), use_hash);
|
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
|
|
|
else
|
2014-02-23 07:09:12 +00:00
|
|
|
return is_lt(app_arg(a), app_arg(b), use_hash);
|
2014-04-17 17:52:07 +00:00
|
|
|
case expr_kind::Lambda: case expr_kind::Pi:
|
2014-05-16 18:13:50 +00:00
|
|
|
if (binding_domain(a) != binding_domain(b))
|
|
|
|
return is_lt(binding_domain(a), binding_domain(b), use_hash);
|
2013-09-24 19:16:32 +00:00
|
|
|
else
|
2014-05-16 18:13:50 +00:00
|
|
|
return is_lt(binding_body(a), binding_body(b), use_hash);
|
2014-02-23 07:09:12 +00:00
|
|
|
case expr_kind::Sort:
|
|
|
|
return is_lt(sort_level(a), sort_level(b), use_hash);
|
|
|
|
case expr_kind::Local: case expr_kind::Meta:
|
|
|
|
if (mlocal_name(a) != mlocal_name(b))
|
|
|
|
return mlocal_name(a) < mlocal_name(b);
|
|
|
|
else
|
|
|
|
return is_lt(mlocal_type(a), mlocal_type(b), use_hash);
|
|
|
|
case expr_kind::Macro:
|
2014-04-25 22:02:52 +00:00
|
|
|
if (macro_def(a) != macro_def(b))
|
|
|
|
return macro_def(a) < macro_def(b);
|
|
|
|
if (macro_num_args(a) != macro_num_args(b))
|
|
|
|
return macro_num_args(a) < macro_num_args(b);
|
|
|
|
for (unsigned i = 0; i < macro_num_args(a); i++) {
|
|
|
|
if (macro_arg(a, i) != macro_arg(b, i))
|
|
|
|
return is_lt(macro_arg(a, i), macro_arg(b, i), use_hash);
|
|
|
|
}
|
|
|
|
return false;
|
2013-09-24 19:16:32 +00:00
|
|
|
}
|
2013-11-11 17:19:38 +00:00
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
2013-09-24 19:16:32 +00:00
|
|
|
}
|
|
|
|
}
|