title: Printing variant types in OCaml
body: <p>In my OCaml program, I spend considerable time wring "to_string" for variant types over and over again. Either I need them for debugging purpose, or because I need a specific formatted output. </p>
  
  <p>So far, they follow a template such as follows:</p>
  
  <pre><code>let rec to_string = function                                                                                                               
      | Var x -&gt; x                                                                 
      | Implies (f1, f2) -&gt; Printf.sprintf "(=&gt; %s %s)" (to_string f) (to_string f2)
      | And (f1, f2) -&gt; Printf.sprintf "(&amp; %s %s)" (to_string f1) (to_string f2)   
      | Or (f1, f2) -&gt; Printf.sprintf "(| %s %s)" (to_string f1) (to_string f2)    
      | Not (f) -&gt; Printf.sprintf "(~ %s)" (to_string f)                           
      | True -&gt; "#t"                                                               
      | False -&gt;"#f"        
  </code></pre>
  
  <p>I'm wondering if there are more convenient/conventional ways of than this, possibly with the latest development of the language. For instance, automatic generation of templates based on the type? generic printing functions that could be used for debugging?</p>
  
  <p>One way advocated in "Real World OCaml" is to use the Sexp module of the Core library that provides facilities for this very purpose. It seems to work well if you don't need much customization about how you print the values. I'd like to know if there are other/better options. </p>