:- module(core_tests, []).

:- use_module(library(array)).
:- use_module(library(clpBNR)).
:- use_module(library(array/buffer)).

:- begin_tests(core).

x(X) :- shape(X, [3]).
y(Y) :- shape(Y, [2, 3]).

test(transpose, [timeout(10), true(S == [3, 2])]) :-
   y(Y),
   over([I:3, J:2], [T[I, J] := Y[J, I]]),
   shape(T, S0),
   S0 = [3, 2],
   S = S0.
test(shape_is_a_lower_bound, [timeout(10), fail]) :-
   y(Y),
   over([I:3, J:2], [T[I, J] := Y[J, I]]),
   shape(T, [2, 2]).
test(matmul_two_boxes, [timeout(10), true(N == 2)]) :-
   shape(A, [2, 3]), shape(B, [3, 4]), shape(C, [2, 4]),
   over([I:2, J:4], [C[I, J] := 0]),
   over([I:2, J:4, K:3], [C[I, J] := C[I, J] + A[I, K] * B[K, J]]),
   get_attr(C, core, def(_, Boxes)),
   length(Boxes, N).
test(cumsum_extent_expression, [timeout(10), true(N == 5)]) :-
   shape(X, [N, 3]),
   over([J:3], [C[0, J] := X[0, J]]),
   over([T:N-1, J:3], [C[T+1, J] := C[T, J] + X[T+1, J]]),
   shape(C, [5, 3]),
   N = 5.
test(fibonacci_shifted_target, [timeout(10), fail]) :-
   over([], [F[0] := 1, F[1] := 1]),
   over([T:8], [F[T+2] := F[T] + F[T+1]]),
   shape(F, [9]).
test(rank_0_name, [timeout(10), true(S == [])]) :-
   x(X),
   over([], [Sum := 0]),
   over([K:3], [Sum := Sum + X[K]]),
   shape(Sum, S).
test(nested_box, [timeout(10), true(Ns == [1, 1])]) :-
   shape(F, [2, 2]), shape(X, [N, 2]),
   over([T:N-1], [over([I:2], [P[I] := 0]),
                  over([I:2, J:2], [P[I] := P[I] + F[I, J] * X[T, J]]),
                  over([I:2], [X[T+1, I] := P[I]])]),
   get_attr(P, core, def(_, Bp)), get_attr(X, core, def(_, Bx)),
   maplist(length, [Bp, Bx], Ns).
test(reads_relate_extents, [timeout(10), true(Lo == 3)]) :-
   shape(X, [N]),
   over([I:3], [_T[I] := X[I]]),
   range(N, [Lo, _]).
test(symbolic_constant, [timeout(10)]) :-
   shape(X, [N]),
   over([I:N], [_R[I] := X[N-1-I]]).
test(where_and_comparison, [timeout(10)]) :-
   x(X),
   over([I:3], [_R[I] := where(X[I] < 0, -X[I], X[I])]).

test(index_as_name, [timeout(10), throws(error(type_error(name, _), _))]) :-
   over([I:3], [I[0] := 1]).
test(name_as_index, [timeout(10), throws(error(type_error(axes, _), _))]) :-
   x(X),
   over([X:3], [_T[X] := 1]).
test(free_variable, [timeout(10), throws(error(type_error(index, _), _))]) :-
   x(X),
   over([I:3], [_T[I] := X[_]]).
test(undeclared_read, [timeout(10), throws(error(existence_error(name, _), _))]) :-
   over([I:3], [_T[I] := _X[I]]).
test(duplicate_axis, [timeout(10), throws(error(type_error(axes, _), _))]) :-
   over([I:3, I:2], [_T[I] := 1]).
test(inner_axis_shadows, [timeout(10), throws(error(type_error(axes, _), _))]) :-
   over([I:3], [over([I:2], [_T[I] := 1])]).
test(rank_mismatch, [timeout(10), throws(error(domain_error(rank(1), _), _))]) :-
   x(X),
   over([I:3, J:2], [_T[I, J] := X[I, J]]).
test(name_as_scalar, [timeout(10), throws(error(domain_error(rank(0), _), _))]) :-
   x(X),
   over([I:3], [_T[I] := X + 1]).
test(read_out_of_bounds, [timeout(10), throws(error(domain_error(in_bounds, _), _))]) :-
   x(X),
   over([I:3], [_T[I] := X[I+5]]).
test(write_out_of_bounds, [timeout(10), throws(error(domain_error(in_bounds, _), _))]) :-
   shape(T, [2]),
   over([I:3], [T[I] := 1]).
test(negative_subscript, [timeout(10), throws(error(domain_error(in_bounds, _), _))]) :-
   x(X),
   over([I:3], [_T[I] := X[2-I-1]]).
