`Num a =>是什么?在Haskell类型系统中意味着什么? [英] What does `Num a => a` mean in Haskell type system?

查看:76
本文介绍了`Num a =>是什么?在Haskell类型系统中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我强迫Haskell推断数字的类型,我会得到Num a => a.例如:

If I force Haskell to infer the type of a number I'll get Num a => a. For example:

Prelude> :t 1
1 :: Num a => a

但是a => a是什么意思?

推荐答案

1 :: Num a => a表示1具有某种类型a,其中aNum类型类的实例.请注意,Num不是类型,而是类型类,它描述了各种类型的通用属性.例如,Num类型类描述数字类型,因此支持基本算术.本地机器整数类型IntNum的实例,任意大小的Integer,浮点类型Double甚至有理数类型Rational都是如此.

1 :: Num a => a means that 1 has some type a, where a is an instance of the Num typeclass. Note that Num is not a type, but a typeclass, which describes common properties of various types. The Num typeclass, for example, describes types that are numeric, and so support basic arithmetic. The native machine integer type Int is an instance of Num, as is the arbitrary-sized Integer, the floating point type Double, and even the rational number type Rational.

这篇关于`Num a =>是什么?在Haskell类型系统中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