Add more builtin constants

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2013-08-05 14:23:43 -07:00
parent f6057e2b28
commit 7e2d7dcf3d
4 changed files with 104 additions and 28 deletions

View file

@ -4,7 +4,9 @@ Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include "builtin.h"
#include "arith.h"
#include "environment.h"
namespace lean {
@ -23,16 +25,7 @@ public:
char const * int_type_value::g_kind = "int";
expr int_type() {
static thread_local expr r;
if (!r)
r = to_expr(*(new int_type_value()));
return r;
}
bool is_int_type(expr const & e) {
return is_value(e) && to_value(e).kind() == int_type_value::g_kind;
}
MK_BUILTIN(int_type, int_type_value);
class int_value_value : public value {
mpz m_val;
@ -98,19 +91,57 @@ template<char const * Name, unsigned Hash, typename F> char const * int_bin_op<N
constexpr char int_add_name[] = "+";
struct int_add_eval { mpz operator()(mpz const & v1, mpz const & v2) { return v1 + v2; }; };
typedef int_bin_op<int_add_name, 43, int_add_eval> int_add_value;
expr int_add() { return to_expr(*(new int_add_value())); }
bool is_int_add(expr const & e) { return is_value(e) && to_value(e).kind() == int_add_value::g_kind; }
MK_BUILTIN(int_add, int_add_value);
constexpr char int_sub_name[] = "-";
struct int_sub_eval { mpz operator()(mpz const & v1, mpz const & v2) { return v1 - v2; }; };
typedef int_bin_op<int_sub_name, 43, int_sub_eval> int_sub_value;
expr int_sub() { return to_expr(*(new int_sub_value())); }
bool is_int_sub(expr const & e) { return is_value(e) && to_value(e).kind() == int_sub_value::g_kind; }
typedef int_bin_op<int_sub_name, 47, int_sub_eval> int_sub_value;
MK_BUILTIN(int_sub, int_sub_value);
constexpr char int_mul_name[] = "*";
struct int_mul_eval { mpz operator()(mpz const & v1, mpz const & v2) { return v1 * v2; }; };
typedef int_bin_op<int_mul_name, 43, int_mul_eval> int_mul_value;
expr int_mul() { return to_expr(*(new int_mul_value())); }
bool is_int_mul(expr const & e) { return is_value(e) && to_value(e).kind() == int_mul_value::g_kind; }
typedef int_bin_op<int_mul_name, 53, int_mul_eval> int_mul_value;
MK_BUILTIN(int_mul, int_mul_value);
constexpr char int_div_name[] = "div";
struct int_div_eval { mpz operator()(mpz const & v1, mpz const & v2) { return v1 / v2; }; };
typedef int_bin_op<int_div_name, 61, int_div_eval> int_div_value;
MK_BUILTIN(int_div, int_div_value);
class int_leq_value : public value {
public:
static char const * g_kind;
virtual ~int_leq_value() {}
char const * kind() const { return g_kind; }
virtual expr get_type() const {
static thread_local expr r;
if (!r)
r = arrow(int_type(), arrow(int_type(), bool_type()));
return r;
}
virtual bool operator==(value const & other) const { return other.kind() == kind(); }
virtual bool normalize(unsigned num_args, expr const * args, expr & r) const {
if (num_args == 3 && is_int_value(args[1]) && is_int_value(args[2])) {
r = bool_value(int_value_numeral(args[1]) <= int_value_numeral(args[2]));
return true;
} else {
return false;
}
}
virtual void display(std::ostream & out) const { out << "<="; }
virtual format pp() const { return format("<="); }
virtual unsigned hash() const { return 67; }
};
char const * int_leq_value::g_kind = "<=";
MK_BUILTIN(int_leq, int_leq_value);
MK_CONSTANT(int_geq, name(name("int"), "geq"));
MK_CONSTANT(int_lt, name(name("int"), "lt"));
MK_CONSTANT(int_gt, name(name("int"), "gt"));
void add_int_theory(environment & env) {
expr p = arrow(int_type(), arrow(int_type(), bool_type()));
env.add_definition(int_geq_name_obj, p, lambda("x", int_type(), lambda("y", int_type(), app(int_leq(), var(0), var(1)))));
}
}

View file

@ -26,4 +26,22 @@ bool is_int_sub(expr const & e);
expr int_mul();
bool is_int_mul(expr const & e);
expr int_div();
bool is_int_div(expr const & e);
expr int_leq();
bool is_int_leq(expr const & e);
expr int_geq();
bool is_int_geq(expr const & e);
expr int_lt();
bool is_int_lt(expr const & e);
expr int_gt();
bool is_int_gt(expr const & e);
class environment;
void add_int_theory(environment & env);
}

View file

@ -5,6 +5,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include "builtin.h"
#include "environment.h"
namespace lean {
@ -23,16 +24,7 @@ public:
char const * bool_type_value::g_kind = "bool";
expr bool_type() {
static thread_local expr r;
if (!r)
r = to_expr(*(new bool_type_value()));
return r;
}
bool is_bool_type(expr const & e) {
return is_value(e) && to_value(e).kind() == bool_type_value::g_kind;
}
MK_BUILTIN(bool_type, bool_type_value);
class bool_value_value : public value {
bool m_val;
@ -75,4 +67,6 @@ bool is_false(expr const & e) {
return is_bool_value(e) && !to_bool(e);
}
void add_basic_theory(environment & env) {
}
}

View file

@ -16,4 +16,37 @@ bool is_bool_value(expr const & e);
bool to_bool(expr const & e);
bool is_true(expr const & e);
bool is_false(expr const & e);
class environment;
void add_basic_theory(environment & env);
/**
\brief Helper macro for defining constants such as bool_type, int_type, int_add, etc.
*/
#define MK_BUILTIN(Name, ClassName) \
expr Name() { \
static thread_local expr r; \
if (!r) \
r = to_expr(*(new ClassName())); \
return r; \
} \
bool is_##Name(expr const & e) { \
return is_value(e) && to_value(e).kind() == ClassName::g_kind; \
}
/**
\brief Helper macro for generating "defined" constants.
*/
#define MK_CONSTANT(Name, NameObj) \
static name Name ## _name_obj = NameObj; \
expr Name() { \
static thread_local expr r; \
if (!r) \
r = constant(Name ## _name_obj); \
return r; \
} \
bool is_##Name(expr const & e) { \
return is_constant(e) && const_name(e) == Name ## _name_obj; \
}
}