Giter VIP home page Giter VIP logo

sbv's Introduction

SBV: SMT Based Verification in Haskell

Build Status

On Hackage: http://hackage.haskell.org/package/sbv

Express properties about Haskell programs and automatically prove them using SMT solvers.

$ ghci
ghci> :m Data.SBV
ghci> prove $ \x -> x `shiftL` 2 .== 4 * (x::SWord8)
Q.E.D.
ghci> prove $ \x -> x `shiftL` 2 .== 2 * (x::SWord8)
Falsifiable. Counter-example:
  s0 = 32 :: Word8

The function prove establishes theorem-hood, while sat finds a satisfying model if it exists. All satisfying models can be computed using allSat. SBV can also perform static assertion checks, such as absence of division-by-0, and other user given properties. Furthermore, SBV can perform optimization, minimizing/maximizing arithmetic goals for their optimal values.

SBV also allows for an incremental mode: Users are given a handle to the SMT solver as their programs execute, and they can issue SMTLib commands programmatically, query values, and direct the interaction using a high-level typed API. The incremental mode also allows for creation of constraints based on the current model, and access to internals of SMT solvers for advanced users. See the runSMT and query commands for details.

Overview

SBV library provides support for dealing with symbolic values in Haskell. It introduces the types:

  • SBool: Symbolic Booleans (bits).
  • SWord8, SWord16, SWord32, SWord64: Symbolic Words (unsigned).
  • SInt8, SInt16, SInt32, SInt64: Symbolic Ints (signed).
  • SWord N, SInt N, for N > 0: Arbitrary sized unsigned/signed bit-vectors, parameterized by the bitsize. (Using DataKinds extension.)
  • SInteger: Symbolic unbounded integers (signed).
  • SReal: Symbolic infinite precision algebraic reals (signed).
  • SRational: Symbolic rationals, ratio of two symbolic integers. (Rational.)
  • SFloat: IEEE-754 single precision floating point number. (Float.)
  • SDouble: IEEE-754 double precision floating point number. (Double.)
  • SFloatingPoint: IEEE-754 floating point number with user specified exponent and significand sizes. (FloatingPoint)
  • SChar: Symbolic characters, supporting unicode.
  • SString: Symbolic strings.
  • SList: Symbolic lists. (Which can be nested, i.e., lists of lists.)
  • STuple: Symbolic tuples (upto 8-tuples, can be nested)
  • SEither: Symbolic sums
  • SMaybe: Symbolic optional values
  • SSet: Symbolic sets
  • Arrays of symbolic values.
  • Symbolic enumerations, for arbitrary user-defined enumerated types.
  • Symbolic polynomials over GF(2^n ), polynomial arithmetic, and CRCs.
  • Uninterpreted constants and functions over symbolic values, with user defined axioms.
  • Uninterpreted sorts, and proofs over such sorts, potentially with axioms.
  • Ability to define SMTLib functions, generated directly from Haskell versions, including support for recursive and mutually recursive functions.
  • Reasoning with universal and existential quantifiers, including alternating quantifiers.

The user can construct ordinary Haskell programs using these types, which behave like ordinary Haskell values when used concretely. However, when used with symbolic arguments, functions built out of these types can also be:

  • proven correct via an external SMT solver (the prove function),
  • checked for satisfiability (the sat, and allSat functions),
  • checked for assertion violations (the safe function with sAssert calls),
  • checked for delta-satisfiability (the dsat and dprove functions),
  • used in synthesis (the satfunction with existentials),
  • checked for machine-arithmetic overflow/underflow conditions,
  • optimized with respect to cost functions (the optimize, maximize, and minimize functions),
  • quick-checked,
  • used for generating Haskell and C test vectors (the genTest function),
  • compiled down to C, rendered as straight-line programs or libraries (compileToC and compileToCLib functions).

Picking the SMT solver to use

The SBV library uses third-party SMT solvers via the standard SMT-Lib interface. The following solvers are supported:

  • ABC from University of Berkeley
  • Boolector from Johannes Kepler University
  • Bitwuzla from Stanford University
  • CVC4 and CVC5 from Stanford University and the University of Iowa
  • DReal from CMU
  • MathSAT from FBK and DISI-University of Trento
  • OpenSMT from Università della Svizzera italiana
  • Yices from SRI
  • Z3 from Microsoft

Most functions have two variants: For instance prove/proveWith. The former uses the default solver, which is currently Z3. The latter expects you to pass it a configuration that picks the solver. The valid values are abc, boolector, bitwuzla, cvc4, cvc5, dReal, mathSAT, openSMT, yices, and z3.

