This document provides an overview of the OCaml programming language. It covers basic topics such as functions, types, polymorphism, modules and signatures. It also discusses tools and recommended things to read to learn more about OCaml. The document is presented as a walkthrough by Romain Slootmaekers and contains code examples to illustrate OCaml concepts.
2024: Domino Containers - The Next Step. News from the Domino Container commu...
OCaml Walkthrough
1. What is ocaml?
basic programming
modules/signatures/functors
object orientation
tools
things to read
OCaml walkthrough
Romain Slootmaekers
June 24, 2011
Romain Slootmaekers OCaml walkthrough
2. What is ocaml?
basic programming
modules/signatures/functors
object orientation
tools
things to read
Table of Contents I
1 What is ocaml?
2 basic programming
functions
types
polymorphism
higher order functions
imperative features
labels/variants
3 modules/signatures/functors
modules and signatures
first class modules
functors
4 object orientation
Romain Slootmaekers OCaml walkthrough
3. What is ocaml?
basic programming
modules/signatures/functors
object orientation
tools
things to read
Table of Contents II
What is there?
ad hoc objects
basic OO
parametrization
ad hoc interfaces
5 tools
6 things to read
Romain Slootmaekers OCaml walkthrough
4. What is ocaml?
basic programming
modules/signatures/functors
object orientation
tools
things to read
What is OCaml?
ML variant
other ML variants:SML, F#, Moscow ML, Mythryl, . . .
OCaml variants:MetaOCaml, JoCaml, OCamljs, . . .
compiled/interpreted
byte-code interpreter ←→ native code (speed ≈ * 7)
type inference
# let x = 5;;
val x : int = 5
strict typing
# [ 3 ; ”XX ” ] ; ;
E r r o r : This e x p r e s s i o n has type s t r i n g but
an e x p r e s s i o n was e x p e c t e d o f t y p e i n t
Gc,OO,simple FFI, . . .
Romain Slootmaekers OCaml walkthrough
5. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
Trivial Examples(1)
let square x = x ∗ x
l e t rec fac n =
if n = 0 $ ocamlc -i trivial.ml
then 1 val square : int -> int
e l s e n ∗ f a c ( n−1)
val fac : int -> int
l e t rec fac2 = function val fac2 : int -> int
| 0 −> 1
| n −> n ∗ f a c ( n−1)
Romain Slootmaekers OCaml walkthrough
6. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
Trivial Examples(2)
l e t avg x y = ( x +. y ) / . 2 .
l e t ( ∗∗∗ ) = avg
$ ocamlc -i trivial2.ml
val avg : float -> float -> float
val ( *** ) : float -> float -> float
Romain Slootmaekers OCaml walkthrough
7. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
builtins
let l0 = 1 : : 2 : : [ ] ; ; $ ocamlc -i simple data.ml
let l1 = l0 @ [ 3 ; 4 ] ; ;
let l2 = [(1 ,1);(2 ,3)] ; ;
val l0 : int list
l e t s 0 = "A string " ; ; val l1 : int list
s 0 . [ 0 ] <− ’ a ’ ; ; val l2 : (int * int) list
let i = 42;; val s0 : string
l e t i 3 2 = 42 l ; ;
l e t i 6 4 = 42L ; ;
val i : int
l e t a0 = [ | 1 . 1 ; 2 . 2 | ] ; ; val i32 : int32
a0 . ( 0 ) <− 0 . 0 ; ; val i64 : int64
s0 ; ; val a0 : float array
Romain Slootmaekers OCaml walkthrough
8. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
build your own
type f l i s t =
| Empty
| F l i s t of ( f l o a t ∗ f l i s t )
let ( ˆ. ) a b = F l i s t (a , b)
l e t rec s i z e = function
| Empty −> 0
| Flist ( , s ) −> 1 + s i z e s
let () =
l e t f l = 3 . 0 ˆ . 2 . 0 ˆ . 1 . 0 ˆ . Empty i n
let s = s i z e f l in
P r i n t f . p r i n t f " the size is %in" s ; ;
Romain Slootmaekers OCaml walkthrough
9. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
records
type s u i t = | H e a r t s | Diamonds | C l u b s | S p a d e s
type r a n k = L o f i n t | J | Q | K | A
type c a r d = { s u i t : s u i t ;
rank : rank }
type hand = c a r d a r r a y
l e t make card s r = { s u i t = s ; rank = r }
l e t my hand = [ | m a k e card Hearts (L 2);
make card Diamonds ( L 3 ) ;
make card Clubs (L 4 ) ;
make card Spades ( L 5 ) ;
|]
Romain Slootmaekers OCaml walkthrough
10. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
polymorphic functions
l e t rec s i z e = function
| [ ] −> 0
| : : r e s t −> 1 + s i z e r e s t
let () =
P r i n t f . p r i n t f " s1 = %in; s2 = %in"
( size [1;2;3])
( size [ ’c ’; ’h ’; ’ a ’; ’ r ’ ] ) ; ;
$ ocamlc -i pfunc.ml
val size : ’a list -> int = <fun>
Romain Slootmaekers OCaml walkthrough
11. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
polymorphic functions(2)
l e t rec qsort = function
| [ ] −> [ ]
| p : : r e s t −>
let i s l e s s x = x < p in
let l , r = List . partition i s l e s s rest in
qsort l @ [ p ] @ qsort r
$ ocamlc -i pfunc2.ml
val qsort : ’a list -> ’a list = <fun>
Romain Slootmaekers OCaml walkthrough
12. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
polymorphic types
type ’ a b t r e e = | Empty | Node o f ’ a ∗ ’ a b t r e e ∗ ’ a b t r e e
let rec i n s e r t a = function
| Empty −> Node ( a , Empty , Empty )
| Node ( e , l , r ) as n when a = e −> n
| Node ( e , l , r ) when a < e −> Node ( e , i n s e r t a l , r )
| Node ( e , l , r ) −> Node ( e , l , i n s e r t a r )
l e t rec f r o m l i s t t r e e = function
| [ ] −> t r e e
| e : : e s −> f r o m l i s t ( i n s e r t e t r e e ) e s
l e t c t r e e = f r o m l i s t Empty [ ’ a ’ ; ’ l ’ ; ’ u ’ ; ’ m’ ; ’ i ’ ; ’ n ’ ; ’ u ’ ; ’ m ’ ]
l e t i t r e e = f r o m l i s t Empty [ 3 ; 4 ; 5 ; 1 2 ; 5 ; 6 ; 1 ; 3 ] ; ;
Romain Slootmaekers OCaml walkthrough
13. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
higher order functions
l e t rec f o l d l e f t f acc = function
| [ ] −> a c c
| x : : x s −> f o l d l e f t f ( f a c c x ) x s
let =
P r i n t f . p r i n t f " sum =% in"
( f o l d l e f t (+) 0 [ 1 ; 2 ; 3 ; 4 ; 5 ] ) ; ;
$ ocamlc -i ho.ml
val fold_left : (’a -> ’b -> ’a)
-> ’a -> ’b list -> ’a = <fun>
Romain Slootmaekers OCaml walkthrough
14. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
currying
open L i s t $ ocamlc -i curry.ml
l e t sum = f o l d l e f t (+) 0
val sum : int list -> int
l e t prod = f o l d l e f t ( ∗ ) 1 val prod : int list -> int
val len : ’a list -> int
val oops : ’_a list -> int
l e t len xs = f o l d l e f t
( fun a −> a+1) 0 x s
The type of this expression,
’_a list -> int,
l e t oops = f o l d l e f t contains type variables
( fun a −> a+1) 0 that cannot be generalized
Romain Slootmaekers OCaml walkthrough
15. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
for/while/references
let () =
f o r i = 0 to 5 do
P r i n t f . p r i n t f "i =% i " i
done ;
print newline (); $ ocaml for.ml
let j = r e f 0 in i=0 i=1 i=2 i=3 i=4 i=5
w h i l e ! j <= 5 do
j=0 j=1 j=2 j=3 j=4 j=5
P r i n t f . p r i n t f "j =% i " ! j ;
incr j
done ;
print newline ();;
Romain Slootmaekers OCaml walkthrough
16. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
labels
type p o i n t = { x : f l o a t ; y : f l o a t ; z : f l o a t }
l e t make point ˜x ˜y ˜z = {x ; y ; z}
l e t p2s p = P r i n t f . s p r i n t f
"{x =%.2 f;y =%.2 f;z =%.2 f}"
p.x p.y p.z
let () =
l e t a , b , c = 1 . 0 , 2 . 0 , 3 . 0 i n $ ocaml labels.ml
l e t p0 = m a k e p o i n t p0={x=1.00;y=2.00;z=3.00}
˜x : a ˜y : b ˜z : c
in
p1={x=4.00;y=5.00;z=6.00}
let x , y , z = 4.0 , 5.0 , 6.0 in
l e t p1 = m a k e p o i n t
˜x ˜y ˜z
in
P r i n t f . p r i n t f " p0 =% s np1 =% sn"
( p 2 s p0 ) ( p 2 s p1 ) ; ;
Romain Slootmaekers OCaml walkthrough
17. What is ocaml? functions
basic programming types
modules/signatures/functors polymorphism
object orientation higher order functions
tools imperative features
things to read labels/variants
variants
let f = function
| ‘On −> 1
| ‘ O f f −> 0
| ‘ Number n −> n
| −> −1;;
L i s t . map f [ ‘ On ; ‘ O f f ; ‘ Whatever ] ; ;
$ ocamlc -i variants.ml
val f : [> ‘Number of int | ‘Off | ‘On ] -> int
Romain Slootmaekers OCaml walkthrough
18. What is ocaml?
basic programming
modules and signatures
modules/signatures/functors
first class modules
object orientation
functors
tools
things to read
Example:lookup
l e t empty k = r a i s e N o t f o u n d
l e t lookup t k = t k
let insert t a b k =
if k = a
then b
else t k
$ ocamlc -i mod1.ml
val empty : ’a -> ’b
val lookup : (’a -> ’b) -> ’a -> ’b
val insert : (’a -> ’b) -> ’a -> ’b -> ’a -> ’b
Romain Slootmaekers OCaml walkthrough
19. What is ocaml?
basic programming
modules and signatures
modules/signatures/functors
first class modules
object orientation
functors
tools
things to read
in a module
module Lookup = s t r u c t
$ ocamlc -i mod2.ml
l e t empty k = r a i s e N o t f o u n d
l e t lookup t k = t k module Lookup :
sig
let insert t a b k = val empty : ’a -> ’b
if k = a
then b
val lookup : (’a -> ’b) -> ’
else t k -> ’b
end val insert : (’a -> ’b) -> ’
-> ’b
l e t t 0 = Lookup . empty ; ;
end
open Lookup val t0 : ’a -> ’b
l e t t 1 = i n s e r t t 0 5 " five " ; ; val t1 : int -> string
l e t t2 = lookup t0 5 ; ; val t2 : ’a
Romain Slootmaekers OCaml walkthrough
20. What is ocaml?
basic programming
modules and signatures
modules/signatures/functors
first class modules
object orientation
functors
tools
things to read
with signature
module L o o k u p f : s i g
type ( ’ a , ’ b ) t = ’ a −> ’ b
v a l empty : ( ’a , ’b) t
v a l l o o k u p : ( ’ a , ’ b ) t −> ’ a −> ’ b
v a l i n s e r t : ( ’ a , ’ b ) t −> ’ a −> ’ b −> ( ’ a , ’ b ) t
end = s t r u c t
type ( ’ a , ’ b ) t = ’ a −> ’ b
l e t empty k = r a i s e N o t f o u n d
l e t lookup t k = t k
let insert t a b k =
if k = a
then b
else t k
end
l e t t 0 = L o o k u p f . empty ; ;
l e t t 1 = L o o k u p f . i n s e r t t 0 5 " five " ; ;
l e t t2 = Lookup f . lookup t0 5 ; ;
Romain Slootmaekers OCaml walkthrough
21. What is ocaml?
basic programming
modules and signatures
modules/signatures/functors
first class modules
object orientation
functors
tools
things to read
multiple implementations
open Lookup
module type L = s i g
module F = ( s t r u c t
type t
type t = i n t −> s t r i n g
v a l empty : u n i t −> t
l e t empty k = r a i s e N o t f o u n d
val lookup :
l e t lookup t k = t k
t −> i n t −> s t r i n g
let insert t a b k =
val insert :
if k = a
t −> i n t −> s t r i n g −> t
then b
end
else t k
end : L )
Romain Slootmaekers OCaml walkthrough
22. What is ocaml?
basic programming
modules and signatures
modules/signatures/functors
first class modules
object orientation
functors
tools
things to read
multiple implementations(2)
module A = ( s t r u c t
type t = ( i n t ∗ s t r i n g ) l i s t
l e t empty ( ) = [ ]
l e t r e c l o o k u p t k = match t wi th
| [ ] −> r a i s e N o t f o u n d
| ( a , b ) : : r e s t −>
if k = a
then b
else lookup r e s t k
let insert t a b = (a , b ) : : t
end : Lookup . L )
Romain Slootmaekers OCaml walkthrough
23. What is ocaml?
basic programming
modules and signatures
modules/signatures/functors
first class modules
object orientation
functors
tools
things to read
multiple implementations(3)
module H = ( s t r u c t
type t = ( i n t , s t r i n g ) H a s h t b l . t
l e t empty ( ) = H a s h t b l . c r e a t e 17
l e t lookup t k = Hashtbl . f i n d t k
l e t i n s e r t t a b = H a s h t b l . add t a b ; t
let nobody will know () = ()
end : Lookup . L )
Romain Slootmaekers OCaml walkthrough
24. What is ocaml?
basic programming
modules and signatures
modules/signatures/functors
first class modules
object orientation
functors
tools
things to read
example usage
open L o o k u p f
open Lookup h
open L o o k u p a
l e t f 0 = F . empty ( ) ; ;
l e t f 1 = F . i n s e r t f 0 5 " five " ; ;
l e t fv = F . lookup f0 5 ; ;
l e t l 0 = A . empty ( ) ; ;
l e t l 1 = A . i n s e r t l 0 5 " five " ; ;
l e t l v = A. lookup l1 5 ; ;
l e t h0 = H . empty ( ) ; ;
l e t h1 = H . i n s e r t h0 5 " five " ; ;
l e t hv = H . l o o k u p h1 5 ; ;
Romain Slootmaekers OCaml walkthrough
25. What is ocaml?
basic programming
modules and signatures
modules/signatures/functors
first class modules
object orientation
functors
tools
things to read
watch out
open Lookup h
open L o o k u p a
l e t no way= H . empty ( ) ; ;
l e t o o p s = A . i n s e r t no way 5 ” f i v e ” ; ;
$ ocamlbuild liskov.byte
File "liskov.ml", line 5, characters 20-26:
Error: This expression has type Lookup_h.H.t
but an expression was expected of type
Lookup_a.A.t
Romain Slootmaekers OCaml walkthrough
26. What is ocaml?
basic programming
modules and signatures
modules/signatures/functors
first class modules
object orientation
functors
tools
things to read
example
module type DEVICE = s i g v a l i n i t : u n i t −> u n i t end
module PDF = s t r u c t l e t i n i t ( ) = ( ) end
module SVG = s t r u c t l e t i n i t ( ) = ( ) end
l e t d e v i c e s = H a s h t b l . c r e a t e 17
l e t ( ) =H a s h t b l . add d e v i c e s " PDF " ( module PDF : DEVICE )
l e t ( ) =H a s h t b l . add d e v i c e s " SVG " ( module SVG : DEVICE )
module D e v i c e =
( val (
t r y Hashtbl . f i n d d e v i c e s Sys . argv . ( 1 )
wi t h N o t f o u n d −> p r e r r e n d l i n e " Unknown device " ; e x i t 2 )
: DEVICE )
l e t () = Device . i n i t ( ) ; ;
Romain Slootmaekers OCaml walkthrough
27. What is ocaml?
basic programming
modules and signatures
modules/signatures/functors
first class modules
object orientation
functors
tools
things to read
Map example
module S t r i n g M a p = Map . Make ( S t r i n g )
open S t r i n g M a p
let () =
l e t s 0 = add "5" " five " empty i n
l e t v = f i n d "5" s 0 i n
P r i n t f . p r i n t f "v =% sn" v ; ;
Romain Slootmaekers OCaml walkthrough
28. What is ocaml?
basic programming
modules and signatures
modules/signatures/functors
first class modules
object orientation
functors
tools
things to read
ROY
module type E = s i g
type t l e t r e c add x s=
v a l compare : t −> t −> i n t f a i l w i t h " todo "
end
let r e c mem x = f u n c t i o n
module S e t = f u n c t o r ( E l t : E ) −> | Empty −> r a i s e N o t f o u n d
struct | Node ( e , , ) when e = x −>
type e l e m e n t = E l t . t | Node ( e , l , r ) −>
type t r e e = i f E l t . compare e x < 0
| Empty then mem x l
| Node o f ( e l e m e n t ∗ e l s e mem x r
tree ∗ tree ) end
l e t empty = Empty module S t r i n g S e t = S e t ( S t r i n g )
Romain Slootmaekers OCaml walkthrough
29. What is ocaml?
What is there?
basic programming
ad hoc objects
modules/signatures/functors
basic OO
object orientation
parametrization
tools
ad hoc interfaces
things to read
objects
classes
interfaces
initializers
(multiple) inheritance
public/private
parametrization
open types
Romain Slootmaekers OCaml walkthrough
30. What is ocaml?
What is there?
basic programming
ad hoc objects
modules/signatures/functors
basic OO
object orientation
parametrization
tools
ad hoc interfaces
things to read
immediate objects
l e t p =o b j e c t
v a l mutable x = 0
method g e t x = x
method move d = x <− x + d
end
$ ocamlc -i ad hoc.ml
val p : < get_x : int; move : int -> unit >
Romain Slootmaekers OCaml walkthrough
31. What is ocaml?
What is there?
basic programming
ad hoc objects
modules/signatures/functors
basic OO
object orientation
parametrization
tools
ad hoc interfaces
things to read
simple class with immutable objects
class point x y z =
l e t l = s q r t ( x ∗ . x +. y ∗ . y +. z ∗ . z ) i n
object ( s e l f )
method l e n g t h = l
method t o s t r i n g = $ ocamlc -i point class.ml
Printf . sprintf class point :
" {%.2 f ;%.2 f ;%.2 f}"
x y z
float ->
end float ->
float -> object
let () = method length : float
l e t p = new p o i n t 1 . 2 . 3 .
method to_string : string
in
Printf . printf end
"p =% s ;|| p || = %2. fn"
(p # to string )
(p # length ) ; ;
Romain Slootmaekers OCaml walkthrough
32. What is ocaml?
What is there?
basic programming
ad hoc objects
modules/signatures/functors
basic OO
object orientation
parametrization
tools
ad hoc interfaces
things to read
class types
c l a s s type p r i n t a b l e = o b j e c t
method t o s : u n i t −> s t r i n g
end
class point x y =
l e t p0 = new p o i n t 0. 0.
o b j e c t ( s e l f : #p r i n t a b l e )
l e t c0 = new c i r c l e p0 1 . 0
method t o s ( ) =
l e t o b j e c t s = [ p0 ; c0 ]
P r i n t f . s p r i n t f " {%.2 f ;%.2 f}"
let () =
x y
List . iter
end
( fun o −>
class circle p r =
l e t os = o # to s () in
o b j e c t ( s e l f : #p r i n t a b l e )
Printf . printf "%sn" o s )
method t o s ( ) =
objects ; ;
P r i n t f . s p r i n t f "C (%s ,%2. f)"
(p # to s ()) r
end
Romain Slootmaekers OCaml walkthrough
33. What is ocaml?
What is there?
basic programming
ad hoc objects
modules/signatures/functors
basic OO
object orientation
parametrization
tools
ad hoc interfaces
things to read
gotchas
calls on self during construction?
downcasts?
polymorphic methods?
polymorphic exceptions?
Romain Slootmaekers OCaml walkthrough
34. What is ocaml?
What is there?
basic programming
ad hoc objects
modules/signatures/functors
basic OO
object orientation
parametrization
tools
ad hoc interfaces
things to read
parametrization
class [ ’ a ] l i n k e d l i s t (a : ’ a) = object
v a l mutable n e x t = None
method s e t n e x t ( t a i l : ’ a l i n k e d l i s t ) =
n e x t <− Some t a i l
end
let () =
l e t l 0 = new l i n k e d l i s t "a" i n
l e t l 1 = new l i n k e d l i s t "b" i n
l0 # set next l1 ; ;
Romain Slootmaekers OCaml walkthrough
35. What is ocaml?
What is there?
basic programming
ad hoc objects
modules/signatures/functors
basic OO
object orientation
parametrization
tools
ad hoc interfaces
things to read
example
let log o o x =
l e t k s = l e t os = o # t o s t r i n g ( ) in
L w t l o g . debug ( o s ˆ": " ˆ s )
in
Printf . ksprintf k x
$ ocamlfind ocamlc -i -package lwt.unix log o.ml
val log_o :
< to_string : unit -> string; .. > ->
(’a, unit, string, unit Lwt.t) format4 -> ’a
Romain Slootmaekers OCaml walkthrough
36. What is ocaml?
basic programming
modules/signatures/functors
object orientation
tools
things to read
ocamlc, ocamlopt, ocamlc.opt, ocamlopt.opt
ocamlfind
ocamlbuild
emacs, eclipse, omlet, camelia, . . .
others
Romain Slootmaekers OCaml walkthrough
37. What is ocaml?
basic programming
modules/signatures/functors
object orientation
tools
things to read
ocamlfind
knows which packages you have
fills in details for the compilers
Example
$ ocamlopt EchoServer.ml
File "EchoServer.ml", line 1, characters 0-1:
Error: No implementations provided for
the following modules:
Unix referenced from EchoServer.cmx
$ ocamlopt.opt /.../lib/ocaml/unix.cmxa EchoServer.ml
$ ocamlfind
ocamlopt -package unix -linkpkg EchoServer.ml
Romain Slootmaekers OCaml walkthrough
38. What is ocaml?
basic programming
modules/signatures/functors
object orientation
tools
things to read
ocamlbuild
builds things
declarative
target extension
plugins for (c)libraries, documentation, profiler,...
tags
myocamlbuild.ml
Romain Slootmaekers OCaml walkthrough
39. What is ocaml?
basic programming
modules/signatures/functors
object orientation
tools
things to read
IDEs
emacs: tuareg
eclipse: ocaide http://www.algo-prog.info/ocaide/
camelia http://camelia.sourceforge.net/
vim: omlet
. . . lots of dead projects
Romain Slootmaekers OCaml walkthrough
40. What is ocaml?
basic programming
modules/signatures/functors
object orientation
tools
things to read
developing applications with OCaml
http://caml.inria.fr/pub/docs/oreilly-book/
The Objective Caml system http:
//caml.inria.fr/pub/docs/manual-ocaml/index.html
Jane street blog
http://ocaml.janestreet.com/?q=node/89
Lambda The Ultimate http://lambda-the-ultimate.org/
Romain Slootmaekers OCaml walkthrough
41. What is ocaml?
basic programming
modules/signatures/functors
object orientation
tools
things to read
advanced stuff
. . . Next time
Romain Slootmaekers OCaml walkthrough