【发布时间】:2010-12-31 11:28:35
【问题描述】:
可以以下多态函数
let id x = x;;
let compose f g x = f (g x);;
let rec fix f = f (fix f);; (*laziness aside*)
为类型/类型构造函数或模块/函子编写?我试过了
type 'x id = Id of 'x;;
type 'f 'g 'x compose = Compose of ('f ('g 'x));;
type 'f fix = Fix of ('f (Fix 'f));;
对于类型,但它不起作用。
这是用于类型的 Haskell 版本:
data Id x = Id x
data Compose f g x = Compose (f (g x))
data Fix f = Fix (f (Fix f))
-- examples:
l = Compose [Just 'a'] :: Compose [] Maybe Char
type Natural = Fix Maybe -- natural numbers are fixpoint of Maybe
n = Fix (Just (Fix (Just (Fix Nothing)))) :: Natural -- n is 2
-- up to isomorphism composition of identity and f is f:
iso :: Compose Id f x -> f x
iso (Compose (Id a)) = a
【问题讨论】:
-
我不是 100% 确定,因为我不了解 Haskell,而且我不清楚 Compose fgx =... 在 Haskell 中的实际含义,但您可能有兴趣知道OCAML 的开发版本具有一流的模块。
-
我很确定你不能在 ML 中做到这一点,因为你需要更高种类的多态性。你能举一些例子来说明你将如何在 Haskell 中使用这些类型吗?
-
nlucaroni,很有趣! (链接是caml.inria.fr/cgi-bin/viewcvs.cgi/ocaml/branches/fstclassmod 我相信)Chris Conway,我添加了一些示例。
标签: functional-programming ocaml