Skip to content

Instantly share code, notes, and snippets.

@co-dan
Created May 20, 2019 13:03
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 co-dan/4e19be3843a7a0ff4515eaa1ad176df8 to your computer and use it in GitHub Desktop.
Save co-dan/4e19be3843a7a0ff4515eaa1ad176df8 to your computer and use it in GitHub Desktop.
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