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
|
|
|
|
*/
|
|
|
|
#pragma once
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/rc.h"
|
|
|
|
#include "kernel/expr.h"
|
2013-08-13 22:13:54 +00:00
|
|
|
/*
|
|
|
|
Kernel objects.
|
|
|
|
|
|
|
|
We use abstract classes and virtual methods because a kernel
|
|
|
|
frontend may need to create new objects for bookkeeping.
|
|
|
|
*/
|
|
|
|
namespace lean {
|
2013-09-04 15:30:04 +00:00
|
|
|
enum class object_kind { UVarDeclaration, Definition, Postulate, Builtin, BuiltinSet, Neutral };
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
class object_cell {
|
|
|
|
void dealloc() { delete this; }
|
|
|
|
MK_LEAN_RC();
|
|
|
|
object_kind m_kind;
|
2013-08-13 22:13:54 +00:00
|
|
|
public:
|
2013-08-16 22:09:26 +00:00
|
|
|
object_cell(object_kind k):m_rc(1), m_kind(k) {}
|
|
|
|
virtual ~object_cell() {}
|
|
|
|
|
|
|
|
object_kind kind() const { return m_kind; }
|
2013-08-13 22:13:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return the keyword used to define this object in a Lean
|
|
|
|
file. The keyword is also used to identify the class of an object.
|
|
|
|
*/
|
|
|
|
virtual char const * keyword() const = 0;
|
|
|
|
|
|
|
|
/** \brief Return true iff object has a name. */
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual bool has_name() const { return false; }
|
2013-08-13 22:13:54 +00:00
|
|
|
/** \brief Return object name. \pre has_name() */
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual name get_name() const { lean_unreachable(); return name(); }
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
/** \brief Has constraint level associated with it (for universal variables). */
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual bool has_cnstr_level() const { return false; }
|
2013-08-16 22:09:26 +00:00
|
|
|
/** \brief Return the constraint level associated with the object. */
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual level get_cnstr_level() const { lean_unreachable(); return level(); }
|
2013-08-16 22:09:26 +00:00
|
|
|
|
2013-08-13 22:13:54 +00:00
|
|
|
/** \brief Return true iff object has a type. */
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual bool has_type() const { return false; }
|
2013-08-13 22:13:54 +00:00
|
|
|
/** \brief Return object type. \pre has_type() */
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual expr get_type() const { lean_unreachable(); return expr(); }
|
2013-08-13 22:13:54 +00:00
|
|
|
|
|
|
|
/** \brief Return true iff object is a definition */
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual bool is_definition() const { return false; }
|
2013-08-13 22:13:54 +00:00
|
|
|
/** \brief Return true iff the definition is opaque. \pre is_definition() */
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual bool is_opaque() const { lean_unreachable(); return false; }
|
2013-08-13 22:13:54 +00:00
|
|
|
/** \brief Return object value. \pre is_definition() */
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual expr get_value() const { lean_unreachable(); return expr(); }
|
2013-08-26 16:04:17 +00:00
|
|
|
|
|
|
|
virtual bool is_axiom() const { return false; }
|
2013-09-04 15:30:04 +00:00
|
|
|
virtual bool is_builtin() const { return false; }
|
|
|
|
virtual bool is_builtin_set() const { return false; }
|
2013-08-26 16:04:17 +00:00
|
|
|
virtual bool is_theorem() const { return false; }
|
|
|
|
virtual bool is_var_decl() const { return false; }
|
2013-09-04 15:30:04 +00:00
|
|
|
|
|
|
|
virtual bool in_builtin_set(expr const & v) const { return false; }
|
2013-08-13 22:13:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-08-16 22:09:26 +00:00
|
|
|
\brief Neutral objects are mainly used for bookkeeping in
|
2013-08-17 01:34:11 +00:00
|
|
|
frontends built on top of the kernel.
|
|
|
|
The kernel does *not* create neutral objects.
|
2013-08-13 22:13:54 +00:00
|
|
|
*/
|
2013-08-16 22:09:26 +00:00
|
|
|
class neutral_object_cell : public object_cell {
|
2013-08-13 22:13:54 +00:00
|
|
|
public:
|
2013-08-16 22:09:26 +00:00
|
|
|
neutral_object_cell();
|
2013-08-13 22:13:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-08-16 22:09:26 +00:00
|
|
|
\brief Environment objects: definitions, theorems, axioms, universe
|
|
|
|
variable declarations, etc.
|
2013-08-13 22:13:54 +00:00
|
|
|
*/
|
2013-08-16 22:09:26 +00:00
|
|
|
class object {
|
|
|
|
object_cell * m_ptr;
|
|
|
|
explicit object(object_cell * ptr):m_ptr(ptr) {}
|
2013-08-13 22:13:54 +00:00
|
|
|
public:
|
2013-08-16 22:09:26 +00:00
|
|
|
object():m_ptr(nullptr) {}
|
|
|
|
object(object const & s):m_ptr(s.m_ptr) { if (m_ptr) m_ptr->inc_ref(); }
|
|
|
|
object(object && s):m_ptr(s.m_ptr) { s.m_ptr = nullptr; }
|
|
|
|
~object() { if (m_ptr) m_ptr->dec_ref(); }
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
static object const & null();
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
friend void swap(object & a, object & b) { std::swap(a.m_ptr, b.m_ptr); }
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
void release() { if (m_ptr) m_ptr->dec_ref(); m_ptr = nullptr; }
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
object & operator=(object const & s) { LEAN_COPY_REF(object, s); }
|
|
|
|
object & operator=(object && s) { LEAN_MOVE_REF(object, s); }
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
operator bool() const { return m_ptr != nullptr; }
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
object_kind kind() const { return m_ptr->kind(); }
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
friend object mk_uvar_decl(name const & n, level const & l);
|
|
|
|
friend object mk_definition(name const & n, expr const & t, expr const & v, bool opaque);
|
|
|
|
friend object mk_theorem(name const & n, expr const & t, expr const & v);
|
|
|
|
friend object mk_axiom(name const & n, expr const & t);
|
|
|
|
friend object mk_var_decl(name const & n, expr const & t);
|
|
|
|
friend object mk_neutral(neutral_object_cell * c);
|
2013-09-04 15:30:04 +00:00
|
|
|
friend object mk_builtin(expr const & v);
|
|
|
|
friend object mk_builtin_set(expr const & r);
|
2013-08-13 22:13:54 +00:00
|
|
|
|
2013-08-16 22:09:26 +00:00
|
|
|
char const * keyword() const { return m_ptr->keyword(); }
|
|
|
|
bool has_name() const { return m_ptr->has_name(); }
|
2013-09-04 15:30:04 +00:00
|
|
|
name get_name() const { return m_ptr->get_name(); }
|
2013-08-16 22:09:26 +00:00
|
|
|
bool has_type() const { return m_ptr->has_type(); }
|
|
|
|
bool has_cnstr_level() const { return m_ptr->has_cnstr_level(); }
|
|
|
|
level get_cnstr_level() const { return m_ptr->get_cnstr_level(); }
|
2013-09-04 15:30:04 +00:00
|
|
|
expr get_type() const { return m_ptr->get_type(); }
|
2013-08-16 22:09:26 +00:00
|
|
|
bool is_definition() const { return m_ptr->is_definition(); }
|
|
|
|
bool is_opaque() const { return m_ptr->is_opaque(); }
|
2013-09-04 15:30:04 +00:00
|
|
|
expr get_value() const { return m_ptr->get_value(); }
|
2013-08-17 01:34:11 +00:00
|
|
|
|
2013-08-26 16:04:17 +00:00
|
|
|
bool is_axiom() const { return m_ptr->is_axiom(); }
|
|
|
|
bool is_theorem() const { return m_ptr->is_theorem(); }
|
|
|
|
bool is_var_decl() const { return m_ptr->is_var_decl(); }
|
2013-09-04 15:30:04 +00:00
|
|
|
bool is_builtin() const { return m_ptr->is_builtin(); }
|
|
|
|
bool is_builtin_set() const { return m_ptr->is_builtin_set(); }
|
|
|
|
bool in_builtin_set(expr const & e) const { return m_ptr->in_builtin_set(e); }
|
2013-08-26 16:04:17 +00:00
|
|
|
|
2013-08-17 01:34:11 +00:00
|
|
|
object_cell const * cell() const { return m_ptr; }
|
2013-08-13 22:13:54 +00:00
|
|
|
};
|
2013-08-16 22:09:26 +00:00
|
|
|
object mk_uvar_decl(name const & n, level const & l);
|
2013-09-04 15:30:04 +00:00
|
|
|
object mk_builtin(expr const & v);
|
|
|
|
object mk_builtin_set(expr const & r);
|
2013-08-16 22:09:26 +00:00
|
|
|
object mk_definition(name const & n, expr const & t, expr const & v, bool opaque);
|
|
|
|
object mk_theorem(name const & n, expr const & t, expr const & v);
|
|
|
|
object mk_axiom(name const & n, expr const & t);
|
|
|
|
object mk_var_decl(name const & n, expr const & t);
|
|
|
|
inline object mk_neutral(neutral_object_cell * c) { lean_assert(c->get_rc() == 1); return object(c); }
|
2013-08-13 22:13:54 +00:00
|
|
|
}
|