You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
9.5 KiB

1 week ago
  1. Student 1: Name Surname Matriculation Number
  2. Student 2: Name Surname Matriculation Number
  3. ## Basics of z3
  4. The exercises you will solve in the practicals are only going to cover a small subset of the possibilities of solving problems with z3. If you are interested in more background or need to look into some details we suggest you to take a look [here](https://theory.stanford.edu/~nikolaj/programmingz3.html).
  5. ### Types, Sorts and Internal Representation
  6. First, we have to cover the different syntax styles z3 covers. z3 facilitates the SMT-LIB2 standard (you can [read more here](http://smtlib.cs.uiowa.edu/language.shtml), if you are interested) internally. The python API allows us to
  7. ```
  8. p or q
  9. ```
  10. needs to be written like such:
  11. ``` python
  12. Or(p,q)
  13. ```
  14. If you search the internet for examples you will probably stumble upon a syntax like this:
  15. ```
  16. (or p q)
  17. (implies r s)
  18. etc...
  19. ```
  20. which is just a different way of expressing the statement in prefix notation.
  21. The first example that we saw above can be used with the python library [z3-solver](https://pypi.org/project/z3-solver/) (make sure that you `pip install` this and not the `z3` package!).
  22. A very small example program could like like this:
  23. ``` python
  24. # coding: utf-8
  25. import os, sys
  26. from z3 import *
  27. # v-- internal z3 representation
  28. x = Bool('x')
  29. #^-- python variable
  30. # v-- internal z3 representation
  31. gamma = Bool('g') # possible, but not advisable
  32. #^-- python variable
  33. # Declare a solver with which we can do some work
  34. solver = Solver()
  35. p = Bool('p')
  36. qu = Bool('q')
  37. r = Bool('r')
  38. # p -> q, r = ~q, ~p or r
  39. # Add constraints
  40. solver.add(Implies(p,qu))
  41. solver.add(r == Not(qu))
  42. solver.add(Or(Not(p), r))
  43. # solver.add(r == q)
  44. res = solver.check()
  45. if res != sat:
  46. print("unsat")
  47. sys.exit(1)
  48. m = solver.model()
  49. for d in m.decls():
  50. print("%s -> %s" % (d, m[d]))
  51. ```
  52. #### A Simple Example
  53. In a python program we usually follow this workflow:
  54. - import `z3`,
  55. - declare needed variables of specific `Sort` (this is the word we use for types in z3),
  56. - declare a solver: `solver = Solver()` and
  57. - add constraints for the declared variables to the solver.
  58. - After adding all the constraints we tell the solver to try to `check()` for satisfiability and if the solver tells us that the model is satisfiable we may
  59. - print the model.
  60. We can now dissect the example program from above and take a look at which steps we have taken:
  61. At first we import z3 `from z3 import *`.
  62. We then need to declare variables:
  63. ``` python
  64. # v-- internal z3 representation
  65. x = Bool('x')
  66. #^-- python variable
  67. # v-- internal z3 representation
  68. gamma = Bool('g') # possible, but not advisable
  69. #^-- python variable
  70. ```
  71. Lets have a closer look: In z3 we declare variable of some kind of `Sort` just like you would declare variable of some type in other language. z3 has a `BoolSort`, `IntSort`, `RealSort` and some more. Our example from above only covers propositional logic so far so we have declared our variables of type `Bool`.
  72. We have to distinct between z3 variables and python variables. The code block above gives you the answer to this distinction and as the second examples tells you, you may give these two different names, but this is not advisable since it will probably only confuse you and others which need to read your code.
  73. In order to check for satisfiability we are going to need a solver: `solver = Solver()`.
  74. In the next step we will add some constraints to the solver:
  75. ``` python
  76. p = Bool('p')
  77. qu = Bool('qu')
  78. r = Bool('r')
  79. # p -> q, r = ~q, ~p or r
  80. # Add constraints
  81. solver.add(Implies(p,qu))
  82. solver.add(r == Not(qu))
  83. solver.add(Or(Not(p), r))
  84. ```
  85. Adding constraints is done with the solvers `add()` method. Remember that the constraints have to be expressed in prefix notation.
  86. At the very end we have to tell the solver to check whether our constraints are satisfiable, is they are not we simply exit the program:
  87. ``` python
  88. res = solver.check()
  89. if res != sat:
  90. print("unsat")
  91. sys.exit(1)
  92. ```
  93. Our example is satisfiable so in the end we print the model by asking the solver for the created model and print all of the variables which have an associated value in the created model:
  94. ``` python
  95. m = solver.model()
  96. for d in m.decls():
  97. print("%s -> %s" % (d, m[d]))
  98. > q -> True
  99. > p -> False
  100. > r -> False
  101. ```
  102. This can be done with `solver.model().decls()` which lists the assignments as a dictionary. You can also evaluate individual variables in your model:
  103. ``` python
  104. m = solver.model()
  105. print("qu: " + str(m.eval(qu)))
  106. print("p: " + str(m.eval(p)))
  107. print("r: " + str(m.eval(r)))
  108. > qu: True
  109. > p: False
  110. > r: False
  111. ```
  112. ### First Order Logic Types and Constraints
  113. So far we have only touched propositional logic, but z3 is an SMT-solver so lets expand our knowledge to use these funtionalities.
  114. ``` python
  115. from z3 import Solver, Int
  116. from z3 import sat as SAT
  117. x, y = Int('x'), Int("%s" % "y") # create integer variables
  118. solver = Solver() # create a solver
  119. solver.add(x < 6 * y) # assert x < 6y
  120. solver.add(x % 2 == 1) # assert x == 1 mod 2
  121. solver.add(sum([x,y]) == 42) # assert x + y = 42
  122. if solver.check() == SAT: # check if satisfiable
  123. m = solver.model() # retrieve the solution
  124. print(m[x] + m[y]) # print symbolic sum
  125. print(m.eval(x) + m.eval(y)) # use eval to print
  126. # hint: use m[x].as_long() to get python integers
  127. for d in m.decls():
  128. print("%s -> %d" % (d, m[d].as_long()))
  129. > 35 + 7
  130. > 35 + 7
  131. > x -> 35
  132. > y -> 7
  133. ```
  134. From the example above, you can see that creating z3 integer variables follows the same principle as for booleans.
  135. Python expressions are valid in constraints too, for example using a built-in function: `solver.add(sum([x,y]) == 42)`.
  136. ### Custom Datatypes and Sorts
  137. So far we have used z3's capabilities by using boolean or integer valued variables. This already gives us quite a powerful tool, but we want to extend this to be able to use our own custom structures and datatypes. A first approach is to use the `DataType` functionality.
  138. ``` python
  139. Colour = DataType("Colour")
  140. ```
  141. This will create a placeholder that contains constructors and accessors for our custom `Colour` variables.
  142. ``` python
  143. Colour.declare("green")
  144. Colour.declare("yellow")
  145. ColourSort = Colour.create()
  146. ```
  147. We have now defined two constructors for possible values of our `Colour` variable type and finalized the definition of `Colour`. `.create()` returns a sort that we can now work with. z3 will now internally work with these possible values for `Colour`. You may think of `Colour` in the same way as of the `IntSort` mentioned above. Let's consider this once more. We have used `Int(...)` to tell z3 that we want it to create an internal representation of an integer variable. This could be refactored as such:
  148. ``` python
  149. x, y = Const('x', IntSort()), Const("%s" % "y", IntSort()) # create integer variables
  150. ```
  151. This means that `Int("x")` is only syntactic sugar to make our code more legible. But this also tells us how to use our `Colour` datatype:
  152. ``` python
  153. x = Const("cell", ColourSort)
  154. ```
  155. We have used the `DataType` functionality solely to model an enum-type variable. A constructor for such a datatype but might also have some accessor associated with it, allowing us to create algebraic structures like lists or trees.
  156. Another type of a custom structures are uninterpreted sorts. These can be created using `DeclareSort(...)`:
  157. ```python
  158. A = DeclareSort('A')
  159. x, y = Consts('x y', A)
  160. ```
  161. As you can see we may use them in a similar way to the above discussed `DataType`s. z3 will see `x` and `y` as of type `A`. Since these sorts are uninterpreted the do not come with any kind of semantics, i.e. we have no means to compare `x` and `y`. This will be useful for our next topic: Uninterpreted functions.
  162. Note that you do not have to actually `create()` your custom sort, it will be handled like a set of its declared variables.
  163. ### Uninterpreted Functions
  164. Uninterpreted functions give us a way to let z3 model relationships, equalities, etc. between certain variables. A function maps from a set of sorts to a sort.
  165. Consider this example (taken from [here](https://ece.uwaterloo.ca/~agurfink/ece653w17/z3py-advanced)):
  166. ```python
  167. from z3 import *
  168. A = DeclareSort('A')
  169. x, y = Consts('x y', A)
  170. f = Function('f', A, A)
  171. s = Solver()
  172. s.add(f(x) == y, f(f(x)) == x, x != y)
  173. s.check()
  174. m = s.model()
  175. print(m)
  176. print("interpretation assigned to A:")
  177. print("f(x) = " + m.evaluate(f(x)).decl().name())
  178. print("f(y) = " + m.evaluate(f(y)).decl().name())
  179. ```
  180. We use an uninterpreted sort `A` with values `x` and `y`. `f` is declared as a `Function(...)` mapping `A` to `A`. We are then telling the solver that `f` used on `x` will map to `y`, `f` used twice on `x` will give `x` again and that those two values are different. Checking for satisfiability will now check if such a function can exist. If it can the model produced by z3 will contain the look-up table for `f` that we have expected.
  181. ```python
  182. [x = A!val!0,
  183. y = A!val!1,
  184. f = [A!val!1 -> A!val!0, else -> A!val!1]]
  185. interpretation assigned to A:
  186. f(x) = A!val!1
  187. f(y) = A!val!0
  188. ```
  189. This function does not need to be fully defined, as z3 will only check if it can exist with respect to our expressed constraints. In order to get an assignment for all possible values in our sort, we can evaluate the model using the `model_completion=True` flag. This is taked from the `seating-arrangement` example:
  190. ```python
  191. arrangement = ["" for guest in range(len(guests))]
  192. for guest in guests:
  193. arrangement[m.evaluate(position(guest),model_completion=True).as_long()] = guest.decl().name()
  194. ```