See versions for a listing of the versions of these tools SBV has been tested with. Please report if you see any discrepancies!

Other SMT solvers can be used with SBV as well, with a relatively easy hook-up mechanism. Please do get in touch if you plan to use SBV with any other solver.

Using multiple solvers, simultaneously

SBV also allows for running multiple solvers at the same time, either picking the result of the first to complete, or getting results from all. See proveWithAny/proveWithAll and satWithAny/satWithAll functions. The function sbvAvailableSolvers can be used to query the available solvers at run-time.

Copyright, License

The SBV library is distributed with the BSD3 license. See COPYRIGHT for details. The LICENSE file contains the BSD3 verbiage.

Thanks

The following people made major contributions to SBV, by developing new features and contributing to the design in significant ways: Joel Burget, Brian Huffman, Brian Schroeder, and Jeffrey Young.

The following people reported bugs, provided comments/feedback, or contributed to the development of SBV in various ways: Ara Adkins, Kanishka Azimi, Markus Barenhoff, Reid Barton, Ben Blaxill, Ian Blumenfeld, Guillaume Bouchard, Martin Brain, Ian Calvert, Oliver Charles, Christian Conkle, Matthew Danish, Iavor Diatchki, Alex Dixon, Robert Dockins, Thomas DuBuisson, Trevor Elliott, Gergő Érdi, John Erickson, Richard Fergie, Adam Foltzer, Joshua Gancher, Remy Goldschmidt, Brad Hardy, Tom Hawkins, Greg Horn, Jan Hrcek, Georges-Axel Jaloyan, Anders Kaseorg, Tom Sydney Kerckhove, Lars Kuhtz, Piërre van de Laar, Pablo Lamela, Ken Friis Larsen, Andrew Lelechenko, Joe Leslie-Hurd, Nick Lewchenko, Brett Letner, Sirui Lu, Georgy Lukyanov, Martin Lundfall, John Matthews, Curran McConnell, Philipp Meyer, Fabian Mitterwallner, Joshua Moerman, Matt Parker, Jan Path, Matt Peddie, Lucas Peña, Matthew Pickering, Lee Pike, Gleb Popov, Rohit Ramesh, Geoffrey Ramseyer, Jaro Reinders, Stephan Renatus, Dan Rosén, Ryan Scott, Eric Seidel, Austin Seipp, Andrés Sicard-Ramírez, Don Stewart, Greg Sullivan, Josef Svenningsson, George Thomas, May Torrence, Daniel Wagner, Sean Weaver, Nis Wegmann, and Jared Ziegler.

Thanks!

sbv's People

Contributors

alios avatar arrowd avatar bts avatar conklech avatar dmwit avatar doyougnu avatar elliottt avatar geo2a avatar georgefst avatar ghorn avatar gridaphobe avatar hugopeters1024 avatar jhrcek avatar joelburget avatar juliapath avatar kfl avatar larskuhtz avatar leventerkok avatar lsrcz avatar norfairking avatar ocharles avatar octalsrc avatar peddie avatar richardfergie avatar robdockins avatar ryanglscott avatar srenatus avatar terrorjack avatar tomjaguarpaw avatar yav 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar  avatar

sbv's Issues

need new doctest

The recent move to 7.4.1 dev env broke doctest based testing since doctest doesn't compile with ghc-7.4.1 yet. The call to doctest in the Makefile is currently commented out. Re-enable this once doctest becomes available.

Compilation fails with containers-0.5.0.0

Compilation falis with the following message on my system.

$ ghc -V
The Glorious Glasgow Haskell Compilation System, version 7.4.2
$ cabal --version
cabal-install version 0.14.0
using version 1.14.0 of the Cabal library
$ cabal install sbv
Resolving dependencies...
Configuring sbv-2.1...
Building sbv-2.1...
Preprocessing library sbv-2.1...
[ 1 of 53] Compiling Data.SBV.Utils.TDiff ( Data/SBV/Utils/TDiff.hs, dist/build/Data/SBV/Utils/TDiff.o )
[ 2 of 53] Compiling Data.SBV.Utils.Lib ( Data/SBV/Utils/Lib.hs, dist/build/Data/SBV/Utils/Lib.o )
[ 3 of 53] Compiling Data.SBV.Utils.Boolean ( Data/SBV/Utils/Boolean.hs, dist/build/Data/SBV/Utils/Boolean.o )
[ 4 of 53] Compiling Data.SBV.BitVectors.AlgReals ( Data/SBV/BitVectors/AlgReals.hs, dist/build/Data/SBV/BitVectors/AlgReals.o )
[ 5 of 53] Compiling Data.SBV.BitVectors.Data ( Data/SBV/BitVectors/Data.hs, dist/build/Data/SBV/BitVectors/Data.o )

