View source with raw comments or as raw
    3hanoi([], [], _).
    4hanoi(A, B, C):-
    5    any_move(A, B, C, A1, B1, C1),
    6    hanoi(A1, B1, C1).
    7
    8any_move(A, B, C, A1, B, C1):- swap(A, C, A1, C1).
    9any_move(A, B, C, A1, B1, C):- swap(A, B, A1, B1).
   10any_move(A, B, C, A, B1, C1):- swap(C, B, C1, B1).
   11
   12swap(A, B, A1, B1):- moveto(A, B, A1, B1).
   13swap(A, B, A1, B1):- moveto(B, A, B1, A1).
   14
   15moveto([A|A1], B, A1, [A|B]) :- not inverted([A|B]).
   16
   17inverted([A,B|_]):- A > B.
   18
   19
   20?- hanoi([1,2,3], [], []).   21
   22
   23#show hanoi/3.
   24
   25
   26
   27/*
   28?- ?? hanoi([1,2,3],[],[]).
   29
   30Answer 1        (in [23.284] ms):
   31{ hanoi([1,2,3],[],[]) , hanoi([2,3],[],[1]) , hanoi([3],[2],[1]) , hanoi([1,3],[2],[]) , hanoi([3],[1,2],[]) , hanoi([],[1,2],[3]) , hanoi([1],[2],[3]) , hanoi([],[2],[1,3]) , hanoi([2],[],[1,3]) , hanoi([1,2],[],[3]) , hanoi([2],[1],[3]) , hanoi([],[1],[2,3]) , hanoi([1],[],[2,3]) , hanoi([],[],[1,2,3]) }
   32
   33
   34next ? ;
   35
   36Answer 2        (in [13.626] ms):
   37{ hanoi([1,2,3],[],[]) , hanoi([2,3],[],[1]) , hanoi([3],[2],[1]) , hanoi([1,3],[2],[]) , hanoi([3],[1,2],[]) , hanoi([],[1,2],[3]) , hanoi([1],[2],[3]) , hanoi([],[2],[1,3]) , hanoi([2],[],[1,3]) , hanoi([1,2],[],[3]) , hanoi([2],[1],[3]) , hanoi([],[1],[2,3]) , hanoi([],[],[1,2,3]) }
   38
   39
   40next ? 
   41 */