% ================================================================= :- 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. % Program predicate: not be a catamorphism. :- 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). insert_heap(X, leaf, node(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. % Program predicate: need not be a catamorphism. :- 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]). heap_to_list(node(Top,node(LTop,LL,LR),leaf), [Top|Tail]) :- mkbtree(LTop,LL,LR,NewHeap), heap_to_list(NewHeap,Tail). heap_to_list(node(Top,leaf,node(RTop,RL,RR)), [Top|Tail]) :- 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]) :- mkbtree(LT,LL,LR,NewLHeap), mkbtree(RT,RL,RR,NewRHeap), heap_merge(NewLHeap,NewRHeap,MergedHeap), heap_to_list(MergedHeap,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). heap_merge(node(LTop,LL,LR),leaf, node(LTop,LL,LR)). heap_merge(leaf,node(RTop,RL,RR), node(RTop,RL,RR)). heap_merge(node(LTop,LL,LR),node(RTop,RL,RR), node(RTop,node(LTop,LL,LR),MergedHeap)) :- LTop =< RTop, heap_merge(RL,RR,MergedHeap). heap_merge(node(LTop,LL,LR),node(RTop,RL,RR), node(LTop,MergedHeap,node(RTop,RL,RR))) :- LTop > RTop, heap_merge(LL,LR,MergedHeap). % =============================================================== % Catamorphisms. % size of a binary-tree. :- pred btreesize(bintree_int,int). :- mode btreesize(in,out). :- cata btreesize/2-1. btreesize(leaf,S) :- S=1. btreesize(node(T,L,R),S) :- S=SL+SR+1, btreesize(L,SL), btreesize(R,SR). % --- size of a list :- pred listsize(list(int),int). :- mode listsize(in,out). :- cata listsize/2-1. listsize([],Res) :- Res=1. listsize([H|T],Res) :- Res=ResT+1, listsize(T,ResT). % =============================================================== % ff1. Contract on heapsort: listsize :- pred ff1. ff1 :- constr(~(N1=N2)), listsize(L,N1), listsize(S,N2), heapsort(L,S). % %:- spec list_to_heap(L,H) ==> % listsize(L,NL), btreesize(H,NH) => constr(true). % %:- spec heap_to_list(H,L) ==> % btreesize(H,SH), listsize(L,LL) => constr(true). % %:- spec insert_heap(X,H,H1) ==> % btreesize(H,SH), btreesize(H1,SH1) => constr(true). % %:- spec heap_merge(HL,HR,H1) ==> % btreesize(HL,SHL), btreesize(HR,SHR), btreesize(H1,SH1) => constr(true). % %:- spec mkbtree(T,HL,HR,H1) ==> % btreesize(HL,SHL), btreesize(HR,SHR), btreesize(H1,SH1) => constr(true). % =============================================================== :- query ff1/0. % ================================================================ % %% Catamorphic abstraction % :- cata_abs list(int) ==> listsize(L,N1). :- cata_abs bintree_int ==> btreesize(H,NH). % %% ================================================================