~/bend-docscommunity

proof_shape.bend source

proof_shape.bend on the hub · documented module

import Baseimport ./defs.bend as Dimport ./halves.bend as Himport ./proof_nat.bend as N# The shapes H.build makes, as functions of the numbers alone: sp(n) for a# spine of n values, bs(f, big, n) for build(f, big, n, _) on n values. bs's# leaves count n, and bs(f, n >= 2, n) is balanced when f + 1 >= n.def sp(n: Nat) -> D.Tree<&2, Unit>:  match n:    case 0n:      D.Tip{}    case 1n+p:      H.cons(&2, Unit, Unit{}, sp(p))def bs(+f: Nat, big: Bool, +n: Nat) -> D.Tree<&2, Unit>:  match f big:    case 0n _:      sp(n)    case 1n+q False{}:      sp(n)    case 1n+q True{}:      +h = H.half(n)      +m = H.hi(n)      D.Node{bs(q, Nat.is_lt(1n, h), h), bs(q, Nat.is_lt(1n, m), m)}def count_cons(+s: D.Tree<&2, Unit>) -> {D.count(H.cons(&2, Unit, Unit{}, s)) == 1n+D.count(s) : Nat}:  match s:    case D.Tip{}:      {==}    case D.Leaf{x}:      {==}    case D.Node{l, r}:      {==}def count_sp(+n: Nat) -> {D.count(sp(n)) == n : Nat}:  match n:    case 0n:      {==}    case 1n+p:      %Equal.sym(Nat, D.count(H.cons(&2, Unit, Unit{}, sp(p))), 1n+D.count(sp(p)), count_cons(sp(p))) : {_ == 1n+p : Nat}      %Equal.sym(Nat, D.count(sp(p)), p, count_sp(p)) : {1n+_ == 1n+p : Nat}      {==}def count_bs(+f: Nat, big: Bool, +n: Nat) -> {D.count(bs(f, big, n)) == n : Nat}:  match f big:    case 0n _:      count_sp(n)    case 1n+q False{}:      count_sp(n)    case 1n+q True{}:      %Equal.sym(Nat, D.count(bs(q, Nat.is_lt(1n, H.half(n)), H.half(n))), H.half(n), count_bs(q, Nat.is_lt(1n, H.half(n)), H.half(n)))        : {Nat.add(_, D.count(bs(q, Nat.is_lt(1n, H.hi(n)), H.hi(n)))) == n : Nat}      %Equal.sym(Nat, D.count(bs(q, Nat.is_lt(1n, H.hi(n)), H.hi(n))), H.hi(n), count_bs(q, Nat.is_lt(1n, H.hi(n)), H.hi(n)))        : {Nat.add(H.half(n), _) == n : Nat}      N.half_hi(n)# A Node is balanced when its parts are and their counts h, m have# 0 < h <= m <= h + 1.def bal_node(+h: Nat, +m: Nat, -x: D.Tree<&2, Unit>, -y: D.Tree<&2, Unit>,  cx: {D.count(x) == h : Nat}, cy: {D.count(y) == m : Nat},  l1: {Nat.is_lt(0n, h) == True{} : Bool}, l2: {Nat.is_le(h, m) == True{} : Bool}, l3: {Nat.is_le(m, 1n+h) == True{} : Bool},  bx: {D.balanced(x) == True{} : Bool}, by: {D.balanced(y) == True{} : Bool})  -> {D.balanced(D.Node{x, y}) == True{} : Bool}:  %Equal.sym(Nat, D.count(x), h, cx) : {Nat.is_lt(0n, _) && Nat.is_le(_, D.count(y)) && Nat.is_le(D.count(y), 1n+_) && D.balanced(x) && D.balanced(y) == True{} : Bool}  %Equal.sym(Nat, D.count(y), m, cy) : {Nat.is_lt(0n, h) && Nat.is_le(h, _) && Nat.is_le(_, 1n+h) && D.balanced(x) && D.balanced(y) == True{} : Bool}  %Equal.sym(Bool, Nat.is_lt(0n, h), True{}, l1) : {_ && Nat.is_le(h, m) && Nat.is_le(m, 1n+h) && D.balanced(x) && D.balanced(y) == True{} : Bool}  %Equal.sym(Bool, Nat.is_le(h, m), True{}, l2) : {True{} && _ && Nat.is_le(m, 1n+h) && D.balanced(x) && D.balanced(y) == True{} : Bool}  %Equal.sym(Bool, Nat.is_le(m, 1n+h), True{}, l3) : {True{} && True{} && _ && D.balanced(x) && D.balanced(y) == True{} : Bool}  %Equal.sym(Bool, D.balanced(x), True{}, bx) : {True{} && True{} && True{} && _ && D.balanced(y) == True{} : Bool}  %Equal.sym(Bool, D.balanced(y), True{}, by) : {True{} && True{} && True{} && True{} && _ == True{} : Bool}  {==}# bs(f, n >= 2, n) is balanced when n <= f + 1 (the tree of n values has# f = n).def bal_bs(+f: Nat, +n: Nat, +hf: {Nat.is_le(n, 1n+f) == True{} : Bool})  -> {D.balanced(bs(f, Nat.is_lt(1n, n), n)) == True{} : Bool}:  match f n:    case 0n 0n:      {==}    case 0n 1n+p:      match p:        case 0n:          {==}        case 1n+k:          Empty.absurd({D.balanced(bs(0n, Nat.is_lt(1n, 2n+k), 2n+k)) == True{} : Bool}, N.tf(hf))    case 1n+q 0n:      {==}    case 1n+q 1n+p:      match p:        case 0n:          {==}        case 1n+k:          bal_node(1n+H.half(k), 1n+H.hi(k),            bs(q, Nat.is_lt(1n, 1n+H.half(k)), 1n+H.half(k)), bs(q, Nat.is_lt(1n, 1n+H.hi(k)), 1n+H.hi(k)),            count_bs(q, Nat.is_lt(1n, 1n+H.half(k)), 1n+H.half(k)), count_bs(q, Nat.is_lt(1n, 1n+H.hi(k)), 1n+H.hi(k)),            {==}, N.half_hi_le(k), N.hi_half_le(k),            bal_bs(q, 1n+H.half(k), N.le_trans(H.half(k), k, q, N.half_le(k), hf)),            bal_bs(q, 1n+H.hi(k), N.le_trans(H.hi(k), k, q, N.hi_le(k), hf)))