(* BTS: bubble top stack if you push an element onto the stack, and the element is already in the stack, that element "bubbles" to the top of the stack *) datatype BTS = New | push of BTS * int ; fun mem (New,i) = false | mem (push(B,i),j) = if i=j then true else mem(B,j) ; fun size (New) = 0 | size (push(B,i)) = if mem(B,i) then size(B) else size(B)+1 ; exception topEmptyStack; fun top (New) = raise topEmptyStack | top (push(B,i)) = i ; fun pop (New) = New | pop (push(B,i)) = if not(mem(B,i)) then B else if top(B)=i then pop(B) else push( pop( push(pop(B),i) ), top(B) ) ; fun empty (New) = true | empty (B) = size(B)=0 ; (* add data points and test cases *)