import Base
import ../shape/core.bend as Shape
import ./layout.bend as Layout
import ./core.bend as Core
import ./transforms.bend as Transforms
import ./axis_spec.bend as Axis
import ./transpose_spec.bend as Transpose
type PlanSuccess<-input: Shape.Shape, -axes: List<&2, U32>> is Data:
PlanSuccess{-successful: {Result.is_done(&2, &2, Layout.Error, Layout.Plan,
Layout.permutation(input, axes)) == True{} : Bool}}
def read_succeeded(-shape: Shape.Shape, pair: Core.Tensor & Result<&2, &2, Core.Error, F32>) -> Bool:
(owner, result) = pair
Result.is_done(&2, &2, Core.Error, F32, result)
type ReadSuccess<-shape: Shape.Shape, -original: Core.Tensor, -index: U32> is Data:
ReadSuccess{-successful: {read_succeeded(shape, Core.read_flat(shape, original, index)) == True{} : Bool}}
def write_succeeded(-shape: Shape.Shape, pair: Core.Tensor & Result<&2, &2, Core.Error, Unit>) -> Bool:
(owner, result) = pair
Result.is_done(&2, &2, Core.Error, Unit, result)
type WriteSuccess<-shape: Shape.Shape, -original: Core.Tensor, -index: U32, -value: F32> is Data:
WriteSuccess{-successful: {write_succeeded(shape, Core.write_flat(shape, original, index, value)) == True{} : Bool}}
type WordsSuccess<-dimensions: List<&2, U32>, -values: List<&2, U32>> is Data:
WordsSuccess{-successful: {Result.is_done(&2, &2, Shape.Error, Layout.WordsValid(dimensions, values),
Layout.words_check(dimensions, values)) == True{} : Bool}}
type PermuteFailure<-input: Shape.Shape, -axes: List<&2, U32>, -original: Core.Tensor, -error: Layout.Error> is Data:
PermuteFailure{-exact: {Transforms.permute_copy(input, original, axes) == (original, Fail{Transforms.LayoutError{error}}) :
Core.Tensor & Result<&2, &1, Transforms.Error, Core.Dynamic>}}
type TransposeFailure<-input: Shape.Shape, -original: Core.Tensor, -a: U32, -b: U32> is Data:
TransposeFailure{-exact: {Transforms.transpose_copy(input, original, a, b) == (original, Fail{Transforms.LayoutError{Layout.AxisOutOfBounds{}}}) :
Core.Tensor & Result<&2, &1, Transforms.Error, Core.Dynamic>}}
type ReshapeFailure<-input: Shape.Shape, -target: Shape.Shape, -original: Core.Tensor> is Data:
ReshapeFailure{-exact: {Transforms.reshape(input, original, Shape.dimensions(target)) == Fail{Transforms.LayoutError{Layout.ElementCountMismatch{}}} :
Result<&2, &1, Transforms.Error, Core.Dynamic>}}
type MappingSuccess<-input: Shape.Shape, -axes: List<&2, U32>, -plan: Layout.Plan, -index: U32> is Data:
MappingSuccess{-successful: {Result.is_done(&2, &2, Layout.Error, Layout.Mapping,
Layout.map_index(input, axes, plan, index)) == True{} : Bool}}
def copy_succeeded(-input: Shape.Shape, -axes: List<&2, U32>, -plan: Layout.Plan,
pair: Core.Tensor & Result<&2, &1, Transforms.Error, Core.Tensor>) -> Bool:
(source, result) = pair
Result.is_done(&2, &1, Transforms.Error, Core.Tensor, result)
type CopyStepSuccess<-input: Shape.Shape, -axes: List<&2, U32>, -plan: Layout.Plan,
-source: Core.Tensor, -target: Core.Tensor, -index: U32> is Data:
CopyStepSuccess{-successful: {copy_succeeded(input, axes, plan, Transforms.copy_step(input, axes, plan, source, target, index)) == True{} : Bool}}
type CopySuccess<-input: Shape.Shape, -axes: List<&2, U32>, -plan: Layout.Plan,
-pair: Core.Tensor & Result<&2, &1, Transforms.Error, Core.Tensor>> is Data:
CopySuccess{-successful: {copy_succeeded(input, axes, plan, pair) == True{} : Bool}}
def natural_succeeded(-input: Shape.Shape, -axes: List<&2, U32>, -plan: Layout.Plan, -cursor: Nat,
result: Transforms.NaturalCopy) -> Bool:
match result:
case Transforms.NaturalError{source}:
False{}
case Transforms.NaturalCopied{pair, index, exact, candidate}:
copy_succeeded(input, axes, plan, pair)
type NaturalSuccess<-input: Shape.Shape, -axes: List<&2, U32>, -plan: Layout.Plan, -cursor: Nat,
-result: Transforms.NaturalCopy> is Data:
NaturalSuccess{-successful: {natural_succeeded(input, axes, plan, cursor, result) == True{} : Bool}}
def public_succeeded(-input: Shape.Shape, pair: Core.Tensor & Result<&2, &1, Transforms.Error, Core.Dynamic>) -> Bool:
(source, result) = pair
Result.is_done(&2, &1, Transforms.Error, Core.Dynamic, result)
type PublicSuccess<-input: Shape.Shape, -pair: Core.Tensor & Result<&2, &1, Transforms.Error, Core.Dynamic>> is Data:
PublicSuccess{-successful: {public_succeeded(input, pair) == True{} : Bool}}
def inverse_axes(+input: Shape.Shape, axes: List<&2, U32>) -> List<&2, U32>:
Shape.words(Axis.inverse_axes(Shape.rank_dimensions(Shape.dimensions(input)), Shape.naturals(axes)))
type TypedPermutation<-input: Shape.Shape, -axes: List<&2, U32>, -original: Core.Tensor,
-pair: Core.Tensor & Result<&2, &1, Transforms.Error, Core.Dynamic>> is Data:
TypedPermutation{-plan: Layout.Plan, -retained: Core.Tensor, -target: Core.Tensor,
-exact: {(retained, Done{Core.Packed{Layout.output(input, axes, plan), target}}) == pair : Core.Tensor & Result<&2, &1, Transforms.Error, Core.Dynamic>},
-prefix: Transforms.Prefix}
type MappingDone<-input: Shape.Shape, -axes: List<&2, U32>, -plan: Layout.Plan, -index: U32,
-result: Result<&2, &2, Layout.Error, Layout.Mapping>> is Data:
MappingDone{-mapping: Layout.Mapping,
-exact: {Done{mapping} == result : Result<&2, &2, Layout.Error, Layout.Mapping>}}
type CopyObservation<-input: Shape.Shape, -axes: List<&2, U32>, -plan: Layout.Plan, -original: Core.Tensor,
-target: Core.Tensor, -index: U32> is Data:
CopyObservation{-equal: {Core.observe(Layout.output(input, axes, plan), target, index) == Transforms.expected_output(input, axes, plan, original, index) : Maybe<&2, F32>}}
type RestoreObservation<-input: Shape.Shape, -output: Shape.Shape, -original: Core.Tensor, -restored: Core.Tensor