% ================================================================= :- 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]):- % a permutation cons(X,Xs,XXs), delete(H,XXs,Rest), 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],TD) :- % The list need not be ordered. 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). % error: TD in head should be [Y|TD] :- pred cons(int,list(int),list(int)). :- mode cons(in,in,out). cons(H,T,[H|T]). % ==================================================== % catamorphisms :- pred listsize(list(int),int). :- mode listsize(in,out). :- cata listsize/2-1. listsize([],L) :- L=1. listsize([H|T],L) :- L=(LT+1), listsize(T,LT). % ==================================================== % verification. % Property: :- pred ff1. ff1 :- constr( ~(LL=LLP)), listsize(L,LL), listsize(LP,LLP), perm(L,LP). % contracts (postcondition: true) :- spec delete(H,L,LD) ==> listsize(L,SL), listsize(LD,SLD) => true. :- spec cons(H,T,HT) ==> listsize(T,ST), listsize(HT,SHT) => true. % ==================================================== :- query ff1/0. % ====================================================