Data/SBV/BitVectors/Data.hs:1110:27:
Overlapping instances for NFData Pgm
arising from a use of rnf' Matching instances: instance NFData a => NFData (S.Seq a) -- Defined inData.Sequence'
instance NFData Pgm
-- Defined at Data/SBV/BitVectors/Data.hs:1114:10
In the first argument of seq', namelyrnf pgm'
In the second argument of seq', namely rnf pgm seq rnf cstr seq rnf outs'
In the second argument of seq', namely rnf axs seq rnf pgm seq rnf cstr seq rnf outs'
cabal: Error: some packages failed to install:
sbv-2.1 failed during the building phase. The exception was:
ExitFailure 1

(cabal install -v doesn't seem to give more helpful output)

I have both containers-0.4.2.1 and containers-0.5.0.0 installed, and cabal seems to (try to) build against containers-0.5. It compiles fine if I use:

$ cabal install sbv --constraint "containers == 0.4.2.1" --force-reinstalls

Best,

compileToSMTLib inverts formula

The following program

{-# OPTIONS -XScopedTypeVariables #-}
import Data.SBV

f :: Symbolic SBool
f = do
    x <- exists "x" :: Symbolic SInteger
    y <- exists "y"
    return $ x .>= y

main = putStrLn =<< compileToSMTLib True f

produces the (shortened) output:

; --- formula ---
(assert (forall ((s0 Int)
                 (s1 Int))
            (let ((s2 (ite (>= s0 s1) #b1 #b0)))
            (not (= s2 #b1)))))
(check-sat)

As can be seen, the quantifiers were changed from existential to universal.
I guess that this is caused by compileToSMTLib calling the simulate function with the 'isSat' parameter set to False.

Short-cut evaluations are done too late

Short-cut evals on operators are delayed too late. They should be happening earlier. While this causes performance problems at the least, it is completely unacceptable for symbolic-termination issues handled with counters.

Add synthesis example

Add the "AIG" generator example; note that we first need to make sure getModel is improved so we don't have to do the ugly exception trick if all we want to display is just one model.

sketch

can we build a sketch like dsl on top of sbv?

popCount

The next version of the Data.Bits class will have popCount field in it. Track and add this for SBV when it becomes available. The symbolic implementation will likely be table-lookup based.

popCount + testBit

Override default definitions of popCount and testBit to "bail out" in case the argument is symbolic, and point out the use of the sbv-replacement in the latter case.

Note that popCount is only available in GHC >= 7.4.1, so we might wait for the next HP release that'll have it. (Otherwise, we'd need some GLASGOW_HASKELL define magic..)

Improve the AES example

Improve the AES code-generation example by generating code for keySchedule and encrypt/decrypt separately. Note that this will have to wait until fixed-length lists are available in Haskell, via type-naturals preferably.

Also improve the test-suite for AES, by adding extra enc/dec pairs, once the fixed-length lists are supported in the code generator.

Update fib example

Use 64-bit numbers, as 32-bit numbers overflow very quickly..

Also, add a note on the modular arithmetic, so it'll eventually overflow no matter how big.

(Pointed out by Ian Calvert.)

remove Eq instance for SBV

remove Eq instance from SBV for GHC >= 7.4.1; which is no longer needed/necessary since Num doesn't have Eq as a superclass anymore.

Reduce variable names in code generation

The code generator uses way too many variables than necessary, due to the single-static assignment form it generates. It also skips a whole bunch of variable names that designate constants, as those get inlined.

In practice this works OK, since gcc (or any other decent compiler) can detect short lived variables and get rid of them easily. However, it might be nice if we did this automatically for platforms where the compilers may not do these optimizations. (Otherwise, the frames generated for calls will be too large..)

explicit let

See if the "explicit" let idea can buy us anything that stable-pointers don't get us in SBV:

http://www.haskell.org/pipermail/haskell-cafe/2008-February/039639.html

CRC example

  • Polynomial arithmetic: Need more testing on the semantics of pdiv/pmod/pmult..
  • CCITT: 84-inhabitants issue; mismatch with the expected number. What's the semantics?
  • GenPoly: is there a better way of generating good polynomials?

Code generation: Input/output arguments

Currently the code-generator supports input args and output args separately, but there's no support for arguments that are used "in-place"; i.e., use the current values and also update them as well. Sort of like an in-out parameter.

This is not a huge deal actually, since the caller can arrange for this by passing the same argument for both as input and output appropriately, with no loss of functionality. Something like:

 foo (x, y, &x)

where x is used both as input and output.

But this comes at the cost of ugly API, and also it doesn't sound too hard to support this feature, albeit with the explosion of new functions like: cgInputOutput, cgInputOutputArr etc.

diophantine solver proof

can we prove the diophantine solver (as coded in Haskell) correct using our SMT based solver? Could be challenging due to quantifiers and the inherent looping involved, but it might be possible to prove simpler (yet interesting) claims.

typedef SBool as bool?

In the generated internal.h header, rather than

typedef uint8_t SBool ;

what about

typedef bool SBool ;

In particular, I'm having trouble using CBMC for static analysis, since elsewhere I equate SBool with bool.

SBV->C: Support for sized argument rotates

The C generator currently does not support rotates when the argument is a signed quantity. It's not quite clear if we can rely on >> and << to implement this as the C standard sounds a bit murky. We might have to figure out how to do this properly.

Note that the code generator does support rotates when the argument is an unsigned quantity, which covers most use cases in practice.

modernize cabal file

the cabal file should list the extensions we rely on (DefaultSignatures, DeriveDataTypeable, etc.) Currently we simply rely on these without any reference to them in the cabal file, which would fail to compile with versions of ghc that don't have these features.. need to collect the entire list of such dependencies and add them based on the cabal other-extensions field, the full recognized list is at: http://hackage.haskell.org/packages/archive/Cabal/1.14.0/doc/html/Language-Haskell-Extension.html#t:KnownExtension

Build failure for of ghc7.4

Hi Levent,

I think SBV 0.9.24 (current Hackage version) on ghc7.4 is failing:

Data/SBV/BitVectors/Model.hs:555:90:
Could not deduce (Show a) arising from a use of show' from the context (Bounded a, Integral a, Num a, SymWord a) bound by the instance declaration at Data/SBV/BitVectors/Model.hs:544:10-66 Possible fix: add (Show a) to the context of the instance declaration In the second argument of(++)', namely
show (minBound :: a, maxBound :: a)' In the second argument of(++)', namely
" is out-of-bounds " ++ show (minBound :: a, maxBound :: a)' In the second argument of(++)', namely
`show x
++ " is out-of-bounds " ++ show (minBound :: a, maxBound :: a)'

I haven't built it myself under 7.4; I just noticed the build log http://hackage.haskell.org/packages/archive/sbv/0.9.24/logs/failure/ghc-7.4

Rework SMTLib2 generation

The SMTLib2 translator is complicated enough that just using strings is a bit of a mess. Need to rework it and go via non-string based (pretty-printer) solution to give it some structure.

Support for array parameters in the C generator

The SBV->C compiler always "flattens" the functions, i.e., sequences of elements will be expanded as the C-function is produced. In idiomatic C, you would instead pass elements in arrays. So, while we can deal with arrays implicitly, the translation is not nice, it leads to signatures that are unnecessarily complicated and not idiomatic-C.

To implement this properly, we need a Haskell datatype that can represent the size of the elements in its type, similar to Cryptol's sized sequences. There is no native Haskell type that can do that, but there are some packages in Hackage that use type-level tricks to encode them. (In particular the type-level library http://hackage.haskell.org/package/type-level.) However, type-level only supports naturals up to 5000 nicely (D0, D1, .., D5000), larger numbers, while possible, are cumbersome to write. Also, the 'D' prefix is just unsightly, and being a library it's not as seamless as one would like.

The other alternative is to wait for type-naturals extension of GHC to become available (http://hackage.haskell.org/trac/ghc/ticket/4385), and use that feature instead, which should provide a better user experience. The downside of that is that it isn't clear when type-naturals will be part of GHC.

In any case, the latter option seems to be the best way forward, although we will have to delay the implementation for a while.

SReals

Finalize implementation of SReals, backed by rationals on the concrete side with infinite precision algebraic reals on the SMT Side.

TODO:

  • Proper support for acquiring both the root-obj and approx. values from Z3.
  • How do we tell if a root-obj is precise?
  • Support for allSat with reals. Tricky since we do need to have root-objects inside Haskell somehow.

Alas, we need Z3 4.0 to be released for development to see the responses..

C code generation with driver and makefile

Hi Levent,

I'd like the option to disable Makefile & example driver generation during C compilation, particularly using the compileToCLib call---it's complicates my other code generation, containing another main()---I'd like to just "build everything" in the directory with the generated code without worrying about main being multiply-defined. I see the machinery is there to do that (at least the driver can be disabled using the CgConfig options), but that's not exposed with the compileToCX API functions.

What do you suggest?

path tracking

tracking path in ite choices might be beneficial. however this interacts badly with sharing. (See inline comments in the implementation of symbolicMerge). Is there a good compromise?

Treatment of uninterpreted functions

Should uninterpreted functions behave like other free_ allocatable items? That would be a more consistent design, but the word/int assumption under free_ is a strong one. Need to explore the design space here.

code generation for SReals

TODO items:

  • add the missing code gen utils for SReals to C (user chosen map type)
  • if user chosen mappings are given (real + integer), add these as comments in generated code
  • test vector generation: support SReal/SIntegers there? Need to see if there's a need
  • search for "realsTBD" tag, marks missing code

Add AES example

Code up AES and show how fast code can be generated from straightforward Haskell, using the T-Box implementation.

Changes to Haskell Num classes

There's some movement in Haskell to remove Eq req from Num class and Num req from Bits class, both of these are welcome changes as SBV instances for these are "forced".. Can't have equality on a symbolic variable, as the result might be symbolic. Make sure to reflect those changes in SBV. (Likely after the "next" haskell platform release.)

Top level CAFs are problematic

Top level CAF's in the presence of uninterpreted functions are buggy. To see define:

  t = proveWith verboseSMTCfg $ f 2 .== f 3
            where f :: SWord16 -> SWord16
                  f = uninterpret "f

This will work correctly the first time you "run" t in ghci, and will choke the second time you run it, as the effects are skipped due to the CAF status of t.

A workaround is to give a dummy unit argument to t and turn it into a function; but would be nice if we didn't have to do that.

A solution along the lines of Andy Gill's reifyGraph idea might solve this problem.

SMTLib2, support for non-constant array creators

Currently the SMTLib2 translator doesn't handle the cases when the mutated/reseted user defined arrays are done via non-constant initializers. This is a rare case actually (in fact, the whole use of user defined arrays is rather rare), but it can happen; so we should support it.

Note that this doesn't affect the SMTLib1 translator since all variables are in global scope. The reason the same trick doesn't work for SMTLib2 is because the non-constant initializer can be inside the forall block, either as an input, or as one of the locally defined let-bound variables.

Diophantine solver example

Create an example for diophantine equation solving. Consider the quantified approach to the "minimal" set problem. Are the solutions required to be all positive? Then, how does one solve something like x+y+z = -2?

make Z3 the default solver

Once Z3 team releases v3.3; where they'll start supporting MacOSX natively.

Note that this is only a two line change in the library, but the real work is in hauling the test-suite to use Z3 and accordingly SMTLib2; this'll require adding a whole bunch of new gold-files and making sure all is well. So, easy but laborious task.

Uninterpreted function/array counterexamples

Currently not very robust in parsing Yices's output for counterexamples in the presence of uninterpreted functions and arrays. Might be worth waiting till SMT-Lib2 stabilizes.

Option to not generate driver

Lee Pike says:

"What about an option for SBV not to generate an example driver? The reason is that for build simplicity, I'd like to just say "build everything in the directory I've put SBV-generated files." But I have my own custom driver with it's own main."

We can easily support this; by means of a codeGen switch..

Start using new doctest

There's a new version of doctest out, with support for blank-lines in the output. Should start using that for inline tests that spit out multi-lines with blanks.

Issues: it has a >= GHC 7.X dependence.. So I need to upgrade too. Also, until Haddock gets the same patch and starts being used in hackage the docs will not go through, which is a huge headache..

So, the change over to the new style will have to wait till Haddock is up..

Better allSat instance with Z3

Now that we talk to Z3 in a more "interactive" way, we can implement allSat by adding extra assumptions, instead of restarting the solver. There's nothing hard in this, just code engineering..

Easier code generation with uninterpreted functions

For code generation with uninterpreted functions, it'd be nicer if we had a construct like:

foo x y z = cgUninterpret "foo" x y z $ (x+y)*z

which would uninterpret the function value during code generation, but otherwise would make it available during simulation/verification. We should be able to implement this with the existing machinery of uninterpreted functions, barring some design choices.. Would be nice to explore..

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.