Skip to content

Instantly share code, notes, and snippets.

@ramntry
Created April 3, 2014 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramntry/9961573 to your computer and use it in GitHub Desktop.
Save ramntry/9961573 to your computer and use it in GitHub Desktop.
Polymorphic variants expansion power example
type 'e expr = [
| `Var of string
| `Ctr of string * 'e list
]
let string_of_expr string_of_e = function
| `Var vname -> vname
| `Ctr (cname, args) ->
cname ^ "(" ^ String.concat ", " (List.map string_of_e args) ^ ")"
let rec string_of_pure expr = string_of_expr string_of_pure expr
let test_pure () =
let pure = `Ctr ("S", [`Var "x"]) in
print_endline (string_of_pure pure)
(* 10 years passed *)
let string_of_if string_of_e = function
| `If (cond, t, f) ->
"if " ^ string_of_e cond ^
" then " ^ string_of_e t ^
" else " ^ string_of_e f
| #expr as e -> string_of_expr string_of_e e
let rec string_of_if_expr expr = string_of_if string_of_if_expr expr
let test_if_expr () =
let if_expr = `Ctr ("S", [`If (`Var "c", `Var "t", `Var "f")]) in
print_endline (string_of_if_expr if_expr)
let () =
test_pure ();
test_if_expr ()
@ramntry
Copy link
Author

ramntry commented Apr 3, 2014

S(x)
S(if c then t else f)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment