View source with raw comments or as raw
    1/*
    2Dumb (but likely correct) Towers of Hanoi
    3
    4This is a simple version of the Towers of Hanoi which:
    5
    6- Defines the ToH as a (blind) planning: it does any moves it can do
    7  as long as there are discs in the pegs.
    8
    9- Does not tell you the order of the moves, only if there is a solution.
   10
   11- Defines global constraints to remove the incorrect states.
   12
   13- I cannot (as-is) be executed in ASP as it uses lists of size in
   14  principle unknown, but we know that they are bounded by the number
   15  of discs.
   16
   17However:
   18
   19- As there can be repeated movements without upper bound on its
   20  number, it can just loop forever --> s(CASP) should be able to catch
   21  that.
   22
   23- As the constraints on the bad movements are caught when the whole
   24  model is generated, it should be in any case hugely inefficient.
   25
   26*/
   27
   28hanoi([], [], _).
   29hanoi(A, B, C):-
   30    any_move(A, B, C, A1, B1, C1),
   31    hanoi(A1, B1, C1).
   32
   33any_move(A, B, C, A1, B, C1):- swap(A, C, A1, C1).
   34any_move(A, B, C, A1, B1, C):- swap(A, B, A1, B1).
   35any_move(A, B, C, A, B1, C1):- swap(C, B, C1, B1).
   36
   37swap(A, B, A1, B1):- moveto(A, B, A1, B1).
   38swap(A, B, A1, B1):- moveto(B, A, B1, A1).
   39
   40moveto([A|A1], B, A1, [A|B]).
   41
   42:- hanoi(A, B, C), inverted(A).   43:- hanoi(A, B, C), inverted(B).   44:- hanoi(A, B, C), inverted(C).   45
   46inverted([A,B|_]):- A > B.
   47
   48?- hanoi([1,2,3], [], []).