如何阅读以下的haskell定义“ Num(a→b)=> [a] - > [b]“ [英] How to read the following haskell definition " Num (a -> b) => [a] -> [b] "
问题描述
我试图理解下面的类型
Prelude> :t map(10 $)
map(10 $):: Num(a - > b)=> [a] - > [b]
例如 Num(a-> b)
?可以更一般地回答:如何理解 Num SomeCompoundType
?
非常简单,这种约束意味着您尝试将某个类型视为数字类型,尽管它不是数字类型。这种类型只是假的,尽管它不是一个类型错误(理论上,有人可以添加一个 Num(a - > b)
例如)。
特别是,<数字$ c>数字,视为数字通常意味着您写了一个数字字面在某个地方,例如预计会发挥功能。 (10 $)
部分希望 10
是一个函数。由于Haskell数字文字是多态的,本质上没有什么不妥: 10
could 是一个函数,只要它具有合适的 instance Num(a - > b)
。只是,这个实例不存在,但是因为有些人原则上可以在以后添加它,所以编译器将此视为一个约束条件。
I am trying to understand below type
Prelude> :t map(10$)
map(10$) :: Num (a -> b) => [a] -> [b]
How to understand a constraint like Num (a->b)
? can be answered more generally: how to understand Num SomeCompoundType
?
Quite simple, such a constraint means that you tried to treat a type as a number type though it is not a number type. Such a type is just bogus, though it is not a type error (and theoretically, somebody could add a Num (a -> b)
instance).
With Num
in particular, "treated as a number" usually just means that you wrote a number literal in a place where e.g. a function is expected. The section (10$)
wants 10
to be a function. Because Haskell number literals are polymorphic, there is per se nothing wrong with that: 10
could be a function if only it had the suitable instance Num (a -> b)
. Just, this instance does not exist, but because somebody could in principle yet add it later, the compiler leaves this as a constraint-to-be-fulfilled.
这篇关于如何阅读以下的haskell定义“ Num(a→b)=> [a] - > [b]“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!