我已经做了一个测试 fclabels 的最小例子。使用镜头从任一值中检索“正确”值。为什么这会失败?我错过了包括什么吗?
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
-}
最佳答案
该错误相当模糊,但当我们查看 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
不是总镜头,因为如果值为 Left
则它不起作用。错误是说部分镜头需要载体类型为 ArrowZero
,但总镜头不能这样做。如果您在
ghci
中进行实验,则仅通过调用 get right
就会收到此错误,而没有任何参数。如果您将
Data.Label
的导入更改为 Data.Label.Partial
,那么您的代码将起作用。 test
以 Maybe String
类型结束。关于haskell - 使用 fclabels 中的 'left' 镜头时键入失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26370141/