Your definition of same_length/2
will not terminate very often. Instead, consider
same_length([],[]).
same_length([_|Xs], [_|Ys]) :-
same_length(Xs, Ys).
equally, using library(lambda)
use
... maplist(\_^_^true,Xs, Ys), ...
in place of
... same_length(Xs, Ys), ...
It seems you want to reformulate sorting by stating first, that the list is ordered, and only then searching for a permutation. Below works in SICStus, SWI, YAP.
ordered2([]).
ordered2([_]).
ordered2([X,Y|Xs]) :-
when((nonvar(X),nonvar(Y)),
( X =< Y, ordered2([Y|Xs]) )).
list_sorted2(Xs,Ys) :-
maplist(\_^_^true,Xs,Ys),
ordered2(Ys),
perm(Ys,Xs).
Please note that the arguments in perm/2 are now exchanged! Using SWI:
?- time(order([10,9,8,7,6,5,4,3,2,1],Xs)).
% 38,434,099 inferences, 10.655 CPU in 11.474 seconds (93% CPU, 3607101 Lips)
?- time(list_sorted2([10,9,8,7,6,5,4,3,2,1],Xs)).
% 50,139 inferences, 0.023 CPU in 0.032 seconds (72% CPU, 2205620 Lips)