Kri*_*ssy 8 haskell category-theory lenses haskell-lens
甲棱镜为聚焦到联产品类型的光学部件,而仿射遍历是一种光学器件的可聚焦于0 1的元件,即,AffineTraversal s t a b是同构的(s -> Maybe a, (s, b) -> t).据我所知,如果镜头由棱镜组成,我们会得到一个仿射遍历,只要使用适当的棱镜编码.
我感兴趣的是将Maybe那个(幼稚)配方移动到设置器侧而不是吸气器侧,这样我就可以使用一个总是提取一个元素的光学元件,但可能无法将它放回去.
我的用例与细化类型有关.想象一下,我们有一个类型A及其细化B(B ? A).然后有一个棱镜refined :: Prism' A B:一个A可能或可能不是有效的B,但每个B都可以re进入一个A.结合了Lens' C A有refined,我们有一个仿射遍历.在另一个方向上,可以想象一个光学元件unrefined比一个光学元件更聪明re refined:如果它是有效的,A可以变成一个光学元件,或者如果它不是,则可以变成光学元件.现在,如果我们结合了有,我们有我们的双仿射穿越:它可以随时获得从,但放回任何旧的可能违反的不变和产量,而不是.可以以类似的方式确保更复杂的不变量.Just bBNothingLens' C BunrefinedACACNothingJust c
有趣的是,Scala 的单片机库提供了精细类型的棱镜,但没有反向方向.
我很难为这些(s -> a, b -> Maybe t)和(s -> a, (s, b) -> Maybe t)小玩意提出法律,我想知道更抽象的光学配方是否有用.
我知道用透镜,我们有
type Lens s t a b = forall p. Strong p => p a b -> p s t
type Prism s t a b = forall p. Choice p => p a b -> p s t
type AffineTraversal s t a b = forall p. (Strong p, Choice p) => p a b -> p s t
Run Code Online (Sandbox Code Playgroud)
这使得镜头变焦为产品类型非常清楚,棱镜放大到副产品类型,仿射遍历能够放大代数数据类型(产品或副产品,不能少).
答案是否与类似Cochoice甚至相似Costrong(从profunctor中删除产品/副产品而不是引入它)?我无法从他们那里恢复天真的配方,但是......
这是半个答案,显示了Cochoice光学器件 和之间的对应关系(s -> a, b -> Maybe t)。
{-# LANGUAGE RankNTypes #-}
module P where
import Data.Profunctor
import Control.Monad
data P a b s t = P (s -> a) (b -> Maybe t)
instance Profunctor (P a b) where
dimap f g (P u v) = P (u . f) (fmap g . v)
instance Cochoice (P a b) where
unleft (P u v) = P (u . Left) (v >=> v') where
v' (Left t) = Just t
v' (Right _) = Nothing
type Coprism s t a b = forall p. Cochoice p => p a b -> p s t
type ACoprism s t a b = P a b a b -> P a b s t
fromCoprism :: ACoprism s t a b -> P a b s t
fromCoprism p = p (P id Just)
toCoprism :: P a a s t -> Coprism s t a a
toCoprism (P u v) = unleft . dimap f g where
f (Left s) = u s
f (Right a) = a
g b = case v b of
Nothing -> Right b
Just t -> Left t
Run Code Online (Sandbox Code Playgroud)