为什么我在 OCaml 中的小 monad 无法编译?

问题描述 投票:0回答:1

我正在 OCaml 中学习 Monad,但它无法编译。

我减少了代码以最简单的方式重现问题:

文件 try.ml:

module type TRY = sig
  type 'a t
  val return : 'a -> 'a t
end

module Try : TRY = struct

  type 'a t = Success of 'a | Failure of exn

  let return a = Success a
end

let () =
  match Try.return 5 with
  | Try.Success a -> print_int a
  | Try.Failure e -> print_endline (Printexc.to_string e)

然后我编译:

ocamlc try.ml

我收到此错误:

错误:未绑定构造函数 Try.Success

我找不到修复的线索,我需要帮助。

compilation ocaml monads
1个回答
0
投票

好的我明白了, 用 monad 做签名是个坏主意
即使我写

module Try : (TRY with type 'a t = Success of 'a | Failure of exn) = struct

编译失败,因为“of”引发语法错误

我刚刚删除了签名TRY,然后就可以了 只是 OCaml 的东西...

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.