Functional Patterns: State, Laziness, Timing, and Debugging
OCaml gives you a lot for free, but a few patterns that other functional languages build in you write yourself. That is a feature: the pieces are small, and seeing them in full teaches you how they work. This chapter builds three of them: a State module for threading state through pure code (then the same thing again with OCaml 5 effect handlers), lazy sequences for streams you compute on demand, and a timeout that can cut off a long computation. Along the way you get a trace helper for debugging and a small benchmark timer.
A State Module
Some functional languages ship a State monad that carries a value through a chain of steps without a mutable variable. OCaml has no such built-in, so source-code/state_monad/state1.ml defines one. The whole module is under twenty lines:
1 module State = struct
2 type ('s, 'a) t = 's -> 'a * 's
3
4 let return x s = (x, s)
5 let bind m f s =
6 let a, s' = m s in
7 (f a) s'
8
9 (* let* enables do-notation style sequencing *)
10 let ( let* ) = bind
11 let ( >>= ) = bind
12 let ( >> ) m1 m2 = bind m1 (fun _ -> m2)
13
14 let get s = (s, s)
15 let put n _ = ((), n)
16
17 let run_state (m : ('s, 'a) t) s = m s
18 let eval_state (m : ('s, 'a) t) s = fst (m s)
19 let exec_state (m : ('s, 'a) t) s = snd (m s)
20 let map_state f (m : ('s, 'a) t) s = f (m s)
21 end
The key line is the type. A stateful computation of type ('s, 'a) t is a function from a starting state to a pair of a result and a new state. Nothing runs until you hand it a start state.
return xyieldsxand leaves the state alone.bind m frunsm, takes its resultaand new states', then runsf afroms'. It chains two steps so the state flows from the first into the second.getreads the current state as the result.put nthrows the old state away and installsn.run_stateruns a computation and returns both the value and the final state.eval_statekeeps only the value;exec_statekeeps only the state.
do-notation with let*
OCaml lets you name a binding operator let*. When you write let* x = m in ..., the compiler rewrites it to bind m (fun x -> ...). That gives you clean sequencing without a pile of nested functions:
1 open State
2
3 (* Increment the state, returning the old value. do-notation style. *)
4 let increment_state =
5 let* n = get in
6 let* () = put (n + 1) in
7 return n
Read it top to bottom: get the state into n, put n + 1 back, then return the old n. The state threads through for you. The same computation with the raw operators is shorter but harder to read:
1 let increment_state2 = get >>= fun a -> put (a + 1) >> return a
Both forms are the same function. map_state transforms the whole (value, state) pair at the end:
1 let bump_vals (a, b) = (a + 1, b + 2)
Running the demos gives:
1 (1,2)
2 (1,2)
3 (2,4)
4 1
5 2
The first two lines are the two increment_state versions: start at 1, return the old value 1, leave state 2. The third applies bump_vals to (1, 2) to get (2, 4). The last two show eval_state (the value, 1) and exec_state (the state, 2). This State module is enough for any place you want to pass state through pure code without a global.
The Same State with Effect Handlers
The State module is the classic functional answer, and it works in any version of OCaml. OCaml 5 adds a second answer: effect handlers. An effect is like an exception that can be resumed. Code performs an effect to ask a question, a handler further up the call stack answers it, and the code carries on from where it stopped with the answer in hand. State is the standard first example, so source-code/state_monad/state_effects.ml rebuilds the previous section’s program with effects.
First declare the effects. Effect.t is an extensible type, so you add constructors to it the way you would add exceptions. The type parameter is the type of the answer: Get is answered with a state, Put with unit. The functor lets one definition work for any state type:
1 module State (S : sig type t end) = struct
2 type _ Effect.t += Get : S.t Effect.t | Put : S.t -> unit Effect.t
3
4 let get () = Effect.perform Get
5 let put n = Effect.perform (Put n)
Now get and put are ordinary functions. They perform an effect and return its answer. The handler that supplies the answers is run_state:
1 let run_state (f : unit -> 'a) (init : S.t) : 'a * S.t =
2 let step =
3 match f () with
4 | v -> fun s -> (v, s)
5 | effect Get, k -> fun (s : S.t) -> Effect.Deep.continue k s s
6 | effect (Put s'), k -> fun _ -> Effect.Deep.continue k () s'
7 in
8 step init
A match with effect cases, new syntax in OCaml 5.3, runs f () and installs a handler around it. The first case handles a normal return. Each effect case runs when f performs that effect, and it receives k, the continuation: the rest of f’s computation, paused at the perform. Effect.Deep.continue k x resumes it with x as the answer.
The handler reuses the monad’s idea: every case returns a function from a state to a (result, state) pair, which is the monad’s ('s, 'a) t type, so the state is threaded through without a mutable variable:
- On a normal return with value
v, the final step isfun s -> (v, s). - On
Get, the step receives the current states, resumes the computation withsasget’s answer, and passesson unchanged to whatever comes next. That is whysappears twice. - On
Put s', the step ignores the old state, resumes with(), and passes the new states'along.
These are deep handlers: when continue k resumes the computation, the same handler is still installed around it, so the next get or put lands back in run_state. The (s : S.t) annotation is needed because inside an effect case the type checker knows the answer type only locally, and the annotation tells it which state type the step takes. eval_state, exec_state, and map_state are one-liners on top of run_state, as before.
The gain is in how you write stateful code. Here is increment_state again:
1 module IntState = State (struct type t = int end)
2 open IntState
3
4 let increment_state () =
5 let n = get () in
6 put (n + 1);
7 n
There is no let*, no return, and no special type. It is plain sequential OCaml, and it gives the same results as the monadic version:
1 (1,2)
2 (2,4)
3 1
4 2
The bigger difference shows up when stateful code meets ordinary library functions. List.iter knows nothing about state, but with effects its callback can still use get and put:
1 let count_long_words words =
2 List.iter (fun w -> if String.length w > 3 then put (get () + 1)) words
Running exec_state (fun () -> count_long_words [...]) 0 on five words prints long words: 3. With the monad you cannot pass a stateful step to List.iter, because List.iter expects a function returning unit, not a ('s, unit) t. You would have to write a monadic iter first, and a monadic version of every other higher-order function you want to use. Effect handlers exist to solve this problem: monadic types that spread through a code base.
So which should you use? The comparison comes down to a few points:
- Types. The monad puts state in the type, so
('s, 'a) ttells a reader the function is stateful. With effects,increment_statehas typeunit -> int, and nothing in the type says it needs a handler. If you call it without one, OCaml raisesEffect.Unhandledat run time. - Composition. Effects compose with existing code, as
List.itershowed. The monad needs its own version of each combinator. - Portability. The monad is plain functions and works on any OCaml. Effects need OCaml 5, and the
effectpattern syntax needs 5.3 or later.
For a small, self-contained computation the monad is clear and explicit. When state, or another effect such as logging, a generator, or cooperative scheduling, has to pass through code you did not write, effect handlers are the OCaml 5 way to do it. Concurrency libraries such as Eio use them to implement lightweight threads.
Run both versions from the project directory:
1 cd source-code/state_monad
2 dune exec ./state1.exe
3 dune exec ./state_effects.exe
Lazy Sequences and Trace Debugging
The next two examples share a tool: OCaml’s Seq, a lazy sequence you compute one element at a time. A Seq.t is a function you call to get the next element, so an “infinite” sequence like Seq.ints 2 (2, 3, 4, …) is fine: you only ever force the part you take.
The file source-code/debugging/trace_timer.ml uses a lazy prime sieve to show how to print debug output from otherwise pure code. First the trace helper. Other functional languages have a built-in trace; here it is one line:
1 let trace msg x = Printf.eprintf "%s\n%!" msg; x
It prints msg to stderr and returns x unchanged, so you can drop it into any expression without changing the result. The %! flushes stderr right away, so trace lines never sit in a buffer past the moment they fire.
The sieve of Eratosthenes builds primes lazily:
1 let rec sieve seq () =
2 match seq () with
3 | Seq.Nil -> Seq.Nil
4 | Seq.Cons (p, rest) ->
5 Printf.eprintf " -- inside sieve recursion\n%!";
6 Seq.Cons (p, sieve (Seq.filter (fun y -> y mod p <> 0) rest))
A Seq.t is a function of unit, so sieve seq () returns either Seq.Nil (empty) or Seq.Cons (p, rest) (a head p and a tail rest). The sieve keeps the head prime p and filters every later multiple of p out of the tail. Because the tail is lazy, no work happens until something asks for the next prime.
That “asks for the next” is the point of the example. OCaml is strict, so a plain trace msg x would compute x before printing msg. Building the primes as a lazy Seq restores the “trace when the value is used” order:
1 let any_calculation_will_do' n =
2 trace (Printf.sprintf " -- sieve n: %d" n) (sieve (Seq.ints 2))
3 |> Seq.take n
4 |> List.of_seq
Seq.take n forces exactly the first n primes, and List.of_seq collects them. Run it:
1 cd source-code/debugging
2 dune exec ./trace_timer.exe
The trace lines go to stderr and the final list, [2; 3; 5; 7; 11], goes to stdout. Because stderr and stdout are separate, you can keep the debug chatter and still pipe the real output somewhere clean.
Timing and Timeouts
The file source-code/timers/timer_test.ml reuses the same lazy sieve and adds two tools every performance study needs: a benchmark timer and a hard timeout.
The timer wraps a function, prints how long it took, and returns its value:
1 let time_it label f =
2 let t0 = Unix.gettimeofday () in
3 let v = f () in
4 Printf.printf "%s: %.6f s\n%!" label (Unix.gettimeofday () -. t0);
5 v
Unix.gettimeofday returns wall-clock seconds as a float. Because time_it returns v, you can wrap any expression in it without changing what the program computes.
The timeout is the interesting part. It runs f () but gives up with None if the work runs past a deadline:
1 exception Timeout
2
3 let timeout seconds f =
4 let old = Sys.signal Sys.sigalrm (Sys.Signal_handle (fun _ -> raise Timeout)) in
5 let disarm () =
6 ignore (Unix.setitimer Unix.ITIMER_REAL
7 { Unix.it_interval = 0.; it_value = 0. });
8 Sys.set_signal Sys.sigalrm old
9 in
10 ignore (Unix.setitimer Unix.ITIMER_REAL
11 { Unix.it_interval = 0.; it_value = seconds });
12 match f () with
13 | v -> disarm (); Some v
14 | exception Timeout -> disarm (); None
Here is how it works. Unix.setitimer arms an interval timer that fires the SIGALRM signal after seconds. The signal handler raises the Timeout exception. The match f () with form catches both a normal result and that exception: a normal value gives Some v, and Timeout gives None. Either way disarm cancels the timer and restores the old signal handler, so the timeout leaves no trace behind.
One detail makes this work. OCaml delivers signals at memory allocation points, so a loop that allocates (like the sieve building lists) can be cut off mid-run. A tight loop that never allocates could run past the deadline, but the allocating computations in this book are all interruptible.
The demo prints how long the prime runs take, then tries the sieve under a 0.1 second limit for growing sizes:
1 List.iter
2 (fun n ->
3 report_timeout (Printf.sprintf "primes %d" n)
4 (timeout 0.1 (fun () -> ignore (last (any_calculation_will_do n)))))
5 [ 4; 40; 400; 4000; 40000 ]
Small sizes finish in time; the large ones time out and report so. Run it with dune exec ./timer_test.exe. You now have three reusable tools: a State module, lazy Seq streams, and a timeout, each built from a handful of lines of standard OCaml.