test(name_is_never_bound, [timeout(10), throws(error(permission_error(bind, name, _), _))]) :-
   x(X),
   X = foo.
test(not_a_shape, [timeout(10), throws(error(type_error(shape, _), _))]) :-
   shape(_, [2, foo]).
test(not_a_value, [timeout(10), throws(error(type_error(value, _), _))]) :-
   over([I:3], [_T[I] := foo(I)]).

% Kinds: inferred forwards from the values written, backwards from the uses of an input, and
% labelled once the program is built.
test(kind_float_accumulation, [timeout(10), true(K == float)]) :-
   x(X),
   over([I:3], [S[I] := 0]),
   over([I:3], [S[I] := S[I] + X[I]]),
   label_kinds([X, S]), kind(S, K).
test(kind_count_is_int, [timeout(10), true(K == int)]) :-
   x(X),
   over([I:3], [C[I] := 0]),
   over([I:3], [C[I] := C[I] + where(X[I] < 0, 1, 0)]),
   label_kinds([X, C]), kind(C, K).
test(kind_comparison_is_bool, [timeout(10), true(K == bool)]) :-
   x(X),
   over([I:3], [B[I] := X[I] < 0]),
   label_kinds([X, B]), kind(B, K).
test(kind_division_is_float, [timeout(10), true(K == float)]) :-
   over([I:3], [T[I] := I / 2]),
   label_kinds([T]), kind(T, K).
test(kind_index_arithmetic_is_int, [timeout(10), true(K == int)]) :-
   over([I:3], [T[I] := I + 1]),
   label_kinds([T]), kind(T, K).
test(kind_input_defaults_to_float, [timeout(10), true(K == float)]) :-
   x(X),
   over([I:3], [_T[I] := X[I]]),
   label_kinds([X]), kind(X, K).
test(kind_declared_before_shape, [timeout(10), true(K == int)]) :-
   kind(Idx, int), shape(Idx, [4]), elements(Idx, 0-2), shape(H, [3]),
   over([K4:4], [H[Idx[K4]] := 1]),
   label_kinds([Idx, H]), kind(Idx, K).
test(kind_where_condition_narrows_input, [timeout(10), true(K == bool)]) :-
   x(X),
   over([I:3], [_T[I] := where(X[I], 1, 0)]),
   label_kinds([X]), kind(X, K).
test(kind_subscript_defaults_to_int, [timeout(10), true(K == int)]) :-
   shape(Idx, [4]), elements(Idx, 0-2), shape(H, [3]),
   over([K4:4], [H[Idx[K4]] := 1]),
   label_kinds([Idx, H]), kind(Idx, K).
test(kind_float_subscript, [timeout(10), throws(error(domain_error(kind, _), _))]) :-
   shape(Idx, [4]), elements(Idx, 0-2), kind(Idx, float), shape(H, [3]),
   over([K4:4], [H[Idx[K4]] := 1]).
test(kind_where_condition_not_bool, [timeout(10), throws(error(domain_error(kind, _), _))]) :-
   x(X), kind(X, float),
   over([I:3], [_T[I] := where(X[I], 1, 0)]).
test(kind_declared_too_low, [timeout(10), throws(error(domain_error(kind, _), _))]) :-
   shape(S, [3]), kind(S, bool),
   over([I:3], [S[I] := 2]).
test(kind_unknown, [timeout(10), throws(error(type_error(oneof(_), _), _))]) :-
   x(X), kind(X, double).
test(kind_bits_is_int_of_anything, [timeout(10), true(K == int)]) :-
   x(X),
   over([I:3], [B[I] := bits(X[I])]),
   label_kinds([X, B]), kind(B, K).
test(permutation_is_int, [timeout(10), true(K == int)]) :-
   shape(P, [4]), permutation(P),
   label_kinds([P]), kind(P, K).
test(permutation_of_a_float, [timeout(10), throws(error(domain_error(kind, _), _))]) :-
   shape(P, [4]), kind(P, float), permutation(P).

% Beyond the structural check: a gather needs the range of the index array's elements, and
% a read of a place must come after its write.
test(gather, [timeout(10)]) :-
   shape(Idx, [4]), elements(Idx, 0-2), shape(H, [3]),
   over([K:4], [H[Idx[K]] := H[Idx[K]] + 1]).
test(gather_symbolic_range, [timeout(10), true(Lo == 3)]) :-
   shape(H, [N]), shape(Idx, [4]), elements(Idx, 0-(N-1)),
   over([K:4], [H[Idx[K]] := H[Idx[K]] + 1]),
   N = 3, range(N, [Lo, _]).
test(gather_range_above_extent, [timeout(10), throws(error(domain_error(in_bounds, _), _))]) :-
   shape(H, [N]), shape(Idx, [4]), elements(Idx, 0-N),
   over([K:4], [H[Idx[K]] := 1]).
