sortingprologquicksorttracesearch-tree

How to draw a diagram for running trace of quicksort in Prolog


I have a code for quicksort in prolog:

gt(X,Y):- X @> Y.
conc([], List, List).
conc([Head|Tail], List1, [Head|List2]):- conc(Tail, List1, List2).

quicksort([], []).
quicksort([X|Tail], Sorted):-
    split(X,Tail,Small,Big),
    quicksort(Small,SortedSmall),
    quicksort(Big, SortedBig),
    conc(SortedSmall, [X|SortedBig], Sorted).

split(X,[],[],[]).
split(X,[Y|Tail],[Y|Small],Big):-
    gt(X,Y),!,
    split(X,Tail,Small, Big).
split(X,[Y|Tail],Small,[Y|Big]):-
    split(X,Tail,Small,Big).

The example is quicksort([3,2,4,1,5], Sorted). I've almost draw this one, but I only find the trace for a list of Small=[2, 1], then I coudn't do the same for a list of Big number. Is there anybody can help me to draw a diagram for this code? I want to understand the running trace the program. I'd really appreciate that!


Solution

  • Drawing proof trees is a fashinating theme, not entirely solved.

    Proof trees contains information essential while debugging, but inferring the shape from the trace is not easy, since each step is marked by an activation number. And we have limited attention, disturbed by the sheer amount of information that a proof tree exposes.

    But the shape can be recovered: for instance, a DCG that parses trace and translate to (for instance) Graphviz...

    Please be patient, I'll try to post some code. Your question give me a chance to implement what seems to be a nice addition to my small Prolog IDE (loqt).

    (I use my SW here to render the tree)