gpt4 book ai didi

scala - 是否有 `Optional` 的 van Laarhoven 表示

转载 作者:行者123 更新时间:2023-12-03 20:51:07 24 4
gpt4 key购买 nike

许多类型的光学器件都有 van Laarhoven 表示。

例如,Lens类型 Lens s t a b可以表示为:

 Functor f => (a -> f b) -> s -> f t

类似的 Traversal ,可以用类似的方式表示,交换 Functor Applicative 的约束:
 Applicative f => (a -> f b) -> s -> f t

几种光学框架,如 MonocleArrow定义一个名为 Optional 的类型.

In Monocle's Optics heirarchy Optional适合 LensTraversal
据我了解:如果 Traversal就像 Lens可能有零到多个目标,然后是 Optional就像 Lens可能有零到一的目标。

在单片眼镜中, Optional定义为一对函数:
getOrModify :: s -> Either t a 
set :: (b, s) -> t

Comments in the Monocle source code建议也可以表示 Optional “作为较弱的 PLens 和较弱的 PPrism

是否可以表示 Optional作为van Laarhoven函数?

最佳答案

如果 Functor/Applicative/Monad 层次结构更细粒度,就会有一种方法来表示它。尤其是:

class Functor f => Pointed f where
pure :: a -> f a

type Optional s t a b = forall f. Pointed f => (a -> f b) -> s -> f t

请注意,该类型可能会被命名为 Affine如果在类层次结构中整齐的话,在镜头库中。

关于scala - 是否有 `Optional` 的 van Laarhoven 表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58583458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com