what4

Solver-agnostic symbolic values support for issuing queries

https://github.com/GaloisInc/what4

Version on this page:1.5.1
LTS Haskell 22.42:1.5.1
Stackage Nightly 2024-11-24:1.6.2
Latest on Hackage:1.6.2

See all snapshots what4 appears in

BSD-3-Clause licensed by Galois Inc.
This version can be pinned in stack with:what4-1.5.1@sha256:74813c9b8d1fadf1b8da045817c29eb882b745779d36a5270e3eeb28504904dc,10631

Module documentation for 1.5.1

What4

Introduction

What is What4?

What4 is a Haskell library developed at Galois that presents a generic interface to SMT solvers (Z3, Yices, etc.). Users of What4 use an embedded DSL to create fresh constants representing unknown values of various types (integer, boolean, etc.), assert various properties about those constants, and ask a locally-installed SMT solver for satisfying instances.

What4 relies heavily on advanced GHC extensions to ensure that solver expressions are type correct. The parameterized-utils library is used throughout What4 as a “standard library” for dependently-typed Haskell.

Quick start

Let’s start with a quick end-to-end tutorial, demonstrating how to create a model for a basic satisfiability problem and ask a solver for a satisfying instance. The code for this quick start may be found in doc/QuickStart.hs, and you can compile and run the quickstart by executing the following line at the command line from the source root of this package.

$ cabal v2-run what4:quickstart

We will be using an example from the first page of Donald Knuth’s The Art Of Computer Programming, Volume 4, Fascicle 6: Satisfiability:

F(p, q, r) = (p | !q) & (q | r) & (!p | !r) & (!p | !q | r)

We will use What4 to:

  • generate fresh constants for the three variables p, q, and r
  • construct an expression for F
  • assert that expression to our backend solver
  • ask the solver for a satisfying instance.

We first enable the GADTs extension (necessary for most uses of What4) and pull in a number of modules from What4 and parameterized-utils:

