lean2/src/kernel/context.cpp
Leonardo de Moura 73c8bf4436 refactor(tests/kernel): move tests to new kernel
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
2014-03-18 10:27:55 -07:00

29 lines
743 B
C++

/*
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include <utility>
#include "util/exception.h"
#include "kernel/context.h"
namespace lean {
std::pair<name, expr> const & lookup(context const & c, unsigned i) {
auto const * it = &c;
while (*it) {
if (i == 0)
return head(*it);
--i;
it = &tail(*it);
}
throw exception("unknown free variable");
}
optional<std::pair<name, expr>> find(context const & c, unsigned i) {
try {
return optional<std::pair<name, expr>>(lookup(c, i));
} catch (exception &) {
return optional<std::pair<name, expr>>();
}
}
}