% ================================================================= :- pred constr(bool). :- mode constr(in). :- ignore constr/1. % ==================================================== % Program. :- pred perm(list(int),list(int)). :- mode perm(in,in). % in,in: perm is not functional perm([],[]). perm([X|Xs],[H|Perm]):- cons(X,Xs,XXs), delete(X,XXs,Rest), % error: X should be H perm(Rest,Perm). :- pred delete(int,list(int),list(int)). :- mode delete(in,in,in). % delete is not total delete(X,[Y|T],T) :- constr(X=Y). % Taking X away from [Y|T]. Deleting exactly one copy of X. delete(X,[Y|T],[Y|TD]) :- % The list need not be ordered. The element X need not be the minimum. constr(~(X=Y)), % delete(X,L,LD) succeeds **IFF** X is a member of L. delete(X,T,TD). :- pred cons(int,list(int),list(int)). :- mode cons(in,in,out). cons(H,T,[H|T]). % ==================================================== % catamorphisms :- pred sum(list(int),int). :- mode sum(in,out). :- cata sum/2-1. sum([],S) :- constr(S=0). sum([H|T],S) :- constr( S=(H+ST) ), sum(T,ST). % ==================================================== % verification. % Property: :- pred ff1. ff1 :- % perm constr(~(SL=SS)), sum(L,SL), sum(LP,SS), perm(L,LP). :- pred ff2. ff2 :- % delete constr( ~(SLD = (SL-X) )), % delete(X,L,LD) succeeds iff X is a member of L sum(L,SL), sum(LD,SLD), delete(X,L,LD). :- pred ff3. ff3 :- % cons constr( ~( SHT = (ST+H) ) ), sum(T,ST), sum(HT,SHT), cons(H,T,HT). % ================================================================= :- query ff1/0. :- query ff2/0. :- query ff3/0. % =================================================================