Ocaml Lwt 类型混淆

Ocaml Lwt type confusion

我很困惑为什么 Lwt 打印函数 Lwt_io.print 有类型 string -> unit Lwt.t 但是如果我 运行 Lwt_io.print "a" >>= fun () -> Lwt_io.print "b";; 结果是打印 "ab" 并返回类型单元。

我想这会是一个类型错误,因为 Lwt_io.print returns unit Lwt.t 不是 unit。为什么叫线程的第二部分?

我怀疑您感到困惑,因为 utop 很聪明。

如果你看utop documentation,它写着

when using the lwt or async libraries, UTop will automatically wait for ['a Lwt.t] or ['a Deferred.t] values and return the ['a] instead

这就是为什么

Lwt_io.print "a" >>= fun () -> Lwt_io.print "b";;

似乎属于 unit 类型。要查看真实类型,请尝试以下操作

let res = Lwt_io.print "a" >>= fun () -> Lwt_io.print "b";;
#show res;;

您会看到,当您得到您所期望的结果时,unit Lwt.t

更新:

为了明确类型,我们有

let f = fun () -> Lwt_io.print "b"
val ( >>= ) : 'a Lwt.t -> ('a -> 'b Lwt.t) -> 'b Lwt.t
val print : string -> unit Lwt.t
val f : unit -> unit Lwt.t

Lwt_io.print "a" 因此 returns 一个 unit Lwt.t。 这是(>>=)的第一个参数,因此'a就是unit(>>=)的第二个参数是ff 需要一个 unit,这是我们需要的,因为 'aunit。它returns一个unit Lwt.t,所以'b也是unit。这意味着最终结果将是 unit Lwt.t.

在 Utop[1] 上,顶层 lwtasync 表达式分别自动 运行 和 Lwt_main.runThread_safe.block_on_async_exn

要禁用它,运行

UTop.set_auto_run_lwt false;; (* for lwt *)
UTop.set_auto_run_async false;; (* for async *)

[1] https://github.com/ocaml-community/utop/blob/c898602882e032ec3c5c8315d260cfbdb2f9eeb4/src/lib/uTop.mli#L114-L133