Ocaml 值与模块和签名中的参数化类型不匹配

Posted

技术标签:

【中文标题】Ocaml 值与模块和签名中的参数化类型不匹配【英文标题】:Ocaml values do not match for parameterized type in module and signature 【发布时间】:2017-10-05 05:03:33 【问题描述】:

我正在尝试在http://okmij.org/ftp/tagless-final/nondet-effect.html#no-functor 中进行一项扩展练习,并将int_t 类型替换为'a repr。在尝试执行此操作时,我遇到了以下错误:

Values do not match:
  val cons : '_a repr -> '_a list_t -> '_a list_t
is not included in
  val cons : 'a repr -> 'a list_t -> 'a list_t

我对@9​​87654325@ 的实现看起来像

let cons: 'a repr -> 'a list_t -> 'a list_t =
  liftm2 (fun h t -> h::t)

这绝对是正确的类型。为什么这些明显相同的类型不兼容?

【问题讨论】:

【参考方案1】:

做一个最小的例子帮助我解决了这个问题! 我能够将失败案例简化为:

module type Test = sig
  type 'a t
  val id: 'a t -> 'a t
end

module TestT: Test = struct
  type 'a t = 'a

  let id_maker () x = x
  let id: 'a t -> 'a t =
    id_maker ()
end

这表明我正在成为value restriction 的受害者。 this other stack overflow question 也有类似的问题,但是我被模块错误信息误导了。 我通过更改解决了问题

let cons: 'a repr -> 'a list_t -> 'a list_t =
  liftm2 (fun h t -> h::t)

let cons: 'a repr -> 'a list_t -> 'a list_t =
  fun h t -> liftm2 (fun h t -> h::t) h t

【讨论】:

以上是关于Ocaml 值与模块和签名中的参数化类型不匹配的主要内容,如果未能解决你的问题,请参考以下文章