import Base
import ./defs.bend as D
import ./halves.bend as H
import ./proof_nat.bend as N
import ./proof_shape.bend as S
# H.tree, by walks that hand their result out with facts about it (a list of
# values used once serves one walk only): sb (split), spb (spine), blb
# (build), mb (measure), and tb, which hands out tree(xs) with its leaves
# (xs) and that its shape is balanced.
# The lengths of a pair of lists.
def lenl(a, -A: Kind(a), p: List & List) -> Nat:
match p:
case (l, r):
List.length(a, A, l)
def lenr(a, -A: Kind(a), p: List & List) -> Nat:
match p:
case (l, r):
List.length(a, A, r)
# ==========================================================================
# split
# ==========================================================================
# split(xs, k) handed out, with: it joins back to xs, its first list has
# min(k, |xs|) values, and the two lengths add up to |xs|.
def SB(a, -A: Kind(a), -xs: List, -k: Nat) -> Type:
Sigma<&1, &1, List & List, p =>
{p == H.split(a, A, xs, k) : List & List}
& ({H.join(a, A, p) == xs : List}
& ({lenl(a, A, p) == Nat.min(k, List.length(a, A, xs)) : Nat}
& {Nat.add(lenl(a, A, p), lenr(a, A, p)) == List.length(a, A, xs) : Nat}))>
# split.put(h, q) handed out, with what it does to the join and lengths.
def JP(a, -A: Kind(a), -h: A, -q: List & List) -> Type:
Sigma<&1, &1, List & List, v =>
{v == H.split.put(a, A, h, q) : List & List}
& ({H.join(a, A, v) == h <> H.join(a, A, q) : List}
& ({lenl(a, A, v) == 1n+lenl(a, A, q) : Nat}
& {lenr(a, A, v) == lenr(a, A, q) : Nat}))>
def jp(a, -A: Kind(a), h: A, q: List & List) -> JP(a, A, h, q):
match q:
case (l, r):
((h <> l, r), ({==}, ({==}, ({==}, {==}))))
def sb.len(a, -A: Kind(a), -v: List & List, -q: List & List, -t: List,
f3: {lenl(a, A, v) == 1n+lenl(a, A, q) : Nat}, f4: {lenr(a, A, v) == lenr(a, A, q) : Nat},
e4: {Nat.add(lenl(a, A, q), lenr(a, A, q)) == List.length(a, A, t) : Nat})
-> {Nat.add(lenl(a, A, v), lenr(a, A, v)) == 1n+List.length(a, A, t) : Nat}:
%Equal.sym(Nat, lenl(a, A, v), 1n+lenl(a, A, q), f3) : {Nat.add(_, lenr(a, A, v)) == 1n+List.length(a, A, t) : Nat}
%Equal.sym(Nat, lenr(a, A, v), lenr(a, A, q), f4) : {Nat.add(1n+lenl(a, A, q), _) == 1n+List.length(a, A, t) : Nat}
%Equal.sym(Nat, Nat.add(lenl(a, A, q), lenr(a, A, q)), List.length(a, A, t), e4) : {1n+_ == 1n+List.length(a, A, t) : Nat}
{==}
def sb.k2(a, -A: Kind(a), -h: A, -t: List, -p: Nat, -q: List & List,
e1: {q == H.split(a, A, t, p) : List & List}, e2: {H.join(a, A, q) == t : List},
e3: {lenl(a, A, q) == Nat.min(p, List.length(a, A, t)) : Nat},
e4: {Nat.add(lenl(a, A, q), lenr(a, A, q)) == List.length(a, A, t) : Nat},
s: JP(a, A, h, q)) -> SB(a, A, h <> t, 1n+p):
(v, f) = s
(f1, g) = f
(f2, g2) = g
(+f3, f4) = g2
(v, (Equal.trans(List & List, v, H.split.put(a, A, h, q), H.split.put(a, A, h, H.split(a, A, t, p)), f1,
Equal.cong(List & List, List & List, x => H.split.put(a, A, h, x), q, H.split(a, A, t, p), e1)),
(Equal.trans(List, H.join(a, A, v), h <> H.join(a, A, q), h <> t, f2,
Equal.cong(List, List, x => h <> x, H.join(a, A, q), t, e2)),
(Equal.trans(Nat, lenl(a, A, v), 1n+lenl(a, A, q), 1n+Nat.min(p, List.length(a, A, t)), f3,
Equal.cong(Nat, Nat, x => 1n+x, lenl(a, A, q), Nat.min(p, List.length(a, A, t)), e3)),
sb.len(a, A, v, q, t, f3, f4, e4)))))
def sb.k(a, -A: Kind(a), h: A, -t: List, -p: Nat, s: SB(a, A, t, p)) -> SB(a, A, h <> t, 1n+p):
(q, e) = s
(e1, g) = e
(e2, g2) = g
(e3, e4) = g2
sb.k2(a, A, h, t, p, q, e1, e2, e3, e4, jp(a, A, h, q))
def sb(a, -A: Kind(a), xs: List, +k: Nat) -> SB(a, A, xs, k):
match xs k:
case Nil{} _:
((Nil{}, Nil{}), ({==}, ({==}, (Equal.sym(Nat, Nat.min(k, 0n), 0n, N.min0(k)), {==}))))
case h <> t 0n:
((Nil{}, h <> t), ({==}, ({==}, ({==}, {==}))))
case h <> t 1n+p:
sb.k(a, A, h, t, p, sb(a, A, t, p))
# ==========================================================================
# spine
# ==========================================================================
# cons(x, s) handed out, with its leaves and shape.
def CB(a, -A: Kind(a), -x: A, -s: D.Tree) -> Type:
Sigma, u =>
{u == H.cons(a, A, x, s) : D.Tree}
& ({D.leaves(a, A, u) == x <> D.leaves(a, A, s) : List}
& {D.shape(a, A, u) == H.cons(&2, Unit, Unit{}, D.shape(a, A, s)) : D.Tree<&2, Unit>})>
def cb(a, -A: Kind(a), x: A, s: D.Tree) -> CB(a, A, x, s):
match s:
case D.Tip{}:
(D.Leaf{x}, ({==}, ({==}, {==})))
case D.Leaf{y}:
(D.Node{D.Leaf{x}, D.Leaf{y}}, ({==}, ({==}, {==})))
case D.Node{l, r}:
(D.Node{D.Leaf{x}, D.Node{l, r}}, ({==}, ({==}, {==})))
# spine(xs) handed out, with its leaves (xs) and shape (sp(|xs|)).
def SP(a, -A: Kind(a), -xs: List) -> Type:
Sigma, s =>
{s == H.spine(a, A, xs) : D.Tree}
& ({D.leaves(a, A, s) == xs : List}
& {D.shape(a, A, s) == S.sp(List.length(a, A, xs)) : D.Tree<&2, Unit>})>
def spb.k2(a, -A: Kind(a), -h: A, -t: List, -s0: D.Tree,
e1: {s0 == H.spine(a, A, t) : D.Tree}, e2: {D.leaves(a, A, s0) == t : List},
e3: {D.shape(a, A, s0) == S.sp(List.length(a, A, t)) : D.Tree<&2, Unit>},
c: CB(a, A, h, s0)) -> SP(a, A, h <> t):
(u, f) = c
(f1, g) = f
(f2, f3) = g
(u, (Equal.trans(D.Tree, u, H.cons(a, A, h, s0), H.cons(a, A, h, H.spine(a, A, t)), f1,
Equal.cong(D.Tree, D.Tree, x => H.cons(a, A, h, x), s0, H.spine(a, A, t), e1)),
(Equal.trans(List, D.leaves(a, A, u), h <> D.leaves(a, A, s0), h <> t, f2,
Equal.cong(List, List, x => h <> x, D.leaves(a, A, s0), t, e2)),
Equal.trans(D.Tree<&2, Unit>, D.shape(a, A, u), H.cons(&2, Unit, Unit{}, D.shape(a, A, s0)),
H.cons(&2, Unit, Unit{}, S.sp(List.length(a, A, t))), f3,
Equal.cong(D.Tree<&2, Unit>, D.Tree<&2, Unit>, x => H.cons(&2, Unit, Unit{}, x), D.shape(a, A, s0), S.sp(List.length(a, A, t)), e3)))))
def spb.k(a, -A: Kind(a), h: A, -t: List, s: SP(a, A, t)) -> SP(a, A, h <> t):
(s0, e) = s
(e1, g) = e
(e2, e3) = g
spb.k2(a, A, h, t, s0, e1, e2, e3, cb(a, A, h, s0))
def spb(a, -A: Kind(a), xs: List) -> SP(a, A, xs):
match xs:
case Nil{}:
(D.Tip{}, ({==}, ({==}, {==})))
case h <> t:
spb.k(a, A, h, t, spb(a, A, t))
# ==========================================================================
# build
# ==========================================================================
# build(f, big, n, split(xs, n / 2)) handed out, with its leaves (xs) and
# shape (bs(f, big, n)).
def BL(a, -A: Kind(a), -f: Nat, -big: Bool, -n: Nat, -xs: List) -> Type:
Sigma, t =>
{t == H.build(a, A, f, big, n, H.split(a, A, xs, H.half(n))) : D.Tree}
& ({D.leaves(a, A, t) == xs : List}
& {D.shape(a, A, t) == S.bs(f, big, n) : D.Tree<&2, Unit>})>
# The bottom: the spine of what is left.
def BS(a, -A: Kind(a), -n: Nat, -xs: List) -> Type:
Sigma, t =>
{t == H.spine(a, A, H.join(a, A, H.split(a, A, xs, H.half(n)))) : D.Tree}
& ({D.leaves(a, A, t) == xs : List}
& {D.shape(a, A, t) == S.sp(n) : D.Tree<&2, Unit>})>
def blb.base2(a, -A: Kind(a), -n: Nat, -xs: List, hl: {List.length(a, A, xs) == n : Nat},
-p: List & List, e1: {p == H.split(a, A, xs, H.half(n)) : List & List},
+e2: {H.join(a, A, p) == xs : List}, s: SP(a, A, H.join(a, A, p))) -> BS(a, A, n, xs):
(u, f) = s
(f1, g) = f
(f2, f3) = g
(u, (Equal.trans(D.Tree, u, H.spine(a, A, H.join(a, A, p)), H.spine(a, A, H.join(a, A, H.split(a, A, xs, H.half(n)))), f1,
Equal.cong(List & List, D.Tree, x => H.spine(a, A, H.join(a, A, x)), p, H.split(a, A, xs, H.half(n)), e1)),
(Equal.trans(List, D.leaves(a, A, u), H.join(a, A, p), xs, f2, e2),
Equal.trans(D.Tree<&2, Unit>, D.shape(a, A, u), S.sp(List.length(a, A, H.join(a, A, p))), S.sp(n), f3,
Equal.cong(Nat, D.Tree<&2, Unit>, x => S.sp(x), List.length(a, A, H.join(a, A, p)), n,
Equal.trans(Nat, List.length(a, A, H.join(a, A, p)), List.length(a, A, xs), n,
Equal.cong(List, Nat, x => List.length(a, A, x), H.join(a, A, p), xs, e2), hl))))))
def blb.base(a, -A: Kind(a), -n: Nat, -xs: List, hl: {List.length(a, A, xs) == n : Nat},
s: SB(a, A, xs, H.half(n))) -> BS(a, A, n, xs):
(p, e) = s
(e1, g) = e
(e2, g2) = g
blb.base2(a, A, n, xs, hl, p, e1, e2, spb(a, A, H.join(a, A, p)))
def blb.eq1(a, -A: Kind(a), -q: Nat, -n: Nat, -xs: List, -l: List, -r: List, -tl: D.Tree, -tr: D.Tree,
e1: {(l, r) == H.split(a, A, xs, H.half(n)) : List & List},
f1: {tl == H.build(a, A, q, Nat.is_lt(1n, H.half(n)), H.half(n), H.split(a, A, l, H.half(H.half(n)))) : D.Tree},
g1: {tr == H.build(a, A, q, Nat.is_lt(1n, H.hi(n)), H.hi(n), H.split(a, A, r, H.half(H.hi(n)))) : D.Tree})
-> {D.Node{tl, tr} == H.build(a, A, 1n+q, True{}, n, H.split(a, A, xs, H.half(n))) : D.Tree}:
%e1 : {D.Node{tl, tr} == H.build(a, A, 1n+q, True{}, n, _) : D.Tree}
%f1 : {D.Node{tl, tr} == D.Node{_, H.build(a, A, q, Nat.is_lt(1n, H.hi(n)), H.hi(n), H.split(a, A, r, H.half(H.hi(n))))} : D.Tree}
%g1 : {D.Node{tl, tr} == D.Node{tl, _} : D.Tree}
{==}
def blb.eq2(a, -A: Kind(a), -xs: List, -l: List, -r: List, -tl: D.Tree, -tr: D.Tree,
e2: {List.append(a, A, l, r) == xs : List},
f2: {D.leaves(a, A, tl) == l : List}, g2: {D.leaves(a, A, tr) == r : List})
-> {D.leaves(a, A, D.Node{tl, tr}) == xs : List}:
%Equal.sym(List, D.leaves(a, A, tl), l, f2) : {List.append(a, A, _, D.leaves(a, A, tr)) == xs : List}
%Equal.sym(List, D.leaves(a, A, tr), r, g2) : {List.append(a, A, l, _) == xs : List}
e2
def blb.eq3(a, -A: Kind(a), -q: Nat, -n: Nat, -tl: D.Tree, -tr: D.Tree,
f3: {D.shape(a, A, tl) == S.bs(q, Nat.is_lt(1n, H.half(n)), H.half(n)) : D.Tree<&2, Unit>},
g3: {D.shape(a, A, tr) == S.bs(q, Nat.is_lt(1n, H.hi(n)), H.hi(n)) : D.Tree<&2, Unit>})
-> {D.shape(a, A, D.Node{tl, tr}) == S.bs(1n+q, True{}, n) : D.Tree<&2, Unit>}:
%Equal.sym(D.Tree<&2, Unit>, D.shape(a, A, tl), S.bs(q, Nat.is_lt(1n, H.half(n)), H.half(n)), f3)
: {D.Node{_, D.shape(a, A, tr)} == D.Node{S.bs(q, Nat.is_lt(1n, H.half(n)), H.half(n)), S.bs(q, Nat.is_lt(1n, H.hi(n)), H.hi(n))} : D.Tree<&2, Unit>}
%Equal.sym(D.Tree<&2, Unit>, D.shape(a, A, tr), S.bs(q, Nat.is_lt(1n, H.hi(n)), H.hi(n)), g3)
: {D.Node{S.bs(q, Nat.is_lt(1n, H.half(n)), H.half(n)), _} == D.Node{S.bs(q, Nat.is_lt(1n, H.half(n)), H.half(n)), S.bs(q, Nat.is_lt(1n, H.hi(n)), H.hi(n))} : D.Tree<&2, Unit>}
{==}
def blb.node(a, -A: Kind(a), -q: Nat, -n: Nat, -xs: List, -l: List, -r: List,
e1: {(l, r) == H.split(a, A, xs, H.half(n)) : List & List},
e2: {List.append(a, A, l, r) == xs : List},
s1: BL(a, A, q, Nat.is_lt(1n, H.half(n)), H.half(n), l), s2: BL(a, A, q, Nat.is_lt(1n, H.hi(n)), H.hi(n), r))
-> BL(a, A, 1n+q, True{}, n, xs):
(tl, f) = s1
(f1, fg) = f
(f2, f3) = fg
(tr, g) = s2
(g1, gg) = g
(g2, g3) = gg
(D.Node{tl, tr}, (blb.eq1(a, A, q, n, xs, l, r, tl, tr, e1, f1, g1),
(blb.eq2(a, A, xs, l, r, tl, tr, e2, f2, g2), blb.eq3(a, A, q, n, tl, tr, f3, g3))))
# |l| = n / 2 and |r| = n - n / 2 for the halves of a list of n values.
def len_l(a, -A: Kind(a), +n: Nat, -xs: List, -l: List,
hl: {List.length(a, A, xs) == n : Nat}, e3: {List.length(a, A, l) == Nat.min(H.half(n), List.length(a, A, xs)) : Nat})
-> {List.length(a, A, l) == H.half(n) : Nat}:
%Equal.sym(Nat, List.length(a, A, l), Nat.min(H.half(n), List.length(a, A, xs)), e3) : {_ == H.half(n) : Nat}
%Equal.sym(Nat, List.length(a, A, xs), n, hl) : {Nat.min(H.half(n), _) == H.half(n) : Nat}
N.min_half0(n)
def len_r.eq(a, -A: Kind(a), +n: Nat, -xs: List, -l: List, -r: List,
hl: {List.length(a, A, xs) == n : Nat}, hll: {List.length(a, A, l) == H.half(n) : Nat},
e4: {Nat.add(List.length(a, A, l), List.length(a, A, r)) == List.length(a, A, xs) : Nat})
-> {Nat.add(H.half(n), List.length(a, A, r)) == Nat.add(H.half(n), H.hi(n)) : Nat}:
%hll : {Nat.add(_, List.length(a, A, r)) == Nat.add(H.half(n), H.hi(n)) : Nat}
%Equal.sym(Nat, Nat.add(List.length(a, A, l), List.length(a, A, r)), List.length(a, A, xs), e4) : {_ == Nat.add(H.half(n), H.hi(n)) : Nat}
%Equal.sym(Nat, List.length(a, A, xs), n, hl) : {_ == Nat.add(H.half(n), H.hi(n)) : Nat}
Equal.sym(Nat, Nat.add(H.half(n), H.hi(n)), n, N.half_hi(n))
def len_r(a, -A: Kind(a), +n: Nat, -xs: List, -l: List, -r: List,
hl: {List.length(a, A, xs) == n : Nat}, hll: {List.length(a, A, l) == H.half(n) : Nat},
e4: {Nat.add(List.length(a, A, l), List.length(a, A, r)) == List.length(a, A, xs) : Nat})
-> {List.length(a, A, r) == H.hi(n) : Nat}:
N.add_cancel(H.half(n), List.length(a, A, r), H.hi(n),
len_r.eq(a, A, n, xs, l, r, hl, hll, e4))
def blb(a, -A: Kind(a), +f: Nat, big: Bool, +n: Nat, -xs: List, +hl: {List.length(a, A, xs) == n : Nat},
s: SB(a, A, xs, H.half(n))) -> BL(a, A, f, big, n, xs):
match f big:
case 0n _:
blb.base(a, A, n, xs, hl, s)
case 1n+q False{}:
blb.base(a, A, n, xs, hl, s)
case 1n+q True{}:
(p, e) = s
(l, r) = p
(e1, g) = e
(e2, g2) = g
(e3, e4) = g2
+hll = len_l(a, A, n, xs, l, hl, e3)
blb.node(a, A, q, n, xs, l, r, e1, e2,
blb(a, A, q, Nat.is_lt(1n, H.half(n)), H.half(n), l, hll, sb(a, A, l, H.half(H.half(n)))),
blb(a, A, q, Nat.is_lt(1n, H.hi(n)), H.hi(n), r, len_r(a, A, n, xs, l, r, hl, hll, e4), sb(a, A, r, H.half(H.hi(n)))))
# ==========================================================================
# measure, and the tree
# ==========================================================================
def mfst(a, -A: Kind(a), p: Nat & List) -> Nat:
match p:
case (n, ys):
n
def msnd(a, -A: Kind(a), p: Nat & List) -> List:
match p:
case (n, ys):
ys
# measure(xs) handed out, with its parts: |xs| and xs.
def MB(a, -A: Kind(a), -xs: List) -> Type:
Sigma<&1, &1, Nat & List, p =>
{p == H.measure(a, A, xs) : Nat & List}
& ({mfst(a, A, p) == List.length(a, A, xs) : Nat}
& {msnd(a, A, p) == xs : List})>
def MP(a, -A: Kind(a), -h: A, -q: Nat & List) -> Type:
Sigma<&1, &1, Nat & List, v =>
{v == H.measure.put(a, A, h, q) : Nat & List}
& ({mfst(a, A, v) == 1n+mfst(a, A, q) : Nat}
& {msnd(a, A, v) == h <> msnd(a, A, q) : List})>
def mp(a, -A: Kind(a), h: A, q: Nat & List) -> MP(a, A, h, q):
match q:
case (n, t):
((1n+n, h <> t), ({==}, ({==}, {==})))
def mb.k2(a, -A: Kind(a), -h: A, -t: List, -q: Nat & List,
e1: {q == H.measure(a, A, t) : Nat & List}, e2: {mfst(a, A, q) == List.length(a, A, t) : Nat},
e3: {msnd(a, A, q) == t : List}, s: MP(a, A, h, q)) -> MB(a, A, h <> t):
(v, f) = s
(f1, g) = f
(f2, f3) = g
(v, (Equal.trans(Nat & List, v, H.measure.put(a, A, h, q), H.measure.put(a, A, h, H.measure(a, A, t)), f1,
Equal.cong(Nat & List, Nat & List, x => H.measure.put(a, A, h, x), q, H.measure(a, A, t), e1)),
(Equal.trans(Nat, mfst(a, A, v), 1n+mfst(a, A, q), 1n+List.length(a, A, t), f2,
Equal.cong(Nat, Nat, x => 1n+x, mfst(a, A, q), List.length(a, A, t), e2)),
Equal.trans(List, msnd(a, A, v), h <> msnd(a, A, q), h <> t, f3,
Equal.cong(List, List, x => h <> x, msnd(a, A, q), t, e3)))))
def mb.k(a, -A: Kind(a), h: A, -t: List, s: MB(a, A, t)) -> MB(a, A, h <> t):
(q, e) = s
(e1, g) = e
(e2, e3) = g
mb.k2(a, A, h, t, q, e1, e2, e3, mp(a, A, h, q))
def mb(a, -A: Kind(a), xs: List) -> MB(a, A, xs):
match xs:
case Nil{}:
((0n, Nil{}), ({==}, ({==}, {==})))
case h <> t:
mb.k(a, A, h, t, mb(a, A, t))
# tree(xs) handed out, with its leaves (xs) and that its shape is balanced.
def TB(a, -A: Kind(a), -xs: List) -> Type:
Sigma, t =>
{t == H.tree(a, A, xs) : D.Tree}
& ({D.leaves(a, A, t) == xs : List}
& {D.balanced(D.shape(a, A, t)) == True{} : Bool})>
def tb.bal(a, -A: Kind(a), +n: Nat, -t: D.Tree,
f3: {D.shape(a, A, t) == S.bs(n, Nat.is_lt(1n, n), n) : D.Tree<&2, Unit>})
-> {D.balanced(D.shape(a, A, t)) == True{} : Bool}:
%Equal.sym(D.Tree<&2, Unit>, D.shape(a, A, t), S.bs(n, Nat.is_lt(1n, n), n), f3) : {D.balanced(_) == True{} : Bool}
S.bal_bs(n, n, N.le_next(n))
def tb.k2(a, -A: Kind(a), +n: Nat, -xs: List, -ys: List,
e1: {(n, ys) == H.measure(a, A, xs) : Nat & List}, e3: {ys == xs : List},
s: BL(a, A, n, Nat.is_lt(1n, n), n, ys)) -> TB(a, A, xs):
(t, f) = s
(f1, g) = f
(f2, f3) = g
(t, (Equal.trans(D.Tree, t, H.tree.k(a, A, (n, ys)), H.tree.k(a, A, H.measure(a, A, xs)), f1,
Equal.cong(Nat & List, D.Tree, x => H.tree.k(a, A, x), (n, ys), H.measure(a, A, xs), e1)),
(Equal.trans(List, D.leaves(a, A, t), ys, xs, f2, e3), tb.bal(a, A, n, t, f3))))
def tb.k1(a, -A: Kind(a), +n: Nat, -xs: List, ys: List,
e1: {(n, ys) == H.measure(a, A, xs) : Nat & List}, e2: {n == List.length(a, A, xs) : Nat},
+e3: {ys == xs : List}) -> TB(a, A, xs):
tb.k2(a, A, n, xs, ys, e1, e3,
blb(a, A, n, Nat.is_lt(1n, n), n, ys,
Equal.trans(Nat, List.length(a, A, ys), List.length(a, A, xs), n,
Equal.cong(List, Nat, x => List.length(a, A, x), ys, xs, e3), Equal.sym(Nat, n, List.length(a, A, xs), e2)),
sb(a, A, ys, H.half(n))))
def tb.k(a, -A: Kind(a), -xs: List, s: MB(a, A, xs)) -> TB(a, A, xs):
(p, e) = s
(+n, ys) = p
(e1, g) = e
(e2, e3) = g
tb.k1(a, A, n, xs, ys, e1, e2, e3)
def tb(a, -A: Kind(a), xs: List) -> TB(a, A, xs):
tb.k(a, A, xs, mb(a, A, xs))