Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Get types in OCaml
let _ =
let open Typedtree in
let open Types in
let types_from_arrow ty =
let rec helper ty =
match ty.desc with
| Tlink ty -> helper ty
| Tarrow (_, t1, t2, _) -> helper t1 @ helper t2
| Tconstr (Path.Pident id, _, _) -> [Ident.name id]
| _ -> failwith "oops" in
helper ty in
let e = let _ = Compmisc.init_path false in
Compmisc.initial_env () in
let buf = Lexing.from_string "let add x y = x + y" in
let p = Parse.implementation buf in
let tstr, _tsig, _newe = Typemod.type_structure e p Location.none in
(* let _ = Printtyped.implementation Format.std_formatter tstr in *)
let {str_items} = tstr in
let {str_desc} :: _ = str_items in
let Typedtree.Tstr_value (_, lst) = str_desc in
let rec helper = function
| [] -> ()
| ({ pat_type }, _) :: rest ->
Printtyp.raw_type_expr Format.std_formatter pat_type;
helper rest in
helper lst;
@jsyeo
Owner
jsyeo commented Oct 25, 2013

Compile with ocamlbuild -pkg compiler-libs -lib ocamlcommon gettyp.ml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment