{
  "cells": [
    {
      "metadata": {
        "collapsed": true
      },
      "cell_type": "markdown",
      "source": "# Strategies\n\nHigh-performance solvers, such as Z3, contain many tightly integrated, handcrafted heuristic combinations of algorithmic proof methods. While these heuristic combinations tend to be highly tuned for known classes of problems, they may easily perform very badly on new classes of problems. This issue is becoming increasingly pressing as solvers begin to gain the attention of practitioners in diverse areas of science and engineering. In many cases, changes to the solver heuristics can make a tremendous difference.\n\nMore information on Z3 is available from https://github.com/z3prover/z3.git\n\n## Introduction\nZ3 implements a methodology for orchestrating reasoning engines where \"big\" symbolic reasoning steps are represented as functions known as tactics, and tactics are composed using combinators known as tacticals. Tactics process sets of formulas called Goals.\n\nWhen a tactic is applied to some goal G, four different outcomes are possible. The tactic succeeds in showing G to be satisfiable (i.e., feasible); succeeds in showing G to be unsatisfiable (i.e., infeasible); produces a sequence of subgoals; or fails. When reducing a goal G to a sequence of subgoals G1, ..., Gn, we face the problem of model conversion. A model converter construct a model for G using a model for some subgoal Gi.\n\nIn the following example, we create a goal g consisting of three formulas, and a tactic t composed of two built-in tactics: simplify and solve-eqs. The tactic simplify apply transformations equivalent to the ones found in the command simplify. The tactic solver-eqs eliminate variables using Gaussian elimination. Actually, solve-eqs is not restricted only to linear arithmetic. It can also eliminate arbitrary variables. Then, combinator Then applies simplify to the input goal and solve-eqs to each subgoal produced by simplify. In this example, only one subgoal is produced."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "!pip install \"z3-solver\"\n\nfrom z3 import *\n\nx, y = Reals('x y')\ng  = Goal()\ng.add(x > 0, y > 0, x == y + 2)\nprint(g)\n\nt1 = Tactic('simplify')\nt2 = Tactic('solve-eqs')\nt  = Then(t1, t2)\nprint(t(g))",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "In the example above, variable x is eliminated, and is not present the resultant goal.\n\nIn Z3, we say a clause is any constraint of the form Or(f_1, ..., f_n). The tactic split-clause will select a clause Or(f_1, ..., f_n) in the input goal, and split it n subgoals. One for each subformula f_i."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "x, y = Reals('x y')\ng  = Goal()\ng.add(Or(x < 0, x > 0), x == y + 1, y < 0)\n\nt = Tactic('split-clause')\nr = t(g)\nfor g in r: \n    print(g)",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "Tactics\nZ3 comes equipped with many built-in tactics. The command describe_tactics() provides a short description of all built-in tactics.\n\n describe_tactics()\nZ3Py comes equipped with the following tactic combinators (aka tacticals):\n\n* Then(t, s) applies t to the input goal and s to every subgoal produced by t.\n* OrElse(t, s) first applies t to the given goal, if it fails then returns the result of s applied to the given goal.\n* Repeat(t) Keep applying the given tactic until no subgoal is modified by it.\n* Repeat(t, n) Keep applying the given tactic until no subgoal is modified by it, or the number of iterations is greater than n.\n* TryFor(t, ms) Apply tactic t to the input goal, if it does not return in ms milliseconds, it fails.\n* With(t, params) Apply the given tactic using the given parameters.\n\nThe following example demonstrate how to use these combinators."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "x, y, z = Reals('x y z')\ng = Goal()\ng.add(Or(x == 0, x == 1), \n      Or(y == 0, y == 1), \n      Or(z == 0, z == 1),\n      x + y + z > 2)\n\n# Split all clauses\"\nsplit_all = Repeat(OrElse(Tactic('split-clause'),\n                          Tactic('skip')))\nprint(split_all(g))\n\nsplit_at_most_2 = Repeat(OrElse(Tactic('split-clause'),\n                          Tactic('skip')),\n                         1)\nprint(split_at_most_2(g))\n\n# Split all clauses and solve equations\nsplit_solve = Then(Repeat(OrElse(Tactic('split-clause'),\n                                 Tactic('skip'))),\n                   Tactic('solve-eqs'))\n\nprint(split_solve(g))",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "In the tactic split_solver, the tactic solve-eqs discharges all but one goal. Note that, this tactic generates one goal: the empty goal which is trivially satisfiable (i.e., feasible)\n\nThe list of subgoals can be easily traversed using the Python for statement."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "x, y, z = Reals('x y z')\ng = Goal()\ng.add(Or(x == 0, x == 1), \n      Or(y == 0, y == 1), \n      Or(z == 0, z == 1),\n      x + y + z > 2)\n\n# Split all clauses\"\nsplit_all = Repeat(OrElse(Tactic('split-clause'),\n                          Tactic('skip')))\nfor s in split_all(g):\n    print(s)",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "A tactic can be converted into a solver object using the method solver(). If the tactic produces the empty goal, then the associated solver returns sat. If the tactic produces a single goal containing False, then the solver returns unsat. Otherwise, it returns unknown."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "bv_solver = Then('simplify', \n                 'solve-eqs', \n                 'bit-blast', \n                 'sat').solver()\n\nx, y = BitVecs('x y', 16)\nsolve_using(bv_solver, x | y == 13, x > y)",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "In the example above, the tactic bv_solver implements a basic bit-vector solver using equation solving, bit-blasting, and a propositional SAT solver. Note that, the command Tactic is suppressed. All Z3Py combinators automatically invoke Tactic command if the argument is a string. Finally, the command solve_using is a variant of the solve command where the first argument specifies the solver to be used.\n\nIn the following example, we use the solver API directly instead of the command solve_using. We use the combinator With to configure our little solver. We also include the tactic aig which tries to compress Boolean formulas using And-Inverted Graphs."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "bv_solver = Then(With('simplify', mul2concat=True),\n                 'solve-eqs', \n                 'bit-blast', \n                 'aig',\n                 'sat').solver()\nx, y = BitVecs('x y', 16)\nbv_solver.add(x*32 + y == 13, x & y < 10, y > -100)\nprint(bv_solver.check())\nm = bv_solver.model()\nprint(m)\nprint(x*32 + y, \"==\", m.evaluate(x*32 + y))\nprint(x & y, \"==\", m.evaluate(x & y))",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "The tactic smt wraps the main solver in Z3 as a tactic."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "x, y = Ints('x y')\ns = Tactic('smt').solver()\ns.add(x > y + 1)\nprint(s.check())\nprint(s.model())",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "Now, we show how to implement a solver for integer arithmetic using SAT. The solver is complete only for problems where every variable has a lower and upper bound."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "s = Then(With('simplify', arith_lhs=True, som=True),\n         'normalize-bounds', 'lia2pb', 'pb2bv', \n         'bit-blast', 'sat').solver()\nx, y, z = Ints('x y z')\nsolve_using(s, \n            x > 0, x < 10, \n            y > 0, y < 10, \n            z > 0, z < 10,\n            3*y + 2*x == z)\n# It fails on the next example (it is unbounded)\ns.reset()\nsolve_using(s, 3*y + 2*x == z)",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "Tactics can be combined with solvers. For example, we can apply a tactic to a goal, produced a set of subgoals, then select one of the subgoals and solve it using a solver. The next example demonstrates how to do that, and how to use model converters to convert a model for a subgoal into a model for the original goal."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "t = Then('simplify', \n         'normalize-bounds', \n         'solve-eqs')\n\nx, y, z = Ints('x y z')\ng = Goal()\ng.add(x > 10, y == x + 3, z > y)\n\nr = t(g)\n# r contains only one subgoal\nprint(r)\n\ns = Solver()\ns.add(r[0])\nprint(s.check())\n# Model for the subgoal\nprint(s.model())\n# Model for the original goal\nprint(r[0].convert_model(s.model()))",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "## Probes\n\nProbes (aka formula measures) are evaluated over goals. Boolean expressions over them can be built using relational operators and Boolean connectives. The tactic FailIf(cond) fails if the given goal does not satisfy the condition cond. Many numeric and Boolean measures are available in Z3Py. The command describe_probes() provides the list of all built-in probes."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "describe_probes()",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "In the following example, we build a simple tactic using FailIf. It also shows that a probe can be applied directly to a goal."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "x, y, z = Reals('x y z')\ng = Goal()\ng.add(x + y + z > 0)\n\np = Probe('num-consts')\nprint(\"num-consts:\", p(g))\n\nt = FailIf(p > 2)\ntry:\n    t(g)\nexcept Z3Exception:\n    print(\"tactic failed\")\n\nprint(\"trying again...\")\ng = Goal()\ng.add(x + y > 0)\nprint(t(g))",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {},
      "cell_type": "markdown",
      "source": "Z3Py also provides the combinator (tactical) If(p, t1, t2) which is a shorthand for:\n\nOrElse(Then(FailIf(Not(p)), t1), t2)\n\nThe combinator When(p, t) is a shorthand for:\n\nIf(p, t, 'skip')\nThe tactic skip just returns the input goal. The following example demonstrates how to use the If combinator."
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "x, y, z = Reals('x y z')\ng = Goal()\ng.add(x**2 - y**2 >= 0)\n\np = Probe('num-consts')\nt = If(p > 2, 'simplify', 'factor')\n\nprint(t(g))\n\ng = Goal()\ng.add(x + x + y + z >= 0, x**2 - y**2 >= 0)\n\nprint(t(g))",
      "execution_count": null,
      "outputs": []
    },
    {
      "metadata": {
        "trusted": true
      },
      "cell_type": "code",
      "source": "",
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3",
      "language": "python"
    },
    "language_info": {
      "mimetype": "text/x-python",
      "nbconvert_exporter": "python",
      "name": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.6",
      "file_extension": ".py",
      "codemirror_mode": {
        "version": 3,
        "name": "ipython"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}