Skip to content

Instantly share code, notes, and snippets.

@ramntry
Created September 16, 2014 20:33
Show Gist options
  • Select an option

  • Save ramntry/f9b01440c19c17b20689 to your computer and use it in GitHub Desktop.

Select an option

Save ramntry/f9b01440c19c17b20689 to your computer and use it in GitHub Desktop.
OCaml GADT's usage example
type _ t =
| Const : int -> int t
| Add : int t * int t -> int t
| Less : int t * int t -> bool t
| IfThenElse : bool t * int t * int t -> int t
let rec eval : type a. a t -> a = function
| Const x -> x
| Add (l, r) -> (eval l) + (eval r)
| Less (l, r) -> (eval l) < (eval r)
| IfThenElse (cond, then_, else_) ->
eval (if eval cond then then_ else else_)
let int_term =
Add (Const 10,
IfThenElse (Less (Add (Const 20,
Const 30),
Const 40),
Const 0,
Add (Const 30,
Const 2)))
let bool_term = Less (int_term, Const 50)
let () =
assert (eval int_term = 42);
assert (eval bool_term)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment