Skip to content

Commit 469bad9

Browse files
authored
Merge pull request #83 from mvcisback/attr
Use Attr to mirror aiger-bv
2 parents f601fb0 + 2f7d5e7 commit 469bad9

File tree

6 files changed

+19
-12
lines changed

6 files changed

+19
-12
lines changed

aiger/aig.py

Lines changed: 10 additions & 7 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()
@@ -79,9 +81,7 @@ def __repr__(self):
7981
return repr(self._to_aag())
8082

8183
def __getitem__(self, others):
82-
if not isinstance(others, tuple):
83-
return super().__getitem__(others)
84-
84+
assert isinstance(others, tuple) and len(others) == 2
8585
kind, relabels = others
8686
assert kind in {'i', 'o', 'l'}
8787

@@ -98,6 +98,9 @@ def __getitem__(self, others):
9898
input_kinds = (Input,) if kind == 'i' else (LatchIn,)
9999
return seq_compose(cmn.tee(relabels), self, input_kinds=input_kinds)
100100

101+
def evolve(self, **kwargs):
102+
return attr.evolve(self, **kwargs)
103+
101104
@property
102105
def outputs(self):
103106
return frozenset(fn.pluck(0, self.node_map))
@@ -165,7 +168,7 @@ def sub(node):
165168

166169
circ = self._modify_leafs(sub)
167170
_cones = {(l_map[k][0], v) for k, v in circ.latch_map if k in latches}
168-
aig = self._replace(
171+
aig = self.evolve(
169172
node_map=circ.node_map | _cones,
170173
inputs=self.inputs | {n for n, _ in l_map.values()},
171174
latch_map={(k, v) for k, v in circ.latch_map if k not in latches},
@@ -201,7 +204,7 @@ def sub(node):
201204
out2latch = {oname: lname for oname, lname in zip(outputs, latches)}
202205
_latch_map = {(out2latch[k], v) for k, v in _latch_map}
203206
l2init = frozenset((n, val) for n, val in zip(latches, initials))
204-
return aig._replace(
207+
return aig.evolve(
205208
inputs=aig.inputs - set(inputs),
206209
node_map=aig.node_map if keep_outputs else frozenset(node_map),
207210
latch_map=aig.latch_map | _latch_map,
@@ -261,7 +264,7 @@ def _mod(node):
261264

262265
node_map = ((name, _mod(cone)) for name, cone in self.node_map)
263266
latch_map = ((name, _mod(cone)) for name, cone in self.latch_map)
264-
return self._replace(
267+
return self.evolve(
265268
node_map=frozenset(node_map),
266269
latch_map=frozenset(latch_map)
267270
)

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()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
git+git://github.com/mvcisback/hypothesis-cfg.git@master#egg=hypothesis_cfg
2+
attrs==18.1.0
23
bidict==0.17.2
34
blessings==1.7
45
click==6.7

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name='py-aiger',
7-
version='1.0.0',
7+
version='2.0.0',
88
description=DESC,
99
url='http://github.com/mvcisback/py-aiger',
1010
author='Marcell Vazquez-Chanlatte',
@@ -17,6 +17,7 @@
1717
],
1818
},
1919
install_requires=[
20+
'attr',
2021
'bidict',
2122
'click',
2223
'funcy',

0 commit comments

Comments
 (0)