Skip to content

Commit ddaaf7f

Browse files
committed
Use attr instead of NamedTuple to mirror aigbv
1 parent f601fb0 commit ddaaf7f

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

aiger/aig.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from functools import reduce
33
from typing import Tuple, FrozenSet, NamedTuple, Union
44

5+
import attr
56
import funcy as fn
67
from toposort import toposort
78

@@ -66,7 +67,8 @@ def _is_const_true(node):
6667
Node = Union[AndGate, ConstFalse, Inverter, Input, LatchIn]
6768

6869

69-
class AIG(NamedTuple):
70+
@attr.s(frozen=True, slots=True, auto_attribs=True, repr=False)
71+
class AIG:
7072
inputs: FrozenSet[str] = frozenset()
7173
node_map: FrozenSet[Tuple[str, Node]] = frozenset()
7274
latch_map: FrozenSet[Tuple[str, Node]] = frozenset()
@@ -98,6 +100,9 @@ def __getitem__(self, others):
98100
input_kinds = (Input,) if kind == 'i' else (LatchIn,)
99101
return seq_compose(cmn.tee(relabels), self, input_kinds=input_kinds)
100102

103+
def evolve(self, **kwargs):
104+
return attr.evolve(self, **kwargs)
105+
101106
@property
102107
def outputs(self):
103108
return frozenset(fn.pluck(0, self.node_map))
@@ -165,7 +170,7 @@ def sub(node):
165170

166171
circ = self._modify_leafs(sub)
167172
_cones = {(l_map[k][0], v) for k, v in circ.latch_map if k in latches}
168-
aig = self._replace(
173+
aig = self.evolve(
169174
node_map=circ.node_map | _cones,
170175
inputs=self.inputs | {n for n, _ in l_map.values()},
171176
latch_map={(k, v) for k, v in circ.latch_map if k not in latches},
@@ -201,7 +206,7 @@ def sub(node):
201206
out2latch = {oname: lname for oname, lname in zip(outputs, latches)}
202207
_latch_map = {(out2latch[k], v) for k, v in _latch_map}
203208
l2init = frozenset((n, val) for n, val in zip(latches, initials))
204-
return aig._replace(
209+
return aig.evolve(
205210
inputs=aig.inputs - set(inputs),
206211
node_map=aig.node_map if keep_outputs else frozenset(node_map),
207212
latch_map=aig.latch_map | _latch_map,
@@ -261,7 +266,7 @@ def _mod(node):
261266

262267
node_map = ((name, _mod(cone)) for name, cone in self.node_map)
263268
latch_map = ((name, _mod(cone)) for name, cone in self.latch_map)
264-
return self._replace(
269+
return self.evolve(
265270
node_map=frozenset(node_map),
266271
latch_map=frozenset(latch_map)
267272
)

aiger/expr.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
from typing import NamedTuple, Union
1+
from typing import Union
22

3+
import attr
34
import funcy as fn
45

56
from aiger import aig
67
from aiger import common as cmn
78

89

9-
class BoolExpr(NamedTuple):
10+
@attr.s(frozen=True, slots=True, cmp=False, auto_attribs=True)
11+
class BoolExpr:
1012
aig: aig.AIG
1113

1214
def __call__(self, inputs):

aiger/hypothesis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def parse(circ_str: str):
6969

7070
def make_circuit(term):
7171
circ_str = ''.join(term)
72-
return parse(circ_str)._replace(comments=(circ_str, ))
72+
return parse(circ_str).evolve(comments=(circ_str, ))
7373

7474

7575
Circuits = st.builds(make_circuit,

aiger/test_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_sink_aag():
5252

5353
@given(aigh.Circuits, st.data())
5454
def test_dummylatches_aag(circ, ddata):
55-
circ2 = circ._replace(
55+
circ2 = circ.evolve(
5656
latch2init={common._fresh(): False}
5757
)
5858
circ3 = circ._to_aag()._to_aig()

0 commit comments

Comments
 (0)