Skip to content

Instantly share code, notes, and snippets.

@tizoc
Created February 11, 2014 13:30
Show Gist options
  • Select an option

  • Save tizoc/8934825 to your computer and use it in GitHub Desktop.

Select an option

Save tizoc/8934825 to your computer and use it in GitHub Desktop.
type _ trepr =
| Int : int trepr
| Bool : bool trepr
| List : 'a trepr -> 'a list trepr
let bool_to_string = function
| true -> "True"
| false -> "False"
let int_to_string = Printf.sprintf "%d"
let strings_to_string strings =
Printf.sprintf "[%s]" (String.concat "; " strings)
let show : type a. a trepr -> a -> string = fun _ x ->
failwith "failed overloading resolution"
let show : type a. a trepr -> a -> string = function
| Bool -> bool_to_string
| trepr -> show trepr
let show : type a. a trepr -> a -> string = function
| Int -> int_to_string
| trepr -> show trepr
let show : type a. a trepr -> a -> string = function
| List trepr -> fun x -> strings_to_string (List.map (show trepr) x)
| trepr -> show trepr
let test_show : string =
show Bool true
let print : 'a trepr -> 'a -> unit =
fun trepr x -> print_endline (show trepr x)
let print_ints : unit =
print (List Int) [1;2;3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment