Applicative与Generative仿函数

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

我最近学习了SML,当我开始了解术语 - 应用和生成的函子。我也知道SML使用生成函子。

我尝试使用谷歌条款,但找不到任何令人信服的资源来解释这些术语的含义以及两者之间的区别。

所以,我只是想以一些实际上可行的方式知道这些术语的实际含义,以及它如何与SML生成相关。

functional-programming terminology sml functor
1个回答
9
投票

它与仿函数应用程序产生的模块中抽象类型的相等性有关。

生成意味着两个仿函数的调用将生成包含不相等抽象类型的模块。

Applicative意味着如果参数在某种意义上是相等的(例如在语法上相同),两个仿函数的调用将产生包含相同抽象类型的模块。

我将在OCaml中给出一个例子,因为它恰好支持两者:

module App (M : sig end) : sig
  type t
  val zero : t
end = struct
  type t = int
  let zero = 0
end

(* A () argument signifies a generative functor in OCaml. *)
module Gen (M : sig end) () : sig
  type t
  val zero : t
end = struct
  type t = int
  let zero = 0
end

module Empty = struct end

module A = App (Empty)
module B = App (Empty)
module C = App (struct end) (* The argument is syntactically different. *)

module D = Gen (Empty) ()
module E = Gen (Empty) ()

let _ = begin
  (* A.t and B.t are compatible. *)
  ignore (A.zero = B.zero);  (* OK *)

  (* A.t and C.t are not compatible because the functor arguments
   * are not syntactically equal. *)
  ignore (A.zero = C.zero);  (* type error *)

  (* D.t and C.t are not compatible because they are produced
   * from generative functors. *)
  ignore (D.zero = E.zero); (* type error *)
end
© www.soinside.com 2019 - 2024. All rights reserved.