test(gather_out_of_range, [timeout(10), throws(error(domain_error(in_bounds, _), _))]) :-
   shape(Idx, [4]), elements(Idx, 0-5), shape(H, [3]),
   over([K:4], [H[Idx[K]] := H[Idx[K]] + 1]).
test(gather_needs_a_range, [timeout(10), throws(error(existence_error(elements, _), _))]) :-
   shape(Idx, [4]), shape(H, [3]),
   over([K:4], [H[Idx[K]] := 1]).

% Beyond affine: a remainder is hulled by its operation on exact hulls of its operands, and the
% whole posted once as terms in the extents.
test(hull_mod_relates_extents, [timeout(10), true(Lo == 3)]) :-
   shape(X, [M]),
   over([I:7], [_T[I] := X[I mod 3]]),
   range(M, [Lo, _]).
test(hull_quotient_is_exact, [timeout(10), true(Lo == 4)]) :-
   shape(X, [M]),
   over([I:7], [_T[I] := X[I // 2]]),
   range(M, [Lo, _]).
test(hull_quotient_symbolic, [timeout(10)]) :-
   shape(X, [N]), shape(Y, [H]),
   over([I:N], [Y[I // 2] := X[I]]),
   N = 8, range(H, [4, _]).
test(hull_nested_affine_is_exact, [timeout(10), true(Lo == 13)]) :-
   shape(X, [M]),
   over([I:4, J:2], [_T[I, J] := X[(2*I + J) mod 7 + 2*I]]),
   range(M, [Lo, _]).
test(hull_clamp_fits_anything, [timeout(10)]) :-
   shape(X, [N]),
   over([I:N], [_T[I] := X[min(I*3, N-1)]]).
test(hull_where_is_the_union, [timeout(10), true(Lo == 7)]) :-
   shape(X, [M]),
   over([I:3], [_T[I] := X[where(I < 1, I + 4, I)]]),
   range(M, [Lo, _]).
test(hull_symbolic_coefficient, [timeout(10), true(Lo == 6)]) :-
   shape(X, [N, M]),
   over([I:2, J:3], [T[I*M + J] := X[I, J]]),
   N = 2, M = 3, shape(T, [L]), range(L, [Lo, _]).
test(hull_xor_with_a_power_of_two, [timeout(10)]) :-
   power_of_two(N), shape(X, [N]), shape(S, [P]), elements(S, 0-(N-1)),
   over([Pi:P, I:N], [_T[Pi, I] := X[I xor S[Pi]]]).
test(hull_xor_needs_a_power_of_two, [timeout(10), throws(error(domain_error(power_of_two, _), _))]) :-
   shape(X, [N]),
   over([I:N], [_T[I] := X[I xor 1]]).
test(hull_and_needs_no_declaration, [timeout(10)]) :-
   shape(X, [N]), shape(S, [P]), elements(S, 0-(N-1)),
   over([Pi:P, I:N], [_T[Pi, I] := X[I /\ S[Pi]]]).
test(hull_shift, [timeout(10), true(Lo == 2)]) :-
   shape(X, [M]),
   over([I:8], [_T[I] := X[I >> 2]]),
   range(M, [Lo, _]).
test(hull_rejects_a_float_subscript, [timeout(10), throws(error(domain_error(kind, _), _))]) :-
   shape(X, [4]),
   over([I:4], [_T[I] := X[I / 2]]).
test(hull_rejects_a_float_operand, [timeout(10), throws(error(domain_error(kind, _), _))]) :-
   shape(X, [4]), shape(W, [4]), kind(W, float),
   over([I:4], [_T[I] := X[W[I] mod 2]]).
test(hull_quotient_needs_a_positive_divisor, [timeout(10), throws(error(domain_error(positive, _), _))]) :-
   shape(X, [N]), shape(D, [1]), elements(D, 0-3),
   over([I:N], [_T[I] := X[I // D[0]]]).
test(power_of_two_on_a_number, [timeout(10)]) :-
   power_of_two(8).
test(power_of_two_rejects_six, [timeout(10), throws(error(domain_error(power_of_two, 6), _))]) :-
   power_of_two(6).
test(power_of_two_binds_to_a_power, [timeout(10)]) :-
   power_of_two(N), shape(_X, [N]), N = 512.
test(power_of_two_refuses_six_later, [timeout(10)]) :-
   power_of_two(N), shape(_X, [N]),
   \+ catch(N = 6, error(domain_error(power_of_two, _), _), fail).
% A name created by a write over a product extent is of that extent, the product as it
% stands, the shape of the names declared with it; over any other extent, and at any other
% target than the index, its extent is new, a lower bound.
test(over_keeps_a_product_extent, [timeout(10)]) :-
   K::integer(1, 100), N = 64*K,
   shape(X, [N, 3]),
   over([I:N, J:3], [Y[I, J] := X[I, J] * 2]),
   shape(Y, Sy), shape(X, Sx),
   Sy = [Ny, 3], Sx = [Nx, 3],
   Ny == Nx, Ny == 64*K.
test(over_keeps_a_product_among_new_extents, [timeout(10)]) :-
   K::integer(1, 100), M::integer(1, 8),
   over([I:M, J:64*K, L:2*K*3], [Y[J, I, L + 1] := I + J + L]),
   shape(Y, [Nj, Ni, Nl]),
   Nj == 64*K,
   var(Ni), Ni \== M,
   var(Nl).
test(read_before_write, [timeout(10), fixme(needs_dependence_analysis),
                         throws(error(domain_error(written, _), _))]) :-
   over([T:8], [F[T] := F[T+1]]).

:- end_tests(core).

:- begin_tests(core_value).

floats(Values, Buffer) :-
   length(Values, N),
   compound_name_arguments(Data, data, Values),
   buffer_from_data(Buffer, float32, N, Data).

ints(Values, Buffer) :-
   length(Values, N),
   compound_name_arguments(Data, data, Values),
   buffer_from_data(Buffer, int32, N, Data).

% int_name(+Values, -A): an int name of Values, realized on host.
int_name(Values, A) :-
   length(Values, N),
   ints(Values, B),
   shape(A, [N]), kind(A, int),
   value(A, B).

test(value_round_trip, [timeout(10)]) :-
   floats([1.0, 2.0, 3.0], B),
   shape(A, [3]),
   value(A, B),
   value(A, B1), B1 == B,
   kind(A, float),
   device(A, host).

test(no_value, [timeout(10), fail]) :-
   shape(A, [3]),
   value(A, _).

test(value_needs_a_known_shape, [timeout(10), error(instantiation_error)]) :-
   floats([1.0], B),
   N::integer(1, 4),
   shape(A, [N]),
   value(A, B).

test(value_needs_its_bytes, [timeout(10), error(domain_error(value_bytes(16), _))]) :-
   floats([1.0, 2.0, 3.0], B),
   shape(A, [4]),
   value(A, B).

test(value_is_on_host, [timeout(10), error(domain_error(device(host), nvidia))]) :-
   floats([1.0], B),
   shape(A, [1]), device(A, nvidia),
   value(A, B).

test(realized_name_is_not_written, [timeout(10), error(permission_error(modify, realized_name, _))]) :-
   floats([1.0, 2.0], B),
   shape(A, [2]), value(A, B),
   over([I:2], [A[I] := 0]).

% A name unrealized has no value, and stays on its device.
test(unrealize_keeps_the_device, [timeout(10)]) :-
   floats([1.0, 2.0], B),
   shape(A, [2]), value(A, B),
   unrealize(A),
   \+ value(A, _),
   device(A, host).

% A realized name of integer elements has the bounds of its value, unless it declares wider.
test(inferred_bounds, [timeout(10), true(R == -7-12)]) :-
   int_name([3, -7, 12], A),
   elements(A, R).

test(no_bounds_for_floats, [timeout(10), fail]) :-
   floats([1.0], B),
   shape(A, [1]), value(A, B),
   elements(A, _).

test(declared_bounds_hold_the_value, [timeout(10), error(domain_error(elements(0-9), _))]) :-
   ints([3, 12], B),
   shape(A, [2]), kind(A, int), elements(A, 0-9),
   value(A, B).

test(bounds_declared_after_the_value, [timeout(10), error(domain_error(elements(0-3), _))]) :-
   int_name([0, 5], A),
   elements(A, 0-3).

test(declared_bounds_win, [timeout(10), true(R == 0-9)]) :-
   ints([3, 4], B),
   shape(A, [2]), kind(A, int), elements(A, 0-9),
   value(A, B),
   elements(A, R).

% A range in an unknown extent: the value constrains the extent.
test(value_bounds_an_extent, [timeout(10), true(Lo == 8)]) :-
   ints([7, 0], B),
   N::integer(1, 100),
   shape(A, [2]), kind(A, int), elements(A, 0-(N-1)),
   value(A, B),
   range(N, [Lo, _]).

test(bool_stored_as_ints, [timeout(10)]) :-
   ints([0, 1, 1], B),
   shape(A, [3]), kind(A, bool),
   value(A, B),
   kind(A, bool).

test(bool_out_of_range, [timeout(10), error(domain_error(elements(0-1), _))]) :-
   buffer_from_data(B, uint8, 2, data(0, 2)),
   shape(A, [2]), kind(A, bool),
   value(A, B).

% The bounds of a value stay on the name unrealized, its declared range: a program may have
% checked a subscript through it against them, so a value given again keeps them.
test(unrealize_keeps_inferred_bounds_as_declared, [timeout(10)]) :-
   int_name([1, 2], A),
   unrealize(A),
   elements(A, 1-2),
   ints([2, 1], B),
   value(A, B).

test(unrealize_refuses_a_wider_value_after, [timeout(10), error(domain_error(elements(1-2), _))]) :-
   int_name([1, 2], A),
   unrealize(A),
   ints([0, 5], B),
   value(A, B).

test(unrealize_keeps_a_declared_range, [timeout(10)]) :-
   shape(A, [2]), kind(A, int), elements(A, 0-9),
   ints([1, 2], B), value(A, B),
   unrealize(A),
   elements(A, 0-9),
   ints([0, 9], B1), value(A, B1).

:- end_tests(core_value).

:- begin_tests(core_device).

% The names of a box share one device, which unification propagates.
test(device_propagates, [timeout(10), true(D == nvidia)]) :-
   shape(X, [3]),
   over([I:3], [Y[I] := X[I] + 1]),
   device(Y, nvidia),
   device(X, D).

test(device_stays_unknown, [timeout(10)]) :-
   shape(X, [3]),
   over([I:3], [Y[I] := X[I] + 1]),
   device(X, D), device(Y, D1),
   var(D), D1 == D.

test(one_device_per_box, [timeout(10), error(domain_error(one_device, _))]) :-
   shape(X, [3]), device(X, pocl),
   shape(Z, [3]), device(Z, nvidia),
   over([I:3], [Y[I] := X[I] + Z[I]]),
   shape(Y, _).

test(no_box_on_host, [timeout(10), error(permission_error(compute, host, _))]) :-
   shape(X, [3]), device(X, host),
   over([I:3], [Y[I] := X[I] + 1]),
   shape(Y, _).

% A copy is a new name of the same shape and kind on its own device, which may stay unknown.
test(copy_across_devices, [timeout(10)]) :-
   N::integer(1, 10),
   shape(X, [N]), kind(X, int), elements(X, 0-3),
   copy(X, Y, D),
   shape(Y, S), S == [N],
   kind(Y, int),
   elements(Y, R), R == 0-3,
   device(X, DX), var(DX), var(D), DX \== D,
   over([I:N], [Z[I] := Y[I] * 2]),
   device(Z, DZ), DZ == D.

test(copy_into_a_written_name, [timeout(10), error(permission_error(modify, written_name, _))]) :-
   shape(X, [2]),
   over([I:2], [Y[I] := 1]),
   copy(X, Y, nvidia).

:- end_tests(core_device).

:- begin_tests(core_types).

% format_of(+Names, +Out, -Format): the format of Out once the names of its program are
% labelled, undone.
format_of(Names, Out, Format) :-
   findall(F, ( label_types(Names), type(Out, F) ), [Format]).

test(counter_of_literals_is_int32, [timeout(10), true(F == int32)]) :-
   over([], [S := 0]),
   over([_:10], [S := S + 1]),
   format_of([S], S, F).
test(index_alone_is_int32, [timeout(10), true(F == int32)]) :-
   over([I:100], [A[I] := I]),
   format_of([A], A, F).
test(int8_accumulator_stays_int8, [timeout(10), true(F == int8)]) :-
   shape(X, [10]), type(X, int8),
   over([], [S := 0]),
   over([K:10], [S := S + X[K]]),
   format_of([X, S], S, F).
test(count_of_bools_is_int32, [timeout(10), true(F == int32)]) :-
   shape(X, [3]),
   over([], [C := 0]),
   over([I:3], [C := C + (X[I] < 0)]),
   format_of([X, C], C, F).
test(index_over_extent_is_float32, [timeout(10), true(F == float32)]) :-
   over([I:100], [A[I] := I/100]),
   format_of([A], A, F).
test(float16_times_index_stays, [timeout(10), true(F == float16)]) :-
   shape(X, [100]), type(X, float16),
   over([I:100], [A[I] := X[I]*I]),
   format_of([X, A], A, F).
test(float16_times_wide_index_widens, [timeout(10), true(F == float32)]) :-
   N::integer(1, 100000),
   shape(X, [N]), type(X, float16),
   over([I:N], [A[I] := X[I]*I]),
   format_of([X, A], A, F).
test(float16_times_unbounded_index_stays, [timeout(10), true(F == float16)]) :-
   % an extent of no bound says nothing of its values: as a float literal
   shape(X, [N]), type(X, float16),
   over([I:N], [A[I] := X[I]*I/N]),
   format_of([X, A], A, F).
test(float16_times_literal_stays, [timeout(10), true(F == float16)]) :-
   shape(X, [3]), type(X, float16),
   over([I:3], [A[I] := X[I]*0.5 + 1]),
   format_of([X, A], A, F).
test(input_defaults, [timeout(10), true(Fs == [float32, int32, bool])]) :-
   shape(X, [3]), shape(Y, [3]), kind(Y, int), shape(Z, [3]), kind(Z, bool),
   findall(Fs0, ( label_types([X, Y, Z]), maplist(type, [X, Y, Z], Fs0) ), [Fs]).
test(int32_into_float_needs_float64, [timeout(10), true(F == float64)]) :-
   % a name on no device yet may be a float64, as a device may: the int needs a range or a
   % cast to stay in 32 bits
   shape(X, [3]), kind(X, int), shape(Y, [3]),
   over([I:3], [A[I] := X[I]*Y[I]]),
   format_of([X, Y, A], A, F).
test(int32_cast_into_float, [timeout(10), true(F == float32)]) :-
   shape(X, [3]), kind(X, int), shape(Y, [3]),
   over([I:3], [A[I] := cast(X[I], float32)*Y[I]]),
   format_of([X, Y, A], A, F).
test(ranged_int_into_float_stays_float32, [timeout(10), true(F == float32)]) :-
   shape(X, [3]), kind(X, int), elements(X, 0-255), shape(Y, [3]),
   over([I:3], [A[I] := X[I]*Y[I]]),
   format_of([X, Y, A], A, F).
test(range_in_extents_read_at_labelling, [timeout(10), true(F == float32)]) :-
   N::integer(1, 1000),
   shape(X, [3]), kind(X, int), elements(X, 0-(N-1)), shape(Y, [3]),
   over([I:3], [A[I] := X[I]*Y[I]]),
   format_of([X, Y, A], A, F).
test(range_declared_after_the_box, [timeout(10), true(F == float32)]) :-
   shape(X, [3]), kind(X, int), shape(Y, [3]),
   over([I:3], [A[I] := X[I]*Y[I]]),
   elements(X, 0-255),
   format_of([X, Y, A], A, F).
test(written_twice_reads_itself, [timeout(10), true(F == int16)]) :-
   shape(X, [3]), type(X, int8), shape(Y, [3]), type(Y, uint8),
   over([], [S := 0]),
   over([I:3], [S := S + X[I]]),
   over([I:3], [S := S + Y[I]]),
   format_of([X, Y, S], S, F).
test(bits_of_float32_is_int32, [timeout(10), true(F == int32)]) :-
   shape(X, [3]), kind(X, float),
   over([I:3], [A[I] := bits(X[I])]),
   format_of([X, A], A, F).
test(bits_of_float16_is_int32, [timeout(10), true(F == int32)]) :-
   shape(X, [3]), type(X, float16),
   over([I:3], [A[I] := bits(X[I])]),
   format_of([X, A], A, F).
test(comparison_is_bool, [timeout(10), true(F == bool)]) :-
   shape(X, [3]),
   over([I:3], [A[I] := X[I] < 0]),
   format_of([X, A], A, F).
test(where_joins_its_branches, [timeout(10), true(F == int16)]) :-
   shape(X, [3]), type(X, int8), shape(Y, [3]), type(Y, uint8),
   over([I:3], [A[I] := where(X[I] < 0, X[I], Y[I])]),
   format_of([X, Y, A], A, F).
test(declared_is_widened, [timeout(10), true(Fy-Fz == float32-float32)]) :-
   % Y is declared wider than what is written to it, and what reads it reads its format
   shape(X, [3]), type(X, int8), type(Y, float32),
   over([I:3], [Y[I] := X[I]]),
   over([I:3], [Z[I] := Y[I]*2]),
   format_of([X, Y, Z], Y, Fy),
   format_of([X, Y, Z], Z, Fz).
test(declared_narrower_is_an_error, [timeout(10), error(domain_error(type, _))]) :-
   shape(X, [3]), type(X, float32), type(Y, float16),
   over([I:3], [Y[I] := X[I]]).
test(declared_after_narrower_is_an_error, [timeout(10), error(domain_error(type(float16), _))]) :-
   shape(X, [3]), type(X, float32),
   over([I:3], [Y[I] := X[I]]),
   type(Y, float16).
test(declared_output_does_not_type_inputs, [timeout(10), error(domain_error(type, _))]) :-
   % the input takes its default, a float32, which the float16 declared does not hold
   shape(X, [3]), type(Y, float16),
   over([I:3], [Y[I] := X[I]*2]),
   format_of([X, Y], Y, _).
test(declared_output_leaves_input_open, [timeout(10), true(T = type(_, _, _, _, _, _, _))]) :-
   shape(X, [3]), type(Y, float16),
   over([I:3], [Y[I] := X[I]*2]),
   type(X, T).
test(cast_narrows, [timeout(10), true(F == float16)]) :-
   shape(X, [3]), type(Y, float16),
   over([I:3], [Y[I] := cast(X[I]*2, float16)]),
   format_of([X, Y], Y, F).
test(cast_sets_the_kind, [timeout(10), true(K == int)]) :-
   shape(X, [3]),
   over([I:3], [Y[I] := cast(X[I], uint8)]),
   findall(K0, ( label_kinds([X, Y]), kind(Y, K0) ), [K]).
test(shared_type_bound_late, [timeout(10), true(F == float16)]) :-
   shape(X, [3]), shape(Y, [3]), type(X, T), type(Y, T),
   over([I:3], [Z[I] := X[I] + Y[I]]),
   type(X, float16),
   format_of([X, Y, Z], Z, F).
test(type_like_follows, [timeout(10), true(Fs == [float16, float32])]) :-
   shape(X, [3]), type(X, float16), shape(B, [3]), type(B, like(X)),
   shape(Y, [3]), shape(C, [3]), type(C, like(Y)),
   findall(Fs0, ( label_types([X, B, Y, C]), maplist(type, [B, C], Fs0) ), [Fs]).
test(type_like_follows_a_written_name, [timeout(10), true(F == float16)]) :-
   shape(X, [3]), type(X, float16),
   over([I:3], [Y[I] := X[I] * 2]),
   shape(B, [3]), type(B, like(Y)),
   over([I:3], [Z[I] := B[I] + 1]),
   format_of([X, Y, B, Z], Z, F).
test(type_like_follows_a_name_outside_the_program, [timeout(10), true(Fs == [float16, float16])]) :-
   % the program of Z mentions neither Y nor X: they are resolved for B all the same
   shape(X, [3]), type(X, float16),
   over([I:3], [Y[I] := X[I] * 2]),
   shape(B, [3]), type(B, like(Y)),
   over([I:3], [Z[I] := B[I] * 3]),
   findall(Fs0, ( label_types([B, Z]), maplist(type, [B, Z], Fs0) ), [Fs]).
test(type_like_follows_an_input_resolved_after, [timeout(10), true(Fs == [float32, float32])]) :-
   % B is labelled before the input it follows: the input takes its default with it
   shape(X, [3]), kind(X, float), shape(B, [3]), type(B, like(X)),
   label_types([B]),
   label_types([X]),
   maplist(type, [B, X], Fs).
test(type_like_is_one_way, [timeout(10), true(T = type(_, _, _, _, _, _, _))]) :-
   % what the follower is written to never reaches the name it follows
   shape(X, [3]), shape(B, [3]), type(B, like(X)),
   type(Y, float16),
   over([I:3], [Y[I] := cast(B[I], float16)]),
   type(X, T).
test(type_declares_the_kind, [timeout(10), true(K == int)]) :-
   shape(X, [3]), type(X, uint8), kind(X, K).
test(type_against_kind, [timeout(10), error(domain_error(kind(float), _))]) :-
   shape(X, [3]), kind(X, int), type(X, float16).
test(unknown_format, [timeout(10), error(domain_error(format, f16))]) :-
   shape(X, [3]), type(X, f16).
test(uint64_int64_is_an_error, [timeout(10), error(domain_error(type, _))]) :-
   shape(X, [3]), type(X, uint64), shape(Y, [3]), type(Y, int64),
   over([I:3], [Z[I] := X[I] + Y[I]]),
   format_of([X, Y, Z], Z, _).
test(no_format_says_what_is_needed, [timeout(10), true(M == 'it needs a signed int of 65 bits, which no format holds: declare the inputs or cast')]) :-
   shape(X, [3]), type(X, uint64), shape(Y, [3]), type(Y, int64),
   over([I:3], [Z[I] := X[I] + Y[I]]),
   catch(format_of([X, Y, Z], Z, _), error(domain_error(type, _), context(_, M)), true).
test(literal_too_wide_for_declared, [timeout(10), error(domain_error(type, _))]) :-
   type(Y, uint8),
   over([I:3], [Y[I] := 300]).
test(copy_has_the_type, [timeout(10), true(F == float16)]) :-
   shape(X, [3]), type(X, float16), device(X, host),
   copy(X, Y, pocl),
   format_of([X, Y], Y, F).
test(kept_range_is_read_as_elements, [timeout(10), true(R == 0-9)]) :-
   shape(X, [3]), kind(X, int), device(X, host),
   buffer_from_data(B, int32, 3, data(0, 5, 9)),
   value(X, B),
   unrealize(X),
   copy(X, Xd, pocl),
   elements(Xd, R).
test(kept_range_does_not_narrow, [timeout(10), true(F == float64)]) :-
   % the bounds a value leaves behind are read by a subscript, never by a type: the float
   % still needs the mantissa of an int32, a float64
   shape(X, [3]), kind(X, int), device(X, host),
   buffer_from_data(B, int32, 3, data(0, 5, 9)),
   value(X, B),
   unrealize(X),
   copy(X, Xd, pocl),
   shape(Y, [3]),
   over([I:3], [A[I] := Xd[I]*Y[I]]),
   format_of([X, Xd, Y, A], A, F).
test(declared_range_narrows_after_unrealize, [timeout(10), true(F == float32)]) :-
   shape(X, [3]), kind(X, int), device(X, host),
   buffer_from_data(B, int32, 3, data(0, 5, 9)),
   value(X, B),
   unrealize(X),
   elements(X, 0-9),
   copy(X, Xd, pocl),
   shape(Y, [3]),
   over([I:3], [A[I] := Xd[I]*Y[I]]),
   format_of([X, Xd, Y, A], A, F).
test(host_holds_float64, [timeout(10), true(F == float64)]) :-
   shape(X, [3]), type(X, float64), device(X, host),
   format_of([X], X, F).
test(unplaced_name_may_be_float64, [timeout(10), true(F == float64)]) :-
   % a name on no device yet has every format a device may have
   shape(X, [3]), type(X, float64),
   format_of([X], X, F).

% A device without doubles, nodoubles, its formats given as compile.pl keeps those of an
% OpenCL device: no OpenCL device is asked.
no_doubles(Ref) :-
   asserta(compile:known_formats(nodoubles, [bool, int8, uint8, int16, uint16, int32, uint32, int64, uint64,
                                             float8_e4m3, float16, bfloat16, float32]), Ref).

test(declared_float64_on_a_device_without_doubles, [timeout(10), setup(no_doubles(Ref)), cleanup(erase(Ref)),
      true(M == 'it needs float64, a format the device nodoubles does not have')]) :-
   shape(X, [3]), type(X, float64), device(X, nodoubles),
   catch(format_of([X], X, _), error(domain_error(type, _), context(_, M)), true).
test(int32_into_float_on_a_device_without_doubles, [timeout(10), setup(no_doubles(Ref)), cleanup(erase(Ref)),
      true(M == 'it needs float64, a format the device nodoubles does not have')]) :-
   shape(X, [3]), kind(X, int), shape(Y, [3]),
   over([I:3], [A[I] := X[I]*Y[I]]),
   device(A, nodoubles),
   catch(format_of([X, Y, A], A, _), error(domain_error(type, _), context(_, M)), true).
test(labelled_with_no_device_then_placed_without_doubles, [timeout(10), setup(no_doubles(Ref)), cleanup(erase(Ref)),
      true(F-M == float64-'it needs float64, a format the device nodoubles does not have')]) :-
   % the types resolved while the device is unknown stay resolved: placed on a device
   % without doubles, the name is an error, never a narrower format
   shape(X, [3]), kind(X, int), shape(Y, [3]),
   over([I:3], [A[I] := X[I]*Y[I]]),
   label_types([X, Y, A]),
   type(A, F),
   device(A, nodoubles),
   catch(label_types([X, Y, A]), error(domain_error(type, _), context(_, M)), true).
test(labelled_with_no_device_stores_as_known, [timeout(10), true(Fs == [float64, float64])]) :-
   % labelled with no device, then placed on one with doubles: the format of the name is the
   % one it has when the device is known from the start
   shape(X, [3]), kind(X, int), shape(Y, [3]),
   over([I:3], [A[I] := X[I]*Y[I]]),
   label_types([X, Y, A]),
   device(A, pocl),
   core:storage(A, F1),
   shape(X2, [3]), kind(X2, int), shape(Y2, [3]),
   over([I:3], [A2[I] := X2[I]*Y2[I]]),
   device(A2, pocl),
   label_types([X2, Y2, A2]),
   core:storage(A2, F2),
   Fs = [F1, F2].
test(unrealize_keeps_the_format, [timeout(10), true(F == int32)]) :-
   shape(X, [3]), kind(X, int), device(X, host),
   buffer_from_data(B, int32, 3, data(0, 5, 9)),
   value(X, B),
   unrealize(X),
   type(X, F).
test(extent_copy_has_no_type_goal, [timeout(10), true(Gs =@= Gs0)]) :-
   % the goals of an extent are those of the same program without a type in sight
   N::integer(1, 1000),
   shape(X, [N]), type(X, float16),
   over([I:N], [_[I] := X[I]*I]),
   copy_term([N], [N1], Gs1),
   N0::integer(1, 1000),
   shape(X0, [N0]),
   over([J:N0], [_[J] := X0[J]]),
   copy_term([N0], [N01], Gs2),
   Gs = N1-Gs1, Gs0 = N01-Gs2.
test(residual_goal, [timeout(10), true(memberchk(type(_, float16), Gs))]) :-
   shape(X, [3]), type(X, float16),
   copy_term(X, _, Gs).

:- end_tests(core_types).
