Haskell 默认超类实例
我想从一些自定义类的 Num 声明中取出一些样板文件(称为单项式和多项式)。而不是写
instance Num (Monomial) where
f - g = f + (negate g)
abs _ = undefined
有没有办法解决这个问题?我遇到了 默认超类实例 和名为“strathclyde haskell 增强" 如果实现的话可能会让我写一些类似的东西,
class SimpleNum a => Num a where
(+) :: a -> a -> a -- standard ring stuff
(*) :: a -> a -> a
one :: a
zero :: a
instance Num (SimpleNum a) where
f - g = f + (negate g)
abs _ = undefined
什么是通常/简单的处理方法?
I want to take some of the boilerplate out of Num declarations for a few custom classes (call them Monomial and Polynomial). Instead of writing
instance Num (Monomial) where
f - g = f + (negate g)
abs _ = undefined
Is there a way to get around this? I came across default superclass instances and something called "the strathclyde haskell enhancement" which if implemented might let me write something like,
class SimpleNum a => Num a where
(+) :: a -> a -> a -- standard ring stuff
(*) :: a -> a -> a
one :: a
zero :: a
instance Num (SimpleNum a) where
f - g = f + (negate g)
abs _ = undefined
What's the usual / simple way of dealing with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
处理此问题的常用方法是至少执行以下一项或多项操作:
多发牢骚。
像这样编写辅助函数:
使用诸如 Template Haskell 之类的工具 和 派生。
尝试实现您提到的扩展。 (不幸的是,这并不像您想象的那么容易。)
The usual ways of dealing this is to do at least one or more of the following:
Grumble a lot.
Write helper functions like this:
Use tools like Template Haskell and Derive.
Attempt to implement extensions like the one you mention. (This unfortunately not as easy as you might think.)