import Base import ../shape/core.bend as Shape import ../storage/main.bend as Storage import ../storage/model.bend as Model import ../storage/refinement.bend as Refinement import ./core.bend as Core def scalar_shape() -> Shape.Shape: Shape.SShape{Nil{}, 1n, {==}, Unit{}, Unit{}} def sequential_sum(values: List<&2, F32>, accumulator: F32) -> F32: match values: case Nil{}: accumulator case value <> tail: sequential_sum(tail, F32.add(accumulator, value)) def read_step(-shape: Shape.Shape, rec: Core.Tensor -> F32 -> Result<&2, &1, Core.Error, Core.Tensor & F32>, accumulator: F32, pair: Core.Tensor & Result<&2, &2, Core.Error, F32>) -> Result<&2, &1, Core.Error, Core.Tensor & F32>: (tensor, result) = pair match result: case Fail{error}: Fail{error} case Done{value}: rec(tensor, F32.add(accumulator, value)) def sum_decided(-shape: Shape.Shape, rec: Core.Tensor -> F32 -> Result<&2, &1, Core.Error, Core.Tensor & F32>, tensor: Core.Tensor, +cursor: Nat, +index: U32, accumulator: F32, decision: Shape.BoolCase) -> Result<&2, &1, Core.Error, Core.Tensor & F32>: match decision: case Shape.BFalse{equal}: Fail{Core.StorageError{Storage.InvalidIndex{}}} case Shape.BTrue{equal}: read_step(shape, rec, accumulator, Core.read_flat(shape, tensor, index)) def natural_sum_loop(fuel: Nat, -shape: Shape.Shape, tensor: Core.Tensor, +cursor: Nat, accumulator: F32) -> Result<&2, &1, Core.Error, Core.Tensor & F32>: match fuel: case 0n: Done{(tensor, accumulator)} case 1n+tail: +index = U32.from_nat(cursor) sum_decided(shape, tensor => value => natural_sum_loop(tail, shape, tensor, 1n+cursor, value), tensor, cursor, index, accumulator, Shape.bool_case(Nat.is_eq(U32.to_nat(index), cursor))) def sum_loop(fuel: Nat, -shape: Shape.Shape, tensor: Core.Tensor, +index: U32, accumulator: F32) -> Result<&2, &1, Core.Error, Core.Tensor & F32>: natural_sum_loop(fuel, shape, tensor, U32.to_nat(index), accumulator) def scalar_result(-input_shape: Shape.Shape, tensor: Core.Tensor, result: Result<&2, &1, Core.Error, Core.Tensor>) -> Result<&2, &1, Core.Error, Core.Tensor & Core.Tensor>: match result: case Fail{error}: Fail{error} case Done{scalar}: Done{(tensor, scalar)} def sum_result(-shape: Shape.Shape, result: Result<&2, &1, Core.Error, Core.Tensor & F32>) -> Result<&2, &1, Core.Error, Core.Tensor & Core.Tensor>: match result: case Fail{error}: Fail{error} case Done{pair}: (tensor, value) = pair scalar_result(shape, tensor, Core.new(scalar_shape(), value)) # The fuel + 1 words of the array from slot `cursor`, whose read `pair` is # already made, added to `accumulator` left to right. Each step reads the # next word and hands it on as an argument: one tail call with no checked # read and no closure per word. def array_run(fuel: Nat, +cursor: Nat, accumulator: F32, pair: Array & F32) -> F32 & Array: match fuel pair: case 0n Tuple{array, word}: (F32.add(accumulator, word), array) case 1n+rest Tuple{array, word}: array_run(rest, 1n+cursor, F32.add(accumulator, word), Array.get(F32, array, U32.from_nat(1n+cursor))) # The first `fuel` words of the array from slot `cursor`, added to # `accumulator` left to right: the F32 adds of `natural_sum_loop`, in the # same order. def array_sum(fuel: Nat, +cursor: Nat, accumulator: F32, array: Array) -> F32 & Array: match fuel: case 0n: (accumulator, array) case 1n+rest: array_run(rest, cursor, accumulator, Array.get(F32, array, U32.from_nat(cursor))) def array_from(pair: Array & F32) -> Array: Refinement.first(F32, pair) type Fact<-P: Type> is Data: Fact{-proof: P} def kept_balanced(-depth: Nat, -original: Array, -balanced: Model.Balanced(F32, depth, original), -kept: Array, -same: {kept == original : Array}) -> Fact: Fact{ %Equal.sym(Array, kept, original, same) : Model.Balanced(F32, depth, _) balanced} # The array a read hands back is the array it read. def read_keeps(-depth: Nat, -original: Array, -balanced: Model.Balanced(F32, depth, original), -array: Array, -index: U32, -pair: Array & F32, -exact: {pair == Array.get(F32, array, index) : Array & F32}, -same: {array == original : Array}) -> Fact<{array_from(pair) == original : Array}>: Fact{Equal.trans(Array, array_from(pair), array_from(Array.get(F32, array, index)), original, Equal.cong(Array & F32, Array, p => array_from(p), pair, Array.get(F32, array, index), exact), Equal.trans(Array, array_from(Array.get(F32, array, index)), array, original, Refinement.law_get_retains(F32, depth, array, index, {\{Fact: p => p} : Fact<(Model.Balanced(F32, depth, array))> -> Model.Balanced(F32, depth, array)}( kept_balanced(depth, original, balanced, array, same))), same))} # A run hands back the array it read. def array_run_keeps(-depth: Nat, -original: Array, -balanced: Model.Balanced(F32, depth, original), fuel: Nat, +cursor: Nat, -accumulator: F32, -array: Array, pair: Array & F32, -exact: {pair == Array.get(F32, array, U32.from_nat(cursor)) : Array & F32}, -same: {array == original : Array}) -> Fact<{Pair.snd(F32, Array, array_run(fuel, cursor, accumulator, pair)) == original : Array}>: match fuel pair: case 0n Tuple{kept, word}: read_keeps(depth, original, balanced, array, U32.from_nat(cursor), (kept, word), exact, same) case 1n+rest Tuple{kept, word}: array_run_keeps(depth, original, balanced, rest, 1n+cursor, F32.add(accumulator, word), kept, Array.get(F32, kept, U32.from_nat(1n+cursor)), {==}, {\{Fact: p => p} : Fact<({array_from((kept, word)) == original : Array})> -> {kept == original : Array}}( read_keeps(depth, original, balanced, array, U32.from_nat(cursor), (kept, word), exact, same))) def array_sum_keeps(-depth: Nat, -original: Array, -balanced: Model.Balanced(F32, depth, original), fuel: Nat, +cursor: Nat, -accumulator: F32, array: Array, -same: {array == original : Array}) -> Fact<{Pair.snd(F32, Array, array_sum(fuel, cursor, accumulator, array)) == original : Array}>: match fuel: case 0n: Fact{same} case 1n+rest: array_run_keeps(depth, original, balanced, rest, cursor, accumulator, array, Array.get(F32, array, U32.from_nat(cursor)), {==}, same) def owned_sum(-shape: Shape.Shape, +depth: Nat, +length: Nat, -array: Array, -balanced: Model.Balanced(F32, depth, array), -valid: {Storage.bounds(depth, length) == True{} : Bool}, -exact: {length == Shape.logical_numel(Shape.dimensions(shape)) : Nat}, pair: F32 & Array, -kept: Fact<{Pair.snd(F32, Array, pair) == array : Array}>) -> Result<&2, &1, Core.Error, Core.Tensor & F32>: (value, retained) = pair Done{(Core.TTensor{Storage.Owned{depth, length, retained, {\{Fact: p => p} : Fact<(Model.Balanced(F32, depth, retained))> -> Model.Balanced(F32, depth, retained)}( kept_balanced(depth, array, balanced, retained, {\{Fact: p => p} : Fact<({Pair.snd(F32, Array, (value, retained)) == array : Array})> -> {retained == array : Array}}(kept))), valid}, exact}, value)} # The first `count` words of the tensor added left to right from +0.0, next # to the tensor, which keeps every word. def tensor_sum(-shape: Shape.Shape, +count: Nat, tensor: Core.Tensor) -> Result<&2, &1, Core.Error, Core.Tensor & F32>: Core.TTensor{storage, exact} = tensor match storage: case Storage.Empty{}: Done{(Core.TTensor{Storage.Empty{}, exact}, 0.0)} case Storage.Owned{+depth, +length, array, balanced, valid}: owned_sum(shape, depth, length, array, balanced, valid, exact, array_sum(count, 0n, 0.0, array), array_sum_keeps(depth, array, balanced, count, 0n, 0.0, array, {==})) def sum(+shape: Shape.Shape, tensor: Core.Tensor) -> Result<&2, &1, Core.Error, Core.Tensor & Core.Tensor>: sum_result(shape, tensor_sum(shape, Shape.elements(shape), tensor)) def pack_result(+shape: Shape.Shape, result: Result<&2, &1, Core.Error, Core.Tensor & Core.Tensor>) -> Result<&2, &1, Core.Error, Core.Dynamic & Core.Dynamic>: match result: case Fail{error}: Fail{error} case Done{pair}: (tensor, scalar) = pair Done{(Core.pack(shape, tensor), Core.pack(scalar_shape(), scalar))} def sum_dynamic(dynamic: Core.Dynamic) -> Result<&2, &1, Core.Error, Core.Dynamic & Core.Dynamic>: Core.Packed{+shape, tensor} = dynamic pack_result(shape, sum(shape, tensor)) def sum_observation(-shape: Shape.Shape, result: Result<&2, &1, Core.Error, Core.Tensor & Core.Tensor>) -> Maybe<&2, F32>: match result: case Fail{error}: None{} case Done{pair}: (tensor, scalar) = pair Core.observe(scalar_shape(), scalar, 0)