-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChapter07.swift
More file actions
554 lines (513 loc) · 16.1 KB
/
Chapter07.swift
File metadata and controls
554 lines (513 loc) · 16.1 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//
// Chapter07.swift
// ElementsOfProgramming
//
import EOP
func weightRecursive<C: BifurcateCoordinate>(c: C) -> WeightType {
// Precondition: tree(c)
guard !c.isEmpty() else { return 0 }
var l = N(0), r = N(0)
if let ls = c.leftSuccessor {
l = weightRecursive(c: ls)
}
if let rs = c.rightSuccessor {
r = weightRecursive(c: rs)
}
let t = l + r
return t.successor()
}
func heightRecursive<C: BifurcateCoordinate>(c: C) -> WeightType {
// Precondition: tree(c)
guard !c.isEmpty() else { return 0 }
var l = N(0), r = N(0)
if let ls = c.leftSuccessor {
l = heightRecursive(c: ls)
}
if let rs = c.rightSuccessor {
r = heightRecursive(c: rs)
}
return maxSelect(a: l, b: r).successor()
}
enum Visit: Int, Comparable {
case pre = 1, `in` = 2, post = 3
static func <(lhs: Visit, rhs: Visit) -> Bool {
return lhs.rawValue < rhs.rawValue
}
}
func traverseNonempty<
C: BifurcateCoordinate,
P: BinaryProcedure
>(
c: C,
proc: P
) -> P
where P.BinaryProcedureType1 == Visit, P.BinaryProcedureType2 == C {
var proc = proc
// Precondition: tree(c) ∧ ¬empty(c)
proc.call(.pre, c)
if let ls = c.leftSuccessor {
proc = traverseNonempty(c: ls, proc: proc)
}
proc.call(.in, c)
if let rs = c.rightSuccessor {
proc = traverseNonempty(c: rs, proc: proc)
}
proc.call(.post, c)
return proc
}
func isLeftSuccessor<T: BidirectionalBifurcateCoordinate>(j: T) -> Bool? {
// Precondition: has_predecessor(j)
guard let i = j.iteratorPredecessor else { return nil }
guard let ls = i.leftSuccessor else { return false }
return ls == j
}
func isRightSuccessor<T: BidirectionalBifurcateCoordinate>(j: T) -> Bool? {
// Precondition: has_predecessor(j)
guard let i = j.iteratorPredecessor else { return nil }
guard let rs = i.rightSuccessor else { return false }
return rs == j
}
func traverseStep<C: BidirectionalBifurcateCoordinate>(
v: inout Visit,
c: inout C
) -> Int? {
// Precondition: has_predecessor(c) ∨ v ≠ post
switch v {
case .pre:
guard let ls = c.leftSuccessor else {
v = .in
return 0
}
c = ls
return 1
case .in:
guard let rs = c.rightSuccessor else {
v = .post
return 0
}
v = .pre
c = rs
return 1
case .post:
guard let ils = isLeftSuccessor(j: c) else { return nil }
if ils {
v = .in
}
guard let p = c.iteratorPredecessor else { return nil }
c = p
return -1
}
}
func reachable<C: BidirectionalBifurcateCoordinate>(
x: C, y: C
) -> Bool {
var x = x
// Precondition: tree(x)
guard !x.isEmpty() else { return false }
let root = x
var v = Visit.pre
repeat {
guard x != y else { return true }
guard let _ = traverseStep(v: &v, c: &x) else { return false }
} while x != root || v != .post
return false
}
func weight<C: BidirectionalBifurcateCoordinate>(c: C) -> WeightType? {
var c = c
// Precondition: tree(c)
guard !c.isEmpty() else { return 0 }
let root = c
var v = Visit.pre
var n = N(1) // Invariant: n is count of .pre visits so far
repeat {
guard let _ = traverseStep(v: &v, c: &c) else { return nil }
if v == .pre { n = n.successor() }
} while c != root || v != .post
return n
}
func height<C: BidirectionalBifurcateCoordinate>(c: C) -> WeightType? {
var c = c
// Precondition: tree(c)
guard !c.isEmpty() else { return 0 }
let root = c
var v = Visit.pre
var n = N(1) // Invariant: n is max of height of .pre visits so far
var m = N(1) // Invariant: m is height of current .pre visit
repeat {
guard let ts = traverseStep(v: &v, c: &c) else { return nil }
m = (m - 1) + N(ts + 1)
n = max(n, m)
} while c != root || v != .post
return n
}
func traverse<
C: BidirectionalBifurcateCoordinate,
P: BinaryProcedure
>(
c: C,
proc: P
) -> P?
where P.BinaryProcedureType1 == Visit, P.BinaryProcedureType2 == C {
var c = c
// Precondition: tree(c)
guard !c.isEmpty() else { return proc }
let root = c
var v = Visit.pre
proc.call(.pre, c)
repeat {
guard let _ = traverseStep(v: &v, c: &c) else { return nil }
proc.call(v, c)
} while c != root || v != .post
return proc
}
// Exercise 7.3: Use traverse_step and the procedures of Chapter 2 to determine
// whether the descendants of a bidirectional bifurcate coordinate form a DAG
func bifurcateIsomorphicNonempty<
C0: BifurcateCoordinate,
C1: BifurcateCoordinate
>(
c0: C0,
c1: C1
) -> Bool {
// Precondition: tree(c0) ∧ tree(c1) ∧ ¬empty(c0) ∧ ¬empty(c1)
if let c0ls = c0.leftSuccessor, let c1ls = c1.leftSuccessor {
guard bifurcateIsomorphicNonempty(c0: c0ls,
c1: c1ls) else { return false }
} else if let _ = c1.leftSuccessor { return false }
if let c0rs = c0.rightSuccessor, let c1rs = c1.rightSuccessor {
guard bifurcateIsomorphicNonempty(c0: c0rs,
c1: c1rs) else { return false }
} else if let _ = c1.rightSuccessor { return false }
return true
}
func bifurcateIsomorphic<
C0: BidirectionalBifurcateCoordinate,
C1: BidirectionalBifurcateCoordinate
>(
c0: C0,
c1: C1
) -> Bool? {
var c0 = c0, c1 = c1
// Precondition: tree(c0) ∧ tree(c1)
guard !c0.isEmpty() else { return c1.isEmpty() }
guard !c1.isEmpty() else { return false }
let root0 = c0
var v0 = Visit.pre
var v1 = Visit.pre
while true {
guard let _ = traverseStep(v: &v0, c: &c0),
let _ = traverseStep(v: &v1, c: &c1) else { return nil }
guard v0 == v1 else { return false }
if c0 == root0 && v0 == .post { return true }
}
}
func lexicographicalEquivalent<
I0: Readable & Iterator,
I1: Readable & Iterator
>(
f0: I0, l0: I0,
f1: I1, l1: I1,
r: Relation<I0.Source>
) -> Bool?
where I0.Source == I1.Source {
// Precondition: readable_bounded_range(f0, l0)
// Precondition: readable_bounded_range(f1, l1)
// Precondition: equivalence(r)
guard let p: Pair<I0, I1> = findMismatch(f0: f0, l0: l0,
f1: f1, l1: l1,
r: r) else { return nil }
return p.m0 == l0 && p.m1 == l1
}
func lexicographicalEqual<
I0: Readable & Iterator,
I1: Readable & Iterator
>(
f0: I0, l0: I0,
f1: I1, l1: I1
) -> Bool?
where I0.Source == I1.Source {
return lexicographicalEquivalent(f0: f0, l0: l0,
f1: f1, l1: l1,
r: equal)
}
// Could specialize to use lexicographic_equal for k > some cutoff
func lexicographicalEqual<
I0: Readable & ForwardIterator,
I1: Readable & ForwardIterator
>(
k: Int,
f0: I0,
f1: I1
) -> Bool?
where I0.Source == I1.Source {
guard k != 0 else { return true }
guard f0.source == f1.source else { return false }
guard let f0s = f0.iteratorSuccessor,
let f1s = f1.iteratorSuccessor else { return nil }
return lexicographicalEqual(k: k - 1,
f0: f0s,
f1: f1s)
}
func bifurcateEquivalentNonempty<
C0: Readable & BifurcateCoordinate,
C1: Readable & BifurcateCoordinate
>(
c0: C0,
c1: C1,
r: Relation<C0.Source>
) -> Bool?
where C0.Source == C1.Source {
// Precondition: readable_tree(c0) ∧ readable_tree(c1)
// Precondition: ¬empty(c0) ∧ ¬empty(c1)
// Precondition: equivalence(r)
guard let c0src = c0.source,
let c1src = c1.source else { return nil }
guard r(c0src, c1src) else { return false }
if let c0ls = c0.leftSuccessor, let c1ls = c1.leftSuccessor {
guard let b = bifurcateEquivalentNonempty(c0: c0ls,
c1: c1ls,
r: r) else { return nil }
guard b else { return false }
} else if let _ = c1.leftSuccessor { return false }
if let c0rs = c0.rightSuccessor, let c1rs = c1.rightSuccessor {
guard let b = bifurcateEquivalentNonempty(c0: c0rs,
c1: c1rs,
r: r) else { return nil }
guard b else { return false }
} else if let _ = c1.rightSuccessor { return false }
return true
}
func bifurcateEquivalent<
C0: Readable & BidirectionalBifurcateCoordinate,
C1: Readable & BidirectionalBifurcateCoordinate
>(
c0: C0,
c1: C1,
r: Relation<C0.Source>
) -> Bool?
where C0.Source == C1.Source {
var c0 = c0, c1 = c1
// Precondition: readable_tree(c0) ∧ readable_tree(c1)
// Precondition: equivalence(r)
guard !c0.isEmpty() else { return c1.isEmpty() }
guard !c1.isEmpty() else { return false }
let root0 = c0
var v0 = Visit.pre
var v1 = Visit.pre
while true {
guard let c0src = c0.source,
let c1src = c1.source else { return nil }
guard !(v0 == .pre && !r(c0src, c1src)) else { return false }
guard let _ = traverseStep(v: &v0, c: &c0),
let _ = traverseStep(v: &v1, c: &c1) else { return nil }
guard v0 == v1 else { return false }
if c0 == root0 && v0 == .post { return true }
}
}
func bifurcateEqual<
C0: Readable & BidirectionalBifurcateCoordinate,
C1: Readable & BidirectionalBifurcateCoordinate
>(
c0: C0,
c1: C1
) -> Bool?
where C0.Source == C1.Source {
return bifurcateEquivalent(c0: c0, c1: c1, r: equal)
}
func lexicographicalCompare<
I0: Readable & Iterator,
I1: Readable & Iterator
>(
f0: I0, l0: I0,
f1: I1, l1: I1,
r: Relation<I0.Source>
) -> Bool?
where I0.Source == I1.Source {
var f0 = f0, f1 = f1
// Precondition: readable_bounded_range(f0, l0)
// Precondition: readable_bounded_range(f1, l1)
// Precondition: weak_ordering(r)
while true {
guard f1 != l1 else { return false }
guard f0 != l0 else { return true }
guard let f0src = f0.source,
let f1src = f1.source else { return nil }
guard !r(f0src, f1src) else { return true }
guard !r(f1src, f0src) else { return false }
guard let f0s = f0.iteratorSuccessor,
let f1s = f1.iteratorSuccessor else { return nil }
f0 = f0s
f1 = f1s
}
}
func lexicographicalLess<
I0: Readable & Iterator,
I1: Readable & Iterator
>(
f0: I0, l0: I0,
f1: I1, l1: I1
) -> Bool?
where I0.Source == I1.Source {
return lexicographicalCompare(f0: f0, l0: l0,
f1: f1, l1: l1,
r: less)
}
func lexicographicalLess<
I0: Readable & ForwardIterator,
I1: Readable & ForwardIterator
>(
k: Int,
f0: I0,
f1: I1
) -> Bool?
where I0.Source == I1.Source {
guard k != 0 else { return false }
guard let f0src = f0.source,
let f1src = f1.source else { return nil }
guard f0src >= f1src else { return true }
guard f0src <= f1src else { return false }
guard let f0s = f0.iteratorSuccessor,
let f1s = f1.iteratorSuccessor else { return nil }
return lexicographicalLess(k: k - 1, f0: f0s, f1: f1s)
}
// Exercise 7.6: bifurcate_compare_nonempty (using 3-way comparsion)
// concept Comparator3Way(F) is
// HomogeneousFunction(F)
// /\ Arity(F) = 2
// /\ Codomain(F) = int
// property(F : Comparator3Way)
// three_way_compare : F
// f |- (all a,b in Domain(F)) f(a, b) in {-1, 0, 1}
// Also need axioms equivalent to weak_order : transitivity, etc.
// We could relax this to OrderedAdditiveGroup
// (allowing subtraction as the comparator for numbers)
// Should sense of positive/negative be flipped?
func comparatorThreeWay<DomainR: Regular>(
r: @escaping Relation<DomainR>
) -> BinaryHomogeneousFunction<DomainR, Int> {
// Precondition: weak_ordering(r)
// Postcondition: three_way_compare(comparator_3_way(r))
return { a, b in
if r(a, b) { return 1 }
if r(b, a) { return -1 }
return 0
}
}
func lexicographicalCompareThreeWay<
I0: Readable & Iterator,
I1: Readable & Iterator
>(
f0: I0, l0: I0,
f1: I1, l1: I1,
comp: BinaryHomogeneousFunction<I0.Source, Int>
) -> Int?
where I0.Source == I1.Source {
var f0 = f0, f1 = f1
// Precondition: readable_bounded_range(f0, l0)
// Precondition: readable_bounded_range(f1, l1)
// Precondition: three_way_compare(comp)
while true {
guard f0 != l0 else {
guard f1 == l1 else { return 1 }
return 0
}
guard f1 != l1 else { return -1 }
guard let f0src = f0.source,
let f1src = f1.source else { return nil }
let tmp = comp(f0src, f1src)
guard tmp == 0 else { return tmp }
guard let f0s = f0.iteratorSuccessor,
let f1s = f1.iteratorSuccessor else { return nil }
f0 = f0s
f1 = f1s
}
}
func bifurcateCompareNonempty<
C0: Readable & BifurcateCoordinate,
C1: Readable & BifurcateCoordinate
>(
c0: C0,
c1: C1,
comp: BinaryHomogeneousFunction<C0.Source, Int>
) -> Int?
where C0.Source == C1.Source {
// Precondition: readable_tree(c0) ∧ readable_tree(c1)
// Precondition: ¬empty(c0) ∧ ¬empty(c1)
// Precondition: three_way_compare(comp)
guard let c0src = c0.source,
let c1src = c1.source else { return nil }
var tmp = comp(c0src, c1src)
guard tmp == 0 else { return tmp }
if let c0ls = c0.leftSuccessor {
guard let c1ls = c1.leftSuccessor else { return -1 }
guard let t = bifurcateCompareNonempty(c0: c0ls,
c1: c1ls,
comp: comp) else { return nil }
tmp = t
guard tmp == 0 else { return tmp }
} else if let _ = c1.leftSuccessor { return 1 }
if let c0rs = c0.rightSuccessor {
guard let c1rs = c1.rightSuccessor else { return -1 }
guard let t = bifurcateCompareNonempty(c0: c0rs,
c1: c1rs,
comp: comp) else { return nil }
tmp = t
guard tmp == 0 else { return tmp }
} else if let _ = c1.rightSuccessor { return 1 }
return 0
}
func bifurcateCompare<
C0: Readable & BidirectionalBifurcateCoordinate,
C1: Readable & BidirectionalBifurcateCoordinate
>(
c0: C0,
c1: C1,
r: Relation<C0.Source>
) -> Bool?
where C0.Source == C1.Source {
var c0 = c0, c1 = c1
// Precondition: readable_tree(c0) ∧ readable_tree(c1) ∧ weak_ordering(r)
guard !c1.isEmpty() else { return false }
guard !c0.isEmpty() else { return true }
let root0 = c0
var v0 = Visit.pre
var v1 = Visit.pre
while true {
if v0 == .pre {
guard let c0src = c0.source,
let c1src = c1.source else { return nil }
guard !r(c0src, c1src) else { return true }
guard !r(c1src, c0src) else { return false }
}
guard let _ = traverseStep(v: &v0, c: &c0),
let _ = traverseStep(v: &v1, c: &c1) else { return nil }
guard v0 == v1 else { return v0 > v1 }
if c0 == root0 && v0 == .post { return false }
}
}
func bifurcateLess<
C0: Readable & BidirectionalBifurcateCoordinate,
C1: Readable & BidirectionalBifurcateCoordinate
>(
c0: C0,
c1: C1
) -> Bool?
where C0.Source == C1.Source {
// Precondition: readable_tree(c0) ∧ readable_tree(c1)
let ls: Relation<C0.Source> = less
return bifurcateCompare(c0: c0, c1: c1, r: ls)
}
func alwaysFalse<T: Regular>(x: T, y: T) -> Bool {
return false
}
func bifurcateShapeCompare<
C0: Readable & BidirectionalBifurcateCoordinate,
C1: Readable & BidirectionalBifurcateCoordinate
>(
c0: C0,
c1: C1
) -> Bool?
where C0.Source == C1.Source {
// Precondition: readable_tree(c0) ∧ readable_tree(c1)
return bifurcateCompare(c0: c0, c1: c1, r: alwaysFalse)
}