# Proofs of LAWS.bend. import Base import ./shape.bend as S import ./LAWS.bend as Laws import bend-mathlib@0.8.1.0/nat.bend as MN import bend-mathlib@0.8.1.0/list.bend as ML # ---- small facts ---- def Tag(b: Bool) -> Type: match b: case False{}: Unit case True{}: Empty def to_empty(e: {False{} == True{} : Bool}) -> Empty: %e : Tag(_) Unit{} def MTag(m: Maybe<&2, List<&2, Nat>>) -> Type: match m: case None{}: Unit case Some{x}: Empty def none_some(+r: List<&2, Nat>, h: {None{} == Some{r} : Maybe<&2, List<&2, Nat>>}) -> Empty: %h : MTag(_) Unit{} def NTag(m: Maybe<&2, Nat>) -> Type: match m: case None{}: Unit case Some{x}: Empty def none_some_n(+r: Nat, h: {None{} == Some{r} : Maybe<&2, Nat>}) -> Empty: %h : NTag(_) Unit{} def or_true(p: Bool) -> {True{} == Bool.or(p, True{}) : Bool}: match p: case True{}: {==} case False{}: {==} def and_left(p: Bool, +q: Bool, h: {Bool.and(p, q) == True{} : Bool}) -> {p == True{} : Bool}: match p: case True{}: {==} case False{}: Empty.absurd({False{} == True{} : Bool}, to_empty(h)) def and_right(p: Bool, +q: Bool, h: {Bool.and(p, q) == True{} : Bool}) -> {q == True{} : Bool}: match p: case True{}: h case False{}: Empty.absurd({q == True{} : Bool}, to_empty(h)) def unsome_l(m: Maybe<&2, List<&2, Nat>>) -> List<&2, Nat>: match m: case None{}: [] case Some{x}: x def some_inj_l(+a: List<&2, Nat>, +b: List<&2, Nat>, h: {Some{a} == Some{b} : Maybe<&2, List<&2, Nat>>}) -> {a == b : List<&2, Nat>}: Equal.cong(Maybe<&2, List<&2, Nat>>, List<&2, Nat>, m => unsome_l(m), Some{a}, Some{b}, h) def unsome_n(m: Maybe<&2, Nat>) -> Nat: match m: case None{}: 0n case Some{x}: x def some_inj_n(+a: Nat, +b: Nat, h: {Some{a} == Some{b} : Maybe<&2, Nat>}) -> {a == b : Nat}: Equal.cong(Maybe<&2, Nat>, Nat, m => unsome_n(m), Some{a}, Some{b}, h) # a dimension fits itself def fits_refl(s: List<&2, Nat>) -> {S.fits_rev(s, s) == True{} : Bool}: match s: case []: {==} case +x <> t: %MN.is_eq_refl_sym(x) : {Bool.and(Bool.or(Nat.is_eq(x, 1n), _), S.fits_rev(t, t)) == True{} : Bool} %or_true(Nat.is_eq(x, 1n)) : {Bool.and(_, S.fits_rev(t, t)) == True{} : Bool} fits_refl(t) # x and y both equal d: is_eq(x, y) is is_eq(d, d) def two_eq(+x: Nat, +y: Nat, +d: Nat, xd: {x == d : Nat}, yd: {y == d : Nat}) -> {Nat.is_eq(d, d) == Nat.is_eq(x, y) : Bool}: %Equal.sym(Nat, x, d, xd) : {Nat.is_eq(d, d) == Nat.is_eq(_, y) : Bool} %Equal.sym(Nat, y, d, yd) : {Nat.is_eq(d, d) == Nat.is_eq(d, _) : Bool} {==} # ---- one aligned pair: the result is 1 or equal to each side ---- # x fits d: x is 1 or d def fits1(+x: Nat, +d: Nat) -> Bool: Bool.or(Nat.is_eq(x, 1n), Nat.is_eq(x, d)) def bpick_fits(x1: Bool, y1: Bool, e: Bool, +x: Nat, +y: Nat, +d: Nat, ex: {Nat.is_eq(x, 1n) == x1 : Bool}, ey: {Nat.is_eq(y, 1n) == y1 : Bool}, ee: {Nat.is_eq(x, y) == e : Bool}, h: {S.bpick(x1, y1, e, x, y) == Some{d} : Maybe<&2, Nat>}) -> {Bool.and(fits1(x, d), fits1(y, d)) == True{} : Bool}: match x1 y1 e: case True{} _ _: %some_inj_n(y, d, h) : {Bool.and(fits1(x, _), fits1(y, _)) == True{} : Bool} %Equal.sym(Bool, Nat.is_eq(x, 1n), True{}, ex) : {Bool.and(Bool.or(_, Nat.is_eq(x, y)), fits1(y, y)) == True{} : Bool} %MN.is_eq_refl_sym(y) : {Bool.or(Nat.is_eq(y, 1n), _) == True{} : Bool} Equal.sym(Bool, True{}, Bool.or(Nat.is_eq(y, 1n), True{}), or_true(Nat.is_eq(y, 1n))) case False{} True{} _: %some_inj_n(x, d, h) : {Bool.and(fits1(x, _), fits1(y, _)) == True{} : Bool} %MN.is_eq_refl_sym(x) : {Bool.and(Bool.or(Nat.is_eq(x, 1n), _), fits1(y, x)) == True{} : Bool} %or_true(Nat.is_eq(x, 1n)) : {Bool.and(_, fits1(y, x)) == True{} : Bool} %Equal.sym(Bool, Nat.is_eq(y, 1n), True{}, ey) : {Bool.or(_, Nat.is_eq(y, x)) == True{} : Bool} {==} case False{} False{} True{}: %some_inj_n(x, d, h) : {Bool.and(fits1(x, _), fits1(y, _)) == True{} : Bool} %MN.is_eq_refl_sym(x) : {Bool.and(Bool.or(Nat.is_eq(x, 1n), _), fits1(y, x)) == True{} : Bool} %or_true(Nat.is_eq(x, 1n)) : {Bool.and(_, fits1(y, x)) == True{} : Bool} %MN.is_eq_comm(x, y) : {Bool.or(Nat.is_eq(y, 1n), _) == True{} : Bool} %Equal.sym(Bool, Nat.is_eq(x, y), True{}, ee) : {Bool.or(Nat.is_eq(y, 1n), _) == True{} : Bool} Equal.sym(Bool, True{}, Bool.or(Nat.is_eq(y, 1n), True{}), or_true(Nat.is_eq(y, 1n))) case False{} False{} False{}: Empty.absurd({Bool.and(fits1(x, d), fits1(y, d)) == True{} : Bool}, none_some_n(d, h)) def RTag(r: Result<&2, &2, S.NdErr, List<&2, Nat>>) -> Type: match r: case Done{x}: Unit case Fail{e}: Empty def fail_done(+e: S.NdErr, +s: List<&2, Nat>, h: {Fail{e} == Done{s} : Result<&2, &2, S.NdErr, List<&2, Nat>>}) -> Empty: %Equal.sym(Result<&2, &2, S.NdErr, List<&2, Nat>>, Fail{e}, Done{s}, h) : RTag(_) Unit{} def undone(r: Result<&2, &2, S.NdErr, List<&2, Nat>>) -> List<&2, Nat>: match r: case Done{x}: x case Fail{e}: [] def done_inj(+a: List<&2, Nat>, +b: List<&2, Nat>, h: {Done{a} == Done{b} : Result<&2, &2, S.NdErr, List<&2, Nat>>}) -> {a == b : List<&2, Nat>}: Equal.cong(Result<&2, &2, S.NdErr, List<&2, Nat>>, List<&2, Nat>, r => undone(r), Done{a}, Done{b}, h) # ---- broadcast_sound ---- def mcons_sound(m1: Maybe<&2, Nat>, m2: Maybe<&2, List<&2, Nat>>, +x: Nat, +y: Nat, +xs: List<&2, Nat>, +ys: List<&2, Nat>, +rs: List<&2, Nat>, e1: {S.bdim(x, y) == m1 : Maybe<&2, Nat>}, e2: {S.bcast_rev(xs, ys) == m2 : Maybe<&2, List<&2, Nat>>}, h: {S.mcons(m1, m2) == Some{rs} : Maybe<&2, List<&2, Nat>>}, ih: @r:List<&2, Nat> -> {S.bcast_rev(xs, ys) == Some{r} : Maybe<&2, List<&2, Nat>>} -> {Bool.and(S.fits_rev(xs, r), S.fits_rev(ys, r)) == True{} : Bool}) -> {Bool.and(S.fits_rev(x <> xs, rs), S.fits_rev(y <> ys, rs)) == True{} : Bool}: match m1 m2: case Some{+d} Some{+r}: +fxy = bpick_fits(Nat.is_eq(x, 1n), Nat.is_eq(y, 1n), Nat.is_eq(x, y), x, y, d, {==}, {==}, {==}, e1) %some_inj_l(d <> r, rs, h) : {Bool.and(S.fits_rev(x <> xs, _), S.fits_rev(y <> ys, _)) == True{} : Bool} %Equal.sym(Bool, fits1(x, d), True{}, and_left(fits1(x, d), fits1(y, d), fxy)) : {Bool.and(Bool.and(_, S.fits_rev(xs, r)), Bool.and(fits1(y, d), S.fits_rev(ys, r))) == True{} : Bool} %Equal.sym(Bool, fits1(y, d), True{}, and_right(fits1(x, d), fits1(y, d), fxy)) : {Bool.and(S.fits_rev(xs, r), Bool.and(_, S.fits_rev(ys, r))) == True{} : Bool} ih(r, e2) case None{} _: Empty.absurd({Bool.and(S.fits_rev(x <> xs, rs), S.fits_rev(y <> ys, rs)) == True{} : Bool}, none_some(rs, h)) case Some{d} None{}: Empty.absurd({Bool.and(S.fits_rev(x <> xs, rs), S.fits_rev(y <> ys, rs)) == True{} : Bool}, none_some(rs, h)) def sound_rev(ra: List<&2, Nat>, rb: List<&2, Nat>, +rs: List<&2, Nat>, h: {S.bcast_rev(ra, rb) == Some{rs} : Maybe<&2, List<&2, Nat>>}) -> {Bool.and(S.fits_rev(ra, rs), S.fits_rev(rb, rs)) == True{} : Bool}: match ra rb: case Nil{} Nil{}: %some_inj_l(Nil{}, rs, h) : {Bool.and(S.fits_rev(Nil{}, _), S.fits_rev(Nil{}, _)) == True{} : Bool} {==} case Nil{} +y <> +ys: %some_inj_l(y <> ys, rs, h) : {Bool.and(S.fits_rev(Nil{}, _), S.fits_rev(y <> ys, _)) == True{} : Bool} fits_refl(y <> ys) case +x <> +xs Nil{}: %some_inj_l(x <> xs, rs, h) : {Bool.and(S.fits_rev(x <> xs, _), S.fits_rev(Nil{}, _)) == True{} : Bool} %Equal.sym(Bool, S.fits_rev(x <> xs, x <> xs), True{}, fits_refl(x <> xs)) : {Bool.and(_, True{}) == True{} : Bool} {==} case +x <> +xs +y <> +ys: mcons_sound(S.bdim(x, y), S.bcast_rev(xs, ys), x, y, xs, ys, rs, {==}, {==}, h, r => hr => sound_rev(xs, ys, r, hr)) def sound_pub(m: Maybe<&2, List<&2, Nat>>, +a: List<&2, Nat>, +b: List<&2, Nat>, +s: List<&2, Nat>, e: {S.bcast_rev(List.reverse(&2, Nat, a), List.reverse(&2, Nat, b)) == m : Maybe<&2, List<&2, Nat>>}, h: {S.bcast_done(m, a, b) == Done{s} : Result<&2, &2, S.NdErr, List<&2, Nat>>}) -> {Bool.and(S.fits(a, s), S.fits(b, s)) == True{} : Bool}: match m: case Some{+r}: %done_inj(List.reverse(&2, Nat, r), s, h) : {Bool.and(S.fits_rev(List.reverse(&2, Nat, a), List.reverse(&2, Nat, _)), S.fits_rev(List.reverse(&2, Nat, b), List.reverse(&2, Nat, _))) == True{} : Bool} %ML.reverse_reverse_sym(&2, Nat, r) : {Bool.and(S.fits_rev(List.reverse(&2, Nat, a), _), S.fits_rev(List.reverse(&2, Nat, b), _)) == True{} : Bool} sound_rev(List.reverse(&2, Nat, a), List.reverse(&2, Nat, b), r, e) case None{}: Empty.absurd({Bool.and(S.fits(a, s), S.fits(b, s)) == True{} : Bool}, fail_done(S.NoBroadcast{a, b}, s, h)) def Laws.broadcast_sound(a, b, s, h): sound_pub(S.bcast_rev(List.reverse(&2, Nat, a), List.reverse(&2, Nat, b)), a, b, s, {==}, h) # ---- broadcast_complete ---- def some_l(m: Maybe<&2, List<&2, Nat>>) -> Bool: match m: case Some{x}: True{} case None{}: False{} def some_n(m: Maybe<&2, Nat>) -> Bool: match m: case Some{x}: True{} case None{}: False{} # x fits d and y fits d: the pair broadcasts def bpick_some(x1: Bool, y1: Bool, e: Bool, +x: Nat, +y: Nat, +d: Nat, ex: {Nat.is_eq(x, 1n) == x1 : Bool}, ey: {Nat.is_eq(y, 1n) == y1 : Bool}, ee: {Nat.is_eq(x, y) == e : Bool}, fx: {fits1(x, d) == True{} : Bool}, fy: {fits1(y, d) == True{} : Bool}) -> {some_n(S.bpick(x1, y1, e, x, y)) == True{} : Bool}: match x1 y1 e: case True{} _ _: {==} case False{} True{} _: {==} case False{} False{} True{}: {==} case False{} False{} False{}: +xd = MN.eq_of_is_eq(x, d, Equal.trans(Bool, Nat.is_eq(x, d), fits1(x, d), True{}, Equal.cong(Bool, Bool, p => Bool.or(p, Nat.is_eq(x, d)), False{}, Nat.is_eq(x, 1n), Equal.sym(Bool, Nat.is_eq(x, 1n), False{}, ex)), fx)) +yd = MN.eq_of_is_eq(y, d, Equal.trans(Bool, Nat.is_eq(y, d), fits1(y, d), True{}, Equal.cong(Bool, Bool, p => Bool.or(p, Nat.is_eq(y, d)), False{}, Nat.is_eq(y, 1n), Equal.sym(Bool, Nat.is_eq(y, 1n), False{}, ey)), fy)) +bad = Equal.trans(Bool, True{}, Nat.is_eq(d, d), False{}, MN.is_eq_refl_sym(d), Equal.trans(Bool, Nat.is_eq(d, d), Nat.is_eq(x, y), False{}, two_eq(x, y, d, xd, yd), ee)) Empty.absurd({some_n(S.bpick(False{}, False{}, False{}, x, y)) == True{} : Bool}, to_empty(Equal.sym(Bool, True{}, False{}, bad))) def mcons_some(m1: Maybe<&2, Nat>, m2: Maybe<&2, List<&2, Nat>>, h1: {some_n(m1) == True{} : Bool}, h2: {some_l(m2) == True{} : Bool}) -> {some_l(S.mcons(m1, m2)) == True{} : Bool}: match m1 m2: case Some{d} Some{r}: {==} case None{} _: Empty.absurd({some_l(S.mcons(None{}, m2)) == True{} : Bool}, to_empty(h1)) case Some{d} None{}: Empty.absurd({some_l(S.mcons(Some{d}, None{})) == True{} : Bool}, to_empty(h2)) def complete_rev(ra: List<&2, Nat>, rb: List<&2, Nat>, rs: List<&2, Nat>, +ha: {S.fits_rev(ra, rs) == True{} : Bool}, +hb: {S.fits_rev(rb, rs) == True{} : Bool}) -> {some_l(S.bcast_rev(ra, rb)) == True{} : Bool}: match ra rb rs: case Nil{} Nil{} _: {==} case Nil{} y <> ys _: {==} case x <> xs Nil{} _: {==} case x <> xs y <> ys Nil{}: Empty.absurd({some_l(S.bcast_rev(x <> xs, y <> ys)) == True{} : Bool}, to_empty(ha)) case +x <> +xs +y <> +ys +d <> +r: mcons_some(S.bdim(x, y), S.bcast_rev(xs, ys), bpick_some(Nat.is_eq(x, 1n), Nat.is_eq(y, 1n), Nat.is_eq(x, y), x, y, d, {==}, {==}, {==}, and_left(fits1(x, d), S.fits_rev(xs, r), ha), and_left(fits1(y, d), S.fits_rev(ys, r), hb)), complete_rev(xs, ys, r, and_right(fits1(x, d), S.fits_rev(xs, r), ha), and_right(fits1(y, d), S.fits_rev(ys, r), hb))) def done_of_some(m: Maybe<&2, List<&2, Nat>>, +a: List<&2, Nat>, +b: List<&2, Nat>, h: {some_l(m) == True{} : Bool}) -> {Laws.is_done(S.bcast_done(m, a, b)) == True{} : Bool}: match m: case Some{r}: {==} case None{}: Empty.absurd({Laws.is_done(S.bcast_done(None{}, a, b)) == True{} : Bool}, to_empty(h)) def Laws.broadcast_complete(a, b, s, ha, hb): done_of_some(S.bcast_rev(List.reverse(&2, Nat, a), List.reverse(&2, Nat, b)), a, b, complete_rev(List.reverse(&2, Nat, a), List.reverse(&2, Nat, b), List.reverse(&2, Nat, s), ha, hb)) # ---- broadcast_least ---- # the pair's result fits whatever both sides fit def bpick_least(x1: Bool, y1: Bool, e: Bool, +x: Nat, +y: Nat, +d: Nat, +c: Nat, h: {S.bpick(x1, y1, e, x, y) == Some{d} : Maybe<&2, Nat>}, fx: {fits1(x, c) == True{} : Bool}, fy: {fits1(y, c) == True{} : Bool}) -> {fits1(d, c) == True{} : Bool}: match x1 y1 e: case True{} _ _: %some_inj_n(y, d, h) : {fits1(_, c) == True{} : Bool} fy case False{} True{} _: %some_inj_n(x, d, h) : {fits1(_, c) == True{} : Bool} fx case False{} False{} True{}: %some_inj_n(x, d, h) : {fits1(_, c) == True{} : Bool} fx case False{} False{} False{}: Empty.absurd({fits1(d, c) == True{} : Bool}, none_some_n(d, h)) def least_m(m1: Maybe<&2, Nat>, m2: Maybe<&2, List<&2, Nat>>, rs: List<&2, Nat>, +x: Nat, +y: Nat, +xs: List<&2, Nat>, +ys: List<&2, Nat>, +rt: List<&2, Nat>, e1: {S.bdim(x, y) == m1 : Maybe<&2, Nat>}, e2: {S.bcast_rev(xs, ys) == m2 : Maybe<&2, List<&2, Nat>>}, h: {S.mcons(m1, m2) == Some{rt} : Maybe<&2, List<&2, Nat>>}, +ha: {S.fits_rev(x <> xs, rs) == True{} : Bool}, +hb: {S.fits_rev(y <> ys, rs) == True{} : Bool}, ih: @r:List<&2, Nat> -> @q:List<&2, Nat> -> {S.bcast_rev(xs, ys) == Some{r} : Maybe<&2, List<&2, Nat>>} -> {S.fits_rev(xs, q) == True{} : Bool} -> {S.fits_rev(ys, q) == True{} : Bool} -> {S.fits_rev(r, q) == True{} : Bool}) -> {S.fits_rev(rt, rs) == True{} : Bool}: match m1 m2 rs: case Some{+d} Some{+r} +c <> +q: %some_inj_l(d <> r, rt, h) : {S.fits_rev(_, c <> q) == True{} : Bool} %Equal.sym(Bool, fits1(d, c), True{}, bpick_least(Nat.is_eq(x, 1n), Nat.is_eq(y, 1n), Nat.is_eq(x, y), x, y, d, c, e1, and_left(fits1(x, c), S.fits_rev(xs, q), ha), and_left(fits1(y, c), S.fits_rev(ys, q), hb))) : {Bool.and(_, S.fits_rev(r, q)) == True{} : Bool} ih(r, q, e2, and_right(fits1(x, c), S.fits_rev(xs, q), ha), and_right(fits1(y, c), S.fits_rev(ys, q), hb)) case Some{d} Some{r} Nil{}: Empty.absurd({S.fits_rev(rt, Nil{}) == True{} : Bool}, to_empty(ha)) case None{} _ _: Empty.absurd({S.fits_rev(rt, rs) == True{} : Bool}, none_some(rt, h)) case Some{d} None{} _: Empty.absurd({S.fits_rev(rt, rs) == True{} : Bool}, none_some(rt, h)) def least_rev(ra: List<&2, Nat>, rb: List<&2, Nat>, +rt: List<&2, Nat>, +rs: List<&2, Nat>, h: {S.bcast_rev(ra, rb) == Some{rt} : Maybe<&2, List<&2, Nat>>}, +ha: {S.fits_rev(ra, rs) == True{} : Bool}, +hb: {S.fits_rev(rb, rs) == True{} : Bool}) -> {S.fits_rev(rt, rs) == True{} : Bool}: match ra rb: case Nil{} Nil{}: %some_inj_l(Nil{}, rt, h) : {S.fits_rev(_, rs) == True{} : Bool} {==} case Nil{} +y <> +ys: %some_inj_l(y <> ys, rt, h) : {S.fits_rev(_, rs) == True{} : Bool} hb case +x <> +xs Nil{}: %some_inj_l(x <> xs, rt, h) : {S.fits_rev(_, rs) == True{} : Bool} ha case +x <> +xs +y <> +ys: least_m(S.bdim(x, y), S.bcast_rev(xs, ys), rs, x, y, xs, ys, rt, {==}, {==}, h, ha, hb, r => q => hr => hx => hy => least_rev(xs, ys, r, q, hr, hx, hy)) def least_pub(m: Maybe<&2, List<&2, Nat>>, +a: List<&2, Nat>, +b: List<&2, Nat>, +s: List<&2, Nat>, +t: List<&2, Nat>, e: {S.bcast_rev(List.reverse(&2, Nat, a), List.reverse(&2, Nat, b)) == m : Maybe<&2, List<&2, Nat>>}, h: {S.bcast_done(m, a, b) == Done{t} : Result<&2, &2, S.NdErr, List<&2, Nat>>}, +ha: {S.fits(a, s) == True{} : Bool}, +hb: {S.fits(b, s) == True{} : Bool}) -> {S.fits(t, s) == True{} : Bool}: match m: case Some{+r}: %done_inj(List.reverse(&2, Nat, r), t, h) : {S.fits_rev(List.reverse(&2, Nat, _), List.reverse(&2, Nat, s)) == True{} : Bool} %ML.reverse_reverse_sym(&2, Nat, r) : {S.fits_rev(_, List.reverse(&2, Nat, s)) == True{} : Bool} least_rev(List.reverse(&2, Nat, a), List.reverse(&2, Nat, b), r, List.reverse(&2, Nat, s), e, ha, hb) case None{}: Empty.absurd({S.fits(t, s) == True{} : Bool}, fail_done(S.NoBroadcast{a, b}, t, h)) def Laws.broadcast_least(a, b, s, t, h, ha, hb): least_pub(S.bcast_rev(List.reverse(&2, Nat, a), List.reverse(&2, Nat, b)), a, b, s, t, {==}, h, ha, hb) def Laws.broadcast_examples(): {==} def Laws.reshape_rules(): {==} def Laws.transpose_rules(): {==} def Laws.reduce_rules(): {==} def Laws.axis_rules(): {==} def Laws.slice_rules(): {==} def Laws.concat_rules(): {==}