更新:我在这里内联代码。
我正在尝试通过调用定义Data.Profunctor.Choice
where的实例,但由于某种原因,编译器抱怨这是未知的。right
left
left
newtype StateTrans s i o = ST (Tuple s i → Tuple s o)
instance stFunctor ∷ Functor (StateTrans s a) where
map f (ST st) = ST $ second f <<< st
instance stProfunctor ∷ Profunctor (StateTrans s) where
dimap f g (ST st) = ST $ second g <<< st <<< second f
instance stChoice ∷ Choice (StateTrans s) where
left (ST f) = ST lf
where
lf (Tuple s (Left a)) = let (Tuple s' b) = f (Tuple s a)
in Tuple s' (Left b)
lf (Tuple s (Right c)) = Tuple s (Right c)
-- Fails to compile with: Unknown value left
right f = arr mirror <<< left f <<< arr mirror
where
mirror Left x = Right x
mirror Right x = Left x
可能是一个愚蠢的错误,但我一直在查看我的代码很长时间,我无法弄清楚出了什么问题。
(次要且不相关:在 for 的Right
情况下left
,我必须解包并重新打包该值以使其类型对齐。添加类型归属也无法编译。)
奇怪的是,我对 做同样的事情没有问题Strong
,请参阅:
instance stStrong ∷ Strong (StateTrans s) where
first (ST f) = ST ff
where
ff (Tuple s (Tuple a c)) = let (Tuple s' b) = f $ Tuple s a
in Tuple s' (Tuple b c)
second f = arr swap <<< first f <<< arr swap