Re: printing

"Gabriel Scherer [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAPFanBFOu=pGV33f6FGHMcBN3VYruPyGoosdkQRNwz3UnqNKKw@mail.gmail.com>
No, that is not currently possible. The toplevel has access to typing
information that ordinary OCaml don't, and that would be necessary to
print values in the same way (there is work on that being done, such
as the 2012-2013 work of Grégoire Henry and Pierre Chambart on dynamic
type representations, and even more recent work by Frédéric Bour, Leo
White and Jeremy Yallop on modular implicits).

(Ordinary OCaml programs have access to the runtime representation of
values, which has much less information but still enough to do a very
tiny bit of hackish value dumping -- see the (Std.dump : 'a -> string)
value of Extlib, BatPervasives.dump in Batteries. I do not recommend
its usage as it requires the user to be familiar with runtime value
representation.)

A last option is to have the printing code automatically generated
from the type definition. This capability is proposed by Sexplib for
example ( https://github.com/janestreet/sexplib ), or pa_deriving (for
Camlp4) or ppx-deriving ( https://github.com/whitequark/ppx_deriving
). This might be the best approach to avoid available today.

If you decide to write your own printing functions (flexible and
reliable), it is actually not that hard using already-existing
combinators. For example Batteries exports printing functions for each
of its data structures, parametrized by a printer for the element(s)
type(s), so writing a printer for your more complex types is mostly a
matter of composing together the right combinators -- with some
boilerplate for each variant and record type.

On Fri, Sep 25, 2015 at 9:39 PM, Hendrik Boom [email protected]
[ocaml_beginners] <[email protected]> wrote:
> When I run ocaml interactively, it prints me the values of expressions I
> type, using its own conventions as to formatting.
>
> Presumably the intepreter has a function that does that printing.
>
> Is it possible to call that function from OCaml code?  That would be
> useful diring debugging.  Otherwise I'd have to define custom printing
> functions for every type I might have to writnt during debugging.  Those
> functinos might not be called any other time, and the interpreter's
> print routine might be quite sufficient.
>
> Not to mention not haveing to worry whether the bug is in the code I'm
> debuging or in my print routine.
>
> -- hendrik
>
>
> ------------------------------------
> Posted by: Hendrik Boom <[email protected]>
> ------------------------------------
>
> Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
> The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames etc.
> ------------------------------------
>
> Yahoo Groups Links
>
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.