Printing variant types in OCaml
问题 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. So far, they follow a template such as follows: let rec to_string = function | Var x -> x | Implies (f1, f2) -> Printf.sprintf "(=> %s %s)" (to_string f) (to_string f2) | And (f1, f2) -> Printf.sprintf "(& %s %s)" (to_string f1) (to_string f2) | Or (f1, f2) -> Printf.sprintf "(| %s %s)" (to_string f1)