{-# LANGUAGE GADTs #-}
module Main where

import Data.Foldable (forM_)
import System.IO (FilePath)

import Data.Parameterized.Nonce (newIONonceGenerator)
import Data.Parameterized.Some (Some(..))

import What4.Config (extendConfig)
import What4.Expr
         ( ExprBuilder,  FloatModeRepr(..), newExprBuilder
         , BoolExpr, GroundValue, groundEval
		 , EmptyExprBuilderState(..) )
import What4.Interface
         ( BaseTypeRepr(..), getConfiguration
         , freshConstant, safeSymbol
         , notPred, orPred, andPred )
import What4.Solver
         (defaultLogData, z3Options, withZ3, SatResult(..))
import What4.Protocol.SMTLib2
         (assume, sessionWriter, runCheckSat)

We create a trivial data type for the “builder state” (which we won’t need to use for this simple example), and create a top-level constant pointing to our backend solver, which is Z3 in this example. (To run this code, you’ll need Z3 on your path, or edit this path to point to your Z3.)

z3executable :: FilePath
z3executable = "z3"

We’re ready to start our main function:

main :: IO ()
main = do
  Some ng <- newIONonceGenerator
  sym <- newExprBuilder FloatIEEERepr EmptyExprBuilderState ng

Most of the functions in What4.Interface, the module for building up solver expressions, require an explicit sym parameter. This parameter is a handle for a data structure that caches information for sharing common subexpressions and other bookkeeping purposes. What4.Expr.Builder.newExprBuilder creates one of these, and we will use this sym throughout our code.

Before continuing, we will set up some global configuration for Z3. This sets up some configurable options specific to Z3 with default values.

  extendConfig z3Options (getConfiguration sym)

We declare fresh constants for each of our propositional variables.

  p <- freshConstant sym (safeSymbol "p") BaseBoolRepr
  q <- freshConstant sym (safeSymbol "q") BaseBoolRepr
  r <- freshConstant sym (safeSymbol "r") BaseBoolRepr

Next, we create expressions for their negation.

  not_p <- notPred sym p
  not_q <- notPred sym q
  not_r <- notPred sym r

Then, we build up each clause of F individually.

  clause1 <- orPred sym p not_q
  clause2 <- orPred sym q r
  clause3 <- orPred sym not_p not_r
  clause4 <- orPred sym not_p =<< orPred sym not_q r

Finally, we can create F out of the conjunction of these four clauses.

  f <- andPred sym clause1 =<<
       andPred sym clause2 =<<
       andPred sym clause3 clause4

Now we can we assert f to the backend solver (Z3, in this example), and ask for a satisfying instance.

  -- Determine if f is satisfiable, and print the instance if one is found.
  checkModel sym f [ ("p", p)
                   , ("q", q)
                   , ("r", r)
                   ]

(The checkModel function is not a What4 function; its definition is provided below.)

Now, let’s add one more clause to F which will make it unsatisfiable.

  -- Now, let's add one more clause to f.
  clause5 <- orPred sym p =<< orPred sym q not_r
  g <- andPred sym f clause5

Now, when we ask the solver for a satisfying instance, it should report that the formulat is unsatisfiable.

  checkModel sym g [ ("p", p)
                   , ("q", q)
                   , ("r", r)
                   ]

This concludes the definition of our main function. The definition for checkModel is as follows:

-- | Determine whether a predicate is satisfiable, and print out the values of a
-- set of expressions if a satisfying instance is found.
checkModel ::
  ExprBuilder t st fs ->
  BoolExpr t ->
  [(String, BoolExpr t)] ->
  IO ()
checkModel sym f es = do
  -- We will use z3 to determine if f is satisfiable.
  withZ3 sym z3executable defaultLogData $ \session -> do
    -- Assume f is true.
    assume (sessionWriter session) f
    runCheckSat session $ \result ->
      case result of
        Sat (ge, _) -> do
          putStrLn "Satisfiable, with model:"
          forM_ es $ \(nm, e) -> do
            v <- groundEval ge e
            putStrLn $ "  " ++ nm ++ " := " ++ show v
        Unsat _ -> putStrLn "Unsatisfiable."
        Unknown -> putStrLn "Solver failed to find a solution."

When we compile this code and run it, we should get the following output.

Satisfiable, with model:
  p := False
  q := False
  r := True
Unsatisfiable.

Where to go next

The key modules to look at when modeling a problem with What4 are:

  • What4.BaseTypes (the datatypes What4 understands)
  • What4.Interface (the functions What4 uses to build symbolic expressions)
  • What4.Expr.Builder (the implementation of the functions in What4.Interface)

The key modules to look at when interacting with a solver are:

  • What4.Protocol.SMTLib2 (the functions to interact with a solver backend)
  • What4.Solver (solver-specific implementations of What4.Protocol.SMTLib2)
  • What4.Solver.*
  • What4.Protocol.Online (interface for online solver connections)
  • What4.SatResult and What4.Expr.GroundEval (for analyzing solver output)

Additional implementation and operational documentation can be found in the implementation documentation in doc/implementation.md.

To serialize and deserialize what4 terms, see the following modules:

  • What4.Serialize.Printer (to serialize what4 terms into an s-expression format)
  • What4.Serialize.Parser (to deserialize what4 terms)
  • What4.Serialize.FastSExpr (provides a faster s-expression parser than the default, intended to be used in conjunction with the higher-level parsing in What4.Serialize.Parser)

Formula Construction vs Solving

In what4, building expressions and solving expressions are orthogonal concerns. When you create an ExprBuilder (with newExprBuilder), you are not committing to any particular solver or solving strategy (except insofar as the selected floating point mode might preclude the use of certain solvers). There are two dimensions of solver choice: solver and mode. The supported solvers are listed in What4.Solver.*. There are two modes:

  • All solvers can be used in an “offline” mode, where a new solver process is created for each query (e.g., via What4.Solver.solver_adapter_check_sat)
  • Many solvers also support an “online” mode, where what4 maintains a persistent connection to the solver and can issue multiple queries to the same solver process (via the interfaces in What4.Protocol.Online)

There are a number of reasons to use solvers in online mode. First, state (i.e., previously defined terms and assumptions) can be shared between queries. For a series of closely related queries that share context, this can be a significant performance benefit. Solvers that support online solving provide the SMT push and pop primitives for maintaining context frames that can be discarded (to define local bindings and assumptions). The canonical use of online solving is symbolic execution, which usually requires reflecting the state of the program at every program point into the solver (in the form of a path condition) and using push and pop to mimic the call and return structure of programs. Second, reusing a single solver instance can save process startup overhead in the presence of many small queries.

While it may always seem advantageous to use the online solving mode, there are advantages to offline solving. As offline solving creates a fresh solver process for each query, it enables parallel solving. Online solving necessarily serializes queries. Additionally, offline solving avoids the need for complex state management to synchronize the solver state with the state of the tool using what4. Additionally, not all solvers that support online interaction support per-goal timeouts; using offline solving trivially allows users of what4 to enforce timeouts for each solved goal.

Known working solver versions

What4 has been tested and is known to work with the following solver versions.

Nearby versions may also work; however, subtle changes in solver behavior from version to version sometimes happen and can cause unexpected results, especially for the more experimental logics that have not been standardized. If you encounter such a situation, please open a ticket, as our goal is to work correctly on as wide a collection of solvers as is reasonable.

  • Z3 versions 4.8.7 through 4.8.12
  • Yices 2.6.1 and 2.6.2
  • CVC4 1.7 and 1.8
  • CVC5 1.0.2
  • Boolector 3.2.1 and 3.2.2
  • STP 2.3.3 (However, note https://github.com/stp/stp/issues/363, which prevents effective retrieval of model values. This should be resolved by the next release)
  • dReal v4.20.04.1

Note that the integration with Z3, Yices and CVC4 has undergone significantly more testing than the other solvers.

Changes

1.5.1 (October 2023)

  • Require building with versions >= 6.0.2.

1.5 (July 2023)

  • Allow building with GHC 9.6.

  • The MonadTrans (PartialT sym) instance now has a IsExpr (SymExpr sym) constraint in its instance context. (This is a requirement imposed by MonadTrans gaining a quantified Monad superclass in mtl-2.3.)

  • Make what4 simplify expressions of the form (bvult (bvadd a c) (bvadd b c)) to (bvult a b) when it is sound to do so.

1.4 (January 2023)

  • Allow building with GHC 9.4.

  • Remove the MonadFail instance for VarRecorder, as this instance is no longer straightforward to define due to upstream changes in base-4.17.0.0. This instance ultimately called error anyways, so any uses of fail at type VarRecorder can be replaced with error without any change in behavior.

  • Remove a dependency on data-binary-ieee754, which has been deprecated.

  • Deprecate allSupported which represents the SMT logic ALL_SUPPORTED, and add allLogic instead which represents the SMTLib standard logic ALL.

  • Add support for the cvc5 SMT solver.

  • Add a get-abduct feature which is compatible with cvc5.

  • Add modules to support serialization and deserialization of what4 terms into an s-expression format that is a superset of SMTLib2. See the What4.Serialize.Printer, What4.Serialize.Parser, and What4.Serialize.FastSExpr modules. Note that these modules have names that conflict with the now deprecated what4-serialize package, from which they were copied. If you are updating to this version of what4, delete your dependency on what4-serialize.

  • Add support Syntax-Guided Synthesis (SyGuS) in CVC5 (through the runCVC5SyGuS function) and Constrained Horn Clauses (CHC) in Z3 (through the runZ3Horn function).

  • Make what4 smarter about simplifying intMin x y and intMax x y expressions when either x <= y or y <= x can be statically determined.

1.3 (April 2022)

  • Allow building with GHC 9.2.

  • According to this discussion, the forall identifier will be claimed, and forall made into a full keyword. Therefore, the forall and exists combinators of What4.Protocol.SMTLib2.Syntax have been renamed into forall_ and exists_.

  • Add operations for increased control over the scope of configuration options, both in the What4.Config and What4.Expr.Builder modules.

  • Previously, the exprCounter, sbUserState, sbUnaryThreshold, and sbCacheStartSize fields of ExprBuilder were directly exposed; in principle this allows users to modify them, which was not intended in some cases. These have been uniformly renamed to remove the sb prefix, and exposed as Getter or Lens values instead, as appropriate.

  • The sbBVDomainRangeLimit fields of ExprBuilder was obsolete and has been removed.

  • Allow building with hashable-1.4.*:

    • Add Eq instances for all data types with Hashable instances that were missing corresponding Eq instances. This is required since hashable-1.4.0.0 adds an Eq superclass to Hashable.
    • Some Hashable instances now have extra constraints to match the constraints in their corresponding Eq instances. For example, the Hashable instance for SymNat now has an extra TestEquality constraint to match its Eq instance.
  • Add an unsafeSetAbstractValue function to the IsExpr class which allows one to manually set the AbstractValue used in a symbolic expression. As the name suggests, this function is unsound in the general case, so use this with caution.

  • Add a What4.Utils.ResolveBounds.BV module, which provides a resolveSymBV function that checks if a SymBV is concrete. If it is not concrete, it returns the lower and upper version bounds, as determined by querying an online SMT solver.

  • Add arrayCopy, arraySet, and arrayRangeEq methods to IsExprBuilder.

  • Add a getUnannotatedTerm method to IsExprBuilder for retrieving the original, unannotated term out of an annotated term.

  • IsExprBuilder now has floatSpecialFunction{,0,1,2} and realSpecialFunction{,0,1,2} methods which allow the use of special values or functions such as pi, trigonometric functions, exponentials, or logarithms. Similarly, IsInterpretedFloatExprBuilder now has iFloatSpecialFunction{,0,1,2} methods. Although little solver support exists for special functions, what4 may be able to prove properties about them in limited cases.

    • The realPi, realLog, realExp, realSin, realCos, realTan, realSinh, realCosh, realTanh, and realAtan2 methods of IsExprBuilder now have default implementations in terms of realSpecialFunction{,0,1,2}.
  • Add an exprUninterpConstants method to IsSymExprBuilder which returns the set of uninterpreted constants in a symbolic expression.

  • Add a natToIntegerPure function which behaves like natToInteger but without using IO.

  • asConcrete now supports concretizing float expressions by way of the new ConcreteFloat constructor in ConcreteVal.

  • Add a z3Tactic configuration option to What4.Solver.Z3 that allows specifying a custom tactic to pass to z3.

  • safeSymbol now replaces exclamation marks (!) with underscores (_) so that the generated names are legal in Verilog.

  • Add Foldable, Traversable, and Show instances for LabeledPred.

  • Fix a bug in which what4 would generate incorrect SMTLib code for array lookups and updates when using the CVC4 backend.

  • Fix a bug in which what4 would incorrectly attempt to configure Boolector 3.2.2 or later.

  • Fix a bug in which strings containing \u or ending with \ would be escaped incorrectly.

1.2.1 (June 2021)

  • Include test suite data in the Hackage tarball.

1.2 (June 2021)

This is primarily a bugfix release, but also adds support for GHC 9.x

  • Tweaks to the SolverEvent data type to remove partial fields.

  • Fix issue #126. The shift operations of What4.SWord were not correctly handling cases where the shift amount has more bits than the word to be shifted.

  • Fix issue #121. The ordering of inputs in generated Verilog files is now more predictable. Previously, it was determined by the order the inputs were encountered during term traversal. Now the user can provide a list of (input, name) pairs which are declared in order. Any additional inputs discovered during traversal will be added after these specified inputs.

  • Fix issue #113. The bvSliceLE and bvSliceBE functions of What4.SWord did not properly handle size 0 bit-vectors and requests for 0 length slices. They now correctly fail for slice lengths longer than 0 on 0-length vectors, and correctly allow 0 length slices regardless of the length of the input.

  • Fix issue #103. Some of the string operations would give incorrect results when string offsets are out-of-bounds. The SMTLib 2.6 standard specifies precise results for these cases, which we now implement.

  • Configuration parameters relative to solvers have been renamed in a more consistent and heirarchical fashion; the old configuration parameters still work but will emit deprecation warnings when used.

    • default_solver –> solver.default
    • abc_path –> solver.abc.path
    • boolector_path –> solver.boolector.path
    • cvc4_path –> solver.cvc4.path
    • cvc4.random-seed –> solver.cvc4.random-seed
    • cvc4_timeout –> solver.cvc4.timeout
    • dreal_path –> solver.dreal.path
    • stp_path –> solver.stp.path
    • stp.random-seed –> solver.stp.random-seed
    • yices_path –> solver.yices.path
    • yices_enable-mcsat –> solver.yices.enable-mcsat
    • yices_enable-interactive –> solver.yices.enable-interactive
    • yices_goal_timeout –> solver.yices.goal-timeout
    • yices.* –> solver.yices.* for many yices internal options
    • z3_path –> solver.z3.path
    • z3_timeout –> solver.z3.timeout
  • Added the solver.strict_parsing configuration parameter. This is enabled by default but could be disabled to allow running solvers in debug mode or to workaround other unexpected output from solver processes.

1.1 (February 2021)

  • Use multithread-safe storage primitive for configuration options, and clarify single-threaded use assumptions for other data structures.

  • Fix issue #63, which caused traversals to include the bodies of defined functions at call sites, which yielded confusing results.

  • Add concrete evaluation and constant folding for floating-point operations via the libBF library.

  • Add min and max operations for integers and reals to the expression interface.

  • Remove BaseNatType from the set of base types. There were bugs relating to having nat types appear in structs, arrays and functions that were difficult to fix. Natural number values are still available as scalars (where they are represented by integers with nonzero assumptions) via the SymNat type.

  • Support for exporting What4 terms to Verilog syntax.

  • Various documentation fixes and improvements.

  • Test coverage improvements.

  • Switch to use the prettyprinter package for user-facing output.

1.0 (July 2020)

  • Initial Hackage release