refactor(library): add type_util module, and move get_expect_num_args to it

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2014-09-03 07:42:24 -07:00
parent e802883b03
commit 060093cbab
4 changed files with 44 additions and 12 deletions

View file

@ -8,6 +8,6 @@ add_library(library deep_copy.cpp expr_lt.cpp io_state.cpp occurs.cpp
unifier.cpp unifier_plugin.cpp inductive_unifier_plugin.cpp
explicit.cpp num.cpp string.cpp opaque_hints.cpp head_map.cpp
match.cpp definition_cache.cpp declaration_index.cpp
print.cpp annotation.cpp typed_expr.cpp let.cpp)
print.cpp annotation.cpp typed_expr.cpp let.cpp type_util.cpp)
target_link_libraries(library ${LEAN_LIBS})

View file

@ -16,6 +16,7 @@ Author: Leonardo de Moura
#include "library/kernel_bindings.h"
#include "library/unifier.h"
#include "library/occurs.h"
#include "library/type_util.h"
#include "library/tactic/apply_tactic.h"
namespace lean {
@ -41,17 +42,6 @@ bool collect_simple_metas(expr const & e, buffer<expr> & result) {
return !failed;
}
unsigned get_expect_num_args(type_checker & tc, expr e) {
unsigned r = 0;
while (true) {
e = tc.whnf(e).first;
if (!is_pi(e))
return r;
e = binding_body(e);
r++;
}
}
void collect_simple_meta(expr const & e, buffer<expr> & metas) {
for_each(e, [&](expr const & e, unsigned) {
if (is_meta(e)) {

24
src/library/type_util.cpp Normal file
View file

@ -0,0 +1,24 @@
/*
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 "kernel/instantiate.h"
#include "library/type_util.h"
namespace lean {
unsigned get_expect_num_args(type_checker & tc, expr e) {
name_generator ngen = tc.mk_ngen();
unsigned r = 0;
while (true) {
e = tc.whnf(e).first;
if (!is_pi(e))
return r;
// TODO(Leo): try to avoid the following instantiate.
expr local = mk_local(ngen.next(), binding_name(e), binding_domain(e), binding_info(e));
e = instantiate(binding_body(e), local);
r++;
}
}
}

18
src/library/type_util.h Normal file
View file

@ -0,0 +1,18 @@
/*
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#pragma once
#include "kernel/type_checker.h"
namespace lean {
/** \brief Given an expression \c e, return the number of arguments expected arguments.
\remark This function computes the type of \c e, and then counts the number of nested
Pi's. Weak-head-normal-forms are computed for the type of \c e.
\remark The type and whnf are computed using \c tc.
*/
unsigned get_expect_num_args(type_checker & tc, expr e);
}