Created
August 19, 2015 13:59
-
-
Save gsg/64b299ae55fd25f843e4 to your computer and use it in GitHub Desktop.
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 _ expr = Int : int -> int expr | Float : float -> float expr | |
| type any_expr = Any : 'a expr -> any_expr | |
| type _ ty = TyInt : int ty | TyFloat : float ty | |
| let rec eval : type a . a expr -> a = function | |
| | Int i -> i | |
| | Float f -> f | |
| let rec typeof : type a . a expr -> a ty = function | |
| | Int i -> TyInt | |
| | Float f -> TyFloat | |
| let rec print : type a . a -> a ty -> unit = | |
| fun value ty -> | |
| match ty with | |
| | TyInt -> print_int value | |
| | TyFloat -> print_float value | |
| let rec print_type : type a . a ty -> unit = | |
| function | |
| | TyInt -> print_string "int" | |
| | TyFloat -> print_string "float" | |
| let eval_and_print (Any e) = | |
| let ty = typeof e in | |
| print_string "- : "; | |
| print_type ty; | |
| print_char ' '; | |
| print (eval e) ty; | |
| print_newline () | |
| let parse str = | |
| try Any (Int (int_of_string str)) | |
| with Failure _ -> | |
| Any (Float (float_of_string str)) | |
| let test () = begin | |
| eval_and_print (parse "13.4"); | |
| eval_and_print (parse "1"); | |
| eval_and_print (parse "bad"); | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment