Skip to content

Indicate the computed and actual index sets when a mismatch is detected. #510

Closed
@zayenz

Description

@zayenz

I think that error messages when array dimensions do not match would benefit from improvement.

As a somewhat reasonable example, consider the following code that could be a part of implementing black hole solitaire

int: suits = 4;
set of int: Suits = 1..suits;

int: ranks = 13;
set of int: Ranks = 1..ranks;

int: cards = 52;
set of int: Cards = 1..cards;

function Cards: card_from(Suits: suit, Ranks: rank) =
  (suit - 1) * ranks + rank;
  
function Ranks: next(Ranks: rank) =
  (rank mod ranks) + 1;
  
function Ranks: prev(Ranks: rank) =
  ((rank - 1 + ranks - 1) mod ranks) + 1;

array[int, 1..2] of Cards: black_hole_pairs = array2d(1..(suits * suits * ranks * 2), 1..2, [
    [
      card_from(suit, rank), card_from(other_suit, next(rank)),
      card_from(suit, rank), card_from(other_suit, prev(rank))
    ][i]
  | suit in Suits, rank in Ranks, other_suit in Suits,
    i in 1..4
]);

output [show2d(black_hole_pairs)];

Now, forgetting that we generate two rows per round in the comprehension, it is easy to forget the * 2 part in the array2d construction. That would give the error message

Running array_index_error_message.mzn
MiniZinc: evaluation error: 
/Users/zayenz/projects/minizinc/tests/array_index_error_message.mzn:19:
  in variable declaration for 'black_hole_pairs'
  in call 'array2d'
  mismatch in array dimensions

Process finished with non-zero exit code 1
Finished in 69msec

which, while clearly describing the problem, is not really informative.

As a side note, this is a fairly realistic example that uses the construction proposed in #403 to simulate generate multiple values in each round of the comprehension. While eminently workable, it is not really a discoverable construction IMHO.

Also, given that the compiler knows the number of elements, not having to compute the index set manually would be even better. Still, even if it would be possible to skip the computation here, it is still worth it to have better error message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementresolvedIssue is resolved and the feature or fix will be part of next release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions