Use a built-in pretty printer for debugging?

As I understand it, due to the design and basic philosophy of OCaml, there is no general purpose print function that can be used to print arbitrary data structures such as this

[([2; 1; 0], 1.); ([2; 1], 0.471206873564138595); ([2; 0], 0.467882609464025379)]

      

for debugging purposes. A function that can handle every data structure won't have an input type in OCaml, which doesn't make any sense.

I can write a small function to print any data structure that I can make from standard data structures - lists, tuples, numbers, etc. (as shown in the picture here and here ) and then I have to write another little function for another data structure and so on. This is a lot of work if you just want debug output.

However, utop and ocaml use a built-in pretty-print routine to display any type of evaluation results and display the contents of lists, tuples, etc. Is there a way to access this functionality - i.e. to access the P part of the REPL? In fact, I made a copy of the mouse from the utop terminal output to build the string I used to display the list above. Can't I do this from my code?

I agree that if possible, it would actually be a Bad Thing at all, and that it should only be used for debugging and other simple purposes. To use a general purpose wide format function would disrupt the type system, which is one of the reasons for using OCaml in the first place.

(I'm sure the answer is “No, you can't do this,” or I would have stumbled upon a way to do it. Why though? Seems simple enough.)

+3


source to share


1 answer


The toplex and utop print functions have access to type information. A regular OCaml executable (bytecode or native) has no type information (talk about it). So you can't just plug in a top-level or top-level print function.



This is a fairly frequently asked question (because it will really be useful for debugging). This previous SO answer has a little more information: How can OCaml values ​​be printed outside the top level?

+2


source







All Articles