-
-
Save co-dan/4e19be3843a7a0ff4515eaa1ad176df8 to your computer and use it in GitHub Desktop.
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 bin_op = Plus | Minus | |
type value = Int of int | |
| Unit | |
type exp = | |
| Val of value | |
| Out of value | |
| BinOp of bin_op*value*value | |
let value_fmt : value Fmt.t = fun pp v -> | |
match v with | |
| Int i -> Fmt.int pp i | |
| Unit -> Fmt.string pp "()" | |
let bin_op_fmt : bin_op Fmt.t = fun pp op -> | |
match op with | |
| Plus -> Fmt.char pp '+' | |
| Minus -> Fmt.char pp '-' | |
let exp_fmt : exp Fmt.t = fun pp e -> | |
match e with | |
| Val v -> value_fmt pp v | |
| Out v -> | |
let f = Fmt.(prefix (const string "out") (parens value_fmt)) in | |
f pp v | |
| BinOp (op, v1, v2) -> | |
let f = Fmt.(pair value_fmt (pair bin_op_fmt value_fmt)) in | |
f pp (v1, (op, v2)) | |
let test = BinOp (Plus, Int 1, Int 10) | |
let () = | |
Fmt.pr "%a" (Fmt.hbox exp_fmt) test | |
(* ocamlfind ocamlc -package fmt -linkpkg fmt_test.ml *) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment