% ================================================================= :- 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 in the given list [X|Xs]. quicksort(Bigs, Bs), % X is NOT in Xs. cons(X,Bs,XBs), append(Ls, XBs, ResL). % partition(4,[2,7,4,1],L,B) ==> L(ittles)=[2,1], B(igs)=[7,4] :- 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( (X>Y) ), partition(X, Ys, Ls, Bs). partition(X, [Y | Xs], Bs, [Y | Bs]) :- % error Bs --> Ls 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)) ). % 0 can be any int. 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) & (IsDefLMax = 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 append constr( ~(( (((( IsDefXs & IsDefYs ) => ( IsDefZs & ( MaxZs=Max ) )) & (( IsDefXs & ~IsDefYs ) => ( IsDefZs & ( MaxZs=MaxXs ) ))) & (( ~IsDefXs & IsDefYs ) => ( IsDefZs & ( MaxZs=MaxYs ) ))) & (( ~IsDefXs & ~IsDefYs ) = ( ~IsDefZs )) ) & ( ((((( IsDefYs_LE ) => ( IsDefZs_LE & ( LastZs=LastYs ) )) & (( IsDefXs_LE & ~IsDefYs_LE ) => ( IsDefZs_LE & ( LastZs=LastXs ) )))) & (( ~IsDefXs_LE & ~IsDefYs_LE ) = ( ~IsDefZs_LE ))) )) ), listMax(Xs,IsDefXs,MaxXs), listMax(Ys,IsDefYs,MaxYs), listMax(Zs,IsDefZs,MaxZs), constr( Max = ite(MaxXs>MaxYs,MaxXs,MaxYs) ), lastElem(Xs,IsDefXs_LE,LastXs), lastElem(Ys,IsDefYs_LE,LastYs), lastElem(Zs,IsDefZs_LE,LastZs), append(Xs,Ys,Zs). % ================================================================= :- query ff1/0. :- query ff2/0. :- query ff3/0. % =================================================================