统一记录类型

时间:2017-04-06 22:22:32

标签: purescript

作为一项学习练习,我尝试定义一个新类型作为函数的持有者,可以将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

你能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

logShow的类型是

forall eff. Show a => a -> Eff (console :: CONSOLE | eff) Unit

因此您无法将其存储在ShEff中,因为它必须适用于每个 eff,而logShow仅适用于包含{{ 1}}效果。

您有两种选择:

  1. CONSOLE类型参数移到eff之外:

    ShEff
  2. newtype ShEff eff a = ShEff (a -> Eff eff Unit) f :: forall a eff. Show a => ShEff (console :: CONSOLE | eff) a f = ShEff logShow

    中添加约束
    ShEff
  3. 另请注意,我在两种情况下都将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之外。