【发布时间】:2020-12-19 21:01:15
【问题描述】:
我有以下文件:
module SimpleComposition where
class Intermediate a where
f :: a -> Int
g :: Char -> a
h :: Char -> Int
h = f . g
尝试在 ghci 中加载它时,出现错误:
main.hs:8:5: error:
* No instance for (Intermediate a0) arising from a use of `f'
* In the first argument of `(.)', namely `f'
In the expression: f . g
In an equation for `h': h = f . g
|
8 | h = f . g
| ^
我认为问题在于有人可能会使用 2 种不同的类型,它们是此组合中 Intermediate 的实例。导出这个模块时如何保证是一样的?
PS:与我之前提出的问题 (How to compose polymorphic functions in Haskell?) 相比,这是我遇到的问题的一个更好的最小示例。
【问题讨论】:
-
问题是 Haskell 不知道用什么
a作为中间体。 -
答案给出了错误的解决方案,但我认为不要解释错误所暗示的问题。因此,让我尝试一下作为修复的补充。假设我写
instance Intermediate () where {f _ = 0; g _ = ();}; instance Intermediate Bool where {f _ = 1; g _ = False;}。您希望h有什么行为?
标签: haskell polymorphism function-composition