gpt4 book ai didi

class - 使一个类成为另一个类的实例

转载 作者:行者123 更新时间:2023-12-04 12:33:33 26 4
gpt4 key购买 nike

我有一门课叫 Foo拥有函数 gen :: Int -> [Foo] .例如,我可以创建 Foo 的实例那样:

data FooTest = FooTest Int

instance Foo FooTest where
gen n = replicate n (FooTest 0)

现在,假设我有另一个名为 Bar 的类。定义函数 bar :: Bar -> IO () . Foo 的每个实例必须是 Bar 的实例, 但是 Bar每个实例的实现都完全相同。这是一个例子:
class Foo f where
gen :: Int -> [f]

class Bar b where
bar :: b -> IO ()

instance Bar Foo where -- obviously that doesn’t work
bar _ = putStrLn "bar through any Foo instance"

instance (Foo f) => Bar f where -- this needs the FlexibleInstance GHC extension first, then it still throws shit saying that the constraint is not smaller that I don’t shit
bar _ = putStrLn "bar through any Foo instance"

这里的问题是我找不到任何方法让一个类成为另一个类的实例,以提及第一个类的任何实例都将共享相同的实现来实例化另一个类。

任何想法?

提前致谢。

最佳答案

您可以使用带有两个扩展的最后一个实例来完成您想要的操作:FlexibleInstancesUndecidableInstances .

就像名字所暗示的那样,第二个扩展允许您潜在地编写不终止的实例。这可能会导致编译时出现无限循环;但是,该实现被任意限制在某个递归深度,因此您实际上不应该有无限的编译时间。

我不知道有任何方法可以在没有任何扩展的情况下做到这一点。但是,除非您将来可能想使用其他编译器,否则使用扩展本身并不是坏事。

此外,随机样式注释:如果您只有一个约束,则括号是可选的。所以你可以把它写成:

instance Foo f => Bar f where ...

这不是很重要,但我认为第二个版本看起来更好。

关于class - 使一个类成为另一个类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15220061/

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