1:- module(canny_permutations,
    2          [   permute_sum_of_int/2,     % +N, -Integers
    3              permute_list_to_grid/2    % +List0, -List
    4          ]).
 permute_sum_of_int(+N:nonneg, -Integers:list(integer)) is nondet
Permute sum. Non-deterministically finds all combinations of integer sums between 1 and N. Assumes that 0<=N. The number of possible permutations amounts to 2-to-the-power of N-1; for N=3 there are four as follows: 1+1+1, 1+2, 2+1 and 3.
   13permute_sum_of_int(0, []) :- !.
   14permute_sum_of_int(N, [H|T]) :-
   15    between(1, N, H),
   16    N0 is N - H,
   17    permute_sum_of_int(N0, T).
 permute_list_to_grid(+List0:list, -List:list(list)) is nondet
Permutes a list to two-dimensional grid, a list of lists. Given an ordered List0 of elements, unifies List with all possible rows of columns. Given a, b and c for example, permutes three rows of single columns a, b, c; then a in the first row with b and c in the second; then a and b in the first row, c alone in the second; finally permutes a, b, c on a single row. Permutations always preserve the order of elements from first to last.
   30permute_list_to_grid(List0, List) :-
   31    length(List0, N),
   32    permute_sum_of_int(N, Lengths),
   33    permute_list_to_grid_(List0, Lengths, List).
   34
   35permute_list_to_grid_([], [], []) :- !.
   36permute_list_to_grid_(List0, [H0|T0], [H|T]) :-
   37    length(H, H0),
   38    append(H, H_, List0),
   39    permute_list_to_grid_(H_, T0, T)