3

我做了一个最小的例子来测试 fclabels。使用镜头从 Either 值中检索“正确”值。为什么会失败?我错过了什么吗?

module Label where
import Data.Label
import Data.Label.Base

test = get right (Right "test")

{- Will fail with this message:
Label.hs:5:12:
    No instance for (Control.Arrow.ArrowZero Data.Label.Point.Total)
      arising from a use of `right'
    Possible fix:
      add an instance declaration for
      (Control.Arrow.ArrowZero Data.Label.Point.Total)
    In the first argument of `get', namely `right'
    In the expression: get right (Right "test")
    In an equation for `test': test = get right (Right "test")
Failed, modules loaded: none.

-- Tested with fclabels-2.0.2
-}
4

1 回答 1

2

该错误相当晦涩,但是当我们查看 and 的类型和文档时,它会变得更加get清晰right

get :: (f :-> a) -> f -> a
type :-> f o = Lens Total f o

right :: (ArrowZero arr, ArrowApply arr, ArrowChoice arr)
      => Lens arr (Either a b -> Either a o) (b -> o)

-- Lens pointing to the right value in an Either. (Partial and polymorphic)

本质上,get您使用的仅适用于镜头,但right不是总镜头,因为如果值为 a 它将不起作用Left。错误是说部分镜头需要载体类型为ArrowZero,但总镜头不能做到这一点。

如果您在 中进行实验ghci,您只会从调用 中得到此错误get right,而无需任何参数。

如果您更改 to 的导入Data.LabelData.Label.Partial那么您的代码就可以工作。test以 type 结束Maybe String

于 2014-10-14T21:05:26.323 回答