Question: Please complete the indicated module in OCaml! Thank you! module type STACK = sig (** ['a t] is the type of stacks with element type
Please complete the indicated module in OCaml! Thank you!
module type STACK = sig
(** ['a t] is the type of stacks with element type ['a]. *)
type 'a t
(** [empty] is an empty stack. *)
val empty : 'a t
(** [is_empty] tests is an empty stack. *)
val is_empty : 'a t -> bool
(** [push x s] pushes an element [x] on top of
the stack [s] and returns the new stack. *)
val push : 'a -> 'a t -> 'a t
(** [peek s] peeks the top element of the stack [s].
If the stack is empty, [None] is returned. *)
val peek : 'a t -> 'a option
(** [pop s] pops the top element of the stack [s]
and returns the resulting stack. If the stack
was already empty, [None] is returned. *)
val pop : 'a t -> 'a t option
(** [size s] returns the number of elements in [s]. *)
val size : 'a t -> int
end
(** The type of recipes to build a stack. *)
type 'a build =
| Empty
(** [Empty] means calling [empty] to get a stack. *)
| Push of 'a * 'a build
(** [Push (x, b)] means building a stack [s] using [b],
and then calling [push x s] to get a stack. *)
| Pop of 'a build
(** [Pop b] means building a stack [s] using [b],
and then calling [pop s] to get a stack.
If [pop s] returns [None], the building fails
and an exception should be raised. +*)
(** The type of checks on a stack. *)
type 'a check =
| IsEmpty of bool
(** [IsEmpty b] means that calling [is_empty] on the
stack should get the answer [b]. *)
| Peek of ('a -> bool) option
(** [Peek None] means that calling [peek] on the stack
should get [None].
[Peek (Some p)] means that calling [peek] on the stack
should get [Some x] and [p x] should be true. *)
| Pop of unit option
(** [Pop None] means that calling [pop] on the stack
should get [None].
[Pop (Some ())] means that calling [pop] on the stack
should get [Some s] for some stack [s]. *)
| Size of int
(** [Size i] means that calling [size] on the stack
should get the answer [i]. *)
(** A functor to check the correctness
of an implementation of [STACK]. *)
module StackTester (Stack : STACK) :
sig
(** The exception that indicates that the building fails. *)
exception BuildFailure
(** [build b] builds a stack using the recipe [b].
For example, [build Empty] will give the stack
[Stack.empty].
@raise BuildFailure if the building fails
due to [Stack.pop] returning [None]. *)
val build : 'a build -> 'a Stack.t
(** [test s c] tests whether the stack [s] satisfies
the check [c].
For example, [test s (Size 10)] returns [true]
if and only if [Stack.size s] returns [10]. *)
val test : 'a Stack.t -> 'a check -> bool
end
=
struct
(* Complete this module. *)
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
