Q:

Typing in Module Printf

In the document of Module Printf, I don't understand the mechanism of ('a, out_channel, unit) format, thought I use that a lot in practice.
For instance, the following functions type well while compiling:
type t =
  { x: int;
    y: int }

let print (chan: out_channel) (co: t) : unit =
  Printf.fprintf chan "(%d, %d)" x y

let try (co0: t) (co1: t) =
  Printf.fprintf Pervasives.stdout "From %a To %a" print co0 print co1

The signature of Printf.fprintf is : out_channel -> ('a, out_channel, unit) format -> 'a, but I don't see what ('a, out_channel, unit) format matches in "From %a To %a" print co0 print co1.
Also, the signature of print is out_channel -> t -> unit, why is print co0 print co1 acceptable in the code?
In one word, could anyone explain how Printf.fprintf Pervasives.stdout "From %a To %a" print co0 print co1 is typed?

A:

OCaml has a typing hack around format strings. This is weird but useful for its easy and type-safe printf functions.
Usually, string literals are typed as strings:
# "(%d, %d)";;
- : string = "(%d, %d)"

But they are NOT if they are with a "formatting" type context:
# ( "(%d, %d)" : (_,_,_) format );;
- : (int -> int -> 'a, 'b, 'a) format = <abstr>

OCaml type checker looks at the literal as a format string then uses a special typing rule for it: it finds two %d there, and give a type (int -> int -> 'a, 'b, 'a) format which means it is a format string which can take 2 integers and do something.
Using string literals with Printf functions gives the same "format" typing context so "(%d, %d)" is typed not as a mere string but this format type:
# Printf.fprintf stdout "(%d, %d)";;
- : int -> int -> unit = <fun>

(t1, t2, t3) format roughly has the following meaning:

t1 is for how the format string behaves as a function: "%d" should have "int -> t" for some type t in this t1 part since it takes an integer and do something (mainly printing).
t2 is the type for the channel
t3 is the final result type when all the format parameters are given.

You can verify it as follows:
# (fun x -> Printf.fprintf stdout x, x) "(%d, %d)";;
- : (int -> int -> unit) * (int -> int -> unit, out_channel, unit) format =
(<fun>, <abstr>)

"(%d, %d)" is used here to take 2 integer parameters and it is used to send a string to stdout whose type is out_channel, then finally it returns unit.
Actually format type is just an alias of format6 with 6 type parameters and these 3 more type parameters are used for other format string typing hacks. But we do not go futher from here.
So now for "%a".
# ( "%a" : (_, out_channel, unit) format);;
- : ((out_channel -> 'a -> unit) -> 'a -> unit, out_channel, unit) format = <abstr>

This tells you Printf.fprintf stdout "%a" takes two parameters. The one is a function of type out_channel -> 'a -> unit and the other is 'a. 
If you see this type then it is easy to see Printf.fprintf stdout "%a" print co0 is well-typed. Note that it is NOT Printf.fprintf stdout "%a" (print co0) (this is what I wrongly read when I saw "%a" at the first time years ago.)
Printf.fprintf stdout "%a" print co0 tries to print co0 with the printer print. The channel for this sub-printing is of course stdout which is given to Printf.fprintf.

