:- pred constr(bool). :- mode constr(in). :- ignore constr/1. :- type tree(D) ---> leaf ; node( D, tree(D), tree(D) ). % empty tree key left-subtree right-subtree % tree(d) is the type of the trees whose keys have type D. % Here we assume that "D" is "int". % Note: duplicates are NOT allowed. % ========================================================== % Program. % Binary Search Tree - duplicates not allowed. :- pred bstinsert(int,tree(int),tree(int)). :- mode bstinsert(in,in,out). bstinsert(X,leaf,node(X,leaf,leaf)). bstinsert(X,node(A,L,R), node(A,L,R)) :- constr(X=A). bstinsert(X,node(A,L,R), node(A,L1,R)) :- constr(X N>MaxL)) & (LeqRight=(IsDefR => NMaxR, ite(MaxL>N,MaxL,N), ite(MaxR>N,MaxR,N) ), % max(MaxL,max(MaxR,N)) ite(IsDefL, ite(MaxL>N,MaxL,N), % max(MaxL,N) ite(IsDefR & MaxR>N,MaxR,N))))). % max(MaxR,N) :- pred treemin(tree(int),bool,int). :- mode treemin(in,out,out). :- cata treemin/3-1. treemin(leaf,IsDef,Min) :- constr(~IsDef & Min=0). treemin(node(N,L,R),IsDef,Min) :- treemin(L,IsDefL,MinL), treemin(R,IsDefR,MinR), constr(IsDef & (Min= ite(IsDefL & IsDefR, ite(MinL bstree(T1,B1), treemin(T1,IsDef1,Min1),treemin(T2,IsDef2,Min2), treemax(T1,IsDef1,Max1),treemax(T2,IsDef2,Max2) => constr(IsDef2 & (B1 => (Min2=ite(IsDef1 & Min1X,Max1,X)) ) ). % ========================================================== % Verification :- pred ff1. ff1 :- constr(~ (IsDef2 & (B1 => (Min2=ite(IsDef1 & Min1X,Max1,X)) ) )), bstinsert(X,T1,T2), bstree(T1,B1), treemin(T1,IsDef1,Min1),treemin(T2,IsDef2,Min2), treemax(T1,IsDef1,Max1),treemax(T2,IsDef2,Max2). % ==========================================================% NON-CATA predicates: bstinsert constr