2013-08-13 22:13:54 +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
|
|
|
|
*/
|
2013-12-28 22:39:10 +00:00
|
|
|
#include <string>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "kernel/object.h"
|
|
|
|
#include "kernel/environment.h"
|
2013-08-13 22:13:54 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-12-28 22:39:10 +00:00
|
|
|
typedef std::unordered_map<std::string, object_cell::reader> object_readers;
|
|
|
|
static std::unique_ptr<object_readers> g_object_readers;
|
|
|
|
object_readers & get_object_readers() {
|
|
|
|
if (!g_object_readers)
|
|
|
|
g_object_readers.reset(new object_readers());
|
|
|
|
return *(g_object_readers.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void object_cell::register_deserializer(std::string const & k, reader rd) {
|
|
|
|
object_readers & readers = get_object_readers();
|
|
|
|
lean_assert(readers.find(k) == readers.end());
|
|
|
|
readers[k] = rd;
|
|
|
|
}
|
2013-12-29 01:31:35 +00:00
|
|
|
void read_object(environment const & env, io_state const & ios, std::string const & k, deserializer & d) {
|
2013-12-28 22:39:10 +00:00
|
|
|
object_readers & readers = get_object_readers();
|
|
|
|
auto it = readers.find(k);
|
|
|
|
lean_assert(it != readers.end());
|
|
|
|
it->second(env, ios, d);
|
|
|
|
}
|
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
neutral_object_cell::neutral_object_cell():object_cell(object_kind::Neutral) {}
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
/**
|
|
|
|
\brief Named kernel objects.
|
|
|
|
|
|
|
|
\remark All nonneutral objects have names.
|
|
|
|
*/
|
|
|
|
class named_object_cell : public object_cell {
|
|
|
|
name m_name;
|
|
|
|
public:
|
|
|
|
named_object_cell(object_kind k, name const & n):object_cell(k), m_name(n) {}
|
|
|
|
virtual ~named_object_cell() {}
|
|
|
|
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual bool has_name() const { return true; }
|
|
|
|
virtual name get_name() const { return m_name; }
|
2013-08-16 22:09:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-01-07 00:46:11 +00:00
|
|
|
\brief Universe variable constraint.
|
2013-08-16 22:09:26 +00:00
|
|
|
*/
|
2014-01-07 00:46:11 +00:00
|
|
|
class uvar_constraint_object_cell : public named_object_cell {
|
2013-08-16 22:09:26 +00:00
|
|
|
level m_level;
|
|
|
|
public:
|
2014-01-07 00:46:11 +00:00
|
|
|
uvar_constraint_object_cell(name const & n, level const & l):
|
|
|
|
named_object_cell(object_kind::UVarConstraint, n), m_level(l) {}
|
|
|
|
virtual ~uvar_constraint_object_cell() {}
|
2013-08-16 22:09:26 +00:00
|
|
|
|
2013-12-20 18:45:44 +00:00
|
|
|
virtual bool has_cnstr_level() const { return true; }
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual level get_cnstr_level() const { return m_level; }
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual char const * keyword() const { return "universe"; }
|
|
|
|
virtual void write(serializer & s) const { s << "universe" << get_name() << m_level; }
|
2013-09-04 15:30:04 +00:00
|
|
|
};
|
2014-01-07 00:46:11 +00:00
|
|
|
static void read_uvar_cnstr(environment const & env, io_state const &, deserializer & d) {
|
2013-12-28 22:39:10 +00:00
|
|
|
name n = read_name(d);
|
|
|
|
level lvl = read_level(d);
|
2014-01-07 00:46:11 +00:00
|
|
|
env->add_uvar_cnstr(n, lvl);
|
2013-12-28 22:39:10 +00:00
|
|
|
}
|
2014-01-07 00:46:11 +00:00
|
|
|
static object_cell::register_deserializer_fn uvar_ds("universe", read_uvar_cnstr);
|
2013-08-16 22:09:26 +00:00
|
|
|
|
2013-09-04 15:30:04 +00:00
|
|
|
/**
|
2013-12-20 18:45:44 +00:00
|
|
|
\brief Builtin object.
|
2013-09-04 15:30:04 +00:00
|
|
|
*/
|
|
|
|
class builtin_object_cell : public object_cell {
|
|
|
|
expr m_value;
|
2013-12-20 18:45:44 +00:00
|
|
|
bool m_opaque;
|
2013-09-04 15:30:04 +00:00
|
|
|
public:
|
|
|
|
builtin_object_cell(expr const & v):
|
2013-12-20 18:45:44 +00:00
|
|
|
object_cell(object_kind::Builtin), m_value(v), m_opaque(false) { lean_assert(is_value(v)); }
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual ~builtin_object_cell() {}
|
2013-12-20 18:45:44 +00:00
|
|
|
virtual bool has_name() const { return true; }
|
|
|
|
virtual name get_name() const { return to_value(m_value).get_name(); }
|
|
|
|
virtual bool has_type() const { return true; }
|
|
|
|
virtual expr get_type() const { return to_value(m_value).get_type(); }
|
|
|
|
virtual bool is_definition() const { return true; }
|
|
|
|
virtual bool is_opaque() const { return m_opaque; }
|
|
|
|
virtual void set_opaque(bool f) { m_opaque = f; }
|
|
|
|
virtual expr get_value() const { return m_value; }
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual char const * keyword() const { return "builtin"; }
|
2013-12-20 18:45:44 +00:00
|
|
|
virtual bool is_builtin() const { return true; }
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual void write(serializer & s) const { s << "builtin" << m_value; }
|
2013-09-04 15:30:04 +00:00
|
|
|
};
|
2013-12-28 22:39:10 +00:00
|
|
|
static void read_builtin(environment const & env, io_state const &, deserializer & d) {
|
|
|
|
expr v = read_expr(d);
|
|
|
|
env->add_builtin(v);
|
|
|
|
}
|
2014-01-05 20:05:08 +00:00
|
|
|
static object_cell::register_deserializer_fn builtin_ds("builtin", read_builtin);
|
2013-12-28 22:39:10 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
|
2013-09-04 15:30:04 +00:00
|
|
|
/**
|
|
|
|
\brief Base class for capturing a set of builtin objects such as
|
|
|
|
a) the natural numbers 0, 1, 2, ...
|
|
|
|
b) the integers 0, -1, 1, -2, 2, ...
|
|
|
|
c) the reals
|
|
|
|
d) ...
|
|
|
|
This object represents an infinite set of declarations.
|
|
|
|
This is just a markup to sign that an environment depends on a
|
|
|
|
particular builtin set of values.
|
|
|
|
*/
|
|
|
|
class builtin_set_object_cell : public object_cell {
|
|
|
|
// The representative is only used to test if a builtin value
|
|
|
|
// is in the same C++ class of the representative.
|
|
|
|
expr m_representative;
|
|
|
|
public:
|
|
|
|
builtin_set_object_cell(expr const & r):object_cell(object_kind::BuiltinSet), m_representative(r) { lean_assert(is_value(r)); }
|
|
|
|
virtual ~builtin_set_object_cell() {}
|
|
|
|
virtual bool has_name() const { return true; }
|
|
|
|
virtual name get_name() const { return to_value(m_representative).get_name(); }
|
|
|
|
virtual bool is_builtin_set() const { return true; }
|
|
|
|
virtual bool in_builtin_set(expr const & v) const { return is_value(v) && typeid(to_value(v)) == typeid(to_value(m_representative)); }
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual char const * keyword() const { return "builtinset"; }
|
|
|
|
virtual void write(serializer & s) const { s << "builtinset" << m_representative; }
|
2013-08-16 22:09:26 +00:00
|
|
|
};
|
2013-12-28 22:39:10 +00:00
|
|
|
static void read_builtin_set(environment const & env, io_state const &, deserializer & d) {
|
|
|
|
env->add_builtin_set(read_expr(d));
|
|
|
|
}
|
2014-01-05 20:05:08 +00:00
|
|
|
static object_cell::register_deserializer_fn builtin_set_ds("builtinset", read_builtin_set);
|
2013-08-16 22:09:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Named (and typed) kernel objects.
|
|
|
|
*/
|
|
|
|
class named_typed_object_cell : public named_object_cell {
|
|
|
|
expr m_type;
|
|
|
|
public:
|
|
|
|
named_typed_object_cell(object_kind k, name const & n, expr const & t):
|
|
|
|
named_object_cell(k, n), m_type(t) {}
|
|
|
|
virtual ~named_typed_object_cell() {}
|
|
|
|
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual bool has_type() const { return true; }
|
|
|
|
virtual expr get_type() const { return m_type; }
|
2013-08-16 22:09:26 +00:00
|
|
|
};
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
/**
|
|
|
|
\brief Base class for Axioms and Variable declarations.
|
|
|
|
*/
|
|
|
|
class postulate_object_cell : public named_typed_object_cell {
|
|
|
|
public:
|
|
|
|
postulate_object_cell(name const & n, expr const & t):
|
|
|
|
named_typed_object_cell(object_kind::Postulate, n, t) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Axioms
|
|
|
|
*/
|
|
|
|
class axiom_object_cell : public postulate_object_cell {
|
|
|
|
public:
|
|
|
|
axiom_object_cell(name const & n, expr const & t):postulate_object_cell(n, t) {}
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual char const * keyword() const { return "axiom"; }
|
2013-08-26 16:04:17 +00:00
|
|
|
virtual bool is_axiom() const { return true; }
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual void write(serializer & s) const { s << "ax" << get_name() << get_type(); }
|
2013-08-16 22:09:26 +00:00
|
|
|
};
|
2013-12-28 22:39:10 +00:00
|
|
|
static void read_axiom(environment const & env, io_state const &, deserializer & d) {
|
|
|
|
name n = read_name(d);
|
|
|
|
expr t = read_expr(d);
|
|
|
|
env->add_axiom(n, t);
|
|
|
|
}
|
2014-01-05 20:05:08 +00:00
|
|
|
static object_cell::register_deserializer_fn axiom_ds("ax", read_axiom);
|
2013-12-28 22:39:10 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Variable (aka constant) declaration
|
|
|
|
*/
|
|
|
|
class variable_decl_object_cell : public postulate_object_cell {
|
|
|
|
public:
|
|
|
|
variable_decl_object_cell(name const & n, expr const & t):postulate_object_cell(n, t) {}
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual char const * keyword() const { return "variable"; }
|
2013-08-26 16:04:17 +00:00
|
|
|
virtual bool is_var_decl() const { return true; }
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual void write(serializer & s) const { s << "var" << get_name() << get_type(); }
|
2013-08-16 22:09:26 +00:00
|
|
|
};
|
2013-12-28 22:39:10 +00:00
|
|
|
static void read_variable(environment const & env, io_state const &, deserializer & d) {
|
|
|
|
name n = read_name(d);
|
|
|
|
expr t = read_expr(d);
|
|
|
|
env->add_var(n, t);
|
|
|
|
}
|
2014-01-05 20:05:08 +00:00
|
|
|
static object_cell::register_deserializer_fn var_decl_ds("var", read_variable);
|
2013-08-16 22:09:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Base class for definitions: theorems and definitions.
|
|
|
|
*/
|
|
|
|
class definition_object_cell : public named_typed_object_cell {
|
2013-10-15 21:50:08 +00:00
|
|
|
expr m_value;
|
|
|
|
bool m_opaque;
|
|
|
|
unsigned m_weight;
|
2013-08-16 22:09:26 +00:00
|
|
|
public:
|
2013-12-28 22:39:10 +00:00
|
|
|
definition_object_cell(name const & n, expr const & t, expr const & v, unsigned weight):
|
2013-12-29 05:57:56 +00:00
|
|
|
named_typed_object_cell(object_kind::Definition, n, t), m_value(v), m_opaque(false), m_weight(weight) {}
|
2013-08-16 22:09:26 +00:00
|
|
|
virtual ~definition_object_cell() {}
|
|
|
|
|
2013-10-15 21:50:08 +00:00
|
|
|
virtual bool is_definition() const { return true; }
|
|
|
|
virtual bool is_opaque() const { return m_opaque; }
|
2013-12-20 18:45:44 +00:00
|
|
|
virtual void set_opaque(bool f) { m_opaque = f; }
|
2013-10-15 21:50:08 +00:00
|
|
|
virtual expr get_value() const { return m_value; }
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual char const * keyword() const { return "definition"; }
|
2013-10-15 21:50:08 +00:00
|
|
|
virtual unsigned get_weight() const { return m_weight; }
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual void write(serializer & s) const { s << "def" << get_name() << get_type() << get_value(); }
|
2013-08-16 22:09:26 +00:00
|
|
|
};
|
2013-12-28 22:39:10 +00:00
|
|
|
static void read_definition(environment const & env, io_state const &, deserializer & d) {
|
|
|
|
name n = read_name(d);
|
|
|
|
expr t = read_expr(d);
|
|
|
|
expr v = read_expr(d);
|
|
|
|
env->add_definition(n, t, v);
|
|
|
|
}
|
2014-01-05 20:05:08 +00:00
|
|
|
static object_cell::register_deserializer_fn definition_ds("def", read_definition);
|
2013-08-16 22:09:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Theorems
|
|
|
|
*/
|
|
|
|
class theorem_object_cell : public definition_object_cell {
|
|
|
|
public:
|
|
|
|
theorem_object_cell(name const & n, expr const & t, expr const & v):
|
2013-12-28 22:39:10 +00:00
|
|
|
definition_object_cell(n, t, v, 0) {
|
|
|
|
set_opaque(true);
|
|
|
|
}
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual char const * keyword() const { return "theorem"; }
|
2013-08-26 16:04:17 +00:00
|
|
|
virtual bool is_theorem() const { return true; }
|
2014-01-05 20:05:08 +00:00
|
|
|
virtual void write(serializer & s) const { s << "th" << get_name() << get_type() << get_value(); }
|
2013-08-16 22:09:26 +00:00
|
|
|
};
|
2013-12-28 22:39:10 +00:00
|
|
|
static void read_theorem(environment const & env, io_state const &, deserializer & d) {
|
|
|
|
name n = read_name(d);
|
|
|
|
expr t = read_expr(d);
|
|
|
|
expr v = read_expr(d);
|
|
|
|
env->add_theorem(n, t, v);
|
|
|
|
}
|
2014-01-05 20:05:08 +00:00
|
|
|
static object_cell::register_deserializer_fn theorem_ds("th", read_theorem);
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2014-01-07 00:46:11 +00:00
|
|
|
object mk_uvar_cnstr(name const & n, level const & l) { return object(new uvar_constraint_object_cell(n, l)); }
|
2013-12-28 22:39:10 +00:00
|
|
|
object mk_definition(name const & n, expr const & t, expr const & v, unsigned weight) { return object(new definition_object_cell(n, t, v, weight)); }
|
2013-08-16 22:09:26 +00:00
|
|
|
object mk_theorem(name const & n, expr const & t, expr const & v) { return object(new theorem_object_cell(n, t, v)); }
|
|
|
|
object mk_axiom(name const & n, expr const & t) { return object(new axiom_object_cell(n, t)); }
|
|
|
|
object mk_var_decl(name const & n, expr const & t) { return object(new variable_decl_object_cell(n, t)); }
|
2013-09-04 15:30:04 +00:00
|
|
|
object mk_builtin(expr const & v) { return object(new builtin_object_cell(v)); }
|
|
|
|
object mk_builtin_set(expr const & r) { return object(new builtin_set_object_cell(r)); }
|
2013-08-16 22:09:26 +00:00
|
|
|
}
|