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>
|
2013-09-13 23:14:24 +00:00
|
|
|
#include <algorithm>
|
2014-02-23 22:41:44 +00:00
|
|
|
#include <utility>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/name.h"
|
2014-02-14 02:02:14 +00:00
|
|
|
#include "util/optional.h"
|
2014-02-23 22:41:44 +00:00
|
|
|
#include "util/list.h"
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/sexpr/format.h"
|
|
|
|
#include "util/sexpr/options.h"
|
2013-07-29 05:34:39 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
class environment;
|
|
|
|
struct level_cell;
|
2014-02-14 02:02:14 +00:00
|
|
|
/**
|
|
|
|
\brief Universe level kinds.
|
|
|
|
|
2014-07-22 16:43:18 +00:00
|
|
|
- Zero : It is also Prop level if env.impredicative() is true
|
2014-02-14 02:02:14 +00:00
|
|
|
- Succ(l) : successor level
|
|
|
|
- Max(l1, l2) : maximum of two levels
|
|
|
|
- IMax(l1, l2) : IMax(x, zero) = zero for all x
|
|
|
|
IMax(x, succ(y)) = Max(x, succ(y)) for all x, y
|
|
|
|
|
|
|
|
We use IMax to handle Pi-types, and Max for Sigma-types.
|
|
|
|
Their definitions "mirror" the typing rules for Pi and Sigma.
|
|
|
|
|
2014-02-24 19:05:42 +00:00
|
|
|
- Param(n) : A parameter. In Lean, we have universe polymorphic definitions.
|
2014-05-06 23:13:29 +00:00
|
|
|
- Global(n) : A global level.
|
2014-02-24 19:05:42 +00:00
|
|
|
- Meta(n) : Placeholder. It is the equivalent of a metavariable for universe levels.
|
2014-02-14 02:02:14 +00:00
|
|
|
The elaborator is responsible for replacing Meta with level expressions
|
|
|
|
that do not contain Meta.
|
|
|
|
*/
|
2014-05-06 23:13:29 +00:00
|
|
|
enum class level_kind { Zero, Succ, Max, IMax, Param, Global, Meta };
|
2014-02-14 02:02:14 +00:00
|
|
|
|
2013-07-29 05:34:39 +00:00
|
|
|
/**
|
|
|
|
\brief Universe level.
|
|
|
|
*/
|
|
|
|
class level {
|
|
|
|
friend class environment;
|
|
|
|
level_cell * m_ptr;
|
2014-02-14 02:02:14 +00:00
|
|
|
friend level_cell const & to_cell(level const & l);
|
2014-02-25 02:31:38 +00:00
|
|
|
friend class optional<level>;
|
2013-07-29 05:34:39 +00:00
|
|
|
public:
|
2014-02-14 02:02:14 +00:00
|
|
|
/** \brief Universe zero */
|
2013-07-29 05:34:39 +00:00
|
|
|
level();
|
2014-02-14 02:02:14 +00:00
|
|
|
level(level_cell * ptr);
|
2013-07-29 05:34:39 +00:00
|
|
|
level(level const & l);
|
|
|
|
level(level&& s);
|
|
|
|
~level();
|
|
|
|
|
2014-02-24 19:05:42 +00:00
|
|
|
level_kind kind() const;
|
|
|
|
unsigned hash() const;
|
|
|
|
|
2013-07-29 05:34:39 +00:00
|
|
|
level & operator=(level const & l);
|
|
|
|
level & operator=(level&& l);
|
|
|
|
|
2014-02-14 02:02:14 +00:00
|
|
|
friend bool is_eqp(level const & l1, level const & l2) { return l1.m_ptr == l2.m_ptr; }
|
2013-07-29 05:34:39 +00:00
|
|
|
|
2014-02-14 02:02:14 +00:00
|
|
|
friend void swap(level & l1, level & l2) { std::swap(l1, l2); }
|
2013-12-28 07:30:13 +00:00
|
|
|
|
|
|
|
struct ptr_hash { unsigned operator()(level const & n) const { return std::hash<level_cell*>()(n.m_ptr); } };
|
|
|
|
struct ptr_eq { bool operator()(level const & n1, level const & n2) const { return n1.m_ptr == n2.m_ptr; } };
|
2013-07-29 05:34:39 +00:00
|
|
|
};
|
2013-08-02 01:52:28 +00:00
|
|
|
|
2014-02-25 02:31:38 +00:00
|
|
|
bool operator==(level const & l1, level const & l2);
|
|
|
|
inline bool operator!=(level const & l1, level const & l2) { return !operator==(l1, l2); }
|
|
|
|
|
2015-05-05 01:05:00 +00:00
|
|
|
struct level_hash { unsigned operator()(level const & n) const { return n.hash(); } };
|
|
|
|
struct level_eq { bool operator()(level const & n1, level const & n2) const { return n1 == n2; } };
|
|
|
|
|
2014-02-25 02:31:38 +00:00
|
|
|
SPECIALIZE_OPTIONAL_FOR_SMART_PTR(level)
|
|
|
|
|
|
|
|
inline optional<level> none_level() { return optional<level>(); }
|
|
|
|
inline optional<level> some_level(level const & e) { return optional<level>(e); }
|
|
|
|
inline optional<level> some_level(level && e) { return optional<level>(std::forward<level>(e)); }
|
|
|
|
|
2014-02-14 02:02:14 +00:00
|
|
|
level const & mk_level_zero();
|
|
|
|
level const & mk_level_one();
|
|
|
|
level mk_max(level const & l1, level const & l2);
|
|
|
|
level mk_imax(level const & l1, level const & l2);
|
|
|
|
level mk_succ(level const & l);
|
2014-02-24 19:05:42 +00:00
|
|
|
level mk_param_univ(name const & n);
|
2014-05-06 23:13:29 +00:00
|
|
|
level mk_global_univ(name const & n);
|
2014-02-24 19:05:42 +00:00
|
|
|
level mk_meta_univ(name const & n);
|
2013-08-02 01:52:28 +00:00
|
|
|
|
2014-11-13 01:28:33 +00:00
|
|
|
/** \brief Convert (succ^k l) into (l, k). If l is not a succ, then return (l, 0) */
|
|
|
|
pair<level, unsigned> to_offset(level l);
|
|
|
|
|
2014-02-24 19:05:42 +00:00
|
|
|
inline unsigned hash(level const & l) { return l.hash(); }
|
|
|
|
inline level_kind kind(level const & l) { return l.kind(); }
|
2014-05-06 23:13:29 +00:00
|
|
|
inline bool is_zero(level const & l) { return kind(l) == level_kind::Zero; }
|
|
|
|
inline bool is_param(level const & l) { return kind(l) == level_kind::Param; }
|
|
|
|
inline bool is_global(level const & l) { return kind(l) == level_kind::Global; }
|
|
|
|
inline bool is_meta(level const & l) { return kind(l) == level_kind::Meta; }
|
|
|
|
inline bool is_succ(level const & l) { return kind(l) == level_kind::Succ; }
|
|
|
|
inline bool is_max(level const & l) { return kind(l) == level_kind::Max; }
|
|
|
|
inline bool is_imax(level const & l) { return kind(l) == level_kind::IMax; }
|
2014-05-12 21:42:01 +00:00
|
|
|
bool is_one(level const & l);
|
2014-02-14 02:02:14 +00:00
|
|
|
|
|
|
|
unsigned get_depth(level const & l);
|
|
|
|
|
|
|
|
level const & max_lhs(level const & l);
|
|
|
|
level const & max_rhs(level const & l);
|
|
|
|
level const & imax_lhs(level const & l);
|
|
|
|
level const & imax_rhs(level const & l);
|
|
|
|
level const & succ_of(level const & l);
|
2014-02-24 19:05:42 +00:00
|
|
|
name const & param_id(level const & l);
|
2014-05-06 23:13:29 +00:00
|
|
|
name const & global_id(level const & l);
|
2014-02-24 19:05:42 +00:00
|
|
|
name const & meta_id(level const & l);
|
2014-06-01 04:44:12 +00:00
|
|
|
name const & level_id(level const & l);
|
2014-02-14 02:02:14 +00:00
|
|
|
/**
|
|
|
|
\brief Return true iff \c l is an explicit level.
|
|
|
|
We say a level l is explicit iff
|
|
|
|
1) l is zero OR
|
|
|
|
2) l = succ(l') and l' is explicit
|
|
|
|
*/
|
|
|
|
bool is_explicit(level const & l);
|
2014-10-02 17:22:19 +00:00
|
|
|
/** \brief Convert an explicit universe into a unsigned integer.
|
|
|
|
\pre is_explicit(l)
|
|
|
|
*/
|
|
|
|
unsigned to_explicit(level const & l);
|
2014-02-24 19:05:42 +00:00
|
|
|
/** \brief Return true iff \c l contains placeholder (aka meta parameters). */
|
|
|
|
bool has_meta(level const & l);
|
2014-05-06 23:13:29 +00:00
|
|
|
/** \brief Return true iff \c l contains globals */
|
|
|
|
bool has_global(level const & l);
|
2014-02-24 19:05:42 +00:00
|
|
|
/** \brief Return true iff \c l contains parameters */
|
|
|
|
bool has_param(level const & l);
|
2014-02-14 02:02:14 +00:00
|
|
|
|
2014-02-25 02:31:38 +00:00
|
|
|
/**
|
|
|
|
\brief Return a new level expression based on <tt>l == succ(arg)</tt>, where \c arg is replaced with
|
|
|
|
\c new_arg.
|
|
|
|
|
|
|
|
\pre is_succ(l)
|
|
|
|
*/
|
|
|
|
level update_succ(level const & l, level const & new_arg);
|
|
|
|
/**
|
|
|
|
\brief Return a new level expression based on <tt>l == max(lhs, rhs)</tt>, where \c lhs is replaced with
|
|
|
|
\c new_lhs and \c rhs is replaced with \c new_rhs.
|
|
|
|
|
|
|
|
\pre is_max(l) || is_imax(l)
|
|
|
|
*/
|
|
|
|
level update_max(level const & l, level const & new_lhs, level const & new_rhs);
|
|
|
|
|
2014-02-23 18:35:49 +00:00
|
|
|
/**
|
2014-05-12 01:05:02 +00:00
|
|
|
\brief Return true if lhs and rhs denote the same level.
|
|
|
|
The check is done by normalization.
|
2014-02-23 18:35:49 +00:00
|
|
|
*/
|
2014-05-12 01:05:02 +00:00
|
|
|
bool is_equivalent(level const & lhs, level const & rhs);
|
|
|
|
/** \brief Return the given level expression normal form */
|
|
|
|
level normalize(level const & l);
|
2014-02-23 18:35:49 +00:00
|
|
|
|
2014-05-17 20:19:38 +00:00
|
|
|
/**
|
|
|
|
\brief If the result is true, then forall assignments \c A that assigns all parameters, globals and metavariables occuring
|
|
|
|
in \c l1 and \l2, we have that the universe level l1[A] is bigger or equal to l2[A].
|
|
|
|
|
|
|
|
\remark This function assumes l1 and l2 are normalized
|
|
|
|
*/
|
|
|
|
bool is_geq_core(level l1, level l2);
|
|
|
|
|
|
|
|
bool is_geq(level const & l1, level const & l2);
|
|
|
|
|
|
|
|
|
2014-02-23 22:41:44 +00:00
|
|
|
typedef list<level> levels;
|
|
|
|
|
2014-03-01 01:16:34 +00:00
|
|
|
bool has_meta(levels const & ls);
|
2014-05-06 23:13:29 +00:00
|
|
|
bool has_global(levels const & ls);
|
2014-03-01 01:16:34 +00:00
|
|
|
bool has_param(levels const & ls);
|
|
|
|
|
2014-05-12 01:05:02 +00:00
|
|
|
/** \brief An arbitrary (monotonic) total order on universe level terms. */
|
|
|
|
bool is_lt(level const & l1, level const & l2, bool use_hash);
|
|
|
|
bool is_lt(levels const & as, levels const & bs, bool use_hash);
|
2014-06-12 19:29:04 +00:00
|
|
|
struct level_quick_cmp { int operator()(level const & l1, level const & l2) const { return is_lt(l1, l2, true) ? -1 : (l1 == l2 ? 0 : 1); } };
|
2014-05-12 01:05:02 +00:00
|
|
|
|
2014-05-07 23:07:31 +00:00
|
|
|
/** \brief Functional for applying <tt>F</tt> to each level expressions. */
|
|
|
|
class for_each_level_fn {
|
|
|
|
std::function<bool(level const &)> m_f; // NOLINT
|
|
|
|
void apply(level const & l);
|
|
|
|
public:
|
|
|
|
template<typename F> for_each_level_fn(F const & f):m_f(f) {}
|
|
|
|
void operator()(level const & l) { return apply(l); }
|
|
|
|
};
|
|
|
|
template<typename F> void for_each(level const & l, F const & f) { return for_each_level_fn(f)(l); }
|
2014-02-25 02:31:38 +00:00
|
|
|
|
2014-05-07 23:07:31 +00:00
|
|
|
/** \brief Functional for applying <tt>F</tt> to the level expressions. */
|
2014-02-25 02:31:38 +00:00
|
|
|
class replace_level_fn {
|
|
|
|
std::function<optional<level>(level const &)> m_f;
|
|
|
|
level apply(level const & l);
|
|
|
|
public:
|
|
|
|
template<typename F> replace_level_fn(F const & f):m_f(f) {}
|
|
|
|
level operator()(level const & l) { return apply(l); }
|
|
|
|
};
|
2014-05-07 23:07:31 +00:00
|
|
|
template<typename F> level replace(level const & l, F const & f) { return replace_level_fn(f)(l); }
|
|
|
|
|
2014-05-16 18:13:50 +00:00
|
|
|
typedef list<name> level_param_names;
|
2014-02-25 02:31:38 +00:00
|
|
|
|
2014-05-07 23:07:31 +00:00
|
|
|
/** \brief If \c l contains a global that is not in \c env, then return it. Otherwise, return none. */
|
|
|
|
optional<name> get_undef_global(level const & l, environment const & env);
|
|
|
|
|
|
|
|
/** \brief If \c l contains a parameter that is not in \c ps, then return it. Otherwise, return none. */
|
2014-05-16 18:13:50 +00:00
|
|
|
optional<name> get_undef_param(level const & l, level_param_names const & ps);
|
2014-05-07 23:07:31 +00:00
|
|
|
|
2014-02-14 02:02:14 +00:00
|
|
|
/**
|
2014-02-25 02:31:38 +00:00
|
|
|
\brief Instantiate the universe level parameters \c ps occurring in \c l with the levels \c ls.
|
|
|
|
\pre length(ps) == length(ls)
|
2014-02-14 02:02:14 +00:00
|
|
|
*/
|
2014-05-16 18:13:50 +00:00
|
|
|
level instantiate(level const & l, level_param_names const & ps, levels const & ls);
|
2014-02-25 02:31:38 +00:00
|
|
|
|
|
|
|
/** \brief Printer for debugging purposes */
|
2014-02-14 02:02:14 +00:00
|
|
|
std::ostream & operator<<(std::ostream & out, level const & l);
|
|
|
|
|
|
|
|
/**
|
2014-05-17 20:19:38 +00:00
|
|
|
\brief If the result is true, then forall assignments \c A that assigns all parameters, globals and metavariables occuring
|
|
|
|
in \c l, l[A] != zero.
|
2014-02-14 02:02:14 +00:00
|
|
|
*/
|
|
|
|
bool is_not_zero(level const & l);
|
2013-12-28 07:30:13 +00:00
|
|
|
|
2014-02-14 02:02:14 +00:00
|
|
|
/** \brief Pretty print the given level expression, unicode characters are used if \c unicode is \c true. */
|
|
|
|
format pp(level l, bool unicode, unsigned indent);
|
|
|
|
/** \brief Pretty print the given level expression using the given configuration options. */
|
|
|
|
format pp(level const & l, options const & opts = options());
|
2014-04-25 00:44:43 +00:00
|
|
|
|
|
|
|
/** \brief Pretty print lhs <= rhs, unicode characters are used if \c unicode is \c true. */
|
|
|
|
format pp(level const & lhs, level const & rhs, bool unicode, unsigned indent);
|
|
|
|
/** \brief Pretty print lhs <= rhs using the given configuration options. */
|
|
|
|
format pp(level const & lhs, level const & rhs, options const & opts = options());
|
2014-06-02 23:17:04 +00:00
|
|
|
/** \brief Convert a list of universe level parameter names into a list of levels. */
|
|
|
|
levels param_names_to_levels(level_param_names const & ps);
|
2014-09-23 19:09:13 +00:00
|
|
|
|
|
|
|
void initialize_level();
|
|
|
|
void finalize_level();
|
2013-07-29 05:34:39 +00:00
|
|
|
}
|
2013-08-19 01:25:34 +00:00
|
|
|
void print(lean::level const & l);
|