% ================================================================= :- pred constr(bool). :- mode constr(in). :- ignore constr/1. :- type bintree_int ---> leaf ; node(int,bintree_int,bintree_int). % This is a binary tree supporting a heap. % leaf is the EMPTY bintree_int. Nodes may have DUPLICATE integer values. % ================================================================ % Program: descending sorting. :- pred heapsort(list(int),list(int)). :- mode heapsort(in,out). heapsort(L,SL) :- list_to_heap(L,H), heap_to_list(H,SL). % =============================================================== % making a heap out of a list. :- pred list_to_heap(list(int),bintree_int). :- mode list_to_heap(in,out). list_to_heap([], leaf). list_to_heap([X|Xs], Heap) :- % list_to_heap(Xs, HeapXs), % error: missing atom insert_heap(X, HeapXs, Heap). % ------- :- pred insert_heap(int,bintree_int,bintree_int). :- mode insert_heap(in,in,out). % correct clause below: insert_heap(X, leaf, node(X,leaf,leaf)). insert_heap(X, leaf, leaf). insert_heap(X, node(Top,L,R), node(Top,R,L1)) :- % Torsion! node(Top,R,L1), not node(Top,L1,R), and constr(X=Top), insert_heap(Top,L,L1). % X on top. Insert Top in the left son-node. :- pred mkbtree(int,bintree_int,bintree_int,bintree_int). :- mode mkbtree(in,in,in,out). mkbtree(Top,Left,Right, node(Top,Left,Right)). % ---------------------------------- % Making a weakly-descending list out of a heap. :- pred heap_to_list(bintree_int,list(int)). :- mode heap_to_list(in,out). heap_to_list(leaf, []). heap_to_list(node(Top,leaf,leaf), [Top]). % node(Top,leaf,leaf) heap_to_list(node(Top,node(LTop,LL,LR),leaf), [Top|Tail]) :- % node(Top,node(...),leaf) mkbtree(LTop,LL,LR,NewHeap), heap_to_list(NewHeap,Tail). heap_to_list(node(Top,leaf,node(RTop,RL,RR)), [Top|Tail]) :- % node(Top,leaf,node(...)) mkbtree(RTop,RL,RR,NewHeap), heap_to_list(NewHeap,Tail). heap_to_list(node(Top,node(LT,LL,LR),node(RT,RL,RR)), [Top|Tail]) :- % node(Top,node(...),node(...)) mkbtree(LT,LL,LR,NewLHeap), mkbtree(RT,RL,RR,NewRHeap), heap_merge(NewLHeap,NewRHeap,PercHeap), heap_to_list(PercHeap,Tail). :- pred heap_merge(bintree_int,bintree_int,bintree_int). :- mode heap_merge(in,in,out). % heap_merge is a total function heap_merge(leaf,leaf, leaf). % in: leaf,leaf heap_merge(node(LTop,LL,LR),leaf, node(LTop,LL,LR)). % in: node(...),leaf heap_merge(leaf,node(RTop,RL,RR), node(RTop,RL,RR)). % in: leaf,node(...) heap_merge(node(LTop,LL,LR),node(RTop,RL,RR), node(RTop,node(LTop,LL,LR),PercHeap)) :- % in: node(...),node(...) =< LTop =< RTop, heap_merge(RL,RR,PercHeap). heap_merge(node(LTop,LL,LR),node(RTop,RL,RR), node(LTop,PercHeap,node(RTop,RL,RR))) :- % in: node(...),node(...) > LTop > RTop, heap_merge(LL,LR,PercHeap). % =============================================================== % Catamorphisms. % size of a binary-tree. :- pred btsize(bintree_int,int). :- mode btsize(in,out). :- cata btsize/2-1. btsize(leaf,0). btsize(node(T,L,R),S) :- constr( S=SL+SR+1 ), btsize(L,SL), btsize(R,SR). % --- length of a list :- pred length(list(int),int). :- mode length(in,out). :- cata length/2-1. length([],Res) :- constr( Res=0 ). length([H|T],Res) :- constr( Res=ResT+1 ), length(T,ResT). % =============================================================== % ff1. Contract on heapsort: length :- pred ff1. ff1 :- constr(~(N1=N2)), length(L,N1), length(S,N2), heapsort(L,S). % ---------------------------------------------------------------- % Contract on list_to_heap: :- pred ff11. ff11 :- constr( ~(NL=NH)), length(L,NL), btsize(H,NH), list_to_heap(L,H). % ------------------ % Contract on heap_to_list: :- pred ff12. ff12 :- constr( ~(SH = LL)), btsize(H,SH), length(L,LL), heap_to_list(H,L). % ------------------ % Contract on insert_heap: :- pred ff2. ff2 :- constr( ~(SH1=SH+1)), btsize(H,SH), btsize(H1,SH1), insert_heap(X,H,H1). % ---------------------------------------------------------------- % Contract on heap_merge: :- pred ff3. ff3 :- constr(~(SHL + SHR = SH1)), btsize(HL,SHL), btsize(HR,SHR), btsize(H1,SH1), heap_merge(HL,HR,H1). %---------------------------------------------------------------- % Contract on mkbtree: :- pred ff4. ff4 :- constr(~(SHL + SHR + 1 = SH1)), btsize(HL,SHL), btsize(HR,SHR), btsize(H1,SH1), mkbtree(T,HL,HR,H1). % =============================================================== :- query ff1/0. :- query ff11/0. :- query ff12/0. :- query ff2/0. :- query ff3/0. :- query ff4/0. % ===============================================================