Steve
Steve

Reputation: 729

Confusion with lacaml type signature

I'm currently looking at this document and trying to identify the type signature of pp_fvec. Basically, I am trying to print a vector but it is constantly telling me I do not have the right types.

http://mmottl.github.io/lacaml/api/Lacaml_io.html

The type signature I'm interested in using is:

val pp_fvec : (float, 'elt) pp_vec

Which does something, and then calls pp_vec which has the signature:

type ('el, 'elt) pp_vec = Format.formatter ->
   ('el, 'elt, Bigarray.fortran_layout) Bigarray.Array1.t -> unit 

What is going on here? What does

(float, 'elt) pp_vec 

actually mean? A follow up question, how do I use this? What is the best way to look up and understand OCaml documentation? Tutorials, comments, and explanations seem really sparse at best for most libraries. I hate to keep asking these questions, but the documentation on a lot of this is really bad.

Thanks for your help!

Upvotes: 2

Views: 115

Answers (1)

ivg
ivg

Reputation: 35280

There is an %a specifier in a format strings, that allows you to print value of any type. In general, any specifier in a printf-family format string require you to add zero or more arguments of particular type after this string, e.g.,

 printf "%d"

requires one argument of type int.

The %a specifier asks for two arguments, the first argument is the so named printer, and the second argument is the printed value itself. The printer is a function of type 'output_channel_type -> 'a -> unit and the value must have a type of 'a, i.e., they must match. For example, if you have a float vector with fortran layout of type ('el, 'elt, Bigarray.fortran_layout) Bigarray.Array1.t named vec, you can use pp_fvec function to output it, e.g.,

 Format.printf "vec = %a" pp_fvec vec

Let me also clarify the meaning of 'output_channel_type, I've left this to be a type variable, but usually it is concrete and is equal to the type of the channel to which particular function can write. For example, %a specifier in Printf.printf function requires you to provide a function that prints to out_channel, the same specifier for Format.printf asks for a function that prints into the output channel of type Format.formatter, etc. In general this type must be equal to the second type parameter of the format type of the formatting string.

So, to summarize, this pp* family of functions are used generally as a printers for the %a format specifier for the Format family of functions. This type of printers is also used to print into toplevel or ocaml debugger (and can be installed with install_printer directives). Of course, it can be used in a standalone mode, without a printf function, like this:

 open Format
 pp_fvec std_formatter vec

That is roughly equivalent to the example above.

In the answer to the followup question, all OCaml libraries have a killing feature of being already self-documented with mli files. The best way to introduce yourself to some library is to use your favorite editor and read mli files of this library. With the development of modern tools like ocamlmerlin and ocp-index this is much more easier. You can just jump to the definition. You can find where particular library installs its mli files by opam config var library_name:lib command, where library_name is the name of the library.

Upvotes: 2

Related Questions