:- pred constr(bool). :- mode constr(in). :- ignore constr/1. % ================================================================= % Program :- pred quicksort(list(int),list(int)). :- mode quicksort(in,out). quicksort([], []). quicksort([X | Xs], ResL) :- partition(X, Xs, Smalls, Bigs), % partition of the tail Xs quicksort(Smalls, Ls), % with respect to the pivot X. quicksort(Bigs, Bs), % X is NOT in Xs. concat(Ls,X,Bs,ResL). :- pred partition(int,list(int),list(int),list(int)). :- mode partition(in,in,out,out). partition(X, [], [], []). partition(X, [Y | Ys], [Y | Ls], Bs) :- constr( (XY partition(X, Ys, Ls, Bs). partition(X, [Y | Xs], Ls, [Y | Bs]) :- constr( (X=MaxT,H,MaxT), H )) ), listMax(T,IsDefT,MaxT). :- pred lastElem(list(int),bool,int). :- mode lastElem(in,out,out). :- cata lastElem/3-1. lastElem([],IsDef,Last) :- constr( ((~IsDef) & (Last=0)) ). lastElem([H|T],IsDef,Last) :- constr( IsDef & (Last=ite(IsDefT,LastT,H)) ), lastElem(T,IsDefT,LastT). % ================================================================= % Verification. % Property: :- pred ff1. ff1 :- % listMax of unsorted equal lastElem of ascending, quicksorted list constr( ~(( (IsDefLMax = IsDefSMax) & (IsDefSMax = IsDefSLast)) & (IsDefLMax => ((MaxL=LastS) & (MaxL=MaxS))) )), listMax(L,IsDefLMax,MaxL), listMax(S,IsDefSMax,MaxS), lastElem(S,IsDefSLast,LastS), quicksort(L,S). :- pred ff2. ff2 :- % listMax and lastElem on partition constr( ~(( (((( IsDefS & IsDefB ) => ( IsDefL & ( MaxLs=Max ) )) & (( IsDefS & ~IsDefB ) => ( IsDefL & ( MaxLs=MSmalls ) ))) & (( ~IsDefS & IsDefB ) => ( IsDefL & ( MaxLs=MBigs ) ))) & (( ~IsDefS & ~IsDefB ) = ( ~IsDefL )) ) & ( (((~IsDefLSmalls & IsDefLBigs ) => ( IsDefLastLs & ( LastLs=LastBigs ) )) & (( IsDefLSmalls & ~IsDefLBigs ) => ( IsDefLastLs & ( LastLs=LastSmalls ) ))) & (( ~IsDefLSmalls & ~IsDefLBigs ) = ( ~IsDefLastLs )) ) )), listMax(Smalls,IsDefS,MSmalls), listMax(Bigs,IsDefB,MBigs), % for listMax listMax(Ls,IsDefL,MaxLs), % for listMax constr( Max = ite(MBigs>MSmalls,MBigs,MSmalls) ), % for listMax lastElem(Smalls,IsDefLSmalls,LastSmalls), lastElem(Bigs,IsDefLBigs,LastBigs), % for lastElem lastElem(Ls,IsDefLastLs,LastLs), partition(P,Ls,Smalls,Bigs). :- pred ff3. ff3 :- % listMax and lastElem on concat constr(~(( ((((IsDefXs & IsDefYs ) => ( IsDefZs & ( MaxZs=Max3 ) )) & (( IsDefXs & ~IsDefYs ) => ( IsDefZs & ( MaxZs=ite(MaxXs>Y,MaxXs,Y) ) ))) & (( ~IsDefXs & IsDefYs ) => ( IsDefZs & ( MaxZs=ite(MaxYs>Y,MaxYs,Y) ) ))) & (( ~IsDefXs & ~IsDefYs ) => ( IsDefZs & ( MaxZs=Y ))) ) & ( IsDefLEZs & ( (( IsDefLEYs) => ( IsDefLEZs & ( LastZs=LastYs ))) & ((~IsDefLEYs) => ( IsDefLEZs & ( LastZs=Y )))) ))), listMax(Xs,IsDefXs,MaxXs), listMax(Ys,IsDefYs,MaxYs), listMax(Zs,IsDefZs,MaxZs), lastElem(Xs,IsDefLEXs,LastXs), lastElem(Ys,IsDefLEYs,LastYs), % for lastElem lastElem(Zs,IsDefLEZs,LastZs), constr( Max2=ite(MaxXs>MaxYs,MaxXs,MaxYs) & Max3=ite(Max2>Y,Max2,Y) ), concat(Xs,Y,Ys,Zs). % ================================================================= :- query ff1/0. :- query ff2/0. :- query ff3/0. % =================================================================