def result_lift
bendtorch-core@0.1.0.0 · src/tensor/read_contract.bend:73
import bendtorch-core@0.1.0.0/src/tensor/read_contract.bend as ReadContract
import Base
import bendtorch-core@0.1.0.0/src/shape/core.bend as Shape
import bendtorch-core@0.1.0.0/src/storage/main.bend as Storage
import bendtorch-core@0.1.0.0/src/storage/LAWS.bend as StorageLaws
import bendtorch-core@0.1.0.0/src/storage/arith.bend as Arith
import bendtorch-core@0.1.0.0/src/tensor/core.bend as Core
import bendtorch-core@0.1.0.0/src/tensor/lifecycle.bend as Lifecycle
import bendtorch-core@0.1.0.0/src/tensor/allocation_spec.bend as Allocation
73def result_lift(+shape: Shape.Shape, result: Result<&2, &1, Storage.Error, Storage.Storage>,
74 counted: {Allocation.storage_length(result) == Some{Shape.elements(shape)} : Maybe<&2, Nat>},
75 -P: Result<&2, &1, Core.Error, Core.Tensor<shape>> -> Type,
76 h: @-valid: {Lifecycle.logical_length(StorageLaws.created.of(result)) == Shape.logical_numel(Shape.dimensions(shape)) : Nat} ->
77 P(Done{Core.TTensor{StorageLaws.created.of(result), valid}})) ->
78 P(Core.new_result(shape, result)):
79 match result:
80 case Fail{error}:
81 Empty.absurd(P(Core.new_result(shape, Fail{error})),
82 Arith.clash(Equal.cong(Maybe<&2, Nat>, Bool, value => Maybe.is_some(&2, Nat, value), None{}, Some{Shape.elements(shape)}, counted)))
83 case Done{storage}:
84 attach_lift(shape, storage,
85 Equal.cong(Maybe<&2, Nat>, Nat, value => Maybe.default(&2, Nat, value, 0n),
86 Some{Lifecycle.logical_length(storage)}, Some{Shape.elements(shape)}, counted), P, h)