1 import Base 2 3 def capacity(d: Nat) -> Nat: 4 match d: 5 case 0n: 6 1n 7 case 1n+p: 8 +n = capacity(p) 9 Nat.add(n, n) 10 11 def Path(d: Nat) -> Data: 12 match d: 13 case 0n: 14 Unit 15 case 1n+p: 16 Step
17
18 type Step<-d: Nat> is Data:
19 Step{bit: Bool, tail: Path(d)}
20
21 def offset_step(b: Bool, half: Nat, tail: Nat) -> Nat:
22 match b:
23 case False{}:
24 tail
25 case True{}:
26 Nat.add(half, tail)
27
28 def offset(d: Nat, path: Path(d)) -> Nat:
29 match d:
30 case 0n:
31 0n
32 case 1n++p:
33 Step{b, rest} = path
34 offset_step(b, capacity(p), offset(p, rest))
35
36 def same(d: Nat, a: Path(d), b: Path(d)) -> Bool:
37 match d:
38 case 0n:
39 True{}
40 case 1n+p:
41 Step{ab, ar} = a
42 Step{bb, br} = b
43 Bool.and(Bool.not(Bool.xor(ab, bb)), same(p, ar, br))
44
45 def Balanced(-T: Type, d: Nat, array: Array