2013-07-30 07:25:19 +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 10:35:29 +00:00
|
|
|
#include <utility>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/list.h"
|
|
|
|
#include "kernel/expr.h"
|
2013-07-30 07:25:19 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-09-07 18:11:45 +00:00
|
|
|
/**
|
|
|
|
\brief An element of the Lean context.
|
|
|
|
\see context
|
|
|
|
*/
|
2013-07-30 07:25:19 +00:00
|
|
|
class context_entry {
|
2013-07-30 08:39:29 +00:00
|
|
|
name m_name;
|
2013-08-14 19:11:08 +00:00
|
|
|
expr m_domain;
|
2013-07-30 08:39:29 +00:00
|
|
|
expr m_body;
|
2013-09-07 18:11:45 +00:00
|
|
|
public:
|
2013-08-14 19:11:08 +00:00
|
|
|
context_entry(name const & n, expr const & d, expr const & b):m_name(n), m_domain(d), m_body(b) {}
|
|
|
|
context_entry(name const & n, expr const & d):m_name(n), m_domain(d) {}
|
|
|
|
name const & get_name() const { return m_name; }
|
|
|
|
expr const & get_domain() const { return m_domain; }
|
|
|
|
expr const & get_body() const { return m_body; }
|
2013-07-30 07:25:19 +00:00
|
|
|
};
|
2013-09-07 18:11:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief A context is essentially a mapping from free-variables to types (and definition/body).
|
|
|
|
*/
|
|
|
|
class context {
|
|
|
|
list<context_entry> m_list;
|
|
|
|
explicit context(list<context_entry> const & l):m_list(l) {}
|
|
|
|
public:
|
|
|
|
context() {}
|
2013-09-13 19:49:03 +00:00
|
|
|
context(context const & c, name const & n, expr const & d):m_list(context_entry(n, d), c.m_list) {}
|
|
|
|
context(context const & c, name const & n, expr const & d, expr const & b):m_list(context_entry(n, d, b), c.m_list) {}
|
2013-09-07 18:11:45 +00:00
|
|
|
context_entry const & lookup(unsigned vidx) const;
|
|
|
|
std::pair<context_entry const &, context> lookup_ext(unsigned vidx) const;
|
2013-09-13 01:25:38 +00:00
|
|
|
bool empty() const { return is_nil(m_list); }
|
2013-10-02 00:25:17 +00:00
|
|
|
operator bool() const { return !empty(); }
|
2013-09-07 18:11:45 +00:00
|
|
|
unsigned size() const { return length(m_list); }
|
|
|
|
typedef list<context_entry>::iterator iterator;
|
|
|
|
iterator begin() const { return m_list.begin(); }
|
|
|
|
iterator end() const { return m_list.end(); }
|
|
|
|
friend bool is_eqp(context const & c1, context const & c2) { return is_eqp(c1.m_list, c2.m_list); }
|
2013-10-22 22:52:24 +00:00
|
|
|
/**
|
|
|
|
\brief Return a new context where the entries at positions [s, s+n) were removed.
|
|
|
|
|
|
|
|
\pre size() >= s + n
|
|
|
|
*/
|
|
|
|
context remove(unsigned s, unsigned n) const;
|
2013-09-07 18:11:45 +00:00
|
|
|
};
|
|
|
|
|
2013-08-14 21:42:48 +00:00
|
|
|
/**
|
|
|
|
\brief Return the context entry for the free variable with de
|
|
|
|
Bruijn index \c i, and the context for this entry.
|
|
|
|
*/
|
2013-09-07 18:11:45 +00:00
|
|
|
inline std::pair<context_entry const &, context> lookup_ext(context const & c, unsigned i) { return c.lookup_ext(i); }
|
2013-08-14 21:42:48 +00:00
|
|
|
/**
|
|
|
|
\brief Return the context entry for the free variable with de
|
|
|
|
Bruijn index \c i.
|
|
|
|
*/
|
2013-09-07 18:11:45 +00:00
|
|
|
inline context_entry const & lookup(context const & c, unsigned i) { return c.lookup(i); }
|
|
|
|
inline context extend(context const & c, name const & n, expr const & d, expr const & b) { return context(c, n, d, b); }
|
|
|
|
inline context extend(context const & c, name const & n, expr const & d) { return context(c, n, d); }
|
2013-09-13 01:25:38 +00:00
|
|
|
inline bool empty(context const & c) { return c.empty(); }
|
2013-09-26 00:45:13 +00:00
|
|
|
|
|
|
|
std::ostream & operator<<(std::ostream & out, context const & ctx);
|
2013-07-30 07:25:19 +00:00
|
|
|
}
|