Coder Social home page Coder Social logo

z4's Introduction

z4

PyPI

z3 with some improvements:

  • BitVec:

    • Change the right shift operation to be logical instead of arithmetic
  • Add the ByteVec class

  • Add some helper methods for solving:

    • easy_solve
    • find_all_solutions
    • easy_prove
  • Add some helper functions for z3 variables/constants:

    • BoolToInt
    • Sgn
    • TruncDiv

Features implemented upstream

These features were first provided by z4, before z3 included these features. They have now been removed from z4.

Usage

Install with pip install z4-solver.

easy_solve

import z4

a, b = z4.Ints("a b")
print(z4.easy_solve([a <= 10, b <= 10, a + b == 15]))

Output:

[b = 5, a = 10]

find_all_solutions

import z4

a, b = z4.Ints("a b")
print(*z4.find_all_solutions([a <= 10, b <= 10, a + b == 15]), sep="\n")

Output:

[b = 5, a = 10]
[b = 6, a = 9]
[b = 7, a = 8]
[b = 8, a = 7]
[b = 9, a = 6]
[b = 10, a = 5]

easy_prove

Let's try and prove that 2 * a >= a for all integers a:

import z4

a = z4.Int("a")
print(z4.easy_prove(2 * a >= a))

Output

Traceback (most recent call last):
  ...
z4.Z3CounterExample: [a = -1]

This isn't true so we get an exception with the counter-example a = -1. Of course 2 * -1 = -2 which is less than -1.

Let's try again with the assumption that a must be non-negative:

print(z4.easy_prove(z4.Implies(a >= 0, 2 * a >= a)))

Output:

True

z4's People

Contributors

theblupper avatar tyilo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

theblupper

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.