Created
April 3, 2014 19:48
-
-
Save ramntry/9961573 to your computer and use it in GitHub Desktop.
Polymorphic variants expansion power example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
S(x)
S(if c then t else f)