% ================================================================= :- 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. concat4(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( (X>Y) ), partition(X, Ys, Ls, Bs). partition(X, [Y | Xs], Ls, [Y | Bs]) :- constr( (X= (H=MaxT,H,MaxT), H)) ), listmax(T,IsDefT,MaxT). % --- :- pred hd(list(int),bool,int). :- mode hd(in,out,out). :- cata hd/3-1. hd([],IsDef,Hd) :- constr( ~IsDef & Hd=0 ). % 0 can be any int. hd([H|T],IsDef,Hd) :- constr( IsDef & Hd=H ). % ================================================================= % Verification. % Property: :- pred ff1. ff1 :- constr(~BS), %listmin(L,IsDefMinL,MinL), listmax(L,IsDefMaxL,MaxL), %listmin(S,IsDefMinS,MinS), listmax(S,IsDefMaxS,MaxS), %is_asorted(L,BL), is_asorted(S,BS), quicksort(L,S). % %:- spec partition(P,Xs,Ls,Bs) ==> % listmin(Ls,IsDefMinLs,MinLs), listmax(Ls,IsDefMaxLs,MaxLs), is_asorted(Ls,B3), % listmin(Bs,IsDefMinBs,MinBs), listmax(Bs,IsDefMaxBs,MaxBs), is_asorted(Bs,B4), % listmin(Xs,IsDefMinXs,MinXs), listmax(Xs,IsDefMaxXs,MaxXs), is_asorted(Xs,B5) % => constr(true). % %:- spec concat4(Xs,Y,Ys,Zs) ==> % listmin(Xs,IsDefMinXs,MinXs), listmax(Xs,IsDefMaxXs,MaxXs), % listmin(Ys,IsDefMinYs,MinYs), listmax(Ys,IsDefMaxYs,MaxYs), % listmin(Zs,IsDefMinZs,MinZs), listmax(Zs,IsDefMaxZs,MaxZs), % is_asorted(Xs,B3), is_asorted(Ys,B4), is_asorted(Zs,B5) => constr(true). % ================================================================= :- query ff1/0. % ================================================================= % Catamorphic abstraction :- cata_abs list(int) ==> listmin(Xs,IsDefMinXs,MinXs), listmax(Xs,IsDefMaxXs,MaxXs), is_asorted(Xs,B3). % =================================================================