Skip to content

Commit 2c029ea

Browse files
committed
Split SAT into its own section
1 parent 3fa62fd commit 2c029ea

File tree

3 files changed

+50
-50
lines changed

3 files changed

+50
-50
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
- [Building a report tree](./internals/report_tree.md)
1818
- [Testing and benchmarking](./testing/intro.md)
1919
- [Property testing](./testing/property.md)
20+
- [Comparison with a SAT solver](./testing/sat.md)
2021
- [Benchmarking](./testing/benchmarking.md)
2122
- [How can I contribute? Here are some ideas](./contributing.md)

src/testing/property.md

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -139,53 +139,3 @@ Here are some of these:
139139
Only adding dependencies should impact that.
140140
- **Removing a package that does not appear in a solution cannot break that solution**.
141141
Just as before, it should not impact the existence of a solution.
142-
143-
144-
## Comparison with a SAT solver
145-
146-
The [SAT problem](https://en.wikipedia.org/wiki/Boolean_satisfiability_problem)
147-
aims at answering if there exist a set of Boolean variables assignments
148-
satisfying a given Boolean expression.
149-
So if we can formulate dependencies as a Boolean expression,
150-
we can use these well tested tools to compare
151-
the result of pubgrub with the one of a SAT solver.
152-
SAT solvers usually work with Boolean expressions in a
153-
[conjunctive normal form (CNF)](https://en.wikipedia.org/wiki/Conjunctive_normal_form)
154-
meaning a conjunction of clauses, an "_AND_ of _ORs_".
155-
So our goal is to convert the following rules of dependency solving into a set of clauses.
156-
157-
1. There must be only one version per package.
158-
2. Dependencies of a package must be present if that package is present.
159-
3. The root package must be present in the solution.
160-
161-
Each package version \\((p,v)\\) is associated to a unique Boolean variable
162-
\\(b_{p,v}\\) that can be `true` or `false`.
163-
For a given package \\(p\\) and its set of existing versions \\(V_p\\),
164-
the property (1) can be expressed by the set of clauses
165-
166-
\\[ \\{ \neg b_{p,v_1} \lor \neg b_{p,v_2} \ | \ (v_1, v_2) \in V_p^2, v_1 \neq v_2 \\}. \\]
167-
168-
Each one of these clauses specifies that \\(v_1\\) or \\(v_2\\) is not present
169-
in the solution for package \\(p\\).
170-
These clauses are generated by the `sat_at_most_one` function in the testing code.
171-
A more efficent approach is used when a package has 4 or more versions
172-
but the idea is the same.
173-
For a given package version \\((p,v)\\) depending on another package \\(d\\)
174-
at versions \\((v_1, v_2, \cdots, v_k)\\), the property (2) can be encoded by the clause
175-
176-
\\[ \neg b_{p,v} \lor b_{d,v_1} \lor b_{d,v_2} \lor \cdots \lor b_{d,v_k} \\]
177-
178-
specifying that either \\((p,v)\\) is not present in the solution,
179-
or at least one of versions \\((v_1, v_2, \cdots, v_k)\\) of package \\(d\\)
180-
is present in the solution.
181-
Finally, the last constraint of version solving (3) is encoded with the following unit clause,
182-
183-
\\[ b_{r,v_r} \\]
184-
185-
where \\((r, v_r)\\) is the root package version for which we are solving dependencies.
186-
187-
Once the conversion to the SAT formulation is done,
188-
we can verify the following property:
189-
190-
- **If pubgrub finds a solution, the SAT solver is also satisfied by that solution.**
191-
- **If pubgrub does not find a solution, neither does the SAT solver.**

src/testing/sat.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Comparison with a SAT solver
2+
3+
The [SAT problem](https://en.wikipedia.org/wiki/Boolean_satisfiability_problem)
4+
aims at answering if there exist a set of Boolean variables assignments
5+
satisfying a given Boolean expression.
6+
So if we can formulate dependencies as a Boolean expression,
7+
we can use these well tested tools to compare
8+
the result of pubgrub with the one of a SAT solver.
9+
SAT solvers usually work with Boolean expressions in a
10+
[conjunctive normal form (CNF)](https://en.wikipedia.org/wiki/Conjunctive_normal_form)
11+
meaning a conjunction of clauses, an "_AND_ of _ORs_".
12+
So our goal is to convert the following rules of dependency solving into a set of clauses.
13+
14+
1. There must be only one version per package.
15+
2. Dependencies of a package must be present if that package is present.
16+
3. The root package must be present in the solution.
17+
18+
Each package version \\((p,v)\\) is associated to a unique Boolean variable
19+
\\(b_{p,v}\\) that can be `true` or `false`.
20+
For a given package \\(p\\) and its set of existing versions \\(V_p\\),
21+
the property (1) can be expressed by the set of clauses
22+
23+
\\[ \\{ \neg b_{p,v_1} \lor \neg b_{p,v_2} \ | \ (v_1, v_2) \in V_p^2, v_1 \neq v_2 \\}. \\]
24+
25+
Each one of these clauses specifies that \\(v_1\\) or \\(v_2\\) is not present
26+
in the solution for package \\(p\\).
27+
These clauses are generated by the `sat_at_most_one` function in the testing code.
28+
A more efficent approach is used when a package has 4 or more versions
29+
but the idea is the same.
30+
For a given package version \\((p,v)\\) depending on another package \\(d\\)
31+
at versions \\((v_1, v_2, \cdots, v_k)\\), the property (2) can be encoded by the clause
32+
33+
\\[ \neg b_{p,v} \lor b_{d,v_1} \lor b_{d,v_2} \lor \cdots \lor b_{d,v_k} \\]
34+
35+
specifying that either \\((p,v)\\) is not present in the solution,
36+
or at least one of versions \\((v_1, v_2, \cdots, v_k)\\) of package \\(d\\)
37+
is present in the solution.
38+
Finally, the last constraint of version solving (3) is encoded with the following unit clause,
39+
40+
\\[ b_{r,v_r} \\]
41+
42+
where \\((r, v_r)\\) is the root package version for which we are solving dependencies.
43+
44+
Once the conversion to the SAT formulation is done,
45+
we can combine it with property testing,
46+
and verify the following properties:
47+
48+
- **If pubgrub finds a solution, the SAT solver is also satisfied by that solution.**
49+
- **If pubgrub does not find a solution, neither does the SAT solver.**

0 commit comments

Comments
 (0)