【发布时间】:2020-06-06 16:08:13
【问题描述】:
首先:我知道我不能使用这个功能,除非我启用了TypeApplications。但我认为AllowAmbiguousTypes 是为了解决这个问题。
我目前有以下代码:
{-# LANGUAGE ExplicitForAll, AllowAmbiguousTypes #-}
module A where
class B c where
d :: forall e. e -> c e
class F g where
h :: forall i. g i -> i
data J k = J {l :: k}
instance B J where
d = J
instance F J where
h = l
m :: forall n o. (B n, F n) => o -> o
m = h . d
用 GHCi 8.10.1 解释这个或用 GHC 8.10.1 编译它会导致这个错误:
j.hs:20:5: error:
• Could not deduce (F g0) arising from a use of ‘h’
from the context: (B n, F n)
bound by the type signature for:
m :: forall (n :: * -> *) o. (B n, F n) => o -> o
at j.hs:19:1-37
The type variable ‘g0’ is ambiguous
These potential instance exist:
instance F J -- Defined at j.hs:16:10
• In the first argument of ‘(.)’, namely ‘h’
In the expression: h . d
In an equation for ‘m’: m = h . d
|
20 | m = h . d
| ^
j.hs:20:9: error:
• Could not deduce (B g0) arising from a use of ‘d’
from the context: (B n, F n)
bound by the type signature for:
m :: forall (n :: * -> *) o. (B n, F n) => o -> o
at j.hs:19:1-37
The type variable ‘g0’ is ambiguous
These potential instance exist:
instance B J -- Defined at j.hs:13:10
• In the second argument of ‘(.)’, namely ‘d’
In the expression: h . d
In an equation for ‘m’: m = h . d
|
20 | m = h . d
| ^
我知道,也许编译器无法将B 和F 实例与d 和h 的使用联系起来。我认为这样的事情可以解决这个问题:
m @p = h @p . d @p
但是,即使使用TypeApplications,也会被拒绝。
是否有一些语言选项可供我选择,要么使类似的解决方法成为可能,要么完全使编译器能够推断连接?
【问题讨论】: