import Base import ../shape/core.bend as Shape import ./layout.bend as Layout import ./core.bend as Core import ./transforms.bend as Transforms import ../storage/main.bend as Storage import ../storage/model.bend as Model import ../storage/refinement.bend as Refinement import ./transform_totality.bend as Totality import ./transpose_spec.bend as Transpose law permutation_checked: for +input: Shape.Shape for +axes: List<&2, U32> for plan: Layout.Plan {True{} == Layout.bijective(input, axes) : Bool} law permutation_dimensions: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan {Some{Shape.dimensions(Layout.output(input, axes, plan))} == Layout.reorder(Shape.dimensions(input), axes) : Maybe<&2, List<&2, U32>>} law permutation_count: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan {Shape.logical_numel(Shape.dimensions(input)) == Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))) : Nat} law mapped_inverse: for -input: Shape.Shape for -axes: List<&2, U32> for -plan: Layout.Plan for -i: U32 for +mapping: Layout.Mapping {Some{Layout.input_words(input, axes, plan, i, mapping)} == Layout.inverse_coordinates(Shape.dimensions(input), axes, Layout.output_words(input, axes, plan, i, mapping), 0) : Maybe<&2, List<&2, U32>>} law mapped_flat: for -input: Shape.Shape for -axes: List<&2, U32> for -plan: Layout.Plan for -i: U32 for +mapping: Layout.Mapping {Layout.source_index(input, axes, plan, i, mapping) == Layout.flatten_words(Shape.dimensions(input), Layout.input_words(input, axes, plan, i, mapping), 0) : U32} law mapped_output: for -input: Shape.Shape for -axes: List<&2, U32> for -plan: Layout.Plan for -i: U32 for mapping: Layout.Mapping {Layout.output_words(input, axes, plan, i, mapping) == Layout.reverse_words(Layout.unflatten_words(Layout.reverse_words(Shape.dimensions(Layout.output(input, axes, plan))), i)) : List<&2, U32>} law mapped_bounded: for -input: Shape.Shape for -axes: List<&2, U32> for -plan: Layout.Plan for -i: U32 for mapping: Layout.Mapping {True{} == U32.is_lt(Layout.source_index(input, axes, plan, i, mapping), Shape.count(input)) : Bool} law inverse_coordinate_roundtrip: for -input: Shape.Shape for +axes: List<&2, U32> for -plan: Layout.Plan for -i: U32 for +mapping: Layout.Mapping {Layout.reorder(Layout.input_words(input, axes, plan, i, mapping), axes) == Some{Layout.output_words(input, axes, plan, i, mapping)} : Maybe<&2, List<&2, U32>>} law reshape_linear_order: for +input: Shape.Shape for +target: Shape.Shape for tensor: Core.Tensor for +index: U32 for -same: {Shape.logical_numel(Shape.dimensions(input)) == Shape.logical_numel(Shape.dimensions(target)) : Nat} {Transforms.dynamic_observe(Transforms.reshape_equal(input, target, tensor, same), index) == Core.observe(input, tensor, index) : Maybe<&2, F32>} law reshape_target_dimensions: for +input: Shape.Shape for +target: Shape.Shape for tensor: Core.Tensor for -same: {Shape.logical_numel(Shape.dimensions(input)) == Shape.logical_numel(Shape.dimensions(target)) : Nat} {Transforms.dynamic_dimensions(Transforms.reshape_equal(input, target, tensor, same)) == Shape.dimensions(target) : List<&2, U32>} law copy_input_preservation: for +depth: Nat for array: Array for +index: U32 for +other: U32 for balanced: Model.Balanced(F32, depth, array) {Model.observe(F32, depth, Refinement.first(F32, Array.get(F32, array, index)), Refinement.route(depth, other)) == Model.observe(F32, depth, array, Refinement.route(depth, other)) : Maybe<&2, F32>} law copy_write_frame: for +depth: Nat for array: Array for +index: U32 for +other: U32 for +value: F32 for balanced: Model.Balanced(F32, depth, array) {Model.observe(F32, depth, Array.set(F32, array, index, value), Refinement.route(depth, other)) == Model.select(F32, Model.same(depth, Refinement.route(depth, index), Refinement.route(depth, other)), value, Model.observe(F32, depth, array, Refinement.route(depth, other))) : Maybe<&2, F32>} law zero_count_skips_copy: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for source: Core.Tensor for target: Core.Tensor for +cursor: Nat for empty: {Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))) == 0n : Nat} {Transforms.copy_loop(Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))), input, axes, plan, source, target, cursor) == (source, Done{target}) : Core.Tensor & Result<&2, &1, Transforms.Error, Core.Tensor>} law natural_cursor_checked: for -input: Shape.Shape for -axes: List<&2, U32> for -plan: Layout.Plan for -cursor: Nat for result: Transforms.NaturalCopy Transforms.CursorValid(input, axes, plan, cursor, result) law copy_source_mapping: for -input: Shape.Shape for -axes: List<&2, U32> for -plan: Layout.Plan for -output_index: U32 for -mapping: Layout.Mapping for -source: Core.Tensor for certificate: Core.Read {Core.read_value(input, Core.read_flat(input, source, Layout.source_index(input, axes, plan, output_index, mapping))) == Core.certificate_value(input, source, Layout.source_index(input, axes, plan, output_index, mapping), certificate) : Maybe<&2, F32>} law primitive_source_mapping: for -input: Shape.Shape for -axes: List<&2, U32> for -plan: Layout.Plan for -output_index: U32 for +mapping: Layout.Mapping for +depth: Nat for array: Array for balanced: Model.Balanced(F32, depth, array) {Some{Refinement.second(F32, Array.get(F32, array, Layout.source_index(input, axes, plan, output_index, mapping)))} == Model.observe(F32, depth, array, Refinement.route(depth, Layout.source_index(input, axes, plan, output_index, mapping))) : Maybe<&2, F32>} law copy_value_mapping: for -input: Shape.Shape for -axes: List<&2, U32> for +plan: Layout.Plan for +depth: Nat for +length: Nat for array: Array for +index: U32 for source: Core.Tensor for +value: F32 for balanced: Model.Balanced(F32, depth, array) for -valid: {Storage.bounds(depth, length) == True{} : Bool} for -exact: {length == Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))) : Nat} for allowed: {Nat.is_lt(U32.to_nat(index), length) == True{} : Bool} for matched: {Storage.index_matches(depth, length, index) == True{} : Bool} {Transforms.copy_observe(input, axes, plan, Transforms.copy_read(input, axes, plan, index, source => target => (source, Done{target}), Core.TTensor{Storage.Owned{depth, length, array, balanced, valid}, exact}, (source, Done{value})), index) == Some{value} : Maybe<&2, F32>} law checked_copy_step_mapping: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for +index: U32 for +mapping: Layout.Mapping for map_exact: {Done{mapping} == Layout.map_index(input, axes, plan, index) : Result<&2, &2, Layout.Error, Layout.Mapping>} for -source: Core.Tensor for certificate: Core.Read for +depth: Nat for +length: Nat for array: Array for balanced: Model.Balanced(F32, depth, array) for -valid: {Storage.bounds(depth, length) == True{} : Bool} for -exact: {length == Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))) : Nat} for allowed: {Nat.is_lt(U32.to_nat(index), length) == True{} : Bool} for matched: {Storage.index_matches(depth, length, index) == True{} : Bool} {Transforms.copy_observe(input, axes, plan, Transforms.copy_step(input, axes, plan, source, Core.TTensor{Storage.Owned{depth, length, array, balanced, valid}, exact}, index), index) == Core.certificate_value(input, source, Layout.source_index(input, axes, plan, index, mapping), certificate) : Maybe<&2, F32>} law checked_copy_source_meaning: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for +index: U32 for +mapping: Layout.Mapping for map_exact: {Done{mapping} == Layout.map_index(input, axes, plan, index) : Result<&2, &2, Layout.Error, Layout.Mapping>} for source: Core.Tensor for +depth: Nat for +length: Nat for array: Array for balanced: Model.Balanced(F32, depth, array) for -valid: {Storage.bounds(depth, length) == True{} : Bool} for -exact: {length == Shape.logical_numel(Shape.dimensions(Layout.output(input, axes, plan))) : Nat} for allowed: {Nat.is_lt(U32.to_nat(index), length) == True{} : Bool} for matched: {Storage.index_matches(depth, length, index) == True{} : Bool} Transforms.CopyMeaning law initial_prefix: for -input: Shape.Shape for -axes: List<&2, U32> for -plan: Layout.Plan for -source: Core.Tensor for -target: Core.Tensor Transforms.Prefix law advance_prefix: for -input: Shape.Shape for -axes: List<&2, U32> for -plan: Layout.Plan for -original: Core.Tensor for -before_source: Core.Tensor for -after_source: Core.Tensor for -before_target: Core.Tensor for -after_target: Core.Tensor for -cursor: Nat for prefix: Transforms.Prefix for advanced: Transforms.Advanced Transforms.Prefix law whole_copy_interpretation: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for source: Core.Tensor for target: Core.Tensor Transforms.WholeCopyModel law permute_result_interpretation: for +input: Shape.Shape for source: Core.Tensor for +axes: List<&2, U32> Transforms.PublicModel law transpose_result_interpretation: for +input: Shape.Shape for source: Core.Tensor for +a: U32 for +b: U32 Transforms.PublicModel law reshape_result_interpretation: for +input: Shape.Shape for source: Core.Tensor for +dimensions: List<&2, U32> Transforms.ReshapeModel law reshape_inverse_observations: for +input: Shape.Shape for source: Core.Tensor for +dimensions: List<&2, U32> Transforms.ReshapeInverseModel law mapping_reference: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for +index: U32 for mapping: Layout.Mapping {Some{Layout.source_index(input, axes, plan, index, mapping)} == Layout.reference_source(input, axes, plan, index) : Maybe<&2, U32>} law mapped_mathematical_bounded: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for +index: U32 for mapping: Layout.Mapping {Nat.is_lt(U32.to_nat(Layout.source_index(input, axes, plan, index, mapping)), Shape.logical_numel(Shape.dimensions(input))) == True{} : Bool} law natural_word_exact: for +number: Nat for +limit: U32 for +within: {Nat.is_le(number, U32.to_nat(limit)) == True{} : Bool} {U32.to_nat(U32.from_nat(number)) == number : Nat} law codec_flatten_decode: for +shape: Shape.Shape for +index: U32 for +inside: {True{} == Nat.is_lt(U32.to_nat(index), Shape.elements(shape)) : Bool} {Layout.flatten_words(Shape.dimensions(shape), Layout.reverse_words(Layout.unflatten_words(Layout.reverse_words(Shape.dimensions(shape)), index)), 0) == index : U32} law reshape_valid_success: for +input: Shape.Shape for +target: Shape.Shape for source: Core.Tensor for -same: {Shape.logical_numel(Shape.dimensions(input)) == Shape.logical_numel(Shape.dimensions(target)) : Nat} Transforms.ReshapeSuccessModel law reshape_inverse_valid_success: for +input: Shape.Shape for +target: Shape.Shape for source: Core.Tensor for -same: {Shape.logical_numel(Shape.dimensions(input)) == Shape.logical_numel(Shape.dimensions(target)) : Nat} Transforms.ReshapeInverseSuccessModel law permutation_valid_success: for +input: Shape.Shape for +axes: List<&2, U32> for +valid: {True{} == Layout.bijective(input, axes) : Bool} Totality.PlanSuccess law tensor_read_valid_success: for +shape: Shape.Shape for source: Core.Tensor for +index: U32 for -inside: {Nat.is_lt(U32.to_nat(index), Shape.logical_numel(Shape.dimensions(shape))) == True{} : Bool} Totality.ReadSuccess law tensor_write_valid_success: for +shape: Shape.Shape for target: Core.Tensor for +index: U32 for +value: F32 for -inside: {Nat.is_lt(U32.to_nat(index), Shape.logical_numel(Shape.dimensions(shape))) == True{} : Bool} Totality.WriteSuccess law decoded_words_valid_success: for +shape: Shape.Shape for +index: U32 for +inside: {True{} == Nat.is_lt(U32.to_nat(index), Shape.elements(shape)) : Bool} Totality.WordsSuccess law permute_axes_length_rejection: for +input: Shape.Shape for source: Core.Tensor for +axes: List<&2, U32> for -wrong: {False{} == Nat.is_eq(Shape.rank_dimensions(axes), Shape.rank_dimensions(Shape.dimensions(input))) : Bool} Totality.PermuteFailure law permute_axis_bounds_rejection: for +input: Shape.Shape for source: Core.Tensor for +axes: List<&2, U32> for -ranked: {True{} == Nat.is_eq(Shape.rank_dimensions(axes), Shape.rank_dimensions(Shape.dimensions(input))) : Bool} for -outside: {False{} == Layout.bounded(axes, Shape.rank(input)) : Bool} Totality.PermuteFailure law permute_duplicate_axes_rejection: for +input: Shape.Shape for source: Core.Tensor for +axes: List<&2, U32> for -ranked: {True{} == Nat.is_eq(Shape.rank_dimensions(axes), Shape.rank_dimensions(Shape.dimensions(input))) : Bool} for -bounded: {True{} == Layout.bounded(axes, Shape.rank(input)) : Bool} for -duplicate: {False{} == Layout.unique(axes) : Bool} Totality.PermuteFailure law transpose_axis_bounds_rejection: for +input: Shape.Shape for source: Core.Tensor for +a: U32 for +b: U32 for -outside: {False{} == Transpose.valid(input, a, b) : Bool} Totality.TransposeFailure law reshape_element_count_rejection: for +input: Shape.Shape for +target: Shape.Shape for source: Core.Tensor for -different: {False{} == Nat.is_eq(Shape.logical_numel(Shape.dimensions(input)), Shape.logical_numel(Shape.dimensions(target))) : Bool} Totality.ReshapeFailure law mapping_index_rejection: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for +index: U32 for +outside: {False{} == U32.is_lt(index, Shape.count(Layout.output(input, axes, plan))) : Bool} {Layout.map_index(input, axes, plan, index) == Fail{Layout.ShapeError{Shape.FlatIndexOutOfBounds{}}} : Result<&2, &2, Layout.Error, Layout.Mapping>} law codec_decode_flatten: for +shape: Shape.Shape for +values: List<&2, U32> for +valid: Layout.WordsValid(Shape.dimensions(shape), values) {Layout.reverse_words(Layout.unflatten_words(Layout.reverse_words(Shape.dimensions(shape)), Layout.flatten_words(Shape.dimensions(shape), values, 0))) == values : List<&2, U32>} law mapping_valid_success: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for +index: U32 for +inside: {True{} == U32.is_lt(index, Shape.count(Layout.output(input, axes, plan))) : Bool} Totality.MappingSuccess law natural_cursor_exact_conversion: for +shape: Shape.Shape for +cursor: Nat for +within: {Nat.is_le(cursor, Shape.elements(shape)) == True{} : Bool} Transforms.CursorExact law copy_step_valid_success: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for source: Core.Tensor for target: Core.Tensor for +index: U32 for +inside: {True{} == U32.is_lt(index, Shape.count(Layout.output(input, axes, plan))) : Bool} Totality.CopyStepSuccess law natural_step_valid_success: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for +cursor: Nat for source: Core.Tensor for target: Core.Tensor for +inside: {True{} == Nat.is_lt(cursor, Shape.elements(Layout.output(input, axes, plan))) : Bool} Totality.NaturalSuccess law copy_loop_valid_success: for +fuel: Nat for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for +cursor: Nat for source: Core.Tensor for target: Core.Tensor for +within: {Nat.is_le(Nat.add(fuel, cursor), Shape.elements(Layout.output(input, axes, plan))) == True{} : Bool} Totality.CopySuccess law permute_valid_success: for +input: Shape.Shape for source: Core.Tensor for +axes: List<&2, U32> for +valid: {True{} == Layout.bijective(input, axes) : Bool} Totality.PublicSuccess law inverse_axes_valid: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan {True{} == Layout.bijective(Layout.output(input, axes, plan), Totality.inverse_axes(input, axes)) : Bool} law inverse_permutation_dimensions: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for +inverse: Layout.Plan {Shape.dimensions(Layout.output(Layout.output(input, axes, plan), Totality.inverse_axes(input, axes), inverse)) == Shape.dimensions(input) : List<&2, U32>} law inverse_mapping_restores_index: for +input: Shape.Shape for +axes: List<&2, U32> for +plan: Layout.Plan for +inverse: Layout.Plan for +index: U32 for +second: Layout.Mapping for +first: Layout.Mapping {Layout.source_index(input, axes, plan, Layout.source_index(Layout.output(input, axes, plan), Totality.inverse_axes(input, axes), inverse, index, second), first) == index : U32} law permute_typed_success: for +input: Shape.Shape for source: Core.Tensor for +axes: List<&2, U32> for +valid: {True{} == Layout.bijective(input, axes) : Bool} Totality.TypedPermutation law permute_inverse_restoration: for +input: Shape.Shape for source: Core.Tensor for +axes: List<&2, U32> for +valid: {True{} == Layout.bijective(input, axes) : Bool} Totality.PermutationInverse law transpose_valid_success: for +input: Shape.Shape for source: Core.Tensor for +a: U32 for +b: U32 for +inside: {True{} == Transpose.valid(input, a, b) : Bool} Totality.PublicSuccess law transpose_requested_result: for +input: Shape.Shape for source: Core.Tensor for +a: U32 for +b: U32 for +inside: {True{} == Transpose.valid(input, a, b) : Bool} Totality.RequestedTranspose law transpose_inverse_restoration: for +input: Shape.Shape for source: Core.Tensor for +a: U32 for +b: U32 for +inside: {True{} == Transpose.valid(input, a, b) : Bool} Totality.TransposeInverse