在 Haskell 中,是否有一种有效的方法来生成给定泛型(尤其是带有 monads)类型签名的函数?

Is there an effective way to generate a function given a generic (esp. with monads) type signature in Haskell?

我已经看到了"Given type signature XXX, find implementation in Haskell."形式的各种问题因此很自然地会问这是否可以泛化或算法化。类似的问题是here。然而,很明显,通常这项任务是不可能完成的。所以下一个问题是用一些通用性换取实用性。

Question: Is the problem decidable if all the type signatures consists of rigid type variables together with some constraints, drawn from a fixed set (e.g. Monad, Traversable, Foldable?)

一个典型的问题是 (Monad m) => (m j -> [m d]) -> m [j] -> [m [d]],为了方便,我使用 [] 而不是 (..Constraints t) => t

令人惊讶的是,这实际上是可能的!看看 Djinn (or you could compile the executable yourself), which implements this for you. For instance, given f :: a -> (a -> b) -> (a -> b -> c) -> c, Djinn outputs f a b c = c a (b a) (amongst other possibilities). You can find more examples (with the command-line version) at http://lambda-the-ultimate.org/node/1178。不幸的是,虽然它不支持类型类,但我不排除我还没有找到支持它们的另一种工具。

(如果您对它的工作原理感兴趣,请阅读 Curry-Howard 同构 ,其中指出使用 Haskell 等语言编写的程序等同于证明。例如,为 f :: a -> (a -> b) -> (a -> b -> c) -> c 编写实现等同于证明命题“给定命题 a,则如果 a 蕴含 b,并且 ab 一起意味着 c,那么 c 是真的'。因此,您可以使用自动证明器来计算实现,然后将其机械地转换为获得实现的代码。)