2014-05-20 16:40:30 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include "util/name_generator.h"
|
|
|
|
#include "kernel/type_checker.h"
|
|
|
|
#include "kernel/instantiate.h"
|
|
|
|
#include "kernel/abstract.h"
|
2014-09-27 03:16:03 +00:00
|
|
|
#include "library/reducible.h"
|
2014-05-20 16:40:30 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
class normalize_fn {
|
2014-09-27 03:16:03 +00:00
|
|
|
std::unique_ptr<type_checker> m_tc;
|
|
|
|
name_generator m_ngen;
|
2014-05-20 16:40:30 +00:00
|
|
|
|
|
|
|
expr normalize_binding(expr const & e) {
|
|
|
|
expr d = normalize(binding_domain(e));
|
2014-06-30 16:14:55 +00:00
|
|
|
expr l = mk_local(m_ngen.next(), binding_name(e), d, binding_info(e));
|
2014-05-20 16:40:30 +00:00
|
|
|
expr b = abstract(normalize(instantiate(binding_body(e), l)), l);
|
|
|
|
return update_binding(e, d, b);
|
|
|
|
}
|
|
|
|
|
2014-05-20 18:53:58 +00:00
|
|
|
expr normalize_app(expr const & e) {
|
|
|
|
buffer<expr> args;
|
|
|
|
expr f = get_app_rev_args(e, args);
|
|
|
|
for (expr & a : args)
|
|
|
|
a = normalize(a);
|
|
|
|
return mk_rev_app(f, args);
|
|
|
|
}
|
|
|
|
|
2014-05-20 16:40:30 +00:00
|
|
|
expr normalize(expr e) {
|
2014-09-27 03:16:03 +00:00
|
|
|
e = m_tc->whnf(e).first;
|
2014-05-20 16:40:30 +00:00
|
|
|
switch (e.kind()) {
|
|
|
|
case expr_kind::Var: case expr_kind::Constant: case expr_kind::Sort:
|
|
|
|
case expr_kind::Meta: case expr_kind::Local: case expr_kind::Macro:
|
|
|
|
return e;
|
|
|
|
case expr_kind::Lambda: case expr_kind::Pi:
|
|
|
|
return normalize_binding(e);
|
|
|
|
case expr_kind::App:
|
2014-05-20 18:53:58 +00:00
|
|
|
return normalize_app(e);
|
2014-05-20 16:40:30 +00:00
|
|
|
}
|
|
|
|
lean_unreachable(); // LCOV_EXCL_LINE
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2014-09-27 03:16:03 +00:00
|
|
|
normalize_fn(environment const & env):m_tc(mk_type_checker(env, true)), m_ngen(m_tc->mk_ngen()) {}
|
2014-05-20 16:40:30 +00:00
|
|
|
expr operator()(expr const & e) { return normalize(e); }
|
2014-09-27 03:16:03 +00:00
|
|
|
expr operator()(level_param_names const & ls, expr const & e) {
|
|
|
|
return m_tc->with_params(ls, [&]() {
|
|
|
|
return normalize(e);
|
|
|
|
});
|
|
|
|
}
|
2014-05-20 16:40:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
expr normalize(environment const & env, expr const & e) { return normalize_fn(env)(e); }
|
2014-09-27 03:16:03 +00:00
|
|
|
expr normalize(environment const & env, level_param_names const & ls, expr const & e) { return normalize_fn(env)(ls, e); }
|
2014-05-20 16:40:30 +00:00
|
|
|
}
|