From 24d8092a73af7b6486515a34272fe3251420ea4c Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Thu, 15 May 2014 11:18:06 -0700 Subject: [PATCH] feat(library): add goodies for binary functions Signed-off-by: Leonardo de Moura --- src/library/CMakeLists.txt | 2 +- src/library/bin_app.cpp | 51 ++++++++++++++++++++++++++++++++++++++ src/library/bin_app.h | 27 ++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/library/bin_app.cpp create mode 100644 src/library/bin_app.h diff --git a/src/library/CMakeLists.txt b/src/library/CMakeLists.txt index 05d180c3c..018726be7 100644 --- a/src/library/CMakeLists.txt +++ b/src/library/CMakeLists.txt @@ -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}) diff --git a/src/library/bin_app.cpp b/src/library/bin_app.cpp new file mode 100644 index 000000000..2c3f50ea1 --- /dev/null +++ b/src/library/bin_app.cpp @@ -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 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 const & l) { + return mk_bin_lop(op, unit, l.size(), l.begin()); +} +} diff --git a/src/library/bin_app.h b/src/library/bin_app.h new file mode 100644 index 000000000..a6d408320 --- /dev/null +++ b/src/library/bin_app.h @@ -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 ((f s1) s2), 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 num_args == 0, args[0] if num_args == 1, and + (op args[0] (op args[1] (op ... ))) +*/ +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 const & l); + +/** + \brief Return unit if num_args == 0, args[0] if num_args == 1, and + (op ... (op (op args[0] args[1]) args[2]) ...) +*/ +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 const & l); +}