(**************************************************************************) (* *) (* Thibaut Balabonski, Sylvain Conchon, Jean-Christophe Filliâtre, *) (* Kim Nguyen, Laurent Sartre *) (* *) (* Informatique - MP2I/MPI - CPGE 1re et 2e années. *) (* Cours et exercices corrigés. Éditions Ellipses, 2022. *) (* *) (* https://www.informatique-mpi.fr/ *) (* *) (**************************************************************************) type 'a stack = 'a list ref let create () : 'a stack = ref [] let is_empty (st: 'a stack) : bool = !st = [] let push (st: 'a stack) (x: 'a) : unit = st := x :: !st let top (st: 'a stack) : 'a = match !st with | [] -> invalid_arg "top" | x :: _ -> x let pop (st: 'a stack) : 'a = match !st with | [] -> invalid_arg "pop" | x :: l -> st := l; x