feat(library): add goodies for binary functions

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2014-05-15 11:18:06 -07:00
parent 830537a58b
commit 24d8092a73
3 changed files with 79 additions and 1 deletions

View file

@ -1,5 +1,5 @@
add_library(library deep_copy.cpp expr_lt.cpp io_state.cpp
occurs.cpp kernel_bindings.cpp io_state_stream.cpp)
occurs.cpp kernel_bindings.cpp io_state_stream.cpp bin_app.cpp)
# placeholder.cpp fo_unify.cpp hop_match.cpp)
target_link_libraries(library ${LEAN_LIBS})

51
src/library/bin_app.cpp Normal file
View file

@ -0,0 +1,51 @@
/*
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 "library/bin_app.h"
namespace lean {
bool is_bin_app(expr const & t, expr const & f, expr & lhs, expr & rhs) {
if (is_app(t) && is_app(app_fn(t)) && app_fn(app_fn(t)) == f) {
lhs = app_arg(app_fn(t));
rhs = app_arg(t);
return true;
} else {
return false;
}
}
expr mk_bin_rop(expr const & op, expr const & unit, unsigned num_args, expr const * args) {
if (num_args == 0) {
return unit;
} else {
expr r = args[num_args - 1];
unsigned i = num_args - 1;
while (i > 0) {
--i;
r = mk_app(op, args[i], r);
}
return r;
}
}
expr mk_bin_rop(expr const & op, expr const & unit, std::initializer_list<expr> const & l) {
return mk_bin_rop(op, unit, l.size(), l.begin());
}
expr mk_bin_lop(expr const & op, expr const & unit, unsigned num_args, expr const * args) {
if (num_args == 0) {
return unit;
} else {
expr r = args[0];
for (unsigned i = 1; i < num_args; i++) {
r = mk_app(op, r, args[i]);
}
return r;
}
}
expr mk_bin_lop(expr const & op, expr const & unit, std::initializer_list<expr> const & l) {
return mk_bin_lop(op, unit, l.size(), l.begin());
}
}

27
src/library/bin_app.h Normal file
View file

@ -0,0 +1,27 @@
/*
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/expr.h"
namespace lean {
/** \brief Return true iff \c t is of the form <tt>((f s1) s2)</tt>, if the result is true, then store a1 -> lhs, a2 -> rhs */
bool is_bin_app(expr const & t, expr const & f, expr & lhs, expr & rhs);
/**
\brief Return unit if <tt>num_args == 0</tt>, args[0] if <tt>num_args == 1</tt>, and
<tt>(op args[0] (op args[1] (op ... )))</tt>
*/
expr mk_bin_rop(expr const & op, expr const & unit, unsigned num_args, expr const * args);
expr mk_bin_rop(expr const & op, expr const & unit, std::initializer_list<expr> const & l);
/**
\brief Return unit if <tt>num_args == 0</tt>, args[0] if <tt>num_args == 1</tt>, and
<tt>(op ... (op (op args[0] args[1]) args[2]) ...)</tt>
*/
expr mk_bin_lop(expr const & op, expr const & unit, unsigned num_args, expr const * args);
expr mk_bin_lop(expr const & op, expr const & unit, std::initializer_list<expr> const & l);
}