Skip to content

Commit 7954a3a

Browse files
authored
[mypy] Fixes typing errors in other/dpll (#5759)
+ As per usage examples, clause literals are a list of strings. + Note: symbols extracted from literals are expected to be exactly two characters. + self.literal boolean values are initialized to None, so must be optional + model values should be Booleans, but aren't guaranteed to be non-None in the code. + uses newer '... | None' annotation for Optional values + clauses are passed to the Formula initializer as both lists and sets, they are stored as lists. Returned clauses will always be lists. + use explicit tuple annotation from __future__ rather than using (..., ...) in return signatures + mapping returned by dpll_algorithm is optional per the documentation.
1 parent 765be45 commit 7954a3a

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

Diff for: other/davisb_putnamb_logemannb_loveland.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from __future__ import annotations
1212

1313
import random
14+
from typing import Iterable
1415

1516

1617
class Clause:
@@ -27,12 +28,12 @@ class Clause:
2728
True
2829
"""
2930

30-
def __init__(self, literals: list[int]) -> None:
31+
def __init__(self, literals: list[str]) -> None:
3132
"""
3233
Represent the literals and an assignment in a clause."
3334
"""
3435
# Assign all literals to None initially
35-
self.literals = {literal: None for literal in literals}
36+
self.literals: dict[str, bool | None] = {literal: None for literal in literals}
3637

3738
def __str__(self) -> str:
3839
"""
@@ -52,7 +53,7 @@ def __len__(self) -> int:
5253
"""
5354
return len(self.literals)
5455

55-
def assign(self, model: dict[str, bool]) -> None:
56+
def assign(self, model: dict[str, bool | None]) -> None:
5657
"""
5758
Assign values to literals of the clause as given by model.
5859
"""
@@ -68,7 +69,7 @@ def assign(self, model: dict[str, bool]) -> None:
6869
value = not value
6970
self.literals[literal] = value
7071

71-
def evaluate(self, model: dict[str, bool]) -> bool:
72+
def evaluate(self, model: dict[str, bool | None]) -> bool | None:
7273
"""
7374
Evaluates the clause with the assignments in model.
7475
This has the following steps:
@@ -97,7 +98,7 @@ class Formula:
9798
{{A1, A2, A3'}, {A5', A2', A1}} is ((A1 v A2 v A3') and (A5' v A2' v A1))
9899
"""
99100

100-
def __init__(self, clauses: list[Clause]) -> None:
101+
def __init__(self, clauses: Iterable[Clause]) -> None:
101102
"""
102103
Represent the number of clauses and the clauses themselves.
103104
"""
@@ -139,14 +140,14 @@ def generate_formula() -> Formula:
139140
"""
140141
Randomly generate a formula.
141142
"""
142-
clauses = set()
143+
clauses: set[Clause] = set()
143144
no_of_clauses = random.randint(1, 10)
144145
while len(clauses) < no_of_clauses:
145146
clauses.add(generate_clause())
146-
return Formula(set(clauses))
147+
return Formula(clauses)
147148

148149

149-
def generate_parameters(formula: Formula) -> (list[Clause], list[str]):
150+
def generate_parameters(formula: Formula) -> tuple[list[Clause], list[str]]:
150151
"""
151152
Return the clauses and symbols from a formula.
152153
A symbol is the uncomplemented form of a literal.
@@ -173,8 +174,8 @@ def generate_parameters(formula: Formula) -> (list[Clause], list[str]):
173174

174175

175176
def find_pure_symbols(
176-
clauses: list[Clause], symbols: list[str], model: dict[str, bool]
177-
) -> (list[str], dict[str, bool]):
177+
clauses: list[Clause], symbols: list[str], model: dict[str, bool | None]
178+
) -> tuple[list[str], dict[str, bool | None]]:
178179
"""
179180
Return pure symbols and their values to satisfy clause.
180181
Pure symbols are symbols in a formula that exist only
@@ -198,11 +199,11 @@ def find_pure_symbols(
198199
{'A1': True, 'A2': False, 'A3': True, 'A5': False}
199200
"""
200201
pure_symbols = []
201-
assignment = dict()
202+
assignment: dict[str, bool | None] = dict()
202203
literals = []
203204

204205
for clause in clauses:
205-
if clause.evaluate(model) is True:
206+
if clause.evaluate(model):
206207
continue
207208
for literal in clause.literals:
208209
literals.append(literal)
@@ -225,8 +226,8 @@ def find_pure_symbols(
225226

226227

227228
def find_unit_clauses(
228-
clauses: list[Clause], model: dict[str, bool]
229-
) -> (list[str], dict[str, bool]):
229+
clauses: list[Clause], model: dict[str, bool | None]
230+
) -> tuple[list[str], dict[str, bool | None]]:
230231
"""
231232
Returns the unit symbols and their values to satisfy clause.
232233
Unit symbols are symbols in a formula that are:
@@ -263,7 +264,7 @@ def find_unit_clauses(
263264
Ncount += 1
264265
if Fcount == len(clause) - 1 and Ncount == 1:
265266
unit_symbols.append(sym)
266-
assignment = dict()
267+
assignment: dict[str, bool | None] = dict()
267268
for i in unit_symbols:
268269
symbol = i[:2]
269270
assignment[symbol] = len(i) == 2
@@ -273,8 +274,8 @@ def find_unit_clauses(
273274

274275

275276
def dpll_algorithm(
276-
clauses: list[Clause], symbols: list[str], model: dict[str, bool]
277-
) -> (bool, dict[str, bool]):
277+
clauses: list[Clause], symbols: list[str], model: dict[str, bool | None]
278+
) -> tuple[bool | None, dict[str, bool | None] | None]:
278279
"""
279280
Returns the model if the formula is satisfiable, else None
280281
This has the following steps:

0 commit comments

Comments
 (0)