import Base import ../shape/core.bend as Shape import ./core.bend as Core import ./allocation_spec.bend as Allocation import ./transform_totality.bend as TransformTotality def TensorReads(+shape: Shape.Shape, result: Result<&2, &1, Core.Error, Core.Tensor>) -> Type: match result: case Fail{error}: Unit case Done{tensor}: @+index: U32 -> @inside: {Nat.is_lt(U32.to_nat(index), Shape.logical_numel(Shape.dimensions(shape))) == True{} : Bool} -> TransformTotality.ReadSuccess type TensorDone<-shape: Shape.Shape, -result: Result<&2, &1, Core.Error, Core.Tensor>> is Data: TensorDone{ -witness: Allocation.TensorDone, -successful: {Result.is_done(&2, &1, Core.Error, Core.Tensor, result) == True{} : Bool}, -readable: TensorReads(shape, result)} type ConstructionWitness<-dimensions: List<&2, U32>, -result: Result<&2, &1, Core.Error, Core.Dynamic>> is Data: ConstructionWitness{ -shape: Shape.Shape, -tensor: Core.Tensor, -same: {Done{Core.Packed{shape, tensor}} == result : Result<&2, &1, Core.Error, Core.Dynamic>}, -preserved: {Shape.dimensions(shape) == dimensions : List<&2, U32>}} def ConstructionReads(+dimensions: List<&2, U32>, result: Result<&2, &1, Core.Error, Core.Dynamic>) -> Type: match result: case Fail{error}: Unit case Done{Core.Packed{+shape, tensor}}: @+index: U32 -> @inside: {Nat.is_lt(U32.to_nat(index), Shape.logical_numel(dimensions)) == True{} : Bool} -> TransformTotality.ReadSuccess type ConstructionDone<-dimensions: List<&2, U32>, -result: Result<&2, &1, Core.Error, Core.Dynamic>> is Data: ConstructionDone{ -witness: ConstructionWitness, -successful: {Result.is_done(&2, &1, Core.Error, Core.Dynamic, result) == True{} : Bool}, -readable: ConstructionReads(dimensions, result)}