7
{-# LANGUAGE GADTs #-}

data Foo x y where
  Composition :: Foo b c -> Foo a b -> Foo a c
  FMap :: Functor f => (a->b) -> Foo (f a) (f b)

asFunction :: Foo a b -> a->b
asFunction (FMap m) = fmap m
-- asFunction (Composition (FMap m) (FMap n)) = fmap m . fmap n
asFunction (Composition m n) = asFunction m . asFunction n

这可以按预期工作...直到您取消注释asFunction!的第二个子句 这实际上只是其他两种模式已经匹配的特殊情况的内联版本,所以我希望它没问题。但是 ( ghc-7.6.2, 或者也ghc-7.4.1)

Could not deduce (f ~ f1)
from the context (b1 ~ f a1, b ~ f b2, Functor f)
  bound by a pattern with constructor
             FMap :: forall (f :: * -> *) a b.
                     Functor f =>
                     (a -> b) -> Foo (f a) (f b),
           in an equation for \`asFunction'
  ...

为什么会发生这种情况,为什么不在其他条款中?在更复杂的应用程序中究竟应该做些什么来防止这种麻烦?

4

1 回答 1

3

这可能与强制从 GHC 的类型推理系统中暂时删除的特征左/右分解有关,以便支持更灵活(“不饱和”)的类型函数,然后在发现它具有类似这样的烦人效果时重新引入

于 2014-04-24T03:07:45.893 回答