Skip to content

Add bindings for sequence foldl #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions z3-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3110,6 +3110,9 @@ extern "C" {
/// The function is under-specified if `offset` is negative or larger than the length of `s`.
pub fn Z3_mk_seq_index(c: Z3_context, s: Z3_ast, substr: Z3_ast, offset: Z3_ast) -> Z3_ast;

/// Create a fold of the function `f` over the sequence `s` with accumulator `a`.
pub fn Z3_mk_seq_foldl(c: Z3_context, f: Z3_ast, a: Z3_ast, s: Z3_ast) -> Z3_ast;

/// Convert string to integer.
pub fn Z3_mk_str_to_int(c: Z3_context, s: Z3_ast) -> Z3_ast;

Expand Down
31 changes: 31 additions & 0 deletions z3/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1751,6 +1751,37 @@ impl<'ctx> Seq<'ctx> {
pub fn length(&self) -> Int<'ctx> {
unsafe { Int::wrap(self.ctx, Z3_mk_seq_length(self.ctx.z3_ctx, self.z3_ast)) }
}

/// Create a fold of the function `f` over the sequence with accumulator `a`.
///
/// # Examples
/// ```
/// # use z3::{Config, Context, Solver, Sort};
/// # use z3::ast::{Seq, Int, Dynamic, lambda_const};
/// #
/// # let cfg = Config::new();
/// # let ctx = Context::new(&cfg);
/// # let solver = Solver::new(&ctx);
/// #
/// let seq = Seq::new_const(&ctx, "seq", &Sort::int(&ctx));
/// let accumulator = Int::new_const(&ctx, "acc");
/// let item = Int::new_const(&ctx, "item");
/// let sum = lambda_const(
/// &ctx,
/// &[&accumulator, &item],
/// &Dynamic::from_ast(&Int::add(&ctx, &[&accumulator, &item])),
/// );
///
/// seq.foldl(&sum, &Dynamic::from_ast(&Int::from_u64(&ctx, 0)));
/// ```
pub fn foldl(&self, f: &Array<'ctx>, a: &Dynamic<'ctx>) -> Dynamic<'ctx> {
unsafe {
Dynamic::wrap(
self.ctx,
Z3_mk_seq_foldl(self.ctx.z3_ctx, f.z3_ast, a.z3_ast, self.z3_ast),
)
}
}
}

impl<'ctx> Dynamic<'ctx> {
Expand Down
Loading