Created
October 25, 2013 08:36
-
-
Save jsyeo/7151426 to your computer and use it in GitHub Desktop.
Get types in OCaml
This file contains 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
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile with
ocamlbuild -pkg compiler-libs -lib ocamlcommon gettyp.ml