Skip to content

Instantly share code, notes, and snippets.

@gsg
Created August 19, 2015 13:50
Show Gist options
  • Save gsg/ba070114d4d0b605f14e to your computer and use it in GitHub Desktop.
Save gsg/ba070114d4d0b605f14e to your computer and use it in GitHub Desktop.
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 eval_and_print (Any e) =
print (eval e) (typeof e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment