2013-07-29 05:34:39 +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
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
2013-08-05 01:26:01 +00:00
|
|
|
#include "expr.h"
|
2013-07-29 05:34:39 +00:00
|
|
|
#include "level.h"
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
/**
|
|
|
|
\brief Lean environment for defining constants, inductive
|
|
|
|
datatypes, universe variables, et.c
|
|
|
|
*/
|
|
|
|
class environment {
|
2013-08-08 23:12:46 +00:00
|
|
|
public:
|
|
|
|
class object;
|
|
|
|
private:
|
2013-07-29 05:34:39 +00:00
|
|
|
struct imp;
|
2013-08-04 23:07:37 +00:00
|
|
|
std::shared_ptr<imp> m_imp;
|
2013-08-06 19:24:20 +00:00
|
|
|
void check_type(name const & n, expr const & t, expr const & v);
|
2013-08-04 23:07:37 +00:00
|
|
|
explicit environment(std::shared_ptr<imp> const & ptr);
|
|
|
|
explicit environment(imp * new_ptr);
|
2013-08-08 23:12:46 +00:00
|
|
|
unsigned get_num_objects() const;
|
|
|
|
object const & get_object(unsigned i) const;
|
2013-07-29 05:34:39 +00:00
|
|
|
public:
|
|
|
|
environment();
|
|
|
|
~environment();
|
|
|
|
|
2013-08-09 22:33:34 +00:00
|
|
|
// =======================================
|
|
|
|
// Parent/Child environment management
|
2013-07-29 05:34:39 +00:00
|
|
|
/**
|
2013-08-09 22:33:34 +00:00
|
|
|
\brief Create a child environment. This environment will only allow "read-only" operations until
|
|
|
|
all children environments are deleted.
|
|
|
|
*/
|
|
|
|
environment mk_child() const;
|
|
|
|
|
|
|
|
/** \brief Return true iff this environment has children environments. */
|
|
|
|
bool has_children() const;
|
|
|
|
|
|
|
|
/** \brief Return true iff this environment has a parent environment. */
|
|
|
|
bool has_parent() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return parent environment of this environment.
|
|
|
|
\pre has_parent()
|
|
|
|
*/
|
|
|
|
environment parent() const;
|
|
|
|
// =======================================
|
|
|
|
|
|
|
|
// =======================================
|
|
|
|
// Universe variables
|
|
|
|
/**
|
|
|
|
\brief Add a new universe variable with name \c n and constraint <tt>n >= l</tt>.
|
2013-07-29 05:34:39 +00:00
|
|
|
Return the new variable.
|
|
|
|
|
|
|
|
\remark An exception is thrown if a universe inconsistency is detected.
|
|
|
|
*/
|
2013-08-09 22:33:34 +00:00
|
|
|
level add_uvar(name const & n, level const & l);
|
|
|
|
level add_uvar(name const & n) { return add_uvar(n, level()); }
|
2013-07-29 05:34:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return true iff the constraint l1 >= l2 is implied by the constraints
|
|
|
|
in the environment.
|
|
|
|
*/
|
2013-07-30 02:05:43 +00:00
|
|
|
bool is_ge(level const & l1, level const & l2) const;
|
2013-07-29 05:34:39 +00:00
|
|
|
|
2013-07-30 02:44:26 +00:00
|
|
|
/** \brief Display universal variables, and their constraints */
|
2013-07-29 05:34:39 +00:00
|
|
|
void display_uvars(std::ostream & out) const;
|
2013-08-04 23:07:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return universal variable with the given name.
|
|
|
|
Throw an exception if variable is not defined in this environment.
|
|
|
|
*/
|
|
|
|
level get_uvar(name const & n) const;
|
2013-08-09 22:33:34 +00:00
|
|
|
// =======================================
|
2013-08-04 23:07:37 +00:00
|
|
|
|
2013-08-09 22:33:34 +00:00
|
|
|
// =======================================
|
|
|
|
// Environment Objects
|
2013-08-06 09:03:22 +00:00
|
|
|
enum class object_kind { Definition, Theorem, Var, Axiom };
|
2013-08-05 03:52:14 +00:00
|
|
|
/**
|
|
|
|
\brief Base class for environment objects
|
|
|
|
It is just a place holder at this point.
|
|
|
|
*/
|
|
|
|
class object {
|
2013-08-08 02:10:12 +00:00
|
|
|
protected:
|
2013-08-05 03:52:14 +00:00
|
|
|
public:
|
|
|
|
object() {}
|
|
|
|
object(object const & o) = delete;
|
|
|
|
object & operator=(object const & o) = delete;
|
|
|
|
|
|
|
|
virtual ~object() {}
|
|
|
|
virtual object_kind kind() const = 0;
|
|
|
|
virtual void display(std::ostream & out) const = 0;
|
2013-08-08 02:10:12 +00:00
|
|
|
virtual format pp(environment const &) const = 0;
|
2013-08-05 03:52:14 +00:00
|
|
|
virtual expr const & get_type() const = 0;
|
2013-08-08 23:12:46 +00:00
|
|
|
virtual char const * header() const = 0;
|
2013-08-05 03:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class definition : public object {
|
|
|
|
name m_name;
|
|
|
|
expr m_type;
|
|
|
|
expr m_value;
|
|
|
|
bool m_opaque;
|
|
|
|
public:
|
|
|
|
definition(name const & n, expr const & t, expr const & v, bool opaque);
|
|
|
|
virtual ~definition();
|
2013-08-06 03:06:07 +00:00
|
|
|
virtual object_kind kind() const { return object_kind::Definition; }
|
2013-08-05 03:52:14 +00:00
|
|
|
name const & get_name() const { return m_name; }
|
|
|
|
virtual expr const & get_type() const { return m_type; }
|
|
|
|
expr const & get_value() const { return m_value; }
|
|
|
|
bool is_opaque() const { return m_opaque; }
|
|
|
|
virtual void display(std::ostream & out) const;
|
2013-08-08 02:10:12 +00:00
|
|
|
virtual format pp(environment const & env) const;
|
2013-08-08 23:12:46 +00:00
|
|
|
virtual char const * header() const { return "Definition"; }
|
2013-08-05 03:52:14 +00:00
|
|
|
};
|
|
|
|
|
2013-08-06 09:03:22 +00:00
|
|
|
class theorem : public definition {
|
|
|
|
public:
|
|
|
|
theorem(name const & n, expr const & t, expr const & v):definition(n, t, v, true) {}
|
|
|
|
virtual object_kind kind() const { return object_kind::Theorem; }
|
2013-08-08 23:12:46 +00:00
|
|
|
virtual char const * header() const { return "Theorem"; }
|
2013-08-06 09:03:22 +00:00
|
|
|
};
|
|
|
|
|
2013-08-05 03:52:14 +00:00
|
|
|
class fact : public object {
|
2013-08-06 03:06:07 +00:00
|
|
|
protected:
|
2013-08-05 03:52:14 +00:00
|
|
|
name m_name;
|
|
|
|
expr m_type;
|
|
|
|
public:
|
|
|
|
fact(name const & n, expr const & t);
|
|
|
|
virtual ~fact();
|
|
|
|
name const & get_name() const { return m_name; }
|
|
|
|
virtual expr const & get_type() const { return m_type; }
|
|
|
|
virtual void display(std::ostream & out) const;
|
2013-08-08 02:10:12 +00:00
|
|
|
virtual format pp(environment const &) const;
|
2013-08-05 03:52:14 +00:00
|
|
|
};
|
|
|
|
|
2013-08-06 03:06:07 +00:00
|
|
|
class axiom : public fact {
|
|
|
|
public:
|
|
|
|
axiom(name const & n, expr const & t):fact(n, t) {}
|
|
|
|
virtual object_kind kind() const { return object_kind::Axiom; }
|
2013-08-08 23:12:46 +00:00
|
|
|
virtual char const * header() const { return "Axiom"; }
|
2013-08-06 03:06:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class variable : public fact {
|
|
|
|
public:
|
|
|
|
variable(name const & n, expr const & t):fact(n, t) {}
|
|
|
|
virtual object_kind kind() const { return object_kind::Var; }
|
2013-08-08 23:12:46 +00:00
|
|
|
virtual char const * header() const { return "Variable"; }
|
2013-08-06 03:06:07 +00:00
|
|
|
};
|
|
|
|
|
2013-08-05 03:52:14 +00:00
|
|
|
friend bool is_definition(object const & o) { return o.kind() == object_kind::Definition; }
|
2013-08-06 03:06:07 +00:00
|
|
|
friend bool is_axiom(object const & o) { return o.kind() == object_kind::Axiom; }
|
|
|
|
friend bool is_var(object const & o) { return o.kind() == object_kind::Var; }
|
|
|
|
friend bool is_fact(object const & o) { return is_axiom(o) || is_var(o); }
|
2013-08-05 03:52:14 +00:00
|
|
|
|
|
|
|
friend definition const & to_definition(object const & o) { lean_assert(is_definition(o)); return static_cast<definition const &>(o); }
|
|
|
|
friend fact const & to_fact(object const & o) { lean_assert(is_fact(o)); return static_cast<fact const &>(o); }
|
|
|
|
|
2013-08-05 01:26:01 +00:00
|
|
|
/**
|
|
|
|
\brief Add a new definition n : t := v.
|
|
|
|
It throws an exception if v does not have type t.
|
2013-08-05 03:52:14 +00:00
|
|
|
It throws an exception if there is already an object with the given name.
|
2013-08-05 01:26:01 +00:00
|
|
|
If opaque == true, then definition is not used by normalizer.
|
|
|
|
*/
|
|
|
|
void add_definition(name const & n, expr const & t, expr const & v, bool opaque = false);
|
2013-08-06 19:24:20 +00:00
|
|
|
void add_theorem(name const & n, expr const & t, expr const & v);
|
2013-08-05 01:26:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Add a new definition n : infer_type(v) := v.
|
2013-08-05 03:52:14 +00:00
|
|
|
It throws an exception if there is already an object with the given name.
|
2013-08-05 01:26:01 +00:00
|
|
|
If opaque == true, then definition is not used by normalizer.
|
|
|
|
*/
|
|
|
|
void add_definition(name const & n, expr const & v, bool opaque = false);
|
|
|
|
|
|
|
|
/**
|
2013-08-06 03:06:07 +00:00
|
|
|
\brief Add a new fact (Axiom or Fact) to the environment.
|
2013-08-05 03:52:14 +00:00
|
|
|
It throws an exception if there is already an object with the given name.
|
2013-08-05 01:26:01 +00:00
|
|
|
*/
|
2013-08-06 03:06:07 +00:00
|
|
|
void add_axiom(name const & n, expr const & t);
|
|
|
|
void add_var(name const & n, expr const & t);
|
2013-08-05 03:52:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return the object with the given name.
|
|
|
|
It throws an exception if the environment does not have an object with the given name.
|
|
|
|
*/
|
|
|
|
object const & get_object(name const & n) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return the object with the given name.
|
|
|
|
Return nullptr if there is no object with the given name.
|
|
|
|
*/
|
|
|
|
object const * get_object_ptr(name const & n) const;
|
2013-08-06 03:06:07 +00:00
|
|
|
|
2013-08-08 23:12:46 +00:00
|
|
|
/** \brief Iterator for Lean environment objects. */
|
|
|
|
class object_iterator {
|
|
|
|
environment const & m_env;
|
|
|
|
unsigned m_idx;
|
|
|
|
friend class environment;
|
|
|
|
object_iterator(environment const & env, unsigned idx):m_env(env), m_idx(idx) {}
|
|
|
|
public:
|
|
|
|
object_iterator(object_iterator const & s):m_env(s.m_env), m_idx(s.m_idx) {}
|
|
|
|
object_iterator & operator++() { ++m_idx; return *this; }
|
|
|
|
object_iterator operator++(int) { object_iterator tmp(*this); operator++(); return tmp; }
|
|
|
|
bool operator==(object_iterator const & s) const { lean_assert(&m_env == &(s.m_env)); return m_idx == s.m_idx; }
|
|
|
|
bool operator!=(object_iterator const & s) const { return !operator==(s); }
|
|
|
|
object const & operator*() { return m_env.get_object(m_idx); }
|
|
|
|
};
|
|
|
|
|
|
|
|
/** \brief Return an iterator to the beginning of the sequence of objects stored in this environment */
|
|
|
|
object_iterator begin_objects() const { return object_iterator(*this, 0); }
|
|
|
|
|
|
|
|
/** \brief Return an iterator to the end of the sequence of objects stored in this environment */
|
|
|
|
object_iterator end_objects() const { return object_iterator(*this, get_num_objects()); }
|
2013-08-09 22:33:34 +00:00
|
|
|
// =======================================
|
2013-08-08 23:12:46 +00:00
|
|
|
|
2013-08-09 22:33:34 +00:00
|
|
|
// =======================================
|
|
|
|
// Pretty printing
|
2013-08-06 03:06:07 +00:00
|
|
|
/** \brief Display all objects stored in the environment */
|
|
|
|
void display_objects(std::ostream & out) const;
|
|
|
|
|
|
|
|
/** \brief Display universal variable constraints and objects stored in this environment and its parents. */
|
|
|
|
void display(std::ostream & out) const;
|
2013-08-09 22:33:34 +00:00
|
|
|
// =======================================
|
2013-07-29 05:34:39 +00:00
|
|
|
};
|
2013-08-06 03:06:07 +00:00
|
|
|
inline std::ostream & operator<<(std::ostream & out, environment const & env) { env.display(out); return out; }
|
2013-07-29 05:34:39 +00:00
|
|
|
}
|