作为一项学习练习,我尝试定义一个新类型作为函数的持有者,可以将Show
- 能值转换为Eff
,即:
newtype ShEff a = ShEff (forall eff. Show a => a -> Eff eff Unit)
但是,这个:
f :: forall a. ShEff a
f = ShEff logShow
无法使用此错误进行编译:
Could not match type
( console :: CONSOLE
| t2
)
with type
eff1
while trying to match type Eff
( console :: CONSOLE
| t2
)
with type Eff eff1
while checking that expression logShow
has type t0 -> Eff eff1 Unit
in value declaration f
你能指出我正确的方向吗?
答案 0 :(得分:1)
logShow
的类型是
forall eff. Show a => a -> Eff (console :: CONSOLE | eff) Unit
因此您无法将其存储在ShEff
中,因为它必须适用于每个 eff
,而logShow
仅适用于包含{{ 1}}效果。
您有两种选择:
将CONSOLE
类型参数移到eff
之外:
ShEff
在newtype ShEff eff a = ShEff (a -> Eff eff Unit)
f :: forall a eff. Show a => ShEff (console :: CONSOLE | eff) a
f = ShEff logShow
:
ShEff
另请注意,我在两种情况下都将newtype ShEff a = ShEff (forall eff. a -> Eff (console :: CONSOLE | eff) Unit)
f :: forall a eff. Show a => ShEff a
f = ShEff logShow
约束移到了Show a
之外。