我正在尝试重新创建ST
s rank-2 多态性技巧,以确保某些值无法逃脱自定义单子。以下代码代表了我的解决方案的精神:
data STLike s a = STLike a
class Box box where
runBox :: box a -> a
run :: Box box => (forall s. box (STLike s a)) -> a
run box = let STLike a = runBox box in a
虽然上面的代码编译得很好,但当我尝试引入约束时问题就开始了box (STLike s a)
:
data STLike s a = STLike a
class Box box where
runBox :: box a -> a
run :: Box box => (forall s. Eq (box (STLike s a)) => box (STLike s a)) -> a
run box = let STLike a = runBox box in a
第二个片段无法编译并显示以下消息:
Could not deduce (Eq (box (STLike t0 a)))
arising from a use of `box'
from the context (Box box)
bound by the type signature for
run :: Box box =>
(forall s. Eq (box (STLike s a)) => box (STLike s a)) -> a
at src/TPM/GraphDB/Event.hs:36:8-76
The type variable `t0' is ambiguous
有可能解决这个问题吗?如果不是那为什么?