% ================================================================= :- pred constr(bool). :- mode constr(in). :- ignore constr/1. % ================================================================= % Program. ascending sorting (ascending because we take the minimum) 2 =< 4 =< 7 =< ... :- pred selectionsort(list(int),list(int)). :- mode selectionsort(in,out). selectionsort([],[]). selectionsort([H|T],[Min|T1]) :- cons(H,T,HT), listMinnc(HT,IsDef,Min), % HT is not empty. IsDef is true delete(Min,HT,L1), % Taking the minimum away from list HT selectionsort(L1,T1). :- pred delete(int,list(int),list(int)). :- mode delete(in,in,out). %delete(X,[],[]). % delete is not total delete(X,[Y|T],T) :- constr(X=Y). % Taking X away from [Y|T]. delete(X,[Y|T],[Y|TD]) :- % Deleting exactly one copy of X. constr(~(X=Y)), % The list is not ordered. delete(X,T,TD). % The element X need not be the minimum. :- pred listMinnc(list(int),bool,int). :- mode listMinnc(in,out,out). listMinnc([],IsDef,A) :- constr( ((~IsDef) & (A=0)) ). % 0 can be any int listMinnc([H|T],IsDef,Min) :- constr( IsDef & (Min = ite(IsDefT, ite(H= ((H= (( BL => BLD) & (MinLD >= MinL))) & (IsDefLD => IsDefL)) & ((IsDefL & (~IsDefLD)) => (X=MinL)) )). :- pred ff3. ff3 :- constr( ~( ((BT & IsDefT) & (H= BHT ) ), is_asorted(T,BT), listMin(T,IsDefT,MinT), is_asorted(HT,BHT), cons(H,T,HT). % ================================================================= :- pred ff4. ff4:- constr( ~((IsDefMinL=IsDefMinncL) & (IsDefMinL => (MinL=MinncL)))), listMin(L,IsDefMinL,MinL), listMinnc(L,IsDefMinncL,MinncL). % ================================================================= :- query ff1/0. :- query ff2/0. :- query ff3/0. :- query ff4/0. % =================================================================