Repost- Simple top-down development in OCaml

date

https://blog.janestreet.com/simple-top-down-development-in-ocaml/

Appoarch 1: the traditional / regular one
(* stack.mli *)
type 'a t
val empty : 'a t
val push : 'a t -> 'a -> 'a t
val pop : 'a t -> ('a t * 'a) option

(* stack.ml *)
type 'a t
let empty = failwith "unimplemented"
let push = failwith "unimplemented"
let pop = failwith "unimplemented"

nice, it compiles. but still need to fill in the stubs. and we need to change both files to keep them in sync when interface changes.

Appoarch 2: the proposed one
(* stack_intf.ml *)
module type S = sig
  type 'a t
  val empty : 'a t
  val push : 'a t -> 'a -> 'a t
  val pop : 'a t -> ('a t * 'a) option
end

(* stack.mli *)
include Stack_intf.S

(* stack.ml *)
include (val (failwith "unimplemented") : Stack_intf.S)

One more file, but both both the stack.mli and stack.ml will take on the right module types automatically, without the need to add explicit stubs for type or value definitions.

Once done the initial developments, you can paste the module type into stack.mli, remove stack_intf.ml, and start filling in stack.ml with real definitions.