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"
|
2013-07-30 08:39:29 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-02-18 00:10:11 +00:00
|
|
|
std::pair<name, expr> const & lookup(context const & c, unsigned i) {
|
|
|
|
auto const * it = &c;
|
|
|
|
while (*it) {
|
2013-08-14 21:42:48 +00:00
|
|
|
if (i == 0)
|
2014-02-18 00:10:11 +00:00
|
|
|
return head(*it);
|
2013-07-30 08:39:29 +00:00
|
|
|
--i;
|
2014-02-18 00:10:11 +00:00
|
|
|
it = &tail(*it);
|
2013-07-30 08:39:29 +00:00
|
|
|
}
|
|
|
|
throw exception("unknown free variable");
|
|
|
|
}
|
2014-02-18 01:04:22 +00:00
|
|
|
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>>();
|
|
|
|
}
|
|
|
|
}
|
2013-07-30 08:39:29 +00:00
|
|
|
}
|