2014-07-02 15:08:35 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include "kernel/type_checker.h"
|
|
|
|
#include "library/num.h"
|
2015-10-12 03:29:31 +00:00
|
|
|
#include "library/util.h"
|
2015-01-24 00:50:32 +00:00
|
|
|
#include "library/constants.h"
|
2014-07-02 15:08:35 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2015-10-11 22:03:00 +00:00
|
|
|
bool has_num_decls(environment const & env) {
|
|
|
|
return
|
|
|
|
env.find(get_zero_name()) &&
|
|
|
|
env.find(get_one_name()) &&
|
|
|
|
env.find(get_bit0_name()) &&
|
|
|
|
env.find(get_bit1_name());
|
|
|
|
}
|
2014-09-23 17:45:14 +00:00
|
|
|
|
2015-10-11 22:03:00 +00:00
|
|
|
static bool is_const_app(expr const & e, name const & n, unsigned nargs) {
|
|
|
|
expr const & f = get_app_fn(e);
|
|
|
|
return is_constant(f) && const_name(f) == n && get_app_num_args(e) == nargs;
|
2014-09-23 17:45:14 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 22:03:00 +00:00
|
|
|
bool is_zero(expr const & e) {
|
|
|
|
return is_const_app(e, get_zero_name(), 2);
|
2014-09-23 17:45:14 +00:00
|
|
|
}
|
2014-07-02 15:08:35 +00:00
|
|
|
|
2015-10-11 22:03:00 +00:00
|
|
|
bool is_one(expr const & e) {
|
|
|
|
return is_const_app(e, get_one_name(), 2);
|
2014-07-02 15:08:35 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 22:03:00 +00:00
|
|
|
optional<expr> is_bit0(expr const & e) {
|
|
|
|
if (!is_const_app(e, get_bit0_name(), 3))
|
|
|
|
return none_expr();
|
|
|
|
return some_expr(app_arg(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<expr> is_bit1(expr const & e) {
|
|
|
|
if (!is_const_app(e, get_bit1_name(), 4))
|
|
|
|
return none_expr();
|
|
|
|
return some_expr(app_arg(e));
|
2014-07-02 15:08:35 +00:00
|
|
|
}
|
|
|
|
|
2015-10-12 03:29:31 +00:00
|
|
|
optional<expr> unfold_num_app(environment const & env, expr const & e) {
|
|
|
|
if (is_zero(e) || is_one(e) || is_bit0(e) || is_bit1(e)) {
|
|
|
|
return unfold_app(env, e);
|
|
|
|
} else {
|
|
|
|
return none_expr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_numeral_const_name(name const & n) {
|
|
|
|
return n == get_zero_name() || n == get_one_name() || n == get_bit0_name() || n == get_bit1_name();
|
|
|
|
}
|
|
|
|
|
2015-10-11 22:03:00 +00:00
|
|
|
static bool is_num(expr const & e, bool first) {
|
|
|
|
if (is_zero(e))
|
|
|
|
return first;
|
|
|
|
else if (is_one(e))
|
|
|
|
return true;
|
|
|
|
else if (auto a = is_bit0(e))
|
|
|
|
return is_num(*a, false);
|
|
|
|
else if (auto a = is_bit1(e))
|
|
|
|
return is_num(*a, false);
|
2014-07-02 15:08:35 +00:00
|
|
|
else
|
2015-10-11 22:03:00 +00:00
|
|
|
return false;
|
2014-07-02 15:08:35 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 22:03:00 +00:00
|
|
|
bool is_num(expr const & e) {
|
|
|
|
return is_num(e, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static optional<mpz> to_num(expr const & e, bool first) {
|
|
|
|
if (is_zero(e)) {
|
|
|
|
return first ? some(mpz(0)) : optional<mpz>();
|
|
|
|
} else if (is_one(e)) {
|
2014-08-14 16:08:39 +00:00
|
|
|
return some(mpz(1));
|
2015-10-11 22:03:00 +00:00
|
|
|
} else if (auto a = is_bit0(e)) {
|
|
|
|
if (auto r = to_num(*a, false))
|
|
|
|
return some(2*(*r));
|
|
|
|
} else if (auto a = is_bit1(e)) {
|
|
|
|
if (auto r = to_num(*a, false))
|
|
|
|
return some(2*(*r)+1);
|
2014-08-14 16:08:39 +00:00
|
|
|
}
|
|
|
|
return optional<mpz>();
|
2014-07-02 15:08:35 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 16:08:39 +00:00
|
|
|
optional<mpz> to_num(expr const & e) {
|
2015-10-11 22:03:00 +00:00
|
|
|
return to_num(e, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<mpz> to_pos_num(expr const & e) {
|
|
|
|
if (is_constant(e, get_pos_num_one_name())) {
|
|
|
|
return some(mpz(1));
|
|
|
|
} else if (is_const_app(e, get_pos_num_bit0_name(), 1)) {
|
|
|
|
if (auto r = to_pos_num(app_arg(e)))
|
|
|
|
return some(2*(*r));
|
|
|
|
} else if (is_const_app(e, get_pos_num_bit1_name(), 1)) {
|
|
|
|
if (auto r = to_pos_num(app_arg(e)))
|
|
|
|
return some(2*(*r) + 1);
|
|
|
|
}
|
|
|
|
return optional<mpz>();
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<mpz> to_num_core(expr const & e) {
|
|
|
|
if (is_constant(e, get_num_zero_name()))
|
2014-08-14 16:08:39 +00:00
|
|
|
return some(mpz(0));
|
2015-10-11 22:03:00 +00:00
|
|
|
else if (is_const_app(e, get_num_pos_name(), 1))
|
2014-08-14 16:08:39 +00:00
|
|
|
return to_pos_num(app_arg(e));
|
2014-07-02 15:08:35 +00:00
|
|
|
else
|
|
|
|
return optional<mpz>();
|
|
|
|
}
|
|
|
|
}
|