# Matrices whose shape each value has (`rank=2`: Ada's unconstrained two-dimensional arrays,
# T32a), indexed from 0 in each dimension, and a board of a fixed shape (`shape=`).
from verada import *


class Small(RangeInt, ge=-100, le=100):
    pass


class Matrix(Array[Small], rank=2):
    pass


class Board(Array[Small], shape=(2, 3)):
    pass


def trace(m: Matrix) -> int:
    requires(m.shape[0] == m.shape[1] and m.shape[0] <= 10)
    total = 0
    for i in range(m.shape[0]):
        invariant(-100 * i <= total and total <= 100 * i)
        total += int(m[i, i])
    return total


def row_total(m: Matrix, i: int) -> int:
    requires(0 <= i and i < m.shape[0] and m.shape[1] <= 10)
    total = 0
    for j in range(m.shape[1]):
        invariant(-100 * j <= total and total <= 100 * j)
        total += int(m[i, j])
    return total


def scale(m: Matrix, k: int) -> None:
    requires(-1 <= k and k <= 1)
    for i in range(m.shape[0]):
        for j in range(m.shape[1]):
            m[i, j] = Small(int(m[i, j]) * k)


def board_total(b: Board) -> int:
    total = 0
    for i in range(2):
        invariant(-300 * i <= total and total <= 300 * i)
        for j in range(3):
            invariant(-300 * i - 100 * j <= total and total <= 300 * i + 100 * j)
            total += int(b[i, j])
    return total
