约束多态类型

ane*_*eal 12 ocaml types functional-programming

我有一个范围类型定义为:

type 'a range = Full | Range of ('a * 'a)
Run Code Online (Sandbox Code Playgroud)

但是,我想将'a'限制为整数或浮点数或字符,而没有其他有效类型的'a.

Range(0,10) (* valid *)
Range(0.0, 10.0) (* valid *)
Range('a', 'z') (* valid *)
Range("string1", "string2") (* other types like this shouldn't type check *)
Run Code Online (Sandbox Code Playgroud)

我想我可以将我的类型定义更改为:

type sequential   = S_int of int | S_float of float | S_char of char ;;
type range = Full | Range of (sequential * sequential);;
Run Code Online (Sandbox Code Playgroud)

但是,这将允许以下内容:

Range(S_int(0), S_float(10.0));; (* problem: mixes int and float *)
Run Code Online (Sandbox Code Playgroud)

...但我希望Range的两个组件都是相同的类型.

我想另一种方法是创建一个int_range类型,一个float_range类型和一个char_range类型,但我想知道是否还有另一种方法?

ygr*_*rek 12

另一种方法是声明类型private并公开仅使用您想要的类型构造它的函数,例如:

module Z : sig
  type 'a range = private Full | Range of ('a * 'a)
  val int_range : int -> int -> int range
  val float_range : float -> float -> float range
  val string_range : string -> string -> string range
  val full : 'a range
end = struct
  type 'a range = Full | Range of ('a * 'a)
  let create x y = Range (x,y)
  let int_range = create
  let float_range = create
  let string_range = create
  let full = Full
end

# open Z;;
# int_range 2 3;;
- : int Z.range = Range (2, 3)
# Range ('a','c');;
Error: Cannot create values of the private type char Z.range
Run Code Online (Sandbox Code Playgroud)


小智 10

从Haskell将要做的提示(声明一个类型类(Sequential a) => Range a)中你可以使用一个仿函数:

module Range (S : sig type t end) = struct
  type range = Full | Range of (S.t * S.t)
end
Run Code Online (Sandbox Code Playgroud)

并使用它来提供所需的模块:

module IntRange   = Range (struct type t = int   end)
module FloatRange = Range (struct type t = float end)
module CharRange  = Range (struct type t = char  end)
Run Code Online (Sandbox Code Playgroud)

缺点是你失去了参数range; 好处是你的参数函数range现在存在于模块中Range,因为它们可能应该存在.

通常,Ranges将对s的许多要求Sequential进行补偿以补偿参数的损失.可以在仿函数参数的签名中清晰地指定这些要求:

module type SEQUENTIAL = sig
  type t
  val to_string : t -> string
  val compare : t -> t -> int
  (* ... *)
end

module Range (S : SEQUENTIAL) = struct
  type t = Full | Range of (S.t * S.t)
  let to_string = function
  | Full -> "full"
  | Range (lo, hi) -> "(" ^ S.to_string lo ^ "," ^ S.to_string hi ^ ")"
  let make lo hi =
    if S.compare lo hi > 0 then Range (hi, lo) else Range (lo, hi)
end
Run Code Online (Sandbox Code Playgroud)

要实例化Range特定类型,您现在需要提供一个正确参数化的结构:

module IntRange = Range (struct
  type t = int
  let to_string = string_of_int
  let compare = Pervasives.compare
end)
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用它:

# IntRange.(to_string (make 4 2)) ;;
- : string = "(2,4)"
Run Code Online (Sandbox Code Playgroud)

(使用新语法进行分隔重载).如果需要隐藏Range签名后面的s 实现,则可能需要重新导出SEQUENTIALs 的类型,就像标准库中的数据结构一样:

module Range (S : SEQUENTIAL) : sig
  type elt = S.t
  type t = private Full | Range of (elt * elt)
  val to_string : t -> string
  val make : elt -> elt -> t
end = struct
  type elt = S.t
  type t = Full | Range of (elt * elt)
  let to_string = function
  | Full -> "full"
  | Range (lo, hi) -> "(" ^ S.to_string lo ^ "," ^ S.to_string hi ^ ")"
  let make lo hi =
    if S.compare lo hi > 0 then Range (hi, lo) else Range (lo, hi)
end
Run Code Online (Sandbox Code Playgroud)

这为封装和半透明类型提供了模式匹配但不构造的类型.private在签名中声明类型的替代方法是使用视图类型或解构函数.