diff --git a/src/library/util.cpp b/src/library/util.cpp index f9c796cb6..59432134e 100644 --- a/src/library/util.cpp +++ b/src/library/util.cpp @@ -12,6 +12,31 @@ Author: Leonardo de Moura #include "library/locals.h" namespace lean { +bool is_def_app(environment const & env, expr const & e) { + if (!is_app(e)) + return false; + expr const & f = get_app_fn(e); + if (!is_constant(f)) + return false; + auto decl = env.find(const_name(f)); + return decl && decl->is_definition() && !decl->is_opaque(); +} + +optional unfold_app(environment const & env, expr const & e) { + if (!is_app(e)) + return none_expr(); + expr const & f = get_app_fn(e); + if (!is_constant(f)) + return none_expr(); + auto decl = env.find(const_name(f)); + if (!decl || !decl->is_definition() || decl->is_opaque()) + return none_expr(); + expr d = instantiate_value_univ_params(*decl, const_levels(f)); + buffer args; + get_app_rev_args(e, args); + return some_expr(apply_beta(d, args.size(), args.data())); +} + optional dec_level(level const & l) { switch (kind(l)) { case level_kind::Zero: case level_kind::Param: case level_kind::Global: case level_kind::Meta: diff --git a/src/library/util.h b/src/library/util.h index 4fff091e6..79576e82f 100644 --- a/src/library/util.h +++ b/src/library/util.h @@ -11,6 +11,16 @@ Author: Leonardo de Moura namespace lean { typedef std::unique_ptr type_checker_ptr; +/** \brief Return true iff \c e is of the form (f ...) where + \c f is a non-opaque constant definition */ +bool is_def_app(environment const & env, expr const & e); + +/** \brief If \c e is of the form (f a_1 ... a_n), where \c f is + a non-opaque definition, then unfold \c f, and beta reduce. + Otherwise, return none. +*/ +optional unfold_app(environment const & env, expr const & e); + /** \brief Reduce (if possible) universe level by 1. \pre is_not_zero(l) */