moduletypeStackSig = sig type'a stack exceptionEmpty val empty : 'a stack val is_empty : 'a stack -> bool val push : 'a -> 'a stack -> 'a stack val peek : 'a stack -> 'a val pop : 'a stack -> 'a stack val size : 'a stack -> int end
ListStack Impl
1 2 3 4 5 6 7 8 9 10
moduleListStack : StackSig = struct type'a stack = 'alist exceptionEmpty let empty = [] let is_empty = function[] -> true | _ -> false let push x s = x :: s let peek = function[] -> raise Empty | x :: _ -> x let pop = function[] -> raise Empty | _ :: s -> s let size = List.length end
Since the StackSig signature only exposes type 'a stack, even though in ListStack internally uses 'a list as underlying data structure, external users can only see 'a stack and 'a list is hidden from users. Thus we can accept the first and reject the second, which provides safety from explicit construction.