如何在交互式Ocaml中获取类型信息?

Ale*_*rov 21 ocaml

我正在使用版本4的Ocaml.当我以交互方式定义某种类型时,解释器会在此之后立即打印出该类型的字符串表示:

# type foo = Yes | No;;         <-- This is what I entered
type foo = Yes | No             <-- This is what interpreter bounced
Run Code Online (Sandbox Code Playgroud)

但是在我输入更多定义之后,有时我想再次看到该类型的文本表示.

在Haskell中,我可以输入":t foo".

我怎么能在Ocaml中这样做?

ivg*_*ivg 14

在utop中,您可以使用该#typeof指令:

#typeof "list";;
type 'a list = [] | :: of 'a * 'a list 
Run Code Online (Sandbox Code Playgroud)

您可以将值和类型放在双引号内:

let t = [`Hello, `World];;
#typeof "t";;
val t : ([> `Hello ] * [> `World ]) list   
Run Code Online (Sandbox Code Playgroud)

PS甚至更好的解决方案是使用merlin.


cod*_*nk1 2

据我所知,Ocaml中实际上没有办法检索字符串形式下的类型信息

您必须为每种类型构建模式匹配

type foo = Yes | No;;

let getType = function
  |Yes -> "Yes"
  |No -> "No"    
  ;;

let a = Yes;;
print_string (getType a);;
Run Code Online (Sandbox Code Playgroud)