gpt4 book ai didi

haskell - 无法推导出父类(super class)

转载 作者:行者123 更新时间:2023-12-02 00:16:14 24 4
gpt4 key购买 nike

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

为什么 GHC 不推断出给定满足Applicative约束,那么仿函数必须已经在某处了吗? (这种推理“能力”有名字吗?)

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

我知道当然可以向 Monoidal 添加显式的 Functor f 约束以避免出现错误,但我的问题更多是关于为什么实例解析会以这种方式工作

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))

最佳答案

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

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
class A x
class A x => B x
class A x => C x
instance B x => C x

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

关于haskell - 无法推导出父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47093627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com