haskell - Biff 是一个应用程序吗?

标签 haskell

bifunctors 库公开了以下数据类型:

newtype Biff p f g a b = Biff { runBiff :: p (f a) (g b) }

如库中所示,Biff p f g a 是一个 Functor 如果 p 是一个 Bifunctor 并且 g 是一个 Functor

instance (Bifunctor p, Functor g) => Functor (Biff p f g a) where
  fmap f = Biff . second (fmap f) . runBiff
  {-# INLINE fmap #-}

我怀疑(但尚未证明)如果满足以下条件,Biff Either f g a 也是一个应用仿函数:

  • f 是一个Alternative 仿函数
  • g 是一个Applicative 仿函数

这里是相关类型的俄罗斯方 block :

instance (Alternative f, Applicative g) => Applicative (Biff Either f g a)
  where
  pure a = Biff $ Right $ pure a
  Biff f <*> Biff v = Biff $ go f v
    where
    go (Left  x) (Right _) = Left x
    go (Right _) (Left  x) = Left x
    go (Left  x) (Left  y) = Left $ x <|> y
    go (Right x) (Right y) = Right $ x <*> y

这是一个有效的应用实例吗?

最佳答案

换句话说,Biff Either f g a 是两个应用仿函数 Either (f a)g 的组合,因此是一个应用仿函数仿函数。

关于haskell - Biff 是一个应用程序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62480821/

相关文章:

c - haskell FFI : Calling FunPtrs

haskell - 如何从 Haskell 中的文本 block 中提取关键字

Haskell 中带列表的字符串替换运算符

haskell - 存在类型和单子(monad)更改器(mutator)

arrays - 使用 STArray 的 Haskell 并行计算

parsing - SVG 解析和数据类型

haskell - 重复调用 Haskell monad

haskell - 递归 haskell

haskell - 在 Haskell 中计算树中的元素

haskell - 链接两个函数,它们之间没有空格