% ================================================================= :- 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], Ls, [Y | Bs]) :- constr( (X= (X, Xs, Ls, Bs). :- pred append(list(int),list(int),list(int)). :- mode append(in,in,out). append([],Ys,Ys). append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs). :- pred cons(int,list(int),list(int)). :- mode cons(in,in,out). cons(H,T,[H|T]). % ================================================================= % catamorphisms :- pred listMax(list(int),bool,int). :- mode listMax(in,out,out). :- cata listMax/3-1. listMax([],IsDef,A) :- constr( ((~IsDef) & (A=0)) ). % 0 can be any int. listMax([H|T],IsDef,Max) :- constr( IsDef & (Max = ite(IsDefT, ite(H>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), % listMax(Ls,IsDefL,MaxLs), % constr( Max = ite(MBigs>MSmalls,MBigs,MSmalls) ), % lastElem(Smalls,IsDefLSmalls,LastSmalls), lastElem(Bigs,IsDefLBigs,LastBigs), % 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. % ================================================================= % Catamorphic abstraction :- cata_abs list(int) ==> lastElem(Xs,IsDefXs_LE,LastXs), listMax(Xs,IsDefXs,MaxXs). % =================================================================