签名与 Set 实现不匹配

问题描述 投票:0回答:1
module type SET =
sig
  type 'a set
  val member : 'a -> 'a set -> bool
  val subset : 'a set -> 'a set -> bool
end;;

module MySet : SET = struct
  type 'a set = 'a -> bool

  let member element set = set element

  let subset set1 set2 =
    let subset_check elem = member elem set2 in
    member subset_check set1
end

Lines 8-16, characters 21-3:
Error: Signature mismatch:
Modules do not match:
  sig
    type 'a set = 'a -> bool
    val member : 'a -> ('a -> 'b) -> 'b
    val subset : (('a -> 'b) -> 'c) -> ('a -> 'b) -> 'c
  end
is not included in
  SET
Values do not match:
  val subset : (('a -> 'b) -> 'c) -> ('a -> 'b) -> 'c
is not included in
  val subset : 'a set -> 'a set -> bool

有人能告诉我为什么我会收到他的错误吗? 以及正确的代码应该是什么。

module ocaml structure
1个回答
0
投票

您收到签名不匹配错误,因为您的子集函数的类型。缩小类型错误范围的一个好技巧是对更接近问题根源的类型进行注释。如果您向

subset
的参数添加类型注释,您将得到更精确的错误:

let subset (set1: 'a set) (set2: 'a set): bool =
  let subset_check elem = member elem set2 in
  member subset_check set1
Line 15, characters 24-28:
Error: This expression has type 'a set = 'a -> bool
but an expression was expected of type ('a -> bool) -> 'b
The type variable 'a occurs inside 'a -> bool

这表明这里的问题是您将

set
传递给需要元素的
subset_check
函数。

© www.soinside.com 2019 - 2024. All rights reserved.