2013-07-22 11:03:46 +00:00
|
|
|
/*
|
2014-04-25 22:26:03 +00:00
|
|
|
Copyright (c) 2013-2014 Microsoft Corporation. All rights reserved.
|
2013-07-22 11:03:46 +00:00
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#pragma once
|
2013-09-13 23:14:24 +00:00
|
|
|
#include <algorithm>
|
2013-07-22 11:03:46 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <limits>
|
2013-09-13 10:35:29 +00:00
|
|
|
#include <utility>
|
2013-10-26 18:07:06 +00:00
|
|
|
#include <tuple>
|
2013-12-28 09:16:50 +00:00
|
|
|
#include <string>
|
2013-12-09 22:56:48 +00:00
|
|
|
#include "util/thread.h"
|
2013-11-27 03:15:49 +00:00
|
|
|
#include "util/lua.h"
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/rc.h"
|
|
|
|
#include "util/name.h"
|
|
|
|
#include "util/hash.h"
|
|
|
|
#include "util/buffer.h"
|
2013-09-13 01:25:38 +00:00
|
|
|
#include "util/list_fn.h"
|
2013-12-08 07:21:07 +00:00
|
|
|
#include "util/optional.h"
|
2013-12-28 09:16:50 +00:00
|
|
|
#include "util/serializer.h"
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/sexpr/format.h"
|
|
|
|
#include "kernel/level.h"
|
2014-04-25 22:26:03 +00:00
|
|
|
#include "kernel/extension_context.h"
|
2013-07-22 11:03:46 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-05-12 23:33:22 +00:00
|
|
|
// Tags are used by frontends to mark expressions. They are automatically propagated by
|
|
|
|
// procedures such as update_app, update_binder, etc.
|
|
|
|
typedef unsigned tag;
|
|
|
|
constexpr tag nulltag = std::numeric_limits<unsigned>::max();
|
2014-02-15 03:55:02 +00:00
|
|
|
class expr;
|
2013-07-22 11:03:46 +00:00
|
|
|
/* =======================================
|
|
|
|
Expressions
|
2013-08-03 23:09:21 +00:00
|
|
|
expr ::= Var idx
|
2014-02-15 03:55:02 +00:00
|
|
|
| Sort level
|
|
|
|
| Constant name [levels]
|
|
|
|
| Meta name expr
|
|
|
|
| Local name expr
|
|
|
|
| App expr expr
|
2013-08-03 23:09:21 +00:00
|
|
|
| Lambda name expr expr
|
|
|
|
| Pi name expr expr
|
2014-02-19 17:48:28 +00:00
|
|
|
| Let name expr expr expr
|
2013-07-22 11:03:46 +00:00
|
|
|
|
2014-02-15 03:55:02 +00:00
|
|
|
| Macro macro
|
2013-07-22 11:03:46 +00:00
|
|
|
*/
|
2014-04-17 17:52:07 +00:00
|
|
|
enum class expr_kind { Var, Sort, Constant, Meta, Local, App, Lambda, Pi, Let, Macro };
|
2013-07-22 11:03:46 +00:00
|
|
|
class expr_cell {
|
|
|
|
protected:
|
2013-12-21 21:59:45 +00:00
|
|
|
// The bits of the following field mean:
|
2014-04-22 22:38:18 +00:00
|
|
|
// 0-1 - term is an arrow (0 - not initialized, 1 - is arrow, 2 - is not arrow)
|
2014-02-20 18:07:38 +00:00
|
|
|
// Remark: we use atomic_uchar because these flags are computed lazily (i.e., after the expression is created)
|
|
|
|
atomic_uchar m_flags;
|
2014-05-12 23:33:22 +00:00
|
|
|
unsigned m_kind:8;
|
2014-03-01 01:16:34 +00:00
|
|
|
unsigned m_has_mv:1; // term contains metavariables
|
|
|
|
unsigned m_has_local:1; // term contains local constants
|
|
|
|
unsigned m_has_param_univ:1; // term constains parametric universe levels
|
2014-05-12 23:33:22 +00:00
|
|
|
unsigned m_hash; // hash based on the structure of the expression (this is a good hash for structural equality)
|
|
|
|
unsigned m_hash_alloc; // hash based on 'time' of allocation (this is a good hash for pointer-based equality)
|
|
|
|
atomic_uint m_tag;
|
2013-07-22 11:03:46 +00:00
|
|
|
MK_LEAN_RC(); // Declare m_rc counter
|
|
|
|
void dealloc();
|
2013-07-23 18:31:31 +00:00
|
|
|
|
2013-12-21 21:59:45 +00:00
|
|
|
optional<bool> is_arrow() const;
|
|
|
|
void set_is_arrow(bool flag);
|
|
|
|
friend bool is_arrow(expr const & e);
|
|
|
|
|
2014-04-17 18:31:48 +00:00
|
|
|
static void dec_ref(expr & c, buffer<expr_cell*> & todelete);
|
2013-07-22 11:03:46 +00:00
|
|
|
public:
|
2014-03-01 01:16:34 +00:00
|
|
|
expr_cell(expr_kind k, unsigned h, bool has_mv, bool has_local, bool has_param_univ);
|
2013-07-23 15:59:39 +00:00
|
|
|
expr_kind kind() const { return static_cast<expr_kind>(m_kind); }
|
2013-07-22 11:03:46 +00:00
|
|
|
unsigned hash() const { return m_hash; }
|
2013-11-14 10:31:54 +00:00
|
|
|
unsigned hash_alloc() const { return m_hash_alloc; }
|
2014-02-20 18:07:38 +00:00
|
|
|
bool has_metavar() const { return m_has_mv; }
|
|
|
|
bool has_local() const { return m_has_local; }
|
2014-03-01 01:16:34 +00:00
|
|
|
bool has_param_univ() const { return m_has_param_univ; }
|
2014-05-12 23:33:22 +00:00
|
|
|
void set_tag(tag t);
|
|
|
|
tag get_tag() const { return m_tag; }
|
2013-07-22 11:03:46 +00:00
|
|
|
};
|
2014-02-15 03:55:02 +00:00
|
|
|
|
2014-04-25 22:02:52 +00:00
|
|
|
class macro_definition;
|
2014-05-16 18:13:50 +00:00
|
|
|
class binder_info;
|
2014-02-15 03:55:02 +00:00
|
|
|
|
2013-07-22 11:03:46 +00:00
|
|
|
/**
|
|
|
|
\brief Exprs for encoding formulas/expressions, types and proofs.
|
|
|
|
*/
|
|
|
|
class expr {
|
|
|
|
private:
|
|
|
|
expr_cell * m_ptr;
|
2013-12-08 18:17:29 +00:00
|
|
|
explicit expr(expr_cell * ptr):m_ptr(ptr) { if (m_ptr) m_ptr->inc_ref(); }
|
2013-11-19 02:41:08 +00:00
|
|
|
friend class expr_cell;
|
|
|
|
expr_cell * steal_ptr() { expr_cell * r = m_ptr; m_ptr = nullptr; return r; }
|
2013-12-08 18:17:29 +00:00
|
|
|
friend class optional<expr>;
|
2013-07-22 11:03:46 +00:00
|
|
|
public:
|
2013-12-08 22:45:18 +00:00
|
|
|
/**
|
|
|
|
\brief The default constructor creates a reference to a "dummy"
|
|
|
|
expression. The actual "dummy" expression is not relevant, and
|
|
|
|
no procedure should rely on the kind of expression used.
|
|
|
|
|
|
|
|
We have a default constructor because some collections only work
|
|
|
|
with types that have a default constructor.
|
|
|
|
*/
|
2013-12-08 07:21:07 +00:00
|
|
|
expr();
|
2013-07-22 11:03:46 +00:00
|
|
|
expr(expr const & s):m_ptr(s.m_ptr) { if (m_ptr) m_ptr->inc_ref(); }
|
2013-08-01 20:57:43 +00:00
|
|
|
expr(expr && s):m_ptr(s.m_ptr) { s.m_ptr = nullptr; }
|
2013-07-22 11:03:46 +00:00
|
|
|
~expr() { if (m_ptr) m_ptr->dec_ref(); }
|
|
|
|
|
|
|
|
friend void swap(expr & a, expr & b) { std::swap(a.m_ptr, b.m_ptr); }
|
|
|
|
|
2013-12-13 23:00:30 +00:00
|
|
|
expr & operator=(expr const & s) { LEAN_COPY_REF(s); }
|
|
|
|
expr & operator=(expr && s) { LEAN_MOVE_REF(s); }
|
2013-07-22 11:03:46 +00:00
|
|
|
|
|
|
|
expr_kind kind() const { return m_ptr->kind(); }
|
2013-07-30 08:39:29 +00:00
|
|
|
unsigned hash() const { return m_ptr ? m_ptr->hash() : 23; }
|
2013-11-14 10:31:54 +00:00
|
|
|
unsigned hash_alloc() const { return m_ptr ? m_ptr->hash_alloc() : 23; }
|
2013-09-13 01:25:38 +00:00
|
|
|
bool has_metavar() const { return m_ptr->has_metavar(); }
|
2014-02-20 18:07:38 +00:00
|
|
|
bool has_local() const { return m_ptr->has_local(); }
|
2014-03-01 01:16:34 +00:00
|
|
|
bool has_param_univ() const { return m_ptr->has_param_univ(); }
|
2013-07-22 11:03:46 +00:00
|
|
|
|
2014-05-12 23:33:22 +00:00
|
|
|
void set_tag(tag t) { m_ptr->set_tag(t); }
|
|
|
|
tag get_tag() const { return m_ptr->get_tag(); }
|
|
|
|
|
|
|
|
|
2013-07-22 11:03:46 +00:00
|
|
|
expr_cell * raw() const { return m_ptr; }
|
|
|
|
|
2013-08-06 18:27:14 +00:00
|
|
|
friend expr mk_var(unsigned idx);
|
2014-02-15 03:55:02 +00:00
|
|
|
friend expr mk_sort(level const & l);
|
2014-02-16 17:53:55 +00:00
|
|
|
friend expr mk_constant(name const & n, levels const & ls);
|
2014-05-16 20:08:09 +00:00
|
|
|
friend expr mk_metavar(name const & n, expr const & t);
|
|
|
|
friend expr mk_local(name const & n, name const & pp_n, expr const & t);
|
2014-02-15 03:55:02 +00:00
|
|
|
friend expr mk_app(expr const & f, expr const & a);
|
2014-02-04 00:52:49 +00:00
|
|
|
friend expr mk_pair(expr const & f, expr const & s, expr const & t);
|
2014-02-17 23:21:48 +00:00
|
|
|
friend expr mk_proj(bool fst, expr const & p);
|
2014-05-17 18:37:27 +00:00
|
|
|
friend expr mk_binding(expr_kind k, name const & n, expr const & t, expr const & e, binder_info const & i);
|
2014-02-19 17:48:28 +00:00
|
|
|
friend expr mk_let(name const & n, expr const & t, expr const & v, expr const & e);
|
2014-05-02 01:05:46 +00:00
|
|
|
friend expr mk_macro(macro_definition const & m, unsigned num, expr const * args);
|
2013-07-22 11:03:46 +00:00
|
|
|
|
2013-08-04 16:41:49 +00:00
|
|
|
friend bool is_eqp(expr const & a, expr const & b) { return a.m_ptr == b.m_ptr; }
|
2013-07-23 18:56:15 +00:00
|
|
|
// Overloaded operator() can be used to create applications
|
|
|
|
expr operator()(expr const & a1) const;
|
|
|
|
expr operator()(expr const & a1, expr const & a2) const;
|
|
|
|
expr operator()(expr const & a1, expr const & a2, expr const & a3) const;
|
|
|
|
expr operator()(expr const & a1, expr const & a2, expr const & a3, expr const & a4) const;
|
2013-08-06 18:27:14 +00:00
|
|
|
expr operator()(expr const & a1, expr const & a2, expr const & a3, expr const & a4, expr const & a5) const;
|
|
|
|
expr operator()(expr const & a1, expr const & a2, expr const & a3, expr const & a4, expr const & a5, expr const & a6) const;
|
2014-02-15 03:55:02 +00:00
|
|
|
expr operator()(expr const & a1, expr const & a2, expr const & a3, expr const & a4, expr const & a5, expr const & a6,
|
|
|
|
expr const & a7) const;
|
|
|
|
expr operator()(expr const & a1, expr const & a2, expr const & a3, expr const & a4, expr const & a5, expr const & a6,
|
|
|
|
expr const & a7, expr const & a8) const;
|
2013-07-22 11:03:46 +00:00
|
|
|
};
|
|
|
|
|
2013-12-08 18:17:29 +00:00
|
|
|
// =======================================
|
|
|
|
// Structural equality
|
2014-05-15 00:14:57 +00:00
|
|
|
/** \brief Binder information is ignored in the following predicate */
|
|
|
|
bool operator==(expr const & a, expr const & b);
|
2013-12-08 18:17:29 +00:00
|
|
|
inline bool operator!=(expr const & a, expr const & b) { return !operator==(a, b); }
|
2014-05-15 00:14:57 +00:00
|
|
|
/** \brief Similar to ==, but it also compares binder information */
|
|
|
|
bool is_bi_equal(expr const & a, expr const & b);
|
2013-12-08 18:17:29 +00:00
|
|
|
// =======================================
|
|
|
|
|
|
|
|
SPECIALIZE_OPTIONAL_FOR_SMART_PTR(expr)
|
|
|
|
|
2013-12-08 18:34:38 +00:00
|
|
|
inline optional<expr> none_expr() { return optional<expr>(); }
|
|
|
|
inline optional<expr> some_expr(expr const & e) { return optional<expr>(e); }
|
|
|
|
inline optional<expr> some_expr(expr && e) { return optional<expr>(std::forward<expr>(e)); }
|
|
|
|
|
2013-12-08 18:17:29 +00:00
|
|
|
inline bool is_eqp(optional<expr> const & a, optional<expr> const & b) {
|
|
|
|
return static_cast<bool>(a) == static_cast<bool>(b) && (!a || is_eqp(*a, *b));
|
|
|
|
}
|
|
|
|
|
2014-02-15 03:55:02 +00:00
|
|
|
/** \brief Bounded variables. They are encoded using de Bruijn's indices. */
|
2013-07-22 11:03:46 +00:00
|
|
|
class expr_var : public expr_cell {
|
|
|
|
unsigned m_vidx; // de Bruijn index
|
|
|
|
public:
|
|
|
|
expr_var(unsigned idx);
|
|
|
|
unsigned get_vidx() const { return m_vidx; }
|
|
|
|
};
|
2014-02-15 03:55:02 +00:00
|
|
|
|
|
|
|
/** \brief (parametric) Constants. */
|
2013-07-22 11:03:46 +00:00
|
|
|
class expr_const : public expr_cell {
|
2014-02-16 17:53:55 +00:00
|
|
|
name m_name;
|
|
|
|
levels m_levels;
|
2013-07-22 11:03:46 +00:00
|
|
|
public:
|
2014-02-16 17:53:55 +00:00
|
|
|
expr_const(name const & n, levels const & ls);
|
2013-07-22 11:03:46 +00:00
|
|
|
name const & get_name() const { return m_name; }
|
2014-05-16 18:13:50 +00:00
|
|
|
levels const & get_levels() const { return m_levels; }
|
2014-02-15 03:55:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** \brief Metavariables and local constants */
|
2014-02-16 17:53:55 +00:00
|
|
|
class expr_mlocal : public expr_cell {
|
2014-05-16 22:47:30 +00:00
|
|
|
protected:
|
2014-02-15 03:55:02 +00:00
|
|
|
name m_name;
|
|
|
|
expr m_type;
|
2014-02-16 17:53:55 +00:00
|
|
|
friend expr_cell;
|
|
|
|
void dealloc(buffer<expr_cell*> & todelete);
|
2014-02-15 03:55:02 +00:00
|
|
|
public:
|
2014-02-16 17:53:55 +00:00
|
|
|
expr_mlocal(bool is_meta, name const & n, expr const & t);
|
2014-02-15 03:55:02 +00:00
|
|
|
name const & get_name() const { return m_name; }
|
|
|
|
expr const & get_type() const { return m_type; }
|
2013-07-22 11:03:46 +00:00
|
|
|
};
|
2014-02-15 03:55:02 +00:00
|
|
|
|
2014-05-16 20:08:09 +00:00
|
|
|
/** \brief expr_mlocal subclass for local constants. */
|
|
|
|
class expr_local : public expr_mlocal {
|
|
|
|
// The name used in the binder that generate this local,
|
|
|
|
// it is only used for pretty printing. This field is ignored
|
|
|
|
// when comparing expressions.
|
|
|
|
name m_pp_name;
|
2014-05-16 22:47:30 +00:00
|
|
|
friend expr_cell;
|
|
|
|
void dealloc(buffer<expr_cell*> & todelete);
|
2014-05-16 20:08:09 +00:00
|
|
|
public:
|
|
|
|
expr_local(name const & n, name const & pp_name, expr const & t);
|
|
|
|
name const & get_pp_name() const { return m_pp_name; }
|
|
|
|
};
|
|
|
|
|
2014-02-15 03:55:02 +00:00
|
|
|
/** \brief Composite expressions */
|
|
|
|
class expr_composite : public expr_cell {
|
2014-01-21 01:40:44 +00:00
|
|
|
unsigned m_depth;
|
2014-04-17 18:31:48 +00:00
|
|
|
unsigned m_free_var_range;
|
2014-02-15 03:55:02 +00:00
|
|
|
friend unsigned get_depth(expr const & e);
|
2014-04-17 18:31:48 +00:00
|
|
|
friend unsigned get_free_var_range(expr const & e);
|
2014-02-15 03:55:02 +00:00
|
|
|
public:
|
2014-04-17 18:31:48 +00:00
|
|
|
expr_composite(expr_kind k, unsigned h, bool has_mv, bool has_local, bool has_param_univ, unsigned d, unsigned fv_range);
|
2014-02-15 03:55:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** \brief Applications */
|
|
|
|
class expr_app : public expr_composite {
|
|
|
|
expr m_fn;
|
|
|
|
expr m_arg;
|
2013-11-19 02:41:08 +00:00
|
|
|
friend expr_cell;
|
|
|
|
void dealloc(buffer<expr_cell*> & todelete);
|
2013-07-22 11:03:46 +00:00
|
|
|
public:
|
2014-02-15 03:55:02 +00:00
|
|
|
expr_app(expr const & fn, expr const & arg);
|
|
|
|
expr const & get_fn() const { return m_fn; }
|
|
|
|
expr const & get_arg() const { return m_arg; }
|
2013-07-22 11:03:46 +00:00
|
|
|
};
|
2014-02-15 03:55:02 +00:00
|
|
|
|
2014-05-01 17:35:02 +00:00
|
|
|
/**
|
|
|
|
\brief Auxiliary annotation for binders (Lambda and Pi). This information
|
|
|
|
is only used for elaboration.
|
|
|
|
*/
|
2014-05-16 18:13:50 +00:00
|
|
|
class binder_info {
|
2014-05-14 21:54:27 +00:00
|
|
|
unsigned m_implicit:1; //! if true, binder argument is an implicit argument
|
|
|
|
unsigned m_cast:1; //! if true, binder argument is a target for using cast
|
|
|
|
unsigned m_contextual:1; //! if true, binder argument is assumed to be part of the context, and may be argument for metavariables.
|
2014-05-01 17:35:02 +00:00
|
|
|
public:
|
2014-05-16 18:13:50 +00:00
|
|
|
binder_info(bool implicit = false, bool cast = false, bool contextual = true):
|
2014-05-14 21:54:27 +00:00
|
|
|
m_implicit(implicit), m_cast(cast), m_contextual(contextual) {}
|
2014-05-01 17:35:02 +00:00
|
|
|
bool is_implicit() const { return m_implicit; }
|
|
|
|
bool is_cast() const { return m_cast; }
|
2014-05-14 21:54:27 +00:00
|
|
|
bool is_contextual() const { return m_contextual; }
|
2014-05-01 17:35:02 +00:00
|
|
|
};
|
|
|
|
|
2014-05-16 18:13:50 +00:00
|
|
|
bool operator==(binder_info const & i1, binder_info const & i2);
|
|
|
|
inline bool operator!=(binder_info const & i1, binder_info const & i2) { return !(i1 == i2); }
|
2014-05-15 00:14:57 +00:00
|
|
|
|
2014-05-16 17:40:53 +00:00
|
|
|
class binder {
|
2014-05-16 18:13:50 +00:00
|
|
|
friend class expr_binding;
|
2014-05-16 17:40:53 +00:00
|
|
|
name m_name;
|
|
|
|
expr m_type;
|
2014-05-16 18:52:43 +00:00
|
|
|
binder_info m_info;
|
2014-05-16 17:40:53 +00:00
|
|
|
public:
|
2014-05-16 18:13:50 +00:00
|
|
|
binder(name const & n, expr const & t, binder_info const & bi = binder_info()):
|
2014-05-16 17:40:53 +00:00
|
|
|
m_name(n), m_type(t), m_info(bi) {}
|
|
|
|
name const & get_name() const { return m_name; }
|
|
|
|
expr const & get_type() const { return m_type; }
|
2014-05-16 18:13:50 +00:00
|
|
|
binder_info const & get_info() const { return m_info; }
|
2014-05-16 18:52:43 +00:00
|
|
|
binder update_type(expr const & t) const { return binder(m_name, t, m_info); }
|
2014-05-16 17:40:53 +00:00
|
|
|
};
|
|
|
|
|
2014-05-16 18:13:50 +00:00
|
|
|
/** \brief Lambda and Pi expressions */
|
|
|
|
class expr_binding : public expr_composite {
|
2014-05-16 17:40:53 +00:00
|
|
|
binder m_binder;
|
2014-05-01 17:35:02 +00:00
|
|
|
expr m_body;
|
2013-11-19 02:41:08 +00:00
|
|
|
friend class expr_cell;
|
|
|
|
void dealloc(buffer<expr_cell*> & todelete);
|
2013-07-22 11:03:46 +00:00
|
|
|
public:
|
2014-05-16 18:13:50 +00:00
|
|
|
expr_binding(expr_kind k, name const & n, expr const & t, expr const & e, binder_info const & i = binder_info());
|
2014-05-16 17:40:53 +00:00
|
|
|
name const & get_name() const { return m_binder.get_name(); }
|
|
|
|
expr const & get_domain() const { return m_binder.get_type(); }
|
2013-08-03 18:31:42 +00:00
|
|
|
expr const & get_body() const { return m_body; }
|
2014-05-16 18:13:50 +00:00
|
|
|
binder_info const & get_info() const { return m_binder.get_info(); }
|
|
|
|
binder const & get_binder() const { return m_binder; }
|
2013-07-22 11:03:46 +00:00
|
|
|
};
|
2014-02-15 03:55:02 +00:00
|
|
|
|
2013-08-04 16:37:52 +00:00
|
|
|
/** \brief Let expressions */
|
2014-02-15 03:55:02 +00:00
|
|
|
class expr_let : public expr_composite {
|
2014-02-19 17:48:28 +00:00
|
|
|
name m_name;
|
|
|
|
expr m_type;
|
|
|
|
expr m_value;
|
|
|
|
expr m_body;
|
2013-11-19 02:41:08 +00:00
|
|
|
friend class expr_cell;
|
|
|
|
void dealloc(buffer<expr_cell*> & todelete);
|
2013-08-04 16:37:52 +00:00
|
|
|
public:
|
2014-02-19 17:48:28 +00:00
|
|
|
expr_let(name const & n, expr const & t, expr const & v, expr const & b);
|
2013-08-04 16:37:52 +00:00
|
|
|
~expr_let();
|
2014-02-19 17:48:28 +00:00
|
|
|
name const & get_name() const { return m_name; }
|
|
|
|
expr const & get_type() const { return m_type; }
|
|
|
|
expr const & get_value() const { return m_value; }
|
|
|
|
expr const & get_body() const { return m_body; }
|
2013-08-04 16:37:52 +00:00
|
|
|
};
|
2014-02-15 03:55:02 +00:00
|
|
|
|
|
|
|
/** \brief Sort */
|
|
|
|
class expr_sort : public expr_cell {
|
2013-07-30 02:44:26 +00:00
|
|
|
level m_level;
|
2013-07-22 11:03:46 +00:00
|
|
|
public:
|
2014-02-15 03:55:02 +00:00
|
|
|
expr_sort(level const & l);
|
|
|
|
~expr_sort();
|
2013-07-30 02:44:26 +00:00
|
|
|
level const & get_level() const { return m_level; }
|
2013-07-22 11:03:46 +00:00
|
|
|
};
|
2014-02-15 03:55:02 +00:00
|
|
|
|
|
|
|
class formatter;
|
|
|
|
|
2014-05-02 01:05:46 +00:00
|
|
|
/** \brief Abstract class for macro_definitions */
|
|
|
|
class macro_definition_cell {
|
|
|
|
protected:
|
2013-08-03 23:09:21 +00:00
|
|
|
void dealloc() { delete this; }
|
|
|
|
MK_LEAN_RC();
|
2013-09-24 19:16:32 +00:00
|
|
|
/**
|
2014-05-02 01:05:46 +00:00
|
|
|
\brief Auxiliary method used for implementing a total order on macro
|
|
|
|
attachments. It is invoked by operator<, and it is only invoked when
|
|
|
|
<tt>get_name() == other.get_name()</tt>
|
2013-09-24 19:16:32 +00:00
|
|
|
*/
|
2014-05-02 01:05:46 +00:00
|
|
|
virtual bool lt(macro_definition_cell const &) const;
|
2013-07-22 11:03:46 +00:00
|
|
|
public:
|
2014-05-02 01:05:46 +00:00
|
|
|
macro_definition_cell():m_rc(0) {}
|
|
|
|
virtual ~macro_definition_cell() {}
|
2013-09-04 06:15:37 +00:00
|
|
|
virtual name get_name() const = 0;
|
2014-05-15 17:29:35 +00:00
|
|
|
virtual expr get_type(expr const & m, expr const * arg_types, extension_context & ctx) const = 0;
|
|
|
|
virtual optional<expr> expand(expr const & m, extension_context & ctx) const = 0;
|
|
|
|
virtual optional<expr> expand1(expr const & m, extension_context & ctx) const { return expand(m, ctx); }
|
2014-05-02 01:05:46 +00:00
|
|
|
virtual unsigned trust_level() const;
|
|
|
|
virtual bool operator==(macro_definition_cell const & other) const;
|
2014-02-15 03:55:02 +00:00
|
|
|
virtual void display(std::ostream & out) const;
|
|
|
|
virtual format pp(formatter const & fmt, options const & opts) const;
|
|
|
|
virtual bool is_atomic_pp(bool unicode, bool coercion) const;
|
2013-09-04 06:15:37 +00:00
|
|
|
virtual unsigned hash() const;
|
2013-12-28 09:16:50 +00:00
|
|
|
virtual void write(serializer & s) const = 0;
|
2014-04-25 22:02:52 +00:00
|
|
|
typedef std::function<expr(deserializer&, unsigned, expr const *)> reader;
|
2013-12-28 09:16:50 +00:00
|
|
|
};
|
|
|
|
|
2014-05-02 01:05:46 +00:00
|
|
|
/** \brief Smart pointer for macro definitions */
|
|
|
|
class macro_definition {
|
|
|
|
public:
|
|
|
|
macro_definition_cell * m_ptr;
|
|
|
|
public:
|
|
|
|
explicit macro_definition(macro_definition_cell * ptr);
|
|
|
|
macro_definition(macro_definition const & s);
|
|
|
|
macro_definition(macro_definition && s);
|
|
|
|
~macro_definition();
|
|
|
|
|
|
|
|
macro_definition & operator=(macro_definition const & s);
|
|
|
|
macro_definition & operator=(macro_definition && s);
|
|
|
|
|
|
|
|
name get_name() const { return m_ptr->get_name(); }
|
2014-05-15 17:29:35 +00:00
|
|
|
expr get_type(expr const & m, expr const * arg_types, extension_context & ctx) const { return m_ptr->get_type(m, arg_types, ctx); }
|
|
|
|
optional<expr> expand(expr const & m, extension_context & ctx) const { return m_ptr->expand(m, ctx); }
|
|
|
|
optional<expr> expand1(expr const & m, extension_context & ctx) const { return m_ptr->expand1(m, ctx); }
|
2014-05-02 01:05:46 +00:00
|
|
|
unsigned trust_level() const { return m_ptr->trust_level(); }
|
|
|
|
bool operator==(macro_definition const & other) const { return m_ptr->operator==(*other.m_ptr); }
|
|
|
|
bool operator!=(macro_definition const & other) const { return !operator==(other); }
|
|
|
|
bool operator<(macro_definition const & other) const;
|
|
|
|
void display(std::ostream & out) const { return m_ptr->display(out); }
|
|
|
|
format pp(formatter const & fmt, options const & opts) const { return m_ptr->pp(fmt, opts); }
|
|
|
|
bool is_atomic_pp(bool unicode, bool coercion) const { return m_ptr->is_atomic_pp(unicode, coercion); }
|
|
|
|
unsigned hash() const { return m_ptr->hash(); }
|
|
|
|
void write(serializer & s) const { return m_ptr->write(s); }
|
|
|
|
};
|
|
|
|
|
2014-02-15 03:55:02 +00:00
|
|
|
/** \brief Macro attachments */
|
2014-04-25 22:02:52 +00:00
|
|
|
class expr_macro : public expr_composite {
|
2014-05-02 01:05:46 +00:00
|
|
|
macro_definition m_definition;
|
|
|
|
unsigned m_num_args;
|
|
|
|
expr * m_args;
|
2014-04-25 22:02:52 +00:00
|
|
|
friend class expr_cell;
|
2013-08-03 23:09:21 +00:00
|
|
|
friend expr copy(expr const & a);
|
2014-04-25 22:02:52 +00:00
|
|
|
friend expr update_macro(expr const & e, unsigned num, expr const * args);
|
|
|
|
void dealloc(buffer<expr_cell*> & todelete);
|
2013-08-03 23:09:21 +00:00
|
|
|
public:
|
2014-05-02 01:05:46 +00:00
|
|
|
expr_macro(macro_definition const & v, unsigned num, expr const * args);
|
2014-02-15 03:55:02 +00:00
|
|
|
~expr_macro();
|
2013-09-13 01:25:38 +00:00
|
|
|
|
2014-05-02 01:05:46 +00:00
|
|
|
macro_definition const & get_def() const { return m_definition; }
|
2014-04-25 22:02:52 +00:00
|
|
|
expr const * get_args() const { return m_args; }
|
|
|
|
expr const & get_arg(unsigned idx) const { lean_assert(idx < m_num_args); return m_args[idx]; }
|
|
|
|
unsigned get_num_args() const { return m_num_args; }
|
2013-09-13 01:25:38 +00:00
|
|
|
};
|
2013-07-22 11:03:46 +00:00
|
|
|
|
|
|
|
// =======================================
|
|
|
|
// Testers
|
|
|
|
inline bool is_var(expr_cell * e) { return e->kind() == expr_kind::Var; }
|
|
|
|
inline bool is_constant(expr_cell * e) { return e->kind() == expr_kind::Constant; }
|
2014-02-15 03:55:02 +00:00
|
|
|
inline bool is_local(expr_cell * e) { return e->kind() == expr_kind::Local; }
|
|
|
|
inline bool is_metavar(expr_cell * e) { return e->kind() == expr_kind::Meta; }
|
|
|
|
inline bool is_macro(expr_cell * e) { return e->kind() == expr_kind::Macro; }
|
2013-07-22 11:03:46 +00:00
|
|
|
inline bool is_app(expr_cell * e) { return e->kind() == expr_kind::App; }
|
|
|
|
inline bool is_lambda(expr_cell * e) { return e->kind() == expr_kind::Lambda; }
|
|
|
|
inline bool is_pi(expr_cell * e) { return e->kind() == expr_kind::Pi; }
|
2014-02-15 03:55:02 +00:00
|
|
|
inline bool is_sort(expr_cell * e) { return e->kind() == expr_kind::Sort; }
|
2013-08-04 16:37:52 +00:00
|
|
|
inline bool is_let(expr_cell * e) { return e->kind() == expr_kind::Let; }
|
2014-05-17 18:37:27 +00:00
|
|
|
inline bool is_binding(expr_cell * e) { return is_lambda(e) || is_pi(e); }
|
2014-02-16 17:53:55 +00:00
|
|
|
inline bool is_mlocal(expr_cell * e) { return is_metavar(e) || is_local(e); }
|
2014-02-15 03:55:02 +00:00
|
|
|
|
|
|
|
inline bool is_var(expr const & e) { return e.kind() == expr_kind::Var; }
|
|
|
|
inline bool is_constant(expr const & e) { return e.kind() == expr_kind::Constant; }
|
|
|
|
inline bool is_local(expr const & e) { return e.kind() == expr_kind::Local; }
|
|
|
|
inline bool is_metavar(expr const & e) { return e.kind() == expr_kind::Meta; }
|
|
|
|
inline bool is_macro(expr const & e) { return e.kind() == expr_kind::Macro; }
|
|
|
|
inline bool is_app(expr const & e) { return e.kind() == expr_kind::App; }
|
|
|
|
inline bool is_lambda(expr const & e) { return e.kind() == expr_kind::Lambda; }
|
|
|
|
inline bool is_pi(expr const & e) { return e.kind() == expr_kind::Pi; }
|
|
|
|
inline bool is_sort(expr const & e) { return e.kind() == expr_kind::Sort; }
|
|
|
|
inline bool is_let(expr const & e) { return e.kind() == expr_kind::Let; }
|
2014-05-17 18:37:27 +00:00
|
|
|
inline bool is_binding(expr const & e) { return is_lambda(e) || is_pi(e); }
|
2014-02-16 17:53:55 +00:00
|
|
|
inline bool is_mlocal(expr const & e) { return is_metavar(e) || is_local(e); }
|
2014-02-18 00:10:11 +00:00
|
|
|
|
|
|
|
bool is_atomic(expr const & e);
|
|
|
|
bool is_arrow(expr const & t);
|
2014-04-24 03:16:14 +00:00
|
|
|
/** \brief Return true iff \c e is a metavariable or an application of a metavariable */
|
|
|
|
bool is_meta(expr const & e);
|
2013-07-22 11:03:46 +00:00
|
|
|
// =======================================
|
|
|
|
|
|
|
|
// =======================================
|
|
|
|
// Constructors
|
2013-08-06 18:27:14 +00:00
|
|
|
inline expr mk_var(unsigned idx) { return expr(new expr_var(idx)); }
|
|
|
|
inline expr Var(unsigned idx) { return mk_var(idx); }
|
2014-02-16 17:53:55 +00:00
|
|
|
inline expr mk_constant(name const & n, levels const & ls) { return expr(new expr_const(n, ls)); }
|
|
|
|
inline expr mk_constant(name const & n) { return mk_constant(n, levels()); }
|
2013-08-06 18:27:14 +00:00
|
|
|
inline expr Const(name const & n) { return mk_constant(n); }
|
2014-05-02 01:05:46 +00:00
|
|
|
inline expr mk_macro(macro_definition const & m, unsigned num = 0, expr const * args = nullptr) { return expr(new expr_macro(m, num, args)); }
|
2014-05-16 20:08:09 +00:00
|
|
|
inline expr mk_metavar(name const & n, expr const & t) { return expr(new expr_mlocal(true, n, t)); }
|
|
|
|
inline expr mk_local(name const & n, name const & pp_n, expr const & t) { return expr(new expr_local(n, pp_n, t)); }
|
2014-02-15 03:55:02 +00:00
|
|
|
inline expr mk_app(expr const & f, expr const & a) { return expr(new expr_app(f, a)); }
|
2014-02-22 19:44:57 +00:00
|
|
|
expr mk_app(expr const & f, unsigned num_args, expr const * args);
|
2013-08-06 18:27:14 +00:00
|
|
|
expr mk_app(unsigned num_args, expr const * args);
|
|
|
|
inline expr mk_app(std::initializer_list<expr> const & l) { return mk_app(l.size(), l.begin()); }
|
2013-10-26 21:46:12 +00:00
|
|
|
template<typename T> expr mk_app(T const & args) { return mk_app(args.size(), args.data()); }
|
2014-05-18 20:15:44 +00:00
|
|
|
template<typename T> expr mk_app(expr const & f, T const & args) { return mk_app(f, args.size(), args.data()); }
|
2014-02-22 19:44:57 +00:00
|
|
|
expr mk_rev_app(expr const & f, unsigned num_args, expr const * args);
|
|
|
|
expr mk_rev_app(unsigned num_args, expr const * args);
|
|
|
|
template<typename T> expr mk_rev_app(T const & args) { return mk_rev_app(args.size(), args.data()); }
|
2014-04-23 01:32:50 +00:00
|
|
|
template<typename T> expr mk_rev_app(expr const & f, T const & args) { return mk_rev_app(f, args.size(), args.data()); }
|
2014-05-17 18:37:27 +00:00
|
|
|
inline expr mk_binding(expr_kind k, name const & n, expr const & t, expr const & e, binder_info const & i = binder_info()) {
|
2014-05-16 18:13:50 +00:00
|
|
|
return expr(new expr_binding(k, n, t, e, i));
|
2014-05-01 17:35:02 +00:00
|
|
|
}
|
2014-05-16 18:13:50 +00:00
|
|
|
inline expr mk_lambda(name const & n, expr const & t, expr const & e, binder_info const & i = binder_info()) {
|
2014-05-17 18:37:27 +00:00
|
|
|
return mk_binding(expr_kind::Lambda, n, t, e, i);
|
2014-05-01 17:35:02 +00:00
|
|
|
}
|
2014-05-16 18:13:50 +00:00
|
|
|
inline expr mk_pi(name const & n, expr const & t, expr const & e, binder_info const & i = binder_info()) {
|
2014-05-17 18:37:27 +00:00
|
|
|
return mk_binding(expr_kind::Pi, n, t, e, i);
|
2014-05-01 17:35:02 +00:00
|
|
|
}
|
2014-02-19 17:48:28 +00:00
|
|
|
inline expr mk_let(name const & n, expr const & t, expr const & v, expr const & e) { return expr(new expr_let(n, t, v, e)); }
|
2014-02-15 03:55:02 +00:00
|
|
|
inline expr mk_sort(level const & l) { return expr(new expr_sort(l)); }
|
2014-02-16 17:53:55 +00:00
|
|
|
|
2014-05-14 23:24:55 +00:00
|
|
|
/** \brief Return <tt>Pi(x.{sz-1}, domain[sz-1], ..., Pi(x.{0}, domain[0], range)...)</tt> */
|
|
|
|
expr mk_pi(unsigned sz, expr const * domain, expr const & range);
|
|
|
|
inline expr mk_pi(buffer<expr> const & domain, expr const & range) { return mk_pi(domain.size(), domain.data(), range); }
|
|
|
|
|
2014-02-15 03:55:02 +00:00
|
|
|
expr mk_Bool();
|
|
|
|
expr mk_Type();
|
|
|
|
extern expr Type;
|
|
|
|
extern expr Bool;
|
2013-08-06 18:27:14 +00:00
|
|
|
|
2014-02-16 17:53:55 +00:00
|
|
|
bool is_default_var_name(name const & n);
|
|
|
|
expr mk_arrow(expr const & t, expr const & e);
|
|
|
|
inline expr operator>>(expr const & t, expr const & e) { return mk_arrow(t, e); }
|
|
|
|
|
2014-02-15 03:55:02 +00:00
|
|
|
// Auxiliary
|
|
|
|
inline expr mk_app(expr const & e1, expr const & e2, expr const & e3) { return mk_app({e1, e2, e3}); }
|
|
|
|
inline expr mk_app(expr const & e1, expr const & e2, expr const & e3, expr const & e4) { return mk_app({e1, e2, e3, e4}); }
|
|
|
|
inline expr mk_app(expr const & e1, expr const & e2, expr const & e3, expr const & e4, expr const & e5) {
|
|
|
|
return mk_app({e1, e2, e3, e4, e5});
|
|
|
|
}
|
2013-08-06 18:27:14 +00:00
|
|
|
inline expr expr::operator()(expr const & a1) const { return mk_app({*this, a1}); }
|
|
|
|
inline expr expr::operator()(expr const & a1, expr const & a2) const { return mk_app({*this, a1, a2}); }
|
|
|
|
inline expr expr::operator()(expr const & a1, expr const & a2, expr const & a3) const { return mk_app({*this, a1, a2, a3}); }
|
2014-02-15 03:55:02 +00:00
|
|
|
inline expr expr::operator()(expr const & a1, expr const & a2, expr const & a3, expr const & a4) const {
|
|
|
|
return mk_app({*this, a1, a2, a3, a4});
|
|
|
|
}
|
|
|
|
inline expr expr::operator()(expr const & a1, expr const & a2, expr const & a3, expr const & a4, expr const & a5) const {
|
|
|
|
return mk_app({*this, a1, a2, a3, a4, a5});
|
|
|
|
}
|
|
|
|
inline expr expr::operator()(expr const & a1, expr const & a2, expr const & a3, expr const & a4, expr const & a5,
|
|
|
|
expr const & a6) const {
|
|
|
|
return mk_app({*this, a1, a2, a3, a4, a5, a6});
|
|
|
|
}
|
|
|
|
inline expr expr::operator()(expr const & a1, expr const & a2, expr const & a3, expr const & a4, expr const & a5,
|
|
|
|
expr const & a6, expr const & a7) const {
|
|
|
|
return mk_app({*this, a1, a2, a3, a4, a5, a6, a7});
|
|
|
|
}
|
|
|
|
inline expr expr::operator()(expr const & a1, expr const & a2, expr const & a3, expr const & a4, expr const & a5,
|
|
|
|
expr const & a6, expr const & a7, expr const & a8) const {
|
|
|
|
return mk_app({*this, a1, a2, a3, a4, a5, a6, a7, a8});
|
|
|
|
}
|
2014-04-26 01:17:32 +00:00
|
|
|
|
|
|
|
/** \brief Return application (...((f x_{n-1}) x_{n-2}) ... x_0) */
|
|
|
|
expr mk_app_vars(expr const & f, unsigned n);
|
2013-07-22 11:03:46 +00:00
|
|
|
// =======================================
|
|
|
|
|
|
|
|
// =======================================
|
2013-07-23 15:59:39 +00:00
|
|
|
// Casting (these functions are only needed for low-level code)
|
2014-02-15 03:55:02 +00:00
|
|
|
inline expr_var * to_var(expr_cell * e) { lean_assert(is_var(e)); return static_cast<expr_var*>(e); }
|
|
|
|
inline expr_const * to_constant(expr_cell * e) { lean_assert(is_constant(e)); return static_cast<expr_const*>(e); }
|
|
|
|
inline expr_app * to_app(expr_cell * e) { lean_assert(is_app(e)); return static_cast<expr_app*>(e); }
|
2014-05-17 18:37:27 +00:00
|
|
|
inline expr_binding * to_binding(expr_cell * e) { lean_assert(is_binding(e)); return static_cast<expr_binding*>(e); }
|
2014-02-15 03:55:02 +00:00
|
|
|
inline expr_let * to_let(expr_cell * e) { lean_assert(is_let(e)); return static_cast<expr_let*>(e); }
|
|
|
|
inline expr_sort * to_sort(expr_cell * e) { lean_assert(is_sort(e)); return static_cast<expr_sort*>(e); }
|
2014-02-16 17:53:55 +00:00
|
|
|
inline expr_mlocal * to_mlocal(expr_cell * e) { lean_assert(is_mlocal(e)); return static_cast<expr_mlocal*>(e); }
|
2014-05-16 20:08:09 +00:00
|
|
|
inline expr_local * to_local(expr_cell * e) { lean_assert(is_local(e)); return static_cast<expr_local*>(e); }
|
2014-02-16 17:53:55 +00:00
|
|
|
inline expr_mlocal * to_metavar(expr_cell * e) { lean_assert(is_metavar(e)); return static_cast<expr_mlocal*>(e); }
|
2014-04-25 22:02:52 +00:00
|
|
|
inline expr_macro * to_macro(expr_cell * e) { lean_assert(is_macro(e)); return static_cast<expr_macro*>(e); }
|
2013-07-22 11:03:46 +00:00
|
|
|
|
|
|
|
inline expr_var * to_var(expr const & e) { return to_var(e.raw()); }
|
|
|
|
inline expr_const * to_constant(expr const & e) { return to_constant(e.raw()); }
|
|
|
|
inline expr_app * to_app(expr const & e) { return to_app(e.raw()); }
|
2014-05-16 18:13:50 +00:00
|
|
|
inline expr_binding * to_binding(expr const & e) { return to_binding(e.raw()); }
|
2013-08-04 16:37:52 +00:00
|
|
|
inline expr_let * to_let(expr const & e) { return to_let(e.raw()); }
|
2014-02-15 03:55:02 +00:00
|
|
|
inline expr_sort * to_sort(expr const & e) { return to_sort(e.raw()); }
|
2014-02-16 17:53:55 +00:00
|
|
|
inline expr_mlocal * to_mlocal(expr const & e) { return to_mlocal(e.raw()); }
|
|
|
|
inline expr_mlocal * to_metavar(expr const & e) { return to_metavar(e.raw()); }
|
2014-05-16 20:08:09 +00:00
|
|
|
inline expr_local * to_local(expr const & e) { return to_local(e.raw()); }
|
2014-04-25 22:02:52 +00:00
|
|
|
inline expr_macro * to_macro(expr const & e) { return to_macro(e.raw()); }
|
2013-07-22 11:03:46 +00:00
|
|
|
// =======================================
|
|
|
|
|
2014-02-15 03:55:02 +00:00
|
|
|
|
2013-07-22 11:03:46 +00:00
|
|
|
// =======================================
|
|
|
|
// Accessors
|
2014-04-25 22:02:52 +00:00
|
|
|
inline unsigned get_rc(expr_cell * e) { return e->get_rc(); }
|
|
|
|
inline bool is_shared(expr_cell * e) { return get_rc(e) > 1; }
|
|
|
|
inline unsigned var_idx(expr_cell * e) { return to_var(e)->get_vidx(); }
|
|
|
|
inline bool is_var(expr_cell * e, unsigned i) { return is_var(e) && var_idx(e) == i; }
|
|
|
|
inline name const & const_name(expr_cell * e) { return to_constant(e)->get_name(); }
|
2014-05-16 18:13:50 +00:00
|
|
|
inline levels const & const_levels(expr_cell * e) { return to_constant(e)->get_levels(); }
|
2014-04-25 22:02:52 +00:00
|
|
|
inline macro_definition const & macro_def(expr_cell * e) { return to_macro(e)->get_def(); }
|
|
|
|
inline expr const * macro_args(expr_cell * e) { return to_macro(e)->get_args(); }
|
|
|
|
inline expr const & macro_arg(expr_cell * e, unsigned i) { return to_macro(e)->get_arg(i); }
|
|
|
|
inline unsigned macro_num_args(expr_cell * e) { return to_macro(e)->get_num_args(); }
|
|
|
|
inline expr const & app_fn(expr_cell * e) { return to_app(e)->get_fn(); }
|
|
|
|
inline expr const & app_arg(expr_cell * e) { return to_app(e)->get_arg(); }
|
2014-05-16 18:13:50 +00:00
|
|
|
inline name const & binding_name(expr_cell * e) { return to_binding(e)->get_name(); }
|
|
|
|
inline expr const & binding_domain(expr_cell * e) { return to_binding(e)->get_domain(); }
|
|
|
|
inline expr const & binding_body(expr_cell * e) { return to_binding(e)->get_body(); }
|
|
|
|
inline binder_info const & binding_info(expr_cell * e) { return to_binding(e)->get_info(); }
|
|
|
|
inline binder const & binding_binder(expr_cell * e) { return to_binding(e)->get_binder(); }
|
2014-04-25 22:02:52 +00:00
|
|
|
inline level const & sort_level(expr_cell * e) { return to_sort(e)->get_level(); }
|
|
|
|
inline name const & let_name(expr_cell * e) { return to_let(e)->get_name(); }
|
|
|
|
inline expr const & let_value(expr_cell * e) { return to_let(e)->get_value(); }
|
|
|
|
inline expr const & let_type(expr_cell * e) { return to_let(e)->get_type(); }
|
|
|
|
inline expr const & let_body(expr_cell * e) { return to_let(e)->get_body(); }
|
|
|
|
inline name const & mlocal_name(expr_cell * e) { return to_mlocal(e)->get_name(); }
|
|
|
|
inline expr const & mlocal_type(expr_cell * e) { return to_mlocal(e)->get_type(); }
|
|
|
|
|
|
|
|
inline unsigned get_rc(expr const & e) { return e.raw()->get_rc(); }
|
|
|
|
inline bool is_shared(expr const & e) { return get_rc(e) > 1; }
|
|
|
|
inline unsigned var_idx(expr const & e) { return to_var(e)->get_vidx(); }
|
|
|
|
inline bool is_var(expr const & e, unsigned i) { return is_var(e) && var_idx(e) == i; }
|
|
|
|
inline name const & const_name(expr const & e) { return to_constant(e)->get_name(); }
|
2014-05-16 18:13:50 +00:00
|
|
|
inline levels const & const_levels(expr const & e) { return to_constant(e)->get_levels(); }
|
2014-04-25 22:02:52 +00:00
|
|
|
inline macro_definition const & macro_def(expr const & e) { return to_macro(e)->get_def(); }
|
|
|
|
inline expr const * macro_args(expr const & e) { return to_macro(e)->get_args(); }
|
|
|
|
inline expr const & macro_arg(expr const & e, unsigned i) { return to_macro(e)->get_arg(i); }
|
|
|
|
inline unsigned macro_num_args(expr const & e) { return to_macro(e)->get_num_args(); }
|
|
|
|
inline expr const & app_fn(expr const & e) { return to_app(e)->get_fn(); }
|
|
|
|
inline expr const & app_arg(expr const & e) { return to_app(e)->get_arg(); }
|
2014-05-16 18:13:50 +00:00
|
|
|
inline name const & binding_name(expr const & e) { return to_binding(e)->get_name(); }
|
|
|
|
inline expr const & binding_domain(expr const & e) { return to_binding(e)->get_domain(); }
|
|
|
|
inline expr const & binding_body(expr const & e) { return to_binding(e)->get_body(); }
|
|
|
|
inline binder_info const & binding_info(expr const & e) { return to_binding(e)->get_info(); }
|
|
|
|
inline binder const & binding_binder(expr const & e) { return to_binding(e)->get_binder(); }
|
2014-04-25 22:02:52 +00:00
|
|
|
inline level const & sort_level(expr const & e) { return to_sort(e)->get_level(); }
|
|
|
|
inline name const & let_name(expr const & e) { return to_let(e)->get_name(); }
|
|
|
|
inline expr const & let_value(expr const & e) { return to_let(e)->get_value(); }
|
|
|
|
inline expr const & let_type(expr const & e) { return to_let(e)->get_type(); }
|
|
|
|
inline expr const & let_body(expr const & e) { return to_let(e)->get_body(); }
|
|
|
|
inline name const & mlocal_name(expr const & e) { return to_mlocal(e)->get_name(); }
|
|
|
|
inline expr const & mlocal_type(expr const & e) { return to_mlocal(e)->get_type(); }
|
2014-05-16 20:08:09 +00:00
|
|
|
inline name const & local_pp_name(expr const & e) { return to_local(e)->get_pp_name(); }
|
2014-02-16 17:53:55 +00:00
|
|
|
|
|
|
|
inline bool is_constant(expr const & e, name const & n) { return is_constant(e) && const_name(e) == n; }
|
2013-09-13 01:25:38 +00:00
|
|
|
inline bool has_metavar(expr const & e) { return e.has_metavar(); }
|
2014-02-20 18:07:38 +00:00
|
|
|
inline bool has_local(expr const & e) { return e.has_local(); }
|
2014-03-01 01:16:34 +00:00
|
|
|
inline bool has_param_univ(expr const & e) { return e.has_param_univ(); }
|
2014-02-16 17:53:55 +00:00
|
|
|
unsigned get_depth(expr const & e);
|
2014-04-17 18:31:48 +00:00
|
|
|
/**
|
|
|
|
\brief Return \c R s.t. the de Bruijn index of all free variables
|
|
|
|
occurring in \c e is in the interval <tt>[0, R)</tt>.
|
|
|
|
*/
|
|
|
|
unsigned get_free_var_range(expr const & e);
|
|
|
|
/** \brief Return true iff the given expression has free variables. */
|
|
|
|
inline bool has_free_vars(expr const & e) { return get_free_var_range(e) > 0; }
|
|
|
|
/** \brief Return true iff the given expression does not have free variables. */
|
|
|
|
inline bool closed(expr const & e) { return !has_free_vars(e); }
|
|
|
|
/** \brief Return true iff \c e contains a free variable >= low. */
|
|
|
|
inline bool has_free_var_ge(expr const & e, unsigned low) { return get_free_var_range(e) > low; }
|
2014-04-26 01:17:32 +00:00
|
|
|
/**
|
|
|
|
\brief Given \c e of the form <tt>(...(f a1) ... an)</tt>, store a1 ... an in args.
|
|
|
|
If \c e is not an application, then nothing is stored in args.
|
2014-05-18 02:19:10 +00:00
|
|
|
|
|
|
|
It returns the f.
|
2014-04-26 01:17:32 +00:00
|
|
|
*/
|
2014-05-19 18:11:19 +00:00
|
|
|
expr const & get_app_args(expr const & e, buffer<expr> & args);
|
|
|
|
/**
|
|
|
|
\brief Similar to \c get_app_args, but arguments are stored in reverse order in \c args.
|
|
|
|
If e is of the form <tt>(...(f a1) ... an)</tt>, then the procedure stores [an, ..., a1] in \c args.
|
|
|
|
*/
|
|
|
|
expr const & get_app_rev_args(expr const & e, buffer<expr> & args);
|
|
|
|
/**
|
|
|
|
\brief Given of the form <tt>(...(f a1) ... an)</tt>, return \c f. If \c e is not an application,
|
|
|
|
then return \c e.
|
|
|
|
*/
|
|
|
|
expr const & get_app_fn(expr const & e);
|
2013-07-22 11:03:46 +00:00
|
|
|
// =======================================
|
|
|
|
|
2014-02-16 17:53:55 +00:00
|
|
|
|
2013-07-23 17:16:47 +00:00
|
|
|
// =======================================
|
|
|
|
// Expression+Offset
|
|
|
|
typedef std::pair<expr, unsigned> expr_offset;
|
|
|
|
typedef std::pair<expr_cell*, unsigned> expr_cell_offset;
|
|
|
|
// =======================================
|
|
|
|
|
2013-07-23 17:09:04 +00:00
|
|
|
// =======================================
|
2013-07-24 07:45:38 +00:00
|
|
|
// Auxiliary functionals
|
2013-07-26 18:43:53 +00:00
|
|
|
/** \brief Functional object for hashing kernel expressions. */
|
2013-07-23 17:09:04 +00:00
|
|
|
struct expr_hash { unsigned operator()(expr const & e) const { return e.hash(); } };
|
2013-11-14 10:31:54 +00:00
|
|
|
/**
|
|
|
|
\brief Functional object for hashing (based on allocation time) kernel expressions.
|
|
|
|
|
|
|
|
This hash is compatible with pointer equality.
|
|
|
|
\warning This hash is incompatible with structural equality (i.e., std::equal_to<expr>)
|
|
|
|
*/
|
|
|
|
struct expr_hash_alloc { unsigned operator()(expr const & e) const { return e.hash_alloc(); } };
|
2013-07-26 18:43:53 +00:00
|
|
|
/** \brief Functional object for testing pointer equality between kernel expressions. */
|
2013-08-04 16:41:49 +00:00
|
|
|
struct expr_eqp { bool operator()(expr const & e1, expr const & e2) const { return is_eqp(e1, e2); } };
|
2013-07-26 18:43:53 +00:00
|
|
|
/** \brief Functional object for hashing kernel expression cells. */
|
2013-11-14 10:31:54 +00:00
|
|
|
struct expr_cell_hash { unsigned operator()(expr_cell * e) const { return e->hash_alloc(); } };
|
2013-07-26 18:43:53 +00:00
|
|
|
/** \brief Functional object for testing pointer equality between kernel cell expressions. */
|
2013-07-23 17:09:04 +00:00
|
|
|
struct expr_cell_eqp { bool operator()(expr_cell * e1, expr_cell * e2) const { return e1 == e2; } };
|
2013-09-13 19:49:03 +00:00
|
|
|
/** \brief Functional object for hashing a pair (n, k) where n is a kernel expressions, and k is an offset. */
|
2013-11-14 10:31:54 +00:00
|
|
|
struct expr_offset_hash { unsigned operator()(expr_offset const & p) const { return hash(p.first.hash_alloc(), p.second); } };
|
2013-07-26 18:43:53 +00:00
|
|
|
/** \brief Functional object for comparing pairs (expression, offset). */
|
2013-08-04 16:41:49 +00:00
|
|
|
struct expr_offset_eqp { unsigned operator()(expr_offset const & p1, expr_offset const & p2) const { return is_eqp(p1.first, p2.first) && p1.second == p2.second; } };
|
2013-09-13 19:49:03 +00:00
|
|
|
/** \brief Functional object for hashing a pair (n, k) where n is a kernel cell expressions, and k is an offset. */
|
2013-11-14 10:31:54 +00:00
|
|
|
struct expr_cell_offset_hash { unsigned operator()(expr_cell_offset const & p) const { return hash(p.first->hash_alloc(), p.second); } };
|
2013-07-26 18:43:53 +00:00
|
|
|
/** \brief Functional object for comparing pairs (expression cell, offset). */
|
2013-07-23 17:16:47 +00:00
|
|
|
struct expr_cell_offset_eqp { unsigned operator()(expr_cell_offset const & p1, expr_cell_offset const & p2) const { return p1 == p2; } };
|
2013-07-23 17:09:04 +00:00
|
|
|
// =======================================
|
2013-07-22 11:03:46 +00:00
|
|
|
|
2013-07-30 04:28:22 +00:00
|
|
|
// =======================================
|
|
|
|
// Update
|
2014-02-16 17:53:55 +00:00
|
|
|
expr update_app(expr const & e, expr const & new_fn, expr const & new_arg);
|
2014-02-22 19:44:57 +00:00
|
|
|
expr update_rev_app(expr const & e, unsigned num, expr const * new_args);
|
|
|
|
template<typename C> expr update_rev_app(expr const & e, C const & c) { return update_rev_app(e, c.size(), c.data()); }
|
2014-05-17 18:37:27 +00:00
|
|
|
expr update_binding(expr const & e, expr const & new_domain, expr const & new_body);
|
2014-02-19 17:48:28 +00:00
|
|
|
expr update_let(expr const & e, expr const & new_type, expr const & new_val, expr const & new_body);
|
2014-02-16 17:53:55 +00:00
|
|
|
expr update_mlocal(expr const & e, expr const & new_type);
|
2014-02-25 02:31:38 +00:00
|
|
|
expr update_sort(expr const & e, level const & new_level);
|
|
|
|
expr update_constant(expr const & e, levels const & new_levels);
|
2014-04-25 22:02:52 +00:00
|
|
|
expr update_macro(expr const & e, unsigned num, expr const * args);
|
2013-07-30 04:28:22 +00:00
|
|
|
// =======================================
|
2013-09-26 00:45:13 +00:00
|
|
|
|
|
|
|
std::ostream & operator<<(std::ostream & out, expr const & e);
|
2013-07-22 11:03:46 +00:00
|
|
|
}
|