haskell - 在haskell中制作Integral类的newtype实例
问题描述
我想制作我的 Integral 类的新类型实例。我的新类型:
newtype NT = NT Integer deriving (Eq,Ord,Show)
我希望能够div
使用我的newtype NT
:
Prelude> (NT 5) `div` (NT 2)
2
自从:
Prelude> :t div
div :: Integral a => a -> a -> a
我应该制作我NT
的课程实例Integral
。
这是Integral
类的定义方式:
class (Real a, Enum a) => Integral a where
quot, rem :: a -> a -> a
div, mod :: a -> a -> a
quotRem, divMod :: a -> a -> (a,a)
toInteger :: a -> Integer
并且因为我只需要div
我尝试过:
instance Integral NT where
(NT x) `div` (NT y) = (NT q) where (q,r) = divMod x y
或者我可以这样做吗?
instance Integral NT where
div (NT x) (NT y) = NT (div x y )
但无论哪种方式我都会得到错误:
• No instance for (Real NT)
arising from the superclasses of an instance declaration
• In the instance declaration for ‘Integral NT’
我应该首先创建类NT
的实例Real
吗?如何创建?
解决方案
推荐阅读
- docker - 如何在 docker compose 中创建 docker 卷
- flutter - 在按钮按下颤动时增加文本大小
- c# - 将嵌套对象发送到 asp.net core Get 方法
- javascript - 如何在发布之前对表单数据进行分组
- mysql - 检查数组中的任何值是否与 json 列中的任何值匹配
- r - 如何基于 R 中的多个变量创建数据表?
- mysql - 将笑脸从 mssql 迁移到 mysql
- java - Java 运行时执行 SQL 脚本继续 SQL 错误
- java - Windows 10 上的 Hadoop-2.8.0 安装
- angular - Angular 7, implementing Jstree