2013-07-30 08:39:29 +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
|
|
|
|
*/
|
2013-09-13 10:35:29 +00:00
|
|
|
#include <utility>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/exception.h"
|
|
|
|
#include "kernel/context.h"
|
2014-01-01 11:02:41 +00:00
|
|
|
#include "kernel/metavar.h"
|
|
|
|
#include "kernel/free_vars.h"
|
2013-07-30 08:39:29 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-12-12 17:56:54 +00:00
|
|
|
context::context(std::initializer_list<std::pair<char const *, expr const &>> const & l) {
|
|
|
|
for (auto const & p : l)
|
|
|
|
m_list = cons(context_entry(name(p.first), p.second), m_list);
|
|
|
|
}
|
|
|
|
|
2013-09-07 18:11:45 +00:00
|
|
|
std::pair<context_entry const &, context> context::lookup_ext(unsigned i) const {
|
|
|
|
list<context_entry> const * it1 = &m_list;
|
2013-07-30 08:39:29 +00:00
|
|
|
while (*it1) {
|
|
|
|
if (i == 0)
|
2013-09-07 18:11:45 +00:00
|
|
|
return std::pair<context_entry const &, context>(head(*it1), context(tail(*it1)));
|
2013-08-14 21:42:48 +00:00
|
|
|
--i;
|
|
|
|
it1 = &tail(*it1);
|
|
|
|
}
|
|
|
|
throw exception("unknown free variable");
|
|
|
|
}
|
|
|
|
|
2013-09-07 18:11:45 +00:00
|
|
|
context_entry const & context::lookup(unsigned i) const {
|
|
|
|
list<context_entry> const * it1 = &m_list;
|
2013-08-14 21:42:48 +00:00
|
|
|
while (*it1) {
|
|
|
|
if (i == 0)
|
|
|
|
return head(*it1);
|
2013-07-30 08:39:29 +00:00
|
|
|
--i;
|
|
|
|
it1 = &tail(*it1);
|
|
|
|
}
|
|
|
|
throw exception("unknown free variable");
|
|
|
|
}
|
2013-10-22 22:52:24 +00:00
|
|
|
|
2013-12-11 17:54:54 +00:00
|
|
|
optional<context_entry> context::find(unsigned i) const {
|
|
|
|
list<context_entry> const * it1 = &m_list;
|
|
|
|
while (*it1) {
|
|
|
|
if (i == 0)
|
|
|
|
return some(head(*it1));
|
|
|
|
--i;
|
|
|
|
it1 = &tail(*it1);
|
|
|
|
}
|
|
|
|
return optional<context_entry>();
|
|
|
|
}
|
|
|
|
|
2014-01-01 11:02:41 +00:00
|
|
|
list<context_entry> truncate_core(list<context_entry> const & l, unsigned s) {
|
|
|
|
if (s == 0) {
|
|
|
|
return list<context_entry>();
|
|
|
|
} else {
|
|
|
|
return cons(head(l), truncate_core(tail(l), s-1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
context context::truncate(unsigned s) const {
|
|
|
|
return context(truncate_core(m_list, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct remove_no_applicable {};
|
|
|
|
static list<context_entry> remove_core(list<context_entry> const & l, unsigned s, unsigned n, metavar_env const & menv) {
|
2013-12-12 03:51:57 +00:00
|
|
|
if (l) {
|
|
|
|
if (s == 0) {
|
|
|
|
if (n > 0) {
|
2014-01-01 11:02:41 +00:00
|
|
|
return remove_core(tail(l), 0, n-1, menv);
|
2013-12-12 03:51:57 +00:00
|
|
|
} else {
|
|
|
|
return l;
|
|
|
|
}
|
2013-10-22 22:52:24 +00:00
|
|
|
} else {
|
2014-01-01 11:02:41 +00:00
|
|
|
if (has_free_var(head(l), s-1, s+n-1, menv))
|
|
|
|
throw remove_no_applicable();
|
|
|
|
return cons(lower_free_vars(head(l), s+n-1, n, menv), remove_core(tail(l), s-1, n, menv));
|
2013-10-22 22:52:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-12-12 03:51:57 +00:00
|
|
|
return l;
|
2013-10-22 22:52:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-01 11:02:41 +00:00
|
|
|
optional<context> context::remove(unsigned s, unsigned n, metavar_env const & menv) const {
|
|
|
|
try {
|
|
|
|
return some(context(remove_core(m_list, s, n, menv)));
|
|
|
|
} catch (remove_no_applicable&) {
|
|
|
|
return optional<context>();
|
|
|
|
}
|
2013-10-22 22:52:24 +00:00
|
|
|
}
|
2013-07-30 08:39:29 +00:00
|
|
|
}
|