2013-07-25 02:36:54 +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
|
|
|
|
*/
|
|
|
|
#include <algorithm>
|
2013-07-30 08:39:29 +00:00
|
|
|
#include "normalize.h"
|
2013-07-25 02:36:54 +00:00
|
|
|
#include "expr.h"
|
2013-07-30 07:25:19 +00:00
|
|
|
#include "context.h"
|
|
|
|
#include "environment.h"
|
2013-07-26 19:37:13 +00:00
|
|
|
#include "free_vars.h"
|
2013-07-25 02:36:54 +00:00
|
|
|
#include "list.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "trace.h"
|
2013-07-26 02:13:45 +00:00
|
|
|
#include "exception.h"
|
2013-07-25 02:36:54 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
|
2013-08-03 21:16:32 +00:00
|
|
|
class svalue;
|
2013-08-03 23:09:21 +00:00
|
|
|
typedef list<svalue> value_stack; //!< Normalization stack
|
2013-08-03 21:16:32 +00:00
|
|
|
enum class svalue_kind { Expr, Closure, BoundedVar };
|
|
|
|
/** \brief Stack value: simple expressions, closures and bounded variables. */
|
|
|
|
class svalue {
|
|
|
|
svalue_kind m_kind;
|
|
|
|
unsigned m_bvar;
|
|
|
|
expr m_expr;
|
2013-08-03 23:09:21 +00:00
|
|
|
value_stack m_ctx;
|
2013-07-25 02:36:54 +00:00
|
|
|
public:
|
2013-08-03 23:09:21 +00:00
|
|
|
explicit svalue(expr const & e): m_kind(svalue_kind::Expr), m_expr(e) {}
|
|
|
|
explicit svalue(unsigned k): m_kind(svalue_kind::BoundedVar), m_bvar(k) {}
|
|
|
|
svalue(expr const & e, value_stack const & c):m_kind(svalue_kind::Closure), m_expr(e), m_ctx(c) { lean_assert(is_lambda(e)); }
|
2013-07-25 02:36:54 +00:00
|
|
|
|
2013-08-03 21:16:32 +00:00
|
|
|
svalue_kind kind() const { return m_kind; }
|
2013-07-26 02:13:45 +00:00
|
|
|
|
2013-08-03 21:16:32 +00:00
|
|
|
bool is_expr() const { return kind() == svalue_kind::Expr; }
|
|
|
|
bool is_closure() const { return kind() == svalue_kind::Closure; }
|
|
|
|
bool is_bounded_var() const { return kind() == svalue_kind::BoundedVar; }
|
2013-07-26 02:13:45 +00:00
|
|
|
|
2013-07-30 08:39:29 +00:00
|
|
|
expr const & get_expr() const { lean_assert(is_expr() || is_closure()); return m_expr; }
|
2013-08-03 23:09:21 +00:00
|
|
|
value_stack const & get_ctx() const { lean_assert(is_closure()); return m_ctx; }
|
2013-07-30 08:39:29 +00:00
|
|
|
unsigned get_var_idx() const { lean_assert(is_bounded_var()); return m_bvar; }
|
2013-07-25 02:36:54 +00:00
|
|
|
};
|
|
|
|
|
2013-08-03 23:09:21 +00:00
|
|
|
svalue_kind kind(svalue const & v) { return v.kind(); }
|
|
|
|
expr const & to_expr(svalue const & v) { return v.get_expr(); }
|
|
|
|
value_stack const & stack_of(svalue const & v) { return v.get_ctx(); }
|
|
|
|
unsigned to_bvar(svalue const & v) { return v.get_var_idx(); }
|
2013-07-30 07:25:19 +00:00
|
|
|
|
2013-08-03 23:09:21 +00:00
|
|
|
value_stack extend(value_stack const & s, svalue const & v) { return cons(v, s); }
|
2013-07-25 02:36:54 +00:00
|
|
|
|
2013-08-03 21:16:32 +00:00
|
|
|
/** \brief Expression normalizer. */
|
2013-07-30 07:25:19 +00:00
|
|
|
class normalize_fn {
|
|
|
|
environment const & m_env;
|
|
|
|
context const & m_ctx;
|
|
|
|
|
2013-08-03 23:09:21 +00:00
|
|
|
svalue lookup(value_stack const & s, unsigned i, unsigned k) {
|
2013-07-30 08:39:29 +00:00
|
|
|
unsigned j = i;
|
2013-08-03 23:09:21 +00:00
|
|
|
value_stack const * it1 = &s;
|
2013-07-30 08:39:29 +00:00
|
|
|
while (*it1) {
|
|
|
|
if (j == 0)
|
2013-07-30 07:25:19 +00:00
|
|
|
return head(*it1);
|
2013-07-30 08:39:29 +00:00
|
|
|
--j;
|
2013-07-30 07:25:19 +00:00
|
|
|
it1 = &tail(*it1);
|
|
|
|
}
|
2013-07-30 08:39:29 +00:00
|
|
|
context const & c = ::lean::lookup(m_ctx, j);
|
|
|
|
if (c) {
|
|
|
|
context_entry const & entry = head(c);
|
|
|
|
if (entry.get_body())
|
2013-08-03 21:16:32 +00:00
|
|
|
return svalue(::lean::normalize(entry.get_body(), m_env, tail(c)));
|
2013-07-30 08:39:29 +00:00
|
|
|
else
|
2013-08-03 21:16:32 +00:00
|
|
|
return svalue(length(c) - 1);
|
2013-07-25 02:36:54 +00:00
|
|
|
}
|
2013-07-30 07:25:19 +00:00
|
|
|
throw exception("unknown free variable");
|
2013-07-25 02:36:54 +00:00
|
|
|
}
|
2013-07-30 07:25:19 +00:00
|
|
|
|
2013-08-03 21:16:32 +00:00
|
|
|
/** \brief Convert the closure \c a into an expression using the given stack in a context that contains \c k binders. */
|
2013-08-03 23:09:21 +00:00
|
|
|
expr reify_closure(expr const & a, value_stack const & s, unsigned k) {
|
2013-07-30 07:25:19 +00:00
|
|
|
lean_assert(is_lambda(a));
|
2013-08-03 21:16:32 +00:00
|
|
|
expr new_t = reify(normalize(abst_domain(a), s, k), k);
|
|
|
|
expr new_b = reify(normalize(abst_body(a), extend(s, svalue(k)), k+1), k+1);
|
2013-08-02 06:19:34 +00:00
|
|
|
return lambda(abst_name(a), new_t, new_b);
|
|
|
|
#if 0
|
|
|
|
// Eta-reduction + Cumulativity + Set theoretic interpretation is unsound.
|
|
|
|
// Example:
|
|
|
|
// f : (Type 2) -> (Type 2)
|
|
|
|
// (lambda (x : (Type 1)) (f x)) : (Type 1) -> (Type 2)
|
|
|
|
// The domains of these two terms are different. So, they must have different denotations.
|
|
|
|
// However, by eta-reduction, we have:
|
|
|
|
// (lambda (x : (Type 1)) (f x)) == f
|
|
|
|
// For now, we will disable it.
|
|
|
|
// REMARK: we can workaround this problem by applying only when the domain of f is equal
|
|
|
|
// to the domain of the lambda abstraction.
|
|
|
|
//
|
2013-07-30 07:25:19 +00:00
|
|
|
if (is_app(new_b)) {
|
|
|
|
// (lambda (x:T) (app f ... (var 0)))
|
|
|
|
// check eta-rule applicability
|
|
|
|
unsigned n = num_args(new_b);
|
|
|
|
if (is_var(arg(new_b, n - 1), 0) &&
|
|
|
|
std::all_of(begin_args(new_b),
|
|
|
|
end_args(new_b) - 1,
|
|
|
|
[](expr const & arg) { return !has_free_var(arg, 0); })) {
|
|
|
|
if (n == 2)
|
|
|
|
return lower_free_vars(arg(new_b, 0), 1);
|
|
|
|
else
|
|
|
|
return lower_free_vars(app(n - 1, begin_args(new_b)), 1);
|
|
|
|
}
|
|
|
|
return lambda(abst_name(a), new_t, new_b);
|
2013-07-30 08:39:29 +00:00
|
|
|
} else {
|
2013-07-30 07:25:19 +00:00
|
|
|
return lambda(abst_name(a), new_t, new_b);
|
|
|
|
}
|
2013-08-02 06:19:34 +00:00
|
|
|
#endif
|
2013-07-25 02:36:54 +00:00
|
|
|
}
|
2013-07-30 07:25:19 +00:00
|
|
|
|
2013-08-03 21:16:32 +00:00
|
|
|
/** \brief Convert the value \c v back into an expression in a context that contains \c k binders. */
|
|
|
|
expr reify(svalue const & v, unsigned k) {
|
2013-07-30 07:25:19 +00:00
|
|
|
lean_trace("normalize", tout << "Reify kind: " << static_cast<unsigned>(v.kind()) << "\n";
|
|
|
|
if (v.is_bounded_var()) tout << "#" << to_bvar(v); else tout << to_expr(v); tout << "\n";);
|
|
|
|
switch (v.kind()) {
|
2013-08-03 21:16:32 +00:00
|
|
|
case svalue_kind::Expr: return to_expr(v);
|
|
|
|
case svalue_kind::BoundedVar: return var(k - to_bvar(v) - 1);
|
|
|
|
case svalue_kind::Closure: return reify_closure(to_expr(v), stack_of(v), k);
|
2013-07-30 07:25:19 +00:00
|
|
|
}
|
|
|
|
lean_unreachable();
|
|
|
|
return expr();
|
2013-07-26 02:13:45 +00:00
|
|
|
}
|
2013-07-25 02:36:54 +00:00
|
|
|
|
2013-08-03 21:16:32 +00:00
|
|
|
/** \brief Normalize the expression \c a in a context composed of stack \c s and \c k binders. */
|
2013-08-03 23:09:21 +00:00
|
|
|
svalue normalize(expr const & a, value_stack const & s, unsigned k) {
|
2013-07-30 07:25:19 +00:00
|
|
|
lean_trace("normalize", tout << "Normalize, k: " << k << "\n" << a << "\n";);
|
|
|
|
switch (a.kind()) {
|
|
|
|
case expr_kind::Var:
|
2013-08-03 21:16:32 +00:00
|
|
|
return lookup(s, var_idx(a), k);
|
2013-08-03 23:09:21 +00:00
|
|
|
case expr_kind::Constant: case expr_kind::Type: case expr_kind::Value:
|
2013-08-03 21:16:32 +00:00
|
|
|
return svalue(a);
|
2013-07-30 07:25:19 +00:00
|
|
|
case expr_kind::App: {
|
2013-08-03 21:16:32 +00:00
|
|
|
svalue f = normalize(arg(a, 0), s, k);
|
2013-07-30 07:25:19 +00:00
|
|
|
unsigned i = 1;
|
|
|
|
unsigned n = num_args(a);
|
|
|
|
while (true) {
|
|
|
|
if (f.is_closure()) {
|
|
|
|
// beta reduction
|
|
|
|
expr const & fv = to_expr(f);
|
|
|
|
lean_trace("normalize", tout << "beta reduction...\n" << fv << "\n";);
|
2013-08-03 23:09:21 +00:00
|
|
|
value_stack new_s = extend(stack_of(f), normalize(arg(a, i), s, k));
|
2013-08-03 21:16:32 +00:00
|
|
|
f = normalize(abst_body(fv), new_s, k);
|
2013-07-30 07:25:19 +00:00
|
|
|
if (i == n - 1)
|
|
|
|
return f;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buffer<expr> new_args;
|
2013-08-04 02:57:06 +00:00
|
|
|
expr new_f = reify(f, k);
|
|
|
|
new_args.push_back(new_f);
|
2013-07-30 07:25:19 +00:00
|
|
|
for (; i < n; i++)
|
2013-08-03 21:16:32 +00:00
|
|
|
new_args.push_back(reify(normalize(arg(a, i), s, k), k));
|
2013-08-04 02:57:06 +00:00
|
|
|
if (is_value(new_f)) {
|
|
|
|
expr r;
|
|
|
|
if (to_value(new_f).normalize(new_args.size(), new_args.data(), r))
|
|
|
|
return svalue(r);
|
|
|
|
}
|
2013-08-03 21:16:32 +00:00
|
|
|
return svalue(app(new_args.size(), new_args.data()));
|
2013-07-30 07:25:19 +00:00
|
|
|
}
|
2013-07-26 02:13:45 +00:00
|
|
|
}
|
2013-07-25 02:36:54 +00:00
|
|
|
}
|
2013-07-30 07:25:19 +00:00
|
|
|
case expr_kind::Lambda:
|
2013-08-03 21:16:32 +00:00
|
|
|
return svalue(a, s);
|
2013-07-30 07:25:19 +00:00
|
|
|
case expr_kind::Pi: {
|
2013-08-03 21:16:32 +00:00
|
|
|
expr new_t = reify(normalize(abst_domain(a), s, k), k);
|
|
|
|
expr new_b = reify(normalize(abst_body(a), extend(s, svalue(k)), k+1), k+1);
|
|
|
|
return svalue(pi(abst_name(a), new_t, new_b));
|
2013-07-30 07:25:19 +00:00
|
|
|
}}
|
|
|
|
lean_unreachable();
|
2013-08-03 21:16:32 +00:00
|
|
|
return svalue(a);
|
2013-07-25 02:36:54 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 07:25:19 +00:00
|
|
|
public:
|
|
|
|
normalize_fn(environment const & env, context const & ctx):
|
|
|
|
m_env(env),
|
|
|
|
m_ctx(ctx) {
|
|
|
|
}
|
|
|
|
|
|
|
|
expr operator()(expr const & e) {
|
2013-07-30 08:39:29 +00:00
|
|
|
unsigned k = length(m_ctx);
|
2013-08-03 23:09:21 +00:00
|
|
|
return reify(normalize(e, value_stack(), k), k);
|
2013-07-30 07:25:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
expr normalize(expr const & e, environment const & env, context const & ctx) {
|
|
|
|
return normalize_fn(env, ctx)(e);
|
2013-07-25 02:36:54 +00:00
|
|
|
}
|
|
|
|
}
|