In this lab, we'll study how to perform basic theorem proving with Z3 by finishing several exercises. These examples will cover basic logical connectives include implication, disjunction and conjunction. After finishing the examples, you'll do some proofs on your own, these problems will tagged with Exercise. There are some challenging problems which are tagged Challenge, which are optional. Download this code template to start with. For any problems, feel free to ask the TAs for help.
Before starting with this lab, make sure you have finished lab 1 and have Python and Z3 properly installed on your computer.
When you finished the lab, zip you code files (exclude .venv and other temporary folder) with file name <ID>-lab-2.zip (e.g., SA19225789-lab-2.zip), and submit it to USTC Online Teaching Platform.
In this section, let's start by learning how to do proof development with Z3.
Expression | Representation |
---|---|
And | /\ |
Or | \/ |
Not | ¬ |
Implies | -> |
in which, some key points deserve further explanations:
In this section, we'll learn to use these Expression: And, Or, Not, Implies.
Let's start with an example of And(*args).
Example 1:P, Q, R = Bools('P Q R') And(P, Q, R)
Firstly, we need to declare P Q R as Bool type;
then, And(P, Q, R) means proposition : P /\ Q /\ R
We continue to look at our next example of Or(*args):
Example 2:>>> P, Q, R = Bools('P Q R') >>> Or(P, Q, R) Or(P, Q, R)
Firstly, we need to declare P Q R as Bool type;
then, Or(P, Q, R) means proposition : P \/ Q \/ R
We continue to look at our next example of Not(a):
Example 3:>>> P = Bools('P') >>> Not(P) Not(P)
Firstly, we need to declare P as Bool type;
then, Not(P) means proposition : ¬P
We continue to look at our next example of Implies(a, b):
Example 4:>>> P, Q = Bools('P Q') >>> Implies(P, Q) Implies(P, Q)
Firstly, we need to declare P Q as Bool type;
then, Implies(P, Q) means proposition : P -> Q
After learning the usage of these expression, you can try to finish the following exercises.
In this section, we'll learn how to prove predicate logic theorems using Z3. In Z3, we have two additional declarations for full quantifiers:
Expression | Representation |
---|---|
ForAll | ∀x. |
Exists | ∃x. |
in which, some key points deserve further explanations:
In this section, we'll learn to use these Expression: ForAll, Exists.
Let's start with an example of ForAll(vs, body).
Example 5:isort = IntSort() bsort = BoolSort() x = Int('x') P = Function('P', isort, bsort) ForAll(x, P(x)) ForAll(x, P(x))
In Z3, sort refers to the type of data or mathematical structure;
Here, IntSort() represents integer sort, BoolSort() represents
Boolean sort;
Then an Int variable named 'x' was declared;
Then a function (or predicate) P was declared, with an input of
integer type (isort) and an output of Boolean type (bsort)
Finally, we declare the proposition : ForAll(x, P(x)) means proposition : ∀x.P(x)
We continue to look at our next example of Exists(vs, body):
Example 6:isort = IntSort() bsort = BoolSort() x = Int('x') P = Function('P', isort, bsort) Exists(x, P(x)) Exists(x, P(x))
Obviously, we declare the proposition : Exists(x, P(x)) means proposition : ∃x.P(x)
After learning the usage of these expression, you can try to finish the following exercises.
In this section, we need to define some rules to prove the logical theorem.
----------------(odd_1) odd 1 odd n ----------------(odd_ss) odd n + 2Please encode these rules using Z3, then prove that integers 9, 25, and 99 are odd numbers.
After finished the lab, don't forget zip you code files with file name <ID>-lab-2.zip (e.g., SA19225789-lab-2.zip), and submit it to USTC Online Teaching Platform.
Happy hacking!