-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subcircuit.dfy
363 lines (326 loc) · 9.88 KB
/
Subcircuit.dfy
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
module Subcircuit {
import opened Circ
import opened Utils
function SubcircuitComplement(c: Circuit, sc: set<CNode>): (r: set<CNode>)
ensures ScValid(c, r)
{
var all_nodes := AllNodes(c);
reveal ScValid();
all_nodes - sc
}
opaque predicate NPsInSc(sc: set<CNode>, nps: set<NP>)
{
forall np :: np in nps ==> np.n in sc
}
opaque predicate NPsNotInSc(sc: set<CNode>, nps: set<NP>)
{
forall np :: np in nps ==> np.n !in sc
}
lemma ScNoIntersectionNPsNoIntersection(sca: set<CNode>, scb: set<CNode>, npsa: set<NP>, npsb: set<NP>)
requires SetsNoIntersection(sca, scb)
requires NPsInSc(sca, npsa)
requires NPsInSc(scb, npsb)
ensures SetsNoIntersection(npsa, npsb)
{
reveal NPsInSc();
if exists np :: np in npsa && np in npsb {
var np :| np in npsa && np in npsb;
assert np.n in sca;
assert np.n in scb;
assert np.n in sca * scb;
assert false;
}
}
opaque predicate NPsValid(c: Circuit, nps: set<NP>)
{
forall np :: np in nps ==> NPValid(c, np)
}
opaque predicate INPsValid(c: Circuit, nps: set<NP>)
{
forall np :: np in nps ==> INPValid(c, np)
}
opaque predicate ONPsValid(c: Circuit, nps: set<NP>)
{
forall np :: np in nps ==> ONPValid(c, np)
}
lemma INPsAndONPsNoIntersection(c: Circuit, inps: set<NP>, onps: set<NP>)
requires INPsValid(c, inps)
requires ONPsValid(c, onps)
ensures SetsNoIntersection(inps, onps)
{
reveal INPsValid();
reveal ONPsValid();
}
opaque function ConnInputs(c: Circuit, sc: set<CNode>): (r: set<NP>)
requires c.Valid()
requires ScValid(c, sc)
ensures NPsInSc(sc, r)
ensures INPsValid(c, r)
{
reveal Circuit.Valid();
reveal NPsInSc();
reveal INPsValid();
(set np: NP | np.n in sc && np in c.PortSource && c.PortSource[np].n !in sc :: np)
}
opaque function UnconnInputs(c: Circuit, sc: set<CNode>): (r: set<NP>)
requires c.Valid()
requires ScValid(c, sc)
ensures NPsInSc(sc, r)
ensures INPsValid(c, r)
{
reveal NPsInSc();
reveal ScValid();
reveal INPsValid();
(set np: NP | np in AllNPFromNodes(c, sc) && INPValid(c, np) && np !in c.PortSource :: np)
}
lemma UnconnInputsAdd(c: Circuit, sc1: set<CNode>, sc2: set<CNode>)
requires c.Valid()
requires ScValid(c, sc1)
requires ScValid(c, sc2)
ensures ScValid(c, sc1 + sc2)
ensures UnconnInputs(c, sc1) + UnconnInputs(c, sc2) == UnconnInputs(c, sc1 + sc2)
{
reveal ScValid();
reveal UnconnInputs();
}
opaque function ConnOutputs(c: Circuit, sc: set<CNode>): (r: set<NP>)
requires c.Valid()
ensures NPsInSc(sc, r)
ensures ONPsValid(c, r)
{
reveal NPsInSc();
reveal ONPsValid();
reveal Circuit.Valid();
(set np: NP | np.n !in sc && np in c.PortSource && c.PortSource[np].n in sc ::
c.PortSource[np])
}
opaque predicate NodesAllSeq(c: Circuit, nodes: set<CNode>)
{
forall node :: node in nodes ==> node in c.NodeKind && c.NodeKind[node].CSeq?
}
opaque function AllSeq(c: Circuit, sc: set<CNode>): (r: set<CNode>)
requires ScValid(c, sc)
ensures r <= sc
{
reveal ScValid();
(set n | (n in sc) && c.NodeKind[n].CSeq? :: n)
}
opaque function SeqInputs(c: Circuit, sc: set<CNode>): (r: set<NP>)
requires ScValid(c, sc)
ensures NPsInSc(sc, r)
ensures ONPsValid(c, r)
{
reveal NPsInSc();
reveal ScValid();
reveal ONPsValid();
(set n | (n in sc) && c.NodeKind[n].CSeq? :: NP(n, OUTPUT_0))
}
opaque function SeqOutputs(c: Circuit, sc: set<CNode>): (r: set<NP>)
requires ScValid(c, sc)
ensures NPsInSc(sc, r)
ensures INPsValid(c, r)
{
reveal NPsInSc();
reveal ScValid();
reveal INPsValid();
(set n | (n in sc) && c.NodeKind[n].CSeq? :: NP(n, INPUT_0))
}
opaque function AllINPs(c: Circuit, sc: set<CNode>): (r: set<NP>)
requires ScValid(c, sc)
ensures NPsInSc(sc, r)
ensures INPsValid(c, r)
{
reveal NPsInSc();
reveal ScValid();
reveal INPsValid();
(set np | np in AllNPFromNodes(c, sc) && INPValid(c, np) :: np)
}
opaque function AllONPs(c: Circuit, sc: set<CNode>): (r: set<NP>)
requires ScValid(c, sc)
ensures NPsInSc(sc, r)
ensures ONPsValid(c, r)
{
reveal NPsInSc();
reveal ScValid();
reveal ONPsValid();
(set np | np in AllNPFromNodes(c, sc) && ONPValid(c, np) :: np)
}
lemma AllONPsAdd(c: Circuit, sc1: set<CNode>, sc2: set<CNode>)
requires ScValid(c, sc1)
requires ScValid(c, sc2)
ensures
reveal ScValid();
AllONPs(c, sc1 + sc2) == AllONPs(c, sc1) + AllONPs(c, sc2)
{
reveal AllONPs();
reveal ScValid();
}
function AllInputs(c: Circuit, sc: set<CNode>): (r: set<NP>)
requires c.Valid()
requires ScValid(c, sc)
ensures NPsInSc(sc, r)
ensures INPsValid(c, r)
{
reveal NPsInSc();
reveal ScValid();
reveal INPsValid();
reveal ONPsValid();
UnconnInputs(c, sc) + ConnInputs(c, sc)
}
function ConnFromTo(c: Circuit, sca: set<CNode>, scb: set<CNode>): (r: set<NP>)
requires c.Valid()
requires ScValid(c, sca)
requires ScValid(c, scb)
ensures NPsInSc(scb, r)
{
reveal NPsInSc();
reveal ScValid();
(set np: NP | (np.n in scb) && (np in c.PortSource) && (c.PortSource[np].n in sca) :: np)
}
opaque predicate NoConnFromTo(c: Circuit, sca: set<CNode>, scb: set<CNode>)
requires c.Valid()
requires ScValid(c, sca)
requires ScValid(c, scb)
{
|ConnFromTo(c, sca, scb)| == 0
}
function Complement(c: Circuit, sc: set<CNode>): set<CNode>
{
c.NodeKind.Keys - sc
}
lemma InScOrComplement(c: Circuit, sc: set<CNode>, n: CNode)
requires c.Valid()
requires ScValid(c, sc)
requires NodeValid(c, n)
ensures
var sccomp := SubcircuitComplement(c, sc);
((n in sc) || (n in sccomp)) &&
!((n in sc) && (n in sccomp))
{
var sccomp := SubcircuitComplement(c, sc);
reveal Circuit.Valid();
reveal ScValid();
assert (n in sc) || (n in sccomp);
assert !((n in sc) && (n in sccomp));
}
opaque ghost predicate IsIsland(c: Circuit, sc: set<CNode>)
{
&& (forall np :: np in c.PortSource && np.n in sc ==> c.PortSource[np].n in sc)
&& (forall np :: np in c.PortSource && np.n !in sc ==> c.PortSource[np].n !in sc)
}
lemma IsIslandNoConns(c: Circuit, sc1: set<CNode>, sc2: set<CNode>)
requires c.Valid()
requires ScValid(c, sc1)
requires ScValid(c, sc2)
requires SetsNoIntersection(sc1, sc2)
requires IsIsland(c, sc1)
ensures NoConnFromTo(c, sc1, sc2) && NoConnFromTo(c, sc2, sc1)
{
reveal Circuit.Valid();
reveal ScValid();
reveal IsIsland();
reveal NoConnFromTo();
if |ConnFromTo(c, sc1, sc2)| > 0 {
var np :| np in ConnFromTo(c, sc1, sc2);
assert np.n in sc2 && np in c.PortSource && c.PortSource[np].n in sc1;
NotInBoth(np.n, sc1, sc2);
assert false;
}
if |ConnFromTo(c, sc2, sc1)| > 0 {
var np :| np in ConnFromTo(c, sc2, sc1);
assert np.n in sc1 && np in c.PortSource && c.PortSource[np].n in sc2;
NotInBoth(c.PortSource[np].n, sc1, sc2);
assert np.n in sc1 && np in c.PortSource && c.PortSource[np].n !in sc1;
assert false;
}
}
lemma NoConnsToComplementIsIsland(c: Circuit, sc: set<CNode>)
requires c.Valid()
requires ScValid(c, sc)
requires
var sccomp := SubcircuitComplement(c, sc);
NoConnFromTo(c, sc, sccomp) && NoConnFromTo(c, sccomp, sc)
ensures
IsIsland(c, sc)
{
reveal Circuit.Valid();
reveal ScValid();
reveal NoConnFromTo();
reveal IsIsland();
var sccomp := SubcircuitComplement(c, sc);
assert |ConnFromTo(c, sc, sccomp)| == 0;
assert |ConnFromTo(c, sccomp, sc)| == 0;
forall np: NP | np in c.PortSource
ensures np.n in sc ==> c.PortSource[np].n in sc
ensures np.n !in sc ==> c.PortSource[np].n !in sc
{
InScOrComplement(c, sc, np.n);
assert ONPValid(c, c.PortSource[np]);
InScOrComplement(c, sc, c.PortSource[np].n);
if (np.n in sccomp) && (c.PortSource[np].n in sc) {
assert np in ConnFromTo(c, sc, sccomp);
}
assert !((np.n in sccomp) && (c.PortSource[np].n in sc));
if (np.n in sc) && (c.PortSource[np].n in sccomp) {
assert np in ConnFromTo(c, sccomp, sc);
}
assert !((np.n in sc) && (c.PortSource[np].n in sccomp));
if np.n in sc {
assert c.PortSource[np].n in sc;
} else {
assert c.PortSource[np].n !in sc;
}
}
}
lemma SubcircuitComplementOwnInverse(c: Circuit, sc: set<CNode>)
requires c.Valid()
requires ScValid(c, sc)
ensures SubcircuitComplement(c, SubcircuitComplement(c, sc)) == sc
{
reveal Circuit.Valid();
reveal ScValid();
}
lemma IsIslandComplementIsIsland(c: Circuit, sc: set<CNode>)
requires c.Valid()
requires ScValid(c, sc)
requires IsIsland(c, sc)
ensures IsIsland(c, SubcircuitComplement(c, sc))
{
reveal IsIsland();
reveal Circuit.Valid();
reveal ScValid();
var sccomp := SubcircuitComplement(c, sc);
IsIslandNoConns(c, sc, sccomp);
assert NoConnFromTo(c, sc, sccomp);
assert NoConnFromTo(c, sccomp, sc);
SubcircuitComplementOwnInverse(c, sc);
assert sc == SubcircuitComplement(c, sccomp);
NoConnsToComplementIsIsland(c, sccomp);
}
lemma IsIslandNoOutputs(c: Circuit, sc: set<CNode>)
requires c.Valid()
requires ScValid(c, sc)
requires IsIsland(c, sc)
ensures |ConnOutputs(c, sc)| == 0
{
reveal Circuit.Valid();
reveal ScValid();
reveal IsIsland();
reveal ConnOutputs();
}
lemma IsIslandNoInputs(c: Circuit, sc: set<CNode>)
requires c.Valid()
requires ScValid(c, sc)
requires IsIsland(c, sc)
ensures |ConnInputs(c, sc)| == 0
{
reveal Circuit.Valid();
reveal ScValid();
reveal IsIsland();
reveal ConnInputs();
}
opaque predicate PathInSubcircuit(path: seq<NP>, sc: set<CNode>)
{
forall np :: np in path ==> np.n in sc
}
}