-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoc.py
172 lines (153 loc) · 3.1 KB
/
poc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""Demonstration of series-parallel representation of Wheatstone graph"""
from typing import Callable, Tuple, TypeVar, Any
U = int # up; source
L = int # left
UR = int # right1
LR = int # right2
R = Tuple[LR, UR]
DL = int # bottom
DR = int # bottom
D = Tuple[DL, DR] # down; sink
def make_wheatstone_naive(
ul: Callable[[U], L],
ur: Callable[[U], UR],
bridge: Callable[[L], LR],
ld: Callable[[L], DL],
rd: Callable[[R], DR]
) -> Callable[[U], D]:
r"""
This graph is not series parallel.
U
| \
| \
L -> R
\ |
\ |
D
"""
def wheatstone(x: U) -> D:
l = ul(x)
return (ld(l), rd((bridge(l), ur(x))))
return wheatstone
# Let's make it series parallel!
X = TypeVar("X")
Y = TypeVar("Y")
Z = TypeVar("Z")
def s(
bef: Callable[[X,], Y],
aft: Callable[[Y,], Z],
) -> Callable[[X,], Z]:
"""Series composition"""
def _(x: X) -> Z:
return aft(bef(x))
return _
def p(
l: Callable[[X,], Y],
r: Callable[[X,], Z],
) -> Callable[[X,], Tuple[Y, Z]]:
"""Parallel composition"""
def _(x: X) -> Tuple[Y, Z]:
return (l(x), r(x))
return _
def left(xy: Tuple[X, Any]) -> X:
"""Simple left getter"""
return xy[0]
def right(xy: Tuple[Any, Y]) -> Y:
"""Simple right getter"""
return xy[1]
# There are 3 possible series-parallel representations with modified edges
# Strategy 1; Delegate data pass of `ld` to `bridge` and `rd``
def make_wheatstone1(
ul: Callable[[U], L],
ur: Callable[[U], UR],
bridge: Callable[[L], LR],
ld: Callable[[L], DL],
rd: Callable[[R], DR]
) -> Callable[[U], D]:
r"""
Modification strategy #1
U
| \
| \
L => R'
|
|
D
"""
return s(
p(
s(
ul, p(ld, bridge)
),
ur
),
p(
s(left, left),
s(
p(s(left, right), right),
rd
)
)
)
# Strategy 2; Delegate data pass of `ur` to `ul` and `bridge`
def make_wheatstone2(
ul: Callable[[U], L],
ur: Callable[[U], UR],
bridge: Callable[[L], LR],
ld: Callable[[L], DL],
rd: Callable[[R], DR]
) -> Callable[[U], D]:
r"""
Modification strategy #2
U
|
|
L' => R
\ |
\ |
D
"""
return s(
p(ul, ur),
p(
s(left, ld),
s(
p(
s(left, bridge),
right
),
rd,
)
)
)
# Strategy 3; Hide the bridge
def make_wheatstone3(
ul: Callable[[U], L],
ur: Callable[[U], UR],
bridge: Callable[[L], LR],
ld: Callable[[L], DL],
rd: Callable[[R], DR]
) -> Callable[[U], D]:
r"""
Modification strategy #3
U
||
||
LR
||
||
D
"""
return s(
p(ul, ur),
p(
s(left, ld),
s(
p(
s(left, bridge),
right
),
rd,
)
)
)