一般类型合成的可遍历实例

时间:2016-05-02 16:10:43

标签: haskell types composition

我完全坚持这是一本优秀的Haskell Programming书中的练习。

给定以下类型组合的新类型以及Functor和Applicative的实例,编写Traversable (Compose f g)的实例。

newtype Compose f g a =
  Compose { getCompose :: f (g a) }
  deriving (Eq, Show)

instance (Functor f, Functor g) => Functor (Compose f g) where
  fmap f (Compose fga) = Compose $ (fmap . fmap) f fga

instance (Applicative f, Applicative g) => Applicative (Compose f g) where
  pure = Compose <$> pure . pure
  Compose f <*> Compose x =
    Compose $ ((<*>) <$> f) <*> x

我建议的解决方案看起来应该可行,基于traverse.traverse但ghci抱怨的类型。我有一种模糊的感觉,它与重新包装Compose构造函数有关:

instance (Traversable f, Traversable g) => Traversable (Compose f g) where
  traverse f1 (Compose fga) = (traverse.traverse) f1 fga

给出了类型错误:

composing_types.hs:69:31:
    Couldn't match type ‘b’ with ‘g b’
      ‘b’ is a rigid type variable bound by
          the type signature for
            traverse :: Applicative f1 =>
                        (a -> f1 b) -> Compose f g a -> f1 (Compose f g b)
          at composing_types.hs:69:3
    Expected type: f1 (Compose f g b)
      Actual type: f1 (Compose f g (g b))
    Relevant bindings include
      fga :: f (g a) (bound at composing_types.hs:69:24)
      f1 :: a -> f1 b (bound at composing_types.hs:69:12)
      traverse :: (a -> f1 b) -> Compose f g a -> f1 (Compose f g b)
        (bound at composing_types.hs:69:3)
    In the expression: (traverse . traverse) f1 fga
    In an equation for ‘traverse’:
        traverse f1 (Compose fga) = (traverse . traverse) f1 fga

composing_types.hs:69:54:
    Couldn't match type ‘f’ with ‘Compose f g’
      ‘f’ is a rigid type variable bound by
          the instance declaration at composing_types.hs:68:10
    Expected type: Compose f g (g a)
      Actual type: f (g a)
    Relevant bindings include
      fga :: f (g a) (bound at composing_types.hs:69:24)
      traverse :: (a -> f1 b) -> Compose f g a -> f1 (Compose f g b)
        (bound at composing_types.hs:69:3)
    In the second argument of ‘traverse . traverse’, namely ‘fga’
    In the expression: (traverse . traverse) f1 fga

1 个答案:

答案 0 :(得分:11)

H O L E S

这是另一个可以通过多孔表达式解决的重要问题。

首先,让我们假设我们已经定义了可折叠的实例。

λ> instance (Foldable f, Foldable g) => Foldable (Compose f g) where
     foldr = undefined

接下来,实例Traversable。 Compose参数上的模式匹配,因为你知道你必须这样做,但是否则将所有内容都留在一个洞里。

λ> instance (Traversable t, Traversable u) => Traversable (Compose t u) where
     traverse a2fb (Compose tua) = _ tua

GHC将有助于吐出错误 -

<interactive>:...:...
  Found hole ‘_’ with type: f (Compose t u b)

- 除了范围内所有变量的类型。

Relevant bindings include
  tua :: t (u a) (bound at ...)
  a2fb :: a -> f b (bound at ...)
  traverse :: (a -> f b) -> Compose t u a -> f (Compose t u b)
    (bound at ...)

(我已经选择了类型和值名称,以便所有内容整齐排列。不要注意窗帘后面的那个人。)现在然后是小时的问题:如何给出f (Compose t u b)给出的值其他一切。我们知道

  • 构建Compose t u b的唯一方法是创建一个值t (u b)

  • 除了(1)f anything和(2)pure之外,无法生成fmap的值,直觉上我们知道我们无法使用{{1因为我们正在努力收集副作用&#39;这里pure

这引导我们接下来的解决方案。

a2fb :: a -> f b

最后我们有λ> instance (Traversable t, Traversable u) => Traversable (Compose t u) where traverse a2fb (Compose tua) = fmap Compose (_ tua) <interactive>:... Found hole ‘_’ with type: t (u a) -> f (t (u b)) 。我们知道t是可穿越的,所以让我们尝试遍历它。

t

同样的交易。我们知道λ> instance (Traversable t, Traversable u) => Traversable (Compose t u) where traverse a2fb (Compose tua) = fmap Compose ((\tua -> traverse _ tua) tua) <interactive>:56:138: Found hole ‘_’ with type: u a -> f (u b) 是可穿越的,所以让我们尝试遍历它。

u

我们λ> instance (Traversable t, Traversable u) => Traversable (Compose t u) where traverse a2fb (Compose tua) = fmap Compose ((\tua -> traverse (\ua -> traverse _ ua) tua) tua) <interactive>:57:155: Found hole ‘_’ with type: a -> f b 的金色洞洞。

a2fb

Eta-reduce以删除lambda,最终得到the solution

λ> instance (Traversable t, Traversable u) => Traversable (Compose t u) where
     traverse a2fb (Compose tua) =
       fmap Compose ((\tua -> traverse (\ua -> traverse a2fb ua) tua) tua)