Created
September 16, 2014 20:33
-
-
Save ramntry/f9b01440c19c17b20689 to your computer and use it in GitHub Desktop.
OCaml GADT's usage example
This file contains hidden or 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 _ 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