1 import Base 2 import ../../shape/core.bend as Shape 3 import ../core.bend as Core 4 import ./fold.bend as Fold 5 6 # Ordered-fold family, sum instance: fold every word of the input, in index 7 # order, with F32.add. 8 9 def add(operation: Unit, accumulator: F32, word: F32) -> F32: 10 F32.add(accumulator, word) 11 12 def position(plan: Unit, index: Nat) -> Nat: 13 index 14 15 def every(plan: Unit, index: Nat) -> Bool: 16 True{} 17 18 def sum(input: Core.Dynamic) -> Core.Dynamic & Result<&2, &1, Core.Error, F32>: 19 Core.Packed{+shape, tensor} = input 20 Fold.run(~Unit, ~add, ~Unit, ~position, ~every, Unit{}, Unit{}, Shape.elements(shape), Core.Packed{shape, tensor})