代码之家  ›  专栏  ›  技术社区  ›  Raffael

为什么这个函子的设置不起作用?

  •  0
  • Raffael  · 技术社区  · 6 年前

    为了避免与Prelude冲突,我定义了自己的Functor类:

    class F f where
        fm :: (a -> b) -> f a -> f b
    

    instance F ((->) a) where
        fm g f = g . f
    

    GHCI公司:

    *Main> fm (+1) (+10) $ 100
    111
    

    ... 但这不是吗?

    instance F ((->) Int) where
        fm g f = g . f
    

    我不明白-我应该限制底层集合到映射的函数吗 Int

    错误消息没有帮助:

        • Illegal instance declaration for ‘F ((->) Int)’
            (All instance types must be of the form (T a1 ... an)
             where a1 ... an are *distinct type variables*,
             and each type variable appears at most once in the instance head.
             Use FlexibleInstances if you want to disable this.)
        • In the instance declaration for ‘F ((->) Int)’
      |
    4 | instance F ((->) Int) where
      |          ^^^^^^^^^^^^
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   leftaroundabout    6 年前

    是的,而且 -XFlexibleInstances 分机。

    {-# LANGUAGE FlexibleInstances #-}
    class F f where fm :: (a -> b) -> f a -> f b
    instance F ((->) Int) where fm g f = g . f
    

    instance F ((->) a) where fm g f = g . f
    

    FlexibleInstances 因此扩展非常广泛,事实上我几乎在每个模块中都使用它。这是无可争议的。