OCaml By Examples

Language Basics

Project Management

  • dune
  • dune-release
  • opam pin

Advanced Ocaml

  • functors
  • monads

Libraries

Modules

add.ml

We can split code in different files, which OCaml will understand as different **modules**.
let add_one i = i + 1

main.ml

open Core
Other files (modules) will be able to use another module's code by referring to its filename with an uppercase.
let () = 
  let two = Add.add_one 1 in
  printf "two: %d\n" two
Modules can also appear within a file.
module Thing = struct
  let add_two i = i + 2
end

let () = 
  let three = Thing.add_two 1 in
  printf "three: %d\n" three
A module **signature** can also be defined.
module BiggerThing : sig
  val add_three : int -> int
end = struct
  let add_three i = i + 3
end

let () = 
  let four = BiggerThing.add_three 1 in
  printf "four: %d\n" four
A module can also re-use other modules by either **including** a whole module or re-using parts of a module.
module WrappingThing : sig
  val add_one : int -> int
  val add_three : int -> int
  val add_four : int -> int
end = struct
by either **including** a whole module or re-using parts of a module.
  include Add
  let add_three = BiggerThing.add_three
or re-using parts of a module.
  open BiggerThing
  let add_four i = add_one @@ add_three @@ i
end

let () = 
  let five = WrappingThing.add_four 1 in
  printf "five: %d\n" five

module Nested = struct
  module NestedNested = struct
    let add_five i = i + 5
  end

  let add_six i = 1 + NestedNested.add_five i
end

let () = 
  let six = Nested.NestedNested.add_five 1 in
  let seven = Nested.add_six 1 in
  printf "six: %d\n" six;
  printf "seven: %d\n" seven;

module type SomeSignature = sig
  val i : int
end

module SomeModule1 : SomeSignature = struct
  let i = 1
end

module SomeModule2 = struct let i = 2 end

let () =
  let instance1 = (module SomeModule1 : SomeSignature) in
  let modules = [ instance1 ; (module SomeModule2) ] in
  let modules = modules @ [(module struct let i = 3 end)] in
  let module NNN = (val instance1 : SomeSignature) in 
  let instance2 = match List.last modules with Some x -> x | None -> failwith "impossible" in
  let module VVV = (val instance2 : SomeSignature) in
    printf "%d - %d\n" NNN.i VVV.i

console

$ dune exec ./main.exe
two: 2               
three: 3
four: 4
five: 5
six: 6
seven: 7
next: Error Handling