无法演绎超类

nic*_*las 7 haskell typeclass

在下面的代码中,GHC在实例的定义中找不到Functor Monoidal实例.

为什么不GHC推断出给定Applicative约束条件,则Functor 是已经什么地方?(这个推理'能力'有名字吗?)

import Prelude hiding (Applicative (..), Monad (..))

class Functor f => Applicative f where
  pure :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b

class Functor f => Monoidal f where
   unit::f ()
   (*) ::f a -> f b -> f (a,b)

instance Applicative f => Monoidal f where
  unit = pure ()
  a * b = undefined
Run Code Online (Sandbox Code Playgroud)

我知道当然Functor f可以Monoidal为没有错误添加一个显式约束,但我的问题更多的是为什么实例解析以这种方式工作

import Prelude hiding ((*), Applicative (..), Monad (..))

class Functor f => Applicative f where
  pure :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b

class Functor f => Monoidal f where
   unit::f ()
   (*) ::f a -> f b -> f (a,b)

instance (Applicative f, Functor f) => Monoidal f where
  unit = pure ()
  a * b = (pure (,) <*> a <*> b )

instance (Monoidal f, Functor f) => Applicative f where
  pure x = fmap (\_ -> x) unit
  mu <*> mx = fmap (\(f, x) -> f x) ((mu * mx) :: f (a -> b, a))
Run Code Online (Sandbox Code Playgroud)

Dan*_*ner 3

对我来说看起来像一个错误。这是一个显示问题的最小文件,并且不依赖于任何重命名Preludestuff 或undefineds 的恶作剧。

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
class A x
class A x => B x
class A x => C x
instance B x => C x
Run Code Online (Sandbox Code Playgroud)

我建议使用此文件(或非常类似的文件)在 GHC 错误跟踪器上提交错误;发现B x暗示A x应该是可能的推理。

  • 事实证明,从 8.0 开始,这是已知的、官方预期的行为。诡异的。 (2认同)
  • [此评论](https://ghc.haskell.org/trac/ghc/ticket/11427#comment:4) 似乎是为了赚钱,解释了这种行为解决的问题。不过,这很有技术含量。 (